devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
88 lines (87 loc) • 3.41 kB
JavaScript
import { ListUtils } from '@devexpress/utils/lib/utils/list';
import { StringUtils } from '@devexpress/utils/lib/utils/string';
export class FontMappings {
defaultFontName;
rules;
constructor(defaultFontName, rules) {
this.defaultFontName = defaultFontName;
this.rules = rules ? rules : [];
}
copyFrom(obj) {
this.defaultFontName = obj.defaultFontName;
this.rules = ListUtils.map(obj.rules ?? [], rule => new MappingRule(rule.sourceFontFamily, rule.destinationFontName));
}
}
export class MappingRule {
sourceFontFamily;
destinationFontName;
constructor(sourceFontFamily, destinationFontName) {
this.sourceFontFamily = sourceFontFamily;
this.destinationFontName = destinationFontName;
}
}
export class FontsSettings {
static get defaultBaseUrl() { return window.location.origin + '/fonts/'; }
_defaultFolder = FontsSettings.defaultBaseUrl;
fonts = [];
mappings = new FontMappings();
fontsMap = Object.create(null);
mapRules = Object.create(null);
get defaultFolder() { return this._defaultFolder; }
;
set defaultFolder(val) {
if (!StringUtils.endsAt(val, '\\') && !StringUtils.endsAt(val, '/'))
val += '/';
this._defaultFolder = val;
}
get useMappingRules() { return this.limitedFonts && (!!this.mappings.rules.length || !!this.mappings.defaultFontName); }
get limitedFonts() { return !!this.fonts.length; }
getPermittedFont(fontInfoCache, font) {
if (!this.useMappingRules || this.fontsMap[font.name])
return font;
const rule = this.mapRules[font.getFontFamilies()[0]];
if (rule) {
const destFont = fontInfoCache.getItemByName(rule.destinationFontName);
if (destFont)
return destFont;
}
return fontInfoCache.getItemByName(this.mappings.defaultFontName ?? this.fonts[0].name);
}
copyFrom(obj) {
this.defaultFolder = obj.defaultFolder;
this.fonts = ListUtils.map(obj.fonts, font => FontsSettings.createFont(font));
this.mappings.copyFrom(obj.mappings);
this.initInternal();
}
init(obj) {
if (obj) {
this.defaultFolder = obj.defaultFolder ?? FontsSettings.defaultBaseUrl;
this.fonts = ListUtils.map(obj.fonts ?? [], font => FontsSettings.createFont(font));
this.mappings.copyFrom(obj.mappings ?? {});
this.initInternal();
}
}
clone() {
const result = new FontsSettings();
result.copyFrom(this);
return result;
}
initInternal() {
this.fontsMap = Object.create(null);
this.fonts.forEach(font => this.fontsMap[font.name] = true);
this.mapRules = Object.create(null);
this.mappings.rules.forEach(rule => this.mapRules[rule.sourceFontFamily] = rule);
}
static createFont(obj) {
return {
name: obj.name,
fontFamily: obj.fontFamily,
googleFontsResponse: obj.googleFontsResponse,
useGoogleFonts: obj.useGoogleFonts,
italicFontUri: obj.italicFontUri,
boldFontUri: obj.boldFontUri,
boldItalicFontUri: obj.boldItalicFontUri,
regularFontUri: obj.regularFontUri,
};
}
}