X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fcore%2Fbuiltin%2Fedit_lib.ibex;fp=src%2Forg%2Fibex%2Fcore%2Fbuiltin%2Fedit_lib.ibex;h=c842594e9d3e69e7034c860bff34d7b6ef2d14e9;hb=4daeeb4119b901d53b44913c86f8af3ce67db925;hp=0000000000000000000000000000000000000000;hpb=da1f843588c8bd2b2c7cc74a5b4ffff8d57ab712;p=org.ibex.core.git diff --git a/src/org/ibex/core/builtin/edit_lib.ibex b/src/org/ibex/core/builtin/edit_lib.ibex new file mode 100644 index 0000000..c842594 --- /dev/null +++ b/src/org/ibex/core/builtin/edit_lib.ibex @@ -0,0 +1,680 @@ + + + + var cursors = []; + var worddivider = [' ', '-']; + + ibex.thread = function() { + while (true) { ibex.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 = ibex.math.min(t.length, ibex.math.floor(pxpos / (pxlen / t.length))); + var guesspx = ibex.textwidth(tfont, t.substring(0, guessch)); + + if (guesspx > pxpos) { + while (guesspx > pxpos) { + // Textwidth of individual character must account for font kerning. + guesspx -= ibex.textwidth(tfont, t.substring(guessch -1, guessch +1)) - ibex.textwidth(tfont, t.charAt(guessch)); + guessch--; + } + } else if (pxpos > guesspx) { + while (pxpos > guesspx) { + guessch++; + if (guessch >= t.length) break; + guesspx += ibex.textwidth(tfont, t.substring(guessch -1, guessch+1)) - ibex.textwidth(tfont, t.charAt(guessch)); + } + guessch--; // Round down. + } + + return guessch; + } + + + + +