From 370801a0875f1e48a46fe2652104c119cfea9e2a Mon Sep 17 00:00:00 2001 From: megacz Date: Fri, 30 Jan 2004 07:00:21 +0000 Subject: [PATCH] 2003/05/12 05:10:28 darcs-hash:20040130070021-2ba56-251cc43991738f8e8016e7da2beac2ba9cf6395a.gz --- src/org/xwt/builtin/edit_lib.xwt | 680 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 680 insertions(+) create mode 100644 src/org/xwt/builtin/edit_lib.xwt diff --git a/src/org/xwt/builtin/edit_lib.xwt b/src/org/xwt/builtin/edit_lib.xwt new file mode 100644 index 0000000..abcc8eb --- /dev/null +++ b/src/org/xwt/builtin/edit_lib.xwt @@ -0,0 +1,680 @@ + + + + var cursors = []; + var worddivider = [' ', '-']; + + xwt.thread = function() { + while (true) { xwt.sleep(1000); for (var i=0; cursors.length > i; i++) { cursors[i].blink = !cursors[i].blink; } } + } + + // Returns the number of characters the pixel position pos is into text t. end is the pixel with of t. + // t : string -- basis for character counting. + // pxpos : int -- pixel position on the text from string t. + // pxlen : int -- pixel width of text t (known in form of box length, so passed for speed). + // tfont : string -- name of font used for width of text. + var getpos = function(t, pxpos, pxlen, tfont) { + // Short circuit extremes. + if (0 >= pxpos) return 0; + if (pxpos >= pxlen) return t.length; + + // Inital guess based on average character width. + var guessch = xwt.math.min(t.length, xwt.math.floor(pxpos / (pxlen / t.length))); + var guesspx = xwt.textwidth(tfont, t.substring(0, guessch)); + + if (guesspx > pxpos) { + while (guesspx > pxpos) { + // Textwidth of individual character must account for font kerning. + guesspx -= xwt.textwidth(tfont, t.substring(guessch -1, guessch +1)) - xwt.textwidth(tfont, t.charAt(guessch)); + guessch--; + } + } else if (pxpos > guesspx) { + while (pxpos > guesspx) { + guessch++; + if (guessch >= t.length) break; + guesspx += xwt.textwidth(tfont, t.substring(guessch -1, guessch+1)) - xwt.textwidth(tfont, t.charAt(guessch)); + } + guessch--; // Round down. + } + + return guessch; + } + + + + + -- 1.7.10.4