KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > style > RtfFont


1 /*
2  * $Id: RtfFont.java 2784 2007-05-24 15:43:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2001, 2002, 2003, 2004 by Mark Hall
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.rtf.style;
52
53 import java.awt.Color JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.OutputStream JavaDoc;
57
58 import com.lowagie.text.Font;
59 import com.lowagie.text.rtf.RtfExtendedElement;
60 import com.lowagie.text.rtf.document.RtfDocument;
61
62 /**
63  * The RtfFont class stores one font for an rtf document. It extends Font,
64  * so can be set as a font, to allow adding of fonts with arbitrary names.
65  * BaseFont fontname handling contributed by Craig Fleming. Various fixes
66  * Renaud Michel, Werner Daehn.
67  *
68  * Version: $Id: RtfFont.java 2784 2007-05-24 15:43:40Z hallm $
69  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
70  * @author Craig Fleming (rythos@rhana.dhs.org)
71  * @author Renaud Michel (r.michel@immedia.be)
72  * @author Werner Daehn (Werner.Daehn@BusinessObjects.com)
73  * @author Lidong Liu (tmslld@gmail.com)
74  * @author Thomas Bickel (tmb99@inode.at)
75  */

76 public class RtfFont extends Font implements RtfExtendedElement {
77     /**
78      * Constant for the font family to use ("froman")
79      */

80     private static final byte[] FONT_FAMILY = "\\froman".getBytes();
81     /**
82      * Constant for the charset
83      */

84     private static final byte[] FONT_CHARSET = "\\fcharset".getBytes();
85     /**
86      * Constant for the font size
87      */

88     public static final byte[] FONT_SIZE = "\\fs".getBytes();
89     /**
90      * Constant for the bold flag
91      */

92     private static final byte[] FONT_BOLD = "\\b".getBytes();
93     /**
94      * Constant for the italic flag
95      */

96     private static final byte[] FONT_ITALIC = "\\i".getBytes();
97     /**
98      * Constant for the underline flag
99      */

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".getBytes();
105     /**
106      * Constant for the double strikethrough flag
107      */

108     private static final byte[] FONT_DOUBLE_STRIKETHROUGH = "\\striked".getBytes();
109     /**
110      * Constant for the shadow flag
111      */

112     private static final byte[] FONT_SHADOW = "\\shad".getBytes();
113     /**
114      * Constant for the outline flag
115      */

116     private static final byte[] FONT_OUTLINE = "\\outl".getBytes();
117     /**
118      * Constant for the embossed flag
119      */

120     private static final byte[] FONT_EMBOSSED = "\\embo".getBytes();
121     /**
122      * Constant for the engraved flag
123      */

124     private static final byte[] FONT_ENGRAVED = "\\impr".getBytes();
125     /**
126      * Constant for hidden text flag
127      */

128     private static final byte[] FONT_HIDDEN = "\\v".getBytes();
129     
130     /**
131      * Constant for a plain font
132      */

133     public static final int STYLE_NONE = 0;
134     /**
135      * Constant for a bold font
136      */

137     public static final int STYLE_BOLD = 1;
138     /**
139      * Constant for an italic font
140      */

141     public static final int STYLE_ITALIC = 2;
142     /**
143      * Constant for an underlined font
144      */

145     public static final int STYLE_UNDERLINE = 4;
146     /**
147      * Constant for a strikethrough font
148      */

149     public static final int STYLE_STRIKETHROUGH = 8;
150     /**
151      * Constant for a double strikethrough font
152      */

153     public static final int STYLE_DOUBLE_STRIKETHROUGH = 16;
154     /**
155      * Constant for a shadowed font
156      */

157     public static final int STYLE_SHADOW = 32;
158     /**
159      * Constant for an outlined font
160      */

161     public static final int STYLE_OUTLINE = 64;
162     /**
163      * Constant for an embossed font
164      */

165     public static final int STYLE_EMBOSSED = 128;
166     /**
167      * Constant for an engraved font
168      */

169     public static final int STYLE_ENGRAVED = 256;
170     /**
171      * Constant for a font that hides the actual text.
172      */

173     public static final int STYLE_HIDDEN = 512;
174
175     /**
176      * The font name. Defaults to "Times New Roman"
177      */

178     private String JavaDoc fontName = "Times New Roman";
179     /**
180      * The font size. Defaults to 10
181      */

182     private int fontSize = 10;
183     /**
184      * The font style. Defaults to STYLE_NONE
185      */

186     private int fontStyle = STYLE_NONE;
187     /**
188      * The number of this font
189      */

190     private int fontNumber = 0;
191     /**
192      * The colour of this font
193      */

194     private RtfColor color = null;
195     /**
196      * The character set to use for this font
197      */

198     private int charset = 0;
199     /**
200      * The RtfDocument this RtfFont belongs to.
201      */

202     protected RtfDocument document = null;
203     
204     /**
205      * Constructs a RtfFont with the given font name and all other properties
206      * at their default values.
207      *
208      * @param fontName The font name to use
209      */

210     public RtfFont(String JavaDoc fontName) {
211         super(Font.UNDEFINED, Font.UNDEFINED, Font.UNDEFINED, null);
212         this.fontName = fontName;
213     }
214     
215     /**
216      * Constructs a RtfFont with the given font name and font size and all other
217      * properties at their default values.
218      *
219      * @param fontName The font name to use
220      * @param size The font size to use
221      */

222     public RtfFont(String JavaDoc fontName, float size) {
223         super(Font.UNDEFINED, size, Font.UNDEFINED, null);
224         this.fontName = fontName;
225     }
226     
227     /**
228      * Constructs a RtfFont with the given font name, font size and font style and the
229      * default color.
230      *
231      * @param fontName The font name to use
232      * @param size The font size to use
233      * @param style The font style to use
234      */

235     public RtfFont(String JavaDoc fontName, float size, int style) {
236         super(Font.UNDEFINED, size, style, null);
237         this.fontName = fontName;
238     }
239     
240     /**
241      * Constructs a RtfFont with the given font name, font size, font style and
242      * color.
243      *
244      * @param fontName The font name to use
245      * @param size the font size to use
246      * @param style The font style to use
247      * @param color The font color to use
248      */

249     public RtfFont(String JavaDoc fontName, float size, int style, Color JavaDoc color) {
250         super(Font.UNDEFINED, size, style, color);
251         this.fontName = fontName;
252     }
253     
254     /**
255      * Constructs a RtfFont with the given font name, font size, font style, colour
256      * and charset. This can be used when generating non latin-1 text.
257      *
258      * @param fontName The font name to use
259      * @param size the font size to use
260      * @param style The font style to use
261      * @param color The font color to use
262      * @param charset The charset of the font content
263      */

264     public RtfFont(String JavaDoc fontName, float size, int style, Color JavaDoc color, int charset) {
265         this(fontName, size, style, color);
266         this.charset = charset;
267     }
268
269     /**
270      * Special constructor for the default font
271      *
272      * @param doc The RtfDocument this font appears in
273      * @param fontNumber The id of this font
274      */

275     protected RtfFont(RtfDocument doc, int fontNumber) {
276         this.document = doc;
277         this.fontNumber = fontNumber;
278         color = new RtfColor(doc, 0, 0, 0);
279     }
280
281     /**
282      * Constructs a RtfFont from a com.lowagie.text.Font
283      * @param doc The RtfDocument this font appears in
284      * @param font The Font to use as a base
285      */

286     public RtfFont(RtfDocument doc, Font font) {
287         this.document = doc;
288         if(font != null) {
289             if(font instanceof RtfFont) {
290                 this.fontName = ((RtfFont) font).getFontName();
291                 this.charset = ((RtfFont) font).getCharset();
292             } else {
293                 setToDefaultFamily(font.getFamilyname());
294             }
295             if(font.getBaseFont() != null) {
296                 String JavaDoc[][] fontNames = font.getBaseFont().getFullFontName();
297                 for(int i = 0; i < fontNames.length; i++) {
298                     if(fontNames[i][2].equals("0")) {
299                         this.fontName = fontNames[i][3];
300                         break;
301                     } else if(fontNames[i][2].equals("1033") || fontNames[i][2].equals("")) {
302                         this.fontName = fontNames[i][3];
303                     }
304                 }
305             }
306             
307             setSize(font.getSize());
308             setStyle(font.getStyle());
309             setColor(font.getColor());
310         }
311
312         if(this.fontName.equalsIgnoreCase("unknown")) {
313             return;
314         }
315
316         if(document != null) {
317             setRtfDocument(document);
318         }
319     }
320
321     /**
322      * Writes the font definition
323      *
324      * @return A byte array with the font definition
325      * @deprecated replaced by {@link #writeDefinition(OutputStream)}
326      */

327     public byte[] writeDefinition() {
328         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
329         try {
330             writeDefinition(result);
331         } catch(IOException JavaDoc ioe) {
332             ioe.printStackTrace();
333         }
334         return result.toByteArray();
335     }
336     
337     /**
338      * Writes the font definition
339      */

340     public void writeDefinition(final OutputStream JavaDoc result) throws IOException JavaDoc
341     {
342         result.write(FONT_FAMILY);
343         result.write(FONT_CHARSET);
344         result.write(intToByteArray(charset));
345         result.write(DELIMITER);
346         //.result.write(document.filterSpecialChar(fontName, true, false).getBytes());
347
document.filterSpecialChar(result, fontName, true, false);
348     }
349     
350     /**
351      * Writes the font beginning
352      *
353      * @return A byte array with the font start data
354      */

355     public byte[] writeBegin() {
356         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
357         try {
358             if(this.fontNumber != Font.UNDEFINED) {
359                 result.write(RtfFontList.FONT_NUMBER);
360                 result.write(intToByteArray(fontNumber));
361             }
362             if(this.fontSize != Font.UNDEFINED) {
363                 result.write(FONT_SIZE);
364                 result.write(intToByteArray(fontSize * 2));
365             }
366             if(this.fontStyle != UNDEFINED) {
367                 if((fontStyle & STYLE_BOLD) == STYLE_BOLD) {
368                     result.write(FONT_BOLD);
369                 }
370                 if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) {
371                     result.write(FONT_ITALIC);
372                 }
373                 if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) {
374                     result.write(FONT_UNDERLINE);
375                 }
376                 if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) {
377                     result.write(FONT_STRIKETHROUGH);
378                 }
379                 if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) {
380                     result.write(FONT_HIDDEN);
381                 }
382                 if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) {
383                     result.write(FONT_DOUBLE_STRIKETHROUGH);
384                     result.write(intToByteArray(1));
385                 }
386                 if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) {
387                     result.write(FONT_SHADOW);
388                 }
389                 if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) {
390                     result.write(FONT_OUTLINE);
391                 }
392                 if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) {
393                     result.write(FONT_EMBOSSED);
394                 }
395                 if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) {
396                     result.write(FONT_ENGRAVED);
397                 }
398             }
399             if(color != null) {
400                 result.write(color.writeBegin());
401             }
402         } catch(IOException JavaDoc ioe) {
403             ioe.printStackTrace();
404         }
405         return result.toByteArray();
406     }
407     
408     /**
409      * Write the font end
410      *
411      * @return A byte array with the end of font data
412      */

