MCPcopy Create free account
hub / github.com/17cupsofcoffee/nova / new

Method new

src/graphics/text.rs:51–113  ·  view source on GitHub ↗
(gfx: &Graphics, font: &Font, size: f32)

Source from the content-addressed store, hash-verified

49
50impl SpriteFont {
51 pub fn new(gfx: &Graphics, font: &Font, size: f32) -> SpriteFont {
52 // TODO: Refactor to pack then allocate
53 let mut packer = ShelfPacker::new(gfx, 256, 256);
54 let mut cache = HashMap::new();
55 let mut kerning = HashMap::new();
56
57 let line_metrics = font.data.horizontal_line_metrics(size).unwrap();
58
59 for ch in 32u8..128 {
60 let ch = ch as char;
61
62 let (metrics, data) = font.data.rasterize(ch, size);
63
64 let image = if !data.is_empty() {
65 let data: Vec<u8> = data.into_iter().flat_map(|x| [x, x, x, x]).collect();
66
67 let uv = packer
68 .insert(
69 &data,
70 metrics.width as i32,
71 metrics.height as i32,
72 ATLAS_PADDING,
73 )
74 .expect("out of space");
75
76 Some(SpriteFontGlyphImage {
77 offset: Vec2::new(
78 metrics.bounds.xmin - ATLAS_PADDING as f32,
79 -metrics.bounds.height - metrics.bounds.ymin - ATLAS_PADDING as f32,
80 ),
81 uv: Rectangle::new(uv.x as f32, uv.y as f32, uv.width as f32, uv.height as f32),
82 })
83 } else {
84 None
85 };
86
87 cache.insert(
88 ch,
89 SpriteFontGlyph {
90 advance: metrics.advance_width,
91 image,
92 },
93 );
94
95 for ch2 in 32u8..128 {
96 let ch2 = ch2 as char;
97
98 if let Some(k) = font.data.horizontal_kern(ch, ch2, size) {
99 kerning.insert((ch, ch2), k);
100 }
101 }
102 }
103
104 SpriteFont {
105 ascent: line_metrics.ascent,
106 descent: line_metrics.descent,
107 line_gap: line_metrics.line_gap,
108

Callers

nothing calls this directly

Calls 2

insertMethod · 0.80
into_textureMethod · 0.45

Tested by

no test coverage detected