Giter Site home page Giter Site logo

soulwire / fontmetrics Goto Github PK

View Code? Open in Web Editor NEW
175.0 175.0 21.0 29 KB

A lightweight JavaScript library for computing accurate font metrics such as x-height, cap height, ascent, descent and tittle for any loaded web font.

Home Page: https://soulwire.github.io/FontMetrics/

License: MIT License

HTML 2.71% JavaScript 97.29%

fontmetrics's People

Contributors

soulwire 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  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

fontmetrics's Issues

Wrong bottom value or absent line-height metric

Take 48px Roboto font.

fontMetrics gives 74 pixels line-height (or top - bottom) value:

let fontMetrics = require('fontmetrics')
let m = fontMetrics({
  origin: 'top',
  fontFamily: 'Roboto',
  fontSize: 48
})
m.bottom * 48
// 74

That is seen in demo as
image

Measuring it manually by comparing top and bottom text baselines, gives different value:

let canvas = document.body.appendChild(document.createElement('canvas'))
canvas.height = canvas.width = 100
let ctx = canvas.getContext('2d')
ctx.font = '48px Roboto'
ctx.textBaseline = 'top'
ctx.fillText('Sphinx', 0, 0)

let a = first(ctx.getImageData(0, 0, 100, 100))

ctx.clearRect(0, 0, 100, 100)
ctx.textBaseline = 'bottom'
ctx.fillText('Sphinx', 0, 100)

let b = first(ctx.getImageData(0, 0, 100, 100))

let lineHeight = canvas.height - b + a

image

That perfectly corresponds to the bottom value defined in https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline.

I wonder what is the meaning of bottom property in FontMetrics? That makes it impossible to calculate middle value, corresponding to context.textBaseline = 'middle' alignment. An attempt to use it in gl-text failed.

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.