我正在尝试在我的 vaadin 应用程序中使用自定义图标字体。我遵循 this 中提供的指南页面。
我遇到的问题是为字体生成 java 枚举。
在这个页面上说,
(Note that you can easily generate the enum from the list of icons in the zip downloaded from IcoMoon.)
但我不知道如何使用我下载的 icomoon zip 文件生成此枚举。可能是我遗漏了页面假设其读者拥有的一些基本知识。
谁能告诉我如何生成图标枚举?
最佳答案
我遇到了同样的挑战并为此编写了一个类,它自动将 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");
}
}
关于java - 如何在 vaadin 中为自定义图标字体生成字体枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647013/