jdk/src/share/classes/javax/swing/text/rtf/RTFGenerator.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 438 2ae294e4518c
child 1299 027d966d5658
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.rtf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Generates an RTF output stream (java.io.OutputStream) from rich text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * (handed off through a series of LTTextAcceptor calls).  Can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * generate RTF from any object which knows how to write to a text acceptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * (e.g., LTAttributedText and LTRTFFilter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>Note that this is a lossy conversion since RTF's model of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * text does not exactly correspond with LightText's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see LTAttributedText
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see LTRTFFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see LTTextAcceptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see java.io.OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class RTFGenerator extends Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* These dictionaries map Colors, font names, or Style objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
       to Integers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    Dictionary colorTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    int colorCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    Dictionary fontTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    int fontCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    Dictionary styleTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int styleCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* where all the text is going */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    OutputStream outputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    boolean afterKeyword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    MutableAttributeSet outputAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /* the value of the last \\ucN keyword emitted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int unicodeCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /* for efficiency's sake (ha) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Segment workingSegment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int[] outputConversion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /** The default color, used for text without an explicit color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *  attribute. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static public final Color defaultRTFColor = Color.black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static public final float defaultFontSize = 12f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static public final String defaultFontFamily = "Helvetica";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* constants so we can avoid allocating objects in inner loops */
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
    86
    final static private Object MagicToken;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* An array of character-keyword pairs. This could be done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
       as a dictionary (and lookup would be quicker), but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
       would require allocating an object for every character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
       written (slow!). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static class CharacterKeywordPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      { public char character; public String keyword; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static protected CharacterKeywordPair[] textKeywords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        MagicToken = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Dictionary textKeywordDictionary = RTFReader.textKeywords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Enumeration keys = textKeywordDictionary.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Vector tempPairs = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        while(keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            CharacterKeywordPair pair = new CharacterKeywordPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            pair.keyword = (String)keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            pair.character = ((String)textKeywordDictionary.get(pair.keyword)).charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            tempPairs.addElement(pair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        textKeywords = new CharacterKeywordPair[tempPairs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        tempPairs.copyInto(textKeywords);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static final char[] hexdigits = { '0', '1', '2', '3', '4', '5', '6', '7',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
static public void writeDocument(Document d, OutputStream to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    RTFGenerator gen = new RTFGenerator(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    Element root = d.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    gen.examineElement(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    gen.writeRTFHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    gen.writeDocumentProperties(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /* TODO this assumes a particular element structure; is there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
       a way to iterate more generically ? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int max = root.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    for(int idx = 0; idx < max; idx++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        gen.writeParagraphElement(root.getElement(idx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    gen.writeRTFTrailer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
public RTFGenerator(OutputStream to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    colorTable = new Hashtable();
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   137
    colorTable.put(defaultRTFColor, Integer.valueOf(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    colorCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    fontTable = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    fontCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    styleTable = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /* TODO: put default style in style table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    styleCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    workingSegment = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    outputStream = to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    unicodeCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
public void examineElement(Element el)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    AttributeSet a = el.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    String fontName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    Object foregroundColor, backgroundColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    tallyStyles(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /* TODO: default color must be color 0! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        foregroundColor = StyleConstants.getForeground(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (foregroundColor != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            colorTable.get(foregroundColor) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            colorTable.put(foregroundColor, new Integer(colorCount));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            colorCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        backgroundColor = a.getAttribute(StyleConstants.Background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (backgroundColor != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            colorTable.get(backgroundColor) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            colorTable.put(backgroundColor, new Integer(colorCount));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            colorCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        fontName = StyleConstants.getFontFamily(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (fontName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            fontName = defaultFontFamily;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (fontName != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            fontTable.get(fontName) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            fontTable.put(fontName, new Integer(fontCount));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            fontCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    int el_count = el.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    for(int el_idx = 0; el_idx < el_count; el_idx ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        examineElement(el.getElement(el_idx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
private void tallyStyles(AttributeSet a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    while (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (a instanceof Style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Integer aNum = (Integer)styleTable.get(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (aNum == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                styleCount = styleCount + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                aNum = new Integer(styleCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                styleTable.put(a, aNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        a = a.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
private Style findStyle(AttributeSet a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    while(a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (a instanceof Style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            Object aNum = styleTable.get(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (aNum != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return (Style)a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        a = a.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
private Integer findStyleNumber(AttributeSet a, String domain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    while(a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (a instanceof Style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            Integer aNum = (Integer)styleTable.get(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (aNum != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (domain == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    domain.equals(a.getAttribute(Constants.StyleType)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    return aNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        a = a.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
static private Object attrDiff(MutableAttributeSet oldAttrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                               AttributeSet newAttrs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                               Object key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                               Object dfl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    Object oldValue, newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    oldValue = oldAttrs.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    newValue = newAttrs.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    if (newValue == oldValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    if (newValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        oldAttrs.removeAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (dfl != null && !dfl.equals(oldValue))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return dfl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if (oldValue == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        !equalArraysOK(oldValue, newValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        oldAttrs.addAttribute(key, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
static private boolean equalArraysOK(Object a, Object b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    Object[] aa, bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    if (a == b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    if (a == null || b == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    if (a.equals(b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    if (!(a.getClass().isArray() && b.getClass().isArray()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    aa = (Object[])a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    bb = (Object[])b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    if (aa.length != bb.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    int l = aa.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    for(i = 0; i < l; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (!equalArraysOK(aa[i], bb[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
/* Writes a line break to the output file, for ease in debugging */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
public void writeLineBreak()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    writeRawString("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    afterKeyword = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
public void writeRTFHeader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /* TODO: Should the writer attempt to examine the text it's writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
       and pick a character set which will most compactly represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
       document? (currently the writer always uses the ansi character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
       set, which is roughly ISO-8859 Latin-1, and uses Unicode escapes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
       for all other characters. However Unicode is a relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
       recent addition to RTF, and not all readers will understand it.) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    writeBegingroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    writeControlWord("rtf", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    writeControlWord("ansi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    outputConversion = outputConversionForName("ansi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    writeLineBreak();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /* write font table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    String[] sortedFontTable = new String[fontCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    Enumeration fonts = fontTable.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    String font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    while(fonts.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        font = (String)fonts.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        Integer num = (Integer)(fontTable.get(font));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        sortedFontTable[num.intValue()] = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    writeBegingroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    writeControlWord("fonttbl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    for(index = 0; index < fontCount; index ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        writeControlWord("f", index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        writeControlWord("fnil");  /* TODO: supply correct font style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        writeText(sortedFontTable[index]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        writeText(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    writeEndgroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    writeLineBreak();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /* write color table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    if (colorCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        Color[] sortedColorTable = new Color[colorCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Enumeration colors = colorTable.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        Color color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        while(colors.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            color = (Color)colors.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Integer num = (Integer)(colorTable.get(color));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            sortedColorTable[num.intValue()] = color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        writeBegingroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        writeControlWord("colortbl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for(index = 0; index < colorCount; index ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            color = sortedColorTable[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                writeControlWord("red", color.getRed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                writeControlWord("green", color.getGreen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                writeControlWord("blue", color.getBlue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            writeRawString(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        writeEndgroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        writeLineBreak();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /* write the style sheet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    if (styleCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        writeBegingroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        writeControlWord("stylesheet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        Enumeration styles = styleTable.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        while(styles.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            Style style = (Style)styles.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            int styleNumber = ((Integer)styleTable.get(style)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            writeBegingroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            String styleType = (String)style.getAttribute(Constants.StyleType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (styleType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                styleType = Constants.STParagraph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (styleType.equals(Constants.STCharacter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                writeControlWord("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                writeControlWord("cs", styleNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            } else if(styleType.equals(Constants.STSection)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                writeControlWord("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                writeControlWord("ds", styleNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                writeControlWord("s", styleNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            AttributeSet basis = style.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            MutableAttributeSet goat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (basis == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                goat = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                goat = new SimpleAttributeSet(basis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            updateSectionAttributes(goat, style, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            updateParagraphAttributes(goat, style, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            updateCharacterAttributes(goat, style, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            basis = style.getResolveParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (basis != null && basis instanceof Style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                Integer basedOn = (Integer)styleTable.get(basis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (basedOn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    writeControlWord("sbasedon", basedOn.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            Style nextStyle = (Style)style.getAttribute(Constants.StyleNext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (nextStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                Integer nextNum = (Integer)styleTable.get(nextStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                if (nextNum != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    writeControlWord("snext", nextNum.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            Boolean hidden = (Boolean)style.getAttribute(Constants.StyleHidden);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (hidden != null && hidden.booleanValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                writeControlWord("shidden");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            Boolean additive = (Boolean)style.getAttribute(Constants.StyleAdditive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (additive != null && additive.booleanValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                writeControlWord("additive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            writeText(style.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            writeText(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            writeEndgroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        writeEndgroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        writeLineBreak();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    outputAttributes = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
void writeDocumentProperties(Document doc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /* Write the document properties */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    boolean wroteSomething = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    for(i = 0; i < RTFAttributes.attributes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        RTFAttribute attr = RTFAttributes.attributes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (attr.domain() != RTFAttribute.D_DOCUMENT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        Object prop = doc.getProperty(attr.swingName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        boolean ok = attr.writeValue(prop, this, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            wroteSomething = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    if (wroteSomething)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        writeLineBreak();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
public void writeRTFTrailer()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    writeEndgroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    writeLineBreak();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
protected void checkNumericControlWord(MutableAttributeSet currentAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                       AttributeSet newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                       Object attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                       String controlWord,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                       float dflt, float scale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    Object parm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    if ((parm = attrDiff(currentAttributes, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                         attrName, MagicToken)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        float targ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (parm == MagicToken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            targ = dflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            targ = ((Number)parm).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        writeControlWord(controlWord, Math.round(targ * scale));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
protected void checkControlWord(MutableAttributeSet currentAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                AttributeSet newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                RTFAttribute word)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    Object parm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    if ((parm = attrDiff(currentAttributes, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                         word.swingName(), MagicToken)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (parm == MagicToken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            parm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        word.writeValue(parm, this, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
protected void checkControlWords(MutableAttributeSet currentAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                                 AttributeSet newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                 RTFAttribute words[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                                 int domain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    int wordIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    int wordCount = words.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    for(wordIndex = 0; wordIndex < wordCount; wordIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        RTFAttribute attr = words[wordIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (attr.domain() == domain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            checkControlWord(currentAttributes, newAttributes, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
void updateSectionAttributes(MutableAttributeSet current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                             AttributeSet newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                             boolean emitStyleChanges)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    if (emitStyleChanges) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        Object oldStyle = current.getAttribute("sectionStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        Object newStyle = findStyleNumber(newAttributes, Constants.STSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (oldStyle != newStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                resetSectionAttributes(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if (newStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                writeControlWord("ds", ((Integer)newStyle).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                current.addAttribute("sectionStyle", newStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                current.removeAttribute("sectionStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    checkControlWords(current, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                      RTFAttributes.attributes, RTFAttribute.D_SECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
protected void resetSectionAttributes(MutableAttributeSet currentAttributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    writeControlWord("sectd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    int wordIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    int wordCount = RTFAttributes.attributes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    for(wordIndex = 0; wordIndex < wordCount; wordIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        RTFAttribute attr = RTFAttributes.attributes[wordIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (attr.domain() == RTFAttribute.D_SECTION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            attr.setDefault(currentAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    currentAttributes.removeAttribute("sectionStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
void updateParagraphAttributes(MutableAttributeSet current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                               AttributeSet newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                               boolean emitStyleChanges)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    Object parm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    Object oldStyle, newStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /* The only way to get rid of tabs or styles is with the \pard keyword,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
       emitted by resetParagraphAttributes(). Ideally we should avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
       emitting \pard if the new paragraph's tabs are a superset of the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
       paragraph's tabs. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    if (emitStyleChanges) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        oldStyle = current.getAttribute("paragraphStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        newStyle = findStyleNumber(newAttributes, Constants.STParagraph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (oldStyle != newStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                resetParagraphAttributes(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                oldStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        oldStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        newStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    Object oldTabs = current.getAttribute(Constants.Tabs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    Object newTabs = newAttributes.getAttribute(Constants.Tabs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    if (oldTabs != newTabs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (oldTabs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            resetParagraphAttributes(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            oldTabs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            oldStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    if (oldStyle != newStyle && newStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        writeControlWord("s", ((Integer)newStyle).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        current.addAttribute("paragraphStyle", newStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    checkControlWords(current, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                      RTFAttributes.attributes, RTFAttribute.D_PARAGRAPH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    if (oldTabs != newTabs && newTabs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        TabStop tabs[] = (TabStop[])newTabs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        for(index = 0; index < tabs.length; index ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            TabStop tab = tabs[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            switch (tab.getAlignment()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
              case TabStop.ALIGN_LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
              case TabStop.ALIGN_BAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
              case TabStop.ALIGN_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                writeControlWord("tqr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
              case TabStop.ALIGN_CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                writeControlWord("tqc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
              case TabStop.ALIGN_DECIMAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                writeControlWord("tqdec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            switch (tab.getLeader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
              case TabStop.LEAD_NONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
              case TabStop.LEAD_DOTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                writeControlWord("tldot");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
              case TabStop.LEAD_HYPHENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                writeControlWord("tlhyph");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
              case TabStop.LEAD_UNDERLINE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                writeControlWord("tlul");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
              case TabStop.LEAD_THICKLINE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                writeControlWord("tlth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
              case TabStop.LEAD_EQUALS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                writeControlWord("tleq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            int twips = Math.round(20f * tab.getPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (tab.getAlignment() == TabStop.ALIGN_BAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                writeControlWord("tb", twips);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                writeControlWord("tx", twips);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        current.addAttribute(Constants.Tabs, tabs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
public void writeParagraphElement(Element el)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    updateParagraphAttributes(outputAttributes, el.getAttributes(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    int sub_count = el.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    for(int idx = 0; idx < sub_count; idx ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        writeTextElement(el.getElement(idx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    writeControlWord("par");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    writeLineBreak();  /* makes the raw file more readable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
/* debugging. TODO: remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
private static String tabdump(Object tso)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    String buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    if (tso == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return "[none]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    TabStop[] ts = (TabStop[])tso;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    buf = "[";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    for(i = 0; i < ts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        buf = buf + ts[i].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if ((i+1) < ts.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            buf = buf + ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    return buf + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
protected void resetParagraphAttributes(MutableAttributeSet currentAttributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    writeControlWord("pard");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   688
    currentAttributes.addAttribute(StyleConstants.Alignment, Integer.valueOf(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    int wordIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    int wordCount = RTFAttributes.attributes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    for(wordIndex = 0; wordIndex < wordCount; wordIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        RTFAttribute attr = RTFAttributes.attributes[wordIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (attr.domain() == RTFAttribute.D_PARAGRAPH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            attr.setDefault(currentAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    currentAttributes.removeAttribute("paragraphStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    currentAttributes.removeAttribute(Constants.Tabs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
void updateCharacterAttributes(MutableAttributeSet current,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                               AttributeSet newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                               boolean updateStyleChanges)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    Object parm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    if (updateStyleChanges) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        Object oldStyle = current.getAttribute("characterStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        Object newStyle = findStyleNumber(newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                                          Constants.STCharacter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        if (oldStyle != newStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                resetCharacterAttributes(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            if (newStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                writeControlWord("cs", ((Integer)newStyle).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                current.addAttribute("characterStyle", newStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                current.removeAttribute("characterStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    if ((parm = attrDiff(current, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                         StyleConstants.FontFamily, null)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        Number fontNum = (Number)fontTable.get(parm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        writeControlWord("f", fontNum.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    checkNumericControlWord(current, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                            StyleConstants.FontSize, "fs",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                            defaultFontSize, 2f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    checkControlWords(current, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                      RTFAttributes.attributes, RTFAttribute.D_CHARACTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    checkNumericControlWord(current, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                            StyleConstants.LineSpacing, "sl",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                            0, 20f); /* TODO: sl wackiness */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    if ((parm = attrDiff(current, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                         StyleConstants.Background, MagicToken)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        int colorNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (parm == MagicToken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            colorNum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            colorNum = ((Number)colorTable.get(parm)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        writeControlWord("cb", colorNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    if ((parm = attrDiff(current, newAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                         StyleConstants.Foreground, null)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        int colorNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if (parm == MagicToken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            colorNum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            colorNum = ((Number)colorTable.get(parm)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        writeControlWord("cf", colorNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
protected void resetCharacterAttributes(MutableAttributeSet currentAttributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    writeControlWord("plain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    int wordIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    int wordCount = RTFAttributes.attributes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    for(wordIndex = 0; wordIndex < wordCount; wordIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        RTFAttribute attr = RTFAttributes.attributes[wordIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (attr.domain() == RTFAttribute.D_CHARACTER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            attr.setDefault(currentAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    StyleConstants.setFontFamily(currentAttributes, defaultFontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    currentAttributes.removeAttribute(StyleConstants.FontSize); /* =default */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    currentAttributes.removeAttribute(StyleConstants.Background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    currentAttributes.removeAttribute(StyleConstants.Foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    currentAttributes.removeAttribute(StyleConstants.LineSpacing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    currentAttributes.removeAttribute("characterStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
public void writeTextElement(Element el)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    updateCharacterAttributes(outputAttributes, el.getAttributes(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    if (el.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            el.getDocument().getText(el.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                     el.getEndOffset() - el.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                     this.workingSegment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            /* TODO is this the correct error to raise? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            ble.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            throw new InternalError(ble.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        writeText(this.workingSegment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        int sub_count = el.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        for(int idx = 0; idx < sub_count; idx ++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            writeTextElement(el.getElement(idx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
public void writeText(Segment s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    int pos, end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    char[] array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    pos = s.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    end = pos + s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    array = s.array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    for( ; pos < end; pos ++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        writeCharacter(array[pos]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
public void writeText(String s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    int pos, end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    end = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    for( ; pos < end; pos ++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        writeCharacter(s.charAt(pos));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
public void writeRawString(String str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    int strlen = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    for (int offset = 0; offset < strlen; offset ++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        outputStream.write((int)str.charAt(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
public void writeControlWord(String keyword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    outputStream.write('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    writeRawString(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    afterKeyword = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
public void writeControlWord(String keyword, int arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    outputStream.write('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    writeRawString(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    writeRawString(String.valueOf(arg)); /* TODO: correct in all cases? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    afterKeyword = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
public void writeBegingroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    outputStream.write('{');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    afterKeyword = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
public void writeEndgroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    outputStream.write('}');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    afterKeyword = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
public void writeCharacter(char ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /* Nonbreaking space is in most RTF encodings, but the keyword is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
       preferable; same goes for tabs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    if (ch == 0xA0) { /* nonbreaking space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        outputStream.write(0x5C);  /* backslash */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        outputStream.write(0x7E);  /* tilde */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        afterKeyword = false; /* non-alpha keywords are self-terminating */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    if (ch == 0x09) { /* horizontal tab */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        writeControlWord("tab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    if (ch == 10 || ch == 13) { /* newline / paragraph */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        /* ignore CRs, we'll write a paragraph element soon enough */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    int b = convertCharacter(outputConversion, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    if (b == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        /* Unicode characters which have corresponding RTF keywords */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        for(i = 0; i < textKeywords.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            if (textKeywords[i].character == ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                writeControlWord(textKeywords[i].keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        /* In some cases it would be reasonable to check to see if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
           glyph being written out is in the Symbol encoding, and if so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
           to switch to the Symbol font for this character. TODO. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        /* Currently all unrepresentable characters are written as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
           Unicode escapes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        String approximation = approximationForUnicode(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (approximation.length() != unicodeCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            unicodeCount = approximation.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            writeControlWord("uc", unicodeCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        writeControlWord("u", (int)ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        writeRawString(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        writeRawString(approximation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        afterKeyword = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    if (b > 127) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        int nybble;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        outputStream.write('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        outputStream.write('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        nybble = ( b & 0xF0 ) >>> 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        outputStream.write(hexdigits[nybble]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        nybble = ( b & 0x0F );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        outputStream.write(hexdigits[nybble]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        afterKeyword = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    switch (b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        outputStream.write(0x5C);  /* backslash */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        afterKeyword = false;  /* in a keyword, actually ... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        /* fall through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (afterKeyword) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            outputStream.write(0x20);  /* space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            afterKeyword = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        outputStream.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
String approximationForUnicode(char ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    /* TODO: Find reasonable approximations for all Unicode characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
       in all RTF code pages... heh, heh... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    return "?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
/** Takes a translation table (a 256-element array of characters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
 * and creates an output conversion table for use by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
 * convertCharacter(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /* Not very efficient at all. Could be changed to sort the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
       for binary search. TODO. (Even though this is inefficient however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
       writing RTF is still much faster than reading it.) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
static int[] outputConversionFromTranslationTable(char[] table)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    int[] conversion = new int[2 * table.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    for(index = 0; index < table.length; index ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        conversion[index * 2] = table[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        conversion[(index * 2) + 1] = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    return conversion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
static int[] outputConversionForName(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    char[] table = (char[])RTFReader.getCharacterSet(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    return outputConversionFromTranslationTable(table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
/** Takes a char and a conversion table (an int[] in the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
 * implementation, but conversion tables should be treated as an opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
 * type) and returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
 * corresponding byte value (as an int, since bytes are signed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /* Not very efficient. TODO. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
static protected int convertCharacter(int[] conversion, char ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
   int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
   for(index = 0; index < conversion.length; index += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
       if(conversion[index] == ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
           return conversion[index + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
   return 0;  /* 0 indicates an unrepresentable character */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
}