~ubuntu-branches/debian/sid/latexdraw/sid

« back to all changes in this revision

Viewing changes to latexDraw/generators/svg/LTextSVGGenerator.java

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2009-05-03 23:49:35 UTC
  • Revision ID: james.westby@ubuntu.com-20090503234935-cls7n48x018g0vk2
Tags: upstream-2.0.2+1
ImportĀ upstreamĀ versionĀ 2.0.2+1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package latexDraw.generators.svg;
 
2
 
 
3
import java.awt.geom.Point2D;
 
4
import java.text.ParseException;
 
5
import java.util.Vector;
 
6
 
 
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;
 
23
 
 
24
import org.w3c.dom.Element;
 
25
import org.w3c.dom.NodeList;
 
26
 
 
27
/**
 
28
 * Defines a SVG generator for a text.<br>
 
29
 *<br>
 
30
 * This file is part of LaTeXDraw.<br>
 
31
 * Copyright (c) 2005-2008 Arnaud BLOUIN<br>
 
32
 *<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>
 
37
 *<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>
 
41
 *<br>
 
42
 * 11/11/07<br>
 
43
 * @author Arnaud BLOUIN<br>
 
44
 * @version 0.1<br>
 
45
 */
 
46
public class LTextSVGGenerator extends LShapeSVGGenerator
 
47
{
 
48
 
 
49
        public LTextSVGGenerator(Text f)
 
50
        {
 
51
                super(f);
 
52
        }
 
53
 
 
54
        
 
55
        
 
56
        /**
 
57
         * Creates a text from an SVG text element.
 
58
         * @param elt The source element.
 
59
         * @since 2.0.0
 
60
         */
 
61
        public LTextSVGGenerator(SVGTextElement elt)
 
62
        {
 
63
                this(new Text(null, false));
 
64
                
 
65
                if(elt==null)
 
66
                        throw new IllegalArgumentException();
 
67
                
 
68
                Text t = (Text)getShape();
 
69
                
 
70
                t.setText(elt.getText());
 
71
                t.setPosition(new LaTeXDrawPoint2D(elt.getX(), elt.getY()));
 
72
                setTextAttributes(elt);
 
73
                applyTransformations(elt);
 
74
        }
 
75
        
 
76
        
 
77
        
 
78
        public LTextSVGGenerator(SVGGElement elt)
 
79
        {
 
80
                this(elt, true);
 
81
        }
 
82
        
 
83
        
 
84
        
 
85
        /**
 
86
         * Creates a text from a latexdraw-SVG element.
 
87
         * @param elt The source element.
 
88
         * @since 2.0.0
 
89
         */
 
90
        public LTextSVGGenerator(SVGGElement elt, boolean withTransformation)
 
91
        {
 
92
                this(new Text(null, false));
 
93
 
 
94
                if(elt==null)
 
95
                        throw new IllegalArgumentException();
 
96
                
 
97
                double x, y = 0;
 
98
                String v;
 
99
                NodeList nl;
 
100
                String fontFam = elt.getAttribute(SVGAttributes.SVG_FONT_FAMILY);
 
101
                int fontSize; 
 
102
                Text t;
 
103
                
 
104
                try { fontSize = Double.valueOf(elt.getAttribute(SVGAttributes.SVG_FONT_SIZE)).intValue(); }
 
105
                catch(NumberFormatException e) { fontSize = Text.DEFAULT_SIZE.getSize(); }
 
106
                
 
107
                setNumber(elt);
 
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();
 
111
                
 
112
                nl = elt.getElementsByTagNameNS(SVGDocument.SVG_NAMESPACE, SVGElements.SVG_TEXT);
 
113
                
 
114
                if(nl.getLength()>0)
 
115
                {
 
116
                        SVGTextElement text = (SVGTextElement)nl.item(0);
 
117
                        LPSTParser parser   = new LPSTParser("\\put(0,0){" + text.getText() + "}");
 
118
                        Vector<Figure> shapes;
 
119
                        
 
120
                        try{ parser.parseFramedBox(); }
 
121
                        catch(Exception e) {e.printStackTrace(); }
 
122
                        shapes = parser.getShapes();
 
123
                        
 
124
                        if(shapes==null || shapes.isEmpty() || !(shapes.firstElement() instanceof Text))
 
125
                                throw new IllegalArgumentException();
 
126
                        
 
127
                        t = (Text)shapes.firstElement();
 
128
                        y = text.getY();
 
129
                        x = text.getX()<x ? text.getX() : x;
 
130
                        t.setPosition(new LaTeXDrawPoint2D(x, y));
 
131
                        
 
132
                        shape = t;
 
133
                }
 
134
                else
 
135
                        throw new IllegalArgumentException();
 
136
 
 
137
                if(t.hasSimpleFramedBox())
 
138
                {
 
139
                        FramedBox fb  = t.getSimpleBox();
 
140
                        Figure figure = fb.getBox();
 
141
                        
 
142
                        if(((float)fb.getFrameSep())==0f && figure.getLinesColor().equals(figure.getInteriorColor()) &&
 
143
                                        figure.isFilled() && fb.getBoxType()==FramedBox.BOX_RECTANGLE)
 
144
                        {
 
145
                                t.setOpacityColor(figure.getLinesColor());
 
146
                                t.removeAllBoxes();
 
147
                                t.setOpaque(true);
 
148
                                
 
149
                        }
 
150
                }
 
151
                
 
152
                t = (Text)shape;
 
153
                t.setSize(fontSize);
 
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)));
 
