2002-10-06 18:23:27 +00:00
|
|
|
/*
|
|
|
|
The contents of this file are subject to the THDL Open Community License
|
|
|
|
Version 1.0 (the "License"); you may not use this file except in compliance
|
|
|
|
with the License. You may obtain a copy of the License on the THDL web site
|
|
|
|
(http://www.thdl.org/).
|
|
|
|
|
|
|
|
Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific terms governing rights and limitations under the
|
|
|
|
License.
|
|
|
|
|
|
|
|
The Initial Developer of this software is the Tibetan and Himalayan Digital
|
2003-06-29 21:31:48 +00:00
|
|
|
Library (THDL). Portions created by the THDL are Copyright 2001-2003 THDL.
|
2002-10-06 18:23:27 +00:00
|
|
|
All Rights Reserved.
|
|
|
|
|
|
|
|
Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.thdl.tib.text;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import javax.swing.*;
|
|
|
|
import javax.swing.text.*;
|
|
|
|
import javax.swing.text.rtf.RTFEditorKit;
|
|
|
|
import java.io.*;
|
2003-09-05 05:54:35 +00:00
|
|
|
import java.awt.Color;
|
2002-10-06 18:23:27 +00:00
|
|
|
|
|
|
|
import org.thdl.util.ThdlDebug;
|
2003-06-01 23:05:32 +00:00
|
|
|
import org.thdl.util.ThdlOptions;
|
2003-06-28 16:20:19 +00:00
|
|
|
import org.thdl.util.ThdlLazyException;
|
2002-10-06 18:23:27 +00:00
|
|
|
|
2003-05-18 14:14:47 +00:00
|
|
|
/** Represents a character meant to be rendered in a certain font.
|
|
|
|
* @author David Chandler
|
|
|
|
*/
|
|
|
|
class CharacterInAGivenFont {
|
|
|
|
private char character;
|
|
|
|
private String fontName;
|
|
|
|
public CharacterInAGivenFont(char ch, String font) {
|
|
|
|
character = ch;
|
|
|
|
fontName = font;
|
|
|
|
}
|
|
|
|
public CharacterInAGivenFont(String s, String font) {
|
|
|
|
if (s.length() != 1)
|
|
|
|
throw new Error("character in a given font was given a string "
|
|
|
|
+ s + " in a given font");
|
|
|
|
character = s.charAt(0);
|
|
|
|
fontName = font;
|
|
|
|
}
|
|
|
|
public boolean equals(Object x) {
|
|
|
|
return ((x instanceof CharacterInAGivenFont)
|
|
|
|
&& ((CharacterInAGivenFont)x).character == character
|
|
|
|
&& ((CharacterInAGivenFont)x).fontName.equals(fontName));
|
|
|
|
}
|
|
|
|
public int hashCode() {
|
|
|
|
return (int)character + fontName.hashCode();
|
|
|
|
}
|
|
|
|
public String toString() {
|
|
|
|
String characterRepresentation
|
2003-06-22 00:14:18 +00:00
|
|
|
= "'" + (('\'' == character)
|
|
|
|
? "\\'"
|
|
|
|
: new Character(character).toString())
|
|
|
|
+ "' [decimal " + (int)character + "]";
|
2003-05-18 14:14:47 +00:00
|
|
|
if ('\n' == character)
|
2003-06-22 00:14:18 +00:00
|
|
|
characterRepresentation
|
|
|
|
= "newline [decimal " + (int)character + "]";
|
2003-05-18 14:14:47 +00:00
|
|
|
if ('\r' == character)
|
2003-06-22 00:14:18 +00:00
|
|
|
characterRepresentation
|
|
|
|
= "carriage return" + (int)character + "]";
|
2003-05-18 14:14:47 +00:00
|
|
|
return characterRepresentation + " in the font "
|
|
|
|
+ ((null == fontName)
|
|
|
|
? "_ERROR_FINDING_FONT_"
|
|
|
|
: fontName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-06 18:23:27 +00:00
|
|
|
/**
|
2002-11-02 03:38:59 +00:00
|
|
|
* A TibetanDocument is a styled document that knows about Tibetan and
|
|
|
|
* will respect line breaks and the like. It allows you to insert
|
|
|
|
* Tibetan also.
|
2002-10-06 18:23:27 +00:00
|
|
|
* @author Edward Garrett, Tibetan and Himalayan Digital Library
|
2002-11-02 03:38:59 +00:00
|
|
|
* @version 1.0 */
|
2002-10-06 18:23:27 +00:00
|
|
|
public class TibetanDocument extends DefaultStyledDocument {
|
|
|
|
private int tibetanFontSize = 36;
|
|
|
|
|
2002-11-08 04:11:42 +00:00
|
|
|
/** Creates a new TibetanDocument with default styles. */
|
|
|
|
public TibetanDocument() { super(); }
|
2002-11-02 03:38:59 +00:00
|
|
|
|
|
|
|
/** Do not use this contructor. */
|
|
|
|
private TibetanDocument(AbstractDocument.Content c, StyleContext styles ) {
|
|
|
|
super(c, styles);
|
|
|
|
}
|
|
|
|
|
2002-10-06 18:23:27 +00:00
|
|
|
/**
|
|
|
|
* Creates a TibetanDocument.
|
|
|
|
* @param styles a StyleContext, which is simply passed on
|
|
|
|
* to DefaultStyledDocument's constructor
|
|
|
|
*/
|
|
|
|
public TibetanDocument(StyleContext styles) {
|
|
|
|
super(styles);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the point size used by default for Tibetan text.
|
|
|
|
* @param size the point size for Tibetan text
|
|
|
|
*/
|
|
|
|
public void setTibetanFontSize(int size) {
|
|
|
|
tibetanFontSize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the point size for Tibetan text.
|
|
|
|
* @return the point size used for Tibetan text
|
|
|
|
*/
|
|
|
|
public int getTibetanFontSize() {
|
|
|
|
return tibetanFontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes the document to an OutputStream as Rich Text Format (.rtf).
|
|
|
|
* @param out the OutputStream to write to
|
|
|
|
*/
|
|
|
|
public void writeRTFOutputStream(OutputStream out) throws IOException {
|
|
|
|
RTFEditorKit rtf = new RTFEditorKit();
|
|
|
|
|
|
|
|
try {
|
|
|
|
rtf.write(out, this, 0, getLength());
|
|
|
|
}
|
|
|
|
catch (BadLocationException ble) {
|
2003-06-22 22:10:58 +00:00
|
|
|
throw new Error("Cannot write RTF output; [0, " + getLength() + ") constitutes a bad position.");
|
2002-10-06 18:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-04 05:16:47 +00:00
|
|
|
/** Saves the contents of this RTF document as text on out. If
|
|
|
|
* any TM or TMW is in the document, the output will be
|
|
|
|
* garbage. */
|
|
|
|
public void writeTextOutput(BufferedWriter out) throws IOException {
|
|
|
|
// DLC FIXME: try getting blocks of text; I bet it's a huge
|
|
|
|
// speedup.
|
|
|
|
try {
|
|
|
|
for (int i = 0; i < getLength(); i++) {
|
|
|
|
out.write(getText(i,1));
|
|
|
|
}
|
|
|
|
} catch (BadLocationException e) {
|
|
|
|
throw new Error("can't happen");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-06 22:56:10 +00:00
|
|
|
private static boolean allowColors = false;
|
|
|
|
/** Enables the use of colors. */
|
2003-09-05 05:54:35 +00:00
|
|
|
public static void enableColors() { allowColors = true; }
|
2003-09-06 22:56:10 +00:00
|
|
|
/** Disables the use of colors. */
|
2003-09-05 05:54:35 +00:00
|
|
|
public static void disableColors() { allowColors = false; }
|
2003-09-06 22:56:10 +00:00
|
|
|
/** Returns true if and only if the use of colors is currently
|
|
|
|
* enabled. */
|
|
|
|
public static boolean colorsEnabled() { return allowColors; }
|
2003-09-05 05:54:35 +00:00
|
|
|
|
2003-09-04 05:16:47 +00:00
|
|
|
|
2002-10-06 18:23:27 +00:00
|
|
|
/**
|
|
|
|
* Inserts Tibetan text into the document. The font size is applied automatically,
|
|
|
|
* according to the current Tibetan font size.
|
|
|
|
* @param offset the position at which you want to insert text
|
|
|
|
* @param s the string you want to insert
|
|
|
|
* @param attr the attributes to apply, normally a particular TibetanMachineWeb font
|
|
|
|
* @see #setTibetanFontSize(int size)
|
|
|
|
*/
|
|
|
|
public void appendDuff(int offset, String s, MutableAttributeSet attr) {
|
2003-09-05 05:54:35 +00:00
|
|
|
appendDuff(tibetanFontSize, offset, s, attr, Color.BLACK);
|
2003-05-28 00:40:59 +00:00
|
|
|
}
|
|
|
|
|
2003-08-31 16:06:35 +00:00
|
|
|
/**
|
|
|
|
* Inserts Latin text into the document. The font size is applied
|
|
|
|
* automatically, according to the current Roman font size.
|
|
|
|
* @param offset the position at which you want to insert text
|
|
|
|
* @param s the string you want to insert
|
2003-09-06 22:56:10 +00:00
|
|
|
* @param color the color in which to insert, which is used if and only
|
|
|
|
* if {@link #colorsEnabled() colors are enabled}
|
2003-09-10 01:19:05 +00:00
|
|
|
* @see #setRomanAttributeSet(MutableAttributeSet)
|
2003-08-31 16:06:35 +00:00
|
|
|
*/
|
2003-09-05 05:54:35 +00:00
|
|
|
public void appendRoman(int offset, String s, Color color) throws BadLocationException {
|
2003-08-31 16:06:35 +00:00
|
|
|
ThdlDebug.verify(getRomanAttributeSet() != null);
|
2003-09-05 05:54:35 +00:00
|
|
|
if (allowColors)
|
|
|
|
StyleConstants.setForeground(getRomanAttributeSet(), color);
|
2003-08-31 16:06:35 +00:00
|
|
|
insertString(offset, s, getRomanAttributeSet());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts Latin text at the end of the document. The font size is
|
|
|
|
* applied automatically, according to the current Roman font size.
|
|
|
|
* @param s the string you want to insert
|
2003-09-06 22:56:10 +00:00
|
|
|
* @param color the color in which to insert, which is used if and only
|
|
|
|
* if {@link #colorsEnabled() colors are enabled}
|
2003-09-10 01:19:05 +00:00
|
|
|
* @see #setRomanAttributeSet(MutableAttributeSet)
|
2003-08-31 16:06:35 +00:00
|
|
|
*/
|
2003-09-05 05:54:35 +00:00
|
|
|
public void appendRoman(String s, Color color) {
|
2003-08-31 16:06:35 +00:00
|
|
|
try {
|
2003-09-05 05:54:35 +00:00
|
|
|
appendRoman(getLength(), s, color);
|
2003-08-31 16:06:35 +00:00
|
|
|
} catch (BadLocationException e) {
|
|
|
|
throw new Error("can't happen");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-05 05:54:35 +00:00
|
|
|
private void appendDuff(int fontSize, int offset, String s, MutableAttributeSet attr, Color color) {
|
2003-09-10 01:19:05 +00:00
|
|
|
try {
|
|
|
|
StyleConstants.setFontSize(attr, fontSize);
|
|
|
|
if (allowColors)
|
|
|
|
StyleConstants.setForeground(attr, color);
|
|
|
|
insertString(offset, s, attr);
|
|
|
|
}
|
|
|
|
catch (BadLocationException ble) {
|
2002-11-02 03:38:59 +00:00
|
|
|
ThdlDebug.noteIffyCode();
|
2003-09-10 01:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-06 18:23:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a stretch of TibetanMachineWeb data into the document.
|
|
|
|
* @param pos the position at which you want to insert text
|
2003-09-06 22:56:10 +00:00
|
|
|
* @param glyphs the array of Tibetan data you want to insert
|
|
|
|
* @param color the color in which to insert, which is used if and only
|
|
|
|
* if {@link #colorsEnabled() colors are enabled}
|
2002-10-06 18:23:27 +00:00
|
|
|
*/
|
2003-09-05 05:54:35 +00:00
|
|
|
public int insertDuff(int pos, DuffData[] glyphs, Color color) {
|
|
|
|
return insertDuff(tibetanFontSize, pos, glyphs, true, color);
|
|
|
|
}
|
|
|
|
|
2002-10-06 18:23:27 +00:00
|
|
|
public int insertDuff(int pos, DuffData[] glyphs) {
|
2003-09-05 05:54:35 +00:00
|
|
|
return insertDuff(tibetanFontSize, pos, glyphs, true, Color.BLACK);
|
2003-05-28 00:40:59 +00:00
|
|
|
}
|
|
|
|
|
2003-08-31 16:06:35 +00:00
|
|
|
/**
|
|
|
|
* Appends all DuffCodes in glyphs to the end of this document.
|
2003-09-06 22:56:10 +00:00
|
|
|
* @param glyphs the array of Tibetan data you want to insert
|
|
|
|
* @param color the color in which to insert, which is used if and only
|
|
|
|
* if {@link #colorsEnabled() colors are enabled}
|
2003-08-31 16:06:35 +00:00
|
|
|
*/
|
2003-09-05 05:54:35 +00:00
|
|
|
public void appendDuffCodes(DuffCode[] glyphs, Color color) {
|
2003-08-31 16:06:35 +00:00
|
|
|
// PERFORMANCE FIXME: this isn't so speedy, but it reuses
|
|
|
|
// existing code.
|
|
|
|
for (int i = 0; i < glyphs.length; i++) {
|
|
|
|
insertDuff(getLength(),
|
|
|
|
new DuffData[] { new DuffData(new String(new char[] { glyphs[i].getCharacter() }),
|
2003-09-05 05:54:35 +00:00
|
|
|
glyphs[i].getFontNum()) },
|
|
|
|
color);
|
2003-08-31 16:06:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-08 23:12:52 +00:00
|
|
|
|
|
|
|
/** Replacing can be more efficient than inserting and then
|
|
|
|
removing. This replaces the glyph at position pos with glyph,
|
|
|
|
which is interpreted as a TMW glyph if asTMW is true and a TM
|
|
|
|
glyph otherwise. The font size for the new glyph is
|
|
|
|
fontSize. */
|
|
|
|
private void replaceDuff(int fontSize, int pos,
|
|
|
|
DuffData glyph, boolean asTMW) {
|
2003-06-22 22:10:58 +00:00
|
|
|
replaceDuffs(fontSize, pos, pos + 1, glyph.text,
|
|
|
|
glyph.font, asTMW);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Replacing can be more efficient than inserting and then
|
|
|
|
removing. This replaces the glyphs at position [startOffset,
|
|
|
|
endOffset) with data, which is interpreted as TMW glyphs if
|
|
|
|
asTMW is true and as TM glyphs otherwise. The font size for
|
|
|
|
the new glyph is fontSize; the particular TM or TMW font is
|
|
|
|
specified by newFontIndex, which is one-based, not
|
|
|
|
zero-based. */
|
|
|
|
private void replaceDuffs(int fontSize, int startOffset,
|
|
|
|
int endOffset, String data,
|
|
|
|
int newFontIndex, boolean asTMW) {
|
2003-06-08 23:12:52 +00:00
|
|
|
MutableAttributeSet mas
|
|
|
|
= ((asTMW)
|
2003-06-22 22:10:58 +00:00
|
|
|
? TibetanMachineWeb.getAttributeSet(newFontIndex)
|
|
|
|
: TibetanMachineWeb.getAttributeSetTM(newFontIndex));
|
2003-06-08 23:12:52 +00:00
|
|
|
StyleConstants.setFontSize(mas, fontSize);
|
|
|
|
try {
|
2003-06-22 22:10:58 +00:00
|
|
|
replace(startOffset, endOffset - startOffset, data, mas);
|
2003-06-08 23:12:52 +00:00
|
|
|
} catch (BadLocationException ble) {
|
|
|
|
ThdlDebug.noteIffyCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-15 16:27:36 +00:00
|
|
|
/** Replacing can be more efficient than inserting and then
|
2003-06-22 22:10:58 +00:00
|
|
|
removing. This replaces the glyphs at position [startOffset,
|
|
|
|
endOffset) with unicode. The font size for the new unicode is
|
2003-06-28 16:20:19 +00:00
|
|
|
fontSize. Which particular Unicode font is used is specified
|
|
|
|
by unicodeFont.
|
2003-06-22 22:10:58 +00:00
|
|
|
|
2003-06-28 16:20:19 +00:00
|
|
|
@see TibetanMachineWeb#getUnicodeAttributeSet(String) */
|
2003-06-22 22:10:58 +00:00
|
|
|
private void replaceDuffsWithUnicode(int fontSize, int startOffset,
|
2003-06-23 01:58:11 +00:00
|
|
|
int endOffset, String unicode,
|
|
|
|
String unicodeFont) {
|
2003-06-15 16:27:36 +00:00
|
|
|
MutableAttributeSet mas
|
2003-06-23 01:58:11 +00:00
|
|
|
= TibetanMachineWeb.getUnicodeAttributeSet(unicodeFont);
|
2003-06-15 16:27:36 +00:00
|
|
|
StyleConstants.setFontSize(mas, fontSize);
|
|
|
|
try {
|
2003-06-22 22:10:58 +00:00
|
|
|
replace(startOffset, endOffset - startOffset, unicode, mas);
|
2003-06-15 16:27:36 +00:00
|
|
|
} catch (BadLocationException ble) {
|
2003-06-22 22:10:58 +00:00
|
|
|
throw new Error("TMW->Unicode failed because the following constitute a bad position: startOffset " + startOffset + ", endOffset " + endOffset);
|
2003-06-15 16:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-24 03:02:29 +00:00
|
|
|
// DLC NOW do I stick to these rules in TMW->Unicode mappings?
|
|
|
|
// Chris Fynn wrote:
|
|
|
|
//
|
|
|
|
// By normal Tibetan & Dzongkha spelling, writing, and input rules
|
|
|
|
// Tibetan script stacks should be entered and written: 1 headline
|
|
|
|
// consonant (0F40->0F6A), any subjoined consonant(s) (0F90->
|
|
|
|
// 0F9C), achung (0F71), shabkyu (0F74), any above headline
|
|
|
|
// vowel(s) (0F72 0F7A 0F7B 0F7C 0F7D and 0F80) ; any ngaro (0F7E,
|
|
|
|
// 0F82 and 0F83)
|
|
|
|
|
2003-06-15 16:27:36 +00:00
|
|
|
|
2003-05-31 23:21:29 +00:00
|
|
|
private int insertDuff(int fontSize, int pos, DuffData[] glyphs, boolean asTMW) {
|
2003-09-05 05:54:35 +00:00
|
|
|
return insertDuff(fontSize, pos, glyphs, asTMW, Color.BLACK);
|
|
|
|
}
|
|
|
|
private int insertDuff(int fontSize, int pos, DuffData[] glyphs, boolean asTMW, Color color) {
|
2002-10-06 18:23:27 +00:00
|
|
|
if (glyphs == null)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
MutableAttributeSet mas;
|
|
|
|
for (int i=0; i<glyphs.length; i++) {
|
2003-05-31 23:21:29 +00:00
|
|
|
mas = ((asTMW)
|
|
|
|
? TibetanMachineWeb.getAttributeSet(glyphs[i].font)
|
|
|
|
: TibetanMachineWeb.getAttributeSetTM(glyphs[i].font));
|
2003-06-28 16:20:19 +00:00
|
|
|
if (null == mas)
|
|
|
|
throw new Error("Cannot insert that DuffData; the font number is too low or too high; perhaps the programmer has asTMW set incorrectly?");
|
2003-09-05 05:54:35 +00:00
|
|
|
appendDuff(fontSize, pos, glyphs[i].text, mas, color);
|
2002-10-06 18:23:27 +00:00
|
|
|
pos += glyphs[i].text.length();
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the entire document into Extended Wylie.
|
|
|
|
* If the document consists of both Tibetan and
|
|
|
|
* non-Tibetan fonts, however, the conversion stops
|
|
|
|
* at the first non-Tibetan font.
|
2003-07-01 03:43:33 +00:00
|
|
|
* @param noSuchWylie an array which will not be touched if this is
|
|
|
|
* successful; however, if there is no THDL Extended Wylie
|
|
|
|
* corresponding to one of these glyphs, then noSuchWylie[0] will be
|
|
|
|
* set to true
|
|
|
|
* @return the string of Wylie corresponding to this document */
|
|
|
|
public String getWylie(boolean noSuchWylie[]) {
|
|
|
|
return getWylie(0, getLength(), noSuchWylie);
|
2002-10-06 18:23:27 +00:00
|
|
|
}
|
|
|
|
|
2003-09-02 06:39:33 +00:00
|
|
|
/**
|
|
|
|
* Converts the entire document into ACIP. If the document consists of
|
|
|
|
* both Tibetan and non-Tibetan fonts, however, the conversion stops at
|
|
|
|
* the first non-Tibetan font.
|
|
|
|
* @param noSuchACIP an array which will not be touched if this is
|
|
|
|
* successful; however, if there is no ACIP corresponding to one of
|
|
|
|
* these glyphs, then noSuchACIP[0] will be set to true
|
|
|
|
* @return the string of ACIP corresponding to this document */
|
|
|
|
public String getACIP(boolean noSuchACIP[]) {
|
|
|
|
return getACIP(0, getLength(), noSuchACIP);
|
|
|
|
}
|
|
|
|
|
2002-10-06 18:23:27 +00:00
|
|
|
/**
|
|
|
|
* Converts a portion of the document into Extended Wylie.
|
|
|
|
* If the document consists of both Tibetan and
|
|
|
|
* non-Tibetan fonts, however, the conversion stops
|
|
|
|
* at the first non-Tibetan font.
|
|
|
|
* @param begin the beginning of the region to convert
|
|
|
|
* @param end the end of the region to convert
|
2003-07-01 03:43:33 +00:00
|
|
|
* @param noSuchWylie an array which will not be touched if this is
|
|
|
|
* successful; however, if there is no THDL Extended Wylie
|
|
|
|
* corresponding to one of these glyphs, then noSuchWylie[0] will be
|
|
|
|
* set to true
|
|
|
|
* @return the string of Wylie corresponding to this document */
|
2003-09-02 06:39:33 +00:00
|
|
|
public String getWylie(int begin, int end, boolean noSuchWylie[]) {
|
|
|
|
return getTranslit(true, begin, end, noSuchWylie);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a portion of the document into ACIP. If the document
|
|
|
|
* consists of both Tibetan and non-Tibetan fonts, however, the
|
|
|
|
* conversion stops at the first non-Tibetan font.
|
|
|
|
* @param begin the beginning of the region to convert
|
|
|
|
* @param end the end of the region to convert
|
2003-09-10 01:19:05 +00:00
|
|
|
* @param noSuchACIP an array which will not be touched if this is
|
2003-09-02 06:39:33 +00:00
|
|
|
* successful; however, if there is no ACIP corresponding to one of
|
|
|
|
* these glyphs, then noSuchACIP[0] will be set to true
|
|
|
|
* @return the string of ACIP corresponding to this document */
|
|
|
|
public String getACIP(int begin, int end, boolean noSuchACIP[]) {
|
|
|
|
return getTranslit(true, begin, end, noSuchACIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getTranslit(boolean EWTSNotACIP, int begin, int end, boolean noSuch[]) {
|
2002-10-06 18:23:27 +00:00
|
|
|
AttributeSet attr;
|
|
|
|
String fontName;
|
|
|
|
int fontNum;
|
|
|
|
DuffCode dc;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
if (begin >= end)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
java.util.List dcs = new ArrayList();
|
|
|
|
int i = begin;
|
2003-09-02 06:39:33 +00:00
|
|
|
StringBuffer translitBuffer = new StringBuffer();
|
2002-10-06 18:23:27 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
while (i < end) {
|
|
|
|
attr = getCharacterElement(i).getAttributes();
|
|
|
|
fontName = StyleConstants.getFontFamily(attr);
|
|
|
|
|
|
|
|
ch = getText(i,1).charAt(0);
|
|
|
|
|
|
|
|
//current character is formatting
|
|
|
|
if (ch == '\n' || ch == '\t') {
|
|
|
|
if (dcs.size() > 0) {
|
|
|
|
DuffCode[] dc_array = new DuffCode[0];
|
|
|
|
dc_array = (DuffCode[])dcs.toArray(dc_array);
|
2003-09-02 06:39:33 +00:00
|
|
|
translitBuffer.append(TibTextUtils.getTranslit(EWTSNotACIP, dc_array, noSuch));
|
2002-10-06 18:23:27 +00:00
|
|
|
dcs.clear();
|
|
|
|
}
|
2003-09-02 06:39:33 +00:00
|
|
|
translitBuffer.append(ch);
|
2002-10-06 18:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//current character isn't TMW
|
|
|
|
else if ((0 == (fontNum = TibetanMachineWeb.getTMWFontNumber(fontName)))) {
|
|
|
|
if (dcs.size() > 0) {
|
|
|
|
DuffCode[] dc_array = new DuffCode[0];
|
|
|
|
dc_array = (DuffCode[])dcs.toArray(dc_array);
|
2003-09-02 06:39:33 +00:00
|
|
|
translitBuffer.append(TibTextUtils.getTranslit(EWTSNotACIP, dc_array, noSuch));
|
2002-10-06 18:23:27 +00:00
|
|
|
dcs.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//current character is convertable
|
|
|
|
else {
|
|
|
|
dc = new DuffCode(fontNum, ch);
|
|
|
|
dcs.add(dc);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (dcs.size() > 0) {
|
|
|
|
DuffCode[] dc_array = new DuffCode[0];
|
|
|
|
dc_array = (DuffCode[])dcs.toArray(dc_array);
|
2003-09-02 06:39:33 +00:00
|
|
|
translitBuffer.append(TibTextUtils.getTranslit(EWTSNotACIP, dc_array, noSuch));
|
2002-10-06 18:23:27 +00:00
|
|
|
}
|
2003-09-02 06:39:33 +00:00
|
|
|
return translitBuffer.toString();
|
2002-10-06 18:23:27 +00:00
|
|
|
}
|
|
|
|
catch (BadLocationException ble) {
|
|
|
|
ble.printStackTrace();
|
|
|
|
ThdlDebug.noteIffyCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
2003-05-18 14:14:47 +00:00
|
|
|
|
|
|
|
/** Prints to standard output a list of all the indices of
|
|
|
|
characters that are not in a TMW font within the range [start,
|
|
|
|
end). Using a negative number for end means that this will
|
|
|
|
run to the end of the document. SPEED_FIXME: might be faster
|
|
|
|
to run over the elements, if they are one per font.
|
|
|
|
@return 1 if at least one non-TMW character was found in
|
|
|
|
the specified range, zero if none were, -1 on error. */
|
|
|
|
public int findAllNonTMWCharacters(int begin, int end) {
|
2003-05-18 17:17:52 +00:00
|
|
|
return findAllNonTMWCharacters(begin, end, System.out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Configurable so that System.out isn't necessarily used. */
|
|
|
|
public int findAllNonTMWCharacters(int begin, int end, PrintStream out) {
|
2003-06-22 00:14:18 +00:00
|
|
|
return findCharacters(begin, end, out, "Non-TMW", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Prints to standard output a list of all the indices of
|
|
|
|
characters that are not in a TM font within the range [start,
|
|
|
|
end). Using a negative number for end means that this will
|
|
|
|
run to the end of the document. SPEED_FIXME: might be faster
|
|
|
|
to run over the elements, if they are one per font.
|
|
|
|
@return 1 if at least one non-TM character was found in
|
|
|
|
the specified range, zero if none were, -1 on error. */
|
|
|
|
public int findAllNonTMCharacters(int begin, int end) {
|
|
|
|
return findAllNonTMCharacters(begin, end, System.out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Configurable so that System.out isn't necessarily used. */
|
|
|
|
public int findAllNonTMCharacters(int begin, int end, PrintStream out) {
|
|
|
|
return findCharacters(begin, end, out, "Non-TM", true);
|
2003-05-18 14:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Finds the first occurrence of a non-TMW character in a given
|
|
|
|
font and prints it to System.out. If you have a Tahoma
|
2003-06-22 00:14:18 +00:00
|
|
|
newline and an Arial newline, e.g., the first occurrence of
|
|
|
|
each will be reported.
|
2003-05-18 14:14:47 +00:00
|
|
|
|
|
|
|
<p>Works within the range [start, end). Using a negative
|
|
|
|
number for end means that this will run to the end of the
|
|
|
|
document. SPEED_FIXME: might be faster to run over the
|
|
|
|
elements, if they are one per font.
|
|
|
|
@return 1 if at least one non-TMW character was found in
|
|
|
|
the specified range, zero if none were, -1 on error. */
|
|
|
|
public int findSomeNonTMWCharacters(int begin, int end) {
|
2003-05-18 17:17:52 +00:00
|
|
|
return findSomeNonTMWCharacters(begin, end, System.out);
|
|
|
|
}
|
|
|
|
|
2003-06-22 00:14:18 +00:00
|
|
|
/** Finds the first occurrence of a non-TM character in a given
|
|
|
|
font and prints it to System.out. If you have a Tahoma
|
|
|
|
newline and an Arial newline, e.g., the first occurrence of
|
|
|
|
each will be reported.
|
|
|
|
|
|
|
|
<p>Works within the range [start, end). Using a negative
|
|
|
|
number for end means that this will run to the end of the
|
|
|
|
document. SPEED_FIXME: might be faster to run over the
|
|
|
|
elements, if they are one per font.
|
|
|
|
@return 1 if at least one non-TMW character was found in
|
|
|
|
the specified range, zero if none were, -1 on error. */
|
|
|
|
public int findSomeNonTMCharacters(int begin, int end) {
|
|
|
|
return findSomeNonTMCharacters(begin, end, System.out);
|
|
|
|
}
|
|
|
|
|
2003-05-18 17:17:52 +00:00
|
|
|
/** Configurable so that System.out isn't necessarily used. */
|
|
|
|
public int findSomeNonTMWCharacters(int begin, int end, PrintStream out) {
|
2003-06-22 00:14:18 +00:00
|
|
|
return findCharacters(begin, end, out, "Non-TMW", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Configurable so that System.out isn't necessarily used. */
|
|
|
|
public int findSomeNonTMCharacters(int begin, int end, PrintStream out) {
|
|
|
|
return findCharacters(begin, end, out, "Non-TM", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Pass in whatKind=="Non-TMW" or whatKind=="Non-TM" for now; see
|
|
|
|
callers and the code to understand the semantics. Pass in all
|
|
|
|
== true to find all characters or all == false to report each
|
|
|
|
character just once. */
|
|
|
|
private int findCharacters(int begin, int end, PrintStream out,
|
|
|
|
String whatKind, boolean all) {
|
|
|
|
if (whatKind != "Non-TMW" && whatKind != "Non-TM")
|
|
|
|
throw new IllegalArgumentException("You didn't use an interned string.");
|
2003-05-18 14:14:47 +00:00
|
|
|
if (end < 0)
|
|
|
|
end = getLength();
|
|
|
|
if (begin >= end)
|
|
|
|
return 0;
|
|
|
|
int i = begin;
|
|
|
|
int returnValue = 0;
|
|
|
|
try {
|
2003-06-22 00:14:18 +00:00
|
|
|
HashMap cgfTable = null;
|
|
|
|
if (!all) cgfTable = new HashMap();
|
2003-05-18 14:14:47 +00:00
|
|
|
while (i < end) {
|
|
|
|
AttributeSet attr = getCharacterElement(i).getAttributes();
|
|
|
|
String fontName = StyleConstants.getFontFamily(attr);
|
2003-06-22 00:14:18 +00:00
|
|
|
if ((whatKind == "Non-TMW"
|
|
|
|
&& (0 == TibetanMachineWeb.getTMWFontNumber(fontName)))
|
|
|
|
|| (whatKind == "Non-TM"
|
|
|
|
&& (0 == TibetanMachineWeb.getTMFontNumber(fontName)))) {
|
2003-05-18 14:14:47 +00:00
|
|
|
returnValue = 1;
|
|
|
|
CharacterInAGivenFont cgf
|
|
|
|
= new CharacterInAGivenFont(getText(i, 1), fontName);
|
2003-06-22 00:14:18 +00:00
|
|
|
boolean doOutput = all;
|
|
|
|
if (!all && !cgfTable.containsKey(cgf)) {
|
2003-05-18 14:14:47 +00:00
|
|
|
cgfTable.put(cgf, "yes this character appears once");
|
2003-06-22 00:14:18 +00:00
|
|
|
doOutput = true;
|
2003-05-18 14:14:47 +00:00
|
|
|
}
|
2003-06-22 00:14:18 +00:00
|
|
|
if (true == doOutput)
|
|
|
|
out.println(whatKind + " character "
|
|
|
|
+ cgf + " appears "
|
|
|
|
+ ((all) ? "" : "first ")
|
|
|
|
+ "at location " + i);
|
2003-05-18 14:14:47 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (BadLocationException ble) {
|
2003-05-18 17:17:52 +00:00
|
|
|
ble.printStackTrace(out);
|
2003-05-18 14:14:47 +00:00
|
|
|
ThdlDebug.noteIffyCode();
|
|
|
|
returnValue = -1;
|
|
|
|
}
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final DuffData[] leftCurlyBraceTMW
|
|
|
|
= new DuffData[] { new DuffData("{", 1) };
|
|
|
|
private static final DuffData[] rightCurlyBraceTMW
|
|
|
|
= new DuffData[] { new DuffData("}", 1) };
|
|
|
|
private static final DuffData[] backslashTMW
|
|
|
|
= new DuffData[] { new DuffData("\\", 2) };
|
|
|
|
/** This is a band-aid used to help Jskad fix RTF files that are
|
|
|
|
mostly TMW but have some Tahoma characters that should be TMW.
|
|
|
|
Replaces '{', '}', and '\\' characters with the correct
|
|
|
|
TibetanMachineWeb. Works within the range [start, end).
|
|
|
|
Using a negative number for end means that this will run to
|
|
|
|
the end of the document. Be sure to set the size for Tibetan
|
2003-05-31 23:21:29 +00:00
|
|
|
as you like it before using this (well, it usually gets it
|
|
|
|
right on its own, but just in case). SPEED_FIXME: might be
|
2003-05-18 14:14:47 +00:00
|
|
|
faster to run over the elements, if they are one per font. */
|
|
|
|
public void replaceTahomaCurlyBracesAndBackslashes(int begin, int end) {
|
|
|
|
if (end < 0)
|
|
|
|
end = getLength();
|
|
|
|
if (begin >= end)
|
|
|
|
return;
|
|
|
|
int i = begin;
|
|
|
|
try {
|
|
|
|
while (i < end) {
|
|
|
|
AttributeSet attr = getCharacterElement(i).getAttributes();
|
|
|
|
String fontName = StyleConstants.getFontFamily(attr);
|
|
|
|
if (fontName.equals("Tahoma")) {
|
|
|
|
DuffData[] toReplaceWith = null;
|
|
|
|
switch (getText(i, 1).charAt(0)) {
|
|
|
|
case '{':
|
|
|
|
toReplaceWith = leftCurlyBraceTMW;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
toReplaceWith = rightCurlyBraceTMW;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
toReplaceWith = backslashTMW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (null != toReplaceWith) {
|
2003-06-01 23:05:32 +00:00
|
|
|
// SPEED_FIXME: determining font size might be slow
|
2003-05-28 00:40:59 +00:00
|
|
|
int fontSize = tibetanFontSize;
|
|
|
|
try {
|
|
|
|
fontSize = ((Integer)getCharacterElement(i).getAttributes().getAttribute(StyleConstants.FontSize)).intValue();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// leave it as tibetanFontSize
|
|
|
|
}
|
2003-06-08 23:12:52 +00:00
|
|
|
if (replaceInsteadOfInserting()) {
|
|
|
|
replaceDuff(fontSize, i, toReplaceWith[0], true);
|
|
|
|
} else {
|
|
|
|
if (insertBefore()) {
|
|
|
|
insertDuff(fontSize, i, toReplaceWith, true);
|
|
|
|
remove(i+1, 1);
|
|
|
|
} else {
|
|
|
|
insertDuff(fontSize, i+1, toReplaceWith, true);
|
|
|
|
remove(i, 1);
|
|
|
|
}
|
|
|
|
}
|
2003-05-18 14:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (BadLocationException ble) {
|
|
|
|
ble.printStackTrace();
|
|
|
|
ThdlDebug.noteIffyCode();
|
|
|
|
}
|
|
|
|
}
|
2003-05-31 23:21:29 +00:00
|
|
|
|
|
|
|
/** Converts all TibetanMachineWeb glyphs in the document to
|
|
|
|
TibetanMachine. Works within the range [start, end). Using a
|
|
|
|
negative number for end means that this will run to the end of
|
|
|
|
the document. Be sure to set the size for Tibetan as you like
|
|
|
|
it before using this (well, it usually gets it right on its
|
|
|
|
own, but just in case). SPEED_FIXME: might be faster to run
|
2003-06-01 23:05:32 +00:00
|
|
|
over the elements, if they are one per font.
|
2003-06-08 23:12:52 +00:00
|
|
|
@return false on 100% success, true if any exceptional case
|
2003-06-01 23:05:32 +00:00
|
|
|
was encountered
|
|
|
|
@param errors if non-null, then notes about all exceptional
|
|
|
|
cases will be appended to this StringBuffer
|
2003-06-29 21:31:48 +00:00
|
|
|
@param numAttemptedReplacements an array that contains one
|
|
|
|
element; this first element will be, upon exit, incremented by
|
|
|
|
the number of TMW glyphs that we encountered and attempted to
|
|
|
|
convert to TM
|
2003-06-01 23:05:32 +00:00
|
|
|
*/
|
2003-06-29 21:31:48 +00:00
|
|
|
public boolean convertToTM(int begin, int end, StringBuffer errors,
|
|
|
|
long numAttemptedReplacements[]) {
|
|
|
|
return convertHelper(begin, end, true, false, errors, null,
|
|
|
|
numAttemptedReplacements);
|
2003-05-31 23:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Converts all TibetanMachine glyphs in the document to
|
|
|
|
TibetanMachineWeb. Works within the range [start, end).
|
|
|
|
Using a negative number for end means that this will run to
|
|
|
|
the end of the document. Be sure to set the size for Tibetan
|
|
|
|
as you like it before using this (well, it usually gets it
|
|
|
|
right on its own, but just in case). SPEED_FIXME: might be
|
2003-06-01 23:05:32 +00:00
|
|
|
faster to run over the elements, if they are one per font.
|
2003-06-08 23:12:52 +00:00
|
|
|
@return false on 100% success, true if any exceptional case
|
2003-06-01 23:05:32 +00:00
|
|
|
was encountered
|
|
|
|
@param errors if non-null, then notes about all exceptional
|
|
|
|
cases will be appended to this StringBuffer
|
2003-06-29 21:31:48 +00:00
|
|
|
@param numAttemptedReplacements an array that contains one
|
|
|
|
element; this first element will be, upon exit, incremented by
|
|
|
|
the number of TM glyphs that we encountered and attempted to
|
|
|
|
convert to TMW
|
2003-06-01 23:05:32 +00:00
|
|
|
*/
|
2003-06-29 21:31:48 +00:00
|
|
|
public boolean convertToTMW(int begin, int end, StringBuffer errors,
|
|
|
|
long numAttemptedReplacements[]) {
|
|
|
|
return convertHelper(begin, end, false, false, errors, null,
|
|
|
|
numAttemptedReplacements);
|
2003-06-15 16:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Converts all TibetanMachineWeb glyphs in the document to
|
|
|
|
Unicode. Works within the range [start, end). Using a
|
|
|
|
negative number for end means that this will run to the end of
|
|
|
|
the document. Be sure to set the size for Tibetan as you like
|
|
|
|
it before using this (well, it usually gets it right on its
|
|
|
|
own, but just in case). SPEED_FIXME: might be faster to run
|
|
|
|
over the elements, if they are one per font.
|
|
|
|
@return false on 100% success, true if any exceptional case
|
|
|
|
was encountered
|
|
|
|
@param errors if non-null, then notes about all exceptional
|
2003-06-23 01:58:11 +00:00
|
|
|
cases will be appended to this StringBuffer
|
|
|
|
@param unicodeFont the name of the Unicode font to use;
|
2003-06-24 03:02:29 +00:00
|
|
|
defaults to Ximalaya if null
|
2003-06-29 21:31:48 +00:00
|
|
|
@param numAttemptedReplacements an array that contains one
|
|
|
|
element; this first element will be, upon exit, incremented by
|
|
|
|
the number of TMW glyphs that we encountered and attempted to
|
|
|
|
convert to Unicode
|
2003-06-23 01:58:11 +00:00
|
|
|
*/
|
|
|
|
public boolean convertToUnicode(int begin, int end, StringBuffer errors,
|
2003-06-29 21:31:48 +00:00
|
|
|
String unicodeFont,
|
|
|
|
long numAttemptedReplacements[]) {
|
|
|
|
return convertHelper(begin, end, false, true, errors, unicodeFont,
|
|
|
|
numAttemptedReplacements);
|
2003-05-31 23:21:29 +00:00
|
|
|
}
|
|
|
|
|
2003-06-08 23:12:52 +00:00
|
|
|
/** This setting determines whether the formatting is preserved,
|
|
|
|
but with infinite loops in it, or is not preserved, but works
|
|
|
|
well. Inserting + removing must be used rather than replacing
|
|
|
|
because you get the same exception otherwise. FIXME: try Java
|
|
|
|
1.5 -- maybe it beats Java 1.4.
|
|
|
|
|
|
|
|
[java] javax.swing.text.StateInvariantError: infinite loop in formatting
|
|
|
|
[java] at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:404)
|
|
|
|
[java] at javax.swing.text.FlowView.layout(FlowView.java:182)
|
|
|
|
[java] at javax.swing.text.BoxView.setSize(BoxView.java:379)
|
|
|
|
[java] at javax.swing.text.BoxView.updateChildSizes(BoxView.java:348)
|
|
|
|
[java] at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:330)
|
|
|
|
[java] at javax.swing.text.BoxView.layout(BoxView.java:682)
|
|
|
|
[java] at javax.swing.text.BoxView.setSize(BoxView.java:379)
|
|
|
|
[java] at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1598)
|
|
|
|
[java] at javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:800)
|
|
|
|
[java] at javax.swing.JComponent.getPreferredSize(JComponent.java:1272)
|
|
|
|
[java] at javax.swing.JEditorPane.getPreferredSize(JEditorPane.java:1206)
|
|
|
|
[java] at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:769)
|
|
|
|
[java] at java.awt.Container.layout(Container.java:1017)
|
|
|
|
[java] at java.awt.Container.doLayout(Container.java:1007)
|
|
|
|
[java] at java.awt.Container.validateTree(Container.java:1089)
|
|
|
|
[java] at java.awt.Container.validate(Container.java:1064)
|
|
|
|
[java] at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:353)
|
|
|
|
[java] at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:116)
|
|
|
|
[java] at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
|
|
|
|
[java] at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
|
|
|
|
[java] at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
|
|
|
|
[java] javax.swing.text.StateInvariantError: infinite loop in formatting
|
|
|
|
[java] at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:404)
|
|
|
|
[java] at javax.swing.text.FlowView.layout(FlowView.java:182)
|
|
|
|
[java] at javax.swing.text.BoxView.setSize(BoxView.java:379)
|
|
|
|
[java] at javax.swing.text.BoxView.updateChildSizes(BoxView.java:348)
|
|
|
|
[java] at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:316)
|
|
|
|
[java] at javax.swing.text.BoxView.layout(BoxView.java:683)
|
|
|
|
[java] at javax.swing.text.BoxView.setSize(BoxView.java:379)
|
|
|
|
[java] at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1598)
|
|
|
|
[java] at javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:800)
|
|
|
|
[java] at javax.swing.JComponent.getPreferredSize(JComponent.java:1272)
|
|
|
|
[java] at javax.swing.JEditorPane.getPreferredSize(JEditorPane.java:1206)
|
|
|
|
[java] at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:769)
|
|
|
|
[java] at java.awt.Container.layout(Container.java:1017)
|
|
|
|
[java] at java.awt.Container.doLayout(Container.java:1007)
|
|
|
|
[java] at java.awt.Container.validateTree(Container.java:1089)
|
|
|
|
[java] at java.awt.Container.validate(Container.java:1064)
|
|
|
|
[java] at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:353)
|
|
|
|
[java] at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:116)
|
|
|
|
[java] at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
|
|
|
|
[java] at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
|
|
|
|
[java] at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
|
|
|
|
[java] javax.swing.text.StateInvariantError: infinite loop in formatting
|
|
|
|
[java] at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:404)
|
|
|
|
[java] at javax.swing.text.FlowView.layout(FlowView.java:182)
|
|
|
|
[java] at javax.swing.text.BoxView.setSize(BoxView.java:379)
|
|
|
|
[java] at javax.swing.text.BoxView.updateChildSizes(BoxView.java:348)
|
|
|
|
[java] at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:316)
|
|
|
|
[java] at javax.swing.text.BoxView.layout(BoxView.java:683)
|
|
|
|
[java] at javax.swing.text.BoxView.setSize(BoxView.java:379)
|
|
|
|
[java] at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1598)
|
|
|
|
[java] at javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:934)
|
|
|
|
[java] at javax.swing.text.DefaultCaret.repaintNewCaret(DefaultCaret.java:1044)
|
|
|
|
[java] at javax.swing.text.DefaultCaret$1.run(DefaultCaret.java:1023)
|
|
|
|
[java] at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
|
|
|
|
[java] at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
|
|
|
|
[java] at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
|
|
|
|
[java] javax.swing.text.StateInvariantError: infinite loop in formatting
|
|
|
|
[java] at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:404)
|
|
|
|
[java] at javax.swing.text.FlowView.layout(FlowView.java:182)
|
|
|
|
[java] at javax.swing.text.BoxView.setSize(BoxView.java:379)
|
|
|
|
[java] at javax.swing.text.BoxView.updateChildSizes(BoxView.java:348)
|
|
|
|
[java] at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:316)
|
|
|
|
[java] at javax.swing.text.BoxView.layout(BoxView.java:683)
|
|
|
|
[java] at javax.swing.text.BoxView.setSize(BoxView.java:379)
|
|
|
|
[java] at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1598)
|
|
|
|
[java] at javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:800)
|
|
|
|
[java] at javax.swing.JComponent.getPreferredSize(JComponent.java:1272)
|
|
|
|
[java] at javax.swing.JEditorPane.getPreferredSize(JEditorPane.java:1206)
|
|
|
|
[java] at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:769)
|
|
|
|
[java] at java.awt.Container.layout(Container.java:1017)
|
|
|
|
[java] at java.awt.Container.doLayout(Container.java:1007)
|
|
|
|
[java] at java.awt.Container.validateTree(Container.java:1089)
|
|
|
|
[java] at java.awt.Container.validate(Container.java:1064)
|
|
|
|
[java] at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:353)
|
|
|
|
[java] at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:116)
|
|
|
|
[java] at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
|
|
|
|
[java] at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
|
|
|
|
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
|
|
|
|
[java] at java.awt.EventDispatchThread.run(EventDispatchThread.java:99) */
|
|
|
|
private static boolean insertBefore() {
|
|
|
|
return !ThdlOptions.getBooleanOption("thdl.insert.rtf.after.not.before");
|
|
|
|
}
|
|
|
|
private static boolean replaceInsteadOfInserting() {
|
|
|
|
return !ThdlOptions.getBooleanOption("thdl.insert.and.remove.instead.of.replacing");
|
|
|
|
}
|
|
|
|
|
2003-06-15 16:27:36 +00:00
|
|
|
/** Helper function. Converts TMW->TM if !toUnicode&&toTM,
|
|
|
|
TM->TMW if !toUnicode&&!toTM, TMW->Unicode if toUnicode.
|
2003-06-01 23:05:32 +00:00
|
|
|
@param errors if non-null, then notes about all exceptional
|
|
|
|
cases will be appended to this StringBuffer
|
2003-06-08 23:12:52 +00:00
|
|
|
@return false on 100% success, true if any exceptional case
|
2003-06-01 23:05:32 +00:00
|
|
|
was encountered
|
2003-07-04 00:12:59 +00:00
|
|
|
@see #convertToUnicode(int,int,StringBuffer,String,long[])
|
|
|
|
@see #convertToTMW(int,int,StringBuffer,long[])
|
|
|
|
@see #convertToTM(int,int,StringBuffer,long[]) */
|
2003-06-15 16:27:36 +00:00
|
|
|
private boolean convertHelper(int begin, int end, boolean toTM,
|
2003-06-23 01:58:11 +00:00
|
|
|
boolean toUnicode, StringBuffer errors,
|
2003-06-29 21:31:48 +00:00
|
|
|
String unicodeFont,
|
|
|
|
long numAttemptedReplacements[]) {
|
2003-06-22 22:10:58 +00:00
|
|
|
// To preserve formatting, we go paragraph by paragraph.
|
|
|
|
|
|
|
|
// Use positions, not offsets, because our work on paragraph K
|
|
|
|
// will affect the offsets of paragraph K+1.
|
|
|
|
|
|
|
|
Position finalEndPos;
|
|
|
|
if (end < 0) {
|
|
|
|
end = getLength();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
finalEndPos = createPosition(end);
|
|
|
|
} catch (BadLocationException e) {
|
|
|
|
throw new Error("BAD LOCATION DURING CONVERSION");
|
|
|
|
}
|
|
|
|
|
|
|
|
ConversionErrorHelper ceh = new ConversionErrorHelper();
|
2003-06-23 01:24:02 +00:00
|
|
|
int pl = 0;
|
2003-06-28 16:20:19 +00:00
|
|
|
pl = getParagraphs(begin, finalEndPos.getOffset()).length;
|
2003-06-23 01:24:02 +00:00
|
|
|
boolean warn = false;
|
|
|
|
int lastTimeWeExamined = -1; // must be -1
|
|
|
|
boolean noMore = false;
|
2003-06-29 21:31:48 +00:00
|
|
|
|
2003-06-23 01:24:02 +00:00
|
|
|
while (!noMore
|
|
|
|
&& lastTimeWeExamined != ceh.lastOffsetExamined) {
|
|
|
|
lastTimeWeExamined = ceh.lastOffsetExamined;
|
|
|
|
Element thisParagraph
|
|
|
|
= getParagraphElement(lastTimeWeExamined + 1);
|
|
|
|
int p_end = thisParagraph.getEndOffset();
|
|
|
|
if (p_end >= finalEndPos.getOffset()) {
|
|
|
|
noMore = true;
|
2003-06-22 22:10:58 +00:00
|
|
|
ceh.doErrorWrapup = true;
|
2003-06-23 01:24:02 +00:00
|
|
|
}
|
|
|
|
convertHelperHelper(thisParagraph.getStartOffset(),
|
2003-06-22 22:10:58 +00:00
|
|
|
((finalEndPos.getOffset() < p_end)
|
|
|
|
? finalEndPos.getOffset()
|
|
|
|
: p_end),
|
2003-06-23 01:58:11 +00:00
|
|
|
toTM, toUnicode, errors, ceh,
|
2003-06-29 21:31:48 +00:00
|
|
|
unicodeFont,
|
|
|
|
numAttemptedReplacements);
|
2003-06-22 22:10:58 +00:00
|
|
|
}
|
2003-06-23 01:24:02 +00:00
|
|
|
if (!ceh.errorReturn
|
|
|
|
&& pl != getParagraphs(begin, finalEndPos.getOffset()).length) {
|
|
|
|
System.err.println("Conversion WARNING: the number of paragraphs changed from "
|
2003-06-28 16:20:19 +00:00
|
|
|
+ pl + " to " + getParagraphs(begin, finalEndPos.getOffset()).length
|
2003-06-23 01:24:02 +00:00
|
|
|
+ ", indicating that formatting may have been lost.");
|
2003-06-28 16:20:19 +00:00
|
|
|
/* You'll see this with this document:
|
|
|
|
|
|
|
|
{\rtf1\ansi\deff0\deftab720{\fonttbl{\f10\fnil\fprq2 TibetanMachine;}}
|
|
|
|
\deflang1033\pard\plain\f10\fs48\cf0 \u0156\par }
|
|
|
|
|
|
|
|
You'll see it coming (TM->TMW) and going (if you do
|
|
|
|
TMW->TM again). I wonder if finalEndPos isn't one shy
|
|
|
|
of where you'd think it would be. FIXME */
|
2003-06-23 01:24:02 +00:00
|
|
|
}
|
2003-06-22 22:10:58 +00:00
|
|
|
return ceh.errorReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** See the sole caller, convertHelper. */
|
|
|
|
private void convertHelperHelper(int begin, int end, boolean toTM,
|
|
|
|
boolean toUnicode, StringBuffer errors,
|
2003-06-23 01:58:11 +00:00
|
|
|
ConversionErrorHelper ceh,
|
2003-06-29 21:31:48 +00:00
|
|
|
String unicodeFont,
|
|
|
|
long numAttemptedReplacements[]) {
|
2003-06-22 22:10:58 +00:00
|
|
|
final boolean debug = false;
|
|
|
|
if (debug)
|
2003-06-28 16:20:19 +00:00
|
|
|
System.out.println("cHH: [" + begin + ", " + end + ")");
|
2003-06-22 22:10:58 +00:00
|
|
|
// DLC FIXME: here's an idea, a compressor -- use the '-' (ord
|
|
|
|
// 45) or ' ' (ord 32) glyph from the same font as the
|
|
|
|
// preceding glyph, never others. This reduces the size of a
|
|
|
|
// TMW RTF file by a factor of 3 sometimes. To do it, use
|
|
|
|
// this routine, but give it the ability to go from TMW->TMW
|
|
|
|
// and TM->TM.
|
|
|
|
|
2003-06-15 16:27:36 +00:00
|
|
|
// toTM is ignored when toUnicode is true:
|
|
|
|
ThdlDebug.verify(!toUnicode || !toTM);
|
|
|
|
|
2003-06-01 23:05:32 +00:00
|
|
|
boolean toStdout = ThdlOptions.getBooleanOption("thdl.debug");
|
2003-05-31 23:21:29 +00:00
|
|
|
if (end < 0)
|
|
|
|
end = getLength();
|
|
|
|
if (begin >= end)
|
2003-06-22 22:10:58 +00:00
|
|
|
return; // nothing to do
|
|
|
|
|
|
|
|
// For speed, do as few replaces as possible. To preserve
|
|
|
|
// formatting, we'll try to replace one paragraph at a time.
|
|
|
|
// But we *must* replace when we hit a different font (TMW3 as
|
|
|
|
// opposed to TMW2, e.g.), so we'll likely replace many times
|
|
|
|
// per paragraph. One very important optimization is that we
|
|
|
|
// don't have to treat TMW3.45 or TMW3.32 as a different font
|
|
|
|
// than TMW.33 -- that's because each of the ten TMW fonts has
|
|
|
|
// the same glyph at position 32 (space) and the same glyph at
|
|
|
|
// position 45 (tsheg). Note that we're building up a big
|
2003-06-23 01:24:02 +00:00
|
|
|
// StringBuffer; we're trading space for time.
|
2003-05-31 23:21:29 +00:00
|
|
|
try {
|
2003-06-22 22:10:58 +00:00
|
|
|
int replacementStartIndex = begin;
|
|
|
|
StringBuffer replacementQueue = new StringBuffer();
|
|
|
|
int replacementFontIndex = 0;
|
|
|
|
int replacementFontSize = -1;
|
|
|
|
|
|
|
|
int i = begin;
|
2003-06-01 23:05:32 +00:00
|
|
|
Position endPos = createPosition(end);
|
2003-05-31 23:21:29 +00:00
|
|
|
DuffData[] equivalent = new DuffData[1];
|
|
|
|
equivalent[0] = new DuffData();
|
2003-06-22 22:10:58 +00:00
|
|
|
boolean mustReplace = false;
|
|
|
|
int mustReplaceUntil = -1;
|
2003-06-01 23:05:32 +00:00
|
|
|
while (i < endPos.getOffset()) {
|
2003-05-31 23:21:29 +00:00
|
|
|
AttributeSet attr = getCharacterElement(i).getAttributes();
|
|
|
|
String fontName = StyleConstants.getFontFamily(attr);
|
|
|
|
int fontNum
|
2003-06-15 16:27:36 +00:00
|
|
|
= ((toTM || toUnicode)
|
2003-05-31 23:21:29 +00:00
|
|
|
? TibetanMachineWeb.getTMWFontNumber(fontName)
|
|
|
|
: TibetanMachineWeb.getTMFontNumber(fontName));
|
|
|
|
|
|
|
|
if (0 != fontNum) {
|
2003-06-29 21:31:48 +00:00
|
|
|
++numAttemptedReplacements[0];
|
2003-06-22 22:10:58 +00:00
|
|
|
|
|
|
|
// SPEED_FIXME: determining font size might be slow, allow an override.
|
|
|
|
int fontSize = tibetanFontSize;
|
|
|
|
try {
|
|
|
|
fontSize = ((Integer)getCharacterElement(i).getAttributes().getAttribute(StyleConstants.FontSize)).intValue();
|
|
|
|
} catch (Exception e) { /* leave it as tibetanFontSize */ }
|
|
|
|
|
2003-05-31 23:21:29 +00:00
|
|
|
DuffCode dc = null;
|
2003-06-15 16:27:36 +00:00
|
|
|
String unicode = null;
|
|
|
|
if (toUnicode) {
|
|
|
|
unicode = TibetanMachineWeb.mapTMWtoUnicode(fontNum - 1,
|
|
|
|
getText(i,1).charAt(0));
|
2003-06-01 23:05:32 +00:00
|
|
|
} else {
|
2003-06-15 16:27:36 +00:00
|
|
|
if (toTM) {
|
|
|
|
dc = TibetanMachineWeb.mapTMWtoTM(fontNum - 1,
|
2003-06-22 22:10:58 +00:00
|
|
|
getText(i,1).charAt(0),
|
|
|
|
replacementFontIndex);
|
2003-06-15 16:27:36 +00:00
|
|
|
} else {
|
|
|
|
dc = TibetanMachineWeb.mapTMtoTMW(fontNum - 1,
|
2003-06-22 22:10:58 +00:00
|
|
|
getText(i,1).charAt(0),
|
|
|
|
replacementFontIndex);
|
2003-06-15 16:27:36 +00:00
|
|
|
}
|
2003-05-31 23:21:29 +00:00
|
|
|
}
|
2003-06-22 22:10:58 +00:00
|
|
|
if (replacementQueue.length() > 0
|
|
|
|
&& (mustReplace
|
|
|
|
|| ((!toUnicode
|
|
|
|
&& null != dc
|
|
|
|
&& dc.getFontNum() != replacementFontIndex)
|
|
|
|
|| fontSize != replacementFontSize))) {
|
|
|
|
// We must replace now, because the attribute
|
|
|
|
// set has changed.
|
|
|
|
|
|
|
|
// We have two choices: replace or
|
|
|
|
// insert-and-remove. We replace, because
|
|
|
|
// that preserves formatting.
|
|
|
|
|
|
|
|
// this if-else statement is duplicated below; beware!
|
|
|
|
int endIndex = mustReplace ? mustReplaceUntil : i;
|
|
|
|
if (toUnicode) {
|
|
|
|
replaceDuffsWithUnicode(replacementFontSize,
|
|
|
|
replacementStartIndex,
|
|
|
|
endIndex,
|
2003-06-23 01:58:11 +00:00
|
|
|
replacementQueue.toString(),
|
|
|
|
unicodeFont);
|
2003-06-22 22:10:58 +00:00
|
|
|
} else {
|
|
|
|
replaceDuffs(replacementFontSize,
|
|
|
|
replacementStartIndex,
|
|
|
|
endIndex,
|
|
|
|
replacementQueue.toString(),
|
|
|
|
replacementFontIndex,
|
|
|
|
!toTM);
|
2003-05-31 23:21:29 +00:00
|
|
|
}
|
2003-06-15 16:27:36 +00:00
|
|
|
|
2003-06-22 22:10:58 +00:00
|
|
|
// i += numnewchars - numoldchars;
|
|
|
|
if (debug)
|
2003-06-28 16:20:19 +00:00
|
|
|
System.out.println("Incrementing i by " + (replacementQueue.length()
|
2003-06-22 22:10:58 +00:00
|
|
|
- (endIndex - replacementStartIndex)) + "; replaced a patch with font size " + replacementFontSize + ", fontindex " + replacementFontIndex);
|
|
|
|
i += (replacementQueue.length()
|
|
|
|
- (endIndex - replacementStartIndex));
|
2003-06-15 16:27:36 +00:00
|
|
|
|
2003-06-22 22:10:58 +00:00
|
|
|
replacementQueue.delete(0, replacementQueue.length());
|
|
|
|
mustReplace = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null != dc || null != unicode) {
|
|
|
|
if (0 == replacementQueue.length()) {
|
|
|
|
replacementFontSize = fontSize;
|
|
|
|
replacementStartIndex = i;
|
|
|
|
if (!toUnicode) {
|
|
|
|
replacementFontIndex = dc.getFontNum();
|
2003-06-15 16:27:36 +00:00
|
|
|
}
|
2003-06-22 22:10:58 +00:00
|
|
|
}
|
|
|
|
if (toUnicode) {
|
|
|
|
replacementQueue.append(unicode);
|
2003-06-08 23:12:52 +00:00
|
|
|
} else {
|
2003-06-22 22:10:58 +00:00
|
|
|
replacementQueue.append(dc.getCharacter());
|
2003-06-08 23:12:52 +00:00
|
|
|
}
|
2003-05-31 23:21:29 +00:00
|
|
|
} else {
|
2003-06-22 22:10:58 +00:00
|
|
|
// For now, on error, we insert the alphabet
|
|
|
|
// in a big font in TMW to try and get some
|
|
|
|
// attention. We also put the oddballs at the
|
|
|
|
// start of the document. But then we delete
|
|
|
|
// the alphabet usually.
|
2003-06-01 23:05:32 +00:00
|
|
|
|
2003-06-22 22:10:58 +00:00
|
|
|
ceh.errorReturn = true;
|
2003-06-01 23:05:32 +00:00
|
|
|
CharacterInAGivenFont cgf
|
|
|
|
= new CharacterInAGivenFont(getText(i,1), fontName);
|
2003-06-23 01:24:02 +00:00
|
|
|
if (!ceh.problemGlyphsTable.containsKey(cgf)) {
|
|
|
|
ceh.problemGlyphsTable.put(cgf, "yes this character appears once");
|
2003-06-01 23:05:32 +00:00
|
|
|
if (null != errors) {
|
|
|
|
String err
|
2003-06-15 16:27:36 +00:00
|
|
|
= (toUnicode
|
|
|
|
? "TMW->Unicode"
|
|
|
|
: (toTM ? "TMW->TM" : "TM->TMW"))
|
2003-06-01 23:05:32 +00:00
|
|
|
+ " conversion failed for a glyph:\nFont is "
|
|
|
|
+ fontName + ", glyph number is "
|
|
|
|
+ (int)getText(i,1).charAt(0)
|
|
|
|
+ "; first position found (from zero) is "
|
|
|
|
+ i + "\n";
|
|
|
|
errors.append(err);
|
|
|
|
if (toStdout) {
|
|
|
|
System.out.print(err);
|
|
|
|
}
|
2003-06-08 22:37:38 +00:00
|
|
|
|
|
|
|
// Now also put this problem glyph at
|
2003-06-23 01:24:02 +00:00
|
|
|
// the beginning of the document,
|
|
|
|
// after a 'a' character (i.e.,
|
|
|
|
// \tm0062 or \tmw0063):
|
|
|
|
equivalent[0].setData((toUnicode || toTM) ? (char)63 : (char)62, 1);
|
|
|
|
insertDuff(72, ceh.errorGlyphLocation++,
|
|
|
|
equivalent, toUnicode || toTM);
|
|
|
|
++i;
|
2003-06-28 16:20:19 +00:00
|
|
|
// Don't later replace this last guy:
|
|
|
|
if (replacementStartIndex < ceh.errorGlyphLocation) {
|
|
|
|
++replacementStartIndex;
|
|
|
|
}
|
2003-06-08 22:37:38 +00:00
|
|
|
equivalent[0].setData(getText(i,1), fontNum);
|
2003-06-22 22:10:58 +00:00
|
|
|
insertDuff(72, ceh.errorGlyphLocation++,
|
2003-06-15 16:27:36 +00:00
|
|
|
equivalent, toUnicode || toTM);
|
2003-06-08 22:37:38 +00:00
|
|
|
++i;
|
2003-06-28 16:20:19 +00:00
|
|
|
// Don't later replace this last guy:
|
|
|
|
if (replacementStartIndex < ceh.errorGlyphLocation) {
|
|
|
|
++replacementStartIndex;
|
|
|
|
}
|
2003-06-01 23:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-15 16:27:36 +00:00
|
|
|
if (ThdlOptions.getBooleanOption("thdl.leave.bad.tm.tmw.conversions.in.place")) {
|
|
|
|
String trickyTMW
|
|
|
|
= "!-\"-#-$-%-&-'-(-)-*-+-,-.-/-0-1-2-3-4-5-6-7-8-9-:-;-<-=->-?-";
|
|
|
|
equivalent[0].setData(trickyTMW, 1);
|
|
|
|
insertDuff(72, i, equivalent, true);
|
|
|
|
i += trickyTMW.length();
|
|
|
|
}
|
2003-05-31 23:21:29 +00:00
|
|
|
}
|
2003-06-22 22:10:58 +00:00
|
|
|
} else {
|
2003-06-28 16:20:19 +00:00
|
|
|
if (debug) System.out.println("non-tm/tmw found at offset " + i + "; font=" + fontName + " ord " + (int)getText(i,1).charAt(0));
|
2003-06-22 22:10:58 +00:00
|
|
|
if (replacementQueue.length() > 0) {
|
|
|
|
if (!mustReplace) {
|
|
|
|
mustReplaceUntil = i;
|
|
|
|
mustReplace = true;
|
|
|
|
}
|
|
|
|
}
|
2003-05-31 23:21:29 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2003-06-22 22:10:58 +00:00
|
|
|
if (replacementQueue.length() > 0) {
|
|
|
|
// this if-else statement is duplicated above; beware!
|
|
|
|
int endIndex = mustReplace ? mustReplaceUntil : i;
|
|
|
|
if (toUnicode) {
|
|
|
|
replaceDuffsWithUnicode(replacementFontSize,
|
|
|
|
replacementStartIndex,
|
|
|
|
endIndex,
|
2003-06-23 01:58:11 +00:00
|
|
|
replacementQueue.toString(),
|
|
|
|
unicodeFont);
|
2003-06-22 22:10:58 +00:00
|
|
|
} else {
|
|
|
|
replaceDuffs(replacementFontSize,
|
|
|
|
replacementStartIndex,
|
|
|
|
endIndex,
|
|
|
|
replacementQueue.toString(),
|
|
|
|
replacementFontIndex,
|
|
|
|
!toTM);
|
|
|
|
}
|
|
|
|
}
|
2003-06-23 01:24:02 +00:00
|
|
|
ceh.lastOffsetExamined = endPos.getOffset() - 1;
|
2003-06-08 22:37:38 +00:00
|
|
|
|
2003-06-28 16:20:19 +00:00
|
|
|
if (ceh.doErrorWrapup && ceh.errorGlyphLocation > 0) {
|
|
|
|
// Bracket the bad stuff with U+0F3C on the left
|
|
|
|
// and U+0F3D on the right:
|
|
|
|
if (!(toUnicode || toTM)) {
|
|
|
|
equivalent[0].setData((char)209, 1);
|
|
|
|
insertDuff(72, ceh.errorGlyphLocation++,
|
|
|
|
equivalent, false);
|
|
|
|
equivalent[0].setData((char)208, 1);
|
|
|
|
insertDuff(72, 0,
|
|
|
|
equivalent, false);
|
|
|
|
ceh.errorGlyphLocation++;
|
|
|
|
} else {
|
|
|
|
equivalent[0].setData((char)94, 9);
|
|
|
|
insertDuff(72, ceh.errorGlyphLocation++,
|
|
|
|
equivalent, true);
|
|
|
|
equivalent[0].setData((char)93, 9);
|
|
|
|
insertDuff(72, 0,
|
|
|
|
equivalent, true);
|
|
|
|
ceh.errorGlyphLocation++;
|
|
|
|
}
|
|
|
|
}
|
2003-06-08 22:37:38 +00:00
|
|
|
if (!ThdlOptions.getBooleanOption("thdl.leave.bad.tm.tmw.conversions.in.place")) {
|
|
|
|
// Remove all characters other than the oddballs:
|
2003-06-22 22:10:58 +00:00
|
|
|
if (ceh.doErrorWrapup && ceh.errorGlyphLocation > 0) {
|
|
|
|
remove(ceh.errorGlyphLocation, getLength()-ceh.errorGlyphLocation-1);
|
2003-06-08 22:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-31 23:21:29 +00:00
|
|
|
} catch (BadLocationException ble) {
|
|
|
|
ble.printStackTrace();
|
|
|
|
ThdlDebug.noteIffyCode();
|
|
|
|
}
|
2003-06-22 22:10:58 +00:00
|
|
|
}
|
2003-07-01 01:21:57 +00:00
|
|
|
|
|
|
|
/** the attribute set applied to Roman text in this
|
|
|
|
document */
|
2003-09-05 05:54:35 +00:00
|
|
|
private MutableAttributeSet romanAttributeSet = null;
|
2003-07-01 01:21:57 +00:00
|
|
|
|
|
|
|
/** Gets the attribute set applied to Roman text in this
|
|
|
|
document. */
|
2003-09-05 05:54:35 +00:00
|
|
|
public MutableAttributeSet getRomanAttributeSet() {
|
2003-07-01 01:21:57 +00:00
|
|
|
return romanAttributeSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets the attribute set applied to Roman text in this
|
|
|
|
document. */
|
2003-09-05 05:54:35 +00:00
|
|
|
public void setRomanAttributeSet(MutableAttributeSet ras) {
|
2003-07-01 01:21:57 +00:00
|
|
|
romanAttributeSet = ras;
|
|
|
|
}
|
|
|
|
|
2003-08-31 16:06:35 +00:00
|
|
|
/** Sets the attribute set applied to Roman text in this
|
|
|
|
document. */
|
|
|
|
public void setRomanAttributeSet(String font, int size) {
|
|
|
|
SimpleAttributeSet ras = new SimpleAttributeSet();
|
|
|
|
StyleConstants.setFontFamily(ras, font);
|
|
|
|
StyleConstants.setFontSize(ras, size);
|
|
|
|
setRomanAttributeSet(ras);
|
|
|
|
}
|
|
|
|
|
2003-07-01 01:21:57 +00:00
|
|
|
/**
|
|
|
|
* Converts the specified portion of this document to THDL Extended
|
|
|
|
* Wylie.
|
|
|
|
*
|
|
|
|
* @param start the point from which to begin converting to Wylie
|
|
|
|
* @param end the point at which to stop converting to Wylie
|
|
|
|
* @param numAttemptedReplacements an array that contains one element;
|
|
|
|
* this first element will be, upon exit, incremented by the number of
|
2003-07-01 03:43:33 +00:00
|
|
|
* TMW glyphs that we encountered and attempted to convert to Wylie
|
|
|
|
* @return true if entirely successful, false if we put some
|
|
|
|
* "<<[[JSKAD_TMW_TO_WYLIE_ERROR_NO_SUCH_WYLIE: Cannot convert
|
|
|
|
* DuffCode..." text into the document */
|
|
|
|
public boolean toWylie(int start, int end,
|
|
|
|
long numAttemptedReplacements[]) {
|
2003-09-02 06:39:33 +00:00
|
|
|
return toTranslit(true, start, end, numAttemptedReplacements);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DLC DOC just like {@link #toWylie(int,int,long[])}
|
|
|
|
public boolean toACIP(int start, int end,
|
|
|
|
long numAttemptedReplacements[]) {
|
|
|
|
return toTranslit(false, start, end, numAttemptedReplacements);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean toTranslit(boolean EWTSNotACIP, int start, int end,
|
|
|
|
long numAttemptedReplacements[]) {
|
2003-07-01 01:21:57 +00:00
|
|
|
if (start >= end)
|
2003-07-01 03:43:33 +00:00
|
|
|
return true;
|
2003-07-01 01:21:57 +00:00
|
|
|
|
|
|
|
try {
|
2003-07-01 03:43:33 +00:00
|
|
|
boolean noSuchWylie[] = new boolean[] { false };
|
2003-07-01 01:21:57 +00:00
|
|
|
DuffCode[] any_dc_array = new DuffCode[0];
|
|
|
|
DuffCode[] dc_array;
|
|
|
|
Position endPos = createPosition(end);
|
|
|
|
int i = start;
|
|
|
|
java.util.List dcs = new ArrayList();
|
|
|
|
|
|
|
|
while (i < endPos.getOffset()+1) {
|
|
|
|
AttributeSet attr
|
|
|
|
= getCharacterElement(i).getAttributes();
|
|
|
|
String fontName = StyleConstants.getFontFamily(attr);
|
|
|
|
int fontNum;
|
|
|
|
|
|
|
|
if ((0 == (fontNum = TibetanMachineWeb.getTMWFontNumber(fontName))) || i==endPos.getOffset()) {
|
|
|
|
if (i != start) {
|
|
|
|
dc_array = (DuffCode[])dcs.toArray(any_dc_array);
|
|
|
|
remove(start, i-start);
|
2003-07-01 03:43:33 +00:00
|
|
|
ThdlDebug.verify(getRomanAttributeSet() != null);
|
2003-07-01 01:21:57 +00:00
|
|
|
insertString(start,
|
2003-09-02 06:39:33 +00:00
|
|
|
TibTextUtils.getTranslit(EWTSNotACIP,
|
|
|
|
dc_array,
|
|
|
|
noSuchWylie),
|
2003-07-01 01:21:57 +00:00
|
|
|
getRomanAttributeSet());
|
|
|
|
dcs.clear();
|
|
|
|
}
|
|
|
|
start = i+1;
|
|
|
|
} else {
|
|
|
|
char ch = getText(i,1).charAt(0);
|
|
|
|
dcs.add(new DuffCode(fontNum, ch));
|
|
|
|
++numAttemptedReplacements[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2003-07-01 03:43:33 +00:00
|
|
|
return !noSuchWylie[0];
|
2003-07-01 01:21:57 +00:00
|
|
|
} catch (BadLocationException ble) {
|
|
|
|
ble.printStackTrace();
|
|
|
|
ThdlDebug.noteIffyCode();
|
2003-07-01 03:43:33 +00:00
|
|
|
return false;
|
2003-07-01 01:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-22 22:10:58 +00:00
|
|
|
|
|
|
|
/** Returns all the paragraph elements in this document that
|
|
|
|
* contain glyphs with offsets in the range [start, end) where
|
|
|
|
* end < 0 is treated as the document's length. Note that roman,
|
2003-06-24 03:02:29 +00:00
|
|
|
* TM, Ximalaya, and TMW text can all be intermingled
|
2003-06-22 22:10:58 +00:00
|
|
|
* within a paragraph. It's the correct level of abstraction to
|
|
|
|
* use, however, because the next finer grain is roughly one
|
|
|
|
* Element per glyph. */
|
|
|
|
private Element[] getParagraphs(int start, int end) {
|
|
|
|
if (end < 0)
|
|
|
|
end = getLength();
|
|
|
|
Element arrayType[] = new Element[0];
|
|
|
|
ArrayList v = new ArrayList();
|
|
|
|
int pos = start;
|
|
|
|
while (pos <= end) {
|
|
|
|
Element pe = getParagraphElement(pos);
|
|
|
|
v.add(pe);
|
2003-06-23 01:24:02 +00:00
|
|
|
int peo = pe.getEndOffset();
|
|
|
|
if (peo == pos) {
|
|
|
|
// Avoids an infinite loop I've run into:
|
|
|
|
if (getParagraphElement(peo + 1).getEndOffset() > pos)
|
|
|
|
pos = peo + 1;
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
pos = peo;
|
2003-06-22 22:10:58 +00:00
|
|
|
}
|
|
|
|
return (Element[])v.toArray(arrayType);
|
|
|
|
}
|
|
|
|
|
2003-07-01 03:43:33 +00:00
|
|
|
/** Appends to sb a text representation of the characters (glyphs)
|
|
|
|
in this document in the range [begin, end). In this
|
|
|
|
representation, \tmwXYYY and \tmXYYY are used for TMW and TM
|
|
|
|
glyphs, respectively. \otherYYY is used for all other
|
|
|
|
characters. X is zero-based; Y is the decimal glyph number.
|
|
|
|
After every 10 characters, '\n' is added. Note well that some
|
|
|
|
TM oddballs (see TibetanMachineWeb.getUnusualTMtoTMW(int,
|
|
|
|
int)) are not handled well, so you may get \tm08222 etc. */
|
|
|
|
public void getTextRepresentation(int begin, int end, StringBuffer sb) {
|
|
|
|
if (end < 0)
|
|
|
|
end = getLength();
|
|
|
|
if (begin >= end)
|
|
|
|
return; // nothing to do
|
|
|
|
|
|
|
|
// For speed, do as few replaces as possible. To preserve
|
|
|
|
// formatting, we'll try to replace one paragraph at a time.
|
|
|
|
// But we *must* replace when we hit a different font (TMW3 as
|
|
|
|
// opposed to TMW2, e.g.), so we'll likely replace many times
|
|
|
|
// per paragraph. One very important optimization is that we
|
|
|
|
// don't have to treat TMW3.45 or TMW3.32 as a different font
|
|
|
|
// than TMW.33 -- that's because each of the ten TMW fonts has
|
|
|
|
// the same glyph at position 32 (space) and the same glyph at
|
|
|
|
// position 45 (tsheg). Note that we're building up a big
|
|
|
|
// StringBuffer; we're trading space for time.
|
|
|
|
try {
|
|
|
|
int i = begin;
|
|
|
|
int tenCount = 0;
|
|
|
|
while (i < end) {
|
|
|
|
AttributeSet attr = getCharacterElement(i).getAttributes();
|
|
|
|
String fontName = StyleConstants.getFontFamily(attr);
|
|
|
|
int tmwFontNum
|
|
|
|
= TibetanMachineWeb.getTMWFontNumber(fontName);
|
|
|
|
int tmFontNum;
|
|
|
|
if (tmwFontNum != 0) {
|
|
|
|
sb.append("\\tmw" + (tmwFontNum - 1));
|
|
|
|
} else if ((tmFontNum
|
|
|
|
= TibetanMachineWeb.getTMFontNumber(fontName))
|
|
|
|
!= 0) {
|
|
|
|
sb.append("\\tm" + (tmFontNum - 1));
|
|
|
|
} else {
|
|
|
|
// non-tmw, non-tm character:
|
|
|
|
sb.append("\\other");
|
|
|
|
}
|
|
|
|
int ordinal = (int)getText(i,1).charAt(0);
|
|
|
|
if (ordinal < 100)
|
|
|
|
sb.append('0');
|
|
|
|
if (ordinal < 10)
|
|
|
|
sb.append('0');
|
|
|
|
sb.append("" + ordinal);
|
|
|
|
if ((++tenCount) % 10 == 0) {
|
|
|
|
tenCount = 0;
|
|
|
|
sb.append('\n');
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (BadLocationException e) {
|
|
|
|
throw new ThdlLazyException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-30 02:24:11 +00:00
|
|
|
/** For debugging only. Start with an empty document, and call
|
|
|
|
this on it. You'll get all the TibetanMachine glyphs
|
|
|
|
inserted, in order, into your document. */
|
|
|
|
private void insertAllTMGlyphs() {
|
|
|
|
int font;
|
|
|
|
int ord;
|
|
|
|
DuffData[] equivalent = new DuffData[1];
|
|
|
|
equivalent[0] = new DuffData();
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
for (font = 0; font < 5; font++) {
|
|
|
|
for (ord = 32; ord < 255; ord++) {
|
|
|
|
if (TibetanMachineWeb.mapTMtoTMW(font, ord, 0) != null) {
|
|
|
|
equivalent[0].setData((char)ord, font + 1);
|
|
|
|
try {
|
|
|
|
insertDuff(tibetanFontSize, count++, equivalent, false);
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
System.err.println("nullpointerexception happened: font is " + font + " ord is " + ord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** I used this to create a document that helped me validate the
|
|
|
|
TM->TMW conversion. */
|
|
|
|
private void insertAllTMGlyphs2(MutableAttributeSet roman) {
|
|
|
|
int font;
|
|
|
|
int ord;
|
|
|
|
DuffData[] equivalent = new DuffData[1];
|
|
|
|
equivalent[0] = new DuffData();
|
|
|
|
DuffData[] tmwEquivalent = new DuffData[1];
|
|
|
|
tmwEquivalent[0] = new DuffData();
|
|
|
|
DuffData[] achen = new DuffData[1];
|
|
|
|
achen[0] = new DuffData();
|
|
|
|
achen[0].setData((char)62, 1);
|
|
|
|
DuffData[] newline = new DuffData[1];
|
|
|
|
newline[0] = new DuffData();
|
|
|
|
newline[0].setData((char)10, 1);
|
|
|
|
DuffData[] space = new DuffData[1];
|
|
|
|
space[0] = new DuffData();
|
|
|
|
space[0].setData((char)32, 1);
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
for (font = 0; font < 5; font++) {
|
|
|
|
for (ord = 32; ord < 255; ord++) {
|
|
|
|
DuffCode tmw;
|
|
|
|
if ((tmw = TibetanMachineWeb.mapTMtoTMW(font, ord, 0)) != null) {
|
|
|
|
equivalent[0].setData((char)ord, font + 1);
|
|
|
|
tmwEquivalent[0].setData(tmw.getCharacter(), tmw.getFontNum());
|
|
|
|
try {
|
|
|
|
insertDuff(72, count++, achen, false);
|
|
|
|
insertDuff(72, count++, equivalent, false);
|
|
|
|
insertDuff(72, count++, achen, false);
|
|
|
|
insertDuff(72, count++, tmwEquivalent, true);
|
|
|
|
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
System.err.println("nullpointerexception happened: font is " + font + " ord is " + ord);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
String s = " font " + (font+1) + "; ord " + ord + "\n";
|
|
|
|
insertString(count, s, roman);
|
|
|
|
count += s.length();
|
|
|
|
} catch (BadLocationException e) {
|
|
|
|
throw new Error("badness");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-22 22:10:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A helper class used by TibetanDocument.convertHelper(..). */
|
|
|
|
class ConversionErrorHelper {
|
|
|
|
boolean errorReturn;
|
|
|
|
/** one more than the location of the last error glyph, or zero if no
|
|
|
|
* error glyphs yet exist */
|
|
|
|
int errorGlyphLocation;
|
|
|
|
boolean doErrorWrapup;
|
2003-06-23 01:24:02 +00:00
|
|
|
int lastOffsetExamined;
|
|
|
|
HashMap problemGlyphsTable;
|
2003-06-22 22:10:58 +00:00
|
|
|
ConversionErrorHelper() {
|
|
|
|
errorReturn = false;
|
|
|
|
errorGlyphLocation = 0;
|
|
|
|
doErrorWrapup = false;
|
2003-06-23 01:24:02 +00:00
|
|
|
lastOffsetExamined = 0;
|
|
|
|
problemGlyphsTable = new HashMap();
|
2003-05-31 23:21:29 +00:00
|
|
|
}
|
2002-10-06 18:23:27 +00:00
|
|
|
}
|