My favorites
▼
|
Sign in
lindenb
Pierre Lindenbaum's library
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
src
/
java
/
org
/
lindenb
/
svg
/
SVGRenderer.java
‹r349
r508
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package org.lindenb.svg;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import org.lindenb.awt.Cap;
import org.lindenb.awt.ColorUtils;
import org.lindenb.awt.Dimension2D;
import org.lindenb.awt.Join;
import org.lindenb.lang.InvalidXMLException;
import org.lindenb.sw.vocabulary.SVG;
import org.lindenb.util.StringUtils;
import org.lindenb.xml.XMLUtilities;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
/**
* A Basic Renderer for Scalable Vector Graphics SVG
* @author lindenb
*
*/
public class SVGRenderer
extends SVGUtils
{
public SVGRenderer()
{
}
/**
* Record the Graphic context for a given node
* @author lindenb
*
*/
private static class Shuttle
{
/** current graphics2D */
Graphics2D g;
/** the svg:svg root */
Element svgRoot;
/** current node */
Element node;
/** current clip */
Shape clip;
/** current shape */
Shape shape;
float fontSize=12;
String fontFamily=null;
Paint fill=null;
Paint stroke=null;
float strokeWidth;
Cap linecap=Cap.BUTT;
Join linejoin=Join.MITER;
AffineTransform transform=new AffineTransform();
float opacity=1f;
public Shuttle(Graphics2D g,Element root)
{
this.g=g;
Font font= g.getFont();
this.fontSize= font.getSize();
this.fontFamily= font.getFamily();
this.svgRoot=root;
this.node=root;
this.transform=new AffineTransform(g.getTransform());
this.clip= g.getClip();
}
Shuttle(Shuttle cp,Element e)
{
this.node=e;
this.g = cp.g;
this.svgRoot = cp.svgRoot;
this.shape=cp.shape;
this.clip=cp.clip;
this.fontSize=cp.fontSize;
this.fontFamily=cp.fontFamily;
this.stroke=cp.stroke;
this.fill=cp.fill;
this.strokeWidth=cp.strokeWidth;
this.transform=new AffineTransform(cp.transform);
this.opacity=cp.opacity;
if(e.hasAttributes())
{
NamedNodeMap atts=e.getAttributes();
for(int i=0;i< atts.getLength();++i)
{
Attr att= Attr.class.cast(atts.item(i));
if(att.getNamespaceURI()!=null) continue;
String s=att.getName();
String value= att.getValue();
if(s.equals("style"))
{
for(String styles:value.split("[;]+"))
{
int j=styles.indexOf(':');
if(j!=-1)
{
applyStyle(styles.substring(0,j).trim(),styles.substring(j+1).trim());
}
}
}
else
{
applyStyle(s,att.getValue());
}
}
}
}
private void applyStyle(String key,String value)
{
if(key.equals("fill"))
{
if(value!=null && value.equals("none")) return;
this.fill= ColorUtils.parseColor(value);
}
else if(key.equals("stroke"))
{
if(value!=null && value.equals("none")) return;
this.stroke= ColorUtils.parseColor(value);
}
else if(key.equals("stroke-width"))
{
this.strokeWidth= Float.parseFloat(value);
}
else if(key.equals("stroke-linecap"))
{
this.linecap= Cap.parseSVG(value);
}
else if(key.equals("stroke-linejoin"))
{
this.linejoin=Join.parseSVG(value);
}
else if(key.equals("transform"))
{
AffineTransform tr=svgToaffineTransform(value);
//tr.concatenate(this.transform);
//this.transform=tr;
this.transform.concatenate(tr);
}
else if(key.equals("font-size"))
{
this.fontSize= Float.parseFloat(value);
}
else if(key.equals("font-family"))
{
this.fontFamily= value;
}
else if(key.equals("opacity"))
{
this.opacity *= Float.parseFloat(value);
}
}
}
public void paint(
Graphics2D g,
Node dom
) throws InvalidXMLException
{
paint(g,dom,null);
}
public void paint(
Graphics2D g,
Node dom,
Rectangle2D viewRect
) throws InvalidXMLException
{
if(g==null) throw new NullPointerException("g is null");
if(dom==null) throw new NullPointerException("dom is null");
Element root=null;
if(dom.getNodeType()==Node.DOCUMENT_NODE)
{
root= Document.class.cast(dom).getDocumentElement();
}
else if(dom.getNodeType()==Node.ELEMENT_NODE)
{
root = Element.class.cast(dom);
}
if(root==null) throw new InvalidXMLException(dom,"no root");
if(!XMLUtilities.isA(root, SVG.NS, "svg")) throw new InvalidXMLException(root,"not a SVG root");
Dimension2D srcSize= getSize(root);
Rectangle2D viewBox=null;
Attr viewBoxAttr = root.getAttributeNode("viewBox");
if(viewBoxAttr!=null)
{
String tokens[]= viewBoxAttr.getValue().trim().split("[ \t\n]+");
if(tokens.length!=4) throw new InvalidXMLException(viewBoxAttr,"invalid ");
viewBox = new Rectangle2D.Double(
Double.parseDouble(tokens[0]),
Double.parseDouble(tokens[1]),
Double.parseDouble(tokens[2]),
Double.parseDouble(tokens[3])
);
srcSize= new Dimension2D.Double(viewBox.getWidth(),viewBox.getHeight());
}
AffineTransform originalTr=null;
if(viewRect==null)
{
viewRect = new Rectangle2D.Double(0,0,srcSize.getWidth(),srcSize.getHeight());
}
else
{
if(srcSize.getWidth()>0 && srcSize.getHeight()>0)
{
originalTr= g.getTransform();
double ratio= Math.min(
viewRect.getWidth()/srcSize.getWidth(),
viewRect.getHeight()/srcSize.getHeight()
);
g.translate(
(viewRect.getWidth() -srcSize.getWidth()*ratio)/2.0,
(viewRect.getHeight()-srcSize.getHeight()*ratio)/2.0
);
g.scale(ratio,ratio);
}
}
Shape oldclip= g.getClip();
Shuttle shuttle=new Shuttle(g,root);
if(viewBox!=null)
{
AffineTransform tr= AffineTransform.getTranslateInstance(
-viewBox.getX(),
-viewBox.getY()
);
shuttle.transform.concatenate(tr);
Area area= new Area(shuttle.clip);
area.intersect(new Area(new Rectangle2D.Double(
0,0,
viewBox.getWidth(),
viewBox.getHeight()
)));
shuttle.clip= area;
}
paint(shuttle);
if(originalTr!=null)
{
g.setTransform(originalTr);
}
g.setClip(oldclip);
}
private void paint(Shuttle shuttle) throws InvalidXMLException
{
Element e= shuttle.node;
String shapeName= e.getLocalName();
if(!SVG.NS.equals(e.getNamespaceURI()))
{
for(Node c=e.getFirstChild();c!=null;c=c.getNextSibling())
{
if(c.getNodeType()!=Node.ELEMENT_NODE) continue;
Shuttle cp= new Shuttle(shuttle,Element.class.cast(c));
paint(cp);
}
}
else if(shapeName==null)
{
LOG.warning("shapeName is null");
}
else if(shapeName.equals("g"))
{
for(Node c=e.getFirstChild();c!=null;c=c.getNextSibling())
{
if(c.getNodeType()!=Node.ELEMENT_NODE) continue;
Shuttle cp= new Shuttle(shuttle,Element.class.cast(c));
paint(cp);
}
}
else if(shapeName.equals("path"))
{
Attr d= e.getAttributeNode("d");
if(d!=null)
{
shuttle.shape = SVGUtils.pathToShape(d.getValue());
drawShape(shuttle);
}
}
else if(shapeName.equals("polyline"))
{
Attr points= e.getAttributeNode("points");
if(points!=null)
{
shuttle.shape = SVGUtils.polylineToShape(points.getValue());
drawShape(shuttle);
}
}
else if(shapeName.equals("polygon"))
{
Attr points= e.getAttributeNode("points");
if(points!=null)
{
shuttle.shape = SVGUtils.polygonToShape(points.getValue());
drawShape(shuttle);
}
}
else if(shapeName.equals("rect"))
{
Attr x= e.getAttributeNode("x");
Attr y= e.getAttributeNode("y");
Attr w= e.getAttributeNode("width");
Attr h= e.getAttributeNode("height");
if(x!=null && y!=null && w!=null && h!=null)
{
shuttle.shape =new Rectangle2D.Double(
Double.parseDouble(x.getValue()),
Double.parseDouble(y.getValue()),
Double.parseDouble(w.getValue()),
Double.parseDouble(h.getValue())
);
drawShape(shuttle);
}
}
else if(shapeName.equals("line"))
{
Attr x1= e.getAttributeNode("x1");
Attr y1= e.getAttributeNode("y1");
Attr x2= e.getAttributeNode("x2");
Attr y2= e.getAttributeNode("y2");
if(x1!=null && y1!=null && x2!=null && y2!=null)
{
shuttle.shape =new Line2D.Double(
Double.parseDouble(x1.getValue()),
Double.parseDouble(y1.getValue()),
Double.parseDouble(x2.getValue()),
Double.parseDouble(y2.getValue())
);
drawShape(shuttle);
}
}
else if(shapeName.equals("circle"))
{
Attr cx= e.getAttributeNode("cx");
Attr cy= e.getAttributeNode("cy");
Attr r= e.getAttributeNode("r");
if(cx!=null && cy!=null && r!=null)
{
double radius=Double.parseDouble(r.getValue());
shuttle.shape =new Ellipse2D.Double(
Double.parseDouble(cx.getValue())-radius,
Double.parseDouble(cy.getValue())-radius,
radius*2,
radius*2
);
drawShape(shuttle);
}
}
else if(shapeName.equals("ellipse"))
{
Attr cx= e.getAttributeNode("cx");
Attr cy= e.getAttributeNode("cy");
Attr rx= e.getAttributeNode("rx");
Attr ry= e.getAttributeNode("ry");
if(cx!=null && cy!=null && rx!=null && ry!=null)
{
double radiusx=Double.parseDouble(rx.getValue());
double radiusy=Double.parseDouble(ry.getValue());
shuttle.shape =new Ellipse2D.Double(
Double.parseDouble(cx.getValue())-radiusx,
Double.parseDouble(cy.getValue())-radiusy,
radiusx*2,
radiusy*2
);
drawShape(shuttle);
}
}
else if(StringUtils.isIn(shapeName,"title","defs","desc","metadata"))
{
//ignore
}
else if(shapeName.equals("text"))
{
Attr x= e.getAttributeNode("x");
Attr y= e.getAttributeNode("y");
if(x!=null && y!=null)
{
Font f= new Font(shuttle.fontFamily,Font.PLAIN,(int)shuttle.fontSize);
FontRenderContext frc = shuttle.g.getFontRenderContext();
TextLayout tl = new TextLayout(e.getTextContent(), f, frc);
shuttle.shape= tl.getOutline(null);
shuttle.shape = AffineTransform.getTranslateInstance(
Double.parseDouble(x.getValue()),
Double.parseDouble(y.getValue())).createTransformedShape(shuttle.shape)
;
drawShape(shuttle);
}
}
else if(shapeName.equals("svg"))
{
for(Node c=e.getFirstChild();c!=null;c=c.getNextSibling())
{
if(c.getNodeType()!=Node.ELEMENT_NODE) continue;
Shuttle cp = new Shuttle(shuttle,Element.class.cast(c));
paint(cp);
}
}
else
{
LOG.warning("cannot display <"+e.getLocalName()+">");
}
}
private void drawShape(Shuttle shuttle)
{
Graphics2D g= shuttle.g;
Shape oldclip=shuttle.g.getClip();
g.setClip(shuttle.clip);
Composite oldcomposite=g.getComposite();
if(shuttle.opacity!=1f)
{
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,shuttle.opacity));
}
AffineTransform oldtr=g.getTransform();
g.setTransform(shuttle.transform);
Stroke oldStroke= g.getStroke();
Stroke newStroke= new BasicStroke(
shuttle.strokeWidth,
shuttle.linecap.stroke(),
shuttle.linejoin.stroke()
);
g.setStroke(newStroke);
if(shuttle.fill!=null)
{
g.setPaint(shuttle.fill);
g.fill(shuttle.shape);
}
if(shuttle.stroke!=null)
{
g.setPaint(shuttle.stroke);
g.draw(shuttle.shape);
}
g.setClip(oldclip);
g.setStroke(oldStroke);
g.setTransform(oldtr);
g.setComposite(oldcomposite);
}
}
Show details
Hide details
Change log
r350
by plindenbaum on Jul 21, 2009
Diff
cont
Go to:
...nb/lang/InvalidXMLException.java
...ava/org/lindenb/svg/SVGIcon.java
...org/lindenb/svg/SVGRenderer.java
Project members,
sign in
to write a code review
Older revisions
r349
by plindenbaum on Jul 21, 2009
Diff
cont
All revisions of this file
File info
Size: 11766 bytes, 487 lines
View raw file
Powered by
Google Project Hosting