Skip to content
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

fix: font weight issue and types improvements #13

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/astro-font/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ import { Buffer } from 'node:buffer'
import { getFallbackMetricsFromFontFile } from './font.ts'
import { pickFontFileForFallbackGeneration } from './fallback.ts'

interface Record {
rishi-raj-jain marked this conversation as resolved.
Show resolved Hide resolved
[property: string]: string
}
type GlobalValues = "inherit" | "initial" | "revert" | "revert-layer" | "unset"

type FontWeight = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
rishi-raj-jain marked this conversation as resolved.
Show resolved Hide resolved

interface Source {
path: string
css?: Record
style: string
css?: Record<string, string>
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-style
style: "normal" | "italic" | "oblique" | `oblique ${number}deg` | GlobalValues | {}
rishi-raj-jain marked this conversation as resolved.
Show resolved Hide resolved
preload?: boolean
weight?: string | number
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
weight?: "normal" | "bold" | "lighter" | "bolder" | GlobalValues | FontWeight | `${FontWeight}` | {}
}

interface Config {
name: string
src: Source[]
fetch?: boolean
display: string
// https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display
display: "auto" | "block" | "swap" | "fallback" | "optional" | {}
verbose?: boolean
selector?: string
preload?: boolean
Expand Down Expand Up @@ -227,7 +230,7 @@ export async function generateFonts(fontCollection: Config[]): Promise<Config[]>
return duplicatedCollection
}

async function getFallbackFont(fontCollection: Config): Promise<Record> {
async function getFallbackFont(fontCollection: Config): Promise<Record<string, string>> {
const fonts: any[] = []
let writeAllowed, tmpDir, cachedFilePath, cacheDir
const [os, fs] = await Promise.all([getOS(), getFS()])
Expand Down Expand Up @@ -256,7 +259,7 @@ async function getFallbackFont(fontCollection: Config): Promise<Record> {
if (res) {
fonts.push({
style: i.style,
weight: i.weight,
weight: i.weight?.toString(),
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
metadata: create(res),
})
}
Expand Down
7 changes: 4 additions & 3 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import { AstroFont } from 'astro-font'
basePath: './public',
src: [
{
style: 'bold',
style: 'normal',
weight: 'bold',
// (Optional) but ensures that it is preloaded
preload: true,
path: 'https://fonts.gstatic.com/s/afacad/v1/6NUK8FKMIQOGaw6wjYT7ZHG_zsBBfvLqagk-80KjZfJ_uw.woff2',
},
{
weight: '500',
style: 'medium',
weight: 500,
style: 'normal',
// (Optional) but does not preload this specifically
preload: true,
path: './public/fonts/Afacad-Medium.ttf',
Expand Down
2 changes: 1 addition & 1 deletion src/pages/second.astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { AstroFont } from 'astro-font'
// },
{
weight: '500',
style: 'medium',
style: 'normal',
// (Optional) but does not preload this specifically
preload: false,
path: join(process.cwd(), 'public', 'fonts', 'Afacad-Medium.ttf'),
Expand Down