fschutt/azul

text_layout word_positions_to_inline_text_layout seems to loose words

ronnybremer opened this issue · 2 comments

Description

I have been trying to use the workaround outlined here: PrintPDF Issue 39, however, it looks to me that the above mentioned function is missing words at the end of the passed in text.
For debugging reasons I added a few printlns after calling the following functions:

fn svg_text_layout_from_str(
    text: &str,
    font_bytes: &[u8],
    font_index: u32,
    text_layout_options: text_layout::ResolvedTextLayoutOptions,
    horizontal_alignment: StyleTextAlignmentHorz,
) -> SvgTextLayout {
    //    text_layout_options.font_size_px = SVG_FAKE_FONT_SIZE;
    println!("text: {}", text);
    let words = text_layout::split_text_into_words(text);
    println!("words: {:?}", words);
    let font_metrics = get_font_metrics_freetype(font_bytes, font_index as i32);
    let scaled_words = text_layout::words_to_scaled_words(
        &words,
        font_bytes,
        font_index,
        font_metrics,
        text_layout_options.font_size_px,
    );
    let word_positions = text_layout::position_words(&words, &scaled_words, &text_layout_options);
    let mut inline_text_layout =
        text_layout::word_positions_to_inline_text_layout(&word_positions, &scaled_words);
    println!("inline_text_layout: {:?}", inline_text_layout);
    inline_text_layout.align_children_horizontal(horizontal_alignment);
    println!("inline_text_layout: {:?}", inline_text_layout);
    let layouted_glyphs =
        text_layout::get_layouted_glyphs(&word_positions, &scaled_words, &inline_text_layout);
    SvgTextLayout {
        words,
        scaled_words,
        word_positions,
        layouted_glyphs,
        inline_text_layout,
    }
}

The output is:

text: A-Remarkable-Long-And-Even-Longer Text
words: Words { items: [Word { start: 0, end: 33, word_type: Word }, Word { start: 33, end: 34, word_type: Space }, Word { start: 34, end: 38, word_type: Word }], internal_str: "A-Remarkable-Long-And-Even-Longer Text", internal_chars: ['A', '-', 'R', 'e', 'm', 'a', 'r', 'k', 'a', 'b', 'l', 'e', '-', 'L', 'o', 'n', 'g', '-', 'A', 'n', 'd', '-', 'E', 'v', 'e', 'n', '-', 'L', 'o', 'n', 'g', 'e', 'r', ' ', 'T', 'e', 'x', 't'] }
inline_text_layout: InlineTextLayout { lines: [InlineTextLine { bounds: 254.32813x16.1728 @ (0, 16.1728), word_start: 0, word_end: 2 }] }
inline_text_layout: InlineTextLayout { lines: [InlineTextLine { bounds: 254.32813x16.1728 @ (0, 16.1728), word_start: 0, word_end: 2 }] }

As you can see, inline_text_layout only contains a single line of text.

Here is the output from tripling the text:

