Java如何对Path2D进行三角测量?

时间:2015-01-09 21:52:59

标签: java graphics bezier triangulation true-type-fonts

我正在开发一个应用程序,它解析TrueType字体中的字形数据并将其转换为多边形,以便使用OpenGL ES 2.0进行渲染。

我设法将字形数据转换为离散指令,例如MOVE_TOLINE_TOQUAD_TOCLOSE,类似于Java的Path2D类。然后我triangulate这些路径用于渲染。

我遇到的问题是我似乎以非标准的方式处理这些指令,因为我可以有效地渲染某些字体但却无法渲染其他字体,如下所示。经过一些测试后,它似乎是我在LINE_TOBEZIER_TO指令之间移动时使用的决策逻辑,但我找不到与这两种字体兼容的序列。

是否有关于Java的Path2D数据如何被三角化的在线文档?

1。正确渲染的字形('c')。

A correctly rendered character.

2。不正确渲染的字形,来自不同的字体('c')。

An incorrectly rendered character.

在下面的片段中,我选择了用于绘制三角测量的点。无论我们是在曲线的内部还是外部,我们都会绘制贝塞尔曲线或贝塞尔控制点的末端。贝塞尔曲线与此代码分别正确呈现。

switch(lVectorPathComponent) {
        case MOVE_TO   :
            /* Append the current vertex to PolygonPoints. */
            if((lLastPathComponent == EVectorPathComponent.BEZIER_TO || lLastPathComponent == EVectorPathComponent.MOVE_TO)) {
                lPolygonPoints.add(new PolygonPoint(pVectorPath.getPathData()[i + 1], pVectorPath.getPathData()[i + 2]));
            }
            /* Initialize the start location of the path. */
            lStartX = pVectorPath.getPathData()[i + 1];
            lStartY = pVectorPath.getPathData()[i + 2];
        break;
        case LINE_TO   : 
            if(lLastPathComponent != EVectorPathComponent.MOVE_TO) {
                lPolygonPoints.add(new PolygonPoint(pVectorPath.getPathData()[i + 1], pVectorPath.getPathData()[i + 2]));
            }
            else {
                if((lNextPathComponent == EVectorPathComponent.LINE_TO || lNextPathComponent == EVectorPathComponent.BEZIER_TO)) {
                    lPolygonPoints.add(new PolygonPoint(pVectorPath.getPathData()[i + 1], pVectorPath.getPathData()[i + 2]));
                }
            }
        break;
        case BEZIER_TO : 
            if(VectorPathGlobal.onCalculateBezierDirection(pVectorPath, i) == pVectorPath.getWindingOrder()) {
                if(!(lLastPathComponent == EVectorPathComponent.LINE_TO && lNextPathComponent == EVectorPathComponent.BEZIER_TO)) {
                    lPolygonPoints.add(new PolygonPoint(pVectorPath.getPathData()[i - 2], pVectorPath.getPathData()[i - 1])); /* Last X, Y */
                    lPolygonPoints.add(new PolygonPoint(pVectorPath.getPathData()[i + 1], pVectorPath.getPathData()[i + 2])); /* Control Point */
                    lPolygonPoints.add(new PolygonPoint(pVectorPath.getPathData()[i + 3], pVectorPath.getPathData()[i + 4])); /* Bezier end X, Y */
                }
            }
            else {
                lPolygonPoints.add(new PolygonPoint(pVectorPath.getPathData()[i + 3], pVectorPath.getPathData()[i + 4]));
            }
        break;
        case CLOSE     : 
            lPolygonPoints.add(new PolygonPoint(lStartX, lStartY));
        break;
    }
}

正确渲染的曲线包含以下命令:

