import { Util, Collection } from '../Util'; import { Factory, Validators } from '../Factory'; import { Shape } from '../Shape'; import { UA } from '../Global'; import { GetSet } from '../types'; // constants var AUTO = 'auto', //CANVAS = 'canvas', CENTER = 'center', JUSTIFY = 'justify', CHANGE_KONVA = 'Change.konva', CONTEXT_2D = '2d', DASH = '-', LEFT = 'left', TEXT = 'text', TEXT_UPPER = 'Text', TOP = 'top', BOTTOM = 'bottom', MIDDLE = 'middle', NORMAL = 'normal', PX_SPACE = 'px ', SPACE = ' ', RIGHT = 'right', WORD = 'word', CHAR = 'char', NONE = 'none', ELLIPSIS = '…', ATTR_CHANGE_LIST = [ 'fontFamily', 'fontSize', 'fontStyle', 'fontVariant', 'padding', 'align', 'verticalAlign', 'lineHeight', 'text', 'width', 'height', 'wrap', 'ellipsis', 'letterSpacing' ], // cached variables attrChangeListLen = ATTR_CHANGE_LIST.length; var dummyContext; function getDummyContext() { if (dummyContext) { return dummyContext; } dummyContext = Util.createCanvasElement().getContext(CONTEXT_2D); return dummyContext; } function _fillFunc(context) { context.fillText(this.partialText, 0, 0); } function _strokeFunc(context) { context.strokeText(this.partialText, 0, 0); } /** * Text constructor * @constructor * @memberof Konva * @augments Konva.Shape * @param {Object} config * @param {String} [config.fontFamily] default is Arial * @param {Number} [config.fontSize] in pixels. Default is 12 * @param {String} [config.fontStyle] can be normal, bold, or italic. Default is normal * @param {String} [config.fontVariant] can be normal or small-caps. Default is normal * @param {String} [config.textDecoration] can be line-through, underline or empty string. Default is empty string. * @param {String} config.text * @param {String} [config.align] can be left, center, or right * @param {String} [config.verticalAlign] can be top, middle or bottom * @param {Number} [config.padding] * @param {Number} [config.lineHeight] default is 1 * @param {String} [config.wrap] can be "word", "char", or "none". Default is word * @param {Boolean} [config.ellipsis] can be true or false. Default is false. if Konva.Text config is set to wrap="none" and ellipsis=true, then it will add "..." to the end * @@shapeParams * @@nodeParams * @example * var text = new Konva.Text({ * x: 10, * y: 15, * text: 'Simple Text', * fontSize: 30, * fontFamily: 'Calibri', * fill: 'green' * }); */ export class Text extends Shape { textArr: Array<{ text: string; width: number }>; partialText: string; textWidth: number; textHeight: number; constructor(config) { config = config || {}; // set default color to black if ( !config.fillLinearGradientColorStops && !config.fillRadialGradientColorStops && !config.fillPatternImage ) { config.fill = config.fill || 'black'; } super(config); // update text data for certain attr changes for (var n = 0; n < attrChangeListLen; n++) { this.on(ATTR_CHANGE_LIST[n] + CHANGE_KONVA, this._setTextData); } this._setTextData(); this.sceneFunc(this._sceneFunc); this.hitFunc(this._hitFunc); } _sceneFunc(context) { var padding = this.padding(), textHeight = this.getTextHeight(), lineHeightPx = this.lineHeight() * textHeight, textArr = this.textArr, textArrLen = textArr.length, verticalAlign = this.verticalAlign(), alignY = 0, align = this.align(), totalWidth = this.getWidth(), letterSpacing = this.letterSpacing(), fill = this.fill(), fontSize = this.fontSize(), textDecoration = this.textDecoration(), shouldUnderline = textDecoration.indexOf('underline') !== -1, shouldLineThrough = textDecoration.indexOf('line-through') !== -1, n; context.setAttr('font', this._getContextFont()); context.setAttr('textBaseline', MIDDLE); context.setAttr('textAlign', LEFT); // handle vertical alignment if (verticalAlign === MIDDLE) { alignY = (this.getHeight() - textArrLen * lineHeightPx - padding * 2) / 2; } else if (verticalAlign === BOTTOM) { alignY = this.getHeight() - textArrLen * lineHeightPx - padding * 2; } if (padding) { context.translate(padding, alignY + padding + lineHeightPx / 2); } else { context.translate(0, alignY + lineHeightPx / 2); } // draw text lines for (n = 0; n < textArrLen; n++) { var obj = textArr[n], text = obj.text, width = obj.width, lastLine = n !== textArrLen - 1, spacesNumber, oneWord, lineWidth; // horizontal alignment context.save(); if (align === RIGHT) { context.translate(totalWidth - width - padding * 2, 0); } else if (align === CENTER) { context.translate((totalWidth - width - padding * 2) / 2, 0); } if (shouldUnderline) { context.save(); context.beginPath(); context.moveTo(0, Math.round(lineHeightPx / 2)); spacesNumber = text.split(' ').length - 1; oneWord = spacesNumber === 0; lineWidth = align === JUSTIFY && lastLine && !oneWord ? totalWidth - padding * 2 : width; context.lineTo(Math.round(lineWidth), Math.round(lineHeightPx / 2)); // TODO: I have no idea what is real ratio // just /15 looks good enough context.lineWidth = fontSize / 15; context.strokeStyle = fill; context.stroke(); context.restore(); } if (shouldLineThrough) { context.save(); context.beginPath(); context.moveTo(0, 0); spacesNumber = text.split(' ').length - 1; oneWord = spacesNumber === 0; lineWidth = align === JUSTIFY && lastLine && !oneWord ? totalWidth - padding * 2 : width; context.lineTo(Math.round(lineWidth), 0); context.lineWidth = fontSize / 15; context.strokeStyle = fill; context.stroke(); context.restore(); } if (letterSpacing !== 0 || align === JUSTIFY) { // var words = text.split(' '); spacesNumber = text.split(' ').length - 1; for (var li = 0; li < text.length; li++) { var letter = text[li]; // skip justify for the last line if (letter === ' ' && n !== textArrLen - 1 && align === JUSTIFY) { context.translate( Math.floor((totalWidth - padding * 2 - width) / spacesNumber), 0 ); } this.partialText = letter; context.fillStrokeShape(this); context.translate( Math.round(this._getTextSize(letter).width) + letterSpacing, 0 ); } } else { this.partialText = text; context.fillStrokeShape(this); } context.restore(); if (textArrLen > 1) { context.translate(0, lineHeightPx); } } } _hitFunc(context) { var width = this.getWidth(), height = this.getHeight(); context.beginPath(); context.rect(0, 0, width, height); context.closePath(); context.fillStrokeShape(this); } setText(text) { var str = Util._isString(text) ? text : (text || '').toString(); this._setAttr(TEXT, str); return this; } getWidth() { var isAuto = this.attrs.width === AUTO || this.attrs.width === undefined; return isAuto ? this.getTextWidth() + this.padding() * 2 : this.attrs.width; } getHeight() { var isAuto = this.attrs.height === AUTO || this.attrs.height === undefined; return isAuto ? this.getTextHeight() * this.textArr.length * this.lineHeight() + this.padding() * 2 : this.attrs.height; } /** * get pure text width without padding * @method * @name Konva.Text#getTextWidth * @returns {Number} */ getTextWidth() { return this.textWidth; } /** * get height of one line of text * @method * @name Konva.Text#getTextHeight * @returns {Number} */ getTextHeight() { return this.textHeight; } _getTextSize(text) { var _context = getDummyContext(), fontSize = this.fontSize(), metrics; _context.save(); _context.font = this._getContextFont(); metrics = _context.measureText(text); _context.restore(); return { width: metrics.width, height: fontSize }; } _getContextFont() { // IE don't want to work with usual font style // bold was not working // removing font variant will solve // fix for: https://github.com/konvajs/konva/issues/94 if (UA.isIE) { return ( this.fontStyle() + SPACE + this.fontSize() + PX_SPACE + this.fontFamily() ); } return ( this.fontStyle() + SPACE + this.fontVariant() + SPACE + this.fontSize() + PX_SPACE + this.fontFamily() ); } _addTextLine(line) { if (this.align() === JUSTIFY) { line = line.trim(); } var width = this._getTextWidth(line); return this.textArr.push({ text: line, width: width }); } _getTextWidth(text) { var letterSpacing = this.letterSpacing(); var length = text.length; return ( getDummyContext().measureText(text).width + (length ? letterSpacing * (length - 1) : 0) ); } _setTextData() { var lines = this.text().split('\n'), fontSize = +this.fontSize(), textWidth = 0, lineHeightPx = this.lineHeight() * fontSize, width = this.attrs.width, height = this.attrs.height, fixedWidth = width !== AUTO && width !== undefined, fixedHeight = height !== AUTO && height !== undefined, padding = this.padding(), maxWidth = width - padding * 2, maxHeightPx = height - padding * 2, currentHeightPx = 0, wrap = this.wrap(), // align = this.align(), shouldWrap = wrap !== NONE, wrapAtWord = wrap !== CHAR && shouldWrap, shouldAddEllipsis = this.ellipsis() && !shouldWrap; this.textArr = []; getDummyContext().font = this._getContextFont(); var additionalWidth = shouldAddEllipsis ? this._getTextWidth(ELLIPSIS) : 0; for (var i = 0, max = lines.length; i < max; ++i) { var line = lines[i]; var lineWidth = this._getTextWidth(line); if (fixedWidth && lineWidth > maxWidth) { /* * if width is fixed and line does not fit entirely * break the line into multiple fitting lines */ while (line.length > 0) { /* * use binary search to find the longest substring that * that would fit in the specified width */ var low = 0, high = line.length, match = '', matchWidth = 0; while (low < high) { var mid = (low + high) >>> 1, substr = line.slice(0, mid + 1), substrWidth = this._getTextWidth(substr) + additionalWidth; if (substrWidth <= maxWidth) { low = mid + 1; match = substr + (shouldAddEllipsis ? ELLIPSIS : ''); matchWidth = substrWidth; } else { high = mid; } } /* * 'low' is now the index of the substring end * 'match' is the substring * 'matchWidth' is the substring width in px */ if (match) { // a fitting substring was found if (wrapAtWord) { // try to find a space or dash where wrapping could be done var wrapIndex; var nextChar = line[match.length]; var nextIsSpaceOrDash = nextChar === SPACE || nextChar === DASH; if (nextIsSpaceOrDash && matchWidth <= maxWidth) { wrapIndex = match.length; } else { wrapIndex = Math.max(match.lastIndexOf(SPACE), match.lastIndexOf(DASH)) + 1; } if (wrapIndex > 0) { // re-cut the substring found at the space/dash position low = wrapIndex; match = match.slice(0, low); matchWidth = this._getTextWidth(match); } } // if (align === 'right') { match = match.trimRight(); // } this._addTextLine(match); textWidth = Math.max(textWidth, matchWidth); currentHeightPx += lineHeightPx; if ( !shouldWrap || (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) ) { /* * stop wrapping if wrapping is disabled or if adding * one more line would overflow the fixed height */ break; } line = line.slice(low); line = line.trimLeft(); if (line.length > 0) { // Check if the remaining text would fit on one line lineWidth = this._getTextWidth(line); if (lineWidth <= maxWidth) { // if it does, add the line and break out of the loop this._addTextLine(line); currentHeightPx += lineHeightPx; textWidth = Math.max(textWidth, lineWidth); break; } } } else { // not even one character could fit in the element, abort break; } } } else { // element width is automatically adjusted to max line width this._addTextLine(line); currentHeightPx += lineHeightPx; textWidth = Math.max(textWidth, lineWidth); } // if element height is fixed, abort if adding one more line would overflow if (fixedHeight && currentHeightPx + lineHeightPx > maxHeightPx) { break; } } this.textHeight = fontSize; // var maxTextWidth = 0; // for(var j = 0; j < this.textArr.length; j++) { // maxTextWidth = Math.max(maxTextWidth, this.textArr[j].width); // } this.textWidth = textWidth; } // for text we can't disable stroke scaling // if we do, the result will be unexpected getStrokeScaleEnabled() { return true; } fontFamily: GetSet; fontSize: GetSet; fontStyle: GetSet; fontVariant: GetSet; align: GetSet; letterSpacing: GetSet; verticalAlign: GetSet; padding: GetSet; lineHeight: GetSet; textDecoration: GetSet; text: GetSet; wrap: GetSet; ellipsis: GetSet; } Text.prototype._fillFunc = _fillFunc; Text.prototype._strokeFunc = _strokeFunc; Text.prototype.className = TEXT_UPPER; /** * get/set width of text area, which includes padding. * @name Konva.Text#width * @method * @param {Number} width * @returns {Number} * @example * // get width * var width = text.width(); * * // set width * text.width(20); * * // set to auto * text.width('auto'); * text.width() // will return calculated width, and not "auto" */ Factory.addGetterSetter( Text, 'width', undefined, Validators.getNumberOrAutoValidator() ); /** * get/set the height of the text area, which takes into account multi-line text, line heights, and padding. * @name Konva.Text#height * @method * @param {Number} height * @returns {Number} * @example * // get height * var height = text.height(); * * // set height * text.height(20); * * // set to auto * text.height('auto'); * text.height() // will return calculated height, and not "auto" */ Factory.addGetterSetter( Text, 'height', undefined, Validators.getNumberOrAutoValidator() ); /** * get/set font family * @name Konva.Text#fontFamily * @method * @param {String} fontFamily * @returns {String} * @example * // get font family * var fontFamily = text.fontFamily(); * * // set font family * text.fontFamily('Arial'); */ Factory.addGetterSetter(Text, 'fontFamily', 'Arial'); /** * get/set font size in pixels * @name Konva.Text#fontSize * @method * @param {Number} fontSize * @returns {Number} * @example * // get font size * var fontSize = text.fontSize(); * * // set font size to 22px * text.fontSize(22); */ Factory.addGetterSetter(Text, 'fontSize', 12, Validators.getNumberValidator()); /** * get/set font style. Can be 'normal', 'italic', or 'bold'. 'normal' is the default. * @name Konva.Text#fontStyle * @method * @param {String} fontStyle * @returns {String} * @example * // get font style * var fontStyle = text.fontStyle(); * * // set font style * text.fontStyle('bold'); */ Factory.addGetterSetter(Text, 'fontStyle', NORMAL); /** * get/set font variant. Can be 'normal' or 'small-caps'. 'normal' is the default. * @name Konva.Text#fontVariant * @method * @param {String} fontVariant * @returns {String} * @example * // get font variant * var fontVariant = text.fontVariant(); * * // set font variant * text.fontVariant('small-caps'); */ Factory.addGetterSetter(Text, 'fontVariant', NORMAL); /** * get/set padding * @name Konva.Text#padding * @method * @param {Number} padding * @returns {Number} * @example * // get padding * var padding = text.padding(); * * // set padding to 10 pixels * text.padding(10); */ Factory.addGetterSetter(Text, 'padding', 0, Validators.getNumberValidator()); /** * get/set horizontal align of text. Can be 'left', 'center', 'right' or 'justify' * @name Konva.Text#align * @method * @param {String} align * @returns {String} * @example * // get text align * var align = text.align(); * * // center text * text.align('center'); * * // align text to right * text.align('right'); */ Factory.addGetterSetter(Text, 'align', LEFT); /** * get/set vertical align of text. Can be 'top', 'middle', 'bottom'. * @name Konva.Text#verticalAlign * @method * @param {String} verticalAlign * @returns {String} * @example * // get text vertical align * var verticalAlign = text.verticalAlign(); * * // center text * text.verticalAlign('middle'); */ Factory.addGetterSetter(Text, 'verticalAlign', TOP); /** * get/set line height. The default is 1. * @name Konva.Text#lineHeight * @method * @param {Number} lineHeight * @returns {Number} * @example * // get line height * var lineHeight = text.lineHeight(); * * // set the line height * text.lineHeight(2); */ Factory.addGetterSetter(Text, 'lineHeight', 1, Validators.getNumberValidator()); /** * get/set wrap. Can be "word", "char", or "none". Default is "word". * In "word" wrapping any word still can be wrapped if it can't be placed in the required width * without breaks. * @name Konva.Text#wrap * @method * @param {String} wrap * @returns {String} * @example * // get wrap * var wrap = text.wrap(); * * // set wrap * text.wrap('word'); */ Factory.addGetterSetter(Text, 'wrap', WORD); /** * get/set ellipsis. Can be true or false. Default is false. * if Konva.Text config is set to wrap="none" and ellipsis=true, then it will add "..." to the end * @name Konva.Text#ellipsis * @method * @param {Boolean} ellipsis * @returns {Boolean} * @example * // get ellipsis * var ellipsis = text.ellipsis(); * * // set ellipsis * text.ellipsis(true); */ Factory.addGetterSetter(Text, 'ellipsis', false); /** * set letter spacing property. Default value is 0. * @name Konva.Text#letterSpacing * @method * @param {Number} letterSpacing */ Factory.addGetterSetter( Text, 'letterSpacing', 0, Validators.getNumberValidator() ); /** * get/set text * @name Konva.Text#text * @method * @param {String} text * @returns {String} * @example * // get text * var text = text.text(); * * // set text * text.text('Hello world!'); */ Factory.addGetterSetter(Text, 'text', '', Validators.getStringValidator()); /** * get/set text decoration of a text. Possible values are 'underline', 'line-through' or combination of these values separated by space * @name Konva.Text#textDecoration * @method * @param {String} textDecoration * @returns {String} * @example * // get text decoration * var textDecoration = text.textDecoration(); * * // underline text * text.textDecoration('underline'); * * // strike text * text.textDecoration('line-through'); * * // underline and strike text * text.textDecoration('underline line-through'); */ Factory.addGetterSetter(Text, 'textDecoration', ''); Collection.mapMethods(Text);