gpt4 book ai didi

java - 使用 Java 从 TrueType 字体渲染重音字符在某些系统上看起来是错误的

转载 作者:搜寻专家 更新时间:2023-11-01 03:24:31 25 4
gpt4 key购买 nike

我在将 TrueType 字体(例如 Arial)的字符转换为 java.awt.Shape 以进一步手动渲染时遇到问题(如 EPS,但这并不重要)。

我已将执行此操作的程序分解为多个小进程,以查明问题出在哪里,在我看来问题出在从字体加载字形的过程中。

我正在使用以下代码片段加载字体(Arial,来自 msttcore 包)并将字符 Ö 转换为我稍后可以使用的 Shape:

Font font = new Font("Arial", Font.PLAIN, 24);
AttributedString attributedString = new AttributedString("Ö");
attributedString.addAttribute(TextAttribute.FONT, font, 0, "Ö".length());
FontRenderContext fontRenderContext = new FontRenderContext(null, false, false);
TextLayout layout = new TextLayout(attributedString.getIterator(), fontRenderContext);
Shape shape = layout.getOutline(null);

我也尝试过使用以下代码片段,但它给了我相同的结果:

Font font = new Font("Arial", Font.PLAIN, 24);
FontRenderContext fontRenderContext = new FontRenderContext(null, false, false);
GlyphVector glyphVector = font.createGlyphVector(fontRenderContext, text);
Shape shape = glyphVector.getOutline();

之后,我使用 shape.getPathIterator(null) 并遍历它给我打印点坐标的线段。我在三个不同的系统上这样做:

  • Mac OS X Mountain Lion (10.8),我负责使用 Font() 构造函数,允许我指向正确的 arial.ttf 文件,以避免我的代码片段使用系统的内置 Arial 字体。
  • 一个看起来基于 Fedora 的 Amazon AWS linux 发行版
  • 基于 Ubuntu 服务器的 Amazon AWS linux 发行版

在我的 Mac 上生成 java.awt.Shape 时,生成的 EPS 文件看起来是正确的。在 Linux 机器上生成 java.awt.Shape 时,某些点坐标似乎与我在 Mac 上生成的坐标不同。

  • 'Ö' 的 O 部分坐标不同,但只是在某种程度上它看起来只是舍入误差,我的眼睛无法察觉:

  • 不过,¨ 部分看起来很奇怪,而且点坐标的差异远大于舍入误差。

见下图:

Both glyphs on the same picture

绿色是在我的 Mac 上生成的 Shape 的路径,红色是在类似 Fedora 的计算机上生成的 Shape 的路径。

因为两者在 O 部分重叠得很好,所以它看起来有点深绿色。但是你可以看到 ¨ 部分非常不同。它甚至不以红色路径为中心...

我的实验总结:

  • Java 版本/发行版看起来不会影响该问题。
  • 这似乎是所有重读字符(到目前为止:ÖÄÅÜ)的普遍问题。
  • 我对 Times New Roman 字体也有同样的问题。

我想我已经尝试过很多事情都没有成功,我真的不明白为什么会这样,如果有任何提示,我将不胜感激。


这是我能给你的最小的完整代码片段,它显示了问题:

package Experiments;

import java.awt.Font;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.PathIterator;
import java.io.File;
import java.text.AttributedString;

