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

@downpourdigital/bmfont-utils

v0.0.3

Published

BMFont utilities for WebGL

Downloads

56

Readme

BMFont utils

BMFont utilities for WebGL.

yarn add @downpourdigital/bmfont-utils
npm i @downpourdigital/bmfont-utils

LayoutGenerator

import {
	LayoutGenerator,
} from '@downpourdigital/bmfont-utils';

const generator = new LayoutGenerator( font: BMFont );

The generator instance exposes some metrics about the font:

generator.capHeight
generator.xHeight
generator.ascenderHeight
generator.descenderHeight

If you're not familiar with the terminology, Monotype has you covered. All metrics are relative to font-size 1.


LayoutGenerator only has one method:

generator.layout({
	text: string;
	width?: number;
	letterSpacing?: number;
	lineHeight?: number;
	noWrap?: boolean;
	tabWidth?: number;
	xShift?: number;
	align?: 'left' | 'right' | 'center' | 'justify';
	computeLineY?: boolean;
	computeCharUvs?: boolean;
	computeCharIndex?: boolean;
	computeLineIndex?: boolean;
});

width defines the maximum line-width at font-size 1. Most of the other options should be self-explanatory. By setting the computeXXXX options to true, you can compute some additional vertex attributes:

LineY describes the verical position of the vertex between ascender (0) and descender (1).

CharUvs gives you normalized coordinates relative to the current glyph quad.

CharIndex and LineIndex should be self-explanatory.


LayoutGenerator.layout() returns an object, which may be used to construct an indexed buffer geometry:

{
	verts: number[],
	uvs: number[],
	indices: number[],
	lineY?: number[],
	charUvs?: number[],
	charIndices?: number[],
	lineIndices?: number[],
	charCount: number,
	lineCount: number,
}

verts are the actual 2D vertices, indices are the pointer indices for your ELEMENT_ARRAY_BUFFER.

It's important to note that everything here is relative to font-size 1, so some scaling may be necessary on your end.

WordWrapper

WordWrapper is a naïve implementation of a greedy word wrapper. It bases its measurements on the actual glyph widths and takes kernings into account.

import {
	WordWrapper,
} from '@downpourdigital/bmfont-utils';

const wrapper = new WordWrapper({
	font: BMFont;
	breakPoints?: BreakPoint[];
	useKernings?: boolean;
});

breakPoints defines an array of possible line break opportunities. By default, space (\u0020), soft hyphen (\u00AD), en dash (\u2013), em dash (\u2014) and hyphen minus (\u002D) are used.

A BreakPoint consists of a char to look for and its replacement, containing the actual line-break:

{	// en dash
	char: '–'.charCodeAt( 0 ),
	replace: '–\n',
}

Two methods are exported:

wrapper.mesure( text: string );

Returns the width of a given string at font-size 1.


wrapper.wrap(
	text: string;
	width?: number;
	letterSpacing?: number;
	tabWidth?: number;
});

Returns a given string text with inserted line-breaks.

width defines the maximum line-width at font-size 1, letterSpacing and tabWidth should be self-explanatory

License

© 2019 DOWNPOUR DIGITAL, licensed under BSD-4-Clause