1 18 package org.apache.batik.bridge; 19 20 import java.util.StringTokenizer ; 21 import java.util.List ; 22 import java.util.ArrayList ; 23 24 import org.apache.batik.gvt.font.Kern; 25 import org.apache.batik.gvt.font.UnicodeRange; 26 import org.w3c.dom.Element ; 27 28 34 public abstract class SVGKernElementBridge extends AbstractSVGBridge { 35 36 46 public Kern createKern(BridgeContext ctx, 47 Element kernElement, 48 SVGGVTFont font) { 49 50 String u1 = kernElement.getAttributeNS(null, SVG_U1_ATTRIBUTE); 52 String u2 = kernElement.getAttributeNS(null, SVG_U2_ATTRIBUTE); 53 String g1 = kernElement.getAttributeNS(null, SVG_G1_ATTRIBUTE); 54 String g2 = kernElement.getAttributeNS(null, SVG_G2_ATTRIBUTE); 55 String k = kernElement.getAttributeNS(null, SVG_K_ATTRIBUTE); 56 if (k.length() == 0) { 57 k = SVG_KERN_K_DEFAULT_VALUE; 58 } 59 60 float kernValue = Float.parseFloat(k); 62 63 int firstGlyphLen = 0, secondGlyphLen = 0; 65 int [] firstGlyphSet = null; 66 int [] secondGlyphSet = null; 67 List firstUnicodeRanges = new ArrayList (); 68 List secondUnicodeRanges = new ArrayList (); 69 70 StringTokenizer st = new StringTokenizer (u1, ","); 72 while (st.hasMoreTokens()) { 73 String token = st.nextToken(); 74 if (token.startsWith("U+")) { firstUnicodeRanges.add(new UnicodeRange(token)); 76 } else { 77 int[] glyphCodes = font.getGlyphCodesForUnicode(token); 78 if (firstGlyphSet == null) { 79 firstGlyphSet = glyphCodes; 80 firstGlyphLen = glyphCodes.length; 81 }else { 82 if ((firstGlyphLen + glyphCodes.length) > 83 firstGlyphSet.length) { 84 int sz = firstGlyphSet.length*2; 85 if (sz <firstGlyphLen + glyphCodes.length) 86 sz = firstGlyphLen + glyphCodes.length; 87 int [] tmp = new int[sz]; 88 for (int i = 0; i < firstGlyphLen; i++) 89 tmp[i] = firstGlyphSet[i]; 90 firstGlyphSet = tmp; 91 } 92 for (int i = 0; i < glyphCodes.length; i++) 93 firstGlyphSet[firstGlyphLen++] = glyphCodes[i]; 94 } 95 } 96 } 97 98 st = new StringTokenizer (u2, ","); 100 while (st.hasMoreTokens()) { 101 String token = st.nextToken(); 102 if (token.startsWith("U+")) { secondUnicodeRanges.add(new UnicodeRange(token)); 104 } else { 105 int[] glyphCodes = font.getGlyphCodesForUnicode(token); 106 if (secondGlyphSet == null) { 107 secondGlyphSet = glyphCodes; 108 secondGlyphLen = glyphCodes.length; 109 } else { 110 if ((secondGlyphLen + glyphCodes.length) > 111 secondGlyphSet.length) { 112 int sz = secondGlyphSet.length*2; 113 if (sz <secondGlyphLen + glyphCodes.length) 114 sz = secondGlyphLen + glyphCodes.length; 115 int [] tmp = new int[sz]; 116 for (int i = 0; i < secondGlyphLen; i++) 117 tmp[i] = secondGlyphSet[i]; 118 secondGlyphSet = tmp; 119 } 120 for (int i = 0; i < glyphCodes.length; i++) 121 secondGlyphSet[secondGlyphLen++] = glyphCodes[i]; 122 } 123 } 124 } 125 126 st = new StringTokenizer (g1, ","); 128 while (st.hasMoreTokens()) { 129 String token = st.nextToken(); 130 int[] glyphCodes = font.getGlyphCodesForName(token); 131 if (firstGlyphSet == null) { 132 firstGlyphSet = glyphCodes; 133 firstGlyphLen = glyphCodes.length; 134 }else { 135 if ((firstGlyphLen + glyphCodes.length) > 136 firstGlyphSet.length) { 137 int sz = firstGlyphSet.length*2; 138 if (sz <firstGlyphLen + glyphCodes.length) 139 sz = firstGlyphLen + glyphCodes.length; 140 int [] tmp = new int[sz]; 141 for (int i = 0; i < firstGlyphLen; i++) 142 tmp[i] = firstGlyphSet[i]; 143 firstGlyphSet = tmp; 144 } 145 for (int i = 0; i < glyphCodes.length; i++) 146 firstGlyphSet[firstGlyphLen++] = glyphCodes[i]; 147 } 148 } 149 150 st = new StringTokenizer (g2, ","); 152 while (st.hasMoreTokens()) { 153 String token = st.nextToken(); 154 int[] glyphCodes = font.getGlyphCodesForName(token); 155 if (secondGlyphSet == null) { 156 secondGlyphSet = glyphCodes; 157 secondGlyphLen = glyphCodes.length; 158 } else { 159 if ((secondGlyphLen + glyphCodes.length) > 160 secondGlyphSet.length) { 161 int sz = secondGlyphSet.length*2; 162 if (sz <secondGlyphLen + glyphCodes.length) 163 sz = secondGlyphLen + glyphCodes.length; 164 int [] tmp = new int[sz]; 165 for (int i = 0; i < secondGlyphLen; i++) 166 tmp[i] = secondGlyphSet[i]; 167 secondGlyphSet = tmp; 168 } 169 for (int i = 0; i < glyphCodes.length; i++) 170 secondGlyphSet[secondGlyphLen++] = glyphCodes[i]; 171 } 172 } 173 174 int[] firstGlyphs; 176 if ((firstGlyphLen == 0) || 177 (firstGlyphLen == firstGlyphSet.length)) { 178 firstGlyphs = firstGlyphSet; 179 } else { 180 firstGlyphs = new int[firstGlyphLen]; 181 System.arraycopy(firstGlyphSet, 0, firstGlyphs, 0, firstGlyphLen); 182 } 183 int[] secondGlyphs; 184 if ((secondGlyphLen == 0) || 185 (secondGlyphLen == secondGlyphSet.length)) { 186 secondGlyphs = secondGlyphSet; 187 } else { 188 secondGlyphs = new int[secondGlyphLen]; 189 System.arraycopy(secondGlyphSet, 0, secondGlyphs, 0, 190 secondGlyphLen); 191 } 192 193 UnicodeRange[] firstRanges; 194 firstRanges = new UnicodeRange[firstUnicodeRanges.size()]; 195 firstUnicodeRanges.toArray(firstRanges); 196 197 UnicodeRange[] secondRanges; 198 secondRanges = new UnicodeRange[secondUnicodeRanges.size()]; 199 secondUnicodeRanges.toArray(secondRanges); 200 201 return new Kern(firstGlyphs, secondGlyphs, 203 firstRanges, secondRanges, kernValue); 204 } 205 } 206 | Popular Tags |