413     public byte[] writeEnd() {
414         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
415         try {
416             if(this.fontStyle != UNDEFINED) {
417                 if((fontStyle & STYLE_BOLD) == STYLE_BOLD) {
418                     result.write(FONT_BOLD);
419                     result.write(intToByteArray(0));
420                 }
421                 if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) {
422                     result.write(FONT_ITALIC);
423                     result.write(intToByteArray(0));
424                 }
425                 if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) {
426                     result.write(FONT_UNDERLINE);
427                     result.write(intToByteArray(0));
428                 }
429                 if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) {
430                     result.write(FONT_STRIKETHROUGH);
431                     result.write(intToByteArray(0));
432                 }
433                 if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) {
434                     result.write(FONT_HIDDEN);
435                     result.write(intToByteArray(0));
436                 }
437                 if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) {
438                     result.write(FONT_DOUBLE_STRIKETHROUGH);
439                     result.write(intToByteArray(0));
440                 }
441                 if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) {
442                     result.write(FONT_SHADOW);
443                     result.write(intToByteArray(0));
444                 }
445                 if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) {
446                     result.write(FONT_OUTLINE);
447                     result.write(intToByteArray(0));
448                 }
449                 if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) {
450                     result.write(FONT_EMBOSSED);
451                     result.write(intToByteArray(0));
452                 }
453                 if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) {
454                     result.write(FONT_ENGRAVED);
455                     result.write(intToByteArray(0));
456                 }
457             }
458         } catch(IOException JavaDoc ioe) {
459             ioe.printStackTrace();
460         }
461         return result.toByteArray();
462     }
463
464     /**
465      * Unused
466      * @return an empty byte array
467      * @deprecated replaced by {@link #writeContent(OutputStream)}
468      */

