LuaTeX: UTF-8 문자 추출은 다른 것보다 더 정확하거나 바람직합니다.

LuaTeX: UTF-8 문자 추출은 다른 것보다 더 정확하거나 바람직합니다.

TeX 상자에서 UTF-8 문자열을 추출하는 방법을 실험하던 중 사용자의 게시물을 발견했습니다.미칼-h21여기:UTF-8 텍스트 추출. Glyph 데이터가 nodelist에 저장되는 방식을 살펴본 후 다른 접근 방식이 작동하는지 확인하기 위해 코드를 수정했습니다. 내 접근 방식에서는 구성된 복잡한 글리프/디스크의 구성 요소를 탐색하여 구성 문자를 추출합니다. 그의 접근 방식에서 그는 합자와 같은 복잡한 문자 모양을 일부 함수에 전달하여 분해하는 것 같습니다. 두 코드(예제의 테스트 문자열)에 의해 인쇄된 출력은 동일해 보입니다. 누군가 검토하고 두 가지 접근 방식이 모두 기능적으로 올바른지 제안해 주실 수 있습니까? (내 코드에 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}

관련 정보