月度归档:2021年09月

使用pango在windows中渲染文字的一些细节(2)

上篇文章使用pango在windows中渲染文字的一些细节(1)大致描述了pangov1.45.3中存在的的稍嫌不足之处,本文尝试去解决这些问题。

一、为pango提供SurrogateFallback(替换性备选字体)支持

1、第一步,实现windows系统读取SurrogateFallback的代码

read_windows_surrogate_fallback是在pango的read_windows_fallbacks()中使用,实现时直接传入line_buffer变量,并将当前字体的“替换性备选字体”追加进line_buffer变量中。

static void
read_windows_surrogate_fallback(GString* line_buffer, const wchar_t* fontname)
{
    HKEY hKey;
    LSTATUS status;

    wchar_t szPath[261];
    swprintf_s(szPath, 261, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\LanguagePack\\SurrogateFallback\\%s", fontname);
    status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, &hKey);
    if (status != ERROR_SUCCESS)
    {
        swprintf_s(szPath, 261, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\LanguagePack\\SurrogateFallback");
        status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, &hKey);
    }
    if (status != ERROR_SUCCESS)
        return;

    // Unicode当前规范包含从0x00000-0xfffff总共16个平面(panel)
    for (int i = 2; i < 16; i++)
    {
        wchar_t szPanel[16];
        DWORD dwSize;

        swprintf_s(szPanel, 16, L"Plane%d", i);

        dwSize = sizeof(szPath) / sizeof(szPath[0]);
        status = RegGetValueW(hKey, NULL, szPanel, RRF_RT_REG_SZ, NULL, szPath, &dwSize);
        if (status == ERROR_SUCCESS)
        {
            gchar* sutf8 = g_utf16_to_utf8(szPath, -1, NULL, NULL, NULL);
            g_string_append_printf(line_buffer, ",%s", sutf8);
            g_free(sutf8);
        }
    }

    RegCloseKey(hKey);
}

2、第二步,在pango的read_windows_fallbacks函数中增加SurrogateFallback支持