469     public byte[] write() {
470         return new byte[0];
471     }
472     /**
473      * unused
474      */

475     public void writeContent(OutputStream JavaDoc out) throws IOException JavaDoc
476     {
477     }
478     
479     /**
480      * Tests for equality of RtfFonts. RtfFonts are equal if their fontName,
481      * fontSize, fontStyle and fontSuperSubscript are equal
482      *
483      * @param obj The RtfFont to compare with this RtfFont
484      * @return <code>True</code> if the RtfFonts are equal, <code>false</code> otherwise
485      */

486     public boolean equals(Object JavaDoc obj) {
487         if(!(obj instanceof RtfFont)) {
488             return false;
489         }
490         RtfFont font = (RtfFont) obj;
491         boolean result = true;
492         result = result & this.fontName.equals(font.getFontName());
493
494         return result;
495     }
496
497     /**
498      * Returns the hash code of this RtfFont. The hash code is the hash code of the
499      * string containing the font name + font size + "-" + the font style + "-" + the
500      * font super/supscript value.
501      *
502      * @return The hash code of this RtfFont
503      */

504     public int hashCode() {
505         return (this.fontName + this.fontSize + "-" + this.fontStyle).hashCode();
506     }
507     
508     /**
509      * Gets the font name of this RtfFont
510      *
511      * @return The font name
512      */

