File: RtfFont.java

package info (click to toggle)
libitext1-java 1.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 7,636 kB
  • sloc: java: 86,865; xml: 396; makefile: 15
file content (681 lines) | stat: -rw-r--r-- 22,406 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
 * $Id: RtfFont.java,v 1.23 2006/02/09 17:11:31 hallm Exp $
 * $Name:  $
 *
 * Copyright 2001, 2002, 2003, 2004 by Mark Hall
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */

package com.lowagie.text.rtf.style;

import com.lowagie.text.rtf.RtfExtendedElement;
import com.lowagie.text.rtf.document.RtfDocument;
import com.lowagie.text.Font;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * The RtfFont class stores one font for an rtf document. It extends Font,
 * so can be set as a font, to allow adding of fonts with arbitrary names.
 * BaseFont fontname handling contributed by Craig Fleming. Various fixes
 * Renaud Michel, Werner Daehn.
 *
 * Version: $Id: RtfFont.java,v 1.23 2006/02/09 17:11:31 hallm Exp $
 * @author Mark Hall (mhall@edu.uni-klu.ac.at)
 * @author Craig Fleming (rythos@rhana.dhs.org)
 * @author Renaud Michel (r.michel@immedia.be)
 * @author Werner Daehn (Werner.Daehn@BusinessObjects.com)
 */
public class RtfFont extends Font implements RtfExtendedElement {
    /**
     * Constant for the font family to use ("froman")
     */
    private static final byte[] FONT_FAMILY = "\\froman".getBytes();
    /**
     * Constant for the charset
     */
    private static final byte[] FONT_CHARSET = "\\fcharset".getBytes();
    /**
     * Constant for the font size
     */
    public static final byte[] FONT_SIZE = "\\fs".getBytes();
    /**
     * Constant for the bold flag
     */
    private static final byte[] FONT_BOLD = "\\b".getBytes();
    /**
     * Constant for the italic flag
     */
    private static final byte[] FONT_ITALIC = "\\i".getBytes();
    /**
     * Constant for the underline flag
     */
    private static final byte[] FONT_UNDERLINE = "\\ul".getBytes();
    /**
     * Constant for the strikethrough flag
     */
    private static final byte[] FONT_STRIKETHROUGH = "\\strike".getBytes();
    /**
     * Constant for the double strikethrough flag
     */
    private static final byte[] FONT_DOUBLE_STRIKETHROUGH = "\\striked".getBytes();
    /**
     * Constant for the shadow flag
     */
    private static final byte[] FONT_SHADOW = "\\shad".getBytes();
    /**
     * Constant for the outline flag
     */
    private static final byte[] FONT_OUTLINE = "\\outl".getBytes();
    /**
     * Constant for the embossed flag
     */
    private static final byte[] FONT_EMBOSSED = "\\embo".getBytes();
    /**
     * Constant for the engraved flag
     */
    private static final byte[] FONT_ENGRAVED = "\\impr".getBytes();
    /**
     * Constant for hidden text flag
     */
    private static final byte[] FONT_HIDDEN = "\\v".getBytes();
    
    /**
     * Constant for a plain font
     */
    public static final int STYLE_NONE = 0;
    /**
     * Constant for a bold font
     */
    public static final int STYLE_BOLD = 1;
    /**
     * Constant for an italic font
     */
    public static final int STYLE_ITALIC = 2;
    /**
     * Constant for an underlined font
     */
    public static final int STYLE_UNDERLINE = 4;
    /**
     * Constant for a strikethrough font
     */
    public static final int STYLE_STRIKETHROUGH = 8;
    /**
     * Constant for a double strikethrough font
     */
    public static final int STYLE_DOUBLE_STRIKETHROUGH = 16;
    /**
     * Constant for a shadowed font
     */
    public static final int STYLE_SHADOW = 32;
    /**
     * Constant for an outlined font
     */
    public static final int STYLE_OUTLINE = 64;
    /**
     * Constant for an embossed font
     */
    public static final int STYLE_EMBOSSED = 128;
    /**
     * Constant for an engraved font
     */
    public static final int STYLE_ENGRAVED = 256;
    /**
     * Constant for a font that hides the actual text.
     */
    public static final int STYLE_HIDDEN = 512;

