193 lines
5.9 KiB
JavaScript
Raw Normal View History

2010-11-20 23:43:54 +01:00
/*
2012-10-04 20:48:01 -03:00
* Copyright [2012] [wisemapping]
2011-08-29 20:10:05 -03:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2011-08-30 14:21:55 -03:00
web2d.peer.svg.TextPeer = new Class({
Extends: web2d.peer.svg.ElementPeer,
2014-03-04 20:47:23 -03:00
initialize: function () {
2011-08-30 14:21:55 -03:00
var svgElement = window.document.createElementNS(this.svgNamespace, 'text');
this.parent(svgElement);
2014-03-04 20:47:23 -03:00
this._position = {x: 0, y: 0};
2011-08-30 14:21:55 -03:00
this._font = new web2d.Font("Arial", this);
},
2014-03-04 20:47:23 -03:00
append: function (element) {
2014-03-04 21:09:59 -03:00
this._native.appendChild(element._native);
2011-08-30 14:21:55 -03:00
},
2014-03-04 20:47:23 -03:00
setTextAlignment: function (align) {
2011-09-02 02:31:03 -03:00
this._textAlign = align;
},
2014-03-04 20:47:23 -03:00
getTextAlignment: function () {
2011-09-02 02:31:03 -03:00
return $defined(this._textAlign) ? this._textAlign : 'left';
},
2014-03-04 20:47:23 -03:00
setText: function (text) {
2012-05-12 15:28:04 -03:00
// Remove all previous nodes ...
while (this._native.firstChild) {
this._native.removeChild(this._native.firstChild);
}
2011-09-02 02:31:03 -03:00
2011-08-30 14:21:55 -03:00
this._text = text;
2012-05-12 15:28:04 -03:00
if (text) {
var lines = text.split('\n');
2014-03-04 20:47:23 -03:00
lines.forEach(function (line) {
2012-05-12 15:28:04 -03:00
var tspan = window.document.createElementNS(this.svgNamespace, 'tspan');
tspan.setAttribute('dy', '1em');
tspan.setAttribute('x', this.getPosition().x);
tspan.textContent = line.length == 0 ? " " : line;
2014-03-04 21:09:59 -03:00
this._native.appendChild(tspan);
2012-05-12 15:28:04 -03:00
}.bind(this));
}
2011-08-30 14:21:55 -03:00
},
2014-03-04 20:47:23 -03:00
getText: function () {
2011-08-30 14:21:55 -03:00
return this._text;
},
2014-03-04 20:47:23 -03:00
setPosition: function (x, y) {
this._position = {x: x, y: y};
2011-09-02 02:31:03 -03:00
this._native.setAttribute('y', y);
2011-08-30 14:21:55 -03:00
this._native.setAttribute('x', x);
2011-09-02 02:31:03 -03:00
// tspan must be positioned manually.
2014-03-04 20:47:23 -03:00
this._native.getElements('tspan').forEach(function (span) {
2012-05-12 15:28:04 -03:00
span.setAttribute('x', x);
});
2011-08-30 14:21:55 -03:00
},
2014-03-04 20:47:23 -03:00
getPosition: function () {
2011-08-30 14:21:55 -03:00
return this._position;
},
2014-03-04 20:47:23 -03:00
setFont: function (font, size, style, weight) {
2011-08-30 14:21:55 -03:00
if ($defined(font)) {
this._font = new web2d.Font(font, this);
}
if ($defined(style)) {
this._font.setStyle(style);
}
if ($defined(weight)) {
this._font.setWeight(weight);
}
if ($defined(size)) {
this._font.setSize(size);
}
this._updateFontStyle();
},
2014-03-04 20:47:23 -03:00
_updateFontStyle: function () {
2011-08-30 14:21:55 -03:00
this._native.setAttribute('font-family', this._font.getFontFamily());
this._native.setAttribute('font-size', this._font.getGraphSize());
this._native.setAttribute('font-style', this._font.getStyle());
this._native.setAttribute('font-weight', this._font.getWeight());
},
2011-09-04 03:28:09 -03:00
2014-03-04 20:47:23 -03:00
setColor: function (color) {
2011-08-30 14:21:55 -03:00
this._native.setAttribute('fill', color);
},
2014-03-04 20:47:23 -03:00
getColor: function () {
2011-08-30 14:21:55 -03:00
return this._native.getAttribute('fill');
},
2014-03-04 20:47:23 -03:00
setTextSize: function (size) {
2011-08-30 14:21:55 -03:00
this._font.setSize(size);
this._updateFontStyle();
},
2011-08-29 20:10:05 -03:00
2014-03-04 20:47:23 -03:00
setContentSize: function (width, height) {
2011-08-30 14:21:55 -03:00
this._native.xTextSize = width.toFixed(1) + "," + height.toFixed(1);
},
2009-06-07 18:59:43 +00:00
2014-03-04 20:47:23 -03:00
setStyle: function (style) {
2009-06-07 18:59:43 +00:00
this._font.setStyle(style);
2011-08-30 14:21:55 -03:00
this._updateFontStyle();
},
2014-03-04 20:47:23 -03:00
setWeight: function (weight) {
2009-06-07 18:59:43 +00:00
this._font.setWeight(weight);
2011-08-30 14:21:55 -03:00
this._updateFontStyle();
},
2014-03-04 20:47:23 -03:00
setFontFamily: function (family) {
2011-08-30 14:21:55 -03:00
var oldFont = this._font;
this._font = new web2d.Font(family, this);
this._font.setSize(oldFont.getSize());
this._font.setStyle(oldFont.getStyle());
this._font.setWeight(oldFont.getWeight());
this._updateFontStyle();
},
2014-03-04 20:47:23 -03:00
getFont: function () {
2011-08-30 14:21:55 -03:00
return {
2014-03-04 20:47:23 -03:00
font: this._font.getFont(),
size: parseInt(this._font.getSize()),
style: this._font.getStyle(),
weight: this._font.getWeight()
2011-08-30 14:21:55 -03:00
};
},
2014-03-04 20:47:23 -03:00
setSize: function (size) {
2009-06-07 18:59:43 +00:00
this._font.setSize(size);
2011-08-30 14:21:55 -03:00
this._updateFontStyle();
},
2014-03-04 20:47:23 -03:00
getWidth: function () {
2014-01-05 18:57:47 -03:00
var computedWidth;
2012-03-08 00:11:54 -03:00
// Firefox hack for this issue:http://stackoverflow.com/questions/6390065/doing-ajax-updates-in-svg-breaks-getbbox-is-there-a-workaround
try {
2014-01-05 18:57:47 -03:00
computedWidth = this._native.getBBox().width;
// Chrome bug is producing this error, oly during page loading. Remove the hack if it works. The issue seems to be
// caused when the element is hidden. I don't know why, but it works ...
2014-03-04 20:47:23 -03:00
if (computedWidth == 0) {
var bbox = this._native.getBBox();
computedWidth = bbox.width;
2014-01-05 18:57:47 -03:00
}
2014-03-04 20:47:23 -03:00
} catch (e) {
2012-03-08 00:11:54 -03:00
computedWidth = 10;
2012-03-08 00:11:54 -03:00
}
2011-08-30 14:21:55 -03:00
var width = parseInt(computedWidth);
width = width + this._font.getWidthMargin();
return width;
},
2014-03-04 20:47:23 -03:00
getHeight: function () {
2012-03-08 00:11:54 -03:00
// Firefox hack for this issue:http://stackoverflow.com/questions/6390065/doing-ajax-updates-in-svg-breaks-getbbox-is-there-a-workaround
try {
var computedHeight = this._native.getBBox().height;
2014-03-04 20:47:23 -03:00
} catch (e) {
2012-03-08 00:11:54 -03:00
computedHeight = 10;
}
2011-08-30 14:21:55 -03:00
return parseInt(computedHeight);
},
2014-03-04 20:47:23 -03:00
getHtmlFontSize: function () {
2011-08-30 14:21:55 -03:00
return this._font.getHtmlSize();
2009-06-07 18:59:43 +00:00
}
2011-08-30 14:21:55 -03:00
});
2009-06-07 18:59:43 +00:00