Coder Social home page Coder Social logo

fontmetrics.js's Introduction

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.

fontmetrics.js's People

Contributors

pomax avatar oampo avatar

Stargazers

 avatar Nguyen Vu Cuong (Ralph) avatar Max Hoffmann avatar Yves M. avatar Monir Abu Hilal avatar Rustam Garifulin avatar LeeBox avatar Noel avatar blessdyb avatar Ricardo Canelas avatar Dmytro Antonenko avatar kevinyma avatar Steve Piron avatar Bradley Botha avatar Chris Hart avatar Isaac A. Dettman avatar Carlos Martin avatar Kevin Doughty avatar Krishna Biradar avatar Sarah Grefalda avatar Alexei Vanyashin avatar Boaz Hachlili avatar  avatar nazikus avatar electronori avatar foo99 avatar random avatar Shawn Allen avatar Brett Camper avatar  avatar opaopa6969 avatar Michael Anthony avatar Edgar de Oliveira Maciel avatar Wolfger Schramm avatar Bernhard avatar Jakub Jirutka avatar Ain Tohvri avatar Nikolay Astakhayev avatar Dom Christie avatar Brad Pillow avatar Roderic Page avatar Joseph Gimness avatar Lars Schwarz avatar Daniel Weck avatar  avatar Dealga McArdle avatar Hiroaki Yamane avatar  avatar Sebastian Werner avatar tensaix2j avatar Peter Geil avatar Tuan Kuranes avatar Olivier Refalo avatar Devon Govett avatar Matt McCray avatar Chee Aun avatar

Watchers

 avatar James Cloos avatar Michael Anthony avatar Elias Zolotas avatar  avatar

fontmetrics.js's Issues

Index or size was negative, or greater than the allowed value

On a 1024x58px canvas it throws up on fontmetrics.js:136:

[Error] IndexSizeError: DOM Exception 1: Index or size was negative, or greater than the allowed value.

Context:

  var canvas = document.createElement('canvas');
  var width = 1024;
  var height = 58;
  canvas.setAttribute('width', width);
  canvas.setAttribute('height', height);
  var context = canvas.getContext('2d');
  context.font = '48 px Arial';
  context.textAlign = 'center';
  context.fillStyle = 'black';
  context.fillText(text, width * 0.5, height * 0.5);
  console.log('measured', context.measureText(text)); 

Opera and Safari misbehave

Even though Opera and Safari compute the text metrics correctly, based on a correctly rendered canvas as used inside the library, they don't render the normal canvas correctly. I don't know why, but to anyone who does know: please do enlighten me.

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.