    /**
     * The font name. Defaults to "Times New Roman"
     */
    private String fontName = "Times New Roman";
    /**
     * The font size. Defaults to 10
     */
    private int fontSize = 10;
    /**
     * The font style. Defaults to STYLE_NONE
     */
    private int fontStyle = STYLE_NONE;
    /**
     * The number of this font
     */
    private int fontNumber = 0;
    /**
     * The colour of this font
     */
    private RtfColor color = null;
    /**
     * The character set to use for this font
     */
    private int charset = 0;
    /**
     * The RtfDocument this RtfFont belongs to.
     */
    protected RtfDocument document = null;
    
    /**
     * Constructs a RtfFont with the given font name and all other properties
     * at their default values.
     * 
     * @param fontName The font name to use
     */
    public RtfFont(String fontName) {
        super(Font.UNDEFINED, Font.UNDEFINED, Font.UNDEFINED, null);
        this.fontName = fontName;
    }
    
    /**
     * Constructs a RtfFont with the given font name and font size and all other
     * properties at their default values.
     * 
     * @param fontName The font name to use
     * @param size The font size to use
     */
    public RtfFont(String fontName, float size) {
        super(Font.UNDEFINED, size, Font.UNDEFINED, null);
        this.fontName = fontName;
    }
    
    /**
     * Constructs a RtfFont with the given font name, font size and font style and the
     * default color.
     * 
     * @param fontName The font name to use
     * @param size The font size to use
     * @param style The font style to use
     */
    public RtfFont(String fontName, float size, int style) {
        super(Font.UNDEFINED, size, style, null);
        this.fontName = fontName;
    }
    
    /**
     * Constructs a RtfFont with the given font name, font size, font style and
     * color.
     * 
     * @param fontName The font name to use
     * @param size the font size to use
     * @param style The font style to use
     * @param color The font color to use
     */
    public RtfFont(String fontName, float size, int style, Color color) {
        super(Font.UNDEFINED, size, style, color);
        this.fontName = fontName;
    }
    
    /**
     * Special constructor for the default font
     *
     * @param doc The RtfDocument this font appears in
     * @param fontNumber The id of this font
     */
    protected RtfFont(RtfDocument doc, int fontNumber) {
        this.document = doc;
        this.fontNumber = fontNumber;
        color = new RtfColor(doc, 0, 0, 0);
    }

    /**
     * Constructs a RtfFont from a com.lowagie.text.Font
     * @param doc The RtfDocument this font appears in
     * @param font The Font to use as a base
     */
    public RtfFont(RtfDocument doc, Font font) {
        this.document = doc;
        if(font != null) {
            if(font instanceof RtfFont) {
                this.fontName = ((RtfFont) font).getFontName();
            } else {
                setToDefaultFamily(font.getFamilyname());
            }
            if(font.getBaseFont() != null) {
                String[][] fontNames = font.getBaseFont().getFullFontName();
                for(int i = 0; i < fontNames.length; i++) {
                    if(fontNames[i][2].equals("0")) {
                        this.fontName = fontNames[i][3];
                        break;
                    } else if(fontNames[i][2].equals("1033") || fontNames[i][2].equals("")) {
                        this.fontName = fontNames[i][3];
                    }
                }
            }
            
            setSize(font.size());
            setStyle(font.style());
            setColor(font.color());
        }

        if(this.fontName.equalsIgnoreCase("unknown")) {
            return;
        }

        if(document != null) {
            setRtfDocument(document);
        }
    }

    /**
     * Writes the font definition
     *
     * @return A byte array with the font definition
     */
    public byte[] writeDefinition() {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            result.write(FONT_FAMILY);
            result.write(FONT_CHARSET);
            result.write(intToByteArray(charset));
            result.write(DELIMITER);
            result.write(document.filterSpecialChar(fontName, true, false).getBytes());
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
        return result.toByteArray();
    }
    
    /**
     * Writes the font beginning
     *
     * @return A byte array with the font start data
     */
    public byte[] writeBegin() {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            if(this.fontNumber != Font.UNDEFINED) {
                result.write(RtfFontList.FONT_NUMBER);
                result.write(intToByteArray(fontNumber));
            }
            if(this.fontSize != Font.UNDEFINED) {
                result.write(FONT_SIZE);
                result.write(intToByteArray(fontSize * 2));
            }
            if(this.fontStyle != UNDEFINED) {
                if((fontStyle & STYLE_BOLD) == STYLE_BOLD) {
                    result.write(FONT_BOLD);
                }
                if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) {
                    result.write(FONT_ITALIC);
                }
                if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) {
                    result.write(FONT_UNDERLINE);
                }
                if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) {
                    result.write(FONT_STRIKETHROUGH);
                }
                if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) {
                    result.write(FONT_HIDDEN);
                }
                if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) {
                    result.write(FONT_DOUBLE_STRIKETHROUGH);
                    result.write(intToByteArray(1));
                }
                if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) {
                    result.write(FONT_SHADOW);
                }
                if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) {
                    result.write(FONT_OUTLINE);
                }
                if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) {
                    result.write(FONT_EMBOSSED);
                }
                if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) {
                    result.write(FONT_ENGRAVED);
                }
            }
            if(color != null) {
                result.write(color.writeBegin());
            }
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
        return result.toByteArray();
    }
    
    /**
     * Write the font end
     *
     * @return A byte array with the end of font data
     */
    public byte[] writeEnd() {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            if(this.fontStyle != UNDEFINED) {
                if((fontStyle & STYLE_BOLD) == STYLE_BOLD) {
                    result.write(FONT_BOLD);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) {
                    result.write(FONT_ITALIC);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) {
                    result.write(FONT_UNDERLINE);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) {
                    result.write(FONT_STRIKETHROUGH);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) {
                    result.write(FONT_HIDDEN);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) {
                    result.write(FONT_DOUBLE_STRIKETHROUGH);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) {
                    result.write(FONT_SHADOW);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) {
                    result.write(FONT_OUTLINE);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) {
                    result.write(FONT_EMBOSSED);
                    result.write(intToByteArray(0));
                }
                if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) {
                    result.write(FONT_ENGRAVED);
                    result.write(intToByteArray(0));
                }
            }
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
        return result.toByteArray();
    }

    /**
     * Unused
     * @return an empty byte array
     */
    public byte[] write() {
        return new byte[0];
    }
    
    /**
     * Tests for equality of RtfFonts. RtfFonts are equal if their fontName,
     * fontSize, fontStyle and fontSuperSubscript are equal
     * 
     * @param obj The RtfFont to compare with this RtfFont
     * @return <code>True</code> if the RtfFonts are equal, <code>false</code> otherwise
     */
    public boolean equals(Object obj) {
        if(!(obj instanceof RtfFont)) {
            return false;
        }
        RtfFont font = (RtfFont) obj;
        boolean result = true;
        result = result & this.fontName.equals(font.getFontName());

        return result;
    }

    /**
     * Returns the hash code of this RtfFont. The hash code is the hash code of the
     * string containing the font name + font size + "-" + the font style + "-" + the
     * font super/supscript value.
     * 
     * @return The hash code of this RtfFont
     */
    public int hashCode() {
        return (this.fontName + this.fontSize + "-" + this.fontStyle).hashCode();
    }
    
    /**
     * Gets the font name of this RtfFont
     * 
     * @return The font name
     */
    public String getFontName() {
        return this.fontName;
    }

    /**
     * Sets the font name of this RtfFont.
     * 
     * @param fontName The font name to use 
     */
    protected void setFontName(String fontName) {
        this.fontName = fontName;
        if(document != null) {
            this.fontNumber = document.getDocumentHeader().getFontNumber(this);
        }
    }
    
    /**
     * @see com.lowagie.text.Font#getFamilyname()
     */
    public String getFamilyname() {
        return this.fontName;
    }
    
    /**
     * @see com.lowagie.text.Font#setFamily(String)
     */
    public void setFamily(String family){
        super.setFamily(family);
        setToDefaultFamily(family);
    }
    
    /**
     * Sets the correct font name from the family name.
     * 
     * @param familyname The family name to set the name to.
     */
    private void setToDefaultFamily(String familyname){
        switch (Font.getFamilyIndex(familyname)) {
            case Font.COURIER:
                this.fontName = "Courier";
                break;
            case Font.HELVETICA:
                this.fontName = "Arial";
                break;
            case Font.SYMBOL:
                this.fontName = "Symbol";
                this.charset = 2;
                break;
            case Font.TIMES_ROMAN:
                this.fontName = "Times New Roman";
                break;
            case Font.ZAPFDINGBATS:
                this.fontName = "Windings";
                break;
            default:
                this.fontName = familyname;
        }
    }
    
    /**
     * Gets the font size of this RtfFont
     * 
     * @return The font size
     */
    public int getFontSize() {
        return this.fontSize;
    }
    
    /**
     * @see com.lowagie.text.Font#setSize(float)
     */
    public void setSize(float size){
        super.setSize(size);
        this.fontSize = (int) size();
    }

    /**
     * Gets the font style of this RtfFont
     * 
     * @return The font style
     */
    public int getFontStyle() {
        return this.fontStyle;
    }
    
    /**
     * @see com.lowagie.text.Font#setStyle(int)
     */
    public void setStyle(int style){
        super.setStyle(style);
        this.fontStyle = style();
    }
    
    /**
     * @see com.lowagie.text.Font#setStyle(String)
     */
    public void setStyle(String style) {
        super.setStyle(style);
        fontStyle = style();
    }

    /**
     * Gets the font number of this RtfFont
     * 
     * @return The font number
     */
    public int getFontNumber() {
        return fontNumber;
    }

    /**
     * Sets the RtfDocument this RtfFont belongs to
     * 
     * @param doc The RtfDocument to use
     */
    public void setRtfDocument(RtfDocument doc) {
        this.document = doc;
        if(document != null) {
            this.fontNumber = document.getDocumentHeader().getFontNumber(this);
        }
        if(this.color != null) {
            this.color.setRtfDocument(this.document);
        }
    }

    /**
     * Unused
     * @param inTable
     */
    public void setInTable(boolean inTable) {
    }
    
    /**
     * Unused
     * @param inHeader
     */
    public void setInHeader(boolean inHeader) {
    }
    
    /**
     * @see com.lowagie.text.Font#setColor(Color)
     */
    public void setColor(Color color) {
        super.setColor(color);
        if(color != null) {
            this.color = new RtfColor(document, color);
        } else {
            this.color = null;
        }
    }
    
    /**
     * @see com.lowagie.text.Font#setColor(int, int, int)
     */
    public void setColor(int red, int green, int blue) {
        super.setColor(red,green,blue);
        this.color = new RtfColor(document, red, green, blue);
    }

    /**
     * Transforms an integer into its String representation and then returns the bytes
     * of that string.
     *
     * @param i The integer to convert
     * @return A byte array representing the integer
     */
    protected byte[] intToByteArray(int i) {
        return Integer.toString(i).getBytes();
    }

    /**
     * Replaces the attributes that are equal to <VAR>null</VAR> with
     * the attributes of a given font.
     *
     * @param font The surrounding font
     * @return A RtfFont
     */
    public Font difference(Font font) {
        String dFamilyname = font.getFamilyname();
        if(dFamilyname == null || dFamilyname.trim().equals("") || dFamilyname.trim().equalsIgnoreCase("unknown")) {
            dFamilyname = this.fontName;
        }

        float dSize = font.size();
        if(dSize == Font.UNDEFINED) {
            dSize = this.size();
        }

        int dStyle = Font.UNDEFINED;
        if(this.style() != Font.UNDEFINED && font.style() != Font.UNDEFINED) {
            dStyle = this.style() | font.style();
        } else if(this.style() != Font.UNDEFINED) {
            dStyle = this.style();
        } else if(font.style() != Font.UNDEFINED) {
            dStyle = font.style();
        }

        Color dColor = font.color();
        if(dColor == null) {
            dColor = this.color();
        }

        return new RtfFont(dFamilyname, dSize, dStyle, dColor);
    }
}