Skip to content

A JavaScript library that extends the Canvas2D context.measureText(<String>) with additional typographical metrics

Notifications You must be signed in to change notification settings

Pomax/fontmetrics.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

This library updates the canvas2d ctx.measureText(<String>) function
so that it returns not just a string's width, but also its height,
ascent wrt the baseline, descent wrt the baseline, the leading distance
(the number of pixels between the baselines of two consecutive lines
of text, pronounced like the metal "lead", not like the management
"leader"), as well as the string's bounding box.

Simply include on a page as:

  <script type="text/javascript" src="fontmetrics.js"></script>

and measureText will return a much more complete object, structured
as follows:

{
  fontsize: <number>, /* as set on the canvas/context */
  width: <number>,    /* typesetting width of the text */
  height: <number>,   /* typesetting height of the text */
  ascent: <number>,   /* height above the baseline for this text */
  descent: <number>,  /* depth below the baseline for this text */
  leading: <number>,  /* the 'lead' (metal) spacing between the baselines of two lines */
  bounds:             /* the bounding box for the rasterised text on a canvas */
  { 
    minx: <number>, /* can be negative */
    miny: <number>, /* can be negative */
    maxx: <number>, /* not necessarily the same as width */
    maxy: <number>  /* not necessarily the same as height */
  }
}

Note: all numbers represent screen pixels (not typographical points)

See http://pomax.github.com/fontmetrics.js/ for a demonstration.

This library is covered by an MIT (Expat flavour) license.

About

A JavaScript library that extends the Canvas2D context.measureText(<String>) with additional typographical metrics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages