You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust中使用Piston库渲染文本仅显示首字符的问题求助

Rust Piston库文本渲染仅显示第一个字符的解决方法

在使用Rust的Piston库进行文本渲染时,若出现仅第一个字符显示的问题,核心原因是Glyphs对象的字形缓存未被正确提交。Piston的文本绘制依赖Glyphs缓存字形数据,绘制后需手动刷新编码器,确保所有字符的字形都被提交到渲染管线。

原始问题代码

use piston_window::types::Color;
use piston_window::*;

const WIDTH: u32 = 1280;
const HEIGHT: u32 = 720;

pub fn draw_text(
    ctx: &Context,
    graphics: &mut G2d,
    glyphs: &mut Glyphs,
    color: Color,
    pos: [u32; 2],
    text: &str,
) {
    text::Text::new_color(color, 20)
        .draw(
            text,
            glyphs,
            &ctx.draw_state,
            ctx.transform.trans(pos[0] as f64, pos[1] as f64),
            graphics,
        )
        .unwrap();
}

fn main() {
    let mut window: PistonWindow = WindowSettings::new("Ray Marching", [WIDTH, HEIGHT])
        .exit_on_esc(true)
        .graphics_api(OpenGL::V3_2)
        .build()
        .unwrap();

    //Fonts
    let mut glyphs = window.load_font("assets/Coolvetica.otf").unwrap();

    while let Some(event) = window.next() {
        window.draw_2d(&event, |context, graphics, _device| {
            //Clear window
            clear([0.3, 0.3, 0.3, 1.0], graphics);

            //Draw text
            draw_text(
                &context,
                graphics,
                &mut glyphs,
                [1.0, 1.0, 1.0, 1.0],
                [3, 20],
                "Hello World!",
            );
        });
    }
}

修改后的解决代码

在draw_text函数中添加缓存刷新逻辑,确保所有字符被渲染:

use piston_window::types::Color;
use piston_window::*;

const WIDTH: u32 = 1280;
const HEIGHT: u32 = 720;

pub fn draw_text(
    ctx: &Context,
    graphics: &mut G2d,
    glyphs: &mut Glyphs,
    color: Color,
    pos: [u32; 2],
    text: &str,
) {
    text::Text::new_color(color, 20)
        .draw(
            text,
            glyphs,
            &ctx.draw_state,
            ctx.transform.trans(pos[0] as f64, pos[1] as f64),
            graphics,
        )
        .unwrap();
    // 刷新字形编码器,提交所有缓存的字形数据
    glyphs.factory.encoder.flush(graphics);
}

fn main() {
    let mut window: PistonWindow = WindowSettings::new("Ray Marching", [WIDTH, HEIGHT])
        .exit_on_esc(true)
        .graphics_api(OpenGL::V3_2)
        .build()
        .unwrap();

    //Fonts
    let mut glyphs = window.load_font("assets/Coolvetica.otf").unwrap();

    while let Some(event) = window.next() {
        window.draw_2d(&event, |context, graphics, _device| {
            //Clear window
            clear([0.3, 0.3, 0.3, 1.0], graphics);

            //Draw text
            draw_text(
                &context,
                graphics,
                &mut glyphs,
                [1.0, 1.0, 1.0, 1.0],
                [3, 20],
                "Hello World!",
            );
        });
    }
}

额外排查点

  • 确认字体文件路径正确,且文件未损坏;
  • 若问题持续,可尝试更换系统默认字体(如DejaVuSans.ttf),排除字体文件本身的问题。

内容的提问来源于stack exchange,提问作者Bigbug314

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 23:50:07