LuaTeX:UTF-8 字元擷取,是比另一種更準確和/或更可取的方法

LuaTeX:UTF-8 字元擷取,是比另一種更準確和/或更可取的方法

在嘗試從 TeX 框中提取 UTF-8 字串的方法時,我發現了用戶的帖子麥可-H21這裡:UTF-8 文字擷取。在查看了字形資料在節點列表中的儲存方式之後,我修改了程式碼以查看另一種方法是否有效。在我的方法中,我遍歷組成的複雜字形/圓盤的組件以提取組成字元。在他的方法中,他似乎將複雜的字形(如連字)傳遞給某些函數來分解它。我們的兩個程式碼(對於範例中的測試字串)列印的輸出看起來相同。有人可以檢查一下,並建議這兩種方法在功能上是否同樣正確(我知道我的程式碼需要對 TeX 連字進行特殊處理,請忽略它)。如果是,哪一個對性能更好(我可以unicode.utf8.char像他一樣在我的程式碼中緩存,請忽略任何關於性能的評論中的差異)。

這是寫入終端機和輸出檔 hello.txt 的輸出文字:Příliš žluťoučký kůň úpěl ďábelské ódy difference diffierence.他的完整程式碼位於UTF-8 文字擷取,我們的程式碼不同的地方是我不使用他的以下函數(get_unicode),而只是堅持unicode.utf8.char(glyphnodename.char)應用於字形組件(而他應用此函數get_unicode來分解複雜的字形,而不是在字形節點中更深層次地挖掘以獲得分解的字形[據我所知])。

local function get_unicode(xchar,font_id)
    local current = {}
    local uchar = identifiers[font_id].characters[xchar].tounicode
    for i= 1, string.len(uchar), 4 do
      local cchar = string.sub(uchar, i, i + 3)
      print(xchar,uchar,cchar, font_id, i)
      table.insert(current,char(tonumber(cchar,16)))
    end
    return current
  end
\documentclass{article}
\usepackage[lmargin=0.5in,tmargin=0.5in,rmargin=0.5in,bmargin=0.5in]{geometry}
\usepackage{fontspec}
\usepackage{microtype}
\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\setbox0=\hbox{Příliš žluťoučký \textit{kůň} úpěl \hbox{ďábelské} ódy difference diffierence.}

\directlua{
  local glyph_id = node.id("glyph")
  local disc_id = node.id("disc")
  local glue_id  = node.id("glue")
  local hlist_id = node.id("hlist")
  local vlist_id = node.id("vlist")
  local minglue = tex.sp("0.2em")
  local function nodeText(n)
    local t =  {}
    for x in node.traverse(n) do
      % glyph node
      if x.id == glyph_id then
        if bit32.band(x.subtype,2) \csstring~=0 and unicode.utf8.char(x.char) \csstring~="“" and unicode.utf8.char(x.char) \csstring~="”" then %
          for g in node.traverse_id(glyph_id,x.components) do
            if bit32.band(g.subtype, 2) \csstring~=0 then
              for gc in node.traverse_id(glyph_id,g.components) do
                table.insert(t,unicode.utf8.char(gc.char))
              end
            else
              table.insert(t,unicode.utf8.char(g.char))
            end
          end
        else
          table.insert(t,unicode.utf8.char(x.char))
        end
      % disc node
      elseif x.id == disc_id then
        for g in node.traverse_id(glyph_id,x.replace) do
          if bit32.band(g.subtype, 2) \csstring~=0 then
            for gc in node.traverse_id(glyph_id,g.components) do
              table.insert(t,unicode.utf8.char(gc.char))
            end
          else
            table.insert(t,unicode.utf8.char(g.char))
          end
        end
        % glue node
      elseif x.id == glue_id and  node.getglue(x) > minglue then      
        table.insert(t," ")
      elseif x.id == hlist_id or x.id == vlist_id then
        table.insert(t,nodeText(x.head))
      end
    end
    return table.concat(t)
  end
  local n = tex.getbox(0)
  print(nodeText(n.head))
  local f = io.open("hello.txt","w")
  f:write(nodeText(n.head))
  f:close()

}

\box0

\end{document}

相關內容