public class MyClass
{
public static void main(String[] args) throws Exception
{
Font font = new Font("Arial", Font.PLAIN, 24);
AttributedString attributedString = new AttributedString("Ö");
attributedString.addAttribute(TextAttribute.FONT, font, 0, "Ö".length());
FontRenderContext fontRenderContext = new FontRenderContext(null, false, false);
TextLayout layout = new TextLayout(attributedString.getIterator(), fontRenderContext);
Shape shape = layout.getOutline(null);
PathIterator it = shape.getPathIterator(null);
double[] points = new double[6];
double x = 0, y = 0;

while (!it.isDone())
{
double x1 = points[0], y1 = points[1];
double x2 = points[2], y2 = points[3];
double x3 = points[4], y3 = points[5];

switch (it.currentSegment(points))
{
case PathIterator.SEG_CLOSE:
System.out.println("close");
break;
case PathIterator.SEG_QUADTO:
// Convert to cubic curve
x3 = x2;
y3 = y2;
x2 = x1 + 1 / 3f * (x2 - x1);
y2 = y1 + 1 / 3f * (y2 - y1);
x1 = x + 2 / 3f * (x1 - x);
y1 = y + 2 / 3f * (y1 - y);
case PathIterator.SEG_CUBICTO:
System.out.println("curve: " + x1 + "," + y1 + "," + x2 + "," + y2 + "," + x3 + "," + y3);
x = x3;
y = y3;
break;
case PathIterator.SEG_LINETO:
System.out.println("lineto: " + x1 + "," + y1);
x = x1;
y = y1;
break;
case PathIterator.SEG_MOVETO:
System.out.println("moveto: " + x1 + "," + y1);
x = x1;
y = y1;
break;
}
it.next();
}
}
}

我的 Mac 上的输出是:

moveto: 0.0,0.0
curve: 0.7734375230502337,-5.57812516624108,0.7734374884748831,-5.57812491687946,0.0,0.0
curve: 0.7734375230502337,-8.429687751224265,1.925781272817403,-13.451171899039764,3.45703125,-15.064453125
curve: 4.988281295634806,-16.677734423079528,6.96484378608875,-17.484375,9.38671875,-17.484375
curve: 10.97265629726462,-17.484375,12.402343768975697,-17.105468738707714,13.67578125,-16.34765625
curve: 14.949218787951395,-15.589843727415428,15.91992188495351,-14.533203104801942,16.587890625,-13.177734375
curve: 17.25585939490702,-11.822265584603883,17.58984375,-10.28515622438863,17.58984375,-8.56640625
curve: 17.58984375,-6.8242186980787665,17.23828123952262,-5.265624979510903,16.53515625,-3.890625
curve: 15.832031229045242,-2.5156249590218067,14.835937480791472,-1.4746093644644134,13.546875,-0.767578125
curve: 12.257812461582944,-0.06054685392882675,10.867187477764674,0.29296875,9.375,0.29296875
curve: 7.757812451804057,0.29296875,6.3124999810243025,-0.09765626164153218,5.0390625,-0.87890625
curve: 3.765624962048605,-1.6601562732830644,2.800781240221113,-2.7265625201398507,2.14453125,-4.078125
close
moveto: 1.16015625,-6.10546875
curve: 2.7226562965661287,-7.589843794237822,2.7226562267169356,-8.343750000349246,1.16015625,-8.3671875
curve: 2.7226562965661287,-6.2734374376013875,4.060546891589183,-4.630859357246663,5.173828125,-3.439453125
curve: 6.287109408178367,-2.248046839493327,7.683593775029294,-1.65234375,9.36328125,-1.65234375
curve: 11.074218800989911,-1.65234375,12.482421891472768,-2.2539062679279596,13.587890625,-3.45703125
curve: 14.693359407945536,-4.660156285855919,15.24609375,-6.367187532945536,15.24609375,-8.578125
curve: 15.24609375,-9.976562541676685,15.009765617956873,-11.197265640541445,14.537109375,-12.240234375
curve: 14.064453110913746,-13.283203156082891,13.373046861437615,-14.091796883556526,12.462890625,-14.666015625
curve: 11.55273434787523,-15.240234392113052,10.531249983119778,-15.52734375,9.3984375,-15.52734375
curve: 7.789062452036887,-15.52734375,6.404296857712325,-14.974609358527232,5.244140625,-13.869140625
close
moveto: 3.50390625,-12.2109375
lineto: 6.046875,-18.234375
lineto: 6.046875,-20.63671875
lineto: 8.25,-20.63671875
close
moveto: 8.25,-18.234375
lineto: 10.41796875,-18.234375
lineto: 10.41796875,-20.63671875
lineto: 12.62109375,-20.63671875
close

在类似 fedora 的计算机上:

