-
Notifications
You must be signed in to change notification settings - Fork 95
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
Support for Paragraphs/Word Wrapping #2
Comments
I'll see if I can do a simple I was working on a scale-bar generator and noticed that centering text is really cumbersome right now. Currently you still need to import freetype-rs into your project and calculate the position manually. PDF only starts the text at the lower left corner, everything else is up to you. Something like this (for centered text): /// Calculates the lower left of a centered string, to position it correctly in the document
///
/// Parameters:
///
/// - text: The actual text that will be embedded in the document
/// - font_scale: The current scale of the font
/// - parent_width: the width of the element in which relation the text should be centered
/// - font_face: The font-face of the text
///
pub fn calc_lower_left_for_centered_text(text: &String, font_scale: i64, parent_width: f64, font_face: &freetype::Face)
-> f64
{
let s_w = calc_text_width_pt(text, font_scale, font_face);
((mm_to_pt!(parent_width) / 2.0) - (s_w / 2.0)) * 0.352778
}
pub fn calc_text_width_pt(text: &String, font_scale: i64, font_face: &freetype::Face)
-> f64
{
// vertical scale for the space character
let vert_scale = {
if let Ok(ch) = font_face.load_char(0x0020, ft::face::NO_SCALE) {
font_face.glyph().metrics().vertAdvance
} else {
1000
}
};
// calculate the width of the text in unscaled units
let sum_width = text.chars().fold(0, |acc, ch|
if let Ok(ch) = font_face.load_char(ch as usize, ft::face::NO_SCALE) {
let glyph_w = font_face.glyph().metrics().horiAdvance;
acc + glyph_w
} else { acc }
);
sum_width as f64 / (vert_scale as f64 / font_scale as f64)
} The Right-aligned text is done the same way, essentially. Once you have the space of the text that will fill one line, it's simple to do alignment. Block text is more difficult, because there you have to distribute spaces sort-of evenly. I'll see what I can do. |
I was thinking maybe as a "wrapper" function(s), like with |
Lemme see if I can do a PR for you |
Hello, |
@TKGgunter yeah, I haven't worked on this ... mostly because I don't use builtin fonts. It isn't possible to calculate the width and height from builtin fonts, because you'd need the .ttf or .otf file to do that - which, for builtin fonts, doesn't exist. I'd also really like to do layout in a seperate library, not in |
Is it still not possible to calculate the width of a built-in font? |
No, how would that be possible? Built-in fonts can even vary from PDF viewer to PDF viewer. You'd need the |
So I'm wondering if there is a way to either support word wrapping when writing text so newlines are automatically inserted, or provide some metrics around glyph sizes so it can be handled externally.
The idea is to be able to support a paragraph of text which wraps when the width is full.
Supporting centered and right aligned text is an obvious extension of this, but is harder to do.
The text was updated successfully, but these errors were encountered: