fix: glyph overflow in text layout, affecting text_size and draw_text_mut #767
Open
var4yn wants to merge 3 commits intoimage-rs:mainfrom
Open
fix: glyph overflow in text layout, affecting text_size and draw_text_mut #767var4yn wants to merge 3 commits intoimage-rs:mainfrom
text_size and draw_text_mut #767var4yn wants to merge 3 commits intoimage-rs:mainfrom
Conversation
Contributor
Author
|
For old tests - I'm ready to do this if this PR is of interest |
Contributor
Author
|
I just noticed that the fix isn't very visible on the DejaVu font. |
Contributor
Author
|
Code to generate Before/After images: use image::RgbaImage;
macro_rules! txt_img {
($func_name:ident, $imageproc: ident) => {
pub fn $func_name(word: &str) -> RgbaImage {
let mplantin = include_bytes!("../fonts/mplantin.ttf");
let mplantin = ab_glyph::FontArc::try_from_slice(mplantin).unwrap();
let font_size = 128.0;
let (w, h) = $imageproc::drawing::text_size(font_size, &mplantin, word);
let mut image = RgbaImage::from_pixel(w, h, image::Rgba([0, 0, 0, 255]));
$imageproc::drawing::draw_text_mut(
&mut image,
image::Rgba([100u8, 100u8, 100u8, 255u8]),
0,
0,
font_size,
&mplantin,
word,
);
image
}
};
}
txt_img!(txt_img_before_fix, imageproc);
txt_img!(txt_img_after_fix, imageproc_2);
fn main() {
let words = [
("ÁÄaaa", "out"),
("Á", "symbol2"),
("Äabcpghky", "symbols"),
("abcpghky", "symbols_little"),
("1.", "number"),
];
for (word, filename) in words {
let img_before = txt_img_before_fix(word);
let img_after = txt_img_after_fix(word);
img_before.save(format!("{}.png", filename)).ok();
img_after.save(format!("{}_after_fix.png", filename)).ok();
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ISSUE: #710
Problem
When rendering glyphs that extend beyond the font's ascent line (e.g.
Ä,Ö,Ü),the returned
text_sizedid not account for the overshoot, causing the rendered textto be clipped.
Solution
Added an second cycle in
layout_glyphsto computeabove_ascentandbelow_ascentvalues.
above_y: the minimum value among glyphs that extend above the ascent line (less than zero).below_y: the minimum value among glyphs that extend below the ascent line (greater than zero).These values are used to calculate the correct
heightand correctoffset_yinlayout_glyphs.Before / After
Upd: The font from the issue is used. The changes are clearly visible.