Skip to content

Commit

Permalink
fix: filter adobe font results
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 6, 2024
1 parent 616b5d2 commit 8960184
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 53 deletions.
9 changes: 8 additions & 1 deletion src/providers/adobe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ export default defineFontProvider<ProviderOption>('adobe', async (options, ctx)
// TODO: Not sure whether this css_names array always has a single element. Still need to investigate.
const cssName = font.css_names[0] ?? family.toLowerCase().split(' ').join('-')

return extractFontFaceData(css, cssName)
return extractFontFaceData(css, cssName).filter((font) => {
const [lowerWeight, upperWeight] = Array.isArray(font.weight) ? font.weight : [0, 0]

return (
(!options.styles || !font.style || options.styles.includes(font.style as 'normal'))
&& (!options.weights || !font.weight || Array.isArray(font.weight) ? options.weights.some(weight => Number(weight) <= upperWeight || Number(weight) >= lowerWeight) : options.weights.includes(String(font.weight)))
)
})
}

return []
Expand Down
52 changes: 0 additions & 52 deletions test/providers.test.ts

This file was deleted.

78 changes: 78 additions & 0 deletions test/providers/adobe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { describe, expect, it } from 'vitest'
import { createUnifont, providers } from '../../src'
import { pickUniqueBy } from '../utils'

describe('adobe', () => {
it('correctly types options for adobe provider', async () => {
providers.adobe({ id: [] })
// @ts-expect-error options must be provided
providers.adobe()

expect(true).toBe(true)
})

it('works', async () => {
const unifont = await createUnifont([providers.adobe({ id: ['sij5ufr', 'grx7wdj'] })])

const { fonts: aleo } = await unifont.resolveFont('Aleo')
const normalised = aleo.map(f => ({
...f,
src: f.src.map(s => ({ ...s, url: 'url' in s ? s.url.replace(/https:\/\/use\.typekit\.net\/.*$/, '<some-font-url>') : undefined })),
}))

expect(normalised).toMatchInlineSnapshot(`
[
{
"display": "auto",
"src": [
{
"format": "woff2",
"url": "<some-font-url>",
},
{
"format": "woff",
"url": "<some-font-url>",
},
{
"format": "opentype",
"url": "<some-font-url>",
},
],
"style": "italic",
"weight": 400,
},
{
"display": "auto",
"src": [
{
"format": "woff2",
"url": "<some-font-url>",
},
{
"format": "woff",
"url": "<some-font-url>",
},
{
"format": "opentype",
"url": "<some-font-url>",
},
],
"style": "normal",
"weight": 400,
},
]
`)

const weights = ['400']
const styles = ['italic'] as Array<'italic'>

const barlow = await unifont.resolveFont('Barlow Semi Condensed', { weights, styles, subsets: [] }).then(r => r.fonts)

const resolvedStyles = pickUniqueBy(barlow, fnt => fnt.style)
const resolvedWeights = pickUniqueBy(barlow, fnt => String(fnt.weight))

expect(barlow).toHaveLength(1)
expect(resolvedStyles).toMatchObject(styles)
expect(resolvedWeights).toMatchObject(weights)
})
})
9 changes: 9 additions & 0 deletions test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Quick and dirty way to pick into a new array, not needed if the test is un-necessary
export function pickUniqueBy<T, K>(arr: T[], by: (arg: T) => K): K[] {
return [...arr.reduce((acc, fnt) => {
const prop = by(fnt)
if (!acc.has(prop))
acc.add(prop)
return acc
}, new Set<K>())]
}

0 comments on commit 8960184

Please sign in to comment.