diff options
author | Jens-Heiner Rechtien <hr@openoffice.org> | 2005-10-24 16:56:51 +0000 |
---|---|---|
committer | Jens-Heiner Rechtien <hr@openoffice.org> | 2005-10-24 16:56:51 +0000 |
commit | 2818481a4c7fc515806ac826cf031e98faa81a08 (patch) | |
tree | 9affad5e93eb1f1f63efb660444d441732bcaa5c /xmerge/source/wordsmith | |
parent | 7324b0936efc98c3a3203cf46644429f315ff853 (diff) |
INTEGRATION: CWS lo8 (1.1.2); FILE ADDED
2005/06/08 16:30:36 lo 1.1.2.1: restructuring of project and fix for #i44847#
Diffstat (limited to 'xmerge/source/wordsmith')
3 files changed, 852 insertions, 0 deletions
diff --git a/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WseHeader.java b/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WseHeader.java new file mode 100644 index 000000000000..43ae5fdb7977 --- /dev/null +++ b/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WseHeader.java @@ -0,0 +1,173 @@ +/************************************************************************ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +package org.openoffice.xmerge.converter.xml.sxw.wordsmith; + +import java.io.IOException; +import java.io.DataOutputStream; +import java.io.ByteArrayOutputStream; + +/** + * This class represents a WordSmith document header. + * + * @author David Proulx + */ +class WseHeader extends Wse { + + private int nParagraphs = 0; + private int nAtoms = 0; + private int nChars = 0; + private int miscSize = 0; + + /** + * Constructor for use when going from DOM to WordSmith. + * + * @param nPara The number of paragraphs. + * @param nAtoms The number of atoms. + * @param nChars The number of characters. + * @param ft The font table. + * @param ct The color table. + */ + public WseHeader(int nPara, int nAtoms, int nChars, WseFontTable ft, + WseColorTable ct) { + nParagraphs = nPara; + this.nAtoms = nAtoms; + this.nChars = nChars; + if (ft != null) miscSize += ft.getByteCount(); + if (ct != null) miscSize += ct.getByteCount(); + } + + + /** + * Constructor for use when going from WordSmith to DOM. + * + * @param dataArray <code>byte</code> array. + * @param i Index. + */ + public WseHeader(byte dataArray[], int i) { + // DJP: write this! + } + + /** + * Return true if <code>dataArray[startIndex]</code> is the start + * of a document header. + * + * @param dataArray <code>byte</code> array. + * @param startIndex The index. + * + * @return true if <code>dataArray[startIndex]</code> is the start + * of a document header, false otherwise. + */ + static boolean isValid(byte dataArray[], int startIndex) { + return ((dataArray[startIndex] == 2) + && (dataArray[startIndex + 1] == 4)); + } + + + /** + * Compute and return the index of the first <code>byte</code> + * following this element. It is assumed that the element + * starting at <code>dataArray[startIndex]</code> is valid. + * + * @param dataArray <code>byte</code> array. + * @param startIndex The start index. + * + * @return The first <code>byte</code> following this element. + */ + static int computeNewIndex(byte dataArray[], int startIndex) { + return startIndex + 18; + } + + + /** + * Return the total number of bytes needed to represent this. + * + * @return The total number of bytes needed to represent this. + */ + int getByteCount() { + return 18; + } + + + /** + * Return a <code>byte</code> array representing this element. + * + * @return A <code>byte</code> array representing this element. + */ + byte[] getBytes() { + DataOutputStream os; // Used for storing the data + ByteArrayOutputStream bs = null; // Used for storing the data + + try { + bs = new ByteArrayOutputStream(); + os = new DataOutputStream(bs); + os.write(2); // binary doc indicator + os.write(4); // binary header indicator + + os.writeInt(nParagraphs); + os.writeInt(nAtoms); + os.writeInt(nChars); + os.writeInt(miscSize); + + } catch (IOException e) { + e.printStackTrace(); + } + + if (bs != null) { + return bs.toByteArray(); + } else return null; + } +} + diff --git a/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WsePara.java b/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WsePara.java new file mode 100644 index 000000000000..6e76ec7ff82f --- /dev/null +++ b/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WsePara.java @@ -0,0 +1,327 @@ +/************************************************************************ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +package org.openoffice.xmerge.converter.xml.sxw.wordsmith; + +import java.io.IOException; + +import org.w3c.dom.NodeList; +import org.w3c.dom.Node; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Element; + +import org.openoffice.xmerge.Document; +import org.openoffice.xmerge.ConverterCapabilities; +import org.openoffice.xmerge.converter.xml.OfficeDocument; +import org.openoffice.xmerge.converter.xml.sxw.SxwDocument; +import org.openoffice.xmerge.converter.xml.*; + + +/** + * This class represents a paragraph in a WordSmith document. + * (A paragraph is "5" followed by 12 bytes of attributes.) + * + * @author David Proulx + */ +class WsePara extends Wse { + + private byte spaceBefore = 0; + private byte spaceAfter = 0; + private byte leftIndent = 0; + private byte firstIndent = 0; + private byte rightIndent = 0; + private byte misc = 0; + private byte style = 0; + private byte lineSpace = 0; + private byte outline = 0; + private byte reserved = 0; + + private static final byte LS_EXACTLY = (byte)0xC0; + private static final byte LS_ATLEAST = (byte)0x80; + private static final byte LS_MULTIPLE = (byte)0x40; + private static final byte LS_VALUEMASK = (byte)0x3F; + + private static final byte ALIGN_RIGHT = (byte)2; + private static final byte ALIGN_LEFT = (byte)0; + private static final byte ALIGN_CENTER = (byte)1; + private static final byte ALIGN_JUST = (byte)3; + + private StyleCatalog sc = null; + + + /** + * Constructor for use when going from DOM to WordSmith. + * + * @param p The paragraph style. + * @param sc The <code>StyleCatalog</code>. + */ + public WsePara(ParaStyle p, StyleCatalog sc) { + this.sc = sc; + ParaStyle ps = (ParaStyle)p.getResolved(); + + if (ps.isAttributeSet(ParaStyle.MARGIN_LEFT)) { + double temp = ps.getAttribute(ParaStyle.MARGIN_LEFT) * 1.6 / 100; + leftIndent = (byte) temp; + if ((temp - leftIndent) > 0.5) leftIndent++; + } + + if (ps.isAttributeSet(ParaStyle.MARGIN_RIGHT)) { + double temp = ps.getAttribute(ParaStyle.MARGIN_RIGHT) * 1.6 / 100; + rightIndent = (byte) temp; + if ((temp - rightIndent) > 0.5) rightIndent++; + } + + if (ps.isAttributeSet(ParaStyle.TEXT_INDENT)) { + double temp = ps.getAttribute(ParaStyle.TEXT_INDENT) * 1.6 / 100; + firstIndent = (byte) temp; + if ((temp - firstIndent) > 0.5) firstIndent++; + } + + if (ps.isAttributeSet(ParaStyle.MARGIN_TOP)) { + double temp = ps.getAttribute(ParaStyle.MARGIN_TOP) * 1.6 / 100; + spaceBefore = (byte) temp; + if ((temp - spaceBefore) > 0.5) spaceBefore++; + } + + if (ps.isAttributeSet(ParaStyle.MARGIN_BOTTOM)) { + double temp = ps.getAttribute(ParaStyle.MARGIN_BOTTOM) * 1.6 / 100; + spaceAfter = (byte) temp; + if ((temp - spaceAfter) > 0.5) spaceAfter++; + } + + if (ps.isAttributeSet(ParaStyle.LINE_HEIGHT)) { + int lh = ps.getAttribute(ParaStyle.LINE_HEIGHT); + if ((lh & ~ParaStyle.LH_VALUEMASK) == 0) + lineSpace = (byte)(LS_MULTIPLE | (lh * 2)); + else if ((lh & ParaStyle.LH_PCT) != 0) { + lh = (lh & ParaStyle.LH_VALUEMASK) / 100; + lineSpace = (byte)(LS_MULTIPLE | (lh * 2)); + } + // DJP: handle other cases.... + } + + if (ps.isAttributeSet(ParaStyle.TEXT_ALIGN)) { + + int val = ps.getAttribute(ParaStyle.TEXT_ALIGN); + + switch (val) { + case ParaStyle.ALIGN_RIGHT: + misc = ALIGN_RIGHT; + break; + case ParaStyle.ALIGN_LEFT: + misc = ALIGN_LEFT; + break; + case ParaStyle.ALIGN_CENTER: + misc = ALIGN_CENTER; + break; + case ParaStyle.ALIGN_JUST: + misc = ALIGN_JUST; + break; + } + } + + } + + + /** + * Constructor for use when going from WordSmith to DOM. + * Assumes <code>dataArray[startIndex]</code> is the first + * <code>byte</code> of a valid WordSmith paragraph descriptor. + * + * @param dataArray <code>byte</code> array. + * @param startIndex The start index. + */ + public WsePara(byte dataArray[], int startIndex) { + spaceBefore = dataArray[startIndex + 1]; + spaceAfter = dataArray[startIndex + 2]; + leftIndent = dataArray[startIndex + 3]; + firstIndent = dataArray[startIndex + 4]; + rightIndent = dataArray[startIndex + 5]; + misc = dataArray[startIndex + 6]; + style = dataArray[startIndex + 7]; + lineSpace = dataArray[startIndex + 8]; + outline = dataArray[startIndex + 9]; + } + + + /** + * Compute the index of the first <code>byte</code> following the + * paragraph descriptor, assuming that + * <code>dataArray[startIndex]</code> is the beginning of a valid + * paragraph descriptor. + * + * @param dataArray <code>byte</code> array. + * @param startIndex The start index. + * + * @return The index of the first <code>byte</code> following the + * paragraph description. + */ + static int computeNewIndex(byte dataArray[], int startIndex) { + return startIndex + 13; + } + + + /** + * Return true if <code>dataArray[startIndex]</code> is the start + * of a valid paragraph descriptor. + * + * @param dataArray <code>byte</code> array. + * @param startIndex The start index. + * + * @return true if <code>dataArray[startIndex]</code> is the start + * of a valid paragraph descriptor, false otherwise. + */ + static boolean isValid(byte dataArray[], int startIndex) { + return (dataArray[startIndex] == 5); + } + + /** + * Return the number of bytes needed to represent this paragraph. + * + * @return The number of bytes needed to represent this paragraph. + */ + int getByteCount() { + return 13; + } + + /** + * Return an <code>byte</code> array representing this paragraph. + * + * @return An <code>byte</code> array representing this paragraph. + */ + byte[] getBytes() { + byte b[] = new byte[13]; + + b[0] = 5; + b[1] = spaceBefore; + b[2] = spaceAfter; + b[3] = leftIndent; + b[4] = firstIndent; + b[5] = rightIndent; + b[6] = misc; + b[7] = style; + b[8] = lineSpace; + b[9] = outline; + b[10] = reserved; + b[11] = 0; + b[12] = 0; + + return b; + } + + /** + * Return a <code>ParaStyle</code> that reflects the formatting of + * this run. + * + * @return A <code>ParaStyle</code> that reflects the formatting + * of this run. + */ + ParaStyle makeStyle() { + /* Csaba: Commented out the LINE_HEIGHT syle, because there was no + incoming data for that style. It was resulting a zero line + height in the xml document, ie. the doc looked empty. + */ + int attrs[] = { ParaStyle.MARGIN_LEFT, ParaStyle.MARGIN_RIGHT, + ParaStyle.TEXT_INDENT, //ParaStyle.LINE_HEIGHT, + ParaStyle.MARGIN_TOP, ParaStyle.MARGIN_BOTTOM, + ParaStyle.TEXT_ALIGN }; + String values[] = new String[attrs.length]; + double temp; + + temp = leftIndent / 1.6; + values[0] = (new Double(temp)).toString() + "mm"; + + temp = rightIndent / 1.6; + values[1] = (new Double(temp)).toString() + "mm"; + + temp = firstIndent / 1.6; + values[2] = (new Double(temp)).toString() + "mm"; + +/* if ((lineSpace & LS_MULTIPLE) != 0) { + temp = (lineSpace & LS_VALUEMASK) / 2; + temp *= 100; + values[3] = (new Double(temp)).toString() + "%"; + } else { + values[3] = (new Double(temp)).toString() + "mm"; + // DJP: handle other cases + } +*/ + temp = spaceBefore / 1.6; +// values[4] = (new Double(temp)).toString() + "mm"; + values[3] = (new Double(temp)).toString() + "mm"; + + temp = spaceAfter / 1.6; +// values[5] = (new Double(temp)).toString() + "mm"; + values[4] = (new Double(temp)).toString() + "mm"; + + switch (misc) { + +// case ALIGN_RIGHT: values[6] = "right"; break; +// case ALIGN_LEFT: values[6] = "left"; break; +// case ALIGN_CENTER:values[6] = "center"; break; +// case ALIGN_JUST: values[6] = "justified"; break; + + case ALIGN_RIGHT: values[5] = "right"; break; + case ALIGN_LEFT: values[5] = "left"; break; + case ALIGN_CENTER:values[5] = "center"; break; + case ALIGN_JUST: values[5] = "justified"; break; + } + ParaStyle x = new ParaStyle(null, "paragraph", null, attrs, + values, sc); + + return x; + } +} + diff --git a/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WseTextRun.java b/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WseTextRun.java new file mode 100644 index 000000000000..083e1ff13b16 --- /dev/null +++ b/xmerge/source/wordsmith/java/org/openoffice/xmerge/converter/xml/sxw/wordsmith/WseTextRun.java @@ -0,0 +1,352 @@ +/************************************************************************ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +package org.openoffice.xmerge.converter.xml.sxw.wordsmith; + +import org.openoffice.xmerge.converter.xml.TextStyle; +import org.openoffice.xmerge.converter.xml.StyleCatalog; +import java.awt.Color; + +/** + * <p>This class represents a text run (aka text atom) in a WordSmith + * document.</p> + * + * <p>WordSmith represents a text run as follows:</p> + * + * <p><ul><li> + * 1 byte Value of "1", indicating beginning of a text atom + * </li><li> + * 2 bytes Length of text (does not include attributes, this length field, + * etc) + * </li><li> + * 1 byte Font index - Index in the font table of font to be used + * </li><li> + * 1 byte Font size (DJP: get details of representation) + * </li><li> + * 1 byte Color index - Index in the color table of font color to be used + * </li><li> + * 1 byte Modifiers - bit flags for bold, italic, etc + * </li><li> + * n bytes Text - the actual text + * </li></ul></p> + * + * @author David Proulx + */ +class WseTextRun extends Wse { + + /** Font specifier. This is an index into the font table. */ + private byte fontIndex = 0; + private String fontName = null; + + /** Size of the font. */ + private byte fontSize = 0; + + /** + * Color of the font. This is an index into the color table. + * High nibble is background color index, low nibble is font color + * index. + */ + private byte colorIndex = 0; + + /** + * Reference to color table for color lookups. + */ + private WseColorTable ct; + + /** + * The modifiers for the text run. (Mostly) Bitwise flags. The "_TOKEN" + * values are not yet implemented in this converter. They may not even + * be implemented in WordSmith yet. + */ + private byte modifiers = 0; + final public static int BOLD = 0x01; + final public static int ITALIC = 0x02; + final public static int UNDERLINE = 0x04; + final public static int STRIKETHRU = 0x08; + final public static int SUPERSCRIPT = 0x10; + final public static int SUBSCRIPT = 0x20; + final public static int LINK = 0x40; + final public static int CUSTOM_TOKEN = 0x80; + final public static int IMAGE_TOKEN = 0x80; + final public static int BOOKMARK_TOKEN = 0x81; + final public static int ANNOTATION_TOKEN = 0x82; + final public static int LINK_TOKEN = 0x83; + + /** The actual text. */ + private String text; + + StyleCatalog sc; + + + /** + * Constructor for use when going from DOM to WordSmith. + * + * @param txt The text. + * @param t The text style. + * @param sc The <code>StyleCatalog</code>. + * @param ft The font table. + * @param ct The color Table. + */ + public WseTextRun(String txt, TextStyle t, StyleCatalog sc, + WseFontTable ft, WseColorTable ct) { + + this.sc = sc; + this.ct = ct; + + TextStyle ts = (TextStyle)t.getResolved(); + + if (ts.isSet(TextStyle.BOLD) && ts.getAttribute(TextStyle.BOLD)) + modifiers |= BOLD; + if (ts.isSet(TextStyle.ITALIC) && ts.getAttribute(TextStyle.ITALIC)) + modifiers |= ITALIC; + if (ts.isSet(TextStyle.UNDERLINE) && ts.getAttribute(TextStyle.UNDERLINE)) + modifiers |= UNDERLINE; + if (ts.isSet(TextStyle.STRIKETHRU) && ts.getAttribute(TextStyle.STRIKETHRU)) + modifiers |= STRIKETHRU; + if (ts.isSet(TextStyle.SUPERSCRIPT) && ts.getAttribute(TextStyle.SUPERSCRIPT)) + modifiers |= SUPERSCRIPT; + if (ts.isSet(TextStyle.SUBSCRIPT) && ts.getAttribute(TextStyle.SUBSCRIPT)) + modifiers |= SUBSCRIPT; + + fontSize = (byte)(ts.getFontSize() * 2); + fontName = ts.getFontName(); + fontIndex = (byte)ft.getFontIndex(fontName); + if (fontIndex == -1) { + ft.add(fontName); + fontIndex = (byte)ft.getFontIndex(fontName); + } + + // Figure out the color index. + Color c = t.getFontColor(); + if (c == null) + c = Color.black; + colorIndex = (byte)ct.findColor(c, true); + c = t.getBackgroundColor(); + if (c == null) + c = Color.white; + colorIndex |= (byte)(ct.findColor(c, false) << 4); + + text = txt; + } + + + /** + * Standard constructor for use when going from WordSmith to DOM. + * + * @param dataArray <code>byte</code> array. + * @param startIndex The start index. + * @param ft The font table. + * @param ct The color table. + */ + public WseTextRun(byte dataArray[], int startIndex, WseFontTable ft, + WseColorTable ct) { + + this.ct = ct; + + startIndex++; // Skip the leading "1" + + int textLen = ((dataArray[startIndex] << 8) + | (dataArray[startIndex+1] & 0xFF)); + startIndex += 2; + + fontIndex = dataArray[startIndex++]; + if (ft != null) + fontName = ft.getFontName(fontIndex); + + fontSize = dataArray[startIndex++]; + + colorIndex = dataArray[startIndex++]; + modifiers = dataArray[startIndex++]; + + text = new String(dataArray, startIndex, textLen); + startIndex += textLen; // skip the text + } + + + /** + * Given a <code>byte</code> sequence, assumed to be a text run, + * compute the index of the first byte past the text run. + * + * @param dataArray <code>byte</code> array. + * @param startIndex The start index + * + * @return The index of the first <code>byte</code> past the + * text run. + */ + public static int computeNewIndex(byte dataArray[], int startIndex) { + + startIndex++; // Skip the leading "1" + + int textLen = ((dataArray[startIndex] << 8) + | (dataArray[startIndex+1] & 0xFF)); + startIndex += 2; + + startIndex += 4; // skip attributes + // text = new String(dataArray, startIndex, textLen); + startIndex += textLen; // skip the text + return startIndex; + } + + + /** + * Return true if the sequence starting at + * <code>dataArray[startIndex]</code> is a valid text run. + * + * @param dataArray <code>byte</code> array. + * @param startIndex The start index. + * + * @return true if the sequence starting at + * <code>dataArray[startIndex]</code> is a valid + * text run, false otherwise. + */ + public static boolean isValid(byte dataArray[], int startIndex) { + return (dataArray[startIndex] == 1); + } + + /** + * Return the number of bytes needed to represent this text run. + * + * @return The number of bytes needed to represent this text run. + */ + int getByteCount() { + return text.length() + 7; + } + + + /** + * Return an <code>byte</code> array representing this text run. + * + * @return An <code>byte</code> array representing this text run. + */ + byte[] getBytes() { + short textLen = (short)text.length(); + byte b[] = new byte[textLen + 7]; + b[0] = 1; + b[1] = (byte)(textLen >> 8); + b[2] = (byte)(textLen & 0xFF); + b[3] = fontIndex; + b[4] = fontSize; + b[5] = colorIndex; + b[6] = modifiers; + byte[] txtBytes = text.getBytes(); + System.arraycopy(txtBytes, 0, b, 7, textLen); + return b; + } + + + /** + * Return the text of this run. + * + * @return The text of this run. + */ + public String getText() { + return text; + } + + + /** + * Return a <code>TextStyle</code> that reflects the formatting + * of this run. + * + * @return A <code>TextStyle</code> that reflects the formatting + * of this run. + */ + public TextStyle makeStyle() { + int mod = 0; + if ((modifiers & BOLD) != 0) mod |= TextStyle.BOLD; + if ((modifiers & ITALIC) != 0) mod |= TextStyle.ITALIC; + if ((modifiers & UNDERLINE) != 0) mod |= TextStyle.UNDERLINE; + if ((modifiers & STRIKETHRU) != 0) + mod |= TextStyle.STRIKETHRU; + if ((modifiers & SUPERSCRIPT) != 0) mod |= TextStyle.SUPERSCRIPT; + if ((modifiers & SUBSCRIPT) != 0) mod |= TextStyle.SUBSCRIPT; + + int mask = TextStyle.BOLD | TextStyle.ITALIC + | TextStyle.UNDERLINE + | TextStyle.STRIKETHRU | TextStyle.SUPERSCRIPT + | TextStyle.SUBSCRIPT; + + TextStyle x = new TextStyle(null, "text", null, mask, + mod, (int)(fontSize/2), fontName, sc); + + // If color table is available, set the colors. + if (ct != null) { + Color fc = ct.getColor(colorIndex & 0xF, true); + Color bc = ct.getColor(colorIndex >> 4, false); + x.setColors(fc, bc); + } + + return x; + } + + + /** + * Display debug information. + */ + public void dump() { + System.out.print("TEXT RUN: fontIndex = " + fontIndex + + " fontsize = " + fontSize + + " colorIndex = " + colorIndex + + " "); + if ((modifiers & BOLD) != 0) System.out.print("BOLD,"); + if ((modifiers & ITALIC) != 0) System.out.print("ITALIC,"); + if ((modifiers & UNDERLINE) != 0) System.out.print("UNDERLINE,"); + if ((modifiers & STRIKETHRU) != 0) System.out.print("STRIKETHRU,"); + if ((modifiers & SUPERSCRIPT) != 0) System.out.print("SUPERSCRIPT,"); + if ((modifiers & SUBSCRIPT) != 0) System.out.print("SUBSCRIPT,"); + System.out.println("\n" + text); + } +} + |