1 /*
2 * $Id: RtfFont.java 3373 2008-05-12 16:21:24Z xlv $
3 *
4 * Copyright 2001, 2002, 2003, 2004 by Mark Hall
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * (the "License"); you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the License.
13 *
14 * The Original Code is 'iText, a free JAVA-PDF library'.
15 *
16 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18 * All Rights Reserved.
19 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21 *
22 * Contributor(s): all the names of the contributors are added in the source code
23 * where applicable.
24 *
25 * Alternatively, the contents of this file may be used under the terms of the
26 * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
27 * provisions of LGPL are applicable instead of those above. If you wish to
28 * allow use of your version of this file only under the terms of the LGPL
29 * License and not to allow others to use your version of this file under
30 * the MPL, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the LGPL.
32 * If you do not delete the provisions above, a recipient may use your version
33 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the MPL as stated above or under the terms of the GNU
37 * Library General Public License as published by the Free Software Foundation;
38 * either version 2 of the License, or any later version.
39 *
40 * This library is distributed in the hope that it will be useful, but WITHOUT
41 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43 * details.
44 *
45 * If you didn't download this code from the following link, you should check if
46 * you aren't using an obsolete version:
47 * http://www.lowagie.com/iText/
48 */
49
50 package com.lowagie.text.rtf.style;
51
52 import java.awt.Color;
53 import java.io.IOException;
54 import java.io.OutputStream;
55
56 import com.lowagie.text.Font;
57 import com.lowagie.text.rtf.RtfExtendedElement;
58 import com.lowagie.text.rtf.document.RtfDocument;
59
60 /**
61 * The RtfFont class stores one font for an rtf document. It extends Font,
62 * so can be set as a font, to allow adding of fonts with arbitrary names.
63 * BaseFont fontname handling contributed by Craig Fleming. Various fixes
64 * Renaud Michel, Werner Daehn.
65 *
66 * Version: $Id: RtfFont.java 3373 2008-05-12 16:21:24Z xlv $
67 * @author Mark Hall (Mark.Hall@mail.room3b.eu)
68 * @author Craig Fleming (rythos@rhana.dhs.org)
69 * @author Renaud Michel (r.michel@immedia.be)
70 * @author Werner Daehn (Werner.Daehn@BusinessObjects.com)
71 * @author Lidong Liu (tmslld@gmail.com)
72 * @author Thomas Bickel (tmb99@inode.at)
73 */
74 public class RtfFont extends Font implements RtfExtendedElement {
75 /**
76 * Constant for the font family to use ("froman")
77 */
78 private static final byte[] FONT_FAMILY = "\\froman".getBytes();
79 /**
80 * Constant for the charset
81 */
82 private static final byte[] FONT_CHARSET = "\\fcharset".getBytes();
83 /**
84 * Constant for the font size
85 */
86 public static final byte[] FONT_SIZE = "\\fs".getBytes();
87 /**
88 * Constant for the bold flag
89 */
90 private static final byte[] FONT_BOLD = "\\b".getBytes();
91 /**
92 * Constant for the italic flag
93 */
94 private static final byte[] FONT_ITALIC = "\\i".getBytes();
95 /**
96 * Constant for the underline flag
97 */
98 private static final byte[] FONT_UNDERLINE = "\\ul".getBytes();
99 /**
100 * Constant for the strikethrough flag
101 */
102 private static final byte[] FONT_STRIKETHROUGH = "\\strike".getBytes();
103 /**
104 * Constant for the double strikethrough flag
105 */
106 private static final byte[] FONT_DOUBLE_STRIKETHROUGH = "\\striked".getBytes();
107 /**
108 * Constant for the shadow flag
109 */
110 private static final byte[] FONT_SHADOW = "\\shad".getBytes();
111 /**
112 * Constant for the outline flag
113 */
114 private static final byte[] FONT_OUTLINE = "\\outl".getBytes();
115 /**
116 * Constant for the embossed flag
117 */
118 private static final byte[] FONT_EMBOSSED = "\\embo".getBytes();
119 /**
120 * Constant for the engraved flag
121 */
122 private static final byte[] FONT_ENGRAVED = "\\impr".getBytes();
123 /**
124 * Constant for hidden text flag
125 */
126 private static final byte[] FONT_HIDDEN = "\\v".getBytes();
127
128 /**
129 * Constant for a plain font
130 */
131 public static final int STYLE_NONE = 0;
132 /**
133 * Constant for a bold font
134 */
135 public static final int STYLE_BOLD = 1;
136 /**
137 * Constant for an italic font
138 */
139 public static final int STYLE_ITALIC = 2;
140 /**
141 * Constant for an underlined font
142 */
143 public static final int STYLE_UNDERLINE = 4;
144 /**
145 * Constant for a strikethrough font
146 */
147 public static final int STYLE_STRIKETHROUGH = 8;
148 /**
149 * Constant for a double strikethrough font
150 */
151 public static final int STYLE_DOUBLE_STRIKETHROUGH = 16;
152 /**
153 * Constant for a shadowed font
154 */
155 public static final int STYLE_SHADOW = 32;
156 /**
157 * Constant for an outlined font
158 */
159 public static final int STYLE_OUTLINE = 64;
160 /**
161 * Constant for an embossed font
162 */
163 public static final int STYLE_EMBOSSED = 128;
164 /**
165 * Constant for an engraved font
166 */
167 public static final int STYLE_ENGRAVED = 256;
168 /**
169 * Constant for a font that hides the actual text.
170 */
171 public static final int STYLE_HIDDEN = 512;
172
173 /**
174 * The font name. Defaults to "Times New Roman"
175 */
176 private String fontName = "Times New Roman";
177 /**
178 * The font size. Defaults to 10
179 */
180 private int fontSize = 10;
181 /**
182 * The font style. Defaults to STYLE_NONE
183 */
184 private int fontStyle = STYLE_NONE;
185 /**
186 * The number of this font
187 */
188 private int fontNumber = 0;
189 /**
190 * The color of this font
191 */
192 private RtfColor color = null;
193 /**
194 * The character set to use for this font
195 */
196 private int charset = 0;
197 /**
198 * The RtfDocument this RtfFont belongs to.
199 */
200 protected RtfDocument document = null;
201
202 /**
203 * Constructs a RtfFont with the given font name and all other properties
204 * at their default values.
205 *
206 * @param fontName The font name to use
207 */
208 public RtfFont(String fontName) {
209 super(Font.UNDEFINED, Font.UNDEFINED, Font.UNDEFINED, null);
210 this.fontName = fontName;
211 }
212
213 /**
214 * Constructs a RtfFont with the given font name and font size and all other
215 * properties at their default values.
216 *
217 * @param fontName The font name to use
218 * @param size The font size to use
219 */
220 public RtfFont(String fontName, float size) {
221 super(Font.UNDEFINED, size, Font.UNDEFINED, null);
222 this.fontName = fontName;
223 }
224
225 /**
226 * Constructs a RtfFont with the given font name, font size and font style and the
227 * default color.
228 *
229 * @param fontName The font name to use
230 * @param size The font size to use
231 * @param style The font style to use
232 */
233 public RtfFont(String fontName, float size, int style) {
234 super(Font.UNDEFINED, size, style, null);
235 this.fontName = fontName;
236 }
237
238 /**
239 * Constructs a RtfFont with the given font name, font size, font style and
240 * color.
241 *
242 * @param fontName The font name to use
243 * @param size the font size to use
244 * @param style The font style to use
245 * @param color The font color to use
246 */
247 public RtfFont(String fontName, float size, int style, Color color) {
248 super(Font.UNDEFINED, size, style, color);
249 this.fontName = fontName;
250 }
251
252 /**
253 * Constructs a RtfFont with the given font name, font size, font style, color
254 * and charset. This can be used when generating non latin-1 text.
255 *
256 * @param fontName The font name to use
257 * @param size the font size to use
258 * @param style The font style to use
259 * @param color The font color to use
260 * @param charset The charset of the font content
261 */
262 public RtfFont(String fontName, float size, int style, Color color, int charset) {
263 this(fontName, size, style, color);
264 this.charset = charset;
265 }
266
267 /**
268 * Special constructor for the default font
269 *
270 * @param doc The RtfDocument this font appears in
271 * @param fontNumber The id of this font
272 */
273 protected RtfFont(RtfDocument doc, int fontNumber) {
274 this.document = doc;
275 this.fontNumber = fontNumber;
276 color = new RtfColor(doc, 0, 0, 0);
277 }
278
279 /**
280 * Constructs a RtfFont from a com.lowagie.text.Font
281 * @param doc The RtfDocument this font appears in
282 * @param font The Font to use as a base
283 */
284 public RtfFont(RtfDocument doc, Font font) {
285 this.document = doc;
286 if(font != null) {
287 if(font instanceof RtfFont) {
288 this.fontName = ((RtfFont) font).getFontName();
289 this.charset = ((RtfFont) font).getCharset();
290 } else {
291 setToDefaultFamily(font.getFamilyname());
292 }
293 if(font.getBaseFont() != null) {
294 String[][] fontNames = font.getBaseFont().getFullFontName();
295 for(int i = 0; i < fontNames.length; i++) {
296 if(fontNames[i][2].equals("0")) {
297 this.fontName = fontNames[i][3];
298 break;
299 } else if(fontNames[i][2].equals("1033") || fontNames[i][2].equals("")) {
300 this.fontName = fontNames[i][3];
301 }
302 }
303 }
304
305 setSize(font.getSize());
306 setStyle(font.getStyle());
307 setColor(font.getColor());
308 }
309
310 if(this.fontName.equalsIgnoreCase("unknown")) {
311 return;
312 }
313
314 if(document != null) {
315 setRtfDocument(document);
316 }
317 }
318
319 /**
320 * Writes the font definition
321 */
322 public void writeDefinition(final OutputStream result) throws IOException
323 {
324 result.write(FONT_FAMILY);
325 result.write(FONT_CHARSET);
326 result.write(intToByteArray(charset));
327 result.write(DELIMITER);
328 document.filterSpecialChar(result, fontName, true, false);
329 }
330
331 /**
332 * Writes the font beginning
333 *
334 * @param result The <code>OutputStream</code> to write to.
335 * @throws IOException On i/o errors.
336 */
337 public void writeBegin(final OutputStream result) throws IOException {
338 if(this.fontNumber != Font.UNDEFINED) {
339 result.write(RtfFontList.FONT_NUMBER);
340 result.write(intToByteArray(fontNumber));
341 }
342 if(this.fontSize != Font.UNDEFINED) {
343 result.write(FONT_SIZE);
344 result.write(intToByteArray(fontSize * 2));
345 }
346 if(this.fontStyle != UNDEFINED) {
347 if((fontStyle & STYLE_BOLD) == STYLE_BOLD) {
348 result.write(FONT_BOLD);
349 }
350 if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) {
351 result.write(FONT_ITALIC);
352 }
353 if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) {
354 result.write(FONT_UNDERLINE);
355 }
356 if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) {
357 result.write(FONT_STRIKETHROUGH);
358 }
359 if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) {
360 result.write(FONT_HIDDEN);
361 }
362 if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) {
363 result.write(FONT_DOUBLE_STRIKETHROUGH);
364 result.write(intToByteArray(1));
365 }
366 if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) {
367 result.write(FONT_SHADOW);
368 }
369 if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) {
370 result.write(FONT_OUTLINE);
371 }
372 if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) {
373 result.write(FONT_EMBOSSED);
374 }
375 if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) {
376 result.write(FONT_ENGRAVED);
377 }
378 }
379 if(color != null) {
380 color.writeBegin(result);
381 }
382 }
383
384 /**
385 * Write the font end
386 *
387 * @param result The <code>OutputStream</code> to write to.
388 * @throws IOException On i/o errors.
389 */
390 public void writeEnd(final OutputStream result) throws IOException{
391 if(this.fontStyle != UNDEFINED) {
392 if((fontStyle & STYLE_BOLD) == STYLE_BOLD) {
393 result.write(FONT_BOLD);
394 result.write(intToByteArray(0));
395 }
396 if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) {
397 result.write(FONT_ITALIC);
398 result.write(intToByteArray(0));
399 }
400 if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) {
401 result.write(FONT_UNDERLINE);
402 result.write(intToByteArray(0));
403 }
404 if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) {
405 result.write(FONT_STRIKETHROUGH);
406 result.write(intToByteArray(0));
407 }
408 if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) {
409 result.write(FONT_HIDDEN);
410 result.write(intToByteArray(0));
411 }
412 if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) {
413 result.write(FONT_DOUBLE_STRIKETHROUGH);
414 result.write(intToByteArray(0));
415 }
416 if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) {
417 result.write(FONT_SHADOW);
418 result.write(intToByteArray(0));
419 }
420 if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) {
421 result.write(FONT_OUTLINE);
422 result.write(intToByteArray(0));
423 }
424 if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) {
425 result.write(FONT_EMBOSSED);
426 result.write(intToByteArray(0));
427 }
428 if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) {
429 result.write(FONT_ENGRAVED);
430 result.write(intToByteArray(0));
431 }
432 }
433 }
434
435 /**
436 * unused
437 */
438 public void writeContent(OutputStream out) throws IOException
439 {
440 }
441
442 /**
443 * Tests for equality of RtfFonts. RtfFonts are equal if their fontName,
444 * fontSize, fontStyle and fontSuperSubscript are equal
445 *
446 * @param obj The RtfFont to compare with this RtfFont
447 * @return <code>True</code> if the RtfFonts are equal, <code>false</code> otherwise
448 */
449 public boolean equals(Object obj) {
450 if(!(obj instanceof RtfFont)) {
451 return false;
452 }
453 RtfFont font = (RtfFont) obj;
454 boolean result = true;
455 result = result & this.fontName.equals(font.getFontName());
456
457 return result;
458 }
459
460 /**
461 * Returns the hash code of this RtfFont. The hash code is the hash code of the
462 * string containing the font name + font size + "-" + the font style + "-" + the
463 * font super/supscript value.
464 *
465 * @return The hash code of this RtfFont
466 */
467 public int hashCode() {
468 return (this.fontName + this.fontSize + "-" + this.fontStyle).hashCode();
469 }
470
471 /**
472 * Gets the font name of this RtfFont
473 *
474 * @return The font name
475 */
476 public String getFontName() {
477 return this.fontName;
478 }
479
480 /**
481 * Sets the font name of this RtfFont.
482 *
483 * @param fontName The font name to use
484 */
485 protected void setFontName(String fontName) {
486 this.fontName = fontName;
487 if(document != null) {
488 this.fontNumber = document.getDocumentHeader().getFontNumber(this);
489 }
490 }
491
492 /**
493 * @see com.lowagie.text.Font#getFamilyname()
494 */
495 public String getFamilyname() {
496 return this.fontName;
497 }
498
499 /**
500 * @see com.lowagie.text.Font#setFamily(String)
501 */
502 public void setFamily(String family){
503 super.setFamily(family);
504 setToDefaultFamily(family);
505 }
506
507 /**
508 * Sets the correct font name from the family name.
509 *
510 * @param familyname The family name to set the name to.
511 */
512 private void setToDefaultFamily(String familyname){
513 switch (Font.getFamilyIndex(familyname)) {
514 case Font.COURIER:
515 this.fontName = "Courier";
516 break;
517 case Font.HELVETICA:
518 this.fontName = "Arial";
519 break;
520 case Font.SYMBOL:
521 this.fontName = "Symbol";
522 this.charset = 2;
523 break;
524 case Font.TIMES_ROMAN:
525 this.fontName = "Times New Roman";
526 break;
527 case Font.ZAPFDINGBATS:
528 this.fontName = "Windings";
529 break;
530 default:
531 this.fontName = familyname;
532 }
533 }
534
535 /**
536 * Gets the font size of this RtfFont
537 *
538 * @return The font size
539 */
540 public int getFontSize() {
541 return this.fontSize;
542 }
543
544 /**
545 * @see com.lowagie.text.Font#setSize(float)
546 */
547 public void setSize(float size){
548 super.setSize(size);
549 this.fontSize = (int) getSize();
550 }
551
552 /**
553 * Gets the font style of this RtfFont
554 *
555 * @return The font style
556 */
557 public int getFontStyle() {
558 return this.fontStyle;
559 }
560
561 /**
562 * @see com.lowagie.text.Font#setStyle(int)
563 */
564 public void setStyle(int style){
565 super.setStyle(style);
566 this.fontStyle = getStyle();
567 }
568
569 /**
570 * @see com.lowagie.text.Font#setStyle(String)
571 */
572 public void setStyle(String style) {
573 super.setStyle(style);
574 fontStyle = getStyle();
575 }
576
577 /**
578 * Gets the charset used for constructing this RtfFont.
579 *
580 * @return The charset of this RtfFont.
581 */
582 public int getCharset() {
583 return charset;
584 }
585
586 /**
587 * Sets the charset used for constructing this RtfFont.
588 *
589 * @param charset The charset to use.
590 */
591 public void setCharset(int charset) {
592 this.charset = charset;
593 }
594
595 /**
596 * Gets the font number of this RtfFont
597 *
598 * @return The font number
599 */
600 public int getFontNumber() {
601 return fontNumber;
602 }
603
604 /**
605 * Sets the RtfDocument this RtfFont belongs to
606 *
607 * @param doc The RtfDocument to use
608 */
609 public void setRtfDocument(RtfDocument doc) {
610 this.document = doc;
611 if(document != null) {
612 this.fontNumber = document.getDocumentHeader().getFontNumber(this);
613 }
614 if(this.color != null) {
615 this.color.setRtfDocument(this.document);
616 }
617 }
618
619 /**
620 * Unused
621 * @param inTable
622 */
623 public void setInTable(boolean inTable) {
624 }
625
626 /**
627 * Unused
628 * @param inHeader
629 */
630 public void setInHeader(boolean inHeader) {
631 }
632
633 /**
634 * @see com.lowagie.text.Font#setColor(Color)
635 */
636 public void setColor(Color color) {
637 super.setColor(color);
638 if(color != null) {
639 this.color = new RtfColor(document, color);
640 } else {
641 this.color = null;
642 }
643 }
644
645 /**
646 * @see com.lowagie.text.Font#setColor(int, int, int)
647 */
648 public void setColor(int red, int green, int blue) {
649 super.setColor(red,green,blue);
650 this.color = new RtfColor(document, red, green, blue);
651 }
652
653 /**
654 * Transforms an integer into its String representation and then returns the bytes
655 * of that string.
656 *
657 * @param i The integer to convert
658 * @return A byte array representing the integer
659 */
660 protected byte[] intToByteArray(int i) {
661 return Integer.toString(i).getBytes();
662 }
663
664 /**
665 * Replaces the attributes that are equal to <VAR>null</VAR> with
666 * the attributes of a given font.
667 *
668 * @param font The surrounding font
669 * @return A RtfFont
670 */
671 public Font difference(Font font) {
672 String dFamilyname = font.getFamilyname();
673 if(dFamilyname == null || dFamilyname.trim().equals("") || dFamilyname.trim().equalsIgnoreCase("unknown")) {
674 dFamilyname = this.fontName;
675 }
676
677 float dSize = font.getSize();
678 if(dSize == Font.UNDEFINED) {
679 dSize = this.getSize();
680 }
681
682 int dStyle = Font.UNDEFINED;
683 if(this.getStyle() != Font.UNDEFINED && font.getStyle() != Font.UNDEFINED) {
684 dStyle = this.getStyle() | font.getStyle();
685 } else if(this.getStyle() != Font.UNDEFINED) {
686 dStyle = this.getStyle();
687 } else if(font.getStyle() != Font.UNDEFINED) {
688 dStyle = font.getStyle();
689 }
690
691 Color dColor = font.getColor();
692 if(dColor == null) {
693 dColor = this.getColor();
694 }
695
696 int dCharset = this.charset;
697 if(font instanceof RtfFont) {
698 dCharset = ((RtfFont) font).getCharset();
699 }
700
701 return new RtfFont(dFamilyname, dSize, dStyle, dColor, dCharset);
702 }
703
704 /**
705 * The <code>RtfFont</code> is never a standard font.
706 *
707 * @since 2.1.0
708 */
709 public boolean isStandardFont() {
710 return false;
711 }
712
713 /**
714 * Compares this <code>RtfFont</code> to either a {@link com.lowagie.text.Font} or
715 * an <code>RtfFont</code>.
716 *
717 * @since 2.1.0
718 */
719 public int compareTo(Object object) {
720 if (object == null) {
721 return -1;
722 }
723 if(object instanceof RtfFont) {
724 if(this.getFontName().compareTo(((RtfFont) object).getFontName()) != 0) {
725 return 1;
726 } else {
727 return super.compareTo(object);
728 }
729 } else if(object instanceof Font) {
730 return super.compareTo(object);
731 } else {
732 return -3;
733 }
734 }
735 }