I am writing to a file coordinates of texts using PathIterator saved as SVG format i defined the Font attributes using AttributedString and TextLayout . It is saving to the file and working properly, but it is just writing as a single line of text where is my target is multiple lines of text, i began using LineBreakMeasurer Class, but some text is removed plus i got single line of text only, there i faced the problem that , How to determine the width to get multiple lines of text ? i tried to figure out from this exampleDrawing Multiple Lines of Text where is drawing multiple lines of text but i am using getBounds().getWidth() of TextLayout object the code i tried :
Java Code:
public class writeFontPathsToFileAsSVG {
private AttributedString text;
private LineBreakMeasurer lineMeasurer;
private int paragraphStart;
private int paragraphEnd;
private static final Hashtable map = new Hashtable();
static {
map.put(TextAttribute.FAMILY, "Arial");
map.put(TextAttribute.SIZE, new Float(25.0));
}
public writeFontPathsToFileAsSVG(){
this.text = new AttributedString( "Salam Marhaba Ahlan Wa Sahlan "
+"Salam Marhaba Ahlan Wa Sahlan"
+"Salam Marhaba Ahlan Wa Sahlan",map);
}
public static void main(String[] args) throws Exception {
writeFontPathsToFileAsSVG getFiel=new writeFontPathsToFileAsSVG();
getFiel.getFontFileAsSVG(new FileWriter(C:\\saveTo.svg"));
}
Shape shape ;
public void getFontFileAsSVG(FileWriter f) throws IOException{
AttributedCharacterIterator attributedChar = text.getIterator() ;
paragraphStart = attributedChar.getBeginIndex();
paragraphEnd = attributedChar.getEndIndex();
FontRenderContext fontRenderContext = new FontRenderContext(null, false, false);
lineMeasurer = new LineBreakMeasurer(attributedChar,fontRenderContext);
TextLayout layout = new TextLayout(text.getIterator(), fontRenderContext);
//To get the Text fits the art-board
double w=layout.getBounds().getWidth();
double h=layout.getBounds().getHeight();
double x=layout.getBounds().getX();
double y=layout.getBounds().getY();
//To get a specific width to go to next line
float formatWidth = (float) w + 250 ;
float drawPosY = 0;
lineMeasurer.setPosition(paragraphEnd);
float drawPosX = 0;
while (lineMeasurer.getPosition() < paragraphEnd) {
layout = lineMeasurer.nextLayout((float) w);
y += layout.getAscent();
if (layout.isLeftToRight()) {
x = 0;
} else {
x = w - layout.getAdvance();
}
y += layout.getDescent() + layout.getLeading();
}
shape = layout.getOutline(null);
PathIterator pi = shape.getPathIterator(null);
PrintWriter out = new PrintWriter(f);
out.println("<?xml version=\"1.0\" standalone=\"no\"?>\n"
+ "<svg xmlns=\"http://ift.tt/nvqhV5\" preserveAspectRatio=\"none\" " +"\n"
+ " width=\""+w+"\" height=\""+h+"\""
+ " viewBox=\""+x+" "+y+" "+w+" "+h+ "\" >" );
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;
}
}
No comments:
Post a Comment