text: A-Remarkable-Long-And-Even-Longer Text A-Remarkable-Long-And-Even-Longer Text A-Remarkable-Long-And-Even-Longer Text
words: Words { items: [Word { start: 0, end: 33, word_type: Word }, Word { start: 33, end: 34, word_type: Space }, Word { start: 34, end: 38, word_type: Word }, Word { start: 38, end: 39, word_type: Space }, Word { start: 39, end: 72, word_type: Word }, Word { start: 72, end: 73, word_type: Space }, Word { start: 73, end: 77, word_type: Word }, Word { start: 77, end: 78, word_type: Space }, Word { start: 78, end: 111, word_type: Word }, Word { start: 111, end: 112, word_type: Space }, Word { start: 112, end: 116, word_type: Word }], internal_str: "A-Remarkable-Long-And-Even-Longer Text A-Remarkable-Long-And-Even-Longer Text A-Remarkable-Long-And-Even-Longer Text", internal_chars: ['A', '-', 'R', 'e', 'm', 'a', 'r', 'k', 'a', 'b', 'l', 'e', '-', 'L', 'o', 'n', 'g', '-', 'A', 'n', 'd', '-', 'E', 'v', 'e', 'n', '-', 'L', 'o', 'n', 'g', 'e', 'r', ' ', 'T', 'e', 'x', 't', ' ', 'A', '-', 'R', 'e', 'm', 'a', 'r', 'k', 'a', 'b', 'l', 'e', '-', 'L', 'o', 'n', 'g', '-', 'A', 'n', 'd', '-', 'E', 'v', 'e', 'n', '-', 'L', 'o', 'n', 'g', 'e', 'r', ' ', 'T', 'e', 'x', 't', ' ', 'A', '-', 'R', 'e', 'm', 'a', 'r', 'k', 'a', 'b', 'l', 'e', '-', 'L', 'o', 'n', 'g', '-', 'A', 'n', 'd', '-', 'E', 'v', 'e', 'n', '-', 'L', 'o', 'n', 'g', 'e', 'r', ' ', 'T', 'e', 'x', 't'] }
inline_text_layout: InlineTextLayout { lines: [InlineTextLine { bounds: 258.02344x16.1728 @ (0, 16.1728), word_start: 0, word_end: 2 }, InlineTextLine { bounds: 258.02344x16.1728 @ (0, 35.425026), word_start: 2, word_end: 4 }, InlineTextLine { bounds: 254.32813x16.1728 @ (0, 54.677254), word_start: 4, word_end: 6 }] }
inline_text_layout: InlineTextLayout { lines: [InlineTextLine { bounds: 258.02344x16.1728 @ (0, 16.1728), word_start: 0, word_end: 2 }, InlineTextLine { bounds: 258.02344x16.1728 @ (0, 35.425026), word_start: 2, word_end: 4 }, InlineTextLine { bounds: 254.32813x16.1728 @ (1.8476563, 54.677254), word_start: 4, word_end: 6 }] }
line: InlineTextLine { bounds: 258.02344x16.1728 @ (0, 16.1728), word_start: 0, word_end: 2 }
word: Word { start: 0, end: 33, word_type: Word }
word: Word { start: 33, end: 34, word_type: Space }
line: A-Remarkable-Long-And-Even-Longer
line: InlineTextLine { bounds: 258.02344x16.1728 @ (0, 35.425026), word_start: 2, word_end: 4 }
word: Word { start: 34, end: 38, word_type: Word }
word: Word { start: 38, end: 39, word_type: Space }
line: Text
line: InlineTextLine { bounds: 254.32813x16.1728 @ (1.8476563, 54.677254), word_start: 4, word_end: 6 }
word: Word { start: 39, end: 72, word_type: Word }
word: Word { start: 72, end: 73, word_type: Space }
line: A-Remarkable-Long-And-Even-Longer

I cannot figure out, why the remaining lines are not returned. Is there something I can change in my code?

Version / OS

  • azul version:
    1.0.0-alpha4

  • Operating system:
    Linux CentOS 7

  • Windowing system (X11 or Wayland, Linux only):
    X11

Steps to Reproduce

Looking at the code from Issue 39, the "lorem ipsum" text has actually 135 words, but the PDF attached as an example only shows 63.
I built the code myself and passed in "A-Remarkable-Long-And-Even-Longer Text" and with max-width set to only allow the first word to go in, the function returns a single line only, containing the word plus the space, the second line will not be returned. If I use push_str() to have the same text 3 times, I get 3 lines back, line 1 is word 1 and space, line 2 is word 2, line 3 is word 1 and space. Nothing else.

Additional Information

see PrintPDF issue 39 for an example.

Just looking at the return from position_words, I still get the field number_of_words, not the field number_of_shaped_words. So the build of the version from cradle.io seems to be several years old.
Maybe the current code already fixes the issue, so I will get a git clone and check it again.

The latest code no longer shows that issue.