moveto: 0.0,0.0
curve: 0.7708333563059568,-5.583333499729633,0.7708333218470216,-5.583333250135183,0.0,0.0
curve: 0.7708333563059568,-8.427083584479988,1.921875022817403,-13.447916690725833,3.453125,-15.0625
curve: 4.984375045634806,-16.677083381451666,6.963541702833027,-17.484375,9.390625,-17.484375
curve: 10.973958380520344,-17.484375,12.403645852347836,-17.106770822079852,13.6796875,-16.3515625
curve: 14.955729204695672,-15.596354144159704,15.92708334326744,-14.539062479743734,16.59375,-13.1796875
curve: 17.26041668653488,-11.820312459487468,17.59375,-10.28124997438863,17.59375,-8.5625
curve: 17.59375,-6.822916614823043,17.24218748952262,-5.265624979510903,16.5390625,-3.890625
curve: 15.835937479045242,-2.5156249590218067,14.838541647419333,-1.4739583227783442,13.546875,-0.765625
curve: 12.255208294838667,-0.05729164555668831,10.864583311136812,0.296875,9.375,0.296875
curve: 7.760416618548334,0.296875,6.315104147652164,-0.09375001164153218,5.0390625,-0.875
curve: 3.763020795304328,-1.6562500232830644,2.796874990221113,-2.723958353511989,2.140625,-4.078125
close
moveto: 1.15625,-6.109375
curve: 2.7187500465661287,-7.5885417107492685,2.7187499767169356,-8.343750000465661,1.15625,-8.375
curve: 2.7187500465661287,-6.2812499376013875,4.057291683275253,-4.638020815560594,5.171875,-3.4453125
curve: 6.286458366550505,-2.2526041311211884,7.682291691657156,-1.65625,9.359375,-1.65625
curve: 11.078125051222742,-1.65625,12.489583349786699,-2.2578125179279596,13.59375,-3.4609375
curve: 14.697916699573398,-4.664062535855919,15.25,-6.369791699573398,15.25,-8.578125
curve: 15.25,-9.973958374932408,15.013020826270804,-11.195312515599653,14.5390625,-12.2421875
curve: 14.065104152541608,-13.289062531199306,13.372395819751546,-14.098958341870457,12.4609375,-14.671875
curve: 11.549479139503092,-15.244791683740914,10.531249983236194,-15.53125,9.40625,-15.53125
curve: 7.791666618548334,-15.53125,6.4036458160262555,-14.979166650213301,5.2421875,-13.875
close
moveto: 3.5,-12.21875
lineto: 6.625,-18.0
lineto: 6.625,-20.390625
lineto: 8.828125,-20.390625
lineto: 8.828125,-18.0
close
moveto: 6.625,-18.0
lineto: 11.0,-18.0
lineto: 11.0,-20.390625
lineto: 13.203125,-20.390625
lineto: 13.203125,-18.0
close

如果你读到这里,我已经很感谢你了:)

最佳答案

你究竟为什么要为 Arial 而烦恼? “免费”的 msttcorefonts 版本是多年未收到修复程序的废弃软件。它针对不再存在的字体渲染系统进行了优化(如今众所周知,微软在这些文件中使用了错误的元数据值来解决其当时最前沿系统中的错误)。

使用旨在跨平台使用的现代字体,并使用实际上允许再分发和派生的许可(参见 DejaVu 或 Google 字体库)。您的形状衍生物。

OSX 上的旧 java 由 Apple 维护,可能使用 Apple 特定的字体引擎。其他系统上的旧 java(如 Oracle 下载)使用专有字体引擎。OpenJDK 使用 freetype(Oracle 不敢更改官方 jdk,因为害怕破坏依赖其旧引擎缺陷的应用程序。遗憾的是,freetype 维护得很好)。

期望所有这些都提供完全相同的塑形效果是徒劳的。字体越复杂、越旧,它们就越有可能发生分歧(旧字体包含旧元数据,新字体不包含,并且根据字体引擎,它会尝试对这些旧数据做一些事情,或者不做。字体格式甚至包括将根据使用的字体引擎读取或忽略的 Apple 和 Windows 特定元数据)。

智能字体格式很棒......

关于java - 使用 Java 从 TrueType 字体渲染重音字符在某些系统上看起来是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18406739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com