513     public String JavaDoc getFontName() {
514         return this.fontName;
515     }
516
517     /**
518      * Sets the font name of this RtfFont.
519      *
520      * @param fontName The font name to use
521      */

522     protected void setFontName(String JavaDoc fontName) {
523         this.fontName = fontName;
524         if(document != null) {
525             this.fontNumber = document.getDocumentHeader().getFontNumber(this);
526         }
527     }
528     
529     /**
530      * @see com.lowagie.text.Font#getFamilyname()
531      */

532     public String JavaDoc getFamilyname() {
533         return this.fontName;
534     }
535     
536     /**
537      * @see com.lowagie.text.Font#setFamily(String)
538      */

539     public void setFamily(String JavaDoc family){
540         super.setFamily(family);
541         setToDefaultFamily(family);
542     }
543     
544     /**
545      * Sets the correct font name from the family name.
546      *
547      * @param familyname The family name to set the name to.
548      */

549     private void setToDefaultFamily(String JavaDoc familyname){
550         switch (Font.getFamilyIndex(familyname)) {
551             case Font.COURIER:
552                 this.fontName = "Courier";
553                 break;
554             case Font.HELVETICA:
555                 this.fontName = "Arial";
556                 break;
557             case Font.SYMBOL:
558                 this.fontName = "Symbol";
559                 this.charset = 2;
560                 break;
561             case Font.TIMES_ROMAN:
562                 this.fontName = "Times New Roman";
563                 break;
564             case Font.ZAPFDINGBATS:
565                 this.fontName = "Windings";
566                 break;
567             default:
568                 this.fontName = familyname;
569         }
570     }
571     
572     /**
573      * Gets the font size of this RtfFont
574      *
575      * @return The font size
576      */

577     public int getFontSize() {
578         return this.fontSize;
579     }
580     
581     /**
582      * @see com.lowagie.text.Font#setSize(float)
583      */

584     public void setSize(float size){
585         super.setSize(size);
586         this.fontSize = (int) getSize();
587     }
588
589     /**
590      * Gets the font style of this RtfFont
591      *
592      * @return The font style
593      */