static void
read_windows_fallbacks (GHashTable *ht_aliases)
{
  ...
  for (value_index = 0; status != ERROR_NO_MORE_ITEMS; value_index++)
    {
      ...
      status = RegEnumValueW (hkey, value_index, name, &name_length,
                              NULL, NULL, NULL, NULL);
      ...
      utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
      ...
      g_string_append_printf (line_buffer,
                              "\"%s\" = \"%s",
                              utf8_name,
                              utf8_name);
      ...
      // 优先加入SurrogateFallback(替换性备选字体)
      read_windows_surrogate_fallback(line_buffer, name);

3、显示效果对比

下图是在windows11(10.0.22000.184)中选用MingLiU字体的显示效果对比。

第一行的pango渲染没有使用SurrogateFallback(替换性备选字体)支持,可以看见数字、英文、及汉字𩽾的显示均与MingLiU字体整体风格不符,尤其是汉字𩽾。

使用pango在windows中渲染文字的一些细节(1)

书写此文的背景

2021年9月使用cairo实现DirectUI时,发现有些特殊文字无法正常显示。

分析cairo及pango的代码后,发现pango虽然已通过fontmap方式解决了大部分文字渲染问题,但在处理Unicode非0平面(Plane)的字符时,仍存在不合理之处。

文字渲染的问题

1、严重错误:某些字符被渲染成“其它字符”比如将”𩽾29F7E“渲染成“齾9f7e“。这是cairo的一个BUG,不具普遍性。

2、无法显示:某些字符被渲染成一个“方块”或“数字码”,这是普遍的文字渲染问题,存在于很多软件中。Windows系统已经处理的很好,而且有完善的编程接口。可以参考:关于Windows TextOut输出文字时的一些难看的“方块”

3、显示不好:已设置为字体A,但部分字符被渲染为其它字体。比如经常看到某些软件或网页中的“少部分”文字的显示效果与其它文字不一样。这也是一个普通性问题。

cairo及pango的现状

一、cairo(v1.16)获取文字glyph索引的方式是完全错误的

没有考虑4字节(2个wchar_t)文字的情况,代码片段如下:

    wchar_t unicode[2];
    ...
    unicode[0] = ucs4;
    unicode[1] = 0;
    if (GetGlyphIndicesW (hdc, unicode, 1, &glyph_index, 0) == GDI_ERROR) {
	...
	glyph_index = 0;
    }
  • cairo官方不建议使用其自身的cairo_show_text渲染文字,而是使用pango。

二、pango(v1.45.3)已通过fontmap解决了“无法显示”的问题

指定字体“无法显示”某个字符的根本原因是:单个字体文件的可包含字符数(2^16)远小于Unicode字符总数(16*2&^16)。当待渲染字符没有被当前字体包含时,将产生“无法显示”问题。

所以在pango中使用fontmap将多个字体关联在一起, 只要这些关联字体的任意一个包含待渲染文字的glyph定义,即可完成渲染。

例如:SimSun(宋体)可以关联MINGLIU(细明)、MSYH(微软雅黑)等字体,渲染字符时,如果宋体里找不到该字符,就再找细明体、微软雅黑体。使用此方式可以解决绝大部分的“无法显示”问题

pango的默认代码中包含了两种fontmap:

1、依据Windows的fontlink信息,fontlink的数据存储在如下注册表位置

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink

2、写在代码内的几个默认定义。开发者可以扩展此方式创建自己的fontmap,还算灵活

static const char * const builtin_aliases[] = {
  "courier = \"courier new\"",
  /* It sucks to use the same GulimChe, MS Gothic, Sylfaen, Kartika,
   * Latha, Mangal and Raavi fonts for all three of sans, serif and
   * mono, but it isn't like there would be much choice. For most
   * non-Latin scripts that Windows includes any font at all for, it
   * has ony one. One solution is to install the free DejaVu fonts
   * that are popular on Linux. They are listed here first.
   */
  "sans = \"dejavu sans,tahoma,arial unicode ms,lucida sans unicode,browallia new,mingliu,simhei,gulimche,ms gothic,sylfaen,kartika,latha,mangal,raavi\"",
  "sans-serif = \"dejavu sans,tahoma,arial unicode ms,lucida sans unicode,browallia new,mingliu,simhei,gulimche,ms gothic,sylfaen,kartika,latha,mangal,raavi\"",
  "serif = \"dejavu serif,georgia,angsana new,mingliu,simsun,gulimche,ms gothic,sylfaen,kartika,latha,mangal,raavi\"",
 "mono = \"dejavu sans mono,courier new,lucida console,courier monothai,mingliu,simsun,SimSun-ExtB,gulimche,ms gothic,sylfaen,kartika,latha,mangal,raavi\"",
  "monospace = \"dejavu sans mono,courier new,lucida console,courier monothai,mingliu,simsun,gulimche,ms gothic,sylfaen,kartika,latha,mangal,raavi\"",
  "emoji = \"segoe ui emoji,segoe ui symbol,segoe ui\"",
  "cursive = \"commic sans ms\"",
  "fantasy = \"gabriola,impact\"",
  "system-ui = \"yu gothic ui,segoe ui,meiryo\"",
};

三、pango(v1.45.3)未考虑Windows的SurrogateFallback机制

Windows的SurrogateFallback机制可以指定Unicode每个平面(Plane)的字符渲染特定字体。这样的直接定位方式,可以避免过大的fontmap产生的效率问题。

例如,Win32系统的如下定义里,第一个”Plane2″=”SimSun-ExtB”指定所有无定义的字体在渲染Plane2字符时,均使用SimSun-ExtB渲染。第二个”Plane2″=”SimSun-ExtB”,特指SimSun字体在渲染Plane2字符时使用SimSun-ExtB渲染。

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\LanguagePack\SurrogateFallback]
"Plane2"="SimSun-ExtB"

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\LanguagePack\SurrogateFallback\SimSun]
"Plane2"="SimSun-ExtB"

四、pango(v1.45.3)未考虑Windows的Font Substitution

Windows的Font Substitution支持在渲染时将字体A替换为字体B,并且可以精确到具体字符集。也就是说,它支持将字体A中的字符集0替换为字体B中的字符集121进行渲染。这些设置保存在如下注册表中:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes