(&mut self, font_idx: usize, ch: char)
| 321 | const SDF_SPREAD: f32 = 6.0; |
| 322 | |
| 323 | fn rasterize_sdf_glyph(&mut self, font_idx: usize, ch: char) -> &GlyphInfo { |
| 324 | let key = (font_idx, ch); |
| 325 | if self.sdf_glyph_cache.contains_key(&key) { |
| 326 | return &self.sdf_glyph_cache[&key]; |
| 327 | } |
| 328 | |
| 329 | let font = &self.fonts[font_idx]; |
| 330 | let (metrics, bitmap) = font.rasterize(ch, Self::SDF_BASE_SIZE as f32); |
| 331 | let gw = metrics.width as u32; |
| 332 | let gh = metrics.height as u32; |
| 333 | |
| 334 | // Generate SDF from bitmap |
| 335 | let sdf_data = if gw > 0 && gh > 0 { |
| 336 | generate_sdf(&bitmap, gw, gh, Self::SDF_SPREAD) |
| 337 | } else { |
| 338 | Vec::new() |
| 339 | }; |
| 340 | |
| 341 | // Place in SDF atlas |
| 342 | if self.sdf_atlas_cursor_x + gw > self.atlas_width { |
| 343 | self.sdf_atlas_cursor_x = 0; |
| 344 | self.sdf_atlas_cursor_y += self.sdf_atlas_row_height; |
| 345 | self.sdf_atlas_row_height = 0; |
| 346 | } |
| 347 | |
| 348 | let ax = self.sdf_atlas_cursor_x; |
| 349 | let ay = self.sdf_atlas_cursor_y; |
| 350 | |
| 351 | for row in 0..gh { |
| 352 | for col in 0..gw { |
| 353 | let src = ((row * gw + col) * 4) as usize; |
| 354 | let dst = (((ay + row) * self.atlas_width + ax + col) * 4) as usize; |
| 355 | if src + 3 < sdf_data.len() && dst + 3 < self.sdf_atlas_data.len() { |
| 356 | self.sdf_atlas_data[dst] = sdf_data[src]; |
| 357 | self.sdf_atlas_data[dst + 1] = sdf_data[src + 1]; |
| 358 | self.sdf_atlas_data[dst + 2] = sdf_data[src + 2]; |
| 359 | self.sdf_atlas_data[dst + 3] = sdf_data[src + 3]; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | self.sdf_atlas_cursor_x += gw + 1; |
| 365 | if gh + 1 > self.sdf_atlas_row_height { |
| 366 | self.sdf_atlas_row_height = gh + 1; |
| 367 | } |
| 368 | self.sdf_atlas_dirty = true; |
| 369 | |
| 370 | self.sdf_glyph_cache.insert(key, GlyphInfo { |
| 371 | atlas_x: ax, |
| 372 | atlas_y: ay, |
| 373 | width: gw, |
| 374 | height: gh, |
| 375 | advance: metrics.advance_width, |
| 376 | x_offset: metrics.xmin as f32, |
| 377 | y_offset: metrics.ymin as f32, |
| 378 | }); |
| 379 | |
| 380 | &self.sdf_glyph_cache[&key] |
no test coverage detected