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

three-bmfont-textra

v4.0.2

Published

renders BMFont files in ThreeJS with word-wrapping

Downloads

18

Readme

three-bmfont-textra

This is a fork of three-bmfont-textra. Meant to be used as an swappable extension if you need extra things for effects.

New Features

  • UVs for letters,words,lines,

  • Expanding the letters to have space for effects (WIP)

  • Subdivision of the plane for vertex effects.

  • Re-creating

    • [x] Make it work by creating From 0
    • [x] Fix Update Method
  • [x] Expand geometry of letters in all directions

    • [x] Create sides and corners
      • [x] Positions
      • [x] Indices
      • [x] Make arrays of the correct size.
    • [x] Make correct UVs
      • [ ] Make the uniform for it
        • [ ] This should probably be an attribute for each letter becasue they have different boundings
        • [ ] Also, shirking and growing dont seem to be as nice as I thouhgt. Perhaps Im missing something or this isnt goign to work so well. I thought I could do it without messing with the texture.
          • [ ] OR, maybe I'm clamping in the wrong s`pots?
    • [x] Make it work with more than one glyph.
  • [x] Subdivide the vertices.

    • [x] Fix vertices
    • [x] Uvs
    • [x] Indices
  • [x] Calculate UV for

    • [x] Letters
    • [x] Words
    • [x] Lines
    • [ ] Paragrahps
  • TODO's

    • [x] Fix UVs for segmented square
    • [ ] Check if making it counterclockwise(it was clockwise before) has any effect
    • Make indices unit9
    • Move loop "pages" to the vertices loop
    • Wonder if i can remove the indices loop by having the other loop do some magic with % and /
  • Optimizations

    • [ ] make the ammount of subdivision dependent on the size of the letter Problems:
  • Subdividing vertices / calculating uvs might make it really thought to recreate text on resize.

    • Perhaps I can subdivide the position buffer, but leave al letters at the center. Then have a second buffer for the positioning (xy).
      • With the idea beign that having a lot of the same attribute might be faster than creating a bunch of vertices.

unstable

(click for demo) - (source)

Bitmap font rendering for ThreeJS, batching glyphs into a single BufferGeometry. Supports word-wrapping, letter spacing, kerning, signed distance fields with standard derivatives, multi-channel signed distance fields, multi-texture fonts, and more. About 12kb after minification.

The latest 3.x version works on Three r109 and beyond. For legacy support (e.g. Three r69-73, r79-108), please use version [email protected].

Below is an example that uses load-bmfont to parse BMFont files on the fly with XHR:

var createGeometry = require('three-bmfont-text')
var loadFont = require('load-bmfont')

loadFont('fonts/Arial.fnt', function(err, font) {
  // create a geometry of packed bitmap glyphs, 
  // word wrapped to 300px and right-aligned
  var geometry = createGeometry({
    width: 300,
    align: 'right',
    font: font
  })

  // change text and other options as desired
  // the options sepcified in constructor will
  // be used as defaults
  geometry.update('Lorem ipsum\nDolor sit amet.')
  
  // the resulting layout has metrics and bounds
  console.log(geometry.layout.height)
  console.log(geometry.layout.descender)
    
  // the texture atlas containing our glyphs
  var textureLoader = new THREE.TextureLoader();
  textureLoader.load('fonts/Arial.png', function (texture) {
    // we can use a simple ThreeJS material
    var material = new THREE.MeshBasicMaterial({
      map: texture,
      transparent: true,
      color: 0xaaffff
    })

    // now do something with our mesh!
    var mesh = new THREE.Mesh(geometry, material)
  })
})

The glyph layout is built on layout-bmfont-text.

Usage

NPM

geometry = createText(opt)

Returns a new BufferGeometry with the given options.

Note: The options set in the constructor become the defaults for any subsequent calls to update().

opt can be an options object, or a String – equivalent to { text: str }.

Options specific to ThreeJS:

  • flipY (boolean) whether the texture will be Y-flipped (default true)
  • multipage (boolean) whether to construct this geometry with an extra buffer containing page IDs. This is necessary for multi-texture fonts (default false)

The rest of the options are passed to layout-bmfont-text:

  • font (required) the BMFont definition which holds chars, kernings, etc
  • text (string) the text to layout. Newline characters (\n) will cause line breaks
  • width (number, optional) the desired width of the text box, causes word-wrapping and clipping in "pre" mode. Leave as undefined to remove word-wrapping (default behaviour)
  • mode (string) a mode for word-wrapper; can be 'pre' (maintain spacing), or 'nowrap' (collapse whitespace but only break on newline characters), otherwise assumes normal word-wrap behaviour (collapse whitespace, break at width or newlines)
  • align (string) can be "left", "center" or "right" (default: left)
  • letterSpacing (number) the letter spacing in pixels (default: 0)
  • lineHeight (number) the line height in pixels (default to font.common.lineHeight)
  • tabSize (number) the number of spaces to use in a single tab (default 4)
  • start (number) the starting index into the text to layout (default 0)
  • end (number) the ending index (exclusive) into the text to layout (default text.length)

geometry.update(opt)

Re-builds the geometry using the given options. Any options not specified here will default to those set in the constructor.

This method will recompute the text layout and rebuild the WebGL buffers.

opt can be a string, which is equivalent to:

geometry.update({ text: 'new text' })

geometry.layout

This is an instance of layout-bmfont-text. This supports metrics for descender, baseline, xHeight, width, height, capHeight, etc.

geometry.visibleGlyphs

A filtered set from geometry.layout.glyphs intended to align with the vertex data being used by the underlying BufferAttributes.

This is an array of { line, position, index, data } objects, see here. For example, this could be used to add a new BufferAttribute for line offset.

Demos

To run/build the demos:

git clone https://github.com/Jam3/three-bmfont-text.git
cd three-bmfont-text
npm install

Then choose one of the demos to run:

# 3D SDF rendering
npm run test-3d

# 2d bitmap rendering
npm run test-2d

# 2D MSDF rendering
npm run test-msdf

# multi-page rendering
npm run test-multi

# custom text shaders
npm run start

Open up localhost:9966 (it may take a few seconds for the initial bundle). Then when you save the corresponding JS file (in test/) it should re-bundle and trigger a live-reload event on the browser.

To build the distribution demo:

npm run build

Help

Asset Handling

See docs/assets.md

(Multi-)Signed Distance Field Rendering

See docs/sdf.md

Multi-Texture Rendering

See docs/multi.md

See Also

See text-modules for more text and font related tools.

Change Log

  • 3.0.0

    • Fixed BufferAttribute problems in new ThreeJS
  • 2.0.1

    • Added shaders/msdf.js and docs around MSDF usage
  • 2.0.0

    • now uses three-buffer-vertex-data to handle some ThreeJS version differences; this may lead to a slight memory increase
    • constructor holds default options for subsequent calls to update()
    • update() and constructor can take string, treated as { text: str }
    • changed to RawShaderMaterial for proper ThreeJS support across versions
    • SDF shader now uses standard derivatives by default for better anti-aliasing, with a fall back using gl_FragCoord.w
    • SDF shader smooth option has been removed for less API surface area
    • Added precision option to built-in shaders
    • default alphaTest for SDF has changed to 0.0001
    • Multipage shader also includes alphaTest now
  • 1.x

    • uses ShaderMaterial, only really supports r69
    • must call update() with all options desired

License

MIT, see LICENSE.md for details.