parley icon indicating copy to clipboard operation
parley copied to clipboard

Layout is empty

Open archseer opened this issue 3 years ago • 1 comments

My code looks something like this, based on your code in https://github.com/dfrg/piet-gpu-text

    // let font = Font::from_file("assets/fonts/Inter Variable/Inter.ttf", 0).unwrap();
    let font = Font::from_file("assets/fonts/ttf/FiraCode-Regular.ttf", 0).unwrap();
    let font = font.as_ref();

    // experimenting with the scaler, glyph outlines are returned fine

    let mut context = ScaleContext::new();
    let mut scaler = context
        .builder(font)
        .hint(true)
        .size(14.)
        .variations(&[("wght", 400.0)])
        .build();
    let glyph_id = font.charmap().map('H');
    let outline = scaler.scale_outline(glyph_id).unwrap();

    // -- Layout

    let mut font_ctx = FontContext::new();
    let font_family = font_ctx.register_fonts(font.data.to_vec()).unwrap();
    let mut layout_ctx: LayoutContext<[u8; 4]> = LayoutContext::new();

    let mut builder = layout_ctx.ranged_builder(&mut font_ctx, "a quick brown fox?", 1.);
    builder.push_default(&StyleProperty::FontStack(FontStack::Single(
        FontFamily::Named(&font_family),
    )));
    builder.push_default(&StyleProperty::FontSize(14.));
    builder.push_default(&StyleProperty::Brush([255, 255, 255, 255]));
    let layout = builder.build();

    for line in layout.lines() {
        let mut last_x = 0.0;
        let mut last_y = 0.0;
        // base transform [1, 0, 0, -1]
        println!("hi!");

        for glyph_run in line.glyph_runs() {
            let run = glyph_run.run();
            // let color = &glyph_run.style().brush.0;
            let font = run.font();
            let font = font.as_ref();

            let mut first = true;

            // TODO: move let scaler here
            for glyph in glyph_run.positioned_glyphs() {
               // ...
            }
        }
    }

I had to make a small modification to font.rs to compile on Linux since Library::default is missing.

let mut builder = LibraryBuilder::default();
builder.build()

For some reason the layout is always empty and contains no data. I'm not relying on any system paths and instead manually registering the font, then using the font family name returned by FontContext.

archseer avatar Apr 15 '22 07:04 archseer

Hmm, turns out I needed to call layout.break_all_lines

archseer avatar Apr 15 '22 07:04 archseer