KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGFontFamily


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.bridge;
19
20 import java.text.AttributedCharacterIterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.batik.gvt.font.GVTFont;
24 import org.apache.batik.gvt.font.GVTFontFace;
25 import org.apache.batik.gvt.font.GVTFontFamily;
26 import org.apache.batik.gvt.text.GVTAttributedCharacterIterator;
27 import org.apache.batik.util.SVGConstants;
28
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 /**
34  * A font family class for SVG fonts.
35  *
36  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
37  * @version $Id: SVGFontFamily.java,v 1.9 2004/11/18 01:46:53 deweese Exp $
38  */

39 public class SVGFontFamily implements GVTFontFamily {
40
41     public static final
42         AttributedCharacterIterator.Attribute JavaDoc TEXT_COMPOUND_DELIMITER =
43         GVTAttributedCharacterIterator.TextAttribute.TEXT_COMPOUND_DELIMITER;
44
45     protected GVTFontFace fontFace;
46     protected Element JavaDoc fontElement;
47     protected BridgeContext ctx;
48     protected Boolean JavaDoc complex = null;
49     
50
51
52     /**
53      * Constructs an SVGFontFamily.
54      *
55      * @param fontFace The font face object that describes this font family.
56      * @param fontElement The element that contains the font data for this family.
57      * @param ctx The bridge context. This is required for lazily loading the
58      * font data at render time.
59      */

60     public SVGFontFamily(GVTFontFace fontFace,
61                          Element JavaDoc fontElement,
62                          BridgeContext ctx) {
63         this.fontFace = fontFace;
64         this.fontElement = fontElement;
65         this.ctx = ctx;
66     }
67
68     /**
69      * Returns the family name of this font.
70      *
71      * @return The font family name.
72      */

73     public String JavaDoc getFamilyName() {
74         return fontFace.getFamilyName();
75     }
76
77     /**
78      * Returns the font-face associated with this font family.
79      *
80      * @return The font face.
81      */

82     public GVTFontFace getFontFace() {
83         return fontFace;
84     }
85
86     /**
87      * Derives a GVTFont object of the correct size.
88      *
89      * @param size The required size of the derived font.
90      * @param aci The character iterator containing the text to be rendered
91      * using the derived font.
92      *
93      * @return The derived font.
94      */

95     public GVTFont deriveFont(float size, AttributedCharacterIterator JavaDoc aci) {
96         return deriveFont(size, aci.getAttributes());
97     }
98
99     /**
100      * Derives a GVTFont object of the correct size from an attribute Map.
101      * @param size The required size of the derived font.
102      * @param attrs The Attribute Map to get Values from.
103      */

104     public GVTFont deriveFont(float size, Map JavaDoc attrs) {
105         SVGFontElementBridge fontBridge;
106         fontBridge = (SVGFontElementBridge)ctx.getBridge(fontElement);
107         Element JavaDoc textElement;
108         textElement = (Element JavaDoc)attrs.get(TEXT_COMPOUND_DELIMITER);
109         return fontBridge.createFont(ctx, fontElement, textElement,
110                                      size, fontFace);
111     }
112      
113     /**
114      * This method looks at the SVG font and checks if any of
115      * the glyphs use renderable child elements. If so this
116      * is a complex font in that full CSS inheritance needs to
117      * be applied. Otherwise if it only uses the 'd' attribute
118      * it does not need CSS treatment.
119      */

120     public boolean isComplex() {
121         if (complex != null) return complex.booleanValue();
122         boolean ret = isComplex(fontElement, ctx);
123         complex = new Boolean JavaDoc(ret);
124         return ret;
125     }
126
127     public static boolean isComplex(Element JavaDoc fontElement, BridgeContext ctx) {
128         NodeList JavaDoc glyphElements = fontElement.getElementsByTagNameNS
129         (SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_GLYPH_TAG);
130
131         int numGlyphs = glyphElements.getLength();
132         for (int i = 0; i < numGlyphs; i++) {
133             Element JavaDoc glyph = (Element JavaDoc)glyphElements.item(i);
134             Node JavaDoc child = glyph.getFirstChild();
135             for (;child != null; child = child.getNextSibling()) {
136                 if (child.getNodeType() != Node.ELEMENT_NODE)
137                     continue;
138                 Element JavaDoc e = (Element JavaDoc)child;
139                 Bridge b = ctx.getBridge(e);
140                 if ((b != null) && (b instanceof GraphicsNodeBridge)) {
141                     return true;
142                 }
143             }
144         }
145         return false;
146     }
147 }
148
Popular Tags