Separate metadata from fonts

This commit is contained in:
Marcel Mraz 2025-03-13 14:36:29 +01:00
parent a61fdb428c
commit a0c16d9bc1
No known key found for this signature in database
GPG key ID: 4EBD6E62DC830CD2
10 changed files with 47 additions and 39 deletions

View file

@ -1,5 +1,6 @@
import { getLineHeight } from "@excalidraw/excalidraw/fonts/FontMetadata";
import { FONT_FAMILY } from "../constants";
import { getLineHeight } from "../fonts";
import { API } from "../tests/helpers/api";
import {

View file

@ -91,7 +91,8 @@ import {
isLinearElement,
isUsingAdaptiveRadius,
} from "../element/typeChecks";
import { Fonts, getLineHeight } from "../fonts";
import { Fonts } from "../fonts";
import { getLineHeight } from "../fonts/FontMetadata";
import { getLanguage, t } from "../i18n";
import { KEYS } from "../keys";
import { randomInteger } from "../random";

View file

@ -18,7 +18,7 @@ import {
isFrameLikeElement,
isArrowElement,
} from "../element/typeChecks";
import { getLineHeight } from "../fonts";
import { getLineHeight } from "../fonts/FontMetadata";
import { t } from "../i18n";
import { CODES, KEYS } from "../keys";
import { getSelectedElements } from "../scene";

View file

@ -305,7 +305,8 @@ import {
} from "../components/hyperlink/Hyperlink";
import { isLocalLink, normalizeLink, toValidURL } from "../data/url";
import { shouldShowBoundingBox } from "../element/transformHandles";
import { Fonts, getLineHeight } from "../fonts";
import { Fonts } from "../fonts";
import { getLineHeight } from "../fonts/FontMetadata";
import {
getFrameChildren,
isCursorInFrame,

View file

@ -37,7 +37,7 @@ import {
isTextElement,
isUsingAdaptiveRadius,
} from "../element/typeChecks";
import { getLineHeight } from "../fonts";
import { getLineHeight } from "../fonts/FontMetadata";
import { syncInvalidIndices } from "../fractionalIndex";
import { randomId } from "../random";
import {

View file

@ -22,7 +22,7 @@ import {
} from "../element/newElement";
import { measureText, normalizeText } from "../element/textMeasurements";
import { isArrowElement } from "../element/typeChecks";
import { getLineHeight } from "../fonts";
import { getLineHeight } from "../fonts/FontMetadata";
import { syncInvalidIndices } from "../fractionalIndex";
import { getSizeFromPoints } from "../points";
import { randomId } from "../random";

View file

@ -1,3 +1,8 @@
import type {
ExcalidrawTextElement,
FontFamilyValues,
} from "@excalidraw/element/types";
import {
FreedrawIcon,
FontFamilyNormalIcon,
@ -149,3 +154,34 @@ export const GOOGLE_FONTS_RANGES = {
/** local protocol to skip the local font from registering or inlining */
export const LOCAL_FONT_PROTOCOL = "local:";
/**
* Calculates vertical offset for a text with alphabetic baseline.
*/
export const getVerticalOffset = (
fontFamily: ExcalidrawTextElement["fontFamily"],
fontSize: ExcalidrawTextElement["fontSize"],
lineHeightPx: number,
) => {
const { unitsPerEm, ascender, descender } =
FONT_METADATA[fontFamily]?.metrics ||
FONT_METADATA[FONT_FAMILY.Excalifont].metrics;
const fontSizeEm = fontSize / unitsPerEm;
const lineGap =
(lineHeightPx - fontSizeEm * ascender + fontSizeEm * descender) / 2;
const verticalOffset = fontSizeEm * ascender + lineGap;
return verticalOffset;
};
/**
* Gets line height for a selected family.
*/
export const getLineHeight = (fontFamily: FontFamilyValues) => {
const { lineHeight } =
FONT_METADATA[fontFamily]?.metrics ||
FONT_METADATA[FONT_FAMILY.Excalifont].metrics;
return lineHeight as ExcalidrawTextElement["lineHeight"];
};

View file

@ -454,37 +454,6 @@ export class Fonts {
}
}
/**
* Calculates vertical offset for a text with alphabetic baseline.
*/
export const getVerticalOffset = (
fontFamily: ExcalidrawTextElement["fontFamily"],
fontSize: ExcalidrawTextElement["fontSize"],
lineHeightPx: number,
) => {
const { unitsPerEm, ascender, descender } =
Fonts.registered.get(fontFamily)?.metadata.metrics ||
FONT_METADATA[FONT_FAMILY.Virgil].metrics;
const fontSizeEm = fontSize / unitsPerEm;
const lineGap =
(lineHeightPx - fontSizeEm * ascender + fontSizeEm * descender) / 2;
const verticalOffset = fontSizeEm * ascender + lineGap;
return verticalOffset;
};
/**
* Gets line height forr a selected family.
*/
export const getLineHeight = (fontFamily: FontFamilyValues) => {
const { lineHeight } =
Fonts.registered.get(fontFamily)?.metadata.metrics ||
FONT_METADATA[FONT_FAMILY.Excalifont].metrics;
return lineHeight as ExcalidrawTextElement["lineHeight"];
};
export interface ExcalidrawFontFaceDescriptor {
uri: string;
descriptors?: FontFaceDescriptors;

View file

@ -32,7 +32,7 @@ import {
isMagicFrameElement,
isImageElement,
} from "../element/typeChecks";
import { getVerticalOffset } from "../fonts";
import { getVerticalOffset } from "../fonts/FontMetadata";
import { getContainingFrame } from "../frame";
import { ShapeCache } from "../scene/ShapeCache";
import { getCornerRadius } from "../shapes";

View file

@ -23,7 +23,7 @@ import {
isInitializedImageElement,
isTextElement,
} from "../element/typeChecks";
import { getVerticalOffset } from "../fonts";
import { getVerticalOffset } from "../fonts/FontMetadata";
import { getContainingFrame } from "../frame";
import { ShapeCache } from "../scene/ShapeCache";
import { getCornerRadius, isPathALoop } from "../shapes";