@compositive/foundation
Version:
Compositive framework foundation package.
109 lines (106 loc) • 3.66 kB
JavaScript
import { __decorate } from 'tslib';
import { Memoized } from '@compositive/commons-memoize';
import { isNumber, isOptional, isInstanceOf, isEnum, isString, isDefined } from '@compositive/commons-predicates';
import { Spec } from '@compositive/commons-spec';
import { compactObject } from '@compositive/commons-util';
import { Style } from '@compositive/primitives';
import '../styling/resolveThemedStyleProperties.js';
import '../styling/ThemedStyle.js';
import 'react';
import { FontDesignation } from './FontDesignation.js';
import { FontStyle } from './FontStyle.js';
import { TextTransform } from './TextTransform.js';
class Font {
constructor(family, weight, style, fontLoader) {
this.family = family;
this.weight = weight;
this.style = style;
this.fontLoader = fontLoader;
}
withSize(size) {
return new FontSpec({ font: this, size });
}
with(attributeNameOrValues, value) {
if (isString(attributeNameOrValues)) {
isDefined.assert(value);
return this.withSize(value);
}
else {
return new FontSpec({ ...attributeNameOrValues, font: this });
}
}
get designation() {
return FontDesignation.for(this.weight, this.style);
}
get bolder() {
var _a;
const bolderWeight = this.weight.bolder;
return bolderWeight != null
? (_a = this.family.getClosestFont(bolderWeight, this.style)) !== null && _a !== void 0 ? _a : this
: this;
}
get lighter() {
var _a;
const lighterWeight = this.weight.lighter;
return lighterWeight != null
? (_a = this.family.getClosestFont(lighterWeight, this.style)) !== null && _a !== void 0 ? _a : this
: this;
}
get italic() {
var _a;
// TODO: Should fallback to a generic font?
return (_a = this.family.getClosestFont(this.weight, FontStyle.Italic)) !== null && _a !== void 0 ? _a : this;
}
get normal() {
var _a;
// TODO: Should fallback to a generic font?
return (_a = this.family.getClosestFont(this.weight, FontStyle.Normal)) !== null && _a !== void 0 ? _a : this;
}
async load() {
return this.fontLoader.load(this);
}
isLoaded() {
return this.fontLoader.isLoaded(this);
}
toString() {
return `${this.family.name} ${this.designation.toHumanString()}`;
}
async toJson() {
return {
family: this.family,
style: this.style,
weight: this.weight,
};
}
}
class FontSpec extends Spec({
size: isNumber,
lineHeight: isOptional(isNumber),
font: isInstanceOf(Font),
letterSpacing: isOptional(isNumber),
textTransform: isOptional(isEnum(TextTransform)),
}) {
get allFonts() {
const variantDefiner = this.variantDefiner;
const combinations = variantDefiner.getCombinations();
const fontSet = combinations.reduce((set, activeVariants) => set.add(this.using(activeVariants).font), new Set());
return [...fontSet];
}
toStaticStyle() {
const { size, letterSpacing, font, lineHeight, textTransform } = this;
return new Style(compactObject({
fontSize: Math.round(size),
letterSpacing,
fontFamily: font.family.toString(),
fontWeight: font.weight.value,
fontStyle: font.style,
lineHeight,
textTransform,
}));
}
}
__decorate([
Memoized()
], FontSpec.prototype, "toStaticStyle", null);
export { Font, FontSpec };
//# sourceMappingURL=Font.js.map