Draw text using SDF atlas. The text scales smoothly to any size.
(
&mut self,
renderer: &mut Renderer,
font_idx: usize,
text: &str,
x: f64,
y: f64,
size: u32,
spacing: f32,
r: f64, g: f64, b: f
| 396 | |
| 397 | /// Draw text using SDF atlas. The text scales smoothly to any size. |
| 398 | pub fn draw_text_sdf( |
| 399 | &mut self, |
| 400 | renderer: &mut Renderer, |
| 401 | font_idx: usize, |
| 402 | text: &str, |
| 403 | x: f64, |
| 404 | y: f64, |
| 405 | size: u32, |
| 406 | spacing: f32, |
| 407 | r: f64, g: f64, b: f64, a: f64, |
| 408 | ) { |
| 409 | let idx = if font_idx < self.fonts.len() { font_idx } else { 0 }; |
| 410 | let scale = size as f32 / Self::SDF_BASE_SIZE as f32; |
| 411 | |
| 412 | for ch in text.chars() { |
| 413 | self.rasterize_sdf_glyph(idx, ch); |
| 414 | } |
| 415 | self.ensure_sdf_atlas_uploaded(renderer); |
| 416 | |
| 417 | let atlas_bg = match self.sdf_atlas_bind_group_idx { |
| 418 | Some(idx) => idx, |
| 419 | None => return, |
| 420 | }; |
| 421 | |
| 422 | let color = [ |
| 423 | (r / 255.0) as f32, |
| 424 | (g / 255.0) as f32, |
| 425 | (b / 255.0) as f32, |
| 426 | (a / 255.0) as f32, |
| 427 | ]; |
| 428 | |
| 429 | let aw = self.atlas_width as f32; |
| 430 | let ah = self.atlas_height as f32; |
| 431 | |
| 432 | let mut cursor_x = x as f32; |
| 433 | let mut first = true; |
| 434 | for ch in text.chars() { |
| 435 | if !first && spacing != 0.0 { cursor_x += spacing * scale; } |
| 436 | first = false; |
| 437 | let key = (idx, ch); |
| 438 | if let Some(glyph) = self.sdf_glyph_cache.get(&key) { |
| 439 | let gx = cursor_x + glyph.x_offset * scale; |
| 440 | let gy = y as f32 - glyph.y_offset * scale - glyph.height as f32 * scale + size as f32; |
| 441 | let gw = glyph.width as f32 * scale; |
| 442 | let gh = glyph.height as f32 * scale; |
| 443 | |
| 444 | if gw > 0.0 && gh > 0.0 { |
| 445 | let u0 = glyph.atlas_x as f32 / aw; |
| 446 | let v0 = glyph.atlas_y as f32 / ah; |
| 447 | let u1 = (glyph.atlas_x + glyph.width) as f32 / aw; |
| 448 | let v1 = (glyph.atlas_y + glyph.height) as f32 / ah; |
| 449 | renderer.draw_textured_quad(gx, gy, gw, gh, u0, v0, u1, v1, color, atlas_bg); |
| 450 | } |
| 451 | |
| 452 | cursor_x += glyph.advance * scale; |
| 453 | } |
| 454 | } |
| 455 | } |
nothing calls this directly
no test coverage detected