-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
msdfonts: base64 distributed msdf fonts
- Loading branch information
1 parent
3f76da1
commit a1df879
Showing
19 changed files
with
6,618 additions
and
17,016 deletions.
There are no files selected for viewing
This file contains 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 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 was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains 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,29 @@ | ||
# msdfonts | ||
|
||
base64 msdf fonts distributed as npm packages | ||
|
||
# usage with R3/uikit | ||
|
||
```jsx | ||
import { inter } from '@pmndrs/msdfonts' | ||
|
||
;<FontFamilyProvider inter={inter}>{...children}</FontFamilyProvider> | ||
``` | ||
|
||
# How to build | ||
|
||
## First Step | ||
|
||
`cd docker-volume` | ||
`docker build . -t msdfonts` | ||
`docker run -v ./docker-volume:/data/:rw -e GOOGLE_FONTS_API_KEY='<insert-api-key>' msdfonts` | ||
|
||
for users on ARM architecture (e.g. Apple M-chips) | ||
|
||
`cd docker-volume` | ||
`docker build . --platform linux/x86_64 -t msdfonts` | ||
`docker run -v ./docker-volume:/data/:rw -e GOOGLE_FONTS_API_KEY='<insert-api-key>' --platform linux/x86_64 msdfonts` | ||
|
||
## Final Step | ||
|
||
Now delete the file in `src/index.ts` and copy the file `docker-volume/index.ts` into `src/index.ts` |
This file contains 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,6 @@ | ||
*.json | ||
!package.json | ||
*.ttf | ||
*.png | ||
*.webp | ||
*.ts |
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
packages/fonts/download.js → packages/msdfonts/docker-volume/download.js
This file contains 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 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,89 @@ | ||
import https from 'https' | ||
import http from 'http' | ||
import fs, { writeFileSync } from 'fs' | ||
import { exec } from 'child_process' | ||
import { dirname, resolve } from 'path' | ||
|
||
async function generate(fontFamily, variant) { | ||
const response = await fetch( | ||
`https://www.googleapis.com/webfonts/v1/webfonts?family=${fontFamily}&key=${process.env.GOOGLE_FONTS_API_KEY}`, | ||
) | ||
const json = await response.json() | ||
if (json.error != null) { | ||
console.error('fetch font families', fontFamily, json.error) | ||
return | ||
} | ||
const result = Object.entries(json.items[0].files).find( | ||
([name]) => name.toLocaleLowerCase() === variant.toLocaleLowerCase(), | ||
) | ||
if (result == null) { | ||
return false | ||
} | ||
await download(result[1], 'font.ttf') | ||
await runCmd("fontforge -lang=ff -c 'Open($1); SelectAll(); RemoveOverlap(); Generate($2)' font.ttf fixed-font.ttf") | ||
await runCmd(`npm run msdf`) | ||
await runCmd(`npm run webp`) | ||
return true | ||
} | ||
|
||
const variants = { light: '300', regular: '400', medium: '500', 'semi-bold': '600', bold: '700' } | ||
const fontFamilies = ['Inter'] | ||
|
||
async function main() { | ||
let result = '' | ||
for (const fontFamily of fontFamilies) { | ||
result += `export const ${fontFamily.toLowerCase()} = {` | ||
for (const [fontWeightName, fontWeightValue] of Object.entries(variants)) { | ||
if (!(await generate(fontFamily, fontWeightValue))) { | ||
continue | ||
} | ||
result += `\t"${fontWeightName}": ${fontToJson('fixed-font.json')},` | ||
} | ||
result += `}\n\n` | ||
} | ||
writeFileSync('./index.ts', result) | ||
} | ||
|
||
main().catch(console.error) | ||
|
||
function runCmd(cmd) { | ||
return new Promise((resolve, reject) => | ||
exec(cmd, (error) => { | ||
if (error == null) { | ||
resolve() | ||
return | ||
} | ||
reject(error) | ||
}), | ||
) | ||
} | ||
|
||
function download(url, to) { | ||
return new Promise((resolve, reject) => { | ||
const file = fs.createWriteStream(to) | ||
;(url.startsWith('https') ? https : http) | ||
.get(url, (response) => { | ||
response.pipe(file) | ||
file.on('finish', () => { | ||
file.close() | ||
resolve() | ||
}) | ||
}) | ||
.on('error', reject) | ||
}) | ||
} | ||
|
||
function fontToJson(jsonPath) { | ||
const json = JSON.parse(fs.readFileSync(jsonPath)) | ||
|
||
for (let i = 0; i < json.pages.length; i++) { | ||
const url = resolve(dirname(jsonPath), json.pages[i]).replace('.png', '.webp') | ||
json.pages[i] = toUrl(fs.readFileSync(url), 'image/webp') | ||
} | ||
|
||
return JSON.stringify(json) | ||
} | ||
|
||
function toUrl(buf, mimeType) { | ||
return `data:${mimeType};base64,${buf.toString('base64')}` | ||
} |
This file contains 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,11 @@ | ||
{ | ||
"type": "module", | ||
"dependencies": { | ||
"msdf-bmfont-xml": "^2.7.0", | ||
"sharp-cli": "4.1" | ||
}, | ||
"scripts": { | ||
"msdf": "msdf-bmfont -f json fixed-font.ttf -i charset.txt -m 256,512 -o fixed-font -s 48", | ||
"webp": "sharp --nearLossless -i fixed-font.png -o fixed-font.webp -f webp" | ||
} | ||
} |
This file contains 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,9 @@ | ||
FROM ubuntu:24.10 | ||
RUN apt -y update | ||
RUN apt -y install nodejs | ||
RUN apt -y install npm | ||
RUN apt -y install -y fontforge | ||
WORKDIR /data/ | ||
CMD npm install && node generate.js | ||
|
||
|
This file contains 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,23 @@ | ||
{ | ||
"name": "@pmndrs/msdfonts", | ||
"version": "0.0.0", | ||
"description": "base64 msdf fonts distributed as npm package", | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"fonts", | ||
"uikit", | ||
"icons", | ||
"threejs", | ||
"r3f", | ||
"msdf" | ||
], | ||
"author": "Bela Bohlender", | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"type": "module", | ||
"main": "dist/index.js" | ||
} | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"declaration": true, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src"] | ||
} |
This file contains 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 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 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.