不同机器上的字体和大小 [英] Fonts and their sizes on different machines

查看:200
本文介绍了不同机器上的字体和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用java编写一个gui的PC程序,我正在使用Java Swing,问题是,对于给定的字体,给定的大小和样式,给定的字符串,我计算边界框(以像素为单位)有些机器,有一种可能性,在一些其他的机器上,为完全相同的字体和完全相同的字符串,计算出的边界框会变成不同的?



如果是可能的(这似乎是根据我的程序得到的结果),那么我怎样才能定义一个字体在给定的字符串,在每一个可能的机器,它将返回完全相同的边界框?

解决方案

你不能,因为渲染取决于几个因素:


  1. 字体本身:有些电脑可能没有你要求的字体,或者字体可能有相同的名字,但实质上却不同。您可以通过在可部署包中嵌入自定义字体来避免此情况。

  2. 屏幕分辨率:字体根据DPI中的屏幕分辨率进行渲染。在密度较高的屏幕上,字体会更大(像素大小),因为否则它们将无法阅读。例如,苹果公司的视网膜显示器的分辨率接近400dpi,与普通屏幕相比,分辨率为72dpi。一个高度为72px的字符串在普通屏幕上会占用1英寸的空间,完全可读,而只能占用0.18英寸,难以阅读。

  3. 用户有权自定义尺寸他的字体。如果我是老花眼,我想要一个更大的字体大小。

    $ 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:

    1. 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.
    2. 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.
    3. 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屋!

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