mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Separate metadata from fonts
This commit is contained in:
parent
a61fdb428c
commit
a0c16d9bc1
10 changed files with 47 additions and 39 deletions
|
@ -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 {
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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"];
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue