001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import org.apache.batik.gvt.text.ArabicTextHandler;
022: import org.apache.batik.gvt.font.GVTFontFace;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.NodeList;
025:
026: /**
027: * Bridge class for the <font> element.
028: *
029: * @author <a href="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
030: * @version $Id: SVGFontElementBridge.java 475685 2006-11-16 11:16:05Z cam $
031: */
032: public class SVGFontElementBridge extends AbstractSVGBridge {
033:
034: /**
035: * Constructs a new bridge for the <font> element.
036: */
037: public SVGFontElementBridge() {
038: }
039:
040: /**
041: * Returns 'font'.
042: */
043: public String getLocalName() {
044: return SVG_FONT_TAG;
045: }
046:
047: /**
048: * Constructs a new SVGGVTFont that represents the specified <font> element
049: * at the requested size.
050: *
051: * @param ctx The current bridge context.
052: * @param fontElement The font element to base the SVGGVTFont construction on.
053: * @param textElement The text element that will use the new font.
054: * @param size The size of the new font.
055: * @param fontFace The font face object that contains the font attributes.
056: *
057: * @return The new SVGGVTFont.
058: */
059: public SVGGVTFont createFont(BridgeContext ctx,
060: Element fontElement, Element textElement, float size,
061: GVTFontFace fontFace) {
062:
063: // construct a list of glyph codes that this font can display and
064: // a list of the glyph elements
065: NodeList glyphElements = fontElement.getElementsByTagNameNS(
066: SVG_NAMESPACE_URI, SVG_GLYPH_TAG);
067: int numGlyphs = glyphElements.getLength();
068: String[] glyphCodes = new String[numGlyphs];
069: String[] glyphNames = new String[numGlyphs];
070: String[] glyphLangs = new String[numGlyphs];
071: String[] glyphOrientations = new String[numGlyphs];
072: String[] glyphForms = new String[numGlyphs];
073: Element[] glyphElementArray = new Element[numGlyphs];
074:
075: for (int i = 0; i < numGlyphs; i++) {
076: Element glyphElement = (Element) glyphElements.item(i);
077: glyphCodes[i] = glyphElement.getAttributeNS(null,
078: SVG_UNICODE_ATTRIBUTE);
079: if (glyphCodes[i].length() > 1) {
080: // ligature, may need to reverse if arabic so that it is in visual order
081: if (ArabicTextHandler.arabicChar(glyphCodes[i]
082: .charAt(0))) {
083: glyphCodes[i] = (new StringBuffer(glyphCodes[i]))
084: .reverse().toString();
085: }
086: }
087: glyphNames[i] = glyphElement.getAttributeNS(null,
088: SVG_GLYPH_NAME_ATTRIBUTE);
089: glyphLangs[i] = glyphElement.getAttributeNS(null,
090: SVG_LANG_ATTRIBUTE);
091: glyphOrientations[i] = glyphElement.getAttributeNS(null,
092: SVG_ORIENTATION_ATTRIBUTE);
093: glyphForms[i] = glyphElement.getAttributeNS(null,
094: SVG_ARABIC_FORM_ATTRIBUTE);
095: glyphElementArray[i] = glyphElement;
096: }
097:
098: // get the missing glyph element
099: NodeList missingGlyphElements = fontElement
100: .getElementsByTagNameNS(SVG_NAMESPACE_URI,
101: SVG_MISSING_GLYPH_TAG);
102: Element missingGlyphElement = null;
103: if (missingGlyphElements.getLength() > 0) {
104: missingGlyphElement = (Element) missingGlyphElements
105: .item(0);
106: }
107:
108: // get the hkern elements
109: NodeList hkernElements = fontElement.getElementsByTagNameNS(
110: SVG_NAMESPACE_URI, SVG_HKERN_TAG);
111: Element[] hkernElementArray = new Element[hkernElements
112: .getLength()];
113:
114: for (int i = 0; i < hkernElementArray.length; i++) {
115: Element hkernElement = (Element) hkernElements.item(i);
116: hkernElementArray[i] = hkernElement;
117: }
118:
119: // get the vkern elements
120: NodeList vkernElements = fontElement.getElementsByTagNameNS(
121: SVG_NAMESPACE_URI, SVG_VKERN_TAG);
122: Element[] vkernElementArray = new Element[vkernElements
123: .getLength()];
124:
125: for (int i = 0; i < vkernElementArray.length; i++) {
126: Element vkernElement = (Element) vkernElements.item(i);
127: vkernElementArray[i] = vkernElement;
128: }
129:
130: // return the new SVGGVTFont
131: return new SVGGVTFont(size, fontFace, glyphCodes, glyphNames,
132: glyphLangs, glyphOrientations, glyphForms, ctx,
133: glyphElementArray, missingGlyphElement,
134: hkernElementArray, vkernElementArray, textElement);
135: }
136: }
|