使用java渲染TrueType字体中突出显示的字符在某些系统上看起来是错误的

时间:2013-08-23 15:37:42

标签: java fonts awt true-type-fonts

我遇到了将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 部分坐标是不同的,但只是在某种程度上,它只看起来像舍入错误而且我的眼睛无法察觉:

    < / LI>
  • ¨部分看起来非常奇怪,点坐标的差异远大于舍入误差。

见下图:

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

如果您在此之前已经阅读过,那么您已经感谢了我们。)

2 个答案:

答案 0 :(得分:2)

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

使用用于跨平台使用的现代字体,以及实际允许重新分发和衍生产品的许可(请参阅DejaVu或Google字体库)。你的形状衍生物。

OSX上的旧java由Apple维护,可能使用Apple特有的字体引擎。 其他系统上的旧Java(与Oracle下载一样)使用专有字体引擎。 OpenJDK使用freetype(甲骨文不敢改变官方的jdk,因为害怕破坏依赖于旧引擎疣的应用程序。遗憾的是,freetype 维护良好)。

期望所有人都能得到完全相同的塑形效果,这是徒劳的。而且字体越复杂和越旧,它们就越有可能发散(旧字体包括旧元数据新字体不会,并且取决于字体引擎,它会尝试使用这些旧数据做某事。或者不是。字体格式甚至包括将根据所使用的字体引擎读取或忽略的Apple和Windows特定元数据。

智能字体格式很精彩......

答案 1 :(得分:0)

nim的回答引导我们前进,所以我接受了它。

我只会在这里记录我们在调查中发现的内容。

通过一些黑盒测试,我们意识到使用更大的字体大小时问题就会消失。然后,使用非常大的字体大小(25000px),我们得到字形的上部点得到负坐标,看起来像整数溢出副作用。

我没有试过找到字体引擎的源代码,但是通过黑盒测试,我可以怀疑我们的java版本中使用的字体引擎在从字体中读取字形时使用整数使用大字体时使用小字体大小和溢出错误时出错。

为了解决我们的问题并尽可能少地更改我们的生产代码,我们决定始终加载字体大小为100px的字体,然后使用AffineTransform将Shape缩放到所需的大小。

我们没有改变nim的答案所建议的字体,因为我们试图尽可能少地修复,但我们不记得msttcore字体有更好的替代品(Google Fonts lib是一个)(顺便说一句,我们也有谷歌字体的舍入错误,即使比msttcore字体少一些))。