594     public int getFontStyle() {
595         return this.fontStyle;
596     }
597     
598     /**
599      * @see com.lowagie.text.Font#setStyle(int)
600      */

601     public void setStyle(int style){
602         super.setStyle(style);
603         this.fontStyle = getStyle();
604     }
605     
606     /**
607      * @see com.lowagie.text.Font#setStyle(String)
608      */

609     public void setStyle(String JavaDoc style) {
610         super.setStyle(style);
611         fontStyle = getStyle();
612     }
613
614     /**
615      * Gets the charset used for constructing this RtfFont.
616      *
617      * @return The charset of this RtfFont.
618      */

619     public int getCharset() {
620         return charset;
621     }
622
623     /**
624      * Sets the charset used for constructing this RtfFont.
625      *
626      * @param charset The charset to use.
627      */

628     public void setCharset(int charset) {
629         this.charset = charset;
630     }
631
632     /**
633      * Gets the font number of this RtfFont
634      *
635      * @return The font number
636      */

637     public int getFontNumber() {
638         return fontNumber;
639     }
640
641     /**
642      * Sets the RtfDocument this RtfFont belongs to
643      *
644      * @param doc The RtfDocument to use
645      */

646     public void setRtfDocument(RtfDocument doc) {
647         this.document = doc;
648         if(document != null) {
649             this.fontNumber = document.getDocumentHeader().getFontNumber(this);
650         }
651         if(this.color != null) {
652             this.color.setRtfDocument(this.document);
653         }
654     }
655
656     /**
657      * Unused
658      * @param inTable
659      */

660     public void setInTable(boolean inTable) {
661     }
662     
663     /**
664      * Unused
665      * @param inHeader
666      */

667     public void setInHeader(boolean inHeader) {
668     }
669     
670     /**
671      * @see com.lowagie.text.Font#setColor(Color)
672      */

673     public void setColor(Color JavaDoc color) {
674         super.setColor(color);
675         if(color != null) {
676             this.color = new RtfColor(document, color);
677         } else {
678             this.color = null;
679         }
680     }
681     
682     /**
683      * @see com.lowagie.text.Font#setColor(int, int, int)
684      */

685     public void setColor(int red, int green, int blue) {
686         super.setColor(red,green,blue);
687         this.color = new RtfColor(document, red, green, blue);
688     }
689
690     /**
691      * Transforms an integer into its String representation and then returns the bytes
692      * of that string.
693      *
694      * @param i The integer to convert
695      * @return A byte array representing the integer
696      */

697     protected byte[] intToByteArray(int i) {
698         return Integer.toString(i).getBytes();
699     }
700
701     /**
702      * Replaces the attributes that are equal to <VAR>null</VAR> with
703      * the attributes of a given font.
704      *
705      * @param font The surrounding font
706      * @return A RtfFont
707      */

708     public Font difference(Font font) {
709         String JavaDoc dFamilyname = font.getFamilyname();
710         if(dFamilyname == null || dFamilyname.trim().equals("") || dFamilyname.trim().equalsIgnoreCase("unknown")) {
711             dFamilyname = this.fontName;
712         }
713
714         float dSize = font.getSize();
715         if(dSize == Font.UNDEFINED) {
716             dSize = this.getSize();
717         }
718
719         int dStyle = Font.UNDEFINED;
720         if(this.getStyle() != Font.UNDEFINED && font.getStyle() != Font.UNDEFINED) {
721             dStyle = this.getStyle() | font.getStyle();
722         } else if(this.getStyle() != Font.UNDEFINED) {
723             dStyle = this.getStyle();
724         } else if(font.getStyle() != Font.UNDEFINED) {
725             dStyle = font.getStyle();
726         }
727
728         Color JavaDoc dColor = font.getColor();
729         if(dColor == null) {
730             dColor = this.getColor();
731         }
732         
733         int dCharset = this.charset;
734         if(font instanceof RtfFont) {
735             dCharset = ((RtfFont) font).getCharset();
736         }
737         
738         return new RtfFont(dFamilyname, dSize, dStyle, dColor, dCharset);
739     }
740 }
741
Popular Tags