在 Linux 上找不到字体名称

发布于 2024-08-07 14:30:06 字数 1498 浏览 2 评论 0原文

在 Java 中,我的代码可以在 OSX 上运行良好,但不能在 Linux 上运行。此代码加载字体文件并使用 Font.createFont()。 代码如下:

log.debug("Loading ttf file AmericanTypewriter.ttf");
InputStream americanTypewriterInputStream = MyClass.class.getClassLoader().getResourceAsStream("AmericanTypewriter.ttf");
log.debug("File AmericanTypewriter.ttf loaded");
Font americanTypewriter = Font.createFont(Font.TRUETYPE_FONT, americanTypewriterInputStream);
log.debug("Font created");
americanTypewriter = americanTypewriter.deriveFont(16f); // Font size 16
log.debug("Font sized at 16");

如前所述,在 OSX 上该方法运行良好,但在 Linux 上则失败。 实际的 ttf 文件是我在 Mac 上使用以下命令提取的:

fondu /Library/Fonts/AmericanTypewriter.dfont 

并获取生成的 AmericanTypewriter.ttf 文件并将其添加到 java 资源路径中。

我预计这也能在 Linux 上工作,因为没有假设该字体已预先安装在主机上(我以编程方式添加它),但我可能错过了一些东西......你能帮忙吗?

日志如下所示:

11:30:59,418 DEBUG MyClass:167 - Loading ttf file AmericanTypewriter.ttf
11:30:59,419 DEBUG MyClass:167 - File AmericanTypewriter.ttf loaded
java.awt.FontFormatException: Font name not found
  at sun.font.TrueTypeFont.init(TrueTypeFont.java:437)
  at sun.font.TrueTypeFont.<init>(TrueTypeFont.java:154)
  at sun.font.FontManager.createFont2D(FontManager.java:1476)
  at java.awt.Font.<init>(Font.java:454)
  at java.awt.Font.createFont(Font.java:761)
  ...

编辑: 我这里一定缺少什么东西。通过告诉 Java“看,这是 ttf 文件,它包含您需要的所有信息”,这是否意味着它是独立于平台的,并且安装什么字体和安装在哪里并不重要? ttf 文件中没有包含 java 所需的所有内容吗?

In Java I have code that works well on OSX but not in linux. This code loads a font file and uses Font.createFont().
Here's the code:

log.debug("Loading ttf file AmericanTypewriter.ttf");
InputStream americanTypewriterInputStream = MyClass.class.getClassLoader().getResourceAsStream("AmericanTypewriter.ttf");
log.debug("File AmericanTypewriter.ttf loaded");
Font americanTypewriter = Font.createFont(Font.TRUETYPE_FONT, americanTypewriterInputStream);
log.debug("Font created");
americanTypewriter = americanTypewriter.deriveFont(16f); // Font size 16
log.debug("Font sized at 16");

As mentioned, on OSX this works well, but fails on linux.
The actual ttf file was extracted by me on a mac using:

fondu /Library/Fonts/AmericanTypewriter.dfont 

and grabbing the resulting AmericanTypewriter.ttf file and adding it to the java resource path.

I expected this to work on linux as well, since there's no assumption that the font is pre-installed on the host (I'm adding it programatically), but I might have missed something... Can you help?

The log looks like this:

11:30:59,418 DEBUG MyClass:167 - Loading ttf file AmericanTypewriter.ttf
11:30:59,419 DEBUG MyClass:167 - File AmericanTypewriter.ttf loaded
java.awt.FontFormatException: Font name not found
  at sun.font.TrueTypeFont.init(TrueTypeFont.java:437)
  at sun.font.TrueTypeFont.<init>(TrueTypeFont.java:154)
  at sun.font.FontManager.createFont2D(FontManager.java:1476)
  at java.awt.Font.<init>(Font.java:454)
  at java.awt.Font.createFont(Font.java:761)
  ...

EDIT:
There must be something I'm missing here. By telling Java "look, here's the ttf file, it has all information you need in it" doesn't that mean that it's platform independent and it really doesn't matter what fonts are installed and where?
Does the ttf file not have all that java needs in it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

摘星┃星的人 2024-08-14 14:30:06

为了回答我自己的问题 - 这是部分答案 - 我认为问题在于将字体从我的 Mac 转换到 Linux 盒子。我不清楚为什么会这样,但我在 Linux 机器上尝试了使用从网上下载的其他随机字体的相同代码,效果很好,只是这种字体让我很困难。
无需在盒子上实际安装字体。如果像我一样将字体文件交给java程序,那就足够了。

