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 🙏

© 2025 – Pkg Stats / Ryan Hefner

fontpath-simple-renderer

v4.0.0

Published

a simplified wrapper for fontpath-renderer

Downloads

167

Readme

fontpath-simple-renderer

experimental

This is a generic module that various render engines (GL, canvas, etc) can use to draw fontpath glyphs to the screen. Most users are encouraged to use the higher level modules, fontpath-canvas for 2D canvas, fontpath-gl for WebGL.

Usage

NPM

var options = {
	font: MyFont,
	fontSize: 32,
	//etc..
}

var renderer = require('fontpath-simple-renderer')(options)

//set up some parameters
renderer.align = 'right'
renderer.wordwrap.mode = 'pre'
renderer.layout(250)

//renders glyphs and underline data
var result = renderer.render(x, y, startIndex, endIndex)

//handle underlines
result.underlines.forEach(function(item) {
	console.log( item.position, item.size )
})

//handle glyphs
result.glyphs.forEach(function(item) {
	//	draw/manipulate the glyph data
	console.log( item.index, item.position, item.scale, item.glyph, item.charCode )
})

renderer = Renderer([options])

Creates a new renderer with the given options. These are optional.

Possible options:

  • font the Font object
  • fontSize the font size in pixels, defaults to what is defined in font
  • text the string of text we will be rendering
  • align a string 'left', 'center', 'right', default left
  • underline boolean, whether to underline, default false
  • underlinePosition the position of underline, leave undefined to compute automatically
  • underlineThickness the underline thickness, leave undefined to compute automatically
  • lineHeight the line height in pixels, default to an automatic value
  • letterSpacing the letter spacing in pixels, defaults to zero
  • wrapMode same as setting renderer.wordwrap.mode, can be normal, pre, or nowrap
  • wrapWidth an initial number in pixels which is passed to layout() after the other options have been set. Otherwise, defaults to no layout (a single line, no breaks)

renderer.render(x, y, start, end)

"Renders" the text and returns a data object with underlines and glyphs information:

{
	underlines: [
		{ position: [x,y], size: [width,height] },
		//etc...
	],
	glyphs: [
		{ 
			charCode: 40,
			position: [x,y], 
			index: i, //character index in 'text' string
			scale: [x,y],
			glyph: glyphData //contains fontpath glyph data
		},
		//etc..
	]
}

The x and y parameters default to zero. The start and end indices allow you to control how much of the layout to draw; this is useful for styling or adding underlines to specific words. It will draw the glyphs within the given indices (start inclusive, end exclusive) as they would sit if the whole layout was rendered. If unspecified, the whole string is rendered.

renderer.clearLayout()

Clears the current layout. The next render will draw all glyphs as if they are on a single line (no line breaks). This occurs whenever you change font, font size, or text.

renderer.layout(wrapWidth)

Forces a new word wrap layout. This should be called after the layout has been cleared, and before rendering. It uses the current settings from the renderer.wordwrap instance.

renderer.getBounds([includeUnderline, out])

Gets the { x, y, width, height} bounds of the current text layout. The height does not extend past the baseline of the last line unless includeUnderline is true, in which case the underline's position and height is included in the calculation.

The bounding y position is offset so that the box has an upper-left origin, for parity with HTML5 canvas rendering.

If out is specified, it will re-use that object, otherwise it will create a new one.

renderer.getDescender()

Utility method to get the font's descender.

renderer.getAscender()

Utility method to get the font's ascender.

members

renderer.font (Font object)

renderer.fontSize (number)

renderer.text (string)

Some font, font size and text parameters. Changing these values will clear the current layout.

renderer.underline (bool)

renderer.underlinePosition (number, in pixels)

renderer.underlineThickness (number, in pixels)

Some underline parameters. If position or thickness is undefined, they will be computed automatically based on font information.

renderer.align (string)

A string which indicates the align mode; "left", "right", or "center".

renderer.wordwrap

An instance of fontpath-wordwrap which controls how layout is done.

renderer.lineHeight (px)

renderer.letterSpacing (px)

Note on fontpath-renderer

This is a simplified wrapper for fontpath-renderer that doens't involve subclassing anything. This has a slight overhead of GC thrashing and doesn't allow you to render glyphs on demand (as they are being laid out) but the difference is small for most applications.

Running the Demo

git clone https://github.com/mattdesl/fontpath-simple-renderer.git
cd fontpath-simple-renderer
npm install
npm start

Then open localhost:9966 in your browser.

License

MIT, see LICENSE.md for details.