如何在 Java Web 应用程序中嵌入字体?
这个问题是通用的,但也与 Amazon Web Services 有关。
我正在尝试将 Java 应用程序部署到 Amazon 云服务器,但该应用程序无法启动,因为它使用 AWT 字体(请参阅以下内容)行):
TextImage testImage = new TextImageImpl(128, 32, new Margin(0, 8));
// Declare or read the fonts you need
Font header = new Font("Sans-Serif", Font.BOLD, 24);
// 1. specify font and write text with a newline
testImage.useFont(header).writeLine(text).newLine();
我遇到的问题是亚马逊的虚拟服务器没有安装正确的字体,因此您无法在应用程序中执行与图像/图表相关的操作。请参阅此了解更多信息。 他们的客户支持建议使用自定义 VM 映像(安装字体或安装 Oracle JDK,因为 Oracle JDK 附带一组字体),这对我来说不是一个干净的解决方案。
我的问题是: 我如何重写我的代码,或者以不同的方式打包它,以便它在全新安装的 Open JDK 上运行,而不需要额外的字体(如果可能的话)。
顺便说一句,这里是一部分我收到的错误:
2011 年 10 月 9 日上午 11:05:18 org.apache.catalina.core.StandardWrapperValve 为 servlet ZipGrocery 应用程序调用严重:Servlet.service() 抛出异常 java.lang.Error:可能的致命错误:找不到字体。 在 sun.font.FontManager.getDefaultPhysicalFont(FontManager.java:1088) 在 sun.font.FontManager.initialiseDeferredFont(FontManager.java:967) 在 sun.font.CompositeFont.doDeferredInitialization(CompositeFont.java:254) 在 sun.font.CompositeFont.getSlotFont(CompositeFont.java:334) 处 sun.font.CompositeStrike.getStrikeForSlot(CompositeStrike.java:77) 在 sun.font.CompositeStrike.getFontMetrics(CompositeStrike.java:93) 在 sun.font.FontDesignMetrics.initMatrixAndMetrics(FontDesignMetrics.java:358) 在 sun.font.FontDesignMetrics.(FontDesignMetrics.java:349) 在 sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:301) 在 sun.java2d.SunGraphics2D.getFontMetrics(SunGraphics2D.java:787) 在 nl.jamiecraane.imagegenerator.impl.TextImageImpl.getFontMetrics(TextImageImpl.java:219) 在 nl.jamiecraane.imagegenerator.impl.TextImageImpl.writeLine(TextImageImpl.java:212)
谢谢
This question is generic, but is also related to Amazon Web Services.
I am trying to deploy a Java application to an Amazon cloud server, but the application fails to start because it uses AWT fonts (see the following lines):
TextImage testImage = new TextImageImpl(128, 32, new Margin(0, 8));
// Declare or read the fonts you need
Font header = new Font("Sans-Serif", Font.BOLD, 24);
// 1. specify font and write text with a newline
testImage.useFont(header).writeLine(text).newLine();
The issue I am having is that Amazon's virtual servers don't have proper fonts installed and as a result you cannot do image/chart related stuff in your application. See this for more information.
Their customer support suggested using a custom VM image (with either fonts or Oracle JDK installed since Oracle JDK comes with a set of fonts) which is not a clean solution for me.
My question is:
How can I rewrite my code, or package it in a different way, so that it runs on a fresh installation of Open JDK with no additional fonts needed (if at all possible).
By the way, here is a part of the error I'm getting:
Oct 9, 2011 11:05:18 AM org.apache.catalina.core.StandardWrapperValve
invoke SEVERE: Servlet.service() for servlet ZipGrocery Application
threw exception java.lang.Error: Probable fatal error:No fonts found.
at sun.font.FontManager.getDefaultPhysicalFont(FontManager.java:1088)
at sun.font.FontManager.initialiseDeferredFont(FontManager.java:967)
at
sun.font.CompositeFont.doDeferredInitialisation(CompositeFont.java:254)
at sun.font.CompositeFont.getSlotFont(CompositeFont.java:334) at
sun.font.CompositeStrike.getStrikeForSlot(CompositeStrike.java:77) at
sun.font.CompositeStrike.getFontMetrics(CompositeStrike.java:93) at
sun.font.FontDesignMetrics.initMatrixAndMetrics(FontDesignMetrics.java:358)
at sun.font.FontDesignMetrics.(FontDesignMetrics.java:349) at
sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:301) at
sun.java2d.SunGraphics2D.getFontMetrics(SunGraphics2D.java:787) at
nl.jamiecraane.imagegenerator.impl.TextImageImpl.getFontMetrics(TextImageImpl.java:219)
at
nl.jamiecraane.imagegenerator.impl.TextImageImpl.writeLine(TextImageImpl.java:212)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有实际的字体,并且拥有适当的许可来分发,则可以打包、加载和注册该字体作为分布式应用程序的一部分。
我发现 OpenJDK 对于生产环境来说存在缺陷且不可靠。如果可以的话我会选择 Oracle 的 JDK。
If you had an actual font you had proper licensing to distribute, you could package, load, and register the font as part of your distributed application.
I've found OpenJDK to be buggy and unreliable for production environments. I'd just go with Oracle's JDK if you can.
Google Fonts API 怎么样?
How about Google Fonts API?