Moving font metadata into common

This commit is contained in:
Marcel Mraz 2025-03-19 15:50:16 +01:00
parent dfd48c221c
commit ccbd004f22
No known key found for this signature in database
GPG key ID: 4EBD6E62DC830CD2
18 changed files with 58 additions and 45 deletions

View file

@ -5,15 +5,6 @@ import type {
FontFamilyValues, FontFamilyValues,
} from "@excalidraw/element/types"; } from "@excalidraw/element/types";
import {
FreedrawIcon,
FontFamilyNormalIcon,
FontFamilyHeadingIcon,
FontFamilyCodeIcon,
} from "../components/icons";
import type { JSX } from "react";
/** /**
* Encapsulates font metrics with additional font metadata. * Encapsulates font metrics with additional font metadata.
* */ * */
@ -29,8 +20,6 @@ export interface FontMetadata {
/** harcoded unitless line-height, https://github.com/excalidraw/excalidraw/pull/6360#issuecomment-1477635971 */ /** harcoded unitless line-height, https://github.com/excalidraw/excalidraw/pull/6360#issuecomment-1477635971 */
lineHeight: number; lineHeight: number;
}; };
/** element to be displayed as an icon */
icon?: JSX.Element;
/** flag to indicate a deprecated font */ /** flag to indicate a deprecated font */
deprecated?: true; deprecated?: true;
/** flag to indicate a server-side only font */ /** flag to indicate a server-side only font */
@ -49,7 +38,6 @@ export const FONT_METADATA: Record<number, FontMetadata> = {
descender: -374, descender: -374,
lineHeight: 1.25, lineHeight: 1.25,
}, },
icon: FreedrawIcon,
}, },
[FONT_FAMILY.Nunito]: { [FONT_FAMILY.Nunito]: {
metrics: { metrics: {
@ -58,7 +46,6 @@ export const FONT_METADATA: Record<number, FontMetadata> = {
descender: -353, descender: -353,
lineHeight: 1.35, lineHeight: 1.35,
}, },
icon: FontFamilyNormalIcon,
}, },
[FONT_FAMILY["Lilita One"]]: { [FONT_FAMILY["Lilita One"]]: {
metrics: { metrics: {
@ -67,7 +54,6 @@ export const FONT_METADATA: Record<number, FontMetadata> = {
descender: -220, descender: -220,
lineHeight: 1.15, lineHeight: 1.15,
}, },
icon: FontFamilyHeadingIcon,
}, },
[FONT_FAMILY["Comic Shanns"]]: { [FONT_FAMILY["Comic Shanns"]]: {
metrics: { metrics: {
@ -76,7 +62,6 @@ export const FONT_METADATA: Record<number, FontMetadata> = {
descender: -250, descender: -250,
lineHeight: 1.25, lineHeight: 1.25,
}, },
icon: FontFamilyCodeIcon,
}, },
[FONT_FAMILY.Virgil]: { [FONT_FAMILY.Virgil]: {
metrics: { metrics: {
@ -85,7 +70,6 @@ export const FONT_METADATA: Record<number, FontMetadata> = {
descender: -374, descender: -374,
lineHeight: 1.25, lineHeight: 1.25,
}, },
icon: FreedrawIcon,
deprecated: true, deprecated: true,
}, },
[FONT_FAMILY.Helvetica]: { [FONT_FAMILY.Helvetica]: {
@ -95,7 +79,6 @@ export const FONT_METADATA: Record<number, FontMetadata> = {
descender: -471, descender: -471,
lineHeight: 1.15, lineHeight: 1.15,
}, },
icon: FontFamilyNormalIcon,
deprecated: true, deprecated: true,
local: true, local: true,
}, },
@ -106,7 +89,6 @@ export const FONT_METADATA: Record<number, FontMetadata> = {
descender: -480, descender: -480,
lineHeight: 1.2, lineHeight: 1.2,
}, },
icon: FontFamilyCodeIcon,
deprecated: true, deprecated: true,
}, },
[FONT_FAMILY["Liberation Sans"]]: { [FONT_FAMILY["Liberation Sans"]]: {

View file

@ -1,6 +1,7 @@
export * from "./binary-heap"; export * from "./binary-heap";
export * from "./colors"; export * from "./colors";
export * from "./constants"; export * from "./constants";
export * from "./font-metadata";
export * from "./keys"; export * from "./keys";
export * from "./points"; export * from "./points";
export * from "./promise-pool"; export * from "./promise-pool";

View file

@ -8,15 +8,13 @@ import {
VERTICAL_ALIGN, VERTICAL_ALIGN,
randomInteger, randomInteger,
randomId, randomId,
getNewGroupIdsForDuplication,
arrayToMap, arrayToMap,
getFontString, getFontString,
getUpdatedTimestamp, getUpdatedTimestamp,
isTestEnv, isTestEnv,
getLineHeight
} from "@excalidraw/common"; } from "@excalidraw/common";
import { getLineHeight } from "@excalidraw/excalidraw/fonts/FontMetadata";
import type { Radians } from "@excalidraw/math"; import type { Radians } from "@excalidraw/math";
import type { AppState } from "@excalidraw/excalidraw/types"; import type { AppState } from "@excalidraw/excalidraw/types";
@ -32,6 +30,7 @@ import { bumpVersion, newElementWith } from "./mutateElement";
import { getBoundTextMaxWidth } from "./textElement"; import { getBoundTextMaxWidth } from "./textElement";
import { normalizeText, measureText } from "./textMeasurements"; import { normalizeText, measureText } from "./textMeasurements";
import { wrapText } from "./textWrapping"; import { wrapText } from "./textWrapping";
import { getNewGroupIdsForDuplication } from "./groups";
import { getElementAbsoluteCoords } from "."; import { getElementAbsoluteCoords } from ".";

View file

@ -13,6 +13,7 @@ import {
distance, distance,
getFontString, getFontString,
isRTL, isRTL,
getVerticalOffset,
} from "@excalidraw/common"; } from "@excalidraw/common";
import { getElementAbsoluteCoords } from "@excalidraw/element/bounds"; import { getElementAbsoluteCoords } from "@excalidraw/element/bounds";
import { getUncroppedImageElement } from "@excalidraw/element/cropElement"; import { getUncroppedImageElement } from "@excalidraw/element/cropElement";
@ -38,8 +39,6 @@ import {
import { getContainingFrame } from "@excalidraw/element/frame"; import { getContainingFrame } from "@excalidraw/element/frame";
import { getCornerRadius } from "@excalidraw/element/shapes"; import { getCornerRadius } from "@excalidraw/element/shapes";
// TODO_SEP: consider separating
import { getVerticalOffset } from "@excalidraw/excalidraw/fonts/FontMetadata";
import type { import type {
ExcalidrawElement, ExcalidrawElement,

View file

@ -19,6 +19,7 @@ import {
getFontFamilyString, getFontFamilyString,
getShortcutKey, getShortcutKey,
tupleToCoors, tupleToCoors,
getLineHeight,
} from "@excalidraw/common"; } from "@excalidraw/common";
import { import {
@ -121,7 +122,6 @@ import {
} from "../components/icons"; } from "../components/icons";
import { Fonts } from "../fonts"; import { Fonts } from "../fonts";
import { getLineHeight } from "../fonts/FontMetadata";
import { getLanguage, t } from "../i18n"; import { getLanguage, t } from "../i18n";
import { import {
canHaveArrowheads, canHaveArrowheads,

View file

@ -4,6 +4,7 @@ import {
DEFAULT_TEXT_ALIGN, DEFAULT_TEXT_ALIGN,
CODES, CODES,
KEYS, KEYS,
getLineHeight,
} from "@excalidraw/common"; } from "@excalidraw/common";
import { import {
@ -28,7 +29,6 @@ import type { ExcalidrawTextElement } from "@excalidraw/element/types";
import { paintIcon } from "../components/icons"; import { paintIcon } from "../components/icons";
import { getLineHeight } from "../fonts/FontMetadata";
import { t } from "../i18n"; import { t } from "../i18n";
import { getSelectedElements } from "../scene"; import { getSelectedElements } from "../scene";
import { CaptureUpdateAction } from "../store"; import { CaptureUpdateAction } from "../store";

View file

@ -72,6 +72,7 @@ import {
normalizeLink, normalizeLink,
toValidURL, toValidURL,
getGridPoint, getGridPoint,
getLineHeight,
type EXPORT_IMAGE_TYPES, type EXPORT_IMAGE_TYPES,
} from "@excalidraw/common"; } from "@excalidraw/common";
@ -424,7 +425,6 @@ import {
} from "../components/hyperlink/Hyperlink"; } from "../components/hyperlink/Hyperlink";
import { Fonts } from "../fonts"; import { Fonts } from "../fonts";
import { getLineHeight } from "../fonts/FontMetadata";
import { editorJotaiStore } from "../editor-jotai"; import { editorJotaiStore } from "../editor-jotai";
import { ImageSceneDataError } from "../errors"; import { ImageSceneDataError } from "../errors";
import { import {

View file

@ -9,7 +9,12 @@ import React, {
import { type FontFamilyValues } from "@excalidraw/element/types"; import { type FontFamilyValues } from "@excalidraw/element/types";
import { arrayToList, debounce, getFontFamilyString } from "@excalidraw/common"; import {
arrayToList,
debounce,
FONT_FAMILY,
getFontFamilyString,
} from "@excalidraw/common";
import type { ValueOf } from "@excalidraw/common/utility-types"; import type { ValueOf } from "@excalidraw/common/utility-types";
@ -24,7 +29,12 @@ import DropdownMenuItem, {
DropDownMenuItemBadgeType, DropDownMenuItemBadgeType,
DropDownMenuItemBadge, DropDownMenuItemBadge,
} from "../dropdownMenu/DropdownMenuItem"; } from "../dropdownMenu/DropdownMenuItem";
import { FontFamilyNormalIcon } from "../icons"; import {
FontFamilyCodeIcon,
FontFamilyHeadingIcon,
FontFamilyNormalIcon,
FreedrawIcon,
} from "../icons";
import { fontPickerKeyHandler } from "./keyboardNavHandlers"; import { fontPickerKeyHandler } from "./keyboardNavHandlers";
@ -51,6 +61,24 @@ interface FontPickerListProps {
onClose: () => void; onClose: () => void;
} }
const getFontFamilyIcon = (fontFamily: FontFamilyValues): JSX.Element => {
switch (fontFamily) {
case FONT_FAMILY.Excalifont:
case FONT_FAMILY.Virgil:
return FreedrawIcon;
case FONT_FAMILY.Nunito:
case FONT_FAMILY.Helvetica:
return FontFamilyNormalIcon;
case FONT_FAMILY["Lilita One"]:
return FontFamilyHeadingIcon;
case FONT_FAMILY["Comic Shanns"]:
case FONT_FAMILY.Cascadia:
return FontFamilyCodeIcon;
default:
return FontFamilyNormalIcon;
}
};
export const FontPickerList = React.memo( export const FontPickerList = React.memo(
({ ({
selectedFontFamily, selectedFontFamily,
@ -76,7 +104,7 @@ export const FontPickerList = React.memo(
.map(([familyId, { metadata, fontFaces }]) => { .map(([familyId, { metadata, fontFaces }]) => {
const fontDescriptor = { const fontDescriptor = {
value: familyId, value: familyId,
icon: metadata.icon ?? FontFamilyNormalIcon, icon: getFontFamilyIcon(familyId),
text: fontFaces[0]?.fontFace?.family ?? "Unknown", text: fontFaces[0]?.fontFace?.family ?? "Unknown",
}; };

View file

@ -16,6 +16,7 @@ import {
arrayToMap, arrayToMap,
getSizeFromPoints, getSizeFromPoints,
normalizeLink, normalizeLink,
getLineHeight,
} from "@excalidraw/common"; } from "@excalidraw/common";
import { import {
getNonDeletedElements, getNonDeletedElements,
@ -65,7 +66,6 @@ import type { MarkOptional, Mutable } from "@excalidraw/common/utility-types";
import { getDefaultAppState } from "../appState"; import { getDefaultAppState } from "../appState";
import { getLineHeight } from "../fonts/FontMetadata";
import { import {
getNormalizedGridSize, getNormalizedGridSize,
getNormalizedGridStep, getNormalizedGridStep,

View file

@ -13,6 +13,7 @@ import {
getFontString, getFontString,
isDevEnv, isDevEnv,
toBrandedType, toBrandedType,
getLineHeight,
} from "@excalidraw/common"; } from "@excalidraw/common";
import { import {
getCommonBounds, getCommonBounds,
@ -61,8 +62,6 @@ import type {
import type { MarkOptional } from "@excalidraw/common/utility-types"; import type { MarkOptional } from "@excalidraw/common/utility-types";
import { getLineHeight } from "../fonts/FontMetadata";
export type ValidLinearElement = { export type ValidLinearElement = {
type: "arrow" | "line"; type: "arrow" | "line";
x: number; x: number;

View file

@ -1,4 +1,5 @@
import { LOCAL_FONT_PROTOCOL } from "../FontMetadata"; import { LOCAL_FONT_PROTOCOL } from "@excalidraw/common";
import { type ExcalidrawFontFaceDescriptor } from "../Fonts"; import { type ExcalidrawFontFaceDescriptor } from "../Fonts";
export const EmojiFontFaces: ExcalidrawFontFaceDescriptor[] = [ export const EmojiFontFaces: ExcalidrawFontFaceDescriptor[] = [

View file

@ -1,9 +1,7 @@
import { promiseTry } from "@excalidraw/common"; import { promiseTry, LOCAL_FONT_PROTOCOL } from "@excalidraw/common";
import { subsetWoff2GlyphsByCodepoints } from "../subset/subset-main"; import { subsetWoff2GlyphsByCodepoints } from "../subset/subset-main";
import { LOCAL_FONT_PROTOCOL } from "./FontMetadata";
type DataURL = string; type DataURL = string;
export class ExcalidrawFontFace { export class ExcalidrawFontFace {

View file

@ -10,7 +10,13 @@ import { getContainerElement } from "@excalidraw/element/textElement";
import { charWidth } from "@excalidraw/element/textMeasurements"; import { charWidth } from "@excalidraw/element/textMeasurements";
import { containsCJK } from "@excalidraw/element/textWrapping"; import { containsCJK } from "@excalidraw/element/textWrapping";
import { getFontString, PromisePool, promiseTry } from "@excalidraw/common"; import {
FONT_METADATA,
type FontMetadata,
getFontString,
PromisePool,
promiseTry,
} from "@excalidraw/common";
import { ShapeCache } from "@excalidraw/element/ShapeCache"; import { ShapeCache } from "@excalidraw/element/ShapeCache";
@ -26,7 +32,6 @@ import { ComicShannsFontFaces } from "./ComicShanns";
import { EmojiFontFaces } from "./Emoji"; import { EmojiFontFaces } from "./Emoji";
import { ExcalidrawFontFace } from "./ExcalidrawFontFace"; import { ExcalidrawFontFace } from "./ExcalidrawFontFace";
import { ExcalifontFontFaces } from "./Excalifont"; import { ExcalifontFontFaces } from "./Excalifont";
import { FONT_METADATA, type FontMetadata } from "./FontMetadata";
import { HelveticaFontFaces } from "./Helvetica"; import { HelveticaFontFaces } from "./Helvetica";
import { LiberationFontFaces } from "./Liberation"; import { LiberationFontFaces } from "./Liberation";
import { LilitaFontFaces } from "./Lilita"; import { LilitaFontFaces } from "./Lilita";

View file

@ -1,4 +1,5 @@
import { LOCAL_FONT_PROTOCOL } from "../FontMetadata"; import { LOCAL_FONT_PROTOCOL } from "@excalidraw/common";
import { type ExcalidrawFontFaceDescriptor } from "../Fonts"; import { type ExcalidrawFontFaceDescriptor } from "../Fonts";
export const HelveticaFontFaces: ExcalidrawFontFaceDescriptor[] = [ export const HelveticaFontFaces: ExcalidrawFontFaceDescriptor[] = [

View file

@ -1,4 +1,5 @@
import { GOOGLE_FONTS_RANGES } from "../FontMetadata"; import { GOOGLE_FONTS_RANGES } from "@excalidraw/common";
import { type ExcalidrawFontFaceDescriptor } from "../Fonts"; import { type ExcalidrawFontFaceDescriptor } from "../Fonts";
import LilitaLatinExt from "./Lilita-Regular-i7dPIFZ9Zz-WBtRtedDbYE98RXi4EwSsbg.woff2"; import LilitaLatinExt from "./Lilita-Regular-i7dPIFZ9Zz-WBtRtedDbYE98RXi4EwSsbg.woff2";

View file

@ -1,4 +1,5 @@
import { GOOGLE_FONTS_RANGES } from "../FontMetadata"; import { GOOGLE_FONTS_RANGES } from "@excalidraw/common";
import { type ExcalidrawFontFaceDescriptor } from "../Fonts"; import { type ExcalidrawFontFaceDescriptor } from "../Fonts";
import Cyrilic from "./Nunito-Regular-XRXI3I6Li01BKofiOc5wtlZ2di8HDIkhdTA3j6zbXWjgevT5.woff2"; import Cyrilic from "./Nunito-Regular-XRXI3I6Li01BKofiOc5wtlZ2di8HDIkhdTA3j6zbXWjgevT5.woff2";

View file

@ -6,6 +6,7 @@ import {
getFontFamilyString, getFontFamilyString,
isRTL, isRTL,
isTestEnv, isTestEnv,
getVerticalOffset,
} from "@excalidraw/common"; } from "@excalidraw/common";
import { normalizeLink, toValidURL } from "@excalidraw/common"; import { normalizeLink, toValidURL } from "@excalidraw/common";
import { getElementAbsoluteCoords, hashString } from "@excalidraw/element"; import { getElementAbsoluteCoords, hashString } from "@excalidraw/element";
@ -44,8 +45,6 @@ import type {
NonDeletedExcalidrawElement, NonDeletedExcalidrawElement,
} from "@excalidraw/element/types"; } from "@excalidraw/element/types";
import { getVerticalOffset } from "../fonts/FontMetadata";
import type { RenderableElementsMap, SVGRenderConfig } from "../scene/types"; import type { RenderableElementsMap, SVGRenderConfig } from "../scene/types";
import type { AppState, BinaryFiles } from "../types"; import type { AppState, BinaryFiles } from "../types";
import type { Drawable } from "roughjs/bin/core"; import type { Drawable } from "roughjs/bin/core";

View file

@ -5,11 +5,10 @@ import { getElementBounds } from "@excalidraw/element";
import { getLineHeightInPx } from "@excalidraw/element/textMeasurements"; import { getLineHeightInPx } from "@excalidraw/element/textMeasurements";
import { KEYS, arrayToMap } from "@excalidraw/common"; import { KEYS, arrayToMap, getLineHeight } from "@excalidraw/common";
import { createPasteEvent, serializeAsClipboardJSON } from "../clipboard"; import { createPasteEvent, serializeAsClipboardJSON } from "../clipboard";
import { getLineHeight } from "../fonts/FontMetadata";
import { Excalidraw } from "../index"; import { Excalidraw } from "../index";
import { API } from "./helpers/api"; import { API } from "./helpers/api";