From 6b95d1a33c6b3061f3640aa856b5f3b1856b7c9e Mon Sep 17 00:00:00 2001 From: fxwan Date: Thu, 27 Apr 2017 11:02:49 +0800 Subject: [PATCH] Fix text position on Y-axis, we should take fontSize in mind to layout the Text/TSpan shapes, SVG coord tes case https://www.w3.org/TR/SVG/images/coords/OrigCoordSys.svg --- lib/extract/extractText.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/extract/extractText.js b/lib/extract/extractText.js index b7e628d9..735278bb 100644 --- a/lib/extract/extractText.js +++ b/lib/extract/extractText.js @@ -80,7 +80,7 @@ function parseDelta(delta) { } } -export default function(props, container) { +export default function(props, container, ref) { const { x, y, @@ -96,34 +96,45 @@ export default function(props, container) { let { children } = props; let content = null; + let fontProps = extractFont(props); + if (props.parentFontProps) { + fontProps = Object.assign({}, props.parentFontProps, fontProps); + } if (typeof children === 'string' || typeof children === 'number') { const childrenString = children.toString(); if (container) { - children = {childrenString}; + children = {childrenString}; } else { content = childrenString; children = null; } - } else if (Children.count(children) > 1 || Array.isArray(children)) { + } else if (Children.count(children) >= 1 || Array.isArray(children)) { children = Children.map(children, child => { if (typeof child === 'string' || typeof child === 'number') { - return {child.toString()}; + return {child.toString()}; } else { - return child; + //return child; + return React.cloneElement(child, { + parentFontProps: fontProps + }); } }); } + let posY = null; + if (!_.isNil(y) && fontProps.fontSize) { + posY = (parseFloat(y || 0) - parseFloat(fontProps.fontSize || 0)).toString(); + } return { textAnchor: anchors[textAnchor] || 0, - font: extractFont(props), + font: fontProps, children, content, deltaX, deltaY, startOffset: (startOffset || 0).toString(), positionX: _.isNil(x) ? null : x.toString(), - positionY: _.isNil(y) ? null : y.toString() + positionY: posY }; }