servo / servo/font-kit

How to render text into one line with a given font?

Open
#204 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
840
Forks
137
Avg merge
4h 56m
Merged PRs (30d)
1

Description

How can I render a whole line of text into a colored bitmap?

I made some attempts, but encountered the following problems:

  1. The width of the space is 0
  2. Emoji rendering without color.

My render function is as follows

fn render_text(text: &str, font: &Font, font_size: f32) -> anyhow::Result<Canvas> {
    let mut chars = vec![];
    for char in text.chars() {
        let glyph_id = match font.glyph_for_char(char) {
            Some(s) => s,
            None => Err(format!("No such char `{char}` in font `{}`", font.family_name()))?,
        };
        let size = font.raster_bounds(
            glyph_id,
            font_size,
            Transform2F::from_translation(Vector2F::new(0.0, font_size)),
            HintingOptions::None,
            RasterizationOptions::SubpixelAa,
        )?;
        chars.push((glyph_id, size));
    }
    let mut x = 0.0;
    let mut canvas = Canvas::new(Vector2I::new(font_size as i32), Format::A8);
    for (glyph_id, rect) in chars {
        font.rasterize_glyph(
            &mut canvas,
            glyph_id,
            font_size,
            Transform2F::from_translation(Vector2F::new(x, font_size)),
            HintingOptions::None,
            RasterizationOptions::SubpixelAa,
        )?;
        x += rect.size().x() as f32;
    }
    Ok(canvas)
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shown render_text function and inspect Font::glyph_for_char, Font::raster_bounds, Font::rasterize_glyph, and Canvas::new. Determine how the existing rendering path should handle space width and colored emoji, then verify that a complete text line is rendered into the requested colored bitmap.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.