1
package latexDraw.generators.svg;
3
import java.awt.geom.Point2D;
4
import java.text.ParseException;
5
import java.util.Vector;
7
import latexDraw.figures.DrawBorders;
8
import latexDraw.figures.Figure;
9
import latexDraw.figures.FramedBox;
10
import latexDraw.figures.Text;
11
import latexDraw.parsers.latexdraw.LPSTParser;
12
import latexDraw.parsers.svg.CSSColors;
13
import latexDraw.parsers.svg.SVGAttributes;
14
import latexDraw.parsers.svg.SVGDocument;
15
import latexDraw.parsers.svg.SVGElements;
16
import latexDraw.parsers.svg.elements.SVGElement;
17
import latexDraw.parsers.svg.elements.SVGGElement;
18
import latexDraw.parsers.svg.elements.SVGTextElement;
19
import latexDraw.parsers.svg.parsers.SVGLengthParser;
20
import latexDraw.psTricks.DviPsColors;
21
import latexDraw.util.LaTeXDrawNamespace;
22
import latexDraw.util.LaTeXDrawPoint2D;
24
import org.w3c.dom.Element;
25
import org.w3c.dom.NodeList;
28
* Defines a SVG generator for a text.<br>
30
* This file is part of LaTeXDraw.<br>
31
* Copyright (c) 2005-2008 Arnaud BLOUIN<br>
33
* LaTeXDraw is free software; you can redistribute it and/or modify
34
* it under the terms of the GNU General Public License as published by
35
* the Free Software Foundation; either version 2 of the License, or
36
* (at your option) any later version.<br>
38
* LaTeXDraw is distributed without any warranty; without even the
39
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
40
* PURPOSE. See the GNU General Public License for more details.<br>
43
* @author Arnaud BLOUIN<br>
46
public class LTextSVGGenerator extends LShapeSVGGenerator
49
public LTextSVGGenerator(Text f)
57
* Creates a text from an SVG text element.
58
* @param elt The source element.
61
public LTextSVGGenerator(SVGTextElement elt)
63
this(new Text(null, false));
66
throw new IllegalArgumentException();
68
Text t = (Text)getShape();
70
t.setText(elt.getText());
71
t.setPosition(new LaTeXDrawPoint2D(elt.getX(), elt.getY()));
72
setTextAttributes(elt);
73
applyTransformations(elt);
78
public LTextSVGGenerator(SVGGElement elt)
86
* Creates a text from a latexdraw-SVG element.
87
* @param elt The source element.
90
public LTextSVGGenerator(SVGGElement elt, boolean withTransformation)
92
this(new Text(null, false));
95
throw new IllegalArgumentException();
100
String fontFam = elt.getAttribute(SVGAttributes.SVG_FONT_FAMILY);
104
try { fontSize = Double.valueOf(elt.getAttribute(SVGAttributes.SVG_FONT_SIZE)).intValue(); }
105
catch(NumberFormatException e) { fontSize = Text.DEFAULT_SIZE.getSize(); }
108
setTextAttributes(elt);
109
v = elt.getAttribute(LaTeXDrawNamespace.LATEXDRAW_NAMESPACE_URI+':'+SVGAttributes.SVG_X);
110
x = v==null ? Double.MAX_VALUE : Double.valueOf(v).doubleValue();
112
nl = elt.getElementsByTagNameNS(SVGDocument.SVG_NAMESPACE, SVGElements.SVG_TEXT);
116
SVGTextElement text = (SVGTextElement)nl.item(0);
117
LPSTParser parser = new LPSTParser("\\put(0,0){" + text.getText() + "}");
118
Vector<Figure> shapes;
120
try{ parser.parseFramedBox(); }
121
catch(Exception e) {e.printStackTrace(); }
122
shapes = parser.getShapes();
124
if(shapes==null || shapes.isEmpty() || !(shapes.firstElement() instanceof Text))
125
throw new IllegalArgumentException();
127
t = (Text)shapes.firstElement();
129
x = text.getX()<x ? text.getX() : x;
130
t.setPosition(new LaTeXDrawPoint2D(x, y));
135
throw new IllegalArgumentException();
137
if(t.hasSimpleFramedBox())
139
FramedBox fb = t.getSimpleBox();
140
Figure figure = fb.getBox();
142
if(((float)fb.getFrameSep())==0f && figure.getLinesColor().equals(figure.getInteriorColor()) &&
143
figure.isFilled() && fb.getBoxType()==FramedBox.BOX_RECTANGLE)
145
t.setOpacityColor(figure.getLinesColor());
154
t.setTextFont(fontFam);
155
t.setIsBold(SVGAttributes.SVG_FONT_WEIGHT_BOLD.equals(elt.getAttribute(SVGAttributes.SVG_FONT_WEIGHT)));
156
t.setIsItalic(SVGAttributes.SVG_FONT_STYLE_ITALIC.equals(elt.getAttribute(SVGAttributes.SVG_FONT_STYLE)));
158
if(withTransformation)
159
applyTransformations(elt);
166
* Sets the text attribute (is italic, font family,...) from the given SVG element.
167
* @param e The source SVG element.
170
public void setTextAttributes(SVGElement e)
175
Text t = (Text)getShape();
176
String svgPref = e.getUsablePrefix(SVGDocument.SVG_NAMESPACE);
179
t.setTextFontByFamily(e.getAttribute(svgPref+SVGAttributes.SVG_FONT_FAMILY));
180
t.setLinesColor(CSSColors.getRGBColour(e.getAttribute(svgPref+SVGAttributes.SVG_FILL)));
182
v = e.getAttribute(svgPref+SVGAttributes.SVG_FONT_SIZE);
186
{ t.setSize((int)new SVGLengthParser(v).parseLength().getValue()); }
187
catch(ParseException ex) { /* */ }
189
v = e.getAttribute(svgPref+SVGAttributes.SVG_FONT_STYLE);
190
t.setIsItalic(v==null || !v.equals(SVGAttributes.SVG_FONT_STYLE_ITALIC) ? false : true);
192
v = e.getAttribute(svgPref+SVGAttributes.SVG_FONT_WEIGHT);
193
t.setIsBold(v==null || !v.equals(SVGAttributes.SVG_FONT_WEIGHT_BOLD) ? false : true);
200
* Creates an SVGTextElement from the given part of text.
201
* @param txt The text to convert in SVG.
202
* @param doc The document to create the text element.
203
* @param position The position of the text.
204
* @return The SVG text element or null if a parameter is null.
207
public SVGTextElement getSVGTextElement(String txt, SVGDocument doc, Point2D position)
209
if(txt==null || position==null || doc==null)
212
SVGTextElement textElt = new SVGTextElement(doc);
214
textElt.setAttribute(SVGAttributes.SVG_X, String.valueOf(position.getX()));
215
textElt.setAttribute(SVGAttributes.SVG_Y, String.valueOf(position.getY()));
216
textElt.appendChild(doc.createCDATASection(txt));
225
public SVGElement toSVG(SVGDocument doc)
230
SVGElement root = new SVGGElement(doc);
231
Text t = (Text)getShape();
232
String ltdPref = LaTeXDrawNamespace.LATEXDRAW_NAMESPACE + ':';
234
root.setAttribute(ltdPref + LaTeXDrawNamespace.XML_TYPE, LaTeXDrawNamespace.XML_TYPE_TEXT);
235
root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
236
root.setAttribute(SVGAttributes.SVG_FONT_FAMILY, String.valueOf(t.getCurrentFont().getFamily()));
237
root.setAttribute(SVGAttributes.SVG_FILL, CSSColors.getColorName(t.getLinesColor(), true));
238
root.setAttribute(SVGAttributes.SVG_FONT_SIZE, String.valueOf(t.getCurrentFont().getSize()));
239
root.setAttribute(ltdPref + SVGAttributes.SVG_X, String.valueOf(t.getPosition().x));
242
root.setAttribute(SVGAttributes.SVG_FONT_STYLE, SVGAttributes.SVG_FONT_STYLE_ITALIC);
245
root.setAttribute(SVGAttributes.SVG_FONT_WEIGHT, SVGAttributes.SVG_FONT_WEIGHT_BOLD);
247
Element txt = new SVGTextElement(doc);
248
String str = t.getCodePSTricksBody(new DrawBorders(false), Figure.PPC);
249
String cols = DviPsColors.getUserColoursCode(str);
252
cols = cols.replace(System.getProperty("line.separator"), "");
254
txt.setAttribute(SVGAttributes.SVG_X, String.valueOf(t.getPosition().x));
255
txt.setAttribute(SVGAttributes.SVG_Y, String.valueOf(t.getPosition().y));
256
txt.appendChild(doc.createCDATASection(cols+str));
257
root.appendChild(txt);
259
setSVGRotationAttribute(root);