From 917864574c753bffe274e6088738ec6c320092cc Mon Sep 17 00:00:00 2001 From: dchandler Date: Mon, 23 Jun 2003 01:58:11 +0000 Subject: [PATCH] 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 ...'. --- source/org/thdl/tib/input/Jskad.java | 3 +- .../org/thdl/tib/input/TibetanConverter.java | 5 +- source/org/thdl/tib/text/DuffCode.java | 3 +- source/org/thdl/tib/text/TibetanDocument.java | 34 ++++++++----- .../org/thdl/tib/text/TibetanMachineWeb.java | 48 ++++++++++++------- 5 files changed, 61 insertions(+), 32 deletions(-) diff --git a/source/org/thdl/tib/input/Jskad.java b/source/org/thdl/tib/input/Jskad.java index 4375765..a52fff0 100644 --- a/source/org/thdl/tib/input/Jskad.java +++ b/source/org/thdl/tib/input/Jskad.java @@ -403,7 +403,8 @@ public class Jskad extends JPanel implements DocumentListener { public void theRealActionPerformed(ActionEvent e) { StringBuffer errors = new StringBuffer(); 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) { 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" diff --git a/source/org/thdl/tib/input/TibetanConverter.java b/source/org/thdl/tib/input/TibetanConverter.java index 19c168f..6f0ddb0 100644 --- a/source/org/thdl/tib/input/TibetanConverter.java +++ b/source/org/thdl/tib/input/TibetanConverter.java @@ -177,7 +177,10 @@ public class TibetanConverter { } else if (convertToUnicodeMode) { StringBuffer errors = new StringBuffer(); // 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); exitCode = 42; } diff --git a/source/org/thdl/tib/text/DuffCode.java b/source/org/thdl/tib/text/DuffCode.java index cd3fb20..316e9b6 100644 --- a/source/org/thdl/tib/text/DuffCode.java +++ b/source/org/thdl/tib/text/DuffCode.java @@ -93,7 +93,8 @@ public final class DuffCode { } 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; } diff --git a/source/org/thdl/tib/text/TibetanDocument.java b/source/org/thdl/tib/text/TibetanDocument.java index 8ea555d..8030be0 100644 --- a/source/org/thdl/tib/text/TibetanDocument.java +++ b/source/org/thdl/tib/text/TibetanDocument.java @@ -202,9 +202,10 @@ public class TibetanDocument extends DefaultStyledDocument { @see TibetanMachineWeb#getUnicodeAttributeSet() */ private void replaceDuffsWithUnicode(int fontSize, int startOffset, - int endOffset, String unicode) { + int endOffset, String unicode, + String unicodeFont) { MutableAttributeSet mas - = TibetanMachineWeb.getUnicodeAttributeSet(); + = TibetanMachineWeb.getUnicodeAttributeSet(unicodeFont); StyleConstants.setFontSize(mas, fontSize); try { replace(startOffset, endOffset - startOffset, unicode, mas); @@ -512,7 +513,7 @@ public class TibetanDocument extends DefaultStyledDocument { cases will be appended to this StringBuffer */ 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 @@ -528,7 +529,7 @@ public class TibetanDocument extends DefaultStyledDocument { cases will be appended to this StringBuffer */ 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 @@ -541,9 +542,13 @@ public class TibetanDocument extends DefaultStyledDocument { @return false on 100% success, true if any exceptional case was encountered @param errors if non-null, then notes about all exceptional - cases will be appended to this StringBuffer */ - public boolean convertToUnicode(int begin, int end, StringBuffer errors) { - return convertHelper(begin, end, false, true, errors); + cases will be appended to this StringBuffer + @param unicodeFont the name of the Unicode font to use; + 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 @@ -690,7 +695,8 @@ public class TibetanDocument extends DefaultStyledDocument { @see convertToTMW(int,int) @see convertToTM(int,int) */ 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. // 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), - toTM, toUnicode, errors, ceh); + toTM, toUnicode, errors, ceh, + unicodeFont); } if (!ceh.errorReturn && pl != getParagraphs(begin, finalEndPos.getOffset()).length) { @@ -742,7 +749,8 @@ public class TibetanDocument extends DefaultStyledDocument { /** See the sole caller, convertHelper. */ private void convertHelperHelper(int begin, int end, boolean toTM, boolean toUnicode, StringBuffer errors, - ConversionErrorHelper ceh) { + ConversionErrorHelper ceh, + String unicodeFont) { final boolean debug = false; if (debug) System.err.println("cHH: [" + begin + ", " + end + ")"); @@ -835,7 +843,8 @@ public class TibetanDocument extends DefaultStyledDocument { replaceDuffsWithUnicode(replacementFontSize, replacementStartIndex, endIndex, - replacementQueue.toString()); + replacementQueue.toString(), + unicodeFont); } else { replaceDuffs(replacementFontSize, replacementStartIndex, @@ -937,7 +946,8 @@ public class TibetanDocument extends DefaultStyledDocument { replaceDuffsWithUnicode(replacementFontSize, replacementStartIndex, endIndex, - replacementQueue.toString()); + replacementQueue.toString(), + unicodeFont); } else { replaceDuffs(replacementFontSize, replacementStartIndex, diff --git a/source/org/thdl/tib/text/TibetanMachineWeb.java b/source/org/thdl/tib/text/TibetanMachineWeb.java index 4374684..de16c9b 100644 --- a/source/org/thdl/tib/text/TibetanMachineWeb.java +++ b/source/org/thdl/tib/text/TibetanMachineWeb.java @@ -76,7 +76,7 @@ public class TibetanMachineWeb implements THDLWylieConstants { private static final String DELIMITER = "~"; private static Set top_vowels; /** 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 that family of 10 fonts: */ private static SimpleAttributeSet[] webFontAttributeSet = new SimpleAttributeSet[11]; @@ -283,10 +283,9 @@ public class TibetanMachineWeb implements THDLWylieConstants { readInTMFontFiles(); } - unicodeFontAttributeSet = new SimpleAttributeSet(); - StyleConstants.setFontFamily(unicodeFontAttributeSet, - ThdlOptions.getStringOption("thdl.tmw.to.unicode.font", - "Arial Unicode MS")); + defaultUnicodeFontAttributeSet = new SimpleAttributeSet(); + StyleConstants.setFontFamily(defaultUnicodeFontAttributeSet, + "Arial Unicode MS"); webFontAttributeSet[0] = null; for (int i=1; iUnicode conversion. This information is required in * order to be able to put styled text into {@link 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 * of encoding the font itself */ -public static SimpleAttributeSet getUnicodeAttributeSet() { - return unicodeFontAttributeSet; +public static SimpleAttributeSet getUnicodeAttributeSet(String unicodeFont) { + 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. @@ -1008,25 +1022,25 @@ public static DuffCode mapTMtoTMW(int font, int ordinal, int suggestedFont) { if (0 == suggestedFont) return TMW_cr; 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') { if (0 == suggestedFont) return TMW_lf; 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') { if (0 == suggestedFont) return TMW_tab; 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 { // for robustness, just return font 1, char ordinal. ThdlDebug.noteIffyCode(); return null; } } - if (0 != suggestedFont && 32 == ordinal || 45 == ordinal) { - return new DuffCode(suggestedFont, (char)ordinal); + if ((0 != suggestedFont) && (32 == ordinal || 45 == ordinal)) { + return new DuffCode(suggestedFont, (char)ordinal); // FIXME: don't create a new one each time; it wastes heap } return TMtoTMW[font][ordinal-32]; } @@ -1061,25 +1075,25 @@ public static DuffCode mapTMWtoTM(int font, int ordinal, int suggestedFont) { if (0 == suggestedFont) return TM_cr; 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') { if (0 == suggestedFont) return TM_lf; 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') { if (0 == suggestedFont) return TM_tab; 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 { // for robustness, just return font 1, char ordinal. ThdlDebug.noteIffyCode(); return null; } } - if (0 != suggestedFont && 32 == ordinal || 45 == ordinal) { - return new DuffCode(suggestedFont, (char)ordinal); + if ((0 != suggestedFont) && (32 == ordinal || 45 == 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]; return ans;