KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.batik.gvt.text.ArabicTextHandler;
21 import org.apache.batik.gvt.font.GVTFontFace;
22 import org.w3c.dom.Element JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24
25 /**
26  * Bridge class for the <font> element.
27  *
28  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
29  * @version $Id: SVGFontElementBridge.java,v 1.9 2004/08/18 07:12:34 vhardy Exp $
30  */

31 public class SVGFontElementBridge extends AbstractSVGBridge {
32
33     /**
34      * Constructs a new bridge for the &lt;font> element.
35      */

36     public SVGFontElementBridge() {
37     }
38
39     /**
40      * Returns 'font'.
41      */

42     public String JavaDoc getLocalName() {
43         return SVG_FONT_TAG;
44     }
45
46     /**
47      * Constructs a new SVGGVTFont that represents the specified &lt;font> element
48      * at the requested size.
49      *
50      * @param ctx The current bridge context.
51      * @param fontElement The font element to base the SVGGVTFont construction on.
52      * @param textElement The text element that will use the new font.
53      * @param size The size of the new font.
54      * @param fontFace The font face object that contains the font attributes.
55      *
56      * @return The new SVGGVTFont.
57      */

58     public SVGGVTFont createFont(BridgeContext ctx,
59                                  Element JavaDoc fontElement,
60                                  Element JavaDoc textElement,
61                                  float size,
62                                  GVTFontFace fontFace) {
63
64
65         // construct a list of glyph codes that this font can display and
66
// a list of the glyph elements
67
NodeList JavaDoc glyphElements = fontElement.getElementsByTagNameNS
68         (SVG_NAMESPACE_URI, SVG_GLYPH_TAG);
69         int numGlyphs = glyphElements.getLength();
70         String JavaDoc[] glyphCodes = new String JavaDoc[numGlyphs];
71         String JavaDoc[] glyphNames = new String JavaDoc[numGlyphs];
72         String JavaDoc[] glyphLangs = new String JavaDoc[numGlyphs];
73         String JavaDoc[] glyphOrientations = new String JavaDoc[numGlyphs];
74         String JavaDoc[] glyphForms = new String JavaDoc[numGlyphs];
75         Element JavaDoc[] glyphElementArray = new Element JavaDoc[numGlyphs];
76
77         for (int i = 0; i < numGlyphs; i++) {
78             Element JavaDoc glyphElement = (Element JavaDoc)glyphElements.item(i);
79             glyphCodes[i] = glyphElement.getAttributeNS(null, SVG_UNICODE_ATTRIBUTE);
80             if (glyphCodes[i].length() > 1) {
81                 // ligature, may need to reverse if arabic so that it is in visual order
82
if (ArabicTextHandler.arabicChar(glyphCodes[i].charAt(0))) {
83                     glyphCodes[i] = (new StringBuffer JavaDoc(glyphCodes[i])).reverse().toString();
84                 }
85             }
86             glyphNames[i] = glyphElement.getAttributeNS(null, SVG_GLYPH_NAME_ATTRIBUTE);
87             glyphLangs[i] = glyphElement.getAttributeNS(null, SVG_LANG_ATTRIBUTE);
88             glyphOrientations[i] = glyphElement.getAttributeNS(null, SVG_ORIENTATION_ATTRIBUTE);
89             glyphForms[i] = glyphElement.getAttributeNS(null, SVG_ARABIC_FORM_ATTRIBUTE);
90             glyphElementArray[i] = glyphElement;
91         }
92
93         // get the missing glyph element
94
NodeList JavaDoc missingGlyphElements = fontElement.getElementsByTagNameNS
95         (SVG_NAMESPACE_URI, SVG_MISSING_GLYPH_TAG);
96         Element JavaDoc missingGlyphElement = null;
97         if (missingGlyphElements.getLength() > 0) {
98             missingGlyphElement = (Element JavaDoc)missingGlyphElements.item(0);
99         }
100
101         // get the hkern elements
102
NodeList JavaDoc hkernElements = fontElement.getElementsByTagNameNS
103         (SVG_NAMESPACE_URI, SVG_HKERN_TAG);
104         Element JavaDoc[] hkernElementArray = new Element JavaDoc[hkernElements.getLength()];
105
106         for (int i = 0; i < hkernElementArray.length; i++) {
107             Element JavaDoc hkernElement = (Element JavaDoc)hkernElements.item(i);
108             hkernElementArray[i] = hkernElement;
109         }
110
111         // get the vkern elements
112
NodeList JavaDoc vkernElements = fontElement.getElementsByTagNameNS
113         (SVG_NAMESPACE_URI, SVG_VKERN_TAG);
114         Element JavaDoc[] vkernElementArray = new Element JavaDoc[vkernElements.getLength()];
115
116         for (int i = 0; i < vkernElementArray.length; i++) {
117             Element JavaDoc vkernElement = (Element JavaDoc)vkernElements.item(i);
118             vkernElementArray[i] = vkernElement;
119         }
120
121         // return the new SVGGVTFont
122
return new SVGGVTFont
123             (size, fontFace, glyphCodes, glyphNames, glyphLangs,
124              glyphOrientations, glyphForms, ctx,
125              glyphElementArray, missingGlyphElement,
126              hkernElementArray, vkernElementArray, textElement);
127     }
128 }
129
Popular Tags