Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal shaping and scaling example #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andrewjensen
Copy link

This adds a minimal example that renders a few lines of text onto a new file called minimal.png:

minimal

I'm not confident that the example is fully idiomatic or correct so I'm looking forward to feedback!

@@ -0,0 +1,166 @@
use image::{ImageBuffer, RgbaImage};
Copy link
Author

Choose a reason for hiding this comment

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

Should I do anything else to make the example program discoverable? Maybe a link from README.md or the lib.rs module docs?


shaper.shape_with(|cluster| {
cluster.glyphs.iter().for_each(|glyph| {
let mut scaler = scale_context
Copy link
Author

@andrewjensen andrewjensen Jun 21, 2024

Choose a reason for hiding this comment

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

Should I maybe extract this block into a render_glyph function? The first bit of this is copied from the rendering section of the scale module but I needed to add the extra logic to place the glyph's pixels onto the shared canvas.

Comment on lines +139 to +142
if pixel_r == 255 && pixel_g == 255 && pixel_b == 255 {
// Blank pixel, skip
continue;
}
Copy link
Author

Choose a reason for hiding this comment

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

Should I switch the renderer to use Source::Bitmap instead? Seems like that might let me clean this up.

}

// Copy the value onto the canvas, at an offset position
let canvas_x = glyph_x + pos_x as i32 + run_offset_x.round() as i32; // TODO: I think we might be missing a glyph offset
Copy link
Author

Choose a reason for hiding this comment

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

TODO: I think we might be missing a glyph offset. Am I?

Comment on lines +64 to +92
// We'll need the character map for our font
let charmap = font.charmap();
// And some storage for the cluster we're working with
let mut cluster = CharCluster::new();
// Now we build a cluster parser which takes a script and
// an iterator that yields a Token per character
let mut parser = Parser::new(
Script::Latin,
text_content.char_indices().map(|(i, ch)| Token {
// The character
ch,
// Offset of the character in code units
offset: i as u32,
// Length of the character in code units
len: ch.len_utf8() as u8,
// Character information
info: ch.into(),
// Pass through user data
data: 0,
}),
);
// Loop over all of the clusters
while parser.next(&mut cluster) {
// Map all of the characters in the cluster
// to nominal glyph identifiers
cluster.map(|ch| charmap.map(ch));
// Add the cluster to the shaper
shaper.add_cluster(&cluster);
}
Copy link
Author

Choose a reason for hiding this comment

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

TODO: switch to shaper.add_str(text_content) to make this more minimal

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.

1 participant