/*************************************************** * * * TextWrap || * * * * A class for naive text-wrapping with SVG 1.0 * * * ***************************************************/ /** * * SVG * **/ function SVG () { } SVG.ns = 'http://www.w3.org/2000/svg'; /** * * TextWrap * **/ function TextWrap (id, node) { this._id = id; this._node = node; this._string = null; this._x = null; this._y = null; this._width = null; this._font = null; this._size = null; this._align = null; this._quality = null; this._interval = null; this._svg = null; this._lines = null; this._initialized = false; this._construct(); } TextWrap.ns = 'http://xmlns.graougraou.com/svg/text/'; TextWrap._instances = new Array(); TextWrap._init = function () { var elements = document.documentElement.getElementsByTagNameNS(this.ns, 'wrap'); for (var i=0; i this._width) { if (!words.length) { line.push(words[0]); } if (line.length!=0) lines.push( new Line(prevLength, line) ); line = new Array(); if (word.indexOf('\n\n')==-1) line.push(words.shift()); } else { if (word.indexOf('\n\n')==-1) line.push(words.shift()); } if (word.indexOf('\n\n')!=-1) { if (line.length!=0) lines.push( new Line(this._width, line) ); lines.push( new Line(this._width, []) ); line = new Array(); line.push(words.shift()); } prevLength = length; if (words.length == 0) { lines.push( new Line(0, line) ); } } this._lines = lines; } TextWrap.prototype._layout = function () { this._clear(); var lines = (new Array(0)).concat(this._lines); var anchor = 'start'; if (this._align == 'center') { anchor = 'middle'; } else if (this._align == 'right') { anchor = 'end'; } for (var i=0; i0 && lines[(i-1)]) hh = lines[(i-1)%lines.length]._words.length==0 ? (parseFloat(this._interval)*1.5)+'em' : this._interval; tspan.setAttribute('dy', i ? hh : '1em'); this._svg.appendChild(tspan); } this._svg.style.setProperty('text-anchor', anchor); this._show(); } /** * * Utility methods * **/ TextWrap.prototype._hide = function () { this._svg.style.setProperty('opacity', '0'); } TextWrap.prototype._show = function () { this._svg.style.setProperty('opacity', '1'); } TextWrap.prototype._clear = function () { while (this._svg.hasChildNodes()) { this._svg.removeChild(this._svg.firstChild); } this._svg.appendChild(document.createTextNode('')); } TextWrap.prototype._kill = function () { this._svg.parentNode.parentNode.removeChild(this._svg.parentNode); } /** * * GET / SET * * Getters and setters for CSS properties and XML attributes, * will be updated when mutation events will be implemented * **/ TextWrap.prototype.getX = function () { return this._x; } TextWrap.prototype.setX = function (x) { if (x != this._x) { this._x = x; this._svg.setAttribute('transform', 'translate(' + this._x + ' ' + this._y + ')'); } } TextWrap.prototype.getY = function () { return this._y; } TextWrap.prototype.setY = function (y) { if (y != this._y) { this._y = y; this._svg.setAttribute('transform', 'translate(' + this._x + ' ' + this._y + ')'); } } TextWrap.prototype.getWidth = function () { return this._width; } TextWrap.prototype.setWidth = function (width) { if (width != this._width) { this._width = width; if (this._initialized) { this._splitString(); this._layout(); } } } TextWrap.prototype.getTextAlign = function () { return this._align; } TextWrap.prototype.setTextAlign = function (align) { if (align != this._align) { this._align = align; if (this._initialized) { this._layout(); } } } TextWrap.prototype.getString = function () { return this._string; } TextWrap.prototype.setString = function (string) { if (string != this._string) { this._string = string; if (this._initialized) { this._splitString(); this._layout(); } } } TextWrap.prototype.getFontFamily = function () { return this._font; } TextWrap.prototype.setFontFamily = function (font) { if (font != this._font) { this._font = font; this._svg.style.setProperty('font-family', this._font); if (this._initialized) { this._splitString(); this._layout(); } } } TextWrap.prototype.getFontSize = function () { return this._size; } TextWrap.prototype.setFontSize = function (size) { if (size != this._size) { this._size = size; this._svg.style.setProperty('font-size', this._size); if (this._initialized) { this._splitString(); this._layout(); } } } TextWrap.prototype.getTextRendering = function () { return this._quality; } TextWrap.prototype.setTextRendering = function (quality) { if (quality != this._quality) { this._quality = quality; this._svg.style.setProperty('text-rendering', this._quality); if (this._initialized) { this._splitString(); this._layout(); } } } TextWrap.prototype.getLineInterval = function () { return this._interval; } TextWrap.prototype.setLineInterval = function (interval) { if (interval != this._interval) { this._interval = interval; if (this._initialized) { var element = this._svg.firstChild; var count = 0; while (element) { if (element.nodeName == 'tspan') { if (count) { element.setAttribute('dy', this._interval); } count++; if (count == this._lines.length) { break; } } element = element.nextSibling; } } } } /***** * * Line * *****/ function Line (width, words) { this._width = width; this._words = words; }