如何使用Java中的PathIterator类将SVG Art-board装配到选定的作品中

时间:2014-06-10 07:04:12

标签: java svg fonts

我试图将文本的边界变为工作表内容选择。 我正在使用AttributedString类在java中编写coed以使用PathIterator类来获取文本的坐标,我将其保存为SVG格式,当我打开svg文件时,它显示文本但是在工作表中的某个地方。这是正确的,但我的问题如何调整页面到内容的大小(或调整页面大小到绘图选择,这是文本),我使用Inkscape来显示文本,其中有一个选项,它允许您将页面(工作表)的大小调整为内容,如此示例所示 resize page to content

我尝试了这段代码,用于保存和获取文本的坐标:

public class pathsFont {

public pathsFont(){
}

public static void main(String[] args) throws Exception {
    pathsFont getFiel=new pathsFont();
    getFiel.getFontFileAsSVG(new FileWriter("C:\\paths01.svg")); }

Shape shape ;
public void getFontFileAsSVG(FileWriter f) throws IOException{
    Font font = new Font("Arial", Font.PLAIN, 40);
    String str="Hello";
    AttributedString attributedString = new AttributedString(str);
    attributedString.addAttribute(TextAttribute.FONT, font, 0, str.length());
    attributedString.addAttribute(TextAttribute.WIDTH, shape);
   FontRenderContext fontRenderContext = new FontRenderContext(null, false, false);
   TextLayout layout = new TextLayout(attributedString.getIterator(), fontRenderContext);
   //to set a specific position 
      AffineTransform at=new AffineTransform();
      at.translate(10,20);

    shape = layout.getOutline(at);
    PathIterator pi = shape.getPathIterator(at);

    PrintWriter out = new PrintWriter(f);

        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<svg\n>" );
        out.println("<path d=\"");

        String temp=new String();

        while (pi.isDone() == false) {
             temp+=getCoordinates(pi);
            pi.next();
          }

              out.println(temp);
            out.format("\"/>"+"\n"+"</svg>");
             out.close();
             f.close();
             System.out.println("Data added to the File successfully");
}

public String getCoordinates(PathIterator pi) {
  String temp=new String();
double[] coor = new double[6];
int type = pi.currentSegment(coor);
switch (type) {
case PathIterator.SEG_MOVETO:
  temp="\n\n"+" M " + coor[0] + ", " + coor[1];
  break;
case PathIterator.SEG_LINETO:
  temp+="\n\n"+" L " + coor[0] + ", " + coor[1];
  break;
case PathIterator.SEG_QUADTO:
  temp+=" Q " + coor[0] + ", " + coor[1] + " "
      + coor[2] + ", " + coor[3];
  break;
case PathIterator.SEG_CUBICTO:
  temp+=" C " + coor[0] + ", " + coor[1] + " "
      + coor[2] + ", " + coor[3] + " " + coor[4] + ", " + coor[5];
  break;
case PathIterator.SEG_CLOSE:
  temp+=" Z";
  break;
default:
  break;
}
return temp; }}

如果问题的某些部分不清楚,请让我知道更清楚地解释

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,您应该使用layout类的TextLayout对象来获取高度,宽度,X和Y坐标,

      double w=layout.getBounds().getWidth(); 
      double h=layout.getBounds().getHeight();
      double X=layout.getBounds().getX();
      double Y=layout.getBounds().getY();

然后在svg viewBox属性中分配这些值。

out.println("<?xml version=\"1.0\" standalone=\"no\"?>\n"
                + "<svg xmlns=\"http://www.w3.org/2000/svg\" preserveAspectRatio=\"none\" " +"\n" 
                + " width=\""+w+"\" height=\""+h+"\""
                + " viewBox=\""+X+" "+Y+" "+w+" "+h+ "\" >" );