Module kas_text::raster[][src]

Expand description

Support for rastering glyphs

This module is only available if ab_glyph, fontdue or both features are enabled.

Example

TextDisplay::glyphs and TextDisplay::glyphs_with_effects yield a sequence of glyphs which may be drawn roughly as follows:

use std::collections::HashMap;
use kas_text::{Glyph, Vec2, TextDisplay};
use kas_text::fonts::{FaceId};
use kas_text::raster::{Config, raster, SpriteDescriptor, Sprite};

#[derive(Default)]
struct DrawText {
    config: Config,
    cache: HashMap::<SpriteDescriptor, Option<Sprite>>,
}

impl DrawText {
    fn text(&mut self, pos: Vec2, text: &TextDisplay) {
        // Ensure input position is not fractional:
        let pos = Vec2(pos.0.round(), pos.1.round());

        let config = &self.config;
        let cache = &mut self.cache;

        let for_glyph = |face: FaceId, dpem: f32, glyph: Glyph| {
            let desc = SpriteDescriptor::new(config, face, glyph, dpem);
            let opt_sprite = cache.entry(desc).or_insert_with(|| {
                let opt_sprite = raster(config, desc);
                if let Some(ref sprite) = opt_sprite {
                    // upload sprite to GPU here ...
                }
                opt_sprite
            });
            if let Some(sprite) = opt_sprite {
                let offset = Vec2(sprite.offset.0 as f32, sprite.offset.1 as f32);
                // Here we must discard sub-pixel position with floor:
                let glyph_pos = Vec2(glyph.position.0.floor(), glyph.position.1.floor());
                let a = pos + glyph_pos + offset;
                let b = a + Vec2(sprite.size.0 as f32, sprite.size.1 as f32);
                // draw rect from a to b
            }
        };

        text.glyphs(for_glyph);
    }
}

Structs

Raster configuration

A rastered sprite

A Sprite descriptor

Functions

Raster a glyph