user12050586
user12050586

Reputation:

module for calculating text width

This is often done in javascript to calculate the width of an item, but for various reasons, I need to give this width to the javascript. The module that I'm using is in C++ (compiled via emscripten), and I was wondering if there are any C++ (or C) libraries that are able to calculate the width of text based on a string of text, font-size, and font-family. For example:

int get_pixel_width(char * str, float font_size, char * font_family);

I know there is something to do this in .net but haven't been able to locate anything in C/C++

Upvotes: 2

Views: 449

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490728

It is possible to do this in a platform agnostic way. For example, Harfbuzz (in conjunction with a font engine such as FreeType) can take a text string, and compute the size and location of each glyph in the string, taking into account not only kerning and leading, but also things like glyphs in some scripts have different shapes and locations, depending on whether they're at the beginning, middle, or end of a word.

That said, Harfbuzz is still a fairly low-level library that's intended primarily for use by higher level rendering libraries and such. In most cases, whatever you use for rendering will probably provide a higher-level routine that may well be simpler to use than what Harfbuzz has.

Upvotes: 1

Related Questions