KickJava   Java API By Example, From Geeks To Geeks.

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


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.RenderingHints JavaDoc;
24 import java.awt.font.FontRenderContext JavaDoc;
25
26 /**
27  * This test validates the convertion of Java 2D text into
28  * SVG Shapes, one of the options of the SVGGraphics2D constructor.
29  * This is the same test as Font testing with regards to the
30  * Java 2D API code, except that it validates text to shapes
31  * convertion.
32  *
33  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
34  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
35  * @version $Id: Font2.java,v 1.7 2004/08/18 07:16:44 vhardy Exp $
36  */

37 public class Font2 implements Painter {
38     public void paint(Graphics2D JavaDoc g) {
39         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
40                            RenderingHints.VALUE_ANTIALIAS_ON);
41
42         // Set default font
43
g.setFont(new Font JavaDoc("Arial", Font.BOLD, 12));
44
45         // Colors used for labels and test output
46
Color JavaDoc labelColor = new Color JavaDoc(0x666699);
47         Color JavaDoc fontColor = Color.black;
48
49         //
50
// First, font size
51
//
52
java.awt.geom.AffineTransform JavaDoc defaultTransform = g.getTransform();
53         Font JavaDoc defaultFont = new Font JavaDoc("Arial", Font.BOLD, 16);
54         g.setFont(defaultFont);
55         FontRenderContext JavaDoc frc = g.getFontRenderContext();
56         g.setPaint(labelColor);
57
58         g.drawString("Font size", 10, 30);
59         g.setPaint(fontColor);
60         g.translate(0, 20);
61         int fontSizes[] = { 6, 8, 10, 12, 18, 36, 48 };
62         for(int i=0; i<fontSizes.length; i++){
63             Font JavaDoc font = new Font JavaDoc(defaultFont.getFamily(),
64                                  Font.PLAIN,
65                                  fontSizes[i]);
66             g.setFont(font);
67             g.drawString("aA", 10, 40);
68             double width = font.createGlyphVector(frc, "aA").getVisualBounds().getWidth();
69             g.translate(width*1.2, 0);
70         }
71
72         g.setTransform(defaultTransform);
73         g.translate(0, 60);
74
75         //
76
// Font style
77
//
78
int fontStyles[] = { Font.PLAIN,
79                              Font.BOLD,
80                              Font.ITALIC,
81                              Font.BOLD | Font.ITALIC };
82         String JavaDoc fontStyleStrings[] = { "Plain", "Bold", "Italic", "Bold Italic" };
83
84         g.setFont(defaultFont);
85         g.setPaint(labelColor);
86         g.drawString("Font Styles", 10, 30);
87         g.translate(0, 20);
88         g.setPaint(fontColor);
89
90         for(int i=0; i<fontStyles.length; i++){
91             Font JavaDoc font = new Font JavaDoc(defaultFont.getFamily(),
92                                  fontStyles[i], 20);
93             g.setFont(font);
94             g.drawString(fontStyleStrings[i], 10, 40);
95             double width = font.createGlyphVector(frc, fontStyleStrings[i]).getVisualBounds().getWidth();
96             g.translate(width*1.2, 0);
97         }
98
99         g.setTransform(defaultTransform);
100         g.translate(0, 120);
101
102         //
103
// Font families
104
//
105
String JavaDoc fontFamilies[] = { "Arial",
106                                   "Times New Roman",
107                                   "Courier New",
108                                   "Verdana" };
109
110         g.setFont(defaultFont);
111         g.setPaint(labelColor);
112         g.drawString("Font Families", 10, 30);
113         g.setPaint(fontColor);
114
115         for(int i=0; i<fontFamilies.length; i++){
116             Font JavaDoc font = new Font JavaDoc(fontFamilies[i], Font.PLAIN, 18);
117             g.setFont(font);
118             double height = font.createGlyphVector(frc, fontFamilies[i]).getVisualBounds().getHeight();
119             g.translate(0, height*1.4);
120             g.drawString(fontFamilies[i], 10, 40);
121         }
122
123         //
124
// Logical fonts
125
//
126
Font JavaDoc logicalFonts[] = { new Font JavaDoc("dialog", Font.PLAIN, 14),
127                                   new Font JavaDoc("dialoginput", Font.BOLD, 14),
128                                   new Font JavaDoc("monospaced", Font.ITALIC, 14),
129                                   new Font JavaDoc("serif", Font.PLAIN, 14),
130                                   new Font JavaDoc("sansserif", Font.BOLD, 14)};
131
132           g.translate(0, 70);
133           g.setFont(defaultFont);
134           g.setPaint(labelColor);
135           g.drawString("Logical Fonts", 10, 0);
136           g.setPaint(fontColor);
137
138           for(int i=0; i<logicalFonts.length; i++){
139               Font JavaDoc font = logicalFonts[i];
140               g.setFont(font);
141               double height = font.createGlyphVector(frc, font.getName()).getVisualBounds().getHeight();
142               g.translate(0, height*1.4);
143               g.drawString(font.getName(), 10, 0);
144           }
145     }
146 }
147
Popular Tags