- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 101
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
Potential Way get length of text at certain fontsize #49
Comments
Wrong crate, this is a PDF library. I've written See here: use azul_text_layout::{
text_layout::{split_text_into_words, words_to_scaled_words},
text_shaping::get_font_metrics_freetype,
};
let font = include_bytes!("Helvetica.ttf");
let font_index = 0; // only for fonts with font collections
let font_metrics = get_font_metrics_freetype(&font, font_index);
let words = split_text_into_words("hello");
let scaled_words = words_to_scaled_words(&words, font, font_index, font_metrics, font_size);
let total_width = scaled_words.items.iter().map(|i| i.word_width).sum(); |
Note: this doesn't take into account the width of the spaces between the words. If you want to do a full text layout (including more advanced things like line breaking, line offset, custom character / word spacing) azul-text-layout does support that too, see here: #39 (comment) |
Fantastic stuff, if want to account for the width of spaces as well It looks like I can also use These are some great libraries. |
Potentially related, is there a way to set the local origin on a line of text to be the top left, as opposed to bottom left ? (i.e. such that the text draws downwards from the origin) let font_metrics = get_font_metrics_freetype(font, font_index as i32);
let font_height = font_metrics.get_height(fontsize); Unfortunately this |
@fschutt You wrote printpdf is the wrong crate. I am curious how would you implement something where you have to "right align" numeric values and so on. From my naive point of view I would see the need to calculate the width of a text with specific parameters (font etc.). Is this a use case where printpdf is not the right tool for the job? |
Yes. printpdf does not do text layout. I've written another crate azul-text-layout with which you can use to calculate the per-line offset. PDF does not know what "right align" means, it only knows left-align. So you have to calculate an "offset" for each line of text that you want to right-align. Text layout / text shaping is more complicated that most people realize. Which is why it's the job of a text layout library, not a PDF library. |
@fschutt thanks for answering, but I still don't understand correctly, azul-text-layout is not a PDF document creation lib, right? What would be the way when I have to generate a PDF with Rust that should display numbers right-aligned? You mean use azul-text-layout only for calculation purposes and printpdf for the rest?. Somehow this has to be possible since it is also possible in libraries in other programming languages. Even if it might be complicated ... can you give me some advice? |
Yes. There is some code here to get you started: #39 (comment) and on the docs.rs page Essentially you'll want to set |
For anyone else stumbling upon this, I went for an alternative route and used rusttype and extracted this function from genpdf-rs to do the text measurements
And converted the width of the text to MM in 72dpi like |
@ninjacato You just have to be aware that rusttype has very rudimentary font shaping and no WOFF2 file support (while azul-text-layout uses the allsorts font shaping engine and does have these features). |
printpdf |
I have been wondering if it would be possible to get the length of a line of text for a certain font and at a certain font size.
Something along the lines of
Any idea how I might go about implementing something like this ?
Or where I should start ?
I'm newish to rust, but I'm keen to give it a shot.
The text was updated successfully, but these errors were encountered: