0

在 C++ 游戏中,我们使用 Pango 用 cairo 渲染文本,然后从那里渲染到 OpenGL 纹理。我最近在通过 Pango 实现文本换行时注意到了这个问题。

我正在做的是通过将我们自己的坐标系转换为像素来计算宽度,然后将其用作 PANGO UNITS 或代码中窗口宽度的一部分:

        float screenEdge = _w * m * static_cast<float>(PANGO_SCALE);
        float tempMaxWidth = _owner->m_wrapping.m_maxWidth;
        tempMaxWidth = std::min(_owner->m_wrapping.m_maxWidth, (0.48f - _owner->dimensions().x1()));
        float wrapWidth = screenEdge * tempMaxWidth;
        maxWidth = static_cast<int>(std::round(wrapWidth));
        pango_layout_set_width(_owner->m_pangoLayout.get(), maxWidth);

然后我们从布局中得到一个矩形,传递给 cairo:

        float border = 2.0f;
        PangoRectangle lRec;
        pango_layout_get_pixel_extents(_owner->m_pangoLayout.get(), nullptr, &lRec);
        m_width = (lRec.width + border);  // Add twice half a border for margins
        m_height = (lRec.height + border);
    std::unique_ptr<cairo_surface_t, decltype(&cairo_surface_destroy)> m_cairoSurface(
      cairo_image_surface_create(CAIRO_FORMAT_ARGB32, m_width, m_height),
      &cairo_surface_destroy);
    std::unique_ptr<cairo_t, decltype(&cairo_destroy)> m_cairoDc(
      cairo_create(m_cairoSurface.get()),
      &cairo_destroy);
    cairo_set_antialias(m_cairoDc.get(), CAIRO_ANTIALIAS_FAST);
    cairo_push_group_with_content (m_cairoDc.get(), CAIRO_CONTENT_COLOR_ALPHA);
    cairo_set_operator(m_cairoDc.get(), CAIRO_OPERATOR_SOURCE);
    // Add Pango line and path to proper position on the DC
    cairo_move_to(m_cairoDc.get(), (0.5f * border), (0.5f * border));  // Margins needed for border stroke to fit in

但是正如您在下面看到的那样,输出是截止的;如果我在这里使用搜索,这不会发生PANGO_ALIGN_LEFT ,我发现Cairo + Pango 布局非左对齐

这似乎与我的问题有关,但并没有完全提供解决方案(或者我没有正确理解);我尝试计算逻辑矩形和墨迹矩形的 x 和 y 坐标之间的差异,并将其添加到cairo_move_to输出中,但这对输出没有真正的影响,所以我只是在这里粘贴了原始代码。

纹理输出(应该是

4

0 回答 0