TMW->Wylie now occurs in the TibetanDocument, not in DuffPane,

which means that the command-line tool can finally function with a headless
graphics device.  Hopefully it will speed things up, too.  It also means that
entering Roman text into the TMW->Unicode conversion and TMW->TM
conversion will be easy.
This commit is contained in:
dchandler 2003-07-01 01:21:57 +00:00
parent 61d29fc355
commit 6151a7bc94
4 changed files with 101 additions and 89 deletions

View file

@ -190,7 +190,6 @@ public class DuffPane extends TibetanPane implements FocusListener {
private String romanFontFamily;
private int romanFontSize;
private MutableAttributeSet romanAttributeSet;
private Clipboard rtfBoard;
@ -337,7 +336,7 @@ public class DuffPane extends TibetanPane implements FocusListener {
if (isTibetan) {
processTibetanChar(key.charAt(0));
} else {
processRomanChar(key, romanAttributeSet);
processRomanChar(key, getTibDoc().getRomanAttributeSet());
}
if (manageCaret) {
caret.setDot(getTibDoc().getLength());
@ -549,7 +548,7 @@ public class DuffPane extends TibetanPane implements FocusListener {
* @param attr the attributes for the text to insert
* @param s the string of text to insert
*/
public void append(String s, MutableAttributeSet attr) {
public void append(String s, AttributeSet attr) {
append(caret.getDot(), s, attr);
}
@ -559,7 +558,7 @@ public class DuffPane extends TibetanPane implements FocusListener {
* @param attr the attributes for the text to insert
* @param s the string of text to insert
*/
public void append(int offset, String s, MutableAttributeSet attr) {
public void append(int offset, String s, AttributeSet attr) {
try {
getTibDoc().insertString(offset, s, attr);
}
@ -617,9 +616,12 @@ public class DuffPane extends TibetanPane implements FocusListener {
* @param size a point size
*/
public void setRomanAttributeSet(String font, int size) {
romanAttributeSet = new SimpleAttributeSet();
StyleConstants.setFontFamily(romanAttributeSet, font);
StyleConstants.setFontSize(romanAttributeSet, size);
if (getTibDoc() != null) {
SimpleAttributeSet ras = new SimpleAttributeSet();
StyleConstants.setFontFamily(ras, romanFontFamily = font);
StyleConstants.setFontSize(ras, romanFontSize = size);
getTibDoc().setRomanAttributeSet(ras);
}
}
/** Like {@link #setRomanAttributeSet}, but allows for noting the
@ -1120,7 +1122,7 @@ public void paste(int offset) {
isCutAndPasteEnabled = false;
}
private void processRomanChar(String key, MutableAttributeSet attSet) {
private void processRomanChar(String key, AttributeSet attSet) {
switch (key.charAt(0)) {
case KeyEvent.VK_TAB:
case KeyEvent.VK_ENTER:
@ -1483,66 +1485,6 @@ public void paste(int offset) {
}
}
/**
* Converts the entire document to Extended Wylie.
*/
public void toWylie() {
int start = getSelectionStart();
int end = getSelectionEnd();
long n[] = new long[] { 0 };
toWylie(start, end, n);
}
/**
* Converts the specified portion
* of this object's document to 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
* TMW glyphs that we encountered and attempted to convert to Wylie */
public void toWylie(int start, int end,
long numAttemptedReplacements[]) {
if (start == end)
return;
try {
DuffCode[] any_dc_array = new DuffCode[0];
DuffCode[] dc_array;
Position endPos = getTibDoc().createPosition(end);
int i = start;
java.util.List dcs = new ArrayList();
while (i < endPos.getOffset()+1) {
AttributeSet attr
= getTibDoc().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);
getTibDoc().remove(start, i-start);
append(start, TibTextUtils.getWylie(dc_array), romanAttributeSet);
dcs.clear();
}
start = i+1;
} else {
char ch = getTibDoc().getText(i,1).charAt(0);
dcs.add(new DuffCode(fontNum, ch));
++numAttemptedReplacements[0];
}
i++;
}
} catch (BadLocationException ble) {
ble.printStackTrace();
ThdlDebug.noteIffyCode();
}
}
/**
* Converts a string of Extended Wylie to TibetanMachineWeb, and
* inserts it at the specified position.

View file

@ -1076,8 +1076,9 @@ public class Jskad extends JPanel implements DocumentListener {
private void toWylie() {
Jskad.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
long n[] = new long[] { 0 };
dp.toWylie(dp.getSelectionStart(), dp.getSelectionEnd(), n);
((TibetanDocument)dp.getDocument()).toWylie(dp.getSelectionStart(),
dp.getSelectionEnd(),
new long[] { 0 });
Jskad.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}

View file

@ -19,6 +19,7 @@ Contributor(s): ______________________________________.
package org.thdl.tib.input;
import java.io.*;
import javax.swing.text.rtf.RTFEditorKit;
import org.thdl.util.*;
import org.thdl.tib.text.*;
@ -180,13 +181,13 @@ public class TibetanConverter implements FontConverterConstants {
return code so that TibetanConverter's usage message is
honored. */
static int reallyConvert(InputStream in, PrintStream out, String ct) {
DuffPane dp = new DuffPane();
TibetanDocument tdoc = new TibetanDocument();
try {
// Read in the rtf file.
if (debug) System.err.println("Start: reading in old RTF file");
if (!ThdlOptions.getBooleanOption("thdl.do.not.fix.rtf.hex.escapes"))
in = new RTFFixerInputStream(in);
dp.rtfEd.read(in, dp.getDocument(), 0);
(new RTFEditorKit()).read(in, tdoc, 0);
if (debug) System.err.println("End : reading in old RTF file");
} catch (Exception e) {
out.println("TibetanConverter:\n"
@ -205,28 +206,28 @@ public class TibetanConverter implements FontConverterConstants {
if (FIND_ALL_NON_TMW == ct) {
// 0, -1 is the entire document.
int exitCode
= ((TibetanDocument)dp.getDocument()).findAllNonTMWCharacters(0, -1, out);
= tdoc.findAllNonTMWCharacters(0, -1, out);
if (out.checkError())
exitCode = 41;
return exitCode;
} else if (FIND_SOME_NON_TMW == ct) {
// 0, -1 is the entire document.
int exitCode
= ((TibetanDocument)dp.getDocument()).findSomeNonTMWCharacters(0, -1, out);
= tdoc.findSomeNonTMWCharacters(0, -1, out);
if (out.checkError())
exitCode = 41;
return exitCode;
} else if (FIND_SOME_NON_TM == ct) {
// 0, -1 is the entire document.
int exitCode
= ((TibetanDocument)dp.getDocument()).findSomeNonTMCharacters(0, -1, out);
= tdoc.findSomeNonTMCharacters(0, -1, out);
if (out.checkError())
exitCode = 41;
return exitCode;
} else if (FIND_ALL_NON_TM == ct) {
// 0, -1 is the entire document.
int exitCode
= ((TibetanDocument)dp.getDocument()).findAllNonTMCharacters(0, -1, out);
= tdoc.findAllNonTMCharacters(0, -1, out);
if (out.checkError())
exitCode = 41;
return exitCode;
@ -235,7 +236,7 @@ public class TibetanConverter implements FontConverterConstants {
if (TM_TO_TMW != ct) {
// DLC make me optional
if (debug) System.err.println("Start: solving curly brace problem");
((TibetanDocument)dp.getDocument()).replaceTahomaCurlyBracesAndBackslashes(0, -1);
tdoc.replaceTahomaCurlyBracesAndBackslashes(0, -1);
if (debug) System.err.println("End : solving curly brace problem");
}
@ -248,13 +249,14 @@ public class TibetanConverter implements FontConverterConstants {
long numAttemptedReplacements[] = new long[] { 0 };
if (TMW_TO_WYLIE == ct) {
// Convert to THDL Wylie:
dp.toWylie(0, dp.getDocument().getLength(),
tdoc.toWylie(0,
tdoc.getLength(),
numAttemptedReplacements);
} else if (TMW_TO_UNI == ct) {
StringBuffer errors = new StringBuffer();
// Convert to Unicode:
if (((TibetanDocument)dp.getDocument()).convertToUnicode(0,
dp.getDocument().getLength(),
if (tdoc.convertToUnicode(0,
tdoc.getLength(),
errors,
ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern(),
numAttemptedReplacements)) {
@ -264,7 +266,7 @@ public class TibetanConverter implements FontConverterConstants {
} else if (TM_TO_TMW == ct) {
StringBuffer errors = new StringBuffer();
// Convert to TibetanMachineWeb:
if (((TibetanDocument)dp.getDocument()).convertToTMW(0, dp.getDocument().getLength(), errors,
if (tdoc.convertToTMW(0, tdoc.getLength(), errors,
numAttemptedReplacements)) {
System.err.println(errors);
exitCode = 42;
@ -273,7 +275,7 @@ public class TibetanConverter implements FontConverterConstants {
ThdlDebug.verify(TMW_TO_TM == ct);
StringBuffer errors = new StringBuffer();
// Convert to TibetanMachine:
if (((TibetanDocument)dp.getDocument()).convertToTM(0, dp.getDocument().getLength(), errors,
if (tdoc.convertToTM(0, tdoc.getLength(), errors,
numAttemptedReplacements)) {
System.err.println(errors);
exitCode = 42;
@ -282,7 +284,7 @@ public class TibetanConverter implements FontConverterConstants {
// Write to standard output the result:
try {
((TibetanDocument)dp.getDocument()).writeRTFOutputStream(out);
tdoc.writeRTFOutputStream(out);
} catch (IOException e) {
exitCode = 40;
}

View file

@ -1077,6 +1077,73 @@ public class TibetanDocument extends DefaultStyledDocument {
}
}
/** the attribute set applied to Roman text in this
document */
private AttributeSet romanAttributeSet = null;
/** Gets the attribute set applied to Roman text in this
document. */
public AttributeSet getRomanAttributeSet() {
return romanAttributeSet;
}
/** Sets the attribute set applied to Roman text in this
document. */
public void setRomanAttributeSet(AttributeSet ras) {
romanAttributeSet = ras;
}
/**
* 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
* TMW glyphs that we encountered and attempted to convert to Wylie */
public void toWylie(int start, int end,
long numAttemptedReplacements[]) {
if (start >= end)
return;
try {
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);
insertString(start,
TibTextUtils.getWylie(dc_array),
getRomanAttributeSet());
dcs.clear();
}
start = i+1;
} else {
char ch = getText(i,1).charAt(0);
dcs.add(new DuffCode(fontNum, ch));
++numAttemptedReplacements[0];
}
i++;
}
} catch (BadLocationException ble) {
ble.printStackTrace();
ThdlDebug.noteIffyCode();
}
}
/** 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,