在 Google Web Toolkit 中使用自定义字体

ADP*_*123 5 java gwt

我想在 Google Web Toolkit Web 应用程序中使用自定义字体。我已尝试遵循https://code.google.com/p/gwt-webfonts/ 上提供的文档/代码,但我仍然遇到问题。我不完全明白我需要做什么。

我有:

  1. 在我的项目中包含 gwt-webfonts-0.1.jar 文件依赖项
  2. 向我的 gwt.xml 文件添加了一个包含依赖项:
  3. 创建了 ClientBundle 的扩展:

    import com.google.gwt.dev.javac.testing.impl.MockResource;
    import com.google.gwt.dev.resource.impl.FileResource;
    import com.google.gwt.resources.client.ClientBundle;
    import com.google.gwt.resources.client.ClientBundle.Source;
    import com.google.gwt.resources.client.CssResource;
    import com.google.gwt.core.client.GWT;
    
    public interface MainClientBundle extends ClientBundle
    {
       @Source("LesJoursHeureux.otf")
       public FontResource myFont();
    
    };
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在我的 ui.xml 文件中,我包含了对字体资源的引用:

    <ui:style type='com.example.TopMenuView.TopMenuViewStyle'>
        .maintitle {
            font-family: value('myFont.getFontName');
        }
    </ui:style>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 这是我不知道该怎么做的地方。在我的 TopMenuView.java 类中,我包含了这个片段:

    private static MainClientBundle MY_RESOURCES = GWT.create(MainClientBundle.class);
    {
        MY_RESOURCES.myFont().ensureInjected();
    }
    
    Run Code Online (Sandbox Code Playgroud)
  6. 当我尝试构建项目时,我收到此错误:

    Creating assignment for style()
         [java]  Performing substitution in node font-family : .....
         [java]  [ERROR] Could not find no-arg method named myFont in type com.example.TopMenuView_TopMenuViewUiBinderImpl_GenBundle
    
    Run Code Online (Sandbox Code Playgroud)

编辑:我使用的是 GWT SDK 的 2.6.0 版。

spg*_*spg 6

这是一个没有任何外部库的解决方案。

首先,DataResource在您的MainClientBundle.java:

@MimeType("application/font-sfnt") // use appropriate mime type depending on font file format
@Source("aller/aller_rg-webfont.ttf")
DataResource allerRegularTtf(); 
Run Code Online (Sandbox Code Playgroud)

DataResource使用 GSS (GWT >= 2.7.0) 在你的 css 文件中导入你的:

@def ALLER_REGULAR_TTF resourceUrl("allerRegularTtf");
Run Code Online (Sandbox Code Playgroud)

或使用经典的 CssResource (GWT < 2.7.0):

@url ALLER_REGULAR_TTF allerRegularTtf;
Run Code Online (Sandbox Code Playgroud)

在您的.css文件中,声明一个@font-face并使用它:

@font-face {
  font-family: "AllerRegular";
  src: ALLER_REGULAR_TTF format("truetype");
}

.myClass {
  font-family: "AllerRegular";
}
Run Code Online (Sandbox Code Playgroud)