不同机器上的字体和大小 [英] Fonts and their sizes on different machines
问题描述
如果是可能的(这似乎是根据我的程序得到的结果),那么我怎样才能定义一个字体在给定的字符串,在每一个可能的机器,它将返回完全相同的边界框?
你不能,因为渲染取决于几个因素:
- 字体本身:有些电脑可能没有你要求的字体,或者字体可能有相同的名字,但实质上却不同。您可以通过在可部署包中嵌入自定义字体来避免此情况。
- 屏幕分辨率:字体根据DPI中的屏幕分辨率进行渲染。在密度较高的屏幕上,字体会更大(像素大小),因为否则它们将无法阅读。例如,苹果公司的视网膜显示器的分辨率接近400dpi,与普通屏幕相比,分辨率为72dpi。一个高度为72px的字符串在普通屏幕上会占用1英寸的空间,完全可读,而只能占用0.18英寸,难以阅读。
- 用户有权自定义尺寸他的字体。如果我是老花眼,我想要一个更大的字体大小。
$ b $ hr
$ b编辑
或者,你可以通过使用预渲染的字符串(保存为光栅图形,甚至SVG路径)来欺骗系统,但要注意我提出的问题。I am writing a gui PC program with java and I am using Java Swing, the question is, for a given Font, with a given size and style, on a given String, i compute the bounding box (in pixels) on some machine, is there a possibility that on some other machine, for the exact same Font and exact same String, the computed bounding box will turn out different?
if it is possible( which seems to be the case according to the results I get on my program ), then how can i define a font where for a given string, in every possible machine, it will return the exact same bounding box?
解决方案You can't, because the rendering depends on several factors:
- The font itself: some computers may not have the font you requested, or the font may have the same name, but be different in its substance. You can avoid this by embedding custom fonts in your deployable package.
- The screen resolution: fonts are rendered accordingly to the screen resolution in DPI. On screens with higher density, fonts will be bigger (in pixel size), because they'd otherwise be unreadable. Think, for example, of Apple's retina displays, whose resolution is close to 400dpi, compared to a normal screen, with a 72dpi resolution. A string which is 72px high will take 1 inch on the normal screen, being perfectly readable, while will only occupy 0.18 in, being hardly readable.
- The user has the right to customize the size of his fonts. If I'm presbyopic I'll want a bigger font size.
EDIT
Or, you can fool the system by using pre-rendered strings (saved as raster graphics or even as SVG paths), but beware of the issues I presented you.这篇关于不同机器上的字体和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!