refactor(ComparisonView): unify pretext font string generation with a utility function
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
export * from './utils/getPretextFontString';
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import {
|
||||||
|
describe,
|
||||||
|
expect,
|
||||||
|
it,
|
||||||
|
} from 'vitest';
|
||||||
|
import { getPretextFontString } from './getPretextFontString';
|
||||||
|
|
||||||
|
describe('getPretextFontString', () => {
|
||||||
|
it('correctly formats the font string for pretext', () => {
|
||||||
|
const weight = 400;
|
||||||
|
const sizePx = 16;
|
||||||
|
const fontName = 'Inter';
|
||||||
|
const expected = '400 16px "Inter"';
|
||||||
|
|
||||||
|
expect(getPretextFontString(weight, sizePx, fontName)).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works with different weight and size', () => {
|
||||||
|
const weight = 700;
|
||||||
|
const sizePx = 32;
|
||||||
|
const fontName = 'Roboto';
|
||||||
|
const expected = '700 32px "Roboto"';
|
||||||
|
|
||||||
|
expect(getPretextFontString(weight, sizePx, fontName)).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles font names with spaces', () => {
|
||||||
|
const weight = 400;
|
||||||
|
const sizePx = 16;
|
||||||
|
const fontName = 'Open Sans';
|
||||||
|
const expected = '400 16px "Open Sans"';
|
||||||
|
|
||||||
|
expect(getPretextFontString(weight, sizePx, fontName)).toBe(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* Formats a font configuration into a string format required by @chenglou/pretext.
|
||||||
|
*
|
||||||
|
* @param weight - Numeric font weight (e.g., 400).
|
||||||
|
* @param sizePx - Font size in pixels.
|
||||||
|
* @param fontName - The font family name.
|
||||||
|
* @returns A formatted font string: `"weight sizepx \"fontName\""`.
|
||||||
|
*/
|
||||||
|
export function getPretextFontString(weight: number, sizePx: number, fontName: string): string {
|
||||||
|
return `${weight} ${sizePx}px "${fontName}"`;
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ import {
|
|||||||
import { typographySettingsStore } from '$features/SetupFont/model';
|
import { typographySettingsStore } from '$features/SetupFont/model';
|
||||||
import { createPersistentStore } from '$shared/lib';
|
import { createPersistentStore } from '$shared/lib';
|
||||||
import { untrack } from 'svelte';
|
import { untrack } from 'svelte';
|
||||||
|
import { getPretextFontString } from '../../lib';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Storage schema for comparison state
|
* Storage schema for comparison state
|
||||||
@@ -205,8 +206,8 @@ export class ComparisonStore {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fontAString = `${weight} ${size}px "${fontAName}"`;
|
const fontAString = getPretextFontString(weight, size, fontAName);
|
||||||
const fontBString = `${weight} ${size}px "${fontBName}"`;
|
const fontBString = getPretextFontString(weight, size, fontBName);
|
||||||
|
|
||||||
// Check if already loaded to avoid UI flash
|
// Check if already loaded to avoid UI flash
|
||||||
const isALoaded = document.fonts.check(fontAString);
|
const isALoaded = document.fonts.check(fontAString);
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import clsx from 'clsx';
|
|||||||
import { getContext } from 'svelte';
|
import { getContext } from 'svelte';
|
||||||
import { Spring } from 'svelte/motion';
|
import { Spring } from 'svelte/motion';
|
||||||
import { fade } from 'svelte/transition';
|
import { fade } from 'svelte/transition';
|
||||||
|
import { getPretextFontString } from '../../lib';
|
||||||
import { comparisonStore } from '../../model';
|
import { comparisonStore } from '../../model';
|
||||||
import Character from '../Character/Character.svelte';
|
import Character from '../Character/Character.svelte';
|
||||||
import Line from '../Line/Line.svelte';
|
import Line from '../Line/Line.svelte';
|
||||||
@@ -132,8 +133,8 @@ $effect(() => {
|
|||||||
|
|
||||||
if (container && fontA && fontB) {
|
if (container && fontA && fontB) {
|
||||||
// PRETEXT API strings: "weight sizepx family"
|
// PRETEXT API strings: "weight sizepx family"
|
||||||
const fontAStr = `${_weight} ${_size}px "${fontA.name}"`;
|
const fontAStr = getPretextFontString(_weight, _size, fontA.name);
|
||||||
const fontBStr = `${_weight} ${_size}px "${fontB.name}"`;
|
const fontBStr = getPretextFontString(_weight, _size, fontB.name);
|
||||||
|
|
||||||
// Use offsetWidth to avoid transform scaling issues
|
// Use offsetWidth to avoid transform scaling issues
|
||||||
const width = container.offsetWidth;
|
const width = container.offsetWidth;
|
||||||
@@ -165,8 +166,8 @@ $effect(() => {
|
|||||||
containerWidth = width;
|
containerWidth = width;
|
||||||
layoutResult = comparisonEngine.layout(
|
layoutResult = comparisonEngine.layout(
|
||||||
comparisonStore.text,
|
comparisonStore.text,
|
||||||
`${typography.weight} ${typography.renderedSize}px "${fontA.name}"`,
|
getPretextFontString(typography.weight, typography.renderedSize, fontA.name),
|
||||||
`${typography.weight} ${typography.renderedSize}px "${fontB.name}"`,
|
getPretextFontString(typography.weight, typography.renderedSize, fontB.name),
|
||||||
width - padding,
|
width - padding,
|
||||||
typography.renderedSize * typography.height,
|
typography.renderedSize * typography.height,
|
||||||
typography.spacing,
|
typography.spacing,
|
||||||
|
|||||||
Reference in New Issue
Block a user