Add minimal shaping and scaling example#53
Conversation
| @@ -0,0 +1,166 @@ | |||
| use image::{ImageBuffer, RgbaImage}; | |||
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| if pixel_r == 255 && pixel_g == 255 && pixel_b == 255 { | ||
| // Blank pixel, skip | ||
| continue; | ||
| } |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
TODO: I think we might be missing a glyph offset. Am I?
| // 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); | ||
| } |
There was a problem hiding this comment.
TODO: switch to shaper.add_str(text_content) to make this more minimal
This adds a minimal example that renders a few lines of text onto a new file called
minimal.png:I'm not confident that the example is fully idiomatic or correct so I'm looking forward to feedback!