java 提取ttf指定文字样式

有没有哪位DL知道,用java怎么实现提取ttf字体文件部分字体样式,生成一个新的ttf给到前端使用。

https://ask.csdn.net/questions/1032323?spm=1005.2026.3001.5635&utm_medium=distribute.pc_relevant_ask_down.none-task-ask-2~default~OPENSEARCH~Rate-2-1032323-ask-7769039.pc_feed_download_top3ask&depth_1-utm_source=distribute.pc_relevant_ask_down.none-task-ask-2~default~OPENSEARCH~Rate-2-1032323-ask-7769039.pc_feed_download_top3ask

1.确保你的电脑已经安装了Java环境(能运行Java命令),这是必须的。

2.复制要提取的源字体(jz.ttf)到sfnttool所在目录下。

3.命令行进入到sfnttool所在目录下。(一个小技巧,在当前文件夹里按住Shift再右键,里面有个“在此处打开命令行”。)

4.输入下面的命令即可:

java -jar sfnttool.jar  -s "这是要提取的文字,单引号表示时不能有空格" haibaob.ttf haibao.ttf


5.新的TTF文件,有可能不能使用。测试在 Cocos Creator 中 不能使用(可能是因为 新的字体中缺少完整的配置信息。字体文件不能被编辑器正确加载导致的)

Download Font [ haibo_LABEL ] failed, using Arial or system default font instead

需要再 转换一下, 可以在线转换,
https://transfonter.org/, 选中 ttf 格式, 转换 -> 下载,
新的字体文件可以在Cocos Creator 中使用了

sfnttool 是谷歌开源项目 sfntly 内置的工具,他的做用是从一个字体文件中提取指定的文字,导出的字体中将只包含你须要的文字。

  1. 确保你的电脑已经安装了Java环境(能运行Java命令)。
  2. 命令行进入到sfnttool所在目录下。(一个小技巧,在当前文件夹里按住Shift再右键,里面有个“在此处打开命令行”。)google
  3. 输入下面的命令便可:
    java -jar sfnttool.jar  -s '这是一段测试文字' msyh.ttf msyh_simplify.ttf
    
    sfnttool.jar说明以下:
    java -jar sfnttool.jar -hsubset [-?|-h|-help] [-b] [-s string] fontfile outfileprototype font subsetter        -?,-help        print this help information        -s,-string       string to subset        -b,-bench        benchmark (run 10000 iterations)        -h,-hints        strip hints        -w,-woff         output woff format        -e,-eot  output eot format        -x,-mtx  enable microtype express compression for eot format
    
    msyh.ttf :字体库文件
    msyh_simplify.ttf:目标文件/输出文件
    代码:
     private static void process() throws Exception{
           String content = "猜你喜欢 主编力荐 限时 精选 品质 热门推荐 新书 包括完本 全部 连载";
        content = content.replaceAll("\\s*",""); //文字内容去除空格字符

        File toolFile = ResourceUtils.getFile("classpath:lib/sfnttool.jar"); //这种方法在linux下无法工作
        File fontFile = ResourceUtils.getFile("classpath:font/fzxbs_gbk.ttf"); //这种方法在linux下无法工作
        //jar文件的绝对路径
        String jarAbsolutePath = toolFile.getAbsolutePath();
        //字体库文件的绝对路径
        String fontAbsolutePath = fontFile.getAbsolutePath();
        //目标目录
        String target = "./base-font.ttf";
        //java -jar sfnttool.jar -s '这是一段测试文字' msyh.ttf msyh_simplify.ttf
        String params = " java -jar %s -s '%s' %s %s ";
        String command = String.format(params, jarAbsolutePath, content, fontAbsolutePath, target);
        log.info("执行命令 command = {} ", command);
        Process exec ;
        try {
            exec = Runtime.getRuntime().exec(command);
            int i = exec.waitFor(); //当前线程等待 0表示正常终止
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

文件对应目录截图

img


文件输出截图

img


在推荐一个GUI Java coding 文件对应截图 就是SfntToolGUI.jar

     private static void useGUI() throws Exception{

        File guiFile = ResourceUtils.getFile("classpath:lib/SfntToolGUI.jar"); //这种方法在linux下无法工作
        //jar文件的绝对路径
        String jarAbsolutePath = guiFile.getAbsolutePath();
        String command = " java -jar ".concat(jarAbsolutePath);

        Process exec ;
        try {
            exec = Runtime.getRuntime().exec(command);
            int i = exec.waitFor(); //当前线程等待 0表示正常终止

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
public static void main(String[] args) throws Exception {

        //process();
        useGUI();
    }