1 50 51 package com.lowagie.text.pdf; 52 53 import java.io.UnsupportedEncodingException ; 54 import java.util.HashMap ; 55 56 import com.lowagie.text.ExceptionConverter; 57 62 class FontDetails { 63 64 66 PdfIndirectReference indirectReference; 67 69 PdfName fontName; 70 72 BaseFont baseFont; 73 75 TrueTypeFontUnicode ttu; 76 77 CJKFont cjkFont; 78 80 byte shortTag[]; 81 84 HashMap longTag; 85 86 IntHashtable cjkTag; 87 89 int fontType; 90 92 boolean symbolic; 93 96 protected boolean subset = true; 97 104 FontDetails(PdfName fontName, PdfIndirectReference indirectReference, BaseFont baseFont) { 105 this.fontName = fontName; 106 this.indirectReference = indirectReference; 107 this.baseFont = baseFont; 108 fontType = baseFont.getFontType(); 109 switch (fontType) { 110 case BaseFont.FONT_TYPE_T1: 111 case BaseFont.FONT_TYPE_TT: 112 shortTag = new byte[256]; 113 break; 114 case BaseFont.FONT_TYPE_CJK: 115 cjkTag = new IntHashtable(); 116 cjkFont = (CJKFont)baseFont; 117 break; 118 case BaseFont.FONT_TYPE_TTUNI: 119 longTag = new HashMap (); 120 ttu = (TrueTypeFontUnicode)baseFont; 121 symbolic = baseFont.isFontSpecific(); 122 break; 123 } 124 } 125 126 129 PdfIndirectReference getIndirectReference() { 130 return indirectReference; 131 } 132 133 136 PdfName getFontName() { 137 return fontName; 138 } 139 140 143 BaseFont getBaseFont() { 144 return baseFont; 145 } 146 147 153 byte[] convertToBytes(String text) { 154 byte b[] = null; 155 switch (fontType) { 156 case BaseFont.FONT_TYPE_T3: 157 return baseFont.convertToBytes(text); 158 case BaseFont.FONT_TYPE_T1: 159 case BaseFont.FONT_TYPE_TT: { 160 b = baseFont.convertToBytes(text); 161 int len = b.length; 162 for (int k = 0; k < len; ++k) 163 shortTag[((int)b[k]) & 0xff] = 1; 164 break; 165 } 166 case BaseFont.FONT_TYPE_CJK: { 167 int len = text.length(); 168 for (int k = 0; k < len; ++k) 169 cjkTag.put(cjkFont.getCidCode(text.charAt(k)), 0); 170 b = baseFont.convertToBytes(text); 171 break; 172 } 173 case BaseFont.FONT_TYPE_DOCUMENT: { 174 b = baseFont.convertToBytes(text); 175 break; 176 } 177 case BaseFont.FONT_TYPE_TTUNI: { 178 try { 179 int len = text.length(); 180 int metrics[] = null; 181 char glyph[] = new char[len]; 182 int i = 0; 183 if (symbolic) { 184 b = PdfEncodings.convertToBytes(text, "symboltt"); 185 len = b.length; 186 for (int k = 0; k < len; ++k) { 187 metrics = ttu.getMetricsTT(b[k] & 0xff); 188 if (metrics == null) 189 continue; 190 longTag.put(new Integer (metrics[0]), new int[]{metrics[0], metrics[1], ttu.getUnicodeDifferences(b[k] & 0xff)}); 191 glyph[i++] = (char)metrics[0]; 192 } 193 } 194 else { 195 for (int k = 0; k < len; ++k) { 196 char c = text.charAt(k); 197 metrics = ttu.getMetricsTT(c); 198 if (metrics == null) 199 continue; 200 int m0 = metrics[0]; 201 Integer gl = new Integer (m0); 202 if (!longTag.containsKey(gl)) 203 longTag.put(gl, new int[]{m0, metrics[1], c}); 204 glyph[i++] = (char)m0; 205 } 206 } 207 String s = new String (glyph, 0, i); 208 b = s.getBytes(CJKFont.CJK_ENCODING); 209 } 210 catch (UnsupportedEncodingException e) { 211 throw new ExceptionConverter(e); 212 } 213 break; 214 } 215 } 216 return b; 217 } 218 219 222 void writeFont(PdfWriter writer) { 223 try { 224 switch (fontType) { 225 case BaseFont.FONT_TYPE_T3: 226 baseFont.writeFont(writer, indirectReference, null); 227 break; 228 case BaseFont.FONT_TYPE_T1: 229 case BaseFont.FONT_TYPE_TT: { 230 int firstChar; 231 int lastChar; 232 for (firstChar = 0; firstChar < 256; ++firstChar) { 233 if (shortTag[firstChar] != 0) 234 break; 235 } 236 for (lastChar = 255; lastChar >= firstChar; --lastChar) { 237 if (shortTag[lastChar] != 0) 238 break; 239 } 240 if (firstChar > 255) { 241 firstChar = 255; 242 lastChar = 255; 243 } 244 baseFont.writeFont(writer, indirectReference, new Object []{new Integer (firstChar), new Integer (lastChar), shortTag, new Boolean (subset)}); 245 break; 246 } 247 case BaseFont.FONT_TYPE_CJK: 248 baseFont.writeFont(writer, indirectReference, new Object []{cjkTag}); 249 break; 250 case BaseFont.FONT_TYPE_TTUNI: 251 baseFont.writeFont(writer, indirectReference, new Object []{longTag, new Boolean (subset)}); 252 break; 253 } 254 } 255 catch(Exception e) { 256 throw new ExceptionConverter(e); 257 } 258 } 259 260 264 public boolean isSubset() { 265 return subset; 266 } 267 268 273 public void setSubset(boolean subset) { 274 this.subset = subset; 275 } 276 } 277 | Popular Tags |