question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to know the baseline and x value for font.getPath() ?

See original GitHub issue

Hello, this question have already been asked but the answer given didn’t work : var baseline = (font.ascender / font.unitsPerEm * fontSize); So since it’s been a while maybe now there is a solution for it.

Expected Behavior

When I use font.getPath(‘text’, 0,0,40) => the text will be cut on the top or the bottom, or the right… And I would like to know the value of x and y relatively to the font and text. Or to make it simple, i would like the text to not be cut.

Steps to Reproduce (for bugs)

I made a small repo to visualize the text generated

  1. git clone https://github.com/microSoftware/display-font.git
  2. npm install
  3. node opentype.js (or nodemon opentype.js if you want to watch for modif)
  4. modify the words to see which one work and don’t work
  5. Go on http://localhost:8900 to see the result

Thank you

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
fdbcommented, Apr 19, 2017

font.getPath draws the text with 0,0 being the baseline.

So, given a function hline that looks like this:

function hline(y) {
    ctx.beginPath();
    ctx.moveTo(-9999, Math.round(y)-0.5);
    ctx.lineTo(9999, Math.round(y)-0.5);
    ctx.stroke();
}

You can see the relationship between baseline, ascender and descender here: this code:

// Translate the canvas 150 pixels down.
ctx.translate(0, 150);
// Draw the baseline.
hline(0);

// Calculate / draw ascender and descender.
var fontSize = 64;
var fontScale = 1 / font.unitsPerEm * fontSize;
// Note that ascender / descender are bottom-up, so we need to flip (negate) them.
var topY = -(font.ascender * fontScale);
var bottomY = -(font.descender * fontScale);
ctx.strokeStyle = '#66f';
hline(topY);
hline(bottomY);

// Draw the text at 0,0 (baseline).
var path = font.getPath('The quick brown fox jumps over the lazy dog', 0, 0, fontSize);
path.draw(ctx);

Will draw the text like this, with ascender/descender in blue:

screen shot 2017-04-19 at 14 35 28

Working example in JSFiddle

1reaction
axkibecommented, Apr 20, 2017

Use path.getBoundingBox()

Then y1 of that is what you seek.

But note that ‘X’ is for example in most fonts taller than ‘o’, so both letters would be positioned to the top if you use it like that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

opentype.js - npm
This value determines the size of the grid. Common values are 2048 and 4096. Font.getPath(text, x, y, fontSize, options).
Read more >
OpenType JS and FabricJS text convert to path - Stack Overflow
Calling font.getPath(string, x, y, fontSize) will "draw" a path from bottom to top: Example: draw text element at x=500, y=250
Read more >
nodebox/opentype.js - Gitter
does anyone know how to implement a fallback font for opentype.parse(arraybuffer)?. or the appropriate way to handle missing glyphs? like ✩.
Read more >
SkFont Class Reference - Skia
getPath() Modifies path to be the outline of the glyph. If the glyph has an outline, modifies path to be the glyph's outline...
Read more >
opentype.js.Font JavaScript and Node.js code examples
let font = opts.fontFile ? opentype.loadSync(opts.fontFile) : defaultFont let svgString = font.getPath(str, x, y, opts.fontSize).toSVG()
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found