Giter VIP home page Giter VIP logo

font-minify's Introduction

font-minify

npm version npm downloads bundle JSDocs License

Keep only used glyphs and remove unused glyphs. Generate font subsets to achieve the purpose of compression

Install

Using npm:

npm install font-minify --save-dev

Usage

import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join, parse } from 'node:path'
import minify from 'font-minify'
// or use CommonJS
// const minify = require('font-minify')

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

const TEST_STRING = `Font-Minify 你说,儿豁**人不骗**人`
const fontPath = join(__dirname, 'fonts/fontfile.ttf')
const buffer = fs.readFileSync(fontPath)

;(async () => {
  const ext = 'ttf'
  const newBuffer = await minify({
    buffer,
    text: TEST_STRING,
    readOptions: {
      type: ext,
    },
    writeOptions: {
      type: ext,
    },
  })

  const fontPath = join(__dirname, 'fonts/fontfile.new.ttf')
  fs.writeFileSync(fontPath, newBuffer)
})()

If you're going to run it in a browser and you're going to work with woff2 fonts, then you should also need dist/woff2.wasm, and when you do, you must also pass in the path to the woff2.wasm file. Otherwise you can't handle the woff2 font.

// example
import minify from 'font-minify'

const newBuffer = await minify({
  buffer,
  text: TEST_STRING,
  wasm: {
    // The path where you store `woff2.wasm`.
    woff2: 'woff2.wasm' // If you're using a packaging tool to build your front-end project, then you should put `woff2.wasm` in the `public` directory
  },
  readOptions: {
    type: 'ttf',
  },
  writeOptions: {
    type: 'woff2',
  },
})

If you're using vite to build your front-end project (Vite Static Asset Handling)

// example
import minify from 'font-minify'
import woff2Wasm from 'font-minify/woff2Wasm?url'

const newBuffer = await minify({
  buffer,
  text: TEST_STRING,
  wasm: {
    woff2: woff2Wasm
  },
  readOptions: {
    type: 'ttf',
  },
  writeOptions: {
    type: 'woff2',
  },
})

License

MIT License © 2024-PRESENT Lete114

font-minify's People

Contributors

lete114 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.