001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. 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: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.font;
031:
032: import org.pdfbox.cos.COSArray;
033: import org.pdfbox.cos.COSBase;
034: import org.pdfbox.cos.COSDictionary;
035: import org.pdfbox.cos.COSName;
036: import org.pdfbox.cos.COSString;
037: import org.pdfbox.cos.COSStream;
038:
039: import org.pdfbox.pdmodel.common.COSObjectable;
040: import org.pdfbox.pdmodel.common.PDRectangle;
041: import org.pdfbox.pdmodel.common.PDStream;
042:
043: /**
044: * This class represents an implementation to the font descriptor that gets its
045: * information from a COS Dictionary.
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.4 $
049: */
050: public class PDFontDescriptorDictionary extends PDFontDescriptor
051: implements COSObjectable {
052: private COSDictionary dic;
053:
054: /**
055: * Constructor.
056: */
057: public PDFontDescriptorDictionary() {
058: dic = new COSDictionary();
059: dic.setName("Type", "FontDescriptor");
060: }
061:
062: /**
063: * Constructor.
064: *
065: * @param desc The wrapped COS Dictionary.
066: */
067: public PDFontDescriptorDictionary(COSDictionary desc) {
068: dic = desc;
069: }
070:
071: /**
072: * This will get the dictionary for this object.
073: *
074: * @return The COS dictionary.
075: */
076: public COSDictionary getCOSDictionary() {
077: return dic;
078: }
079:
080: /**
081: * Convert this standard java object to a COS object.
082: *
083: * @return The cos object that matches this Java object.
084: */
085: public COSBase getCOSObject() {
086: return dic;
087: }
088:
089: /**
090: * Get the font name.
091: *
092: * @return The name of the font.
093: */
094: public String getFontName() {
095: String retval = null;
096: COSName name = (COSName) dic.getDictionaryObject(COSName
097: .getPDFName("FontName"));
098: if (name != null) {
099: retval = name.getName();
100: }
101: return retval;
102: }
103:
104: /**
105: * This will set the font name.
106: *
107: * @param fontName The new name for the font.
108: */
109: public void setFontName(String fontName) {
110: COSName name = null;
111: if (fontName != null) {
112: name = COSName.getPDFName(fontName);
113: }
114: dic.setItem(COSName.getPDFName("FontName"), name);
115: }
116:
117: /**
118: * A string representing the preferred font family.
119: *
120: * @return The font family.
121: */
122: public String getFontFamily() {
123: String retval = null;
124: COSString name = (COSString) dic.getDictionaryObject(COSName
125: .getPDFName("FontFamily"));
126: if (name != null) {
127: retval = name.getString();
128: }
129: return retval;
130: }
131:
132: /**
133: * This will set the font family.
134: *
135: * @param fontFamily The font family.
136: */
137: public void setFontFamily(String fontFamily) {
138: COSString name = null;
139: if (fontFamily != null) {
140: name = new COSString(fontFamily);
141: }
142: dic.setItem(COSName.getPDFName("FontFamily"), name);
143: }
144:
145: /**
146: * The weight of the font. According to the PDF spec "possible values are
147: * 100, 200, 300, 400, 500, 600, 700, 800 or 900" Where a higher number is
148: * more weight and appears to be more bold.
149: *
150: * @return The font weight.
151: */
152: public float getFontWeight() {
153: return dic.getFloat("FontWeight", 0);
154: }
155:
156: /**
157: * Set the weight of the font.
158: *
159: * @param fontWeight The new weight of the font.
160: */
161: public void setFontWeight(float fontWeight) {
162: dic.setFloat("FontWeight", fontWeight);
163: }
164:
165: /**
166: * A string representing the preferred font stretch.
167: * According to the PDF Spec:
168: * The font stretch value; it must be one of the following (ordered from
169: * narrowest to widest): UltraCondensed, ExtraCondensed, Condensed, SemiCondensed,
170: * Normal, SemiExpanded, Expanded, ExtraExpanded or UltraExpanded.
171: *
172: * @return The stretch of the font.
173: */
174: public String getFontStretch() {
175: String retval = null;
176: COSName name = (COSName) dic.getDictionaryObject(COSName
177: .getPDFName("FontStretch"));
178: if (name != null) {
179: retval = name.getName();
180: }
181: return retval;
182: }
183:
184: /**
185: * This will set the font stretch.
186: *
187: * @param fontStretch The new stretch for the font.
188: */
189: public void setFontStretch(String fontStretch) {
190: COSName name = null;
191: if (fontStretch != null) {
192: name = COSName.getPDFName(fontStretch);
193: }
194: dic.setItem(COSName.getPDFName("FontStretch"), name);
195: }
196:
197: /**
198: * This will get the font flags.
199: *
200: * @return The font flags.
201: */
202: public int getFlags() {
203: return dic.getInt("Flags", 0);
204: }
205:
206: /**
207: * This will set the font flags.
208: *
209: * @param flags The new font flags.
210: */
211: public void setFlags(int flags) {
212: dic.setInt("Flags", flags);
213: }
214:
215: /**
216: * This will get the fonts bouding box.
217: *
218: * @return The fonts bouding box.
219: */
220: public PDRectangle getFontBoundingBox() {
221: COSArray rect = (COSArray) dic.getDictionaryObject(COSName
222: .getPDFName("FontBBox"));
223: PDRectangle retval = null;
224: if (rect != null) {
225: retval = new PDRectangle(rect);
226: }
227: return retval;
228: }
229:
230: /**
231: * Set the fonts bounding box.
232: *
233: * @param rect The new bouding box.
234: */
235: public void setFontBoundingBox(PDRectangle rect) {
236: COSArray array = null;
237: if (rect != null) {
238: array = rect.getCOSArray();
239: }
240: dic.setItem(COSName.getPDFName("FontBBox"), array);
241: }
242:
243: /**
244: * This will get the italic angle for the font.
245: *
246: * @return The italic angle.
247: */
248: public float getItalicAngle() {
249: return dic.getFloat("ItalicAngle", 0);
250: }
251:
252: /**
253: * This will set the italic angle for the font.
254: *
255: * @param angle The new italic angle for the font.
256: */
257: public void setItalicAngle(float angle) {
258: dic.setFloat("ItalicAngle", angle);
259: }
260:
261: /**
262: * This will get the ascent for the font.
263: *
264: * @return The ascent.
265: */
266: public float getAscent() {
267: return dic.getFloat("Ascent", 0);
268: }
269:
270: /**
271: * This will set the ascent for the font.
272: *
273: * @param ascent The new ascent for the font.
274: */
275: public void setAscent(float ascent) {
276: dic.setFloat("Ascent", ascent);
277: }
278:
279: /**
280: * This will get the descent for the font.
281: *
282: * @return The descent.
283: */
284: public float getDescent() {
285: return dic.getFloat("Descent", 0);
286: }
287:
288: /**
289: * This will set the descent for the font.
290: *
291: * @param descent The new descent for the font.
292: */
293: public void setDescent(float descent) {
294: dic.setFloat("Descent", descent);
295: }
296:
297: /**
298: * This will get the leading for the font.
299: *
300: * @return The leading.
301: */
302: public float getLeading() {
303: return dic.getFloat("Leading", 0);
304: }
305:
306: /**
307: * This will set the leading for the font.
308: *
309: * @param leading The new leading for the font.
310: */
311: public void setLeading(float leading) {
312: dic.setFloat("Leading", leading);
313: }
314:
315: /**
316: * This will get the CapHeight for the font.
317: *
318: * @return The cap height.
319: */
320: public float getCapHeight() {
321: return dic.getFloat("CapHeight", 0);
322: }
323:
324: /**
325: * This will set the cap height for the font.
326: *
327: * @param capHeight The new cap height for the font.
328: */
329: public void setCapHeight(float capHeight) {
330: dic.setFloat("CapHeight", capHeight);
331: }
332:
333: /**
334: * This will get the x height for the font.
335: *
336: * @return The x height.
337: */
338: public float getXHeight() {
339: return dic.getFloat("XHeight", 0);
340: }
341:
342: /**
343: * This will set the x height for the font.
344: *
345: * @param xHeight The new x height for the font.
346: */
347: public void setXHeight(float xHeight) {
348: dic.setFloat("XHeight", xHeight);
349: }
350:
351: /**
352: * This will get the stemV for the font.
353: *
354: * @return The stem v value.
355: */
356: public float getStemV() {
357: return dic.getFloat("StemV", 0);
358: }
359:
360: /**
361: * This will set the stem V for the font.
362: *
363: * @param stemV The new stem v for the font.
364: */
365: public void setStemV(float stemV) {
366: dic.setFloat("StemV", stemV);
367: }
368:
369: /**
370: * This will get the stemH for the font.
371: *
372: * @return The stem h value.
373: */
374: public float getStemH() {
375: return dic.getFloat("StemH", 0);
376: }
377:
378: /**
379: * This will set the stem H for the font.
380: *
381: * @param stemH The new stem h for the font.
382: */
383: public void setStemH(float stemH) {
384: dic.setFloat("StemH", stemH);
385: }
386:
387: /**
388: * This will get the average width for the font.
389: *
390: * @return The average width value.
391: */
392: public float getAverageWidth() {
393: return dic.getFloat("AvgWidth", 0);
394: }
395:
396: /**
397: * This will set the average width for the font.
398: *
399: * @param averageWidth The new average width for the font.
400: */
401: public void setAverageWidth(float averageWidth) {
402: dic.setFloat("AvgWidth", averageWidth);
403: }
404:
405: /**
406: * This will get the max width for the font.
407: *
408: * @return The max width value.
409: */
410: public float getMaxWidth() {
411: return dic.getFloat("MaxWidth", 0);
412: }
413:
414: /**
415: * This will set the max width for the font.
416: *
417: * @param maxWidth The new max width for the font.
418: */
419: public void setMaxWidth(float maxWidth) {
420: dic.setFloat("MaxWidth", maxWidth);
421: }
422:
423: /**
424: * This will get the missing width for the font.
425: *
426: * @return The missing width value.
427: */
428: public float getMissingWidth() {
429: return dic.getFloat("MissingWidth", 0);
430: }
431:
432: /**
433: * This will set the missing width for the font.
434: *
435: * @param missingWidth The new missing width for the font.
436: */
437: public void setMissingWidth(float missingWidth) {
438: dic.setFloat("MissingWidth", missingWidth);
439: }
440:
441: /**
442: * This will get the character set for the font.
443: *
444: * @return The character set value.
445: */
446: public String getCharSet() {
447: String retval = null;
448: COSString name = (COSString) dic.getDictionaryObject(COSName
449: .getPDFName("CharSet"));
450: if (name != null) {
451: retval = name.getString();
452: }
453: return retval;
454: }
455:
456: /**
457: * This will set the character set for the font.
458: *
459: * @param charSet The new character set for the font.
460: */
461: public void setCharacterSet(String charSet) {
462: COSString name = null;
463: if (charSet != null) {
464: name = new COSString(charSet);
465: }
466: dic.setItem(COSName.getPDFName("CharSet"), name);
467: }
468:
469: /**
470: * A stream containing a Type 1 font program.
471: *
472: * @return A stream containing a Type 1 font program.
473: */
474: public PDStream getFontFile() {
475: PDStream retval = null;
476: COSStream stream = (COSStream) dic
477: .getDictionaryObject("FontFile");
478: if (stream != null) {
479: retval = new PDStream(stream);
480: }
481: return retval;
482: }
483:
484: /**
485: * Set the type 1 font program.
486: *
487: * @param type1Stream The type 1 stream.
488: */
489: public void setFontFile(PDStream type1Stream) {
490: dic.setItem("FontFile", type1Stream);
491: }
492:
493: /**
494: * A stream containing a true type font program.
495: *
496: * @return A stream containing a true type font program.
497: */
498: public PDStream getFontFile2() {
499: PDStream retval = null;
500: COSStream stream = (COSStream) dic
501: .getDictionaryObject("FontFile2");
502: if (stream != null) {
503: retval = new PDStream(stream);
504: }
505: return retval;
506: }
507:
508: /**
509: * Set the true type font program.
510: *
511: * @param ttfStream The true type stream.
512: */
513: public void setFontFile2(PDStream ttfStream) {
514: dic.setItem("FontFile2", ttfStream);
515: }
516:
517: /**
518: * A stream containing a font program that is not true type or type 1.
519: *
520: * @return A stream containing a font program.
521: */
522: public PDStream getFontFile3() {
523: PDStream retval = null;
524: COSStream stream = (COSStream) dic
525: .getDictionaryObject("FontFile3");
526: if (stream != null) {
527: retval = new PDStream(stream);
528: }
529: return retval;
530: }
531:
532: /**
533: * Set a stream containing a font program that is not true type or type 1.
534: *
535: * @param stream The font program stream.
536: */
537: public void setFontFile3(PDStream stream) {
538: dic.setItem("FontFile3", stream);
539: }
540: }
|