获取字体文件作为File对象或获取其路径 [英] Get font file as a File object or get its path

查看:135
本文介绍了获取字体文件作为File对象或获取其路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字体文件在Java中的Font对象。我需要将该对象转换为File对象或获取字体文件路径。

有没有办法做到这一点?






我在这里做的是从一个外部库调用一个方法来加载一个字体文件来使用它:

  loadTTF(PDDocument pdfFile,File fontfile); 

所以我想让用户从操作系统中定义的字体中选择一种字体:

  GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
Font [] fonts = e.getAllFonts();

然后,当用户选择字体时,我将它传递给 loadTTF ...)方法加载它。



这里有一个不好的做法吗?

 字符串fontFilePath = FontManager.getFontPath(true)+/+ FontManager.getFileNameForFontName(fontName); 

我已经在Windows和Linux中尝试了这种方式,并且都能正常工作。 $ b

I have a Font object in Java for a font file. I need to convert that object to a File object or get the font file path.

Is there a way to do this?


What I'm doing here is calling a method from an external library that loads a font file to use it in writing:

loadTTF(PDDocument pdfFile, File fontfile);

So I wanted to let the user choose a font from the ones defined in his operating system using :

GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = e.getAllFonts();

Then when the user chooses a font, I pass it to the loadTTF(...) method to load it.

Is there a bad practice here?

解决方案

Ok ... This will return the font file path :

String fontFilePath = FontManager.getFontPath( true ) + "/" + FontManager.getFileNameForFontName( fontName );

I have tried this in Windows and Linux and in both it worked fine.

这篇关于获取字体文件作为File对象或获取其路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