mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-16 05:49:43 +02:00
handle string and number font size configurations
This commit is contained in:
@@ -543,12 +543,14 @@ export const drawSimpleText = function (
|
||||
// Remove and ignore br:s
|
||||
const nText = textData.text.replace(common.lineBreakRegex, ' ');
|
||||
|
||||
const [, _fontSizePx] = parseFontSize(textData.fontSize);
|
||||
|
||||
const textElem = elem.append('text');
|
||||
textElem.attr('x', textData.x);
|
||||
textElem.attr('y', textData.y);
|
||||
textElem.style('text-anchor', textData.anchor);
|
||||
textElem.style('font-family', textData.fontFamily);
|
||||
textElem.style('font-size', textData.fontSize);
|
||||
textElem.style('font-size', _fontSizePx);
|
||||
textElem.style('font-weight', textData.fontWeight);
|
||||
textElem.attr('fill', textData.fill);
|
||||
if (textData.class !== undefined) {
|
||||
@@ -722,6 +724,8 @@ export const calculateTextDimensions: (
|
||||
return { width: 0, height: 0 };
|
||||
}
|
||||
|
||||
const [, _fontSizePx] = parseFontSize(fontSize);
|
||||
|
||||
// We can't really know if the user supplied font family will render on the user agent;
|
||||
// thus, we'll take the max width between the user supplied font family, and a default
|
||||
// of sans-serif.
|
||||
@@ -745,7 +749,7 @@ export const calculateTextDimensions: (
|
||||
const textObj = getTextObj();
|
||||
textObj.text = line;
|
||||
const textElem = drawSimpleText(g, textObj)
|
||||
.style('font-size', fontSize)
|
||||
.style('font-size', _fontSizePx)
|
||||
.style('font-weight', fontWeight)
|
||||
.style('font-family', fontFamily);
|
||||
|
||||
@@ -941,6 +945,32 @@ export const insertTitle = (
|
||||
.attr('class', cssClass);
|
||||
};
|
||||
|
||||
/**
|
||||
* Parses a raw fontSize configuration value into a number and string value.
|
||||
*
|
||||
* @param fontSize - a string or number font size configuration value
|
||||
*
|
||||
* @returns parsed number and string style font size values, or nulls if a number value can't
|
||||
* be parsed from an input string.
|
||||
*/
|
||||
export const parseFontSize = (fontSize: string | number | undefined): [number?, string?] => {
|
||||
// if the font size is a number, assume a px string representation
|
||||
if (typeof fontSize === 'number') {
|
||||
return [fontSize, fontSize + 'px'];
|
||||
}
|
||||
|
||||
const fontSizeNumber = parseInt(fontSize, 10);
|
||||
if (Number.isNaN(fontSizeNumber)) {
|
||||
// if a number value can't be parsed, return null for both values
|
||||
return [null, null];
|
||||
} else if (fontSize === String(fontSizeNumber)) {
|
||||
// if a string input doesn't contain any units, assume px units
|
||||
return [fontSizeNumber, fontSize + 'px'];
|
||||
} else {
|
||||
return [fontSizeNumber, fontSize];
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
assignWithDepth,
|
||||
wrapLabel,
|
||||
@@ -964,4 +994,5 @@ export default {
|
||||
directiveSanitizer,
|
||||
sanitizeCss,
|
||||
insertTitle,
|
||||
parseFontSize,
|
||||
};
|
||||
|
Reference in New Issue
Block a user