npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@subframe7536/fonttools

v0.2.1

Published

[ESM only] fonttools for nodejs or web with vite plugin, powered by pyodide

Downloads

299

Readme

@subframe7536/fonttools

fonttools for nodejs or web with vite plugin, powered by pyodide

Under development, breaking changes expected. Use at your own risk

  • pyodide version: 0.26.3

Install

npm install @subframe7536/fonttools
yarn add @subframe7536/fonttools
pnpm add @subframe7536/fonttools

Usage

NodeJS

import { readFileSync, writeFileSync } from 'node:fs'
import { loadInNode } from '@subframe7536/fonttools'
import { generateBasicScript, handleFontBuffer, NameId, processNameScriptUtil } from '@subframe7536/fonttools/utils'

const buf = new Uint8Array(readFileSync('./test.ttf'))

loadInNode({ woff2: true })
  .then(async (py) => {
    const data = await handleFontBuffer(
      py,
      buf,
      generateBasicScript(`${processNameScriptUtil}
set_font_name(font, 'Test', ${NameId.FamilyName})
`)
    )
    writeFileSync('change_name.ttf', data)
    return py
  })
  .then(async (py) => {
    const data = await handleFontBuffer(
      py,
      buf,
      (input, output) => `
from fontTools.ttLib.woff2 import compress
compress('${input}', '${output}')
`
    )
    writeFileSync('output.woff2', data)
  })

Web

import { generateBasicScript, handleFontBuffer, NameId } from '@subframe7536/fonttools/utils'
import { loadInBrowser } from '@subframe7536/fonttools/web'
import src from './test.ttf'

const buf = await (await fetch(src)).arrayBuffer()
const py = await loadInBrowser()
const data = await handleFontBuffer(
  py,
  new Uint8Array(buf),
  generateBasicScript(`
def set_font_name(font: TTFont, name: str, id: int):
    font["name"].setName(name, nameID=id, platformID=1, platEncID=0, langID=0x0)
    font["name"].setName(name, nameID=id, platformID=3, platEncID=1, langID=0x409)
set_font_name(font, 'Test', ${NameId.FamilyName})
`),
)
// download
const url = URL.createObjectURL(new Blob([data]))
const a = document.createElement('a')
a.href = url
a.download = 'output.ttf'
a.click()
a.remove()

Vite Plugin

Copy assets to root while building

import { fonttools } from '@subframe7536/fontools/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [fonttools()],
})

Custom URL

There are 6 assets that the plugin handled:

  • 2 .whl file: For fonttools and brotli(for woff2)
  • 1 lock file: Load .whl
  • 1 asm.js file: EMScript generated file
  • 1 asm.wasm file: EMScript generated file
  • 1 zip file: Python stdlibs

And importer that use loadInBrowser imports lock file, asm.js, asm.wasm and zip file

Final loaded url pattern: {indexURL}{urlPrefix}{assetsName}

By default, all the assets is loaded from the same directory as importer in production. You should set it if your build.assetsDir or build.rollupOptions.output.assetFileNames is modified.

So, here is a example to custom assets url:

fonttools({
  customURL: (
    currentAssetsKey: AssetsKey,
    assetsNameMap: Map<AssetsKey, string>,
    finalAssetsPathMap: Map<AssetsKey, [path: string, source: string | Uint8Array]>,
    importer: [path: string, code: string]
  ) => `deep/${finalAssetsPathMap(currentAssetsKey)![0]}`
})

CDN Example

See in cdn.html

For esm.sh, you may need to setup options.stdlibURL and options.whlURL to correctly load

Credit

pyodide

License

MIT