KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > codec > wmf > MetaFont


1 /*
2  * $Id: MetaFont.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 2001, 2002 Paulo Soares
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf.codec.wmf;
52 import java.io.IOException JavaDoc;
53 import java.io.UnsupportedEncodingException JavaDoc;
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 JavaDoc 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 JavaDoc faceName = "arial";
97     BaseFont font = null;
98
99     public MetaFont() {
100         type = META_FONT;
101     }
102
103     public void init(InputMeta in) throws IOException JavaDoc {
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 JavaDoc(name, 0, k, "Cp1252");
126         }
127         catch (UnsupportedEncodingException JavaDoc e) {
128             faceName = new String JavaDoc(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 JavaDoc 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 JavaDoc 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