LineBreakMeasurer.js 9.32 KB
Newer Older
mengmeng's avatar
mengmeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/**
 * Word wrapping
 *
 * @author (Javascript) Dmitry Farafonov
 */

var AttributedStringIterator = function (text) {
    //this.text = this.rtrim(this.ltrim(text));
    text = text.replace(/(\s)+/, " ");
    this.text = this.rtrim(text);
    /*
    if (beginIndex < 0 || beginIndex > endIndex || endIndex > length()) {
        throw new IllegalArgumentException("Invalid substring range");
    }
    */
    this.beginIndex = 0;
    this.endIndex = this.text.length;
    this.currentIndex = this.beginIndex;

    //console.group("[AttributedStringIterator]");
    var i = 0;
    var string = this.text;
    var fullPos = 0;

    //console.log("string: \"" + string + "\", length: " + string.length);
    this.startWordOffsets = [];
    this.startWordOffsets.push(fullPos);

    // TODO: remove i 1000
    while (i < 1000) {
        var pos = string.search(/[ \t\n\f-\.\,]/);
        if (pos == -1)
            break;

        // whitespace start
        fullPos += pos;
        string = string.substr(pos);
        ////console.log("fullPos: " + fullPos + ", pos: " + pos +  ", string: ", string);

        // remove whitespaces
        var pos = string.search(/[^ \t\n\f-\.\,]/);
        if (pos == -1)
            break;

        // whitespace end
        fullPos += pos;
        string = string.substr(pos);

        ////console.log("fullPos: " + fullPos);
        this.startWordOffsets.push(fullPos);

        i++;
    }
    //console.log("startWordOffsets: ", this.startWordOffsets);
    //console.groupEnd();
};
AttributedStringIterator.prototype = {
    getEndIndex: function (pos) {
        if (typeof (pos) == "undefined")
            return this.endIndex;

        var string = this.text.substr(pos, this.endIndex - pos);

        var posEndOfLine = string.search(/[\n]/);
        if (posEndOfLine == -1)
            return this.endIndex;
        else
            return pos + posEndOfLine;
    },
    getBeginIndex: function () {
        return this.beginIndex;
    },
    isWhitespace: function (pos) {
        var str = this.text[pos];
        var whitespaceChars = " \t\n\f";

        return (whitespaceChars.indexOf(str) != -1);
    },
    isNewLine: function (pos) {
        var str = this.text[pos];
        var whitespaceChars = "\n";

        return (whitespaceChars.indexOf(str) != -1);
    },
    preceding: function (pos) {
        //console.group("[AttributedStringIterator.preceding]");
        for (var i in this.startWordOffsets) {
            var startWordOffset = this.startWordOffsets[i];
            if (pos < startWordOffset && i > 0) {
                //console.log("startWordOffset: " + this.startWordOffsets[i-1]);
                //console.groupEnd();
                return this.startWordOffsets[i - 1];
            }
        }
        //console.log("pos: " + pos);
        //console.groupEnd();
        return this.startWordOffsets[i];
    },
    following: function (pos) {
        //console.group("[AttributedStringIterator.following]");
        for (var i in this.startWordOffsets) {
            var startWordOffset = this.startWordOffsets[i];
            if (pos < startWordOffset && i > 0) {
                //console.log("startWordOffset: " + this.startWordOffsets[i]);
                //console.groupEnd();
                return this.startWordOffsets[i];
            }
        }
        //console.log("pos: " + pos);
        //console.groupEnd();
        return this.startWordOffsets[i];
    },
    ltrim: function (str) {
        var patt2 = /^\s+/g;
        return str.replace(patt2, "");
    },
    rtrim: function (str) {
        var patt2 = /\s+$/g;
        return str.replace(patt2, "");
    },
    getLayout: function (start, limit) {
        return this.text.substr(start, limit - start);
    },
    getCharAtPos: function (pos) {
        return this.text[pos];
    }
};

var LineBreakMeasurer = function (paper, x, y, text, fontAttrs) {
    this.paper = paper;
    this.text = new AttributedStringIterator(text);
    this.fontAttrs = fontAttrs;

    if (this.text.getEndIndex() - this.text.getBeginIndex() < 1) {
        throw {message: "Text must contain at least one character.", code: "IllegalArgumentException"};
    }

    //this.measurer = new TextMeasurer(paper, this.text, this.fontAttrs);
    this.limit = this.text.getEndIndex();
    this.pos = this.start = this.text.getBeginIndex();

    this.rafaelTextObject = this.paper.text(x, y, this.text.text).attr(fontAttrs).attr("text-anchor", "start");
    this.svgTextObject = this.rafaelTextObject[0];
};
LineBreakMeasurer.prototype = {
    nextOffset: function (wrappingWidth, offsetLimit, requireNextWord) {
        //console.group("[nextOffset]");
        var nextOffset = this.pos;
        if (this.pos < this.limit) {
            if (offsetLimit <= this.pos) {
                throw {message: "offsetLimit must be after current position", code: "IllegalArgumentException"};
            }

            var charAtMaxAdvance = this.getLineBreakIndex(this.pos, wrappingWidth);
            //charAtMaxAdvance --;
            //console.log("charAtMaxAdvance:", charAtMaxAdvance, ", [" + this.text.getCharAtPos(charAtMaxAdvance) + "]");

            if (charAtMaxAdvance == this.limit) {
                nextOffset = this.limit;
                //console.log("charAtMaxAdvance == this.limit");
            } else if (this.text.isNewLine(charAtMaxAdvance)) {
                //console.log("isNewLine");
                nextOffset = charAtMaxAdvance + 1;
            } else if (this.text.isWhitespace(charAtMaxAdvance)) {
                // TODO: find next noSpaceChar
                //return nextOffset;
                nextOffset = this.text.following(charAtMaxAdvance);
            } else {
                // Break is in a word;  back up to previous break.
                /*
                var testPos = charAtMaxAdvance + 1;
                if (testPos == this.limit) {
                    console.error("hbz...");
                } else {
                    nextOffset = this.text.preceding(charAtMaxAdvance);
                }
                */
                nextOffset = this.text.preceding(charAtMaxAdvance);

                if (nextOffset <= this.pos) {
                    nextOffset = Math.max(this.pos + 1, charAtMaxAdvance);
                }
            }
        }
        if (nextOffset > offsetLimit) {
            nextOffset = offsetLimit;
        }
        //console.log("nextOffset: " + nextOffset);
        //console.groupEnd();
        return nextOffset;
    },
    nextLayout: function (wrappingWidth) {
        //console.groupCollapsed("[nextLayout]");
        if (this.pos < this.limit) {
            var requireNextWord = false;
            var layoutLimit = this.nextOffset(wrappingWidth, this.limit, requireNextWord);
            //console.log("layoutLimit:", layoutLimit);
            if (layoutLimit == this.pos) {
                //console.groupEnd();
                return null;
            }
            var result = this.text.getLayout(this.pos, layoutLimit);
            //console.log("layout: \"" + result + "\"");

            // remove end of line

            //var posEndOfLine = this.text.getEndIndex(this.pos);
            //if (posEndOfLine < result.length)
            //	result = result.substr(0, posEndOfLine);

            this.pos = layoutLimit;

            //console.groupEnd();
            return result;
        } else {
            //console.groupEnd();
            return null;
        }
    },
    getLineBreakIndex: function (pos, wrappingWidth) {
        //console.group("[getLineBreakIndex]");
        //console.log("pos:"+pos + ", text: \""+ this.text.text.replace(/\n/g, "_").substr(pos, 1) + "\"");

        var bb = this.rafaelTextObject.getBBox();

        var charNum = -1;
        try {
            var svgPoint = this.svgTextObject.getStartPositionOfChar(pos);
            //var dot = this.paper.ellipse(svgPoint.x, svgPoint.y, 1, 1).attr({"stroke-width": 0, fill: Color.blue});
            svgPoint.x = svgPoint.x + wrappingWidth;
            //svgPoint.y = bb.y;
            //console.log("svgPoint:", svgPoint);

            //var dot = this.paper.ellipse(svgPoint.x, svgPoint.y, 1, 1).attr({"stroke-width": 0, fill: Color.red});

            charNum = this.svgTextObject.getCharNumAtPosition(svgPoint);
        } catch (e) {
            console.warn("getStartPositionOfChar error, pos:" + pos);
            /*
            var testPos = pos + 1;
            if (testPos < this.limit) {
                return testPos
            }
            */
        }
        //console.log("charNum:", charNum);
        if (charNum == -1) {
            //console.groupEnd();
            return this.text.getEndIndex(pos);
        } else {
            // When case there is new line between pos and charnum then use this new line
            var newLineIndex = this.text.getEndIndex(pos);
            if (newLineIndex < charNum) {
                console.log("newLineIndex <= charNum, newLineIndex:" + newLineIndex + ", charNum:" + charNum, "\"" + this.text.text.substr(newLineIndex + 1).replace(/\n/g, "?") + "\"");
                //console.groupEnd();

                return newLineIndex;
            }

            //var charAtMaxAdvance  = this.text.text.substring(charNum, charNum + 1);
            var charAtMaxAdvance = this.text.getCharAtPos(charNum);
            //console.log("!!charAtMaxAdvance: " + charAtMaxAdvance);
            //console.groupEnd();
            return charNum;
        }
    },
    getPosition: function () {
        return this.pos;
    }
};