GithubHelp home page GithubHelp logo

layout-bmfont-text's Introduction

layout-bmfont-text

unstable

screenshot

(click for canvas demo)

Provides layout and word-wrapping for left-to-right Latin text, primarily aimed at bitmap font rendering in Canvas/WebGL. The input font should be in the format of BMFont json, see here.

You can use bmfont-lato for testing, or load-bmfont for Node/Browser loading.

var createLayout = require('layout-bmfont-text')
var loadFont = require('load-bmfont')

loadFont('fonts/Arial.fnt', function(err, font) {
  var layout = createLayout({
    font: font,
    text: 'Lorem ipsum dolor\nsit amet',
    width: 300,
    letterSpacing: 2,
    align: 'center'
  })

  //for rendering
  console.log(layout.glyphs)

  //metrics
  console.log(layout.width, layout.height)
  console.log(layout.descender, layout.ascender)
})

Features:

  • uses word-wrapper for layout
    • supports "pre" and "nowrap" modes (like CSS)
    • breaks on explicit newline characters "\n"
  • handles "left", "center" and "right" alignments
  • handles kerning, letter spacing, line height
  • handles space and tab widths
  • provides computed bounds of resulting text box
  • provides metrics for ascender, descender, x-height, etc

Comments/suggestions/PRs welcome.

Usage

NPM

layout = createLayout(opt)

Creates a new layout with the given options.

  • 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)

layout.update(opt)

Updates the layout, all options are the same as in constructor.

layout.glyphs

An array of laid out glyphs that can be used for rendering. Each glyph looks like this:

{
    index: Number,    //the index of this glyph into the string
    data: {...},      //the BMFont "char" object for this glyph
    position: [x, y], //the baseline position to render this glyph
    line: Number      //the line index this glyph appears in
}

All positions are relative to the bottom-left baseline of the text box (i.e. the last line).

layout.width

The width of the text box, or the width provided in constructor.

layout.height

The height of the text box; from baseline to the top of the ascender.

metrics

layout.baseline

The baseline metric: measures top of text layout to the baseline of the first line.

layout.xHeight

The x-height metric; the height of a lowercase character. Uses the first available height of the common lowercase Latin "x-chars", such as 'x', 'u', 'v', 'w', 'z'.

layout.descender

The metric from baseline to the bottom of the descenders (like the bottom of a lowercase "g").

layout.ascender

The metric for ascenders; typically from the top of x-height to the top of the glyph height.

layout.capHeight

The cap height metric; the height of a flat uppercase letter like 'H' or 'I'. Uses the frist available height of common uppercase Latin flat capitals, such as 'H', 'I', 'E', 'T', 'K'.

layout.lineHeight

The line height; the height from one baseline to the next. This is what was passed as opt.lineHeight, or defaults to the font.common.lineHeight.

License

MIT, see LICENSE.md for details.

layout-bmfont-text's People

Contributors

mattdesl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

layout-bmfont-text's Issues

[feature] lineSpacing / lineGap

to act like CSS, so that 1 multiplies the lineHeight by 1, 1.5 multiplies it by 1.5, etc
so it can be independent of font size

Attribute strings / multiple styles

Maybe fit for another module or maybe could be integrated into this one.

Basically supporting multiple font styles (sizes/weights/etc) with different baselines so they all line up.

Since things like color / page ID is entirely decided by the rendering step this would be best to consider across all levels of these modules..

Pixi integration

Some users have requested a pure Pixi implementation of:

  • simple bitmap rendering using PIXI.Texture etc
  • signed distance field rendering for smooth scaling

This could be accomplished with a pixi-bmfont-text module like the ThreeJS one. It's best to wait for Pixi v3 as shaders and static buffers are changing there.

If there is a lot of shared code it could be split into a module, e.g. bmfont-text-vertex-data or something.

Missing dependencies

Thanks for this repo. Very useful.

indexof-property and garnish are missing form package.json but are required for tests. I suggest adding them to devDependencies since they don't appear to be necessary for the actual running code.

Other dependencies could use an update as well. The start and stress scripts are failing, and I suspect it may be because of an outdated dependency.

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.