Migrate to the gfonts-js-library

This commit is contained in:
Sofía Aritz 2023-05-20 22:05:58 +02:00
parent 6ff2cefc70
commit 1739b0dd51
6 changed files with 10 additions and 118 deletions

View File

@ -8,7 +8,7 @@
"build": "vite build",
"preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json",
"deploy": "cd _scripts && deploy.sh"
"deploy": "yarn && cd _scripts && deploy.sh"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^2.0.3",
@ -20,6 +20,6 @@
"vite": "^4.3.2"
},
"dependencies": {
"pb-parser": "git+https://git.sofiaritz.com/sofia/pb-parser"
"gfonts-js-library": "git+https://git.sofiaritz.com/GFonts/gfonts-js-library"
}
}

View File

@ -1,64 +0,0 @@
export type Variant = { weight: number, style: string }
export interface Family {
font: string,
variants: Variant[],
}
/**
* @param input The input should look like this: `Fira+Sans:ital,wght@1,200;1,500`
*/
export function parse_family(input: string): Family {
let [rfont, rvariants] = input.split(":")
let font = rfont.replaceAll("+", "").toLowerCase()
let variants: Variant[] = []
if(rvariants != null && rvariants.length > 0) {
let rfragments = rvariants.split("@").filter(v => v.length > 0)
let fragments
let with_weight = rfragments[0].includes("wght")
let with_ital = rfragments[0].includes("ital")
if (with_ital || with_weight) {
fragments = rfragments[1]
} else {
fragments = rfragments[0]
}
let rsubvariants = fragments.split(";")
for (let rvariant of rsubvariants) {
if (with_weight) {
if (rvariant.includes(",")) {
let variant = rvariant.split(",")
variants.push({
weight: Number(variant[1]),
style: Number(variant[0]) === 1 ? "italic" : "normal",
})
} else {
variants.push({
weight: Number(rvariant),
style: "normal",
})
}
} else if(with_ital) {
let italvariants = rvariant.split(",")
for (let variant of italvariants) {
variants.push({
weight: 400,
style: Number(variant) === 1 ? "italic" : "normal"
})
}
}
}
} else {
// TODO(sofia@git.sofiaritz.com): Is this the default value?
variants.push({
weight: 400,
style: "normal",
})
}
return {
font,
variants,
}
}

View File

@ -1,5 +1,5 @@
import {parse} from "pb-parser";
import {parse_family, type Variant} from "./css2";
import {parse_spec, type Variant} from "gfonts-js-library/src/css2";
import {retrieve_font_metadata} from "gfonts-js-library/src/metadata"
interface MirrorVariant {
path: string,
@ -27,50 +27,6 @@ function to_array(v) {
return [v]
}
}
function font_base_url(font, folder, mirror) {
return `${mirror}/${folder}/${font}`
}
async function get_font_metadata(font, mirror) {
let base_url
let response
// TODO(sofia@git.sofiaritz.com): Improve this re-trying logic. This feels wrong 😭
try {
base_url = font_base_url(font, "ofl", mirror)
let rresponse = await (await fetch(base_url + "/METADATA.pb"))
if (rresponse.ok) {
response = await rresponse.text()
} else {
throw new Error("Font not found.")
}
} catch (e) {
try {
base_url = font_base_url(font, "ufl", mirror)
let rresponse = await (await fetch(base_url + "/METADATA.pb"))
if (rresponse.ok) {
response = await rresponse.text()
} else {
throw new Error("Font not found.")
}
} catch (e) {
try {
base_url = font_base_url(font, "apache", mirror)
let rresponse = await (await fetch(base_url + "/METADATA.pb"))
if (rresponse.ok) {
response = await rresponse.text()
} else {
throw new Error("Font not found.")
}
} catch (e) {
console.error("Font not found.")
throw new Error(`"${font}" not found in mirror "${mirror}".`)
}
}
}
return {base_url, parsed: parse(response)}
}
export async function css2_to_cssfile(css2_url, mirror): Promise<string> {
let url = css2_url
.replace("https://fonts.googleapis.com/css2", "")
@ -81,9 +37,9 @@ export async function css2_to_cssfile(css2_url, mirror): Promise<string> {
rfamilies.shift()
let mirror_meta = await Promise.all(rfamilies
.map(v => v.replace("?family=", ""))
.map(parse_family)
.map(parse_spec)
.map(async (v) => {
let {base_url, parsed: metadata} = await get_font_metadata(v.font, mirror)
let {url, metadata} = await retrieve_font_metadata(v.font, mirror)
let fonts = []
for (let variant of v.variants) {
let { weight, style }: Variant = variant
@ -94,7 +50,7 @@ export async function css2_to_cssfile(css2_url, mirror): Promise<string> {
if (style === "italic") {
fonts.push({
name: metadata.name,
path: base_url + "/" + font.filename,
path: url + "/" + font.filename,
weight,
style,
})
@ -106,7 +62,7 @@ export async function css2_to_cssfile(css2_url, mirror): Promise<string> {
if (matched === false) {
fonts.push({
name: metadata.name,
path: base_url + "/" + rfonts[0].filename,
path: url + "/" + rfonts[0].filename,
weight,
style,
})

View File

@ -1,5 +1,5 @@
import "./lib/css/fonts.css"
import "./lib/css/app.css"
import "./css/fonts.css"
import "./css/app.css"
import App from "./App.svelte"
const app = new App({