MOVE_TO x:121.0 y:682.0
BEZIER_TO cx:121.0 cy:840.0 x:164.0 y:969.0
BEZIER_TO cx:208.0 cy:1098.0 x:289.0 y:1189.0
BEZIER_TO cx:370.0 cy:1281.0 x:485.0 y:1330.0
BEZIER_TO cx:601.0 cy:1380.0 x:746.0 y:1380.0
BEZIER_TO cx:797.0 cy:1380.0 x:838.0 y:1374.0
BEZIER_TO cx:880.0 cy:1369.0 x:914.0 y:1360.0
BEZIER_TO cx:949.0 cy:1351.0 x:978.0 y:1339.0
BEZIER_TO cx:1007.0 cy:1327.0 x:1033.0 y:1314.0
LINE_TO x:978.0 y:1184.0
BEZIER_TO cx:929.0 cy:1207.0 x:872.0 y:1220.0
BEZIER_TO cx:816.0 cy:1234.0 x:746.0 y:1234.0
BEZIER_TO cx:650.0 cy:1234.0 x:571.0 y:1202.0
BEZIER_TO cx:492.0 cy:1170.0 x:435.0 y:1102.0
BEZIER_TO cx:379.0 cy:1035.0 x:348.0 y:931.0
BEZIER_TO cx:317.0 cy:827.0 x:317.0 y:682.0
BEZIER_TO cx:317.0 cy:537.0 x:349.0 y:432.0
BEZIER_TO cx:382.0 cy:327.0 x:441.0 y:259.0
BEZIER_TO cx:500.0 cy:191.0 x:583.0 y:158.0
BEZIER_TO cx:666.0 cy:126.0 x:768.0 y:126.0
BEZIER_TO cx:811.0 cy:126.0 x:847.0 y:131.0
BEZIER_TO cx:884.0 cy:137.0 x:915.0 y:146.0
BEZIER_TO cx:946.0 cy:155.0 x:972.0 y:165.0
BEZIER_TO cx:998.0 cy:176.0 x:1020.0 y:186.0
LINE_TO x:1062.0 y:58.0
BEZIER_TO cx:1009.0 cy:25.0 x:933.0 y:2.0
BEZIER_TO cx:858.0 cy:-20.0 x:746.0 y:-20.0
BEZIER_TO cx:601.0 cy:-20.0 x:485.0 y:30.0
BEZIER_TO cx:370.0 cy:81.0 x:289.0 y:173.0
BEZIER_TO cx:208.0 cy:265.0 x:164.0 y:394.0
BEZIER_TO cx:121.0 cy:524.0 x:121.0 y:682.0

错误渲染的曲线使用以下内容:

MOVE_TO x:831.0 y:1391.0
BEZIER_TO cx:556.0 cy:1391.0 x:398.0 y:1215.0
BEZIER_TO cx:240.0 cy:1039.0 x:240.0 y:733.0
BEZIER_TO cx:240.0 cy:420.0 x:389.0 y:247.0
BEZIER_TO cx:538.0 cy:74.0 x:815.0 y:74.0
BEZIER_TO cx:999.0 cy:74.0 x:1153.0 y:121.0
LINE_TO x:1153.0 y:31.0
BEZIER_TO cx:1008.0 cy:-20.0 x:791.0 y:-20.0
BEZIER_TO cx:483.0 cy:-20.0 x:306.0 y:179.0
BEZIER_TO cx:129.0 cy:378.0 x:129.0 y:735.0
BEZIER_TO cx:129.0 cy:958.0 x:213.0 y:1128.0
BEZIER_TO cx:298.0 cy:1298.0 x:456.0 y:1390.0
BEZIER_TO cx:615.0 cy:1483.0 x:825.0 y:1483.0
BEZIER_TO cx:1039.0 cy:1483.0 x:1208.0 y:1403.0
LINE_TO x:1167.0 y:1311.0
BEZIER_TO cx:1007.0 cy:1391.0 x:831.0 y:1391.0

2 个答案:

答案 0 :(得分:1)

是的,代码没问题。下面的代码使用标准的Path2D quadTo / curveTo。也许它与偶数绕组规则等有关。

public class JavaGUI extends JPanel {

    public static void main(String[] args) {
        final JFrame f = new JFrame("Glyphs");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JavaGUI());
        f.pack();
        f.setLocationRelativeTo(null);

