-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(fonts): local provider infer #13640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c3e6c4a
feat(fonts): local provider infer
florian-lefebvre 3004815
Merge branch 'main' into feat/fonts-local-infer
florian-lefebvre fe34a33
chore: comment
florian-lefebvre bf690c6
feat: work
florian-lefebvre 2b50303
feat: logic
florian-lefebvre dcf2631
feat: refactor
florian-lefebvre 7d9629c
feat: error
florian-lefebvre d1d851b
chore: type
florian-lefebvre a004cee
feat: update fontace
florian-lefebvre 6450451
Merge branch 'main' into feat/fonts-local-infer
florian-lefebvre 95d4051
feat: tests
florian-lefebvre 51647dc
feat: optional instead of infer keyword
florian-lefebvre 449ee01
feat: update unicodeRange handling
florian-lefebvre 241cf63
fix: tests
florian-lefebvre e46d8a8
chore: changeset
florian-lefebvre 678689d
Merge branch 'main' into feat/fonts-local-infer
florian-lefebvre 976100f
feat: remove unicodeRange inference
florian-lefebvre e5b32e1
Update .changeset/tricky-papayas-stand.md
florian-lefebvre bd1840c
Update packages/astro/src/assets/fonts/config.ts
florian-lefebvre d7c3ec8
Update packages/astro/src/core/errors/errors-data.ts
florian-lefebvre d221118
Merge branch 'main' into feat/fonts-local-infer
florian-lefebvre 80d9b80
feat: address feedback
florian-lefebvre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| 'astro': patch | ||
| --- | ||
|
|
||
| Allows inferring `weight` and `style` when using the local provider of the experimental fonts API | ||
|
|
||
| If you want Astro to infer those properties directly from your local font files, let them undefined: | ||
|
|
||
| ```js | ||
| { | ||
| // No weight specified: infer | ||
| style: 'normal' // Do not infer | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/astro/src/assets/fonts/implementations/font-file-reader.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { readFileSync } from 'node:fs'; | ||
| import type { ErrorHandler, FontFileReader } from '../definitions.js'; | ||
| import { fontace } from 'fontace'; | ||
| import type { Style } from '../types.js'; | ||
|
|
||
| export function createFontaceFontFileReader({ | ||
| errorHandler, | ||
| }: { errorHandler: ErrorHandler }): FontFileReader { | ||
| return { | ||
| extract({ family, url }) { | ||
| try { | ||
| const data = fontace(readFileSync(url)); | ||
| return { | ||
| weight: data.weight, | ||
| style: data.style as Style, | ||
| }; | ||
| } catch (cause) { | ||
| throw errorHandler.handle({ | ||
| type: 'cannot-extract-data', | ||
| data: { family, url }, | ||
| cause, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,66 @@ | ||
| import type * as unifont from 'unifont'; | ||
| import { FONT_FORMAT_MAP } from '../constants.js'; | ||
| import type { FontTypeExtractor, UrlProxy } from '../definitions.js'; | ||
| import type { FontFileReader, FontTypeExtractor, UrlProxy } from '../definitions.js'; | ||
| import type { ResolvedLocalFontFamily } from '../types.js'; | ||
|
|
||
| interface Options { | ||
| family: ResolvedLocalFontFamily; | ||
| urlProxy: UrlProxy; | ||
| fontTypeExtractor: FontTypeExtractor; | ||
| fontFileReader: FontFileReader; | ||
| } | ||
|
|
||
| export function resolveLocalFont({ family, urlProxy, fontTypeExtractor }: Options): { | ||
| export function resolveLocalFont({ | ||
| family, | ||
| urlProxy, | ||
| fontTypeExtractor, | ||
| fontFileReader, | ||
| }: Options): { | ||
| fonts: Array<unifont.FontFaceData>; | ||
| } { | ||
| return { | ||
| fonts: family.variants.map((variant) => ({ | ||
| weight: variant.weight, | ||
| style: variant.style, | ||
| fonts: family.variants.map((variant) => { | ||
| const infer = variant.weight === undefined || variant.style === undefined; | ||
florian-lefebvre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // We prepare the data | ||
| const data: unifont.FontFaceData = { | ||
| // If it should be inferred, we don't want to set the value | ||
| weight: variant.weight, | ||
| style: variant.style, | ||
| src: [], | ||
| unicodeRange: variant.unicodeRange, | ||
| display: variant.display, | ||
| stretch: variant.stretch, | ||
| featureSettings: variant.featureSettings, | ||
| variationSettings: variant.variationSettings, | ||
| }; | ||
| // We proxy each source | ||
| src: variant.src.map((source, index) => ({ | ||
| originalURL: source.url, | ||
| url: urlProxy.proxy({ | ||
| url: source.url, | ||
| // We only use the first source for preloading. For example if woff2 and woff | ||
| // are available, we only keep woff2. | ||
| collectPreload: index === 0, | ||
| data: { | ||
| weight: variant.weight, | ||
| style: variant.style, | ||
| }, | ||
| init: null, | ||
| }), | ||
| format: FONT_FORMAT_MAP[fontTypeExtractor.extract(source.url)], | ||
| tech: source.tech, | ||
| })), | ||
| display: variant.display, | ||
| unicodeRange: variant.unicodeRange, | ||
| stretch: variant.stretch, | ||
| featureSettings: variant.featureSettings, | ||
| variationSettings: variant.variationSettings, | ||
| })), | ||
| data.src = variant.src.map((source, index) => { | ||
| // We only try to infer for the first source | ||
florian-lefebvre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (infer && index === 0) { | ||
| const result = fontFileReader.extract({ family: family.name, url: source.url }); | ||
| if (variant.weight === undefined) data.weight = result.weight; | ||
| if (variant.style === undefined) data.style = result.style; | ||
| } | ||
|
|
||
| return { | ||
| originalURL: source.url, | ||
| url: urlProxy.proxy({ | ||
| url: source.url, | ||
| // We only use the first source for preloading. For example if woff2 and woff | ||
| // are available, we only keep woff2. | ||
| collectPreload: index === 0, | ||
| data: { | ||
| weight: data.weight, | ||
| style: data.style, | ||
| }, | ||
| init: null, | ||
| }), | ||
| format: FONT_FORMAT_MAP[fontTypeExtractor.extract(source.url)], | ||
| tech: source.tech, | ||
| }; | ||
| }); | ||
| return data; | ||
| }), | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.