UNPKG

devexpress-richedit

Version:

DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.

82 lines (81 loc) 3.88 kB
import { UnitConverter } from '@devexpress/utils/lib/class/unit-converter'; import { MathUtils } from '@devexpress/utils/lib/utils/math'; import { CharacterFormattingScript, StrikeoutType, UnderlineType } from '../model/character/enums'; import { ColorHelper } from '../model/color/color'; export class HtmlConverter { static importantTag = ' !important'; static getCssRules(charProps, textColor, isWordBox, noStrikeoutAndUnderline, important) { var rules = HtmlConverter.getSizeSignificantRules(charProps, important); rules.push(this.getForeColorRule(textColor)); if (!noStrikeoutAndUnderline) { if (charProps.fontStrikeoutType != StrikeoutType.None && (isWordBox || !charProps.strikeoutWordsOnly)) rules.push("text-decoration: line-through"); if (charProps.fontUnderlineType != UnderlineType.None && (isWordBox || !charProps.underlineWordsOnly)) rules.push("text-decoration: underline"); } return rules; } static getForeColorRule(textColor) { textColor = textColor !== ColorHelper.AUTOMATIC_COLOR ? textColor : ColorHelper.BLACK_COLOR; return "color: " + ColorHelper.getCssStringInternal(textColor); } static getSizeSignificantCssString(characterProperties) { return HtmlConverter.getSizeSignificantRules(characterProperties).join(";"); } static getSizeSignificantRules(characterProperties, important = true) { var rules = []; if (characterProperties.allCaps) rules.push("text-transform: uppercase"); else if (characterProperties.smallCaps) rules.push("font-variant: small-caps"); rules.push("font-family: " + HtmlConverter.buildFontFamilyRule(characterProperties.fontInfo.cssString) + (important ? HtmlConverter.importantTag : '')); if (characterProperties.fontBold) rules.push("font-weight: bold"); if (characterProperties.fontItalic) rules.push("font-style: italic"); var fontSizePx = UnitConverter.pointsToPixelsF(characterProperties.fontSize); if (characterProperties.script == CharacterFormattingScript.Normal) rules.push("font-size: " + MathUtils.round(fontSizePx, 3) + "px" + (important ? HtmlConverter.importantTag : '')); else { rules.push("font-size: " + MathUtils.round(fontSizePx * characterProperties.fontInfo.scriptMultiplier, 3) + "px" + (important ? HtmlConverter.importantTag : '')); } return rules; } static buildFontFamilyRule(cssString) { const parts = this.splitByComma(cssString); const processed = parts.map(p => { let result = p.trim(); const isQuoted = (result.startsWith("'") && result.endsWith("'")) || (result.startsWith('"') && result.endsWith('"')); const needsQuotes = !isQuoted && !/^[a-zA-Z-]+$/.test(result); if (needsQuotes) result = `'${result}'`; return result; }); return processed.join(", "); } static splitByComma(cssString) { let start = 0; let end = 0; const result = []; let quote = null; for (let char of cssString) { if (char === "'" || char === '"') { if (quote === char) quote = null; else quote = char; } else if (char === "," && !quote) { if (end > start) result.push(cssString.substring(start, end).trim()); start = end + 1; } end++; } if (start < end) result.push(cssString.substring(start).trim()); return result; } }