lcpsky

导航

JavaFX字库精简项目开发(二)—— sfnttool.jar精简字库

项目简介

JavaFX是用于构建富互联网应用程序的Java库。使用JavaFX开发的应用程序可以在各种设备上运行,如台式计算机,手机,物联网设备,平板电脑等。最近为了巩固一下JavaFX学习成果,准备利用整个技术开发一个工具软件能够对字库进行裁剪,可以根据用户的设置自动生成精简字库,简化手动裁剪的麻烦。

技术准备

sfnttool.jar(google字体精简工具库)

如何使用sfnttool.jar精简字库

详细使用见:https://blog.csdn.net/fitAllEnv/article/details/109167207?spm=1001.2014.3001.5501

业务编码

package sample;

import com.google.typography.font.tools.sfnttool.SfntTool;

import java.io.File;
import java.io.IOException;

/**
 * @author: Administrator
 * @date: 2021/03/13 21:37
 * @description:
 */
public class FontMakeUtil {
    static File file = null;
    /**
     * 功能描述: <br>
     * 〈将words字符串中字符创建为一个字库,fileSrc为完整字库绝对路径,fileDes为精简字库绝对路径〉
     * @Param: [words, fileSrc, fileDes]
     * @Return: void
     * @Author: Administrator
     * @Date: 2021/03/15 21:24
     */
    public static void convertToSmallFontFile(String words,String fileSrc,String fileDes) {
        boolean isExist = (file = new File(fileDes)).exists();
        if(isExist){
            file.delete();
        }
        String[] args = new String[4];
        args[0] = "-s";
        args[1] = words;
        args[2] = fileSrc;
        args[3] = fileDes;
        try {
            SfntTool.main(args);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

测试

public static void fontConvert(String words){
        String fileSrc = getBasicFilePath()+File.separator+"msyh.ttf";
        File file = new File(fileDes);
        if(file.exists()){
            file.delete();
        }
        FontMakeUtil.convertToSmallFontFile(words, fileSrc, fileDes);
    }

结果展示

在这里插入图片描述
在这里插入图片描述

使用工具类进行字体精简,可以得到msyh_simplify.ttf仅有九十多Kb,而源文件为14.3mb。

总结

以上完成了核心功能的编写,然后可以组装到javaFX快速开发cs工具脚手架中,实现日常操作的自动化操作。

posted on 2021-03-15 21:33  lcpsky  阅读(236)  评论(0编辑  收藏  举报