Skip to content

glifo: Add mask bitmap support - #2016

Open
redstrate wants to merge 1 commit into
linebender:mainfrom
redstrate:work/josh/mask-font
Open

redstrate wants to merge 1 commit into
linebender:mainfrom
redstrate:work/josh/mask-font

Conversation

@redstrate

Copy link
Copy Markdown

This is used by legacy bitmap fonts like Terminus, all we need to do it seems is expand the 8-bit mask into RGBA data.

Here's the font now working in the parley text editor example:

image

Not sure if I need to add a test for this :-)

@squeaktoy

Copy link
Copy Markdown

Very nice. Is there a way to make it not look blurry, though?

@LaurenzV LaurenzV left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I need to add a test for this :-)

If you have access to an AI tool, it should be easy enough to just have it generate a py script that generates such a font (see the existing sbix example ). Otherwise, happy to do this myself if you prefer. :)

Comment thread glifo/src/glyph.rs Outdated
let width = g.width;
let height = g.height;
if let Ok(data) = mask.decode(width, height) {
let rgba = data.iter().flat_map(|&x| [x, x, x, x]).collect::<Vec<_>>();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the glyphs represent an alpha mask, I think the read value should only be applied to the alpha channel, not to the RGB ones. In addition, we should also take the context_color into consideration.

So something like this probably?

let [r, g, b, a] = context_color.to_rgba8().to_u8_array();
let rgba = data
    .iter()
    .flat_map(|&m| [r, g, b, ((u16::from(a) * u16::from(m) + 127) / 255) as u8])
    .collect::<Vec<_>>();

We also need to change the cache key of bitmap glyphs so they are not hardcoded to black, but the actual context color.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me, I didn't even know that was available. The one thing I don't understand though is the math:

255 (context) * 128 (mask) = 128. Okay, makes sense.

But if we include the + 127 then everything drifts positive by 0.498039215686275. It actually becomes 128.498039215686275?

@redstrate redstrate Oct 22, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind I did a little bit of searching and it is for rounding, I was typing all of the math into a calculator that doesn't truncate like the integers would do here

Comment thread glifo/src/glyph.rs Outdated
@LaurenzV

Copy link
Copy Markdown
Collaborator

Very nice. Is there a way to make it not look blurry, though?

That's likely because we are scaling the bitmaps with bilinear filtering. I'm not sure if using nearest-neighbor is better though, as it will lead to aliasing artifacts in case the font size doesn't exactly match...

@squeaktoy

Copy link
Copy Markdown

Very nice. Is there a way to make it not look blurry, though?

That's likely because we are scaling the bitmaps with bilinear filtering. I'm not sure if using nearest-neighbor is better though, as it will lead to aliasing artifacts in case the font size doesn't exactly match...

In Qt and GTK the font size is set to the real sizes of the bitmap font. There's no in-between and thus no scaling. Is something like that possible here too?

This is used by legacy bitmap fonts like Terminus, all we need to do it
seems is expand the 8-bit mask into RGBA data.
@redstrate
redstrate force-pushed the work/josh/mask-font branch from 1f57be4 to 93be70a Compare October 22, 2026 13:08
@redstrate

Copy link
Copy Markdown
Author

Not sure if I need to add a test for this :-)

If you have access to an AI tool, it should be easy enough to just have it generate a py script that generates such a font (see the existing sbix example ). Otherwise, happy to do this myself if you prefer. :)

I took a look and didn't understand a single lick of it, so it's probably better if you do it!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants