Simplify textAnchor and textDecoration handling

This commit is contained in:
Mikael Sand
2017-07-23 20:11:01 +03:00
parent 2b3f251b8b
commit fc61c9dad2
7 changed files with 124 additions and 164 deletions

View File

@@ -8,21 +8,7 @@ const fontFamilySuffix = /[\s"']*$/;
const spaceReg = /\s+/;
const commaReg = /,/g;
const anchors = {
auto: 0,
start: 1,
middle: 2,
end: 3
};
const decorations = {
none: 0,
underline: 1,
overline: 2,
'line-through': 3,
blink: 4
};
let cachedFontObjectsFromString = {};
const cachedFontObjectsFromString = {};
function extractSingleFontFamily(fontFamilyString) {
// SVG on the web allows for multiple font-families to be specified.
@@ -37,40 +23,59 @@ function parseFontString(font) {
if (cachedFontObjectsFromString.hasOwnProperty(font)) {
return cachedFontObjectsFromString[font];
}
let match = fontRegExp.exec(font);
const match = fontRegExp.exec(font);
if (!match) {
return null;
}
let fontFamily = extractSingleFontFamily(match[3]);
let fontSize = match[2] || "12";
let isBold = /bold/.exec(match[1]);
let isItalic = /italic/.exec(match[1]);
const fontFamily = extractSingleFontFamily(match[3]);
const fontSize = match[2] || "12";
const isBold = /bold/.exec(match[1]);
const isItalic = /italic/.exec(match[1]);
const fontWeight = isBold ? 'bold' : 'normal';
const fontStyle = isItalic ? 'italic' : 'normal';
cachedFontObjectsFromString[font] = {
fontSize,
fontFamily,
fontWeight: isBold ? 'bold' : 'normal',
fontStyle: isItalic ? 'italic' : 'normal'
fontWeight,
fontStyle,
};
return cachedFontObjectsFromString[font];
}
export function extractFont(props) {
let font = props.font;
const {
letterSpacing,
wordSpacing,
fontWeight,
fontStyle,
kerning,
textAnchor,
textDecoration,
} = props;
let {
font,
fontSize,
fontFamily,
} = props;
let ownedFont = {
fontFamily: extractSingleFontFamily(props.fontFamily),
letterSpacing: props.letterSpacing,
wordSpacing: props.wordSpacing,
fontWeight: props.fontWeight,
fontStyle: props.fontStyle,
fontSize: props.fontSize ? '' + props.fontSize : null,
kerning: props.kerning,
};
fontFamily = extractSingleFontFamily(fontFamily);
fontSize = fontSize ? '' + fontSize : null;
if (typeof props.font === 'string') {
font = parseFontString(props.font);
const ownedFont = _.pickBy({
fontFamily,
letterSpacing,
wordSpacing,
fontWeight,
fontStyle,
fontSize,
kerning,
textAnchor,
textDecoration,
}, prop => !_.isNil(prop));
if (typeof font === 'string') {
font = parseFontString(font);
}
ownedFont = _.pickBy(ownedFont, prop => !_.isNil(prop));
return _.defaults(ownedFont, font);
}
@@ -88,28 +93,27 @@ function parseSVGLengthList(delta) {
}
export default function(props, container) {
let {
const {
x,
y,
dx,
dy,
rotate,
method,
spacing,
textAnchor,
startOffset,
textDecoration
} = props;
let {
rotate,
children,
startOffset
} = props;
let positionX = parseSVGLengthList(x);
let positionY = parseSVGLengthList(y);
const positionX = parseSVGLengthList(x);
const positionY = parseSVGLengthList(y);
const deltaX = parseSVGLengthList(dx);
const deltaY = parseSVGLengthList(dy);
rotate = parseSVGLengthList(rotate);
let { children } = props;
let content = null;
if (typeof children === 'string' || typeof children === 'number') {
const childrenString = children.toString();
if (container) {
@@ -128,10 +132,11 @@ export default function(props, container) {
});
}
const font = extractFont(props);
startOffset = (startOffset || 0).toString();
return {
textDecoration: decorations[textDecoration] || 0,
textAnchor: anchors[textAnchor] || 0,
font: extractFont(props),
font,
children,
content,
positionX,
@@ -141,6 +146,6 @@ export default function(props, container) {
deltaY,
method,
spacing,
startOffset: (startOffset || 0).toString(),
startOffset,
};
}