KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > AttributedCharacterIterator


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Font JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.font.TextAttribute JavaDoc;
24 import java.text.AttributedString JavaDoc;
25
26
27 /**
28  * This test validates the convertion of Java 2D AffineTransform into SVG
29  * Shapes.
30  *
31  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
32  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
33  * @version $Id: AttributedCharacterIterator.java,v 1.6 2004/08/18 07:16:43 vhardy Exp $
34  */

35 public class AttributedCharacterIterator implements Painter {
36     public void paint(Graphics2D JavaDoc g) {
37         String JavaDoc fontName = "Arial";
38         int fontSize = 15;
39
40         String JavaDoc text = "Attributed Strings are fun !";
41         AttributedString JavaDoc styledText = new AttributedString JavaDoc(text);
42
43         //
44
// Set font family for the whole string
45
//
46
Font JavaDoc font = new Font JavaDoc(fontName, Font.PLAIN, fontSize);
47         styledText.addAttribute(TextAttribute.FAMILY, font.getFamily());
48         styledText.addAttribute(TextAttribute.SIZE, new Float JavaDoc(font.getSize()));
49         styledText.addAttribute(TextAttribute.FOREGROUND, Color.black);
50
51         //
52
// Set font style attributes for different part of the string
53
//
54

55         // "Attributed" is in Bold
56
styledText.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, 0, 10);
57
58         // "String" is italic
59
// styledText.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, 11, 18);
60

61         // fun is Bold and underlined
62
styledText.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 23, 28);
63         // styledText.addAttribute(TextAttribute.SWAP_COLORS, TextAttribute.SWAP_COLORS_ON);
64
// styledText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 23, 28);
65

66         /*TextLayout aLayout = new TextLayout("A", font, frc);
67           Shape aShape = aLayout.getOutline(null);
68
69           ShapeGraphicAttribute aReplacement = new ShapeGraphicAttribute(aShape, GraphicAttribute.ROMAN_BASELINE, true);
70           styledText.addAttribute(TextAttribute.CHAR_REPLACEMENT, aReplacement, 0, 1);
71
72
73           // Create a BufferedImage to decorate the Shape
74           {
75           TextLayout aLayout = new TextLayout("A", font, frc);
76           Shape aShape = aLayout.getOutline(null);
77           Rectangle bounds = aShape.getBounds();
78
79           int blurWidth = 6;
80           BufferedImage image = new BufferedImage(bounds.width + blurWidth*4, bounds.height + blurWidth*4,
81           BufferedImage.TYPE_INT_ARGB);
82           Graphics2D g2 = image.createGraphics();
83           int w = image.getWidth(), h = image.getHeight();
84           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
85           g2.setPaint(Color.black);
86           g2.translate(-bounds.x + (w - bounds.width)/2, -bounds.y + (h - bounds.height)/2);
87           g2.fill(aShape);
88           g2.setStroke(new BasicStroke(blurWidth/2));
89           g2.draw(aShape);
90           g2.dispose();
91
92           float k[] = new float[blurWidth*blurWidth];
93           for(int i=0; i<k.length; i++) k[i] = 1/(float)k.length;
94           Kernel kernel = new Kernel(blurWidth, blurWidth, k);
95           ConvolveOp blur = new ConvolveOp(kernel);
96           image = blur.filter(image, null);
97           g2 = image.createGraphics();
98           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
99           g2.translate(-bounds.x + (w - bounds.width)/2, -bounds.y + (h - bounds.height)/2);
100           g2.setComposite(AlphaComposite.Clear);
101           g2.fill(aShape);
102
103           image = image.getSubimage(blurWidth, blurWidth, image.getWidth() - 2*blurWidth, image.getHeight() - 2*blurWidth);
104
105           ImageGraphicAttribute aImageReplacement = new ImageGraphicAttribute(image, GraphicAttribute.ROMAN_BASELINE, blurWidth,
106           blurWidth + bounds.height);
107           styledText.addAttribute(TextAttribute.CHAR_REPLACEMENT, aImageReplacement, 0, 1);
108           }
109         */

110
111         //
112
// Set text color
113
//
114

115         // "Attributed" is in dard red
116
styledText.addAttribute(TextAttribute.FOREGROUND, new Color JavaDoc(128, 0, 0), 0, 10);
117
118         // "String" is blue
119
styledText.addAttribute(TextAttribute.FOREGROUND, new Color JavaDoc(70, 107, 132), 11, 18);
120
121         // "fun" is yellow on blue background
122
styledText.addAttribute(TextAttribute.FOREGROUND, new Color JavaDoc(236, 214, 70), 23, 28);
123         styledText.addAttribute(TextAttribute.BACKGROUND, new Color JavaDoc(70, 107, 132), 23, 28);
124
125         java.text.AttributedCharacterIterator JavaDoc iter = styledText.getIterator();
126         /*TextLayout layout = new TextLayout(iter, frc);
127
128         Rectangle bounds = layout.getBounds().getBounds();
129         bounds.width += 50;
130         bounds.height += 50;
131
132         layout.draw(g, 25, layout.getAscent() + 25);*/

133         g.drawString(iter, 10, 100);
134     }
135 }
136
Popular Tags