157
                
 
158
                if(withTransformation)
 
159
                        applyTransformations(elt);
 
160
        }
 
161
        
 
162
        
 
163
        
 
164
        
 
165
        /**
 
166
         * Sets the text attribute (is italic, font family,...) from the given SVG element.
 
167
         * @param e The source SVG element.
 
168
         * @since 2.0.0
 
169
         */
 
170
        public void setTextAttributes(SVGElement e)
 
171
        {
 
172
                if(e==null)
 
173
                        return ;
 
174
                
 
175
                Text t = (Text)getShape();
 
176
                String svgPref = e.getUsablePrefix(SVGDocument.SVG_NAMESPACE);
 
177
                String v;
 
178
                
 
179
                t.setTextFontByFamily(e.getAttribute(svgPref+SVGAttributes.SVG_FONT_FAMILY));
 
180
                t.setLinesColor(CSSColors.getRGBColour(e.getAttribute(svgPref+SVGAttributes.SVG_FILL)));
 
181
                
 
182
                v = e.getAttribute(svgPref+SVGAttributes.SVG_FONT_SIZE);
 
183
                
 
184
                if(v!=null)
 
185
                        try 
 
186
                        { t.setSize((int)new SVGLengthParser(v).parseLength().getValue()); }
 
187
                        catch(ParseException ex) { /* */ }
 
188
                
 
189
                v = e.getAttribute(svgPref+SVGAttributes.SVG_FONT_STYLE);
 
190
                t.setIsItalic(v==null || !v.equals(SVGAttributes.SVG_FONT_STYLE_ITALIC) ? false : true);
 
191
                
 
192
                v = e.getAttribute(svgPref+SVGAttributes.SVG_FONT_WEIGHT);
 
193
                t.setIsBold(v==null || !v.equals(SVGAttributes.SVG_FONT_WEIGHT_BOLD) ? false : true);
 
194
        }
 
195
        
 
196
        
 
197
        
 
198
        
 
199
        /**
 
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.
 
205
         * @since 2.0.0
 
206
         */
 
207
        public SVGTextElement getSVGTextElement(String txt, SVGDocument doc, Point2D position)
 
208
        {
 
209
                if(txt==null || position==null || doc==null)
 
210
                        return null;
 
211
                
 
212
                SVGTextElement textElt = new SVGTextElement(doc);
 
213
                
 
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));
 
217
                
 
218
                return textElt;
 
219
        }
 
220
        
 
221
        
 
222
        
 
223
        
 
224
        @Override
 
225
        public SVGElement toSVG(SVGDocument doc)
 
226
        {
 
227
                if(doc == null)
 
228
                        return null;
 
229
 
 
230
                SVGElement root = new SVGGElement(doc);
 
231
                Text t = (Text)getShape();
 
232
                String ltdPref = LaTeXDrawNamespace.LATEXDRAW_NAMESPACE + ':';
 
233
                
 
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));
 
240
                
 
241
                if(t.isItalic())
 
242
                        root.setAttribute(SVGAttributes.SVG_FONT_STYLE, SVGAttributes.SVG_FONT_STYLE_ITALIC);
 
243
                
 
244
                if(t.isBold())
 
245
                        root.setAttribute(SVGAttributes.SVG_FONT_WEIGHT, SVGAttributes.SVG_FONT_WEIGHT_BOLD);
 
246
                
 
247
                Element txt = new SVGTextElement(doc);
 
248
                String str  = t.getCodePSTricksBody(new DrawBorders(false), Figure.PPC);
 
249
                String cols = DviPsColors.getUserColoursCode(str);
 
250
                
 
251
                if(cols!=null)
 
252
                        cols = cols.replace(System.getProperty("line.separator"), "");
 
253
                
 
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);
 
258
                
 
259
                setSVGRotationAttribute(root);
 
260
                
 
261
                return root;
 
262
        }
 
263
}