Giter VIP home page Giter VIP logo

babylon.font's Introduction

About

Library

  • Compile glyph to shapes using WebAssembly.
  • Build mesh from shapes.

Tool

  • Gen
    • Dump mesh geometry to json.
    • Dump mesh geometry to binary.
    • Screenshot to png.
    • more ...

Usage

<script src='babylon.js'></script>
<script src='earcut.js'></script>
<script src='opentype.js'></script>
<script type='module'>

import { Compiler, Font } from './babylon.font.mjs';

(async function() {

  // Create BabylonJS environment
  const scene = ..;

  // Build compiler
  const compiler = await Compiler.Build('compiler.wasm');

  // Install font(s)
  const font = await Font.Install('a.ttf', compiler);

  // Build mesh for single character
  const shapes = Font.Compile(font, 'c', ..); // see ### Font.Compile
  const mesh = Font.BuildMesh(shapes, ..); // see ### Font.BuildMesh

  // Measure a character
  const metrics = Font.Measure(font, 'c', fontSize);
  metrics.advanceWidth;
  metrics.ascender;
  metrics.descender;

})();

</script>

API

Compiler

Compiler.Build

const compiler = await Compiler.Build(
  wasmUrl   // compiler (.wasm) url
);          //-> Compiler{}

Font

Font.Install

const font = await Font.Install(
  fontUrl,   // font (.otf/.ttf) url
  compiler   // Compiler{}
);           //-> Font{}

font.raw;    // opentype.Font{}

Font.Measure

const metrics = Font.Measure(
  font,   // Font{}
  name,   // char name, e.g. 'B'
  size    // font size
);        //-> Font.Metrics{}

metrics.advanceWidth;
metrics.ascender;
metrics.descender;

Font.Compile

const shapes = Font.Compile(
  font, // Font{}
  name, // char name, e.g. 'B'
  size, // font size
  ppc,  // no. of intermediate points used to interp. a bezier curve [0, 255]
  eps   // threshold of decimation, e.g. 0.001
);      //-> Array<Font.Shape>

Font.BuildMesh

const mesh = Font.BuildMesh(
  shapes, // Array<Font.Shape>
  option, // options(excludes shape & holes[]) of MeshBuilder.CreatePolygon()
  scene   // BABYLON.Scene{}
);        //-> BABYLON.Mesh

Thanks

MaxGraey - Helps with AssemblyScript/WASM optimizations

OpentypeJS - Read and write OpenType fonts using JavaScript.

Earcut - The fastest and smallest JavaScript polygon triangulation library for your WebGL apps

BabylonJS - A powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.

AssemblyScript - Compiles a strict subset of TypeScript (basically JavaScript with types) to WebAssembly using Binaryen.

License

MIT

babylon.font's People

Contributors

ycw avatar maxgraey avatar

Watchers

James Cloos avatar  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.