Factored TibetanDocument into two classes, one that is a

DefaultStyledDocument, and another consisting entirely of static utility
methods for processing Tibetan text.  Moved TibetanDocument.DuffData
into its own class.

I think this makes things a bit more transparent, and gets us a little closer to
making clean use of Swing.
This commit is contained in:
dchandler 2002-11-02 03:38:59 +00:00
parent 5249c48807
commit abcf8f19b3
25 changed files with 1490 additions and 1337 deletions

View file

@ -25,7 +25,9 @@ import javax.swing.border.*;
import javax.swing.text.*;
import org.thdl.tib.input.DuffPane;
import org.thdl.tib.text.*;
import org.thdl.tib.text.TibetanDocument.DuffData;
import org.thdl.tib.text.DuffData;
import org.thdl.util.ThdlDebug;
import java.io.Serializable;
/** Used by DictionaryTable to display a Tibetan word or phrase
@ -135,6 +137,7 @@ public class DuffCellRenderer extends DuffPane implements TableCellRenderer, Ser
catch (Exception e)
{
System.out.println(e);
org.thdl.util.ThdlDebug.noteIffyCode();
}
doc.insertDuff(0, (DuffData []) value);
}

View file

@ -0,0 +1,53 @@
/*
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
Library (THDL). Portions created by the THDL are Copyright 2001 THDL.
All Rights Reserved.
Contributor(s): ______________________________________.
*/
package org.thdl.tib.text;
/**
* A wrapper object for a stretch of TibetanMachineWeb data that shares the same font.
* A piece of DuffData consists of a font number and a string.
* The font number is a number from one to ten, corresponding
* to the ten TibetanMachineWeb fonts, as follows:
* <p>
* 1 - TibetanMachineWeb<br>
* 2 - TibetanMachineWeb1<br>
* ...<br>
* 10 - TibetanMachineWeb9<br>
* <p>
* The string represents a contiguous stretch of data in that
* font, i.e. a stretch of TibetanMachineWeb that doesn't require a font change.
*/
public class DuffData {
/**
* a string of text
*/
public String text;
/**
* the font number for this text (see class description)
*/
public int font;
/**
* @param s a string of TibetanMachineWeb text
* @param i a TibetanMachineWeb font number
*/
public DuffData(String s, int i) {
text = s;
font = i;
}
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,144 +1,225 @@
package org.thdl.tib.text;
public class TibetanHTML {
static String[] styleNames =
{"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"};
public static String getStyles(String fontSize) {
return ".tmw {font: "+fontSize+"pt TibetanMachineWeb}\n"+
".tmw1 {font: "+fontSize+"pt TibetanMachineWeb1}\n"+
".tmw2 {font: "+fontSize+"pt TibetanMachineWeb2}\n"+
".tmw3 {font: "+fontSize+"pt TibetanMachineWeb3}\n"+
".tmw4 {font: "+fontSize+"pt TibetanMachineWeb4}\n"+
".tmw5 {font: "+fontSize+"pt TibetanMachineWeb5}\n"+
".tmw6 {font: "+fontSize+"pt TibetanMachineWeb6}\n"+
".tmw7 {font: "+fontSize+"pt TibetanMachineWeb7}\n"+
".tmw8 {font: "+fontSize+"pt TibetanMachineWeb8}\n"+
".tmw9 {font: "+fontSize+"pt TibetanMachineWeb9}\n";
}
public static String getHTML(String wylie) {
try {
return getHTML(TibetanDocument.getTibetanMachineWeb(wylie));
}
catch (InvalidWylieException ive) {
return null;
}
}
public static String getHTML(TibetanDocument.DuffData[] duffData) {
String[] styleNames =
{"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"};
StringBuffer htmlBuffer = new StringBuffer();
htmlBuffer.append("<nobr>");
for (int i=0; i<duffData.length; i++) {
char[] c = duffData[i].text.toCharArray();
for (int k=0; k<c.length; k++) {
htmlBuffer.append("<span class=\"");
htmlBuffer.append(styleNames[duffData[i].font-1]);
htmlBuffer.append("\">");
switch (c[k]) {
case '"':
htmlBuffer.append("&quot");
break;
case '<':
htmlBuffer.append("&lt");
break;
case '>':
htmlBuffer.append("&gt");
break;
case '&':
htmlBuffer.append("&amp");
break;
default:
htmlBuffer.append(c[k]);
break;
}
htmlBuffer.append("</span>");
if (c[k] < 32) //must be formatting, like carriage return or tab
htmlBuffer.append("<br>");
else {
String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k]);
if (TibetanMachineWeb.isWyliePunc(wylie))
htmlBuffer.append("<wbr>");
}
}
}
htmlBuffer.append("</nobr>");
return htmlBuffer.toString();
}
}
//import org.apache.xerces.dom.DOMImplementationImpl;
//import org.w3c.dom.*;
/*
public static Node getHTML(String wylie) {
try {
return getHTML(TibetanDocument.getTibetanMachineWeb(wylie));
}
catch (InvalidWylieException ive) {
return null;
}
}
public static Node getHTML(TibetanDocument.DuffData[] duffData) {
try {
DOMImplementationImpl impl = new DOMImplementationImpl();
Document doc = impl.createDocument(null, "root", null);
// DocumentFragment frag = doc.createDocumentFragment()
Element nobr = doc.createElement("nobr");
// frag.appendChild(nobr);
for (int i=0; i<duffData.length; i++) {
char[] c = duffData[i].text.toCharArray();
for (int k=0; k<c.length; k++) {
Element span = doc.createElement("span");
span.setAttribute("class", styleNames[duffData[i].font-1]);
// Text tib = doc.createTextNode(String.valueOf(c[k]));
// span.appendChild(tib);
nobr.appendChild(span);
String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k]);
if (TibetanMachineWeb.isWyliePunc(wylie)) {
Element wbr = doc.createElement("wbr");
nobr.appendChild(wbr);
}
}
}
//doc.appendChild(nobr);
return nobr;
// return frag;
}
catch (DOMException dome) {
switch (dome.code) {
case DOMException.HIERARCHY_REQUEST_ERR:
System.out.println("hierarchy error!!");
break;
case DOMException.WRONG_DOCUMENT_ERR:
System.out.println("wrong doc error!!");
break;
case DOMException.NO_MODIFICATION_ALLOWED_ERR:
System.out.println("no mod allowed error!!");
break;
}
return null;
}
}
*/
package org.thdl.tib.text;
import java.util.StringTokenizer;
public class TibetanHTML {
static String[] styleNames =
{"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"};
public static String getStyles(String fontSize) {
return ".tmw {font: "+fontSize+"pt TibetanMachineWeb}\n"+
".tmw1 {font: "+fontSize+"pt TibetanMachineWeb1}\n"+
".tmw2 {font: "+fontSize+"pt TibetanMachineWeb2}\n"+
".tmw3 {font: "+fontSize+"pt TibetanMachineWeb3}\n"+
".tmw4 {font: "+fontSize+"pt TibetanMachineWeb4}\n"+
".tmw5 {font: "+fontSize+"pt TibetanMachineWeb5}\n"+
".tmw6 {font: "+fontSize+"pt TibetanMachineWeb6}\n"+
".tmw7 {font: "+fontSize+"pt TibetanMachineWeb7}\n"+
".tmw8 {font: "+fontSize+"pt TibetanMachineWeb8}\n"+
".tmw9 {font: "+fontSize+"pt TibetanMachineWeb9}\n";
}
public static String getHTMLX(String wylie) {
try {
StringBuffer buffer = new StringBuffer();
for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) {
String next = tokenizer.nextToken();
if (next.equals("\t") || next.equals("\n")) {
buffer.append("<wbr/>");
buffer.append(getHTML(TibTextUtils.getTibetanMachineWeb("_")));
buffer.append("<wbr/>");
}
else
buffer.append(getHTML(TibTextUtils.getTibetanMachineWeb(next)));
}
return buffer.toString();
} catch (InvalidWylieException ive) {
return "";
}
}
public static String getHTMLX(DuffData[] duffData) {
String[] styleNames =
{"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"};
StringBuffer htmlBuffer = new StringBuffer();
htmlBuffer.append("<nobr>");
for (int i=0; i<duffData.length; i++) {
char[] c = duffData[i].text.toCharArray();
for (int k=0; k<c.length; k++) {
htmlBuffer.append("<span class=\"");
htmlBuffer.append(styleNames[duffData[i].font-1]);
htmlBuffer.append("\">");
if (c[k] > 32 && c[k] < 127) { //ie if it's not formatting
switch (c[k]) {
case '"':
htmlBuffer.append("&quot;");
break;
case '<':
htmlBuffer.append("&lt;");
break;
case '>':
htmlBuffer.append("&gt;");
break;
case '&':
htmlBuffer.append("&amp;");
break;
default:
htmlBuffer.append(c[k]);
break;
}
htmlBuffer.append("</span>");
String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k]);
if (TibetanMachineWeb.isWyliePunc(wylie))
htmlBuffer.append("<wbr/>");
} else {
htmlBuffer.append("</span><br/>");
}
}
}
htmlBuffer.append("</nobr>");
return htmlBuffer.toString();
}
public static String getIndentedHTML(String wylie) {
return getHTML("_" + wylie);
}
public static String getHTML(String wylie) {
try {
StringBuffer buffer = new StringBuffer();
for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) {
String next = tokenizer.nextToken();
if (next.equals("\t") || next.equals("\n")) {
buffer.append("<wbr/>");
buffer.append(getHTML(TibTextUtils.getTibetanMachineWeb("_")));
buffer.append("<wbr/>");
}
else
buffer.append(getHTML(TibTextUtils.getTibetanMachineWeb(next)));
}
return buffer.toString();
} catch (InvalidWylieException ive) {
return "";
}
}
public static String getHTML(DuffData[] duffData) {
String[] styleNames =
{"tmw","tmw1","tmw2","tmw3","tmw4","tmw5","tmw6","tmw7","tmw8","tmw9"};
StringBuffer htmlBuffer = new StringBuffer();
htmlBuffer.append("<nobr>");
for (int i=0; i<duffData.length; i++) {
htmlBuffer.append("<span class=\"");
htmlBuffer.append(styleNames[duffData[i].font-1]);
htmlBuffer.append("\">");
char[] c = duffData[i].text.toCharArray();
for (int k=0; k<c.length; k++) {
if (c[k] > 31 && c[k] < 127) { //ie if it's not formatting
switch (c[k]) {
case '"':
htmlBuffer.append("&quot;");
break;
case '<':
htmlBuffer.append("&lt;");
break;
case '>':
htmlBuffer.append("&gt;");
break;
case '&':
htmlBuffer.append("&amp;");
break;
default:
htmlBuffer.append(c[k]);
break;
}
String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k]);
if (TibetanMachineWeb.isWyliePunc(wylie))
htmlBuffer.append("<wbr/>");
} else {
htmlBuffer.append("<br/>");
}
}
htmlBuffer.append("</span>");
}
htmlBuffer.append("</nobr>");
return htmlBuffer.toString();
}
public static String getHTMLforJava(String wylie) {
//differences:
// as of 1.4.1, anyway, browser built into java does not accept <wbr/> and <br/>,
// only <wbr> and <br>
try {
StringBuffer buffer = new StringBuffer();
for (StringTokenizer tokenizer = new StringTokenizer(wylie, " \t\n", true); tokenizer.hasMoreElements();) {
String next = tokenizer.nextToken();
if (next.equals("\t") || next.equals("\n")) {
buffer.append("<wbr>");
buffer.append(getHTML(TibTextUtils.getTibetanMachineWeb("_")));
buffer.append("<wbr>");
}
else
buffer.append(getHTML(TibTextUtils.getTibetanMachineWeb(next)));
}
return buffer.toString();
} catch (InvalidWylieException ive) {
return "";
}
}
public static String getHTMLforJava(DuffData[] duffData) {
String[] fontNames = {
"TibetanMachineWeb","TibetanMachineWeb1", "TibetanMachineWeb2",
"TibetanMachineWeb3","TibetanMachineWeb4","TibetanMachineWeb5",
"TibetanMachineWeb6","TibetanMachineWeb7","TibetanMachineWeb8",
"TibetanMachineWeb9"};
StringBuffer htmlBuffer = new StringBuffer();
htmlBuffer.append("<nobr>");
for (int i=0; i<duffData.length; i++) {
htmlBuffer.append("<font size=\"36\" face=\"");
htmlBuffer.append(fontNames[duffData[i].font-1]);
htmlBuffer.append("\">");
char[] c = duffData[i].text.toCharArray();
for (int k=0; k<c.length; k++) {
if (c[k] > 31 && c[k] < 127) { //ie if it's not formatting
switch (c[k]) {
case '"':
htmlBuffer.append("&quot;");
break;
case '<':
htmlBuffer.append("&lt;");
break;
case '>':
htmlBuffer.append("&gt;");
break;
case '&':
htmlBuffer.append("&amp;");
break;
default:
htmlBuffer.append(c[k]);
break;
}
String wylie = TibetanMachineWeb.getWylieForGlyph(duffData[i].font, c[k]);
if (TibetanMachineWeb.isWyliePunc(wylie))
htmlBuffer.append("<wbr>");
} else {
htmlBuffer.append("<br>");
}
}
htmlBuffer.append("</font>");
}
htmlBuffer.append("</nobr>");
return htmlBuffer.toString();
}
}

View file

@ -126,14 +126,14 @@ public class TibetanQTText {
public static String getQTText(String wylie) {
try {
return getQTText(TibetanDocument.getTibetanMachineWeb(wylie));
return getQTText(TibTextUtils.getTibetanMachineWeb(wylie));
}
catch (InvalidWylieException ive) {
return null;
}
}
public static String getQTText(TibetanDocument.DuffData[] duffData) {
public static String getQTText(DuffData[] duffData) {
StringBuffer qtBuffer = new StringBuffer();
qtBuffer.append("{size:" + tibFontSize + "}");

View file

@ -126,14 +126,14 @@ public class TibetanQTText2 {
public static String getQTText(String wylie) {
try {
return getQTText(TibetanDocument.getTibetanMachineWeb(wylie));
return getQTText(TibTextUtils.getTibetanMachineWeb(wylie));
}
catch (InvalidWylieException ive) {
return null;
}
}
public static String getQTText(TibetanDocument.DuffData[] duffData) {
public static String getQTText(DuffData[] duffData) {
StringBuffer qtBuffer = new StringBuffer();
qtBuffer.append("{size:" + tibFontSize + "}");