vicanso/charts-rs

中文显示乱码

scwcd opened this issue · 4 comments

user_hits

代码:

pub fn get_image() -> Vec<u8> {
    let mut horizontal_bar_chart = HorizontalBarChart::new(
        vec![Series::new("频率".to_string(), vec![200.0, 180.0, 100.0])],
        vec!["张三".to_string(), "李四".to_string(), "小明".to_string()],
    );
    // horizontal_bar_chart.font_family = "Noto Sans CJK SC".to_string();
    horizontal_bar_chart.margin.right = 15.0;
    horizontal_bar_chart.series_list[0].label_show = true;
    horizontal_bar_chart.title_align = Align::Left;

    match horizontal_bar_chart.svg() {
        Ok(svg) => match charts_rs::svg_to_png(&svg) {
            Ok(png) => png,
            Err(_) => vec![],
        },
        Err(_) => vec![],
    }
}

错误信息:

2024-03-13T07:15:54.691280Z  WARN usvg::text_to_paths: No fonts with a 频/U+9891 character were found.    
2024-03-13T07:15:54.691321Z  WARN usvg::text_to_paths: No fonts with a 率/U+7387 character were found.    
2024-03-13T07:15:54.692505Z  WARN usvg::text_to_paths: No fonts with a 小/U+5C0F character were found.    
2024-03-13T07:15:54.692518Z  WARN usvg::text_to_paths: No fonts with a 明/U+660E character were found.    
2024-03-13T07:15:54.693530Z  WARN usvg::text_to_paths: No fonts with a 李/U+674E character were found.    
2024-03-13T07:15:54.693543Z  WARN usvg::text_to_paths: No fonts with a 四/U+56DB character were found.    
2024-03-13T07:15:54.694550Z  WARN usvg::text_to_paths: No fonts with a 张/U+5F20 character were found.    
2024-03-13T07:15:54.694564Z  WARN usvg::text_to_paths: No fonts with a 三/U+4E09 character were found. 

操作系统: Ubuntu 22.04

注释了的指定字体集是需要的,不过要确认是否有加载了对应的中文字体,使用get_font_families可以输出当前支持的所有字体集。可以看看示例的实现:https://github.com/vicanso/charts-rs-web/blob/main/src/main.rs#L133

参考了charts-rs-web工程代码,问题已解决。
谢谢!

参考了charts-rs-web工程代码,问题已解决。 谢谢!

能否请教具体怎么解决的吗,要怎么加载字体呀

参考了charts-rs-web工程代码,问题已解决。 谢谢!

能否请教具体怎么解决的吗,要怎么加载字体呀

async fn load_fonts() {
    let bold_opt = match File::open("fonts/NotoSansSC-Bold.otf").await {
        Ok(mut file) => {
            let mut contents = vec![];
            match file.read_to_end(&mut contents).await {
                Ok(_) => Some(contents),
                Err(_) => None,
            }
        }
        Err(_) => None,
    };
    let light_opt = match File::open("fonts/NotoSansSC-Light.otf").await {
        Ok(mut file) => {
            let mut contents = vec![];
            match file.read_to_end(&mut contents).await {
                Ok(_) => Some(contents),
                Err(_) => None,
            }
        }
        Err(_) => None,
    };
    let regular_opt = match File::open("fonts/NotoSansSC-Regular.otf").await {
        Ok(mut file) => {
            let mut contents = vec![];
            match file.read_to_end(&mut contents).await {
                Ok(_) => Some(contents),
                Err(_) => None,
            }
        }
        Err(_) => None,
    };
    if let (Some(bold), Some(light), Some(regular)) = (bold_opt, light_opt, regular_opt) {
       let mut font_buffers: Vec<&[u8]> = vec![];
       font_buffers.push(&bold);
       font_buffers.push(&light);
       font_buffers.push(&regular);
       match charts_rs::get_or_try_init_fonts(Some(font_buffers)) {
          Ok(_) => info!("Font loading successful"),
          Err(e) => error!("Font loading failed: {}", e.to_string()),
      }
   }
}