1 50 51 package com.lowagie.text.pdf.codec.wmf; 52 import java.io.IOException ; 53 import java.io.UnsupportedEncodingException ; 54 55 import com.lowagie.text.ExceptionConverter; 56 import com.lowagie.text.Font; 57 import com.lowagie.text.FontFactory; 58 import com.lowagie.text.pdf.BaseFont; 59 60 public class MetaFont extends MetaObject { 61 static final String fontNames[] = { 62 "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique", 63 "Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique", 64 "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic", 65 "Symbol", "ZapfDingbats"}; 66 67 static final int MARKER_BOLD = 1; 68 static final int MARKER_ITALIC = 2; 69 static final int MARKER_COURIER = 0; 70 static final int MARKER_HELVETICA = 4; 71 static final int MARKER_TIMES = 8; 72 static final int MARKER_SYMBOL = 12; 73 74 static final int DEFAULT_PITCH = 0; 75 static final int FIXED_PITCH = 1; 76 static final int VARIABLE_PITCH = 2; 77 static final int FF_DONTCARE = 0; 78 static final int FF_ROMAN = 1; 79 static final int FF_SWISS = 2; 80 static final int FF_MODERN = 3; 81 static final int FF_SCRIPT = 4; 82 static final int FF_DECORATIVE = 5; 83 static final int BOLDTHRESHOLD = 600; 84 static final int nameSize = 32; 85 static final int ETO_OPAQUE = 2; 86 static final int ETO_CLIPPED = 4; 87 88 int height; 89 float angle; 90 int bold; 91 int italic; 92 boolean underline; 93 boolean strikeout; 94 int charset; 95 int pitchAndFamily; 96 String faceName = "arial"; 97 BaseFont font = null; 98 99 public MetaFont() { 100 type = META_FONT; 101 } 102 103 public void init(InputMeta in) throws IOException { 104 height = Math.abs(in.readShort()); 105 in.skip(2); 106 angle = (float)(in.readShort() / 1800.0 * Math.PI); 107 in.skip(2); 108 bold = (in.readShort() >= BOLDTHRESHOLD ? MARKER_BOLD : 0); 109 italic = (in.readByte() != 0 ? MARKER_ITALIC : 0); 110 underline = (in.readByte() != 0); 111 strikeout = (in.readByte() != 0); 112 charset = in.readByte(); 113 in.skip(3); 114 pitchAndFamily = in.readByte(); 115 byte name[] = new byte[nameSize]; 116 int k; 117 for (k = 0; k < nameSize; ++k) { 118 int c = in.readByte(); 119 if (c == 0) { 120 break; 121 } 122 name[k] = (byte)c; 123 } 124 try { 125 faceName = new String (name, 0, k, "Cp1252"); 126 } 127 catch (UnsupportedEncodingException e) { 128 faceName = new String (name, 0, k); 129 } 130 faceName = faceName.toLowerCase(); 131 } 132 133 public BaseFont getFont() { 134 if (font != null) 135 return font; 136 Font ff2 = FontFactory.getFont(faceName, BaseFont.CP1252, true, 10, ((italic != 0) ? Font.ITALIC : 0) | ((bold != 0) ? Font.BOLD : 0)); 137 font = ff2.getBaseFont(); 138 if (font != null) 139 return font; 140 String fontName; 141 if (faceName.indexOf("courier") != -1 || faceName.indexOf("terminal") != -1 142 || faceName.indexOf("fixedsys") != -1) { 143 fontName = fontNames[MARKER_COURIER + italic + bold]; 144 } 145 else if (faceName.indexOf("ms sans serif") != -1 || faceName.indexOf("arial") != -1 146 || faceName.indexOf("system") != -1) { 147 fontName = fontNames[MARKER_HELVETICA + italic + bold]; 148 } 149 else if (faceName.indexOf("arial black") != -1) { 150 fontName = fontNames[MARKER_HELVETICA + italic + MARKER_BOLD]; 151 } 152 else if (faceName.indexOf("times") != -1 || faceName.indexOf("ms serif") != -1 153 || faceName.indexOf("roman") != -1) { 154 fontName = fontNames[MARKER_TIMES + italic + bold]; 155 } 156 else if (faceName.indexOf("symbol") != -1) { 157 fontName = fontNames[MARKER_SYMBOL]; 158 } 159 else { 160 int pitch = pitchAndFamily & 3; 161 int family = (pitchAndFamily >> 4) & 7; 162 switch (family) { 163 case FF_MODERN: 164 fontName = fontNames[MARKER_COURIER + italic + bold]; 165 break; 166 case FF_ROMAN: 167 fontName = fontNames[MARKER_TIMES + italic + bold]; 168 break; 169 case FF_SWISS: 170 case FF_SCRIPT: 171 case FF_DECORATIVE: 172 fontName = fontNames[MARKER_HELVETICA + italic + bold]; 173 break; 174 default: 175 { 176 switch (pitch) { 177 case FIXED_PITCH: 178 fontName = fontNames[MARKER_COURIER + italic + bold]; 179 break; 180 default: 181 fontName = fontNames[MARKER_HELVETICA + italic + bold]; 182 break; 183 } 184 } 185 } 186 } 187 try { 188 font = BaseFont.createFont(fontName, "Cp1252", false); 189 } 190 catch (Exception e) { 191 throw new ExceptionConverter(e); 192 } 193 194 return font; 195 } 196 197 public float getAngle() { 198 return angle; 199 } 200 201 public boolean isUnderline() { 202 return underline; 203 } 204 205 public boolean isStrikeout() { 206 return strikeout; 207 } 208 209 public float getFontSize(MetaState state) { 210 return Math.abs(state.transformY(height) - state.transformY(0)) * 0.86f; 211 } 212 } 213 | Popular Tags |