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

feat: return font file priority #125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions src/providers/google.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ResolveFontOptions } from '../types'
import type { FontFaceData, ResolveFontOptions } from '../types'

import { hash } from 'ohash'
import { extractFontFaceData } from '../css/parse'
Expand Down Expand Up @@ -65,20 +65,28 @@ export default defineFontProvider<ProviderOption>('google', async (_options = {}
resolvedAxes.push(axis)
}

let css = ''
let priority = 0
const resolvedFontFaceData: FontFaceData[] = []

for (const extension in userAgents) {
css += await $fetch<string>('/css2', {
const data = extractFontFaceData(await $fetch<string>('/css2', {
baseURL: 'https://fonts.googleapis.com',
headers: { 'user-agent': userAgents[extension as keyof typeof userAgents] },
query: {
family: `${family}:${resolvedAxes.join(',')}@${resolvedVariants.join(';')}`,
},
}))
data.map((f) => {
f.meta ??= {}
f.meta.priority = priority
return f
})
Comment on lines +79 to 83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have some logic to decide about increasing priority

  • 1st request: woff2
  • 2nd request: some woff2 and woff

(because naturally browser will follow priority vs extensions if in the same font-face declaration)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's exactly what I'm thinking about. Should we do it directly in unifont or in Nuxt Fonts?

resolvedFontFaceData.push(...data)
priority++
}

// TODO: support subsets
return extractFontFaceData(css)
return resolvedFontFaceData
}

return {
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export interface FontFaceData {
featureSettings?: string
/** Allows low-level control over OpenType or TrueType font variations, by specifying the four letter axis names of the features to vary, along with their variation values. */
variationSettings?: string
/** Metadata for the font face used by unifont */
meta?: {
/** The priority of the font face, usually used to indicate fallbacks. Smaller is more prioritized. */
priority?: number
}
}

export interface ResolveFontResult {
Expand Down
4 changes: 4 additions & 0 deletions test/providers/google.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ describe('google', () => {

const resolvedStyles = pickUniqueBy(fonts, fnt => fnt.style)
const resolvedWeights = pickUniqueBy(fonts, fnt => String(fnt.weight))
const resolvedPriorities = pickUniqueBy(fonts, fnt => fnt.meta?.priority)

const styles = ['oblique 0deg 15deg', 'normal'] as ResolveFontOptions['styles']

// Variable wght and separate weights from 300 to 1000
const weights = ['300,1000', ...([...Array.from({ length: 7 }).keys()].map(i => String(i * 100 + 300)))]

const priorities = [0, 1]

expect(fonts).toHaveLength(11)
expect(resolvedStyles).toMatchObject(styles)
expect(resolvedWeights).toMatchObject(weights)
expect(resolvedPriorities).toMatchObject(priorities)
})
})