如何从具有多个TTF文件的字体家族中导入自定义的java.awt.Font? (包括一个例子) [英] How to import a custom java.awt.Font from a Font family with multiple TTF files? (An example is included)

查看:584
本文介绍了如何从具有多个TTF文件的字体家族中导入自定义的java.awt.Font? (包括一个例子)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  File file = new File(fontPath) ; 
Font font = Font.createFont(Font.TRUETYPE_FONT,file);
//替代:
// Font font = Font.createFont(Font.TRUETYPE_FONT,new FileInputStream(file));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);

然后你可以使用 font.deriveFont(Font.PLAIN,20 ); 可获得所需的样式和尺寸。






示例 Consolas 的例子,那么你有四个TTF文件:

$ b $ li $ consola.ttf (普通)

  • consolab.ttf (粗体)
  • consolai.ttf / li>
  • consolaz.ttf (粗体和斜体)


    当然,你可以用上面描述的方法导入 consola.ttf ,然后使用 font.deriveFont(Font.BOLD, 20); 与使用 consolab.ttf 不一样,因为普通的字体只是简单的转换成 一个粗体的字体。

    $ hr
    $ b

    示例图片


    1. 在这里,我用新的Font( Consolas,Font.PLAIN,20); new Font(Consolas,Font.BOLD,20); 注意,如果字体安装在系统上,如果你使用 deriveFont(Font.BOLD); ),你也可以得到正确的粗体字体:


    1. 这是 consola.ttf ,与 createFont 和派生的粗体字体(都与上面的例子一样,大小为20):






    安装好以后不是问题,但我不希望别人有自定义的Font,所以我想把TTF放到jar文件中,这样我就可以在初始化通过 getResourceAsStream(路径)



    有没有办法导入所有相关的TTF,然后调用新的字体(自定义字体名称,fontStyle,fontSize); 这样它就像一个已安装的字体(图片1),并且看起来不像派生'假'粗体字体(图片2)?

    解决方案

    我不确定究竟是什么问题。你得到了所有的TTF文件,你必须导入并注册它们。下面的测试使用了我的系统中没有安装的DejaVu Sans字体。

    $ h
    $ b

    测试1 / h1>

      Font f = Font.createFont(Font.TRUETYPE_FONT,new File(dvs.ttf)); 

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(f);

    字体dvs = new Font(DejaVu Sans,Font.PLAIN,20);

    这里有一个图片( dvs )和 bold dvs.deriveFont(Font.BOLD))字体。



    测试2



      Font f = Font.createFont(Font.TRUETYPE_FONT,new File(dvs.ttf)); 
    Font fb = Font.createFont(Font.TRUETYPE_FONT,new File(dvsb.ttf));

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(f);
    ge.registerFont(fb);

    字体dvs = new Font(DejaVu Sans,Font.PLAIN,20);
    Font dvsb = new Font(DejaVu Sans,Font.BOLD,20);

    这里有一个图像( dvs )以及真正的粗体( dvsb )字体。



    您可以通过查看来确认是否使用了正确的文件 font2DHandle









    我也测试了斜体 斜体 工作以及 Font#createFont(int,InputStream)方法。

    上面的方法是可行的,因为字体被映射他们的全名(例如Arial,Arial Bold等),只要你的字体名字正确,你就可以注册一个家庭的多个成员。

    I know that you can import a Font in Java with something like this:

    File file = new File(fontPath);
    Font font = Font.createFont(Font.TRUETYPE_FONT, file);
    // alternative:
    // Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(file));
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(font);
    

    Then you would use for example font.deriveFont(Font.PLAIN, 20); to get the desired style and size.


    Example

    But now let's look as an example at the font Consolas, there you have four TTF files:

    • consola.ttf (Plain)
    • consolab.ttf (Bold)
    • consolai.ttf (Italic)
    • consolaz.ttf (Bold & Italic)

    Of course you could just import consola.ttf with the method stated above, but then using font.deriveFont(Font.BOLD, 20); isn't the same as using consolab.ttf because the plain font was simply transformed to look like a bold font.


    Example Pictures

    1. Here I used the installed font with new Font("Consolas", Font.PLAIN, 20); and new Font("Consolas", Font.BOLD, 20); (as a side note, if the font is installed on the system you also get the right bold font if you use deriveFont(Font.BOLD);):

    1. And this is consola.ttf, imported with createFont and derived bold font (both with size 20 like the example above):


    Well when installed it isn't a problem, but I don't expect others to have a custom Font, so I want to put the TTFs into the jar file, so that I can import them during the initialization via getResourceAsStream(path).

    Is there a way to import all relevant TTFs and then just call new Font("Custom Font Name", fontStyle, fontSize); so that it's used like an installed font (Picture 1), and that it doesn't looks like a derived 'fake' bold font (Picture 2)?

    解决方案

    I'm not sure what exactly is the problem. You got all your TTF files and you have to import and register them. Following tests use DejaVu Sans fonts which are not installed on my system.


    Test 1

    Font f = Font.createFont(Font.TRUETYPE_FONT, new File("dvs.ttf"));
    
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(f);
    
    Font dvs = new Font("DejaVu Sans", Font.PLAIN, 20);
    

    Here's an image with plain (dvs) and derived bold (dvs.deriveFont(Font.BOLD)) font.

    Test 2

    Font f = Font.createFont(Font.TRUETYPE_FONT, new File("dvs.ttf"));
    Font fb = Font.createFont(Font.TRUETYPE_FONT, new File("dvsb.ttf"));
    
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(f);
    ge.registerFont(fb);
    
    Font dvs = new Font("DejaVu Sans", Font.PLAIN, 20);
    Font dvsb = new Font("DejaVu Sans", Font.BOLD, 20);
    

    And here's an image with plain (dvs) and truly bold (dvsb) font.

    You can confirm that correct file is used by looking at font2DHandle.


    I also tested italic and bold italic and both worked as well as Font#createFont(int, InputStream) method.

    Above approach works because fonts are mapped by their full name (e.g. Arial, Arial Bold etc.), so as long as your fonts are correctly named you can register multiple members of one family.

    这篇关于如何从具有多个TTF文件的字体家族中导入自定义的java.awt.Font? (包括一个例子)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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