KickJava   Java API By Example, From Geeks To Geeks.

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


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 Fonts into
28  * SVG font attributes.
29  *
30  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
31  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
32  * @version $Id: Font1.java,v 1.7 2004/08/18 07:16:44 vhardy Exp $
33  */

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