001: /*
002: * $Id: MetaFont.java 2366 2006-09-14 23:10:58Z xlv $
003: * $Name$
004: *
005: * Copyright 2001, 2002 Paulo Soares
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf.codec.wmf;
052:
053: import java.io.IOException;
054: import java.io.UnsupportedEncodingException;
055:
056: import com.lowagie.text.ExceptionConverter;
057: import com.lowagie.text.Font;
058: import com.lowagie.text.FontFactory;
059: import com.lowagie.text.pdf.BaseFont;
060:
061: public class MetaFont extends MetaObject {
062: static final String fontNames[] = { "Courier", "Courier-Bold",
063: "Courier-Oblique", "Courier-BoldOblique", "Helvetica",
064: "Helvetica-Bold", "Helvetica-Oblique",
065: "Helvetica-BoldOblique", "Times-Roman", "Times-Bold",
066: "Times-Italic", "Times-BoldItalic", "Symbol",
067: "ZapfDingbats" };
068:
069: static final int MARKER_BOLD = 1;
070: static final int MARKER_ITALIC = 2;
071: static final int MARKER_COURIER = 0;
072: static final int MARKER_HELVETICA = 4;
073: static final int MARKER_TIMES = 8;
074: static final int MARKER_SYMBOL = 12;
075:
076: static final int DEFAULT_PITCH = 0;
077: static final int FIXED_PITCH = 1;
078: static final int VARIABLE_PITCH = 2;
079: static final int FF_DONTCARE = 0;
080: static final int FF_ROMAN = 1;
081: static final int FF_SWISS = 2;
082: static final int FF_MODERN = 3;
083: static final int FF_SCRIPT = 4;
084: static final int FF_DECORATIVE = 5;
085: static final int BOLDTHRESHOLD = 600;
086: static final int nameSize = 32;
087: static final int ETO_OPAQUE = 2;
088: static final int ETO_CLIPPED = 4;
089:
090: int height;
091: float angle;
092: int bold;
093: int italic;
094: boolean underline;
095: boolean strikeout;
096: int charset;
097: int pitchAndFamily;
098: String faceName = "arial";
099: BaseFont font = null;
100:
101: public MetaFont() {
102: type = META_FONT;
103: }
104:
105: public void init(InputMeta in) throws IOException {
106: height = Math.abs(in.readShort());
107: in.skip(2);
108: angle = (float) (in.readShort() / 1800.0 * Math.PI);
109: in.skip(2);
110: bold = (in.readShort() >= BOLDTHRESHOLD ? MARKER_BOLD : 0);
111: italic = (in.readByte() != 0 ? MARKER_ITALIC : 0);
112: underline = (in.readByte() != 0);
113: strikeout = (in.readByte() != 0);
114: charset = in.readByte();
115: in.skip(3);
116: pitchAndFamily = in.readByte();
117: byte name[] = new byte[nameSize];
118: int k;
119: for (k = 0; k < nameSize; ++k) {
120: int c = in.readByte();
121: if (c == 0) {
122: break;
123: }
124: name[k] = (byte) c;
125: }
126: try {
127: faceName = new String(name, 0, k, "Cp1252");
128: } catch (UnsupportedEncodingException e) {
129: faceName = new String(name, 0, k);
130: }
131: faceName = faceName.toLowerCase();
132: }
133:
134: public BaseFont getFont() {
135: if (font != null)
136: return font;
137: Font ff2 = FontFactory.getFont(faceName, BaseFont.CP1252, true,
138: 10, ((italic != 0) ? Font.ITALIC : 0)
139: | ((bold != 0) ? Font.BOLD : 0));
140: font = ff2.getBaseFont();
141: if (font != null)
142: return font;
143: String fontName;
144: if (faceName.indexOf("courier") != -1
145: || faceName.indexOf("terminal") != -1
146: || faceName.indexOf("fixedsys") != -1) {
147: fontName = fontNames[MARKER_COURIER + italic + bold];
148: } else if (faceName.indexOf("ms sans serif") != -1
149: || faceName.indexOf("arial") != -1
150: || faceName.indexOf("system") != -1) {
151: fontName = fontNames[MARKER_HELVETICA + italic + bold];
152: } else if (faceName.indexOf("arial black") != -1) {
153: fontName = fontNames[MARKER_HELVETICA + italic
154: + MARKER_BOLD];
155: } else if (faceName.indexOf("times") != -1
156: || faceName.indexOf("ms serif") != -1
157: || faceName.indexOf("roman") != -1) {
158: fontName = fontNames[MARKER_TIMES + italic + bold];
159: } else if (faceName.indexOf("symbol") != -1) {
160: fontName = fontNames[MARKER_SYMBOL];
161: } else {
162: int pitch = pitchAndFamily & 3;
163: int family = (pitchAndFamily >> 4) & 7;
164: switch (family) {
165: case FF_MODERN:
166: fontName = fontNames[MARKER_COURIER + italic + bold];
167: break;
168: case FF_ROMAN:
169: fontName = fontNames[MARKER_TIMES + italic + bold];
170: break;
171: case FF_SWISS:
172: case FF_SCRIPT:
173: case FF_DECORATIVE:
174: fontName = fontNames[MARKER_HELVETICA + italic + bold];
175: break;
176: default: {
177: switch (pitch) {
178: case FIXED_PITCH:
179: fontName = fontNames[MARKER_COURIER + italic + bold];
180: break;
181: default:
182: fontName = fontNames[MARKER_HELVETICA + italic
183: + bold];
184: break;
185: }
186: }
187: }
188: }
189: try {
190: font = BaseFont.createFont(fontName, "Cp1252", false);
191: } catch (Exception e) {
192: throw new ExceptionConverter(e);
193: }
194:
195: return font;
196: }
197:
198: public float getAngle() {
199: return angle;
200: }
201:
202: public boolean isUnderline() {
203: return underline;
204: }
205:
206: public boolean isStrikeout() {
207: return strikeout;
208: }
209:
210: public float getFontSize(MetaState state) {
211: return Math.abs(state.transformY(height) - state.transformY(0)) * 0.86f;
212: }
213: }
|