Fixed a logic bug in mapTMWtoTM and mapTMtoTMW.

You can now specify which Unicode font to use via 'java
-Dthdl.tmw.to.unicode.font=Ximalaya ...'.
This commit is contained in:
dchandler 2003-06-23 01:58:11 +00:00
parent b6d8fd89f9
commit 917864574c
5 changed files with 61 additions and 32 deletions

View file

@ -403,7 +403,8 @@ public class Jskad extends JPanel implements DocumentListener {
public void theRealActionPerformed(ActionEvent e) { public void theRealActionPerformed(ActionEvent e) {
StringBuffer errors = new StringBuffer(); StringBuffer errors = new StringBuffer();
boolean errorReturn boolean errorReturn
= ((TibetanDocument)dp.getDocument()).convertToUnicode(0, -1, errors); // entire document = ((TibetanDocument)dp.getDocument()).convertToUnicode(0, -1, errors,
ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern()); // entire document
if (errorReturn) { if (errorReturn) {
JOptionPane.showMessageDialog(Jskad.this, JOptionPane.showMessageDialog(Jskad.this,
"At least one error occurred while converting Tibetan Machine Web\nto Unicode. Your document is mostly converted,\nexcept for the following glyphs, which you should replace manually\nbefore retrying:\n" "At least one error occurred while converting Tibetan Machine Web\nto Unicode. Your document is mostly converted,\nexcept for the following glyphs, which you should replace manually\nbefore retrying:\n"

View file

@ -177,7 +177,10 @@ public class TibetanConverter {
} else if (convertToUnicodeMode) { } else if (convertToUnicodeMode) {
StringBuffer errors = new StringBuffer(); StringBuffer errors = new StringBuffer();
// Convert to Unicode: // Convert to Unicode:
if (((TibetanDocument)dp.getDocument()).convertToUnicode(0, dp.getDocument().getLength(), errors)) { if (((TibetanDocument)dp.getDocument()).convertToUnicode(0,
dp.getDocument().getLength(),
errors,
ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern())) {
System.err.println(errors); System.err.println(errors);
exitCode = 42; exitCode = 42;
} }

View file

@ -93,7 +93,8 @@ public final class DuffCode {
} }
private void setFontNum(int font) { private void setFontNum(int font) {
ThdlDebug.verify(font >= 1 && font <= 10); if (!(font >= 1 && font <= 10))
throw new IllegalArgumentException("DuffCodes work with font numbers in the range [1, 5] or [1, 10]. This isn't in the range [1, 10]: " + font);
fontNum = font; fontNum = font;
} }

View file

@ -202,9 +202,10 @@ public class TibetanDocument extends DefaultStyledDocument {
@see TibetanMachineWeb#getUnicodeAttributeSet() @see TibetanMachineWeb#getUnicodeAttributeSet()
*/ */
private void replaceDuffsWithUnicode(int fontSize, int startOffset, private void replaceDuffsWithUnicode(int fontSize, int startOffset,
int endOffset, String unicode) { int endOffset, String unicode,
String unicodeFont) {
MutableAttributeSet mas MutableAttributeSet mas
= TibetanMachineWeb.getUnicodeAttributeSet(); = TibetanMachineWeb.getUnicodeAttributeSet(unicodeFont);
StyleConstants.setFontSize(mas, fontSize); StyleConstants.setFontSize(mas, fontSize);
try { try {
replace(startOffset, endOffset - startOffset, unicode, mas); replace(startOffset, endOffset - startOffset, unicode, mas);
@ -512,7 +513,7 @@ public class TibetanDocument extends DefaultStyledDocument {
cases will be appended to this StringBuffer cases will be appended to this StringBuffer
*/ */
public boolean convertToTM(int begin, int end, StringBuffer errors) { public boolean convertToTM(int begin, int end, StringBuffer errors) {
return convertHelper(begin, end, true, false, errors); return convertHelper(begin, end, true, false, errors, null);
} }
/** Converts all TibetanMachine glyphs in the document to /** Converts all TibetanMachine glyphs in the document to
@ -528,7 +529,7 @@ public class TibetanDocument extends DefaultStyledDocument {
cases will be appended to this StringBuffer cases will be appended to this StringBuffer
*/ */
public boolean convertToTMW(int begin, int end, StringBuffer errors) { public boolean convertToTMW(int begin, int end, StringBuffer errors) {
return convertHelper(begin, end, false, false, errors); return convertHelper(begin, end, false, false, errors, null);
} }
/** Converts all TibetanMachineWeb glyphs in the document to /** Converts all TibetanMachineWeb glyphs in the document to
@ -541,9 +542,13 @@ public class TibetanDocument extends DefaultStyledDocument {
@return false on 100% success, true if any exceptional case @return false on 100% success, true if any exceptional case
was encountered was encountered
@param errors if non-null, then notes about all exceptional @param errors if non-null, then notes about all exceptional
cases will be appended to this StringBuffer */ cases will be appended to this StringBuffer
public boolean convertToUnicode(int begin, int end, StringBuffer errors) { @param unicodeFont the name of the Unicode font to use;
return convertHelper(begin, end, false, true, errors); defaults to Arial Unicode MS if null
*/
public boolean convertToUnicode(int begin, int end, StringBuffer errors,
String unicodeFont) {
return convertHelper(begin, end, false, true, errors, unicodeFont);
} }
/** For debugging only. Start with an empty document, and call /** For debugging only. Start with an empty document, and call
@ -690,7 +695,8 @@ public class TibetanDocument extends DefaultStyledDocument {
@see convertToTMW(int,int) @see convertToTMW(int,int)
@see convertToTM(int,int) */ @see convertToTM(int,int) */
private boolean convertHelper(int begin, int end, boolean toTM, private boolean convertHelper(int begin, int end, boolean toTM,
boolean toUnicode, StringBuffer errors) { boolean toUnicode, StringBuffer errors,
String unicodeFont) {
// To preserve formatting, we go paragraph by paragraph. // To preserve formatting, we go paragraph by paragraph.
// Use positions, not offsets, because our work on paragraph K // Use positions, not offsets, because our work on paragraph K
@ -726,7 +732,8 @@ public class TibetanDocument extends DefaultStyledDocument {
((finalEndPos.getOffset() < p_end) ((finalEndPos.getOffset() < p_end)
? finalEndPos.getOffset() ? finalEndPos.getOffset()
: p_end), : p_end),
toTM, toUnicode, errors, ceh); toTM, toUnicode, errors, ceh,
unicodeFont);
} }
if (!ceh.errorReturn if (!ceh.errorReturn
&& pl != getParagraphs(begin, finalEndPos.getOffset()).length) { && pl != getParagraphs(begin, finalEndPos.getOffset()).length) {
@ -742,7 +749,8 @@ public class TibetanDocument extends DefaultStyledDocument {
/** See the sole caller, convertHelper. */ /** See the sole caller, convertHelper. */
private void convertHelperHelper(int begin, int end, boolean toTM, private void convertHelperHelper(int begin, int end, boolean toTM,
boolean toUnicode, StringBuffer errors, boolean toUnicode, StringBuffer errors,
ConversionErrorHelper ceh) { ConversionErrorHelper ceh,
String unicodeFont) {
final boolean debug = false; final boolean debug = false;
if (debug) if (debug)
System.err.println("cHH: [" + begin + ", " + end + ")"); System.err.println("cHH: [" + begin + ", " + end + ")");
@ -835,7 +843,8 @@ public class TibetanDocument extends DefaultStyledDocument {
replaceDuffsWithUnicode(replacementFontSize, replaceDuffsWithUnicode(replacementFontSize,
replacementStartIndex, replacementStartIndex,
endIndex, endIndex,
replacementQueue.toString()); replacementQueue.toString(),
unicodeFont);
} else { } else {
replaceDuffs(replacementFontSize, replaceDuffs(replacementFontSize,
replacementStartIndex, replacementStartIndex,
@ -937,7 +946,8 @@ public class TibetanDocument extends DefaultStyledDocument {
replaceDuffsWithUnicode(replacementFontSize, replaceDuffsWithUnicode(replacementFontSize,
replacementStartIndex, replacementStartIndex,
endIndex, endIndex,
replacementQueue.toString()); replacementQueue.toString(),
unicodeFont);
} else { } else {
replaceDuffs(replacementFontSize, replaceDuffs(replacementFontSize,
replacementStartIndex, replacementStartIndex,

View file

@ -76,7 +76,7 @@ public class TibetanMachineWeb implements THDLWylieConstants {
private static final String DELIMITER = "~"; private static final String DELIMITER = "~";
private static Set top_vowels; private static Set top_vowels;
/** the font we use when we convert TMW->Unicode: */ /** the font we use when we convert TMW->Unicode: */
private static SimpleAttributeSet unicodeFontAttributeSet = null; private static SimpleAttributeSet defaultUnicodeFontAttributeSet = null;
/** a way of encoding the choice of TibetanMachineWeb font from /** a way of encoding the choice of TibetanMachineWeb font from
that family of 10 fonts: */ that family of 10 fonts: */
private static SimpleAttributeSet[] webFontAttributeSet = new SimpleAttributeSet[11]; private static SimpleAttributeSet[] webFontAttributeSet = new SimpleAttributeSet[11];
@ -283,10 +283,9 @@ public class TibetanMachineWeb implements THDLWylieConstants {
readInTMFontFiles(); readInTMFontFiles();
} }
unicodeFontAttributeSet = new SimpleAttributeSet(); defaultUnicodeFontAttributeSet = new SimpleAttributeSet();
StyleConstants.setFontFamily(unicodeFontAttributeSet, StyleConstants.setFontFamily(defaultUnicodeFontAttributeSet,
ThdlOptions.getStringOption("thdl.tmw.to.unicode.font", "Arial Unicode MS");
"Arial Unicode MS"));
webFontAttributeSet[0] = null; webFontAttributeSet[0] = null;
for (int i=1; i<webFontAttributeSet.length; i++) { for (int i=1; i<webFontAttributeSet.length; i++) {
@ -665,11 +664,26 @@ public static SimpleAttributeSet getAttributeSet(int font) {
* in our TMW->Unicode conversion. This information is required in * in our TMW->Unicode conversion. This information is required in
* order to be able to put styled text into {@link TibetanDocument * order to be able to put styled text into {@link TibetanDocument
* TibetanDocument}. * TibetanDocument}.
* @param unicodeFont the interned name of the Unicode font to use;
* defaults to Arial Unicode MS if null
* @return a SimpleAttributeSet for the Unicode font - that is, a way * @return a SimpleAttributeSet for the Unicode font - that is, a way
* of encoding the font itself */ * of encoding the font itself */
public static SimpleAttributeSet getUnicodeAttributeSet() { public static SimpleAttributeSet getUnicodeAttributeSet(String unicodeFont) {
return unicodeFontAttributeSet; if (null == unicodeFont
|| "Arial Unicode MS" == unicodeFont)
return defaultUnicodeFontAttributeSet;
else {
SimpleAttributeSet cached
= (SimpleAttributeSet)unicodeAttributeSets.get(unicodeFont);
if (null == cached) {
cached = new SimpleAttributeSet();
StyleConstants.setFontFamily(cached, unicodeFont);
unicodeAttributeSets.put(unicodeFont, cached);
} }
return cached;
}
}
private static HashMap unicodeAttributeSets = new HashMap();
/** /**
* Gets the AttributeSet for the given TibetanMachine font. * Gets the AttributeSet for the given TibetanMachine font.
@ -1008,25 +1022,25 @@ public static DuffCode mapTMtoTMW(int font, int ordinal, int suggestedFont) {
if (0 == suggestedFont) if (0 == suggestedFont)
return TMW_cr; return TMW_cr;
else else
return new DuffCode(suggestedFont, (char)ordinal); return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap
} else if (ordinal == (int)'\n') { } else if (ordinal == (int)'\n') {
if (0 == suggestedFont) if (0 == suggestedFont)
return TMW_lf; return TMW_lf;
else else
return new DuffCode(suggestedFont, (char)ordinal); return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap
} else if (ordinal == (int)'\t') { } else if (ordinal == (int)'\t') {
if (0 == suggestedFont) if (0 == suggestedFont)
return TMW_tab; return TMW_tab;
else else
return new DuffCode(suggestedFont, (char)ordinal); return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap
} else { } else {
// for robustness, just return font 1, char ordinal. // for robustness, just return font 1, char ordinal.
ThdlDebug.noteIffyCode(); ThdlDebug.noteIffyCode();
return null; return null;
} }
} }
if (0 != suggestedFont && 32 == ordinal || 45 == ordinal) { if ((0 != suggestedFont) && (32 == ordinal || 45 == ordinal)) {
return new DuffCode(suggestedFont, (char)ordinal); return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap
} }
return TMtoTMW[font][ordinal-32]; return TMtoTMW[font][ordinal-32];
} }
@ -1061,25 +1075,25 @@ public static DuffCode mapTMWtoTM(int font, int ordinal, int suggestedFont) {
if (0 == suggestedFont) if (0 == suggestedFont)
return TM_cr; return TM_cr;
else else
return new DuffCode(suggestedFont, (char)ordinal); return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap
} else if (ordinal == (int)'\n') { } else if (ordinal == (int)'\n') {
if (0 == suggestedFont) if (0 == suggestedFont)
return TM_lf; return TM_lf;
else else
return new DuffCode(suggestedFont, (char)ordinal); return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap
} else if (ordinal == (int)'\t') { } else if (ordinal == (int)'\t') {
if (0 == suggestedFont) if (0 == suggestedFont)
return TM_tab; return TM_tab;
else else
return new DuffCode(suggestedFont, (char)ordinal); return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap
} else { } else {
// for robustness, just return font 1, char ordinal. // for robustness, just return font 1, char ordinal.
ThdlDebug.noteIffyCode(); ThdlDebug.noteIffyCode();
return null; return null;
} }
} }
if (0 != suggestedFont && 32 == ordinal || 45 == ordinal) { if ((0 != suggestedFont) && (32 == ordinal || 45 == ordinal)) {
return new DuffCode(suggestedFont, (char)ordinal); return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap
} }
DuffCode ans = TMWtoTM[font][ordinal-32]; DuffCode ans = TMWtoTM[font][ordinal-32];
return ans; return ans;