Width calculation for text rendered in SILE

10 views
Skip to first unread message

Vadim Plessky

unread,
Nov 24, 2024, 8:06:56 AM11/24/24
to SILE Users
Hello everyone!

I just recently explored SILE,  and very excited with its features and capabilities.
I'd like to use it for typeface specimen at a moment.

In order to have left- and right-aligned words rendered in multiple wights, I need to calculate width of text (word) rendered with specific font family, wight and size.
Can someone point out where to look to get required info?

I am ok to look at source code.

I have working code for Cairo library (in Julia and Python).
I need code in Lua/SILE for actions like this
(exctract from code in Julia):

    select_font_face(cr,fontstyle[i][1], Cairo.FONT_SLANT_NORMAL,fontstyle[i][2]);
    set_font_size(cr, ptsize);

    extents = text_extents(cr, w);
    txtxb = extents[1]
    txtyb = extents[2]

    txtwidth = extents[3]
    txtheight = extents[4]

Required value is `txtwidth` whcih would be used later for calculations.
If someone is interested, I can provide complete sniplet of woring code in Julia or Python.

fontstyle[i][1] - font family, for example Roboto
fontstyle[i][2] - font style (in terms of FonConfig, which supports only Normal and Bold)

Best regards,
Vadim Plessky

Didier Willis

unread,
Nov 24, 2024, 12:36:20 PM11/24/24
to SILE Users
Greetings,

> I'd like to use it for typeface specimen at a moment.

You might have noticed the "specimen" package in SILE's core distribution.
There's also the "fontproof" separate modules (https://github.com/sile-typesetter/fontproof) -- I don't know it well and don't know if it's very active, but I am told it was used by some font designers to show-case fonts in various ways.

> I need to calculate width of text (word) rendered with specific font family, wight and size.

An unconventional question with low-level tones ;)

SILE uses Harfbuzz as "shaper" library by default, and it should be possible to do the same kind of low-level things with it as with Cairo, i.e. shape stuff and retrieve various measurements.
(The above-mentioned "specimen" package actually does that kind of things it its "set-to-width" command implementation)
At a higher-level, one could also build an "hbox" and use it's dimensions.

E.g. from the SILE CLI REPL, you see what the gives:
SILE.call("font", { family = "Libertinus Serif", size = "12pt", weight=400 })
pl.pretty.dump(SILE.shaper:shapeToken("mytext", SILE.font.loadDefaults({})))
SILE.typesetter = SILE.typesetters.base()
print(SILE.typesetter:makeHbox({ "mytext" }).width)


N.B. It picks a font, shapes some word and outputs all the gory low-level details; then it instantiates a typesetter (as we are just in the CLI REPL without much loaded at that time, wraps the text in a box and show its width (height and depth fields of that box could of course be queried).

That's some basics to get you bootstrapped. Now you can read the manual on how to implement your own package for these bits of Lua code, implement some cool commands and access them from the markup ;)

Regards
Didier.
Reply all
Reply to author
Forward
0 new messages