001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.font;
031:
032: import java.util.StringTokenizer;
033: import de.intarsys.pdf.cds.CDSRectangle;
034: import de.intarsys.pdf.cos.COSBasedObject;
035: import de.intarsys.pdf.cos.COSObject;
036:
037: /**
038: * the PDFontDescriptor that is used when no explicit font descriptor is
039: * available in the pdf file. this will happen only when a builtin font is used
040: */
041: public class PDFontDescriptorType1 extends PDFontDescriptor {
042: /**
043: * The meta class implementation
044: */
045: static public class MetaClass extends PDFontDescriptor.MetaClass {
046: protected MetaClass(Class instanceClass) {
047: super (instanceClass);
048: }
049:
050: protected COSBasedObject doCreateCOSBasedObject(COSObject object) {
051: throw new IllegalStateException(
052: "can not instantiate PDFontDescriptorType1 as COSBasedObject");
053: }
054: }
055:
056: /** The meta class instance */
057: static public final MetaClass META = new MetaClass(MetaClass.class
058: .getDeclaringClass());
059:
060: /** The font that is described */
061: private PDFontType1 font;
062:
063: /** derived value for the leading */
064: private int leading;
065:
066: /** cached value for the bounding box */
067: private CDSRectangle bb;
068:
069: /**
070: * Create a font descriptor for a type 1 font.
071: *
072: * @param newFont
073: * type 1 font containing the definitions.
074: */
075: protected PDFontDescriptorType1(PDFontType1 newFont) {
076: super (null);
077: font = newFont;
078: }
079:
080: /*
081: * (non-Javadoc)
082: *
083: * @see de.intarsys.pdf.font.PDFontDescriptor#getAscent()
084: */
085: public float getAscent() {
086: String value = getFont().getAfm().getAttribute("Ascender");
087: if (value == null) {
088: return 0;
089: }
090: return Float.parseFloat(value);
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see de.intarsys.pdf.font.PDFontDescriptor#getAvgWidth()
097: */
098: public float getAvgWidth() {
099: return 0;
100: }
101:
102: /*
103: * (non-Javadoc)
104: *
105: * @see de.intarsys.pdf.font.PDFontDescriptor#getCapHeight()
106: */
107: public float getCapHeight() {
108: String value = getFont().getAfm().getAttribute("CapHeight");
109: if (value == null) {
110: return 0;
111: }
112: return Float.parseFloat(value);
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see de.intarsys.pdf.font.PDFontDescriptor#getDescent()
119: */
120: public float getDescent() {
121: String value = getFont().getAfm().getAttribute("Descender");
122: if (value == null) {
123: return 0;
124: }
125: return Float.parseFloat(value);
126: }
127:
128: /*
129: * (non-Javadoc)
130: *
131: * @see de.intarsys.pdf.font.PDFontDescriptor#getFlags()
132: */
133: public int getFlagsValue() {
134: // todo 1 font support correct flags
135: // AFM afm = getFont().getAfm();
136: return 0;
137: }
138:
139: /*
140: * (non-Javadoc)
141: *
142: * @see de.intarsys.pdf.font.PDFontDescriptor#setFlagsValue(int)
143: */
144: public void setFlagsValue(int value) {
145: // TODO 2
146: }
147:
148: public PDFontType1 getFont() {
149: return font;
150: }
151:
152: /*
153: * (non-Javadoc)
154: *
155: * @see de.intarsys.pdf.font.PDFontDescriptor#getRectangle()
156: */
157: public CDSRectangle getFontBB() {
158: if (bb == null) {
159: float llx = 0;
160: float lly = 0;
161: float urx = 0;
162: float ury = 0;
163: String value = getFont().getAfm().getAttribute("FontBBox");
164: if (value != null) {
165: StringTokenizer st = new StringTokenizer(value);
166: if (st.hasMoreTokens()) {
167: llx = Float.parseFloat(st.nextToken());
168: }
169: if (st.hasMoreTokens()) {
170: lly = Float.parseFloat(st.nextToken());
171: }
172: if (st.hasMoreTokens()) {
173: urx = Float.parseFloat(st.nextToken());
174: }
175: if (st.hasMoreTokens()) {
176: ury = Float.parseFloat(st.nextToken());
177: }
178: }
179: bb = new CDSRectangle(llx, lly, urx, ury);
180: }
181: return bb;
182: }
183:
184: /*
185: * (non-Javadoc)
186: *
187: * @see de.intarsys.pdf.font.PDFontDescriptor#getFontFamily()
188: */
189: public String getFontFamily() {
190: return null;
191: }
192:
193: /*
194: * (non-Javadoc)
195: *
196: * @see de.intarsys.pdf.font.PDFontDescriptor#getFontName()
197: */
198: public String getFontName() {
199: String value = getFont().getAfm().getAttribute("FontName");
200: if (value == null) {
201: return "";
202: }
203: return value.trim();
204: }
205:
206: /*
207: * (non-Javadoc)
208: *
209: * @see de.intarsys.pdf.font.PDFontDescriptor#getItalicAngle()
210: */
211: public float getItalicAngle() {
212: String value = getFont().getAfm().getAttribute("ItalicAngle");
213: if (value == null) {
214: return 0;
215: }
216: return Float.parseFloat(value);
217: }
218:
219: /*
220: * (non-Javadoc)
221: *
222: * @see de.intarsys.pdf.font.PDFontDescriptor#getLeading()
223: */
224: public int getLeading() {
225: if (leading == 0) {
226: // compute a default for the leading
227: CDSRectangle rect = getFontBB();
228: leading = (int) (rect.getUpperRightY() - rect
229: .getLowerLeftY());
230: }
231: return leading;
232: }
233:
234: /*
235: * (non-Javadoc)
236: *
237: * @see de.intarsys.pdf.font.PDFontDescriptor#getMaxWidth()
238: */
239: public int getMaxWidth() {
240: return 0;
241: }
242:
243: /*
244: * (non-Javadoc)
245: *
246: * @see de.intarsys.pdf.font.PDFontDescriptor#getMissingWidth()
247: */
248: public int getMissingWidth() {
249: return 0;
250: }
251:
252: /*
253: * (non-Javadoc)
254: *
255: * @see de.intarsys.pdf.font.PDFontDescriptor#getStemH()
256: */
257: public int getStemH() {
258: String value = getFont().getAfm().getAttribute("StdHW");
259: if (value == null) {
260: return 0;
261: }
262: return (int) Float.parseFloat(value);
263: }
264:
265: /*
266: * (non-Javadoc)
267: *
268: * @see de.intarsys.pdf.font.PDFontDescriptor#getStemV()
269: */
270: public int getStemV() {
271: String value = getFont().getAfm().getAttribute("StdVW");
272: if (value == null) {
273: return 0;
274: }
275: return (int) Float.parseFloat(value);
276: }
277:
278: /*
279: * (non-Javadoc)
280: *
281: * @see de.intarsys.pdf.font.PDFontDescriptor#getXHeight()
282: */
283: public float getXHeight() {
284: String value = getFont().getAfm().getAttribute("XHeight");
285: if (value == null) {
286: return 0;
287: }
288: return Float.parseFloat(value);
289: }
290:
291: /*
292: * (non-Javadoc)
293: *
294: * @see de.intarsys.pdf.font.PDFontDescriptor#isSymbolic()
295: */
296: public boolean isSymbolic() {
297: String name = getFont().getBaseFont().stringValue();
298: return PDFontType1.FONT_ZapfDingbats.equals(name)
299: || PDFontType1.FONT_Symbol.equals(name);
300: }
301:
302: public boolean isNonsymbolic() {
303: return !isSymbolic();
304: }
305: }
|