【问题标题】:Inverted text when rendering text in Cairo在开罗渲染文本时反转文本
【发布时间】:2013-03-27 16:08:49
【问题描述】:

我正在使用 Cairo 编写一个程序,我需要使用以下代码将坐标从屏幕更改为笛卡尔坐标:

cairo_translate( cr, x, height );
cairo_scale( cr, 1.0, -1.0 ); // FLIP the Y axis

但是当我尝试渲染文本 (cairo_show_text) 时,文本也会被反转,因此无法读取。

使用缩放字体 (cairo_scaled_font_t) 时也会发生同样的情况:

cairo_font_options_t *font_options;
cairo_matrix_t ctm, font_matrix;
cairo_scaled_font_t *scaled_font;


font_options = cairo_font_options_create();
cairo_get_matrix( cr, &ctm );
cairo_get_font_matrix( cr, &font_matrix );

font_matrix.xx = font_matrix.yy = 20.0; // font size

// font_face initialized elsewhere and is valid

scaled_font = cairo_scaled_font_create( font_face, &font_matrix, &ctm, font_options );

cairo_set_scaled_font( cr, scaled_font );

cairo_move_to( cr, 1, 50 );

cairo_show_text( cr, "some text" );

...

我认为矩阵需要一些调整,但我不知道如何调整。任何帮助表示赞赏。提前致谢。

编辑:

文字是颠倒的。例如,“L”看起来像 Gamma,“W”看起来像“M”。

【问题讨论】:

  • 倒置是指沿 X 轴还是 Y 轴?
  • 或者你的意思是颜色反转?
  • 我认为他的意思是颠倒的,因为 Y 刻度是 -1。
  • 很抱歉给您带来了困惑。放松是正确的:颠倒。例如,字母“L”看起来像 Gamma,或“W”看起来像“M”。

标签: c++ c cairo


【解决方案1】:

据我所知,有两种方法可以做到这一点:

cairo_matrix_t ms, mt, m;
cairo_matrix_init_scale(&ms, 1.0f, -1.0f);
cairo_matrix_init_translate(&mt, 0.0f, -m_height);
cairo_matrix_multiply(&m, &mt, &ms);
cairo_set_matrix(cr, &m);

或:

cairo_scale(cr, 1.0f, -1.0f);
cairo_translate(cr, 0.0f, -m_height);

两者都为我解决了问题。通过 y 上的 -1 缩放 cr,但这意味着文本不在表面上,因此通过表面的 -height 进行平移,它应该位于正确的位置。

【讨论】:

    【解决方案2】:

    好吧,您正在翻转 Y 轴,因此文本也会翻转。试试这个:

    cairo_save(cr);
    cairo_reset_clip(cr);
    cairo_show_text(cr, "Some Text");
    cairo_restore(cr);
    

    上面替换了您示例中的 cairo_show_text() 调用。

    【讨论】:

    • 我相信您的代码只会影响“剪辑区域”(这是cairo_reset_clip 所做的),但不会解决文本问题。要翻转文本,需要摆弄坐标变换矩阵。
    • 问题不是说文字是翻转的,而是他想让它不翻转?但是,是的,不知道我对这个 cairo_reset_clip() 调用的想法是什么,我想我的意思是 cairo_identify_transform()。
    • 文本被翻转了,所以添加第二个翻转和取消翻转是一回事。我同意基于 cairo_identity_transform 的解决方案可能有效。
    【解决方案3】:

    由于您将坐标系倒置,文本也会倒置显示。此问题的解决方案是手动翻转文本,方法是设置一个字体矩阵,其垂直比例为负数。您可以通过修改代码来设置字体大小,如下所示:

    double font_size = 20.0;
    font_matrix.xx = font_size;
    font_matrix.yy = -font_size; // negative size to vertically flip text
    

    【讨论】:

      猜你喜欢
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多