package com.roiding.rdict.dict;
import java.io.InputStream;
import java.util.Vector;
public class FileTextView {
public class ViewInfo {
public int fontId = 0;
public int flagId = 0;
public int lineNum = 0;
public int offsetX = 0;
public int startPos = 0;
public int endPos = 0;
};
private final int MIN_FORMAT_CODE = 0xF800;
private final int DICT_FONTEND = MIN_FORMAT_CODE;
private final int DICT_FONTYINBIAO = MIN_FORMAT_CODE + 1;
private final int DICT_FONTBOLD = MIN_FORMAT_CODE + 2;
private final int DICT_FONTGLIUM = MIN_FORMAT_CODE + 3;
private final int DICT_FONTGLMRS = MIN_FORMAT_CODE + 4;
private final int DICT_FONTITAL = MIN_FORMAT_CODE + 5;
private final int DICT_FONTSUPER = MIN_FORMAT_CODE + 6;
private final int DICT_FONTSUB = MIN_FORMAT_CODE + 7;
private final int DICT_FONTEXAMPLE = MIN_FORMAT_CODE + 8;
private final int DICT_FLAGCATEGORY = MIN_FORMAT_CODE + 9;
private final int DICT_FLAGEPLID = MIN_FORMAT_CODE + 10;
private final int DICT_FLAGEXAMPLE = MIN_FORMAT_CODE + 11;
private final int DICT_FLAGFILED = MIN_FORMAT_CODE + 12;
private final int DICT_FLAGGRAM = MIN_FORMAT_CODE + 13;
private final int DICT_FLAGINFLEC = MIN_FORMAT_CODE + 14;
private final int DICT_FLAGLANG = MIN_FORMAT_CODE + 15;
private final int DICT_FLAGPHOPLAIN = MIN_FORMAT_CODE + 16;
private final int DICT_FLAGPHRASE = MIN_FORMAT_CODE + 17;
private final int DICT_FLAGPOS = MIN_FORMAT_CODE + 18;
private final int DICT_FLAGSELF = MIN_FORMAT_CODE + 19;
private final int DICT_FLAGSENSEID = MIN_FORMAT_CODE + 20;
private final int DICT_FLAGSTYLE = MIN_FORMAT_CODE + 21;
private final int DICT_FLAGUDDOT = MIN_FORMAT_CODE + 22;
private final int DICT_FLAGUDLINE = MIN_FORMAT_CODE + 23;
private final int DICT_FLAGVARIANT = MIN_FORMAT_CODE + 24;
private final int DICT_FONTPINYIN = MIN_FORMAT_CODE + 25;
private final int DICT_FONTPYREF = MIN_FORMAT_CODE + 26;
private final int DICT_FONTTITLE = MIN_FORMAT_CODE + 27;
private final int DICT_FONTUNDEFINE = MIN_FORMAT_CODE + 28;
private final int DICT_FLAGUNDEFINE = MIN_FORMAT_CODE + 29;
private final int DICT_FLAGENET = MIN_FORMAT_CODE + 30;
private int WIDTH;
private int HEIGHT;
private int LEFT;
private int TOP;
private final int LINEHIGHT = 14;
// private final int FONTSIZE = 12;
private final int BUFSIZE = 512;
private byte tmpByteBuf[];
private StringBuffer tmpStrBuf;
private StringBuffer displayStrBuf;
public int currentPageIndex = 0;
public int totalPages = 0;
private int startPageIndex = 0;
private int endPageIndex = 0;
private AppUtility appUtility = null;
private Vector view = null;
private int nLines = 1;
private int screenLines = 0;
private int offsetX = 0;
private final int boundarySpace = 3;
private int iconSize = 18;
private int scrollBarX = 0;
private int scrollBarY = 0;
private int scrollBarH = 0;
public boolean upRepeated = false;
public boolean downRepeated = false;
// private Image imgDown = null;
//
// private Image imgDown1 = null;
//
// private Image imgDown2 = null;
//
// private Image imgUp = null;
//
// private Image imgUp1 = null;
//
// private Image imgUp2 = null;
private guiFont font = null;
private GUI_CHARINFO charInfo = null;
// private fileLog log = null;
private DictFile myFile = null;
private int dataBytes = 0;
private int dataOffset = 0;
private int currentPageOffset;
private Pronounce pr;
// private _PRONOUCETWOBJ pObj = new _PRONOUCETWOBJ();
public FileTextView(int width, int height, int x, int y, Pronounce pr) {
// log = new fileLog("contentView.txt");
// log.traceString("contentView() -- > begin");
this.pr = pr;
tmpByteBuf = new byte[BUFSIZE];
tmpStrBuf = new StringBuffer();
displayStrBuf = new StringBuffer();
appUtility = AppUtility.getInstance();
WIDTH = width - 2 * boundarySpace - iconSize;
HEIGHT = height - 2 * boundarySpace;
LEFT = x + boundarySpace;
TOP = y + boundarySpace;
screenLines = HEIGHT / LINEHIGHT;
view = new Vector();
font = guiFont.getInstance();
font.initGuiFont();
scrollBarX = x + width - iconSize;
scrollBarY = y;
scrollBarH = height;
try {
// imgDown = Image.createImage("/tup/imgDown.png");
// imgDown1 = Image.createImage("/tup/imgDown1.png");
// imgDown2 = Image.createImage("/tup/imgDown2.png");
// imgUp = Image.createImage("/tup/imgUp.png");
// imgUp1 = Image.createImage("/tup/imgUp1.png");
// imgUp2 = Image.createImage("/tup/imgUp2.png");
} catch (Exception e) {
System.out.print(e.toString());
}
}
public void setFileInfo(DictFile file, int total, int offset) {
myFile = file;
dataBytes = total;
dataOffset = offset;
tmpStrBuf = new StringBuffer();
CreateFileView();
nLines++;
currentPageIndex = 0;
totalPages = nLines / screenLines;
if (nLines % screenLines > 0) {
totalPages += 1;
}
GetPageViewDataByIndex(currentPageIndex);
}
public void goToNextPage() {
if (currentPageIndex < totalPages - 1) {
currentPageIndex++;
GetPageViewDataByIndex(currentPageIndex);
}
}
public void goToLastPage() {
if (currentPageIndex > 0) {
currentPageIndex--;
GetPageViewDataByIndex(currentPageIndex);
}
}
public void goToPage(int index) {
GetPageViewDataByIndex(index);
}
public StringBuffer drawView() {
StringBuffer result = DrawPageByIndex(currentPageIndex);
int startLine = currentPageIndex * screenLines;
drawScroll(startLine);
return result;
}
private void GetPageViewDataByIndex(int aPageIndex) {
// log.traceString("GetPageViewDataByIndex----->start");
int startLineNum = aPageIndex * screenLines;
int endLineNum = (aPageIndex + 1) * screenLines;
int startPos = -1;
int endPos = -1;
startPageIndex = -1;
endPageIndex = -1;
for (int i = 0; i < view.size(); i++) {
//
ViewInfo viewItem = null;
viewItem = (ViewInfo) view.elementAt(i);
if (startLineNum == viewItem.lineNum) {
//
if (startPageIndex == -1) {
startPageIndex = i;
}
if ((startPos == -1) && (viewItem.startPos != -1)) {
startPos = viewItem.startPos;
}
}
//
if (endLineNum == viewItem.lineNum) {
if (endPageIndex == -1) {
endPageIndex = i;
}
if ((endPos == -1) && (viewItem.endPos != -1)) {
endPos = viewItem.endPos;
break;
}
}
}
//
if (endPageIndex == -1) {
endPageIndex = view.size();
}
if (endPos == -1) {
endPos = dataBytes;
}
currentPageOffset = startPos;
int readSize = endPos - startPos;
if (readSize <= 0)
return;
tmpByteBuf = new byte[readSize];
InputStream is = null;
try {
is = myFile.getIn();
SkipInFile(is, dataOffset + currentPageOffset);
is.read(tmpByteBuf, 0, readSize);
is.close();
} catch (Exception e) {
// log.traceString(e.toString());
System.out.println(e.toString());
return;
}
if (displayStrBuf.length() > 0) {
displayStrBuf.delete(0, displayStrBuf.length());
displayStrBuf = new StringBuffer();
}
int encodeOffset = dataOffset + currentPageOffset;
pr.Pron_addRandomTable(tmpByteBuf, tmpByteBuf.length, encodeOffset,
pr.g_pObj.pMObj.FileInfo.EncodeData);
appUtility.stringBufAppendByte(displayStrBuf, tmpByteBuf, readSize);
// log.traceStringBuffer(displayStrBuf);
// font.clearCharCache(displayStrBuf.length());
// for (int i = 0; i < displayStrBuf.length(); i++) {
// font.creatCharMap(displayStrBuf.charAt(i));
// }
// font.creatCharImg();
// log.traceString("GetPageViewDataByIndex----->end");
}
public int getViewLines() {
return nLines;
}
private boolean bHasScrollBar() {
if (nLines > screenLines) {
return true;
} else {
return false;
}
}
public int getScreenLine() {
return screenLines;
}
private void SkipInFile(InputStream is, long size) {
long realSkipSize = 0;
while (size > 0) {
try {
realSkipSize = is.skip(size);
} catch (Exception e) {
System.out.println(e.toString());
}
size -= realSkipSize;
}
}
private void CreateFileView() {
// log.traceString("CreateFileView----->start");
nLines = 0;
offsetX = 0;
view.removeAllElements();
int offsetInFile = 0;
int curFontId = DICT_FONTUNDEFINE;
int curFlagId = DICT_FLAGUNDEFINE;
int curLever = 0;
String str = new String();
InputStream is = null;
boolean first = false;
try {
is = myFile.getIn();// System.class.getResourceAsStream(g_dict.
// fileInfo.pfCurWord);
SkipInFile(is, dataOffset);
} catch (Exception e) {
// log.traceString(e.toString());
System.out.println(e.toString());
return;
}
int start = 0;
while (offsetInFile < dataBytes - BUFSIZE) {
first = true;
tmpByteBuf = new byte[BUFSIZE];
try {
is.read(tmpByteBuf, 0, tmpByteBuf.length);
} catch (Exception e) {
// log.traceString(e.toString());
System.out.println(e.toString());
return;
}
if (tmpStrBuf.length() > 0) {
// log.traceString("if (tmpStrBuf.length() > 0)AAAAAAAAaaaaaa");
str = tmpStrBuf.toString().substring(start);
tmpStrBuf = new StringBuffer();
tmpStrBuf.append(str);
}
int encodeOffset = dataOffset;
pr.Pron_addRandomTable(tmpByteBuf, tmpByteBuf.length, encodeOffset,
pr.g_pObj.pMObj.FileInfo.EncodeData);
StringBuffer buf = new StringBuffer();
appUtility.stringBufAppendByte(buf, tmpByteBuf, tmpByteBuf.length);
// log.traceStringBuffer(buf);
tmpStrBuf.append(buf);
int i = 0;
start = 0;
int size = BUFSIZE / 2 + str.length() > tmpStrBuf.length() ? tmpStrBuf
.length()
: BUFSIZE / 2 + str.length();
for (i = 0; i < size; i++) {
switch (tmpStrBuf.charAt(i)) {
case 0xFEFF:
start = i + 1;
break;
case 0x0A:
AddViewInfo(offsetInFile, start, i, curFontId, curFlagId);
AddNewLineInView();
start = i + 1;
break;
case DICT_FONTEND:
AddViewInfo(offsetInFile, start, i, curFontId, curFlagId);
curLever--;
if (curLever == 1) {
curFlagId = DICT_FLAGUNDEFINE;
} else if (curLever == 0) {
curFontId = DICT_FONTUNDEFINE;
}
start = i + 1;
break;
case DICT_FONTYINBIAO:
case DICT_FONTBOLD:
case DICT_FONTGLIUM:
case DICT_FONTGLMRS:
case DICT_FONTITAL:
case DICT_FONTSUPER:
case DICT_FONTSUB:
case DICT_FONTEXAMPLE:
case DICT_FONTPINYIN:
case DICT_FONTPYREF:
case DICT_FONTTITLE:
case DICT_FONTUNDEFINE:
// log.traceString("case DICT_FONTYINBIAO:");
//
if (i > start) {
AddViewInfo(offsetInFile, start, i, curFontId,
curFlagId);
}
//
curFontId = tmpStrBuf.charAt(i);
curLever++;
start = i + 1;
break;
case DICT_FLAGCATEGORY:
case DICT_FLAGEPLID:
case DICT_FLAGEXAMPLE:
case DICT_FLAGFILED:
case DICT_FLAGGRAM:
case DICT_FLAGINFLEC:
case DICT_FLAGLANG:
case DICT_FLAGPHOPLAIN:
case DICT_FLAGPHRASE:
case DICT_FLAGPOS:
case DICT_FLAGSELF:
case DICT_FLAGSENSEID:
case DICT_FLAGSTYLE:
case DICT_FLAGUDDOT:
case DICT_FLAGUDLINE:
case DICT_FLAGVARIANT:
case DICT_FLAGUNDEFINE:
//
if (i > start) {
AddViewInfo(offsetInFile, start, i, curFontId,
curFlagId);
}
//
curFlagId = tmpStrBuf.charAt(i);
curLever++;
start = i + 1;
break;
default:
break;
}// end of switch
}// end of for(i = 0; i < KMaxBufSize/2; i++)
if (start == 0) {
AddViewInfo(offsetInFile, start, i, curFontId, curFlagId);
start = BUFSIZE / 2;
}
offsetInFile += 2 * start;
}// end of while( leftLen > KMaxBufSize )
//
int leftLen = dataBytes - offsetInFile;
if (leftLen < 0)
return;
tmpByteBuf = new byte[leftLen];
try {
is.read(tmpByteBuf, 0, tmpByteBuf.length);
is.close();
is = null;
} catch (Exception e) {
// log.traceString(e.toString());
System.out.println(e.toString());
return;
}
if (tmpStrBuf.length() > 0) {
if (first == true) {
// log.traceString("if (first == true)");
str = tmpStrBuf.toString().substring(start);
tmpStrBuf = new StringBuffer();
tmpStrBuf.append(str);
first = false;
} else {
// log.traceString("if (first == true)else");
tmpStrBuf = new StringBuffer();
first = false;
}
}
int encodeOffset = dataOffset;
pr.Pron_addRandomTable(tmpByteBuf, tmpByteBuf.length, encodeOffset,
pr.g_pObj.pMObj.FileInfo.EncodeData);
StringBuffer buf = new StringBuffer();
appUtility.stringBufAppendByte(buf, tmpByteBuf, leftLen);
// log.traceStringBuffer(buf);
tmpStrBuf.append(buf);
int i = 0;
start = 0;
int size = leftLen / 2 + str.length() > tmpStrBuf.length() ? tmpStrBuf
.length() : leftLen / 2 + str.length();
for (i = 0; i < size; i++) {
switch (tmpStrBuf.charAt(i)) {
case 0x0A:
AddViewInfo(offsetInFile, start, i, curFontId, curFlagId);
AddNewLineInView();
start = i + 1;
break;
case DICT_FONTEND:
AddViewInfo(offsetInFile, start, i, curFontId, curFlagId);
curLever--;
if (curLever == 1) {
curFlagId = DICT_FLAGUNDEFINE;
} else if (curLever == 0) {
curFontId = DICT_FONTUNDEFINE;
}
start = i + 1;
break;
case DICT_FONTYINBIAO:
case DICT_FONTBOLD:
case DICT_FONTGLIUM:
case DICT_FONTGLMRS:
case DICT_FONTITAL:
case DICT_FONTSUPER:
case DICT_FONTSUB:
case DICT_FONTEXAMPLE:
case DICT_FONTPINYIN:
case DICT_FONTPYREF:
case DICT_FONTTITLE:
case DICT_FONTUNDEFINE:
// log.traceString("case DICT_FONTYINBIAO:");
//
if (i > start) {
AddViewInfo(offsetInFile, start, i, curFontId, curFlagId);
}
//
curFontId = tmpStrBuf.charAt(i);
curLever++;
start = i + 1;
break;
case DICT_FLAGCATEGORY:
case DICT_FLAGEPLID:
case DICT_FLAGEXAMPLE:
case DICT_FLAGFILED:
case DICT_FLAGGRAM:
case DICT_FLAGINFLEC:
case DICT_FLAGLANG:
case DICT_FLAGPHOPLAIN:
case DICT_FLAGPHRASE:
case DICT_FLAGPOS:
case DICT_FLAGSELF:
case DICT_FLAGSENSEID:
case DICT_FLAGSTYLE:
case DICT_FLAGUDDOT:
case DICT_FLAGUDLINE:
case DICT_FLAGVARIANT:
case DICT_FLAGUNDEFINE:
//
if (i > start) {
AddViewInfo(offsetInFile, start, i, curFontId, curFlagId);
}
//
curFlagId = tmpStrBuf.charAt(i);
curLever++;
start = i + 1;
break;
default:
break;
}// end of switch
if (start == size) {
break;
}
}// end of for(i = 0; i < leftLen/2; i++)
if (i > start) {
AddViewInfo(offsetInFile, start, i, curFontId, curFlagId);
}
// log.traceString("CreateFileView---->end");
return;
}
private void AddNewLineInView() {
offsetX = 0;
nLines++;
ViewInfo viewItem = null;
viewItem = new ViewInfo();
viewItem.fontId = 0;
viewItem.flagId = DICT_FLAGENET;
viewItem.lineNum = nLines;
viewItem.offsetX = offsetX;
viewItem.startPos = -1;
viewItem.endPos = -1;
view.addElement(viewItem);
}
private void AddViewInfo(int offset, int start, int end, int fontId,
int flagId) {
if (end <= start) {
return;
}
int len = offsetX;
int charNum = 0;
int start1 = start;
for (int i = 0; i < (end - start); i++) {
if (fontId != DICT_FONTYINBIAO) {
font.creatCharMap(tmpStrBuf.charAt(i + start));
}
charInfo = font
.getFontProperty(tmpStrBuf.charAt(i + start), fontId);
len += charInfo.XSize;
if (len > WIDTH) {
if (charNum > 0) {
ViewInfo viewItem = null;
viewItem = new ViewInfo();
viewItem.fontId = fontId;
viewItem.flagId = flagId;
viewItem.lineNum = nLines;
viewItem.offsetX = offsetX;
viewItem.startPos = start1 * 2 + offset;
viewItem.endPos = (start1 + charNum) * 2 + offset;
view.addElement(viewItem);
start1 += charNum;
}
offsetX = 0;
nLines++;
i--;
charNum = 0;
len = 0;
} else {
charNum++;
}
}
if (charNum > 0) {
ViewInfo viewItem = null;
viewItem = new ViewInfo();
viewItem.fontId = fontId;
viewItem.flagId = flagId;
viewItem.lineNum = nLines;
viewItem.offsetX = offsetX;
viewItem.startPos = start1 * 2 + offset;
viewItem.endPos = (start1 + charNum) * 2 + offset;
view.addElement(viewItem);
offsetX = len;
}
}
private StringBuffer DrawPageByIndex(int aPageIndex) {
// log.traceString("DrawPageByIndex-------------->start");
int startLine = aPageIndex * screenLines;
ViewInfo viewItem;
viewItem = (ViewInfo) view.elementAt(startPageIndex);
int offset = viewItem.startPos;
StringBuffer result = new StringBuffer();
for (int i = startPageIndex; i < endPageIndex; i++) {
// System.out.println("######################################## " +
// i);
result.append(DrawOneViewByIndex(offset, i, startLine));
}
// log.traceString("DrawPageByIndex--------->end");
return result;
}
private StringBuffer DrawOneViewByIndex(int offset, int aIndex,
int startLine) {
// log.traceString("DrawOneViewByIndex-------------start");
StringBuffer result = new StringBuffer();
ViewInfo viewItem;
viewItem = (ViewInfo) view.elementAt(aIndex);
if (viewItem.flagId == DICT_FLAGENET) {
return result;
} else if (viewItem.fontId == DICT_FONTYINBIAO) {
// g.setColor(0xff0000);
} else {
// g.setColor(0);
}
int x = viewItem.offsetX + LEFT;
int y = TOP + (viewItem.lineNum - startLine) * LINEHIGHT;
// System.out.println(displayStrBuf.toString());
for (int i = (viewItem.startPos - currentPageOffset) / 2; i < (viewItem.endPos - currentPageOffset) / 2; i++) {
if (i >= displayStrBuf.length() || i < 0)
break;
charInfo = font.getFontProperty(displayStrBuf.charAt(i),
viewItem.fontId);
font.drawFontImage(displayStrBuf.charAt(i), x, y);
x += charInfo.XSize;
charInfo = null;
// System.out.print(displayStrBuf.charAt(i));
char ch = displayStrBuf.charAt(i);
result.append(ch);
}
// log.traceString("DrawOneViewByIndex----------->end");
return result;
}
private void drawScroll(int startLine) {
// if (startLine <= 1) {
// g.drawImage(imgUp2, scrollBarX, scrollBarY, Graphics.TOP
// | Graphics.LEFT);
//
// } else {
// g.drawImage(imgUp, scrollBarX, scrollBarY, Graphics.TOP
// | Graphics.LEFT);
// }
// if (bHasScrollBar()) {
// g.drawImage(imgDown, scrollBarX,
// scrollBarY + scrollBarH - iconSize, Graphics.TOP
// | Graphics.LEFT);
// }
// if (startLine + screenLines >= nLines)
// g.drawImage(imgDown2, scrollBarX, scrollBarY + scrollBarH
// - iconSize, Graphics.TOP | Graphics.LEFT);
// if (upRepeated) {
// g.drawImage(imgUp1, scrollBarX, scrollBarY, Graphics.TOP
// | Graphics.LEFT);
// }
// if (downRepeated) {
// g.drawImage(imgDown1, scrollBarX, scrollBarY + scrollBarH
// - iconSize, Graphics.TOP | Graphics.LEFT);
// }
// g.drawRect(LEFT - boundarySpace, scrollBarY, WIDTH + 2
// * boundarySpace + iconSize, scrollBarH);
}
}
|