        EventQueue.invokeLater(() -> {
            f.setVisible(true);
        });
    }

    public JavaGUI() {
        setPreferredSize(new Dimension(600, 600));
        setBackground(new Color(0xDDEEFF));
    }

    private double x0;
    private double y0;

    @Override
    public void paintComponent(Graphics g) {
        final String aString = "c";
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        double scale = 0.25;
        g2d.translate(100, 100);
        g2d.scale(scale, scale);

        Path2D.Double path = new Path2D.Double(); // May add rule and such.

        moveTo(path, 121.0, 682.0);
        bezierTo(path, 121.0, 840.0, 164.0, 969.0);
        bezierTo(path, 208.0, 1098.0, 289.0, 1189.0);
        bezierTo(path, 370.0, 1281.0, 485.0, 1330.0);
        bezierTo(path, 601.0, 1380.0, 746.0, 1380.0);
        bezierTo(path, 797.0, 1380.0, 838.0, 1374.0);
        bezierTo(path, 880.0, 1369.0, 914.0, 1360.0);
        bezierTo(path, 949.0, 1351.0, 978.0, 1339.0);
        bezierTo(path, 1007.0, 1327.0, 1033.0, 1314.0);
        lineTo(path, 978.0, 1184.0);
        bezierTo(path, 929.0, 1207.0, 872.0, 1220.0);
        bezierTo(path, 816.0, 1234.0, 746.0, 1234.0);
        bezierTo(path, 650.0, 1234.0, 571.0, 1202.0);
        bezierTo(path, 492.0, 1170.0, 435.0, 1102.0);
        bezierTo(path, 379.0, 1035.0, 348.0, 931.0);
        bezierTo(path, 317.0, 827.0, 317.0, 682.0);
        bezierTo(path, 317.0, 537.0, 349.0, 432.0);
        bezierTo(path, 382.0, 327.0, 441.0, 259.0);
        bezierTo(path, 500.0, 191.0, 583.0, 158.0);
        bezierTo(path, 666.0, 126.0, 768.0, 126.0);
        bezierTo(path, 811.0, 126.0, 847.0, 131.0);
        bezierTo(path, 884.0, 137.0, 915.0, 146.0);
        bezierTo(path, 946.0, 155.0, 972.0, 165.0);
        bezierTo(path, 998.0, 176.0, 1020.0, 186.0);
        lineTo(path, 1062.0, 58.0);
        bezierTo(path, 1009.0, 25.0, 933.0, 2.0);
        bezierTo(path, 858.0, -20.0, 746.0, -20.0);
        bezierTo(path, 601.0, -20.0, 485.0, 30.0);
        bezierTo(path, 370.0, 81.0, 289.0, 173.0);
        bezierTo(path, 208.0, 265.0, 164.0, 394.0);
        bezierTo(path, 121.0, 524.0, 121.0, 682.0);

        path.closePath();

        g2d.setColor(Color.RED);
        g2d.fill(path);

        // ------

        path = new Path2D.Double(); // May add rule and such.

        moveTo(path, 831.0, 1391.0);
        bezierTo(path, 556.0, 1391.0, 398.0, 1215.0);
        bezierTo(path, 240.0, 1039.0, 240.0, 733.0);
        bezierTo(path, 240.0, 420.0, 389.0, 247.0);
        bezierTo(path, 538.0, 74.0, 815.0, 74.0);
        bezierTo(path, 999.0, 74.0, 1153.0, 121.0);
        lineTo(path, 1153.0, 31.0);
        bezierTo(path, 1008.0, -20.0, 791.0, -20.0);
        bezierTo(path, 483.0, -20.0, 306.0, 179.0);
        bezierTo(path, 129.0, 378.0, 129.0, 735.0);
        bezierTo(path, 129.0, 958.0, 213.0, 1128.0);
        bezierTo(path, 298.0, 1298.0, 456.0, 1390.0);
        bezierTo(path, 615.0, 1483.0, 825.0, 1483.0);
        bezierTo(path, 1039.0, 1483.0, 1208.0, 1403.0);
        lineTo(path, 1167.0, 1311.0);
        bezierTo(path, 1007.0, 1391.0, 831.0, 1391.0        );

        path.closePath();

        g2d.setColor(new Color(0x8800CC00, true));
        g2d.fill(path);

        g2d.scale(1/scale, 1/scale);
    }

    private void bezierTo(Path2D.Double path, double cx, double cy,
            double x, double y) {
        path.quadTo(cx, cy, x, y);
        //path.curveTo(x0, y0, cx, cy, x, y);
        x0 = x;
        y0 = y;
    }

    private void moveTo(Path2D.Double path, double x, double y) {
        path.moveTo(x, y);
        x0 = x;
        y0 = y;
    }

    private void lineTo(Path2D.Double path, double x, double y) {
        path.lineTo(x, y);
        x0 = x;
        y0 = y;
    }
}

Two glyphs of both a "c"

答案 1 :(得分:0)

我发现LibTess2中使用的曲面细分方法与Java的Path2D三角剖分兼容。

在我原来的问题中,我遇到的问题是,对于我的顶点三角测量,我使用的是Poly2Tri,它不支持位于多边形边缘的顶点;因为这个问题,正在生成一些奇怪的三角形,这些三角形似乎逐渐降低到原点。 LibTess2也努力对非简单多边形进行三角测量,但是在检测到冲突顶点时,它允许用户通过它的combine()方法回调提供解决冲突的方法。通过在冲突的顶点之间建立相等性,可以解决错误的镶嵌细分,从而产生正确的三角形字形。 earcut-j也足以处理这些顶点。