aboutsummaryrefslogtreecommitdiffstats
path: root/src/elements/Text.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/elements/Text.js')
-rw-r--r--src/elements/Text.js69
1 files changed, 9 insertions, 60 deletions
diff --git a/src/elements/Text.js b/src/elements/Text.js
index b3fb8e0..2951c2f 100644
--- a/src/elements/Text.js
+++ b/src/elements/Text.js
@@ -13,60 +13,14 @@ import * as textable from '../modules/core/textable.js'
export default class Text extends Shape {
// Initialize node
- constructor (node) {
- super(nodeOrNew('text', node), node)
+ constructor (node, attrs = node) {
+ super(nodeOrNew('text', node), attrs)
this.dom.leading = new SVGNumber(1.3) // store leading value for rebuilding
this._rebuild = true // enable automatic updating of dy values
this._build = false // disable build mode for adding multiple lines
}
- // Move over x-axis
- // Text is moved its bounding box
- // text-anchor does NOT matter
- x (x, box = this.bbox()) {
- if (x == null) {
- return box.x
- }
-
- return this.attr('x', this.attr('x') + x - box.x)
- }
-
- // Move over y-axis
- y (y, box = this.bbox()) {
- if (y == null) {
- return box.y
- }
-
- return this.attr('y', this.attr('y') + y - box.y)
- }
-
- move (x, y, box = this.bbox()) {
- return this.x(x, box).y(y, box)
- }
-
- // Move center over x-axis
- cx (x, box = this.bbox()) {
- if (x == null) {
- return box.cx
- }
-
- return this.attr('x', this.attr('x') + x - box.cx)
- }
-
- // Move center over y-axis
- cy (y, box = this.bbox()) {
- if (y == null) {
- return box.cy
- }
-
- return this.attr('y', this.attr('y') + y - box.cy)
- }
-
- center (x, y, box = this.bbox()) {
- return this.cx(x, box).cy(y, box)
- }
-
// Set the text content
text (text) {
// act as getter
@@ -102,11 +56,11 @@ export default class Text extends Shape {
text.call(this, this)
} else {
// store text and make sure text is not blank
- text = text.split('\n')
+ text = (text + '').split('\n')
// build new lines
for (var j = 0, jl = text.length; j < jl; j++) {
- this.tspan(text[j]).newLine()
+ this.newLine(text[j])
}
}
@@ -140,9 +94,10 @@ export default class Text extends Shape {
var blankLineOffset = 0
var leading = this.dom.leading
- this.each(function () {
+ this.each(function (i) {
var fontSize = globals.window.getComputedStyle(this.node)
.getPropertyValue('font-size')
+
var dy = leading * new SVGNumber(fontSize)
if (this.dom.newLined) {
@@ -151,7 +106,7 @@ export default class Text extends Shape {
if (this.text() === '\n') {
blankLineOffset += dy
} else {
- this.attr('dy', dy + blankLineOffset)
+ this.attr('dy', i ? dy + blankLineOffset : 0)
blankLineOffset = 0
}
}
@@ -163,12 +118,6 @@ export default class Text extends Shape {
return this
}
- // Enable / disable build mode
- build (build) {
- this._build = !!build
- return this
- }
-
// overwrite method from parent to set data properly
setData (o) {
this.dom = o
@@ -182,12 +131,12 @@ extend(Text, textable)
registerMethods({
Container: {
// Create text element
- text: wrapWithAttrCheck(function (text) {
+ text: wrapWithAttrCheck(function (text = '') {
return this.put(new Text()).text(text)
}),
// Create plain text element
- plain: wrapWithAttrCheck(function (text) {
+ plain: wrapWithAttrCheck(function (text = '') {
return this.put(new Text()).plain(text)
})
}