【问题标题】:How to generate the font enum for a custom icon font in vaadin如何在 vaadin 中为自定义图标字体生成字体枚举
【发布时间】:2016-01-07 03:43:45
【问题描述】:

我正在尝试在我的 vaadin 应用程序中使用自定义图标字体。我正在遵循this 页面中提供的指南。

我遇到的问题是为字体生成 java 枚举。

在这个页面中它说,

(请注意,您可以轻松地从图标列表中生成枚举 从 IcoMoon 下载的 zip。)

但我不知道如何使用我下载的 icomoon zip 文件生成此枚举。可能是我错过了该页面假定读者拥有的一些基本知识。

谁能告诉我如何生成图标枚举?

【问题讨论】:

  • 您是否检查过您在问题中发布的链接的“3. 利用 Java 中的图标”部分?它提供了一个示例
  • @saljuama 是的,它提供了一个示例。但是为大图标集手动编写代码是不切实际的(页面说可以生成,但没有说明如何生成)。此外,该页面没有提到它从哪里获得枚举值。例如:RIBBON(0xe009)
  • 你可以从下载的zip里面的style.css文件中得到它的值,最后你会得到所有的十六进制代码。不管实用与否,恐怕导游说是必需的。
  • 如果你有几十个图标,也许适合编写一个小的css文件解析器并输出java代码,然后你可以将其包含在你的enum
  • Vaadin 项目为此使用了一个脚本:github.com/vaadin/vaadin/blob/master/scripts/…

标签: java vaadin


【解决方案1】:

我遇到了同样的挑战并为此编写了一个类,它会自动将 selection.js 从 IcoMoon zip 转换为完全与 Vaadin 兼容的 Java 枚举 ;-)

随意重复使用。它只需要来自https://code.google.com/archive/p/json-simple/downloads的json-simple jar

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JSONTools {

    public static void convertIcoMoonJSON2EnumClass(String filename) {

        JSONParser parser = new JSONParser();

        StringBuilder returnTxt = new StringBuilder();

        returnTxt.append("public enum IcoMoon implements FontIcon {\n\n");

        try {

            Object obj = parser.parse(new FileReader(filename));

            JSONObject jsonObject = (JSONObject) obj;
            // loop array
            JSONArray msg = (JSONArray) jsonObject.get("icons");
            Iterator<JSONObject> iterator = msg.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                JSONObject iterator_current = iterator.next();

                JSONObject icon = (JSONObject) iterator_current.get("icon");
                // System.out.println(icon);

                JSONArray tags = (JSONArray) icon.get("tags");
                returnTxt.append(((String) tags.get(0)).toUpperCase().replace("-", "_"));

                returnTxt.append("(");

                JSONObject properties = (JSONObject) iterator_current.get("properties");
                long code = (Long) properties.get("code");
                returnTxt.append(code);
                returnTxt.append(")");

                returnTxt.append(",\n");

                // System.out.println(icon);
            }
            returnTxt.append(";\n");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        returnTxt.append("\r\n" + 
                "   private final int codepoint;\r\n" + 
                "   // This must match (S)CSS\r\n" + 
                "   private final String fontFamily = \"IcoMoon\";\r\n" + 
                "\r\n" + 
                "   IcoMoon(int codepoint) {\r\n" + 
                "       this.codepoint = codepoint;\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "   @Override\r\n" + 
                "   public String getFontFamily() {\r\n" + 
                "       return fontFamily;\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "   @Override\r\n" + 
                "   public int getCodepoint() {\r\n" + 
                "       return codepoint;\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "   @Override\r\n" + 
                "   public String getHtml() {\r\n" + 
                "       return \"<span class=\\\"v-icon IcoMoon\\\">&#x\" + Integer.toHexString(codepoint) + \";</span>\";\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "   @Override\r\n" + 
                "   public String getMIMEType() {\r\n" + 
                "       // Font icons are not real resources\r\n" + 
                "       throw new UnsupportedOperationException(\r\n" + 
                "               FontIcon.class.getSimpleName() + \" should not be used where a MIME type is needed.\");\r\n" + 
                "   }\r\n" + 
                "\r\n" + 
                "}");

        System.out.println(returnTxt);

    }

    public static void main(String[] args) {
        convertIcoMoonJSON2EnumClass("path_to_your_selection.json");
    }

}

【讨论】:

  • 感谢您分享您的解决方案。我通过记事本++宏完成了这项工作。当我向我的字体添加更多图标时,我将使用您的解决方案。
猜你喜欢
  • 2019-06-10
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2013-12-28
  • 2015-05-29
相关资源
最近更新 更多