如何从ttf文件中获取汉字笔顺

如何从ttf文件中获取汉字笔顺

本文介绍了如何从ttf文件中获取汉字笔顺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 getGlyphOutline()可以从JAVA API显示字体。
,我还没有找到任何API显示一个中文笔顺。
,但这是真的: .ttf 包含笔顺。
i只是不知道如何通过JAVA获得它。



可能是我忘记的一些重要的API?

  shape = gv.getGlyphOutline(i,200,200); 
((Graphics2D)g).draw(shape);






现在,我发现PathIterator

 形状shape = gv.getGlyphOutline(0,200,200); 
PathIterator pi = shape.getPathIterator(new AffineTransform());
double [] coords = new double [6];
int count = 0;
while(!pi.isDone()){
int kind = pi.currentSegment(coords);
int [] path = new int [4];
switch(kind){
case PathIterator.SEG_MOVETO:
System.out.println(SEG_MOVETO);
break;
case PathIterator.SEG_LINETO:
System.out.println(SEG_LINETO);
break;
case PathIterator.SEG_CLOSE:
System.out.println(SEG_CLOSE);
g.drawLine((int)coords [0],(int)coords [1],
(int)coords [2],(int)coords [3]);
count ++;
break;
case PathIterator.SEG_QUADTO:
System.out.println(SEG_QUADTO);
g.drawLine((int)coords [0],(int)coords [1],
(int)coords [2],(int)coords [3]);
count ++;
break;
默认值:
抛出新的IllegalArgumentException(Bad path segment);
}
pi.next();





$有一个问题,我不能得到完整的单词..
看起来像虚线...

解决方案

你的意思是画出角色部分的笔画 - 像这样的代码?



  import java.awt。*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font。*;
import java.awt.geom。*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing。*;
import javax.swing.border.EmptyBorder;

public class LettersByStrokeAnimation {

private JComponent ui = null;
String text =;
字体字体;

LettersByStrokeAnimation(){
initUI();

$ b $ public void initUI(){
if(ui!= null){
return;
}

ui = new JPanel(new GridLayout(0,1));
ui.setBorder(new EmptyBorder(4,4,4,4)); (int i = 13444; i< 13450; i ++){
text + = new String(Character.toChars(i));


}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font [] fonts = ge.getAllFonts();
布尔值canDisplay = false;
int i = 0;
while(!canDisplay){
font = fonts [i];
if(font.canDisplayUpTo(text)< 0){
canDisplay = true;
font = font.deriveFont(50f);
}
i ++;
}
JLabel l = new JLabel(text);
l.setFont(font);
ui.add(l);

ui.add(new AnimatedText(text,font,200));
}

public JComponent getUI(){
return ui;


public static void main(String [] args){
Runnable r = new Runnable(){
@Override
public void run ){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception useDefault){
}
LettersByStrokeAnimation o = new LettersByStrokeAnimation();

JFrame f = new JFrame(o.getClass()。getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);



类AnimatedText扩展JPanel {

字体字体;
int counter;
ArrayList< Shape>形状;

AnimatedText(String text,Font font,int delay){
this.font = font;
BufferedImage bi = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.dispose();
FontRenderContext frc = g.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc,text);
Shape shape = gv.getOutline(0,50);
GeneralPath gp = new GeneralPath(shape);

PathIterator pi = gp.getPathIterator(null);
shapes = new ArrayList< Shape>();
while(!pi.isDone()){
shapes.add(getNextStroke(pi));

ActionListener timerListener = new ActionListener(){

@Override
public void actionPerformed(ActionEvent e){
AnimatedText.this.repaint() ;
}
};
定时器定时器=新的定时器(延迟,timerListener);
timer.start();


private final Shape getNextStroke(PathIterator pi){
double [] coords = new double [6];

GeneralPath gp = new GeneralPath();
boolean closed = false;
while(!closed&!pi.isDone()){
int pathSegmentType = pi.currentSegment(coords);
closed = pathSegmentType == PathIterator.SEG_CLOSE;
int windingRule = pi.getWindingRule();
gp.setWindingRule(windingRule);
if(pathSegmentType == PathIterator.SEG_MOVETO){
gp.moveTo(coords [0],coords [1]);
} else if(pathSegmentType == PathIterator.SEG_LINETO){
gp.lineTo(coords [0],coords [1]);
} else if(pathSegmentType == PathIterator.SEG_QUADTO){
gp.quadTo(coords [0],coords [1],coords [2],coords [3]);
} else if(pathSegmentType == PathIterator.SEG_CUBICTO){
gp.curveTo(
coords [0],coords [1],coords [2],
coords [3 ],coords [4],coords [5]);
} else if(pathSegmentType == PathIterator.SEG_CLOSE){
gp.closePath();
} else {
System.err.println(Unexpected value!+ pathSegmentType);
}
pi.next();
}
形状shape = new Area(gp);

返回形状;
}

@Override
保护void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
int current = counter%shapes.size();
for(int i = 0; i< current; i ++){
g2.draw(shapes.get(i));
}

counter ++;
}
}


i find getGlyphOutline() can show font out line from JAVA API.and i have not found out any API for show one chinese stroke order.but this is true: .ttf contain the stroke order.i just don't know how to get it by JAVA.

May be some important APIs i forget?

shape = gv.getGlyphOutline(i, 200, 200);
            ((Graphics2D) g).draw(shape);


now, i found PathIterator

Shape shape = gv.getGlyphOutline(0, 200, 200);
        PathIterator pi = shape.getPathIterator(new AffineTransform());
        double[] coords = new double[6];
        int count = 0;
        while (!pi.isDone()) {
            int kind = pi.currentSegment(coords);
            int[] path = new int[4];
            switch (kind) {
            case PathIterator.SEG_MOVETO:
                System.out.println("SEG_MOVETO");
                break;
            case PathIterator.SEG_LINETO:
                System.out.println("SEG_LINETO");
                break;
            case PathIterator.SEG_CLOSE:
                System.out.println("SEG_CLOSE");
                g.drawLine((int) coords[0], (int) coords[1],
                        (int) coords[2], (int) coords[3]);
                count++;
                break;
            case PathIterator.SEG_QUADTO:
                System.out.println("SEG_QUADTO");
                g.drawLine((int) coords[0], (int) coords[1],
                        (int) coords[2], (int) coords[3]);
                count++;
                break;
            default:
                throw new IllegalArgumentException("Bad path segment");
            }
            pi.next();
        }

There is a problem, i can not get complete word..It looks like dotted line...

解决方案

Do you mean to draw the strokes of the characters 'part by part' - something like this code?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class LettersByStrokeAnimation {

    private JComponent ui = null;
    String text = "";
    Font font;

    LettersByStrokeAnimation() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new GridLayout(0, 1));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        for (int i = 13444; i < 13450; i++) {
            text += new String(Character.toChars(i));
        }
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = ge.getAllFonts();
        boolean canDisplay = false;
        int i = 0;
        while (!canDisplay) {
            font = fonts[i];
            if (font.canDisplayUpTo(text) < 0) {
                canDisplay = true;
                font = font.deriveFont(50f);
            }
            i++;
        }
        JLabel l = new JLabel(text);
        l.setFont(font);
        ui.add(l);

        ui.add(new AnimatedText(text, font, 200));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                LettersByStrokeAnimation o = new LettersByStrokeAnimation();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class AnimatedText extends JPanel {

    Font font;
    int counter;
    ArrayList<Shape> shapes;

    AnimatedText(String text, Font font, int delay) {
        this.font = font;
        BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.dispose();
        FontRenderContext frc = g.getFontRenderContext();
        GlyphVector gv = font.createGlyphVector(frc, text);
        Shape shape = gv.getOutline(0, 50);
        GeneralPath gp = new GeneralPath(shape);

        PathIterator pi = gp.getPathIterator(null);
        shapes = new ArrayList<Shape>();
        while (!pi.isDone()) {
            shapes.add(getNextStroke(pi));
        }
        ActionListener timerListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                AnimatedText.this.repaint();
            }
        };
        Timer timer = new Timer(delay, timerListener);
        timer.start();
    }

    private final Shape getNextStroke(PathIterator pi) {
        double[] coords = new double[6];

        GeneralPath gp = new GeneralPath();
        boolean closed = false;
        while (!closed && !pi.isDone()) {
            int pathSegmentType = pi.currentSegment(coords);
            closed = pathSegmentType == PathIterator.SEG_CLOSE;
            int windingRule = pi.getWindingRule();
            gp.setWindingRule(windingRule);
            if (pathSegmentType == PathIterator.SEG_MOVETO) {
                gp.moveTo(coords[0], coords[1]);
            } else if (pathSegmentType == PathIterator.SEG_LINETO) {
                gp.lineTo(coords[0], coords[1]);
            } else if (pathSegmentType == PathIterator.SEG_QUADTO) {
                gp.quadTo(coords[0], coords[1], coords[2], coords[3]);
            } else if (pathSegmentType == PathIterator.SEG_CUBICTO) {
                gp.curveTo(
                        coords[0], coords[1], coords[2],
                        coords[3], coords[4], coords[5]);
            } else if (pathSegmentType == PathIterator.SEG_CLOSE) {
                gp.closePath();
            } else {
                System.err.println("Unexpected value! " + pathSegmentType);
            }
            pi.next();
        }
        Shape shape = new Area(gp);

        return shape;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        int current = counter % shapes.size();
        for (int i = 0; i < current; i++) {
            g2.draw(shapes.get(i));
        }

        counter++;
    }
}

这篇关于如何从ttf文件中获取汉字笔顺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:21