真正困扰我的是,我期望 Java 是自包含的,并且在主机 x 上运行且所有资源都交给它的 java 程序在主机 y 上运行时,当将相同的资源交给它时,它应该以相同的方式运行。我想对 linux 盒子有一个隐藏的依赖,我只是不清楚。

谁能提供更好的答案吗?

To sort of answer my own question - this is a partial answer - I think the problem is with converting the font from my mac to the linux box. I'm not clear why this is, but I tried the same code on the linux box with other random fonts I downloaded from the web and it worked OK, it's just this font that's giving me hard time.
There's no need to actually install fonts on the box. If the font file is handed to the java program like I do, that's all it needs.

What really troubles me is that I expected Java to be self contained and a java program that runs on host x with all resources handed to it should run the same way on host y when the same resources are handed to it. I suppose there's a hidden dependency on the linux box that is just not clear to me.

Can anyone provide a better answer?

治碍 2024-08-14 14:30:06

americanTypewriterInputStream 添加到日志消息中。也许它是null

如果情况并非如此,则该文件可能已损坏。尝试使用其他工具(字体安装程序/查看器,例如 kfontview)打开它。

Add americanTypewriterInputStream to the log message. Maybe it's null.

If that is not the case, then the file might be corrupt. Try to open it with some other tool (a font installer/viewer like kfontview).

污味仙女 2024-08-14 14:30:06

我们正在谈论哪个版本的 Linux?

在 Linux 中包含 TTF 字体的想法是最近才出现的。

例如,在 Ubuntu 中,TTF 字体的安装是在安装操作系统之后完成的。

如果您搜索有关“安装 Ubuntu 后要做的事情”的内容,您可能会找到有用的建议,

我不确定这是否适用于 Java,以及“American Typewriter”是否是安装后步骤提供的 TTF 字体之一。

编辑:它可能不是特别相关,但是 本页讨论了从Emacs 的角度如何通过全新安装“恢复”丢失的字体。

Which version of Linux are we talking about?

The idea of including TTF fonts with Linux is fairly recent.

In Ubuntu, for example, installation of TTF fonts is done after installation of the OS.

You might find useful advice if you search for something about "Things to do AFTER installing Ubuntu"

I'm not sure if this would work with Java, and if 'American Typewriter' would be one of the TTF fonts provided by post-installation steps.

EDIT: It may not be particularly relevant, but this page discusses how missing fonts from the point of view of Emacs were 'recovered' by a fresh install.

四叶草在未来唯美盛开 2024-08-14 14:30:06

根据您放置文件的位置,您需要通过执行来重建字体缓存

sudo fc-cache -f -v

要让 ubuntu 确实看到字体,您可能希望将其放入 /usr/share/fonts/truetype/然后重建字体缓存。如果我没记错的话,你也可以将一些字体放入 ~/.fonts/ 中。

Depending on where you put the file, you would need to rebuild the font-cache by executing

sudo fc-cache -f -v

To have ubuntu see the font for sure, you might wanna put it into /usr/share/fonts/truetype/ and then rebuild the font-cache. If I remember correctly, you can put some fonts into ~/.fonts/ as well.

花间憩 2024-08-14 14:30:06

我们的系统管理员已将 java 更新为
java版本“1.6.0_39”
OpenJDK 运行时环境 (IcedTea6 1.13.11) (rhel-1.13.11.1.el6_8-x86_64)
OpenJDK 64 位服务器 VM(内部版本 23.25-b01,混合模式


Linux 版本 2.6.32-573.7.1.el6.x86_64 ([电子邮件受保护]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Thu Sep 10 13:42:16 EDT 2015

我通过使用 yum 安装这些解决了类似的问题

dejagnu.noarch                                         
dejavu-sans-mono-fonts.noarch                          
dejavu-serif-fonts.noarch                              

Our sysadmins had updated java to
java version "1.6.0_39"
OpenJDK Runtime Environment (IcedTea6 1.13.11) (rhel-1.13.11.1.el6_8-x86_64)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)

on
Linux version 2.6.32-573.7.1.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Thu Sep 10 13:42:16 EDT 2015

I got around a similar problem by installing these with yum

dejagnu.noarch                                         
dejavu-sans-mono-fonts.noarch                          
dejavu-serif-fonts.noarch                              
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文