Skip to content

Commit

Permalink
createFontStack: Ensure provided size-adjust is factored into overr…
Browse files Browse the repository at this point in the history
…ides (#199)
  • Loading branch information
michaeltaranto authored May 5, 2024
1 parent f55acae commit 630a5fe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
37 changes: 37 additions & 0 deletions .changeset/sour-mayflies-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
'@capsizecss/core': patch
---

createFontStack: Ensure provided \`size-adjust\` is factored into metric overrides

Ensures a custom `size-adjust` value provided via the `fontFaceProperties` option is factored into the calculations for the metric overrides.

#### Example

If a custom `size-adjust` value is provided:

```ts
createFontStack(
[ merriweatherSans, arial ],
{
fontFaceProperties: {
sizeAdjust: '300%'
}
},
)
```

The resulting metric overrides are now adjusted accordingly:

```diff
@font-face {
font-family: "Merriweather Sans Fallback";
src: local('Arial');
- ascent-override: 92.3409%;
+ ascent-override: 32.8%;
- descent-override: 25.619%;
+ descent-override: 9.1%;
line-gap-override: 0%;
size-adjust: 300%;
}
```
6 changes: 3 additions & 3 deletions packages/core/src/createFontStack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ describe('createFontStack', () => {
"fontFaces": "@font-face {
font-family: "Merriweather Sans Fallback";
src: local('Arial');
size-adjust: 300%;
ascent-override: 92.3409%;
descent-override: 25.619%;
ascent-override: 32.8%;
descent-override: 9.1%;
line-gap-override: 0%;
size-adjust: 300%;
}",
"fontFamily": ""Merriweather Sans", "Merriweather Sans Fallback", Arial",
}
Expand Down
19 changes: 13 additions & 6 deletions packages/core/src/createFontStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { round } from './round';
import type { FontMetrics, SupportedSubset } from './types';

const toPercentString = (value: number) => `${round(value * 100)}%`;
const fromPercentString = (value: string) => parseFloat(value) / 100;

export const toCssProperty = (property: string) =>
property.replace(/([A-Z])/g, (property) => `-${property.toLowerCase()}`);
Expand Down Expand Up @@ -40,23 +41,29 @@ interface OverrideValuesParams {
metrics: FontStackMetrics;
fallbackMetrics: FontStackMetrics;
subset: SupportedSubset;
sizeAdjust?: AtRule.FontFace['sizeAdjust'];
}
const calculateOverrideValues = ({
metrics,
fallbackMetrics,
subset,
sizeAdjust: sizeAdjustOverride,
}: OverrideValuesParams): AtRule.FontFace => {
// Calculate size adjust
const preferredFontXAvgRatio =
resolveXWidthAvg(metrics, subset) / metrics.unitsPerEm;
const fallbackFontXAvgRatio =
resolveXWidthAvg(fallbackMetrics, subset) / fallbackMetrics.unitsPerEm;

const sizeAdjust =
const calculatedSizeAdjust =
preferredFontXAvgRatio && fallbackFontXAvgRatio
? preferredFontXAvgRatio / fallbackFontXAvgRatio
: 1;

const sizeAdjust = sizeAdjustOverride
? fromPercentString(sizeAdjustOverride)
: calculatedSizeAdjust;

const adjustedEmSquare = metrics.unitsPerEm * sizeAdjust;

// Calculate metric overrides for preferred font
Expand Down Expand Up @@ -185,12 +192,14 @@ type FontFaceFormatObject = {
const resolveOptions = (options: Parameters<typeof createFontStack>[1]) => {
const fontFaceFormat = options?.fontFaceFormat ?? 'styleString';
const subset = options?.subset ?? 'latin';
const fontFaceProperties = options?.fontFaceProperties ?? {};
const { sizeAdjust, ...fontFaceProperties } =
options?.fontFaceProperties ?? {};

return {
fontFaceFormat,
subset,
fontFaceProperties,
sizeAdjust,
} as const;
};

Expand All @@ -206,7 +215,7 @@ export function createFontStack(
[metrics, ...fallbackMetrics]: FontStackMetrics[],
optionsArg: CreateFontStackOptions = {},
) {
const { fontFaceFormat, fontFaceProperties, subset } =
const { fontFaceFormat, fontFaceProperties, sizeAdjust, subset } =
resolveOptions(optionsArg);
const { familyName } = metrics;

Expand All @@ -230,10 +239,8 @@ export function createFontStack(
metrics,
fallbackMetrics: fallback,
subset,
sizeAdjust,
}),
...(fontFaceProperties?.sizeAdjust
? { sizeAdjust: fontFaceProperties.sizeAdjust }
: {}),
},
});
});
Expand Down

0 comments on commit 630a5fe

Please sign in to comment.