#layout #text

glyph_brush_layout

Text layout for ab_glyph

14 releases

0.2.3 Jul 16, 2021
0.2.1 Oct 30, 2020
0.2.0 May 23, 2020
0.1.9 Feb 20, 2020
0.1.2 Sep 21, 2018

#299 in Text processing

Download history 6619/week @ 2021-04-07 5661/week @ 2021-04-14 4782/week @ 2021-04-21 5915/week @ 2021-04-28 5056/week @ 2021-05-05 6079/week @ 2021-05-12 6647/week @ 2021-05-19 6367/week @ 2021-05-26 7463/week @ 2021-06-02 6778/week @ 2021-06-09 6989/week @ 2021-06-16 7339/week @ 2021-06-23 7097/week @ 2021-06-30 6440/week @ 2021-07-07 7979/week @ 2021-07-14 8294/week @ 2021-07-21

27,313 downloads per month
Used in 152 crates (5 directly)

Apache-2.0

64KB
1.5K SLoC

glyph_brush_layout crates.io Documentation

Text layout for ab_glyph.

  • Generic positioning & linebreaking traits.
  • Built-in layout logic:
    • Mixed font & scale sections in a single layout.
    • Horizontal align left/center/right.
    • Vertical align top/center/bottom.
    • Unicode line breaking.
    • Bounded layouts.
use glyph_brush_layout::{ab_glyph::*, *};

let dejavu = FontRef::try_from_slice(include_bytes!("../../fonts/DejaVuSans.ttf"))?;
let garamond = FontRef::try_from_slice(include_bytes!("../../fonts/GaramondNo8-Reg.ttf"))?;

// Simple font mapping: FontId(0) -> deja vu sans, FontId(1) -> garamond
let fonts = &[dejavu, garamond];

// Layout "hello glyph_brush_layout" on an unbounded line with the second
// word suitably bigger, greener and serif-ier.
let glyphs = Layout::default().calculate_glyphs(
    fonts,
    &SectionGeometry {
        screen_position: (150.0, 50.0),
        ..SectionGeometry::default()
    },
    &[
        SectionText {
            text: "hello ",
            scale: PxScale::from(20.0),
            font_id: FontId(0),
        },
        SectionText {
            text: "glyph_brush_layout",
            scale: PxScale::from(25.0),
            font_id: FontId(1),
        },
    ],
);

assert_eq!(glyphs.len(), 24);

let SectionGlyph { glyph, font_id, section_index, byte_index } = &glyphs[4];
assert_eq!(glyph.id, fonts[0].glyph_id('o'));
assert_eq!(*font_id, FontId(0));
assert_eq!(*section_index, 0);
assert_eq!(*byte_index, 4);

let SectionGlyph { glyph, font_id, section_index, byte_index } = &glyphs[14];
assert_eq!(glyph.id, fonts[1].glyph_id('u'));
assert_eq!(*font_id, FontId(1));
assert_eq!(*section_index, 1);
assert_eq!(*byte_index, 8);

Dependencies

~1MB
~18K SLoC