001: /*
002: * $Id: RtfFont.java 2784 2007-05-24 15:43:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
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.rtf.style;
052:
053: import java.awt.Color;
054: import java.io.ByteArrayOutputStream;
055: import java.io.IOException;
056: import java.io.OutputStream;
057:
058: import com.lowagie.text.Font;
059: import com.lowagie.text.rtf.RtfExtendedElement;
060: import com.lowagie.text.rtf.document.RtfDocument;
061:
062: /**
063: * The RtfFont class stores one font for an rtf document. It extends Font,
064: * so can be set as a font, to allow adding of fonts with arbitrary names.
065: * BaseFont fontname handling contributed by Craig Fleming. Various fixes
066: * Renaud Michel, Werner Daehn.
067: *
068: * Version: $Id: RtfFont.java 2784 2007-05-24 15:43:40Z hallm $
069: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
070: * @author Craig Fleming (rythos@rhana.dhs.org)
071: * @author Renaud Michel (r.michel@immedia.be)
072: * @author Werner Daehn (Werner.Daehn@BusinessObjects.com)
073: * @author Lidong Liu (tmslld@gmail.com)
074: * @author Thomas Bickel (tmb99@inode.at)
075: */
076: public class RtfFont extends Font implements RtfExtendedElement {
077: /**
078: * Constant for the font family to use ("froman")
079: */
080: private static final byte[] FONT_FAMILY = "\\froman".getBytes();
081: /**
082: * Constant for the charset
083: */
084: private static final byte[] FONT_CHARSET = "\\fcharset".getBytes();
085: /**
086: * Constant for the font size
087: */
088: public static final byte[] FONT_SIZE = "\\fs".getBytes();
089: /**
090: * Constant for the bold flag
091: */
092: private static final byte[] FONT_BOLD = "\\b".getBytes();
093: /**
094: * Constant for the italic flag
095: */
096: private static final byte[] FONT_ITALIC = "\\i".getBytes();
097: /**
098: * Constant for the underline flag
099: */
100: private static final byte[] FONT_UNDERLINE = "\\ul".getBytes();
101: /**
102: * Constant for the strikethrough flag
103: */
104: private static final byte[] FONT_STRIKETHROUGH = "\\strike"
105: .getBytes();
106: /**
107: * Constant for the double strikethrough flag
108: */
109: private static final byte[] FONT_DOUBLE_STRIKETHROUGH = "\\striked"
110: .getBytes();
111: /**
112: * Constant for the shadow flag
113: */
114: private static final byte[] FONT_SHADOW = "\\shad".getBytes();
115: /**
116: * Constant for the outline flag
117: */
118: private static final byte[] FONT_OUTLINE = "\\outl".getBytes();
119: /**
120: * Constant for the embossed flag
121: */
122: private static final byte[] FONT_EMBOSSED = "\\embo".getBytes();
123: /**
124: * Constant for the engraved flag
125: */
126: private static final byte[] FONT_ENGRAVED = "\\impr".getBytes();
127: /**
128: * Constant for hidden text flag
129: */
130: private static final byte[] FONT_HIDDEN = "\\v".getBytes();
131:
132: /**
133: * Constant for a plain font
134: */
135: public static final int STYLE_NONE = 0;
136: /**
137: * Constant for a bold font
138: */
139: public static final int STYLE_BOLD = 1;
140: /**
141: * Constant for an italic font
142: */
143: public static final int STYLE_ITALIC = 2;
144: /**
145: * Constant for an underlined font
146: */
147: public static final int STYLE_UNDERLINE = 4;
148: /**
149: * Constant for a strikethrough font
150: */
151: public static final int STYLE_STRIKETHROUGH = 8;
152: /**
153: * Constant for a double strikethrough font
154: */
155: public static final int STYLE_DOUBLE_STRIKETHROUGH = 16;
156: /**
157: * Constant for a shadowed font
158: */
159: public static final int STYLE_SHADOW = 32;
160: /**
161: * Constant for an outlined font
162: */
163: public static final int STYLE_OUTLINE = 64;
164: /**
165: * Constant for an embossed font
166: */
167: public static final int STYLE_EMBOSSED = 128;
168: /**
169: * Constant for an engraved font
170: */
171: public static final int STYLE_ENGRAVED = 256;
172: /**
173: * Constant for a font that hides the actual text.
174: */
175: public static final int STYLE_HIDDEN = 512;
176:
177: /**
178: * The font name. Defaults to "Times New Roman"
179: */
180: private String fontName = "Times New Roman";
181: /**
182: * The font size. Defaults to 10
183: */
184: private int fontSize = 10;
185: /**
186: * The font style. Defaults to STYLE_NONE
187: */
188: private int fontStyle = STYLE_NONE;
189: /**
190: * The number of this font
191: */
192: private int fontNumber = 0;
193: /**
194: * The colour of this font
195: */
196: private RtfColor color = null;
197: /**
198: * The character set to use for this font
199: */
200: private int charset = 0;
201: /**
202: * The RtfDocument this RtfFont belongs to.
203: */
204: protected RtfDocument document = null;
205:
206: /**
207: * Constructs a RtfFont with the given font name and all other properties
208: * at their default values.
209: *
210: * @param fontName The font name to use
211: */
212: public RtfFont(String fontName) {
213: super (Font.UNDEFINED, Font.UNDEFINED, Font.UNDEFINED, null);
214: this .fontName = fontName;
215: }
216:
217: /**
218: * Constructs a RtfFont with the given font name and font size and all other
219: * properties at their default values.
220: *
221: * @param fontName The font name to use
222: * @param size The font size to use
223: */
224: public RtfFont(String fontName, float size) {
225: super (Font.UNDEFINED, size, Font.UNDEFINED, null);
226: this .fontName = fontName;
227: }
228:
229: /**
230: * Constructs a RtfFont with the given font name, font size and font style and the
231: * default color.
232: *
233: * @param fontName The font name to use
234: * @param size The font size to use
235: * @param style The font style to use
236: */
237: public RtfFont(String fontName, float size, int style) {
238: super (Font.UNDEFINED, size, style, null);
239: this .fontName = fontName;
240: }
241:
242: /**
243: * Constructs a RtfFont with the given font name, font size, font style and
244: * color.
245: *
246: * @param fontName The font name to use
247: * @param size the font size to use
248: * @param style The font style to use
249: * @param color The font color to use
250: */
251: public RtfFont(String fontName, float size, int style, Color color) {
252: super (Font.UNDEFINED, size, style, color);
253: this .fontName = fontName;
254: }
255:
256: /**
257: * Constructs a RtfFont with the given font name, font size, font style, colour
258: * and charset. This can be used when generating non latin-1 text.
259: *
260: * @param fontName The font name to use
261: * @param size the font size to use
262: * @param style The font style to use
263: * @param color The font color to use
264: * @param charset The charset of the font content
265: */
266: public RtfFont(String fontName, float size, int style, Color color,
267: int charset) {
268: this (fontName, size, style, color);
269: this .charset = charset;
270: }
271:
272: /**
273: * Special constructor for the default font
274: *
275: * @param doc The RtfDocument this font appears in
276: * @param fontNumber The id of this font
277: */
278: protected RtfFont(RtfDocument doc, int fontNumber) {
279: this .document = doc;
280: this .fontNumber = fontNumber;
281: color = new RtfColor(doc, 0, 0, 0);
282: }
283:
284: /**
285: * Constructs a RtfFont from a com.lowagie.text.Font
286: * @param doc The RtfDocument this font appears in
287: * @param font The Font to use as a base
288: */
289: public RtfFont(RtfDocument doc, Font font) {
290: this .document = doc;
291: if (font != null) {
292: if (font instanceof RtfFont) {
293: this .fontName = ((RtfFont) font).getFontName();
294: this .charset = ((RtfFont) font).getCharset();
295: } else {
296: setToDefaultFamily(font.getFamilyname());
297: }
298: if (font.getBaseFont() != null) {
299: String[][] fontNames = font.getBaseFont()
300: .getFullFontName();
301: for (int i = 0; i < fontNames.length; i++) {
302: if (fontNames[i][2].equals("0")) {
303: this .fontName = fontNames[i][3];
304: break;
305: } else if (fontNames[i][2].equals("1033")
306: || fontNames[i][2].equals("")) {
307: this .fontName = fontNames[i][3];
308: }
309: }
310: }
311:
312: setSize(font.getSize());
313: setStyle(font.getStyle());
314: setColor(font.getColor());
315: }
316:
317: if (this .fontName.equalsIgnoreCase("unknown")) {
318: return;
319: }
320:
321: if (document != null) {
322: setRtfDocument(document);
323: }
324: }
325:
326: /**
327: * Writes the font definition
328: *
329: * @return A byte array with the font definition
330: * @deprecated replaced by {@link #writeDefinition(OutputStream)}
331: */
332: public byte[] writeDefinition() {
333: ByteArrayOutputStream result = new ByteArrayOutputStream();
334: try {
335: writeDefinition(result);
336: } catch (IOException ioe) {
337: ioe.printStackTrace();
338: }
339: return result.toByteArray();
340: }
341:
342: /**
343: * Writes the font definition
344: */
345: public void writeDefinition(final OutputStream result)
346: throws IOException {
347: result.write(FONT_FAMILY);
348: result.write(FONT_CHARSET);
349: result.write(intToByteArray(charset));
350: result.write(DELIMITER);
351: //.result.write(document.filterSpecialChar(fontName, true, false).getBytes());
352: document.filterSpecialChar(result, fontName, true, false);
353: }
354:
355: /**
356: * Writes the font beginning
357: *
358: * @return A byte array with the font start data
359: */
360: public byte[] writeBegin() {
361: ByteArrayOutputStream result = new ByteArrayOutputStream();
362: try {
363: if (this .fontNumber != Font.UNDEFINED) {
364: result.write(RtfFontList.FONT_NUMBER);
365: result.write(intToByteArray(fontNumber));
366: }
367: if (this .fontSize != Font.UNDEFINED) {
368: result.write(FONT_SIZE);
369: result.write(intToByteArray(fontSize * 2));
370: }
371: if (this .fontStyle != UNDEFINED) {
372: if ((fontStyle & STYLE_BOLD) == STYLE_BOLD) {
373: result.write(FONT_BOLD);
374: }
375: if ((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) {
376: result.write(FONT_ITALIC);
377: }
378: if ((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) {
379: result.write(FONT_UNDERLINE);
380: }
381: if ((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) {
382: result.write(FONT_STRIKETHROUGH);
383: }
384: if ((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) {
385: result.write(FONT_HIDDEN);
386: }
387: if ((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) {
388: result.write(FONT_DOUBLE_STRIKETHROUGH);
389: result.write(intToByteArray(1));
390: }
391: if ((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) {
392: result.write(FONT_SHADOW);
393: }
394: if ((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) {
395: result.write(FONT_OUTLINE);
396: }
397: if ((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) {
398: result.write(FONT_EMBOSSED);
399: }
400: if ((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) {
401: result.write(FONT_ENGRAVED);
402: }
403: }
404: if (color != null) {
405: result.write(color.writeBegin());
406: }
407: } catch (IOException ioe) {
408: ioe.printStackTrace();
409: }
410: return result.toByteArray();
411: }
412:
413: /**
414: * Write the font end
415: *
416: * @return A byte array with the end of font data
417: */
418: public byte[] writeEnd() {
419: ByteArrayOutputStream result = new ByteArrayOutputStream();
420: try {
421: if (this .fontStyle != UNDEFINED) {
422: if ((fontStyle & STYLE_BOLD) == STYLE_BOLD) {
423: result.write(FONT_BOLD);
424: result.write(intToByteArray(0));
425: }
426: if ((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) {
427: result.write(FONT_ITALIC);
428: result.write(intToByteArray(0));
429: }
430: if ((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) {
431: result.write(FONT_UNDERLINE);
432: result.write(intToByteArray(0));
433: }
434: if ((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) {
435: result.write(FONT_STRIKETHROUGH);
436: result.write(intToByteArray(0));
437: }
438: if ((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) {
439: result.write(FONT_HIDDEN);
440: result.write(intToByteArray(0));
441: }
442: if ((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) {
443: result.write(FONT_DOUBLE_STRIKETHROUGH);
444: result.write(intToByteArray(0));
445: }
446: if ((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) {
447: result.write(FONT_SHADOW);
448: result.write(intToByteArray(0));
449: }
450: if ((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) {
451: result.write(FONT_OUTLINE);
452: result.write(intToByteArray(0));
453: }
454: if ((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) {
455: result.write(FONT_EMBOSSED);
456: result.write(intToByteArray(0));
457: }
458: if ((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) {
459: result.write(FONT_ENGRAVED);
460: result.write(intToByteArray(0));
461: }
462: }
463: } catch (IOException ioe) {
464: ioe.printStackTrace();
465: }
466: return result.toByteArray();
467: }
468:
469: /**
470: * Unused
471: * @return an empty byte array
472: * @deprecated replaced by {@link #writeContent(OutputStream)}
473: */
474: public byte[] write() {
475: return new byte[0];
476: }
477:
478: /**
479: * unused
480: */
481: public void writeContent(OutputStream out) throws IOException {
482: }
483:
484: /**
485: * Tests for equality of RtfFonts. RtfFonts are equal if their fontName,
486: * fontSize, fontStyle and fontSuperSubscript are equal
487: *
488: * @param obj The RtfFont to compare with this RtfFont
489: * @return <code>True</code> if the RtfFonts are equal, <code>false</code> otherwise
490: */
491: public boolean equals(Object obj) {
492: if (!(obj instanceof RtfFont)) {
493: return false;
494: }
495: RtfFont font = (RtfFont) obj;
496: boolean result = true;
497: result = result & this .fontName.equals(font.getFontName());
498:
499: return result;
500: }
501:
502: /**
503: * Returns the hash code of this RtfFont. The hash code is the hash code of the
504: * string containing the font name + font size + "-" + the font style + "-" + the
505: * font super/supscript value.
506: *
507: * @return The hash code of this RtfFont
508: */
509: public int hashCode() {
510: return (this .fontName + this .fontSize + "-" + this .fontStyle)
511: .hashCode();
512: }
513:
514: /**
515: * Gets the font name of this RtfFont
516: *
517: * @return The font name
518: */
519: public String getFontName() {
520: return this .fontName;
521: }
522:
523: /**
524: * Sets the font name of this RtfFont.
525: *
526: * @param fontName The font name to use
527: */
528: protected void setFontName(String fontName) {
529: this .fontName = fontName;
530: if (document != null) {
531: this .fontNumber = document.getDocumentHeader()
532: .getFontNumber(this );
533: }
534: }
535:
536: /**
537: * @see com.lowagie.text.Font#getFamilyname()
538: */
539: public String getFamilyname() {
540: return this .fontName;
541: }
542:
543: /**
544: * @see com.lowagie.text.Font#setFamily(String)
545: */
546: public void setFamily(String family) {
547: super .setFamily(family);
548: setToDefaultFamily(family);
549: }
550:
551: /**
552: * Sets the correct font name from the family name.
553: *
554: * @param familyname The family name to set the name to.
555: */
556: private void setToDefaultFamily(String familyname) {
557: switch (Font.getFamilyIndex(familyname)) {
558: case Font.COURIER:
559: this .fontName = "Courier";
560: break;
561: case Font.HELVETICA:
562: this .fontName = "Arial";
563: break;
564: case Font.SYMBOL:
565: this .fontName = "Symbol";
566: this .charset = 2;
567: break;
568: case Font.TIMES_ROMAN:
569: this .fontName = "Times New Roman";
570: break;
571: case Font.ZAPFDINGBATS:
572: this .fontName = "Windings";
573: break;
574: default:
575: this .fontName = familyname;
576: }
577: }
578:
579: /**
580: * Gets the font size of this RtfFont
581: *
582: * @return The font size
583: */
584: public int getFontSize() {
585: return this .fontSize;
586: }
587:
588: /**
589: * @see com.lowagie.text.Font#setSize(float)
590: */
591: public void setSize(float size) {
592: super .setSize(size);
593: this .fontSize = (int) getSize();
594: }
595:
596: /**
597: * Gets the font style of this RtfFont
598: *
599: * @return The font style
600: */
601: public int getFontStyle() {
602: return this .fontStyle;
603: }
604:
605: /**
606: * @see com.lowagie.text.Font#setStyle(int)
607: */
608: public void setStyle(int style) {
609: super .setStyle(style);
610: this .fontStyle = getStyle();
611: }
612:
613: /**
614: * @see com.lowagie.text.Font#setStyle(String)
615: */
616: public void setStyle(String style) {
617: super .setStyle(style);
618: fontStyle = getStyle();
619: }
620:
621: /**
622: * Gets the charset used for constructing this RtfFont.
623: *
624: * @return The charset of this RtfFont.
625: */
626: public int getCharset() {
627: return charset;
628: }
629:
630: /**
631: * Sets the charset used for constructing this RtfFont.
632: *
633: * @param charset The charset to use.
634: */
635: public void setCharset(int charset) {
636: this .charset = charset;
637: }
638:
639: /**
640: * Gets the font number of this RtfFont
641: *
642: * @return The font number
643: */
644: public int getFontNumber() {
645: return fontNumber;
646: }
647:
648: /**
649: * Sets the RtfDocument this RtfFont belongs to
650: *
651: * @param doc The RtfDocument to use
652: */
653: public void setRtfDocument(RtfDocument doc) {
654: this .document = doc;
655: if (document != null) {
656: this .fontNumber = document.getDocumentHeader()
657: .getFontNumber(this );
658: }
659: if (this .color != null) {
660: this .color.setRtfDocument(this .document);
661: }
662: }
663:
664: /**
665: * Unused
666: * @param inTable
667: */
668: public void setInTable(boolean inTable) {
669: }
670:
671: /**
672: * Unused
673: * @param inHeader
674: */
675: public void setInHeader(boolean inHeader) {
676: }
677:
678: /**
679: * @see com.lowagie.text.Font#setColor(Color)
680: */
681: public void setColor(Color color) {
682: super .setColor(color);
683: if (color != null) {
684: this .color = new RtfColor(document, color);
685: } else {
686: this .color = null;
687: }
688: }
689:
690: /**
691: * @see com.lowagie.text.Font#setColor(int, int, int)
692: */
693: public void setColor(int red, int green, int blue) {
694: super .setColor(red, green, blue);
695: this .color = new RtfColor(document, red, green, blue);
696: }
697:
698: /**
699: * Transforms an integer into its String representation and then returns the bytes
700: * of that string.
701: *
702: * @param i The integer to convert
703: * @return A byte array representing the integer
704: */
705: protected byte[] intToByteArray(int i) {
706: return Integer.toString(i).getBytes();
707: }
708:
709: /**
710: * Replaces the attributes that are equal to <VAR>null</VAR> with
711: * the attributes of a given font.
712: *
713: * @param font The surrounding font
714: * @return A RtfFont
715: */
716: public Font difference(Font font) {
717: String dFamilyname = font.getFamilyname();
718: if (dFamilyname == null || dFamilyname.trim().equals("")
719: || dFamilyname.trim().equalsIgnoreCase("unknown")) {
720: dFamilyname = this .fontName;
721: }
722:
723: float dSize = font.getSize();
724: if (dSize == Font.UNDEFINED) {
725: dSize = this .getSize();
726: }
727:
728: int dStyle = Font.UNDEFINED;
729: if (this .getStyle() != Font.UNDEFINED
730: && font.getStyle() != Font.UNDEFINED) {
731: dStyle = this .getStyle() | font.getStyle();
732: } else if (this .getStyle() != Font.UNDEFINED) {
733: dStyle = this .getStyle();
734: } else if (font.getStyle() != Font.UNDEFINED) {
735: dStyle = font.getStyle();
736: }
737:
738: Color dColor = font.getColor();
739: if (dColor == null) {
740: dColor = this .getColor();
741: }
742:
743: int dCharset = this .charset;
744: if (font instanceof RtfFont) {
745: dCharset = ((RtfFont) font).getCharset();
746: }
747:
748: return new RtfFont(dFamilyname, dSize, dStyle, dColor, dCharset);
749: }
750: }
|