PDFBox 有个专门构造字体的 PDFontFactory 类。

PDFontFactory 会生成 PDTrueTypeFont。

PDTrueTypeFont 调用 FontMappers.instance().getTrueTypeFont() 来生成 TrueTypeFont。

FontMappers.instance() 返回的是 FontMapperImpl。

FontMapperImpl 通过 DefaultFontProvider.INSTANCE 也就是 FileSystemFontProvider 来获取系统内有哪些字体。

FileSystemFontProvider 再通过 FontFileFinder 来获取系统内有哪些字体。

FontFileFinder 里指定了搜索路径。

以上代码路径都没有提供设置、修改对应实例的接口。

因此,PDFBox 不能指定字体搜索路径,只能使用默认的系统字体路径。

相关代码:https://github.com/apache/pdfbox/blob/2.0.25/fontbox/src/main/java/org/apache/fontbox/util/autodetect/FontFileFinder.java#L45-L84

    private FontDirFinder determineDirFinder()
    {
        final String osName = System.getProperty("os.name");
        if (osName.startsWith("Windows"))
        {
            return new WindowsFontDirFinder();
        }
        else if (osName.startsWith("Mac"))
        {
            return new MacFontDirFinder();
        }
        else if (osName.startsWith("OS/400"))
        {
            return new OS400FontDirFinder();
        }
        else
        {
            return new UnixFontDirFinder();
        }
    }

    /**
     * Automagically finds a list of font files on local system.
     * 
     * @return List<URI> of font files
     */
    public List<URI> find()
    {
        if (fontDirFinder == null)
        {
            fontDirFinder = determineDirFinder();
        }
        List<File> fontDirs = fontDirFinder.find();
        List<URI> results = new ArrayList<URI>();
        for (File dir : fontDirs)
        {
            walk(dir, results);
        }
        return results;
    }

发表回复

您的电子邮箱地址不会被公开。