Java:获取具有特定高度(以像素为单位)的字体
使用 FontMetrics
可以轻松确定字体的渲染高度,但反过来又如何呢?如何获得适合特定高度(以像素为单位)的字体?
“给我
Verdana
,其尺寸从上升到下降的高度为 30 像素。”
我如何向 Java 请求此信息?
It’s easy to determine the rendered height of a font using FontMetrics
, but what about the other way around? How can I obtain a font that will fit into a specific height in pixels?
"Give me
Verdana
in a size that is 30 pixels high from ascender to descender."
How do I ask Java for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这是一个非常古老的问题,但有人可能仍然会找到它:
Java(和许多其他地方)中的字体高度以“印刷点”给出,其定义为大约 1/72 英寸。
计算某个特定点所需的点像素高度,你应该能够使用以下内容:
我还没有对此进行广泛的测试,但它似乎适用于我使用过的显示器。如果我发现它不起作用的情况,我会报告。
对于我使用过的标准系统字体,这会将大写字母的高度(即上升)设置为提供的像素大小。如果需要将 ascent+descent 设置为像素大小,可以使用 FontMetrics:
当然,某些特定字母的实际像素高度将取决于字母和使用的字体,所以如果你想确保你的“H”是一些确切数量像素高,您可能仍然想使用其他答案中提到的试错方法。请记住,如果您使用这些方法来获取要显示的每个特定文本的大小(如 @Bob 建议的那样),您最终可能会在屏幕上看到随机的字体大小混乱,其中出现类似“ace”的文本” 的字母会比“Tag” 大得多。为了避免这种情况,我会选择一个特定的字母或字母序列(“T”或“Tg”或其他)并将其固定为您的像素高度一次,然后使用从各处获得的字体大小。
I know this is a very old question, but someone might still find it:
The font height in Java (and many other places) is given in "typographic points", which are defined as roughly 1/72nd of an inch.
To calculate the points needed for a certain pixel height, you should be able to use the following:
I haven't tested this extensively yet, but it seems to work for the monitors that I've used. I'll report back if I ever find a case where it doesn't work.
For the standard system fonts I've used this with, this sets the height of a capital letter (i.e. the ascent) to the provided pixel size. If you need to set the ascent+descent to the pixel size, you can correct the value using the FontMetrics:
Of course, the actual pixel-height of some specific letters will depend on the letter and the font used, so if you want to make sure that your "H" is some exact number of pixels tall, you might still want to use the trial-and-error methods mentioned in the other answers. Just keep in mind that if you use these methods to get the size for each specific text you want to display (as @Bob suggested), you might end up with a random font-size-mess on your screen where a text like "ace" will have much bigger letters than "Tag". To avoid this, I would pick one specific letter or letter sequence ("T" or "Tg" or something) and fix that one to your pixel height once and then use the font size you get from that everywhere.
我不认为有一种“直接”的方法可以通过高度查找字体;只有一种间接的方式......通过循环遍历尺寸,并测试每个尺寸的高度<=所需的高度。
如果您只执行一次,只需循环遍历它们...如果您“即时”执行此操作,则进行二分搜索,它会更快。
I don't think there's a "direct" way to find a font by height; only an indirect way... by looping through the sizes, and testing the height of each is <= required height.
If you're doing this once, just loop through them... if you've doing it "on the fly" then do a binary search, it'll be quicker.
我不知道如何通过字体的实际高度(以像素为单位)获取字体。这取决于它所使用的上下文,因此可能没有比采样最佳匹配更短的方法了。应该很快就能找到比设计高度高或低的尺寸。这是执行此操作的示例方法:
I'm not aware of a way to get a font by its actual height in pixels. It depends on the context it's used in so there's probably no shorter way than to sample for the best match. It should be pretty quick to look for sizes up or down from the designed height. Here's an example method that does that:
WhiteFang34 的代码与以下返回特定字符串实际高度的方法结合使用非常有用。对于实时渲染来说,它可能有点慢,特别是对于大字体/字符串,我确信它可以进一步优化,但目前它满足我自己的需求,并且足够快,可以在后端进程中运行。
WhiteFang34's code is useful in combination with the following method that returns the actual height of a specific string. It might be a bit slow for real-time rendering, especially for large fonts/strings and I'm sure it can be further optimised, but for now it meets my own needs and is fast enough to run in a back-end process.