Git Product home page Git Product logo

parse-bmfont-xml's Introduction

parse-bmfont-xml

stable

Parses XML BMFont files.

Takes a string or Buffer:

var fs = require('fs')
var parse = require('parse-bmfont-xml')

fs.readFileSync(__dirname+'/Arial.fnt', function(err, data) {
  var result = parse(data)
  console.log(result.info.face)   // "Arial"
  console.log(result.pages)       // [ 'sheet0.png' ]
  console.log(result.chars)       // [ ... char data ... ]
  console.log(result.kernings)    // [ ... kernings data ... ]
})

Also works in the browser, for example using XHR:

var parse = require('parse-bmfont-xml')
var xhr = require('xhr')

xhr({ uri: 'fonts/NexaLight32.xml' }, function(err, res, body) {
  if (err)
    throw err
  var result = parse(body)
  console.log(result.info.face)
})

The spec for the returned JSON object is here. The input XML should match the spec with a <font> root element, see [test/Nexa Light-32.fnt](test/Nexa Light-32.fnt) for an example.

See Also

See text-modules for related modules.

Usage

NPM

result = parse(data)

Parses data, a string or Buffer that represents XML data of an AngelCode BMFont file. The returned result object looks like this:

{
     pages: [
         "sheet_0.png", 
         "sheet_1.png"
     ],
     chars: [
         { chnl, height, id, page, width, x, y, xoffset, yoffset, xadvance },
         ...
     ],
     info: { ... },
     common: { ... },
     kernings: [
         { first, second, amount }
     ]
}

If the data is malformed, an error will be thrown.

The browser implementation relies on xml-parse-from-string, which may not work in environments without valid DOM APIs (like CocoonJS).

License

MIT, see LICENSE.md for details.

parse-bmfont-xml's People

Contributors

masihyeganeh avatar mattdesl avatar siyer2 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

parse-bmfont-xml's Issues

Breaks if no kernings?

I tried using parse-bmfont-xml with a font created with Littera, and got the following error:

     TypeError: Cannot read property '0' of undefined
    at node_modules/parse-bmfont-xml/lib/index.js:39:36

I traced that back to a part of the code that dealt with kernings, and realized that my font didn't include a kernings section.

I'm not familiar with the font format, Is having such a section a requirement for a font? Even if it is, would it be wise to check if kernings exists?

I got around it by adding

<kernings count="0"></kernings>

to my font file, but I had to do a bit of digging to find out what was causing the error.

security vulnerability in dependency xml2js

This package depends on vulnerable versions of xml2js. xml2js should be updated to v0.5.0

From npm audit:

xml2js  <0.5.0
Severity: high
xml2js is vulnerable to prototype pollution  - https://github.com/advisories/GHSA-776f-qx25-q3cc

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.