使用链式替换在 LuaLaTeX 中根据上下文添加替代字符

使用链式替换在 LuaLaTeX 中根据上下文添加替代字符

我正在尝试使用 为 Linux Libertine 添加一些上下文替代fonts.handlers.otf.addfeature。我可以使用普通替换正确添加这些字符,但在使用链式替换时无法识别它们。

我想要的两个替代字符是 +ss02 中的 R 和 K(我不想要它们附带的 J)。这两个字符都位于字体的私有使用部分。通过简单的替换,它们可以作为 和 访问R.altK.alt它们也可以用 调用\Uchar。但我希望替代 R 仅出现在元音之前,而替代 K 仅出现在 n 之前。

下面的代码说明了这个问题。

我的问题是:如何修复所调用的功能,crkalt以便在预期的上下文中识别和使用替代的 R?

(我知道这个优秀的线程关于一般话题。

\documentclass[12pt]{article}
\usepackage{luacode,luatexbase,xcolor}
    
% stylistic alternates for R and K
\directlua{
  fonts.handlers.otf.addfeature {
    name = "rkalt", 
    type = "substitution",
    data =
    {
      ["K"] = "K.alt",  %% works fine
      ["R"] = "R.alt",  %% works fine
    },
  }
  fonts.handlers.otf.addfeature{
    name = "crkalt",
    type = "chainsubstitution",
    lookups = {
      {
          type = "multiple",
          data = {
              ["R"] = "R.alt",   %% does not recognize "R.alt"
          },
      },
      {
          type = "multiple",
          data = {
              ["K"] =  "Khook" , %% "K.alt" not recognized; using Khook to show other code works
          },
      },
      {
          type = "multiple",
          data = {
             ["R"] = "r", %% just to show that the R substitutions are working contextually
          },
      },      
    },
    data = {
      rules = {
        {
          after  = { { "a" , "e", "i", "o", "u" }},
          current = { { "R" } },
          lookups = { 1 },
        },
        {
          after  = { { "n" } },
          current = { { "K" } },
          lookups = { 2 },
        },
        {
          before  = { { "d" }},
          current = { { "R" } },
          lookups = { 3 },
        },
      },
    },
    }
}

% Can call these characters individually
\def\Rswash{\Uchar"E0EC}
\def\Kswash{\Uchar"E0EB}
\def\Khook{\Uchar"0198}


\usepackage{fontspec}
\setmainfont{Linux Libertine O}[%
    Numbers=OldStyle,%
    BoldFont={Linux Libertine O Semibold},%
    BoldFeatures = {Color=violet},
]

%% test string
\def\TestString{Rather Knotty! JR? Re, Ri, Ro, Ru, Rz, dRd}
\newcommand\TryIt[1]{\textsc{\textbf{#1:}} {\addfontfeatures{RawFeature={#1}} \TestString}\par}


\begin{document}
\TestString\par
\TryIt{+ss02}\TryIt{+rkalt}\TryIt{+crkalt}
\Rswash\Kswash\Khook

\end{document}

在此处输入图片描述

答案1

fontspec我通过在块之前加载解决了这个问题directlua

中的某些字形名称Linux Libertine O与默认字体使用的名称不同。(此主题提供了我用来检查字体字形名称的工具。)也许这就是为什么加载顺序很重要。

\documentclass[12pt]{article}
\usepackage{luacode,luatexbase,xcolor}

\usepackage{fontspec}
\setmainfont{Linux Libertine O}[%
    Numbers=OldStyle,%
    BoldFont={Linux Libertine O Semibold},%
    BoldFeatures = {Color=violet},
]
    
% stylistic alternates for R and K
\directlua{
  fonts.handlers.otf.addfeature {
    name = "rkalt", 
    type = "substitution",
    data =
    {
      ["K"] = "K.alt",  
      ["R"] = "R.alt",  
    },
  }
  fonts.handlers.otf.addfeature{
    name = "crkalt",
    type = "chainsubstitution",
    lookups = {
      {
          type = "multiple",
          data = {
              ["R"] = "R.alt", 
          },
      },
      {
          type = "multiple",
          data = {
              ["K"] =  "K.alt" , 
          },
      },
      {
          type = "multiple",
          data = {
             ["R"] = "r", %% just to try a different context for R substitution
          },
      },      
    },
    data = {
      rules = {
        {
          after  = { { "a" , "e", "i", "o", "u" }},
          current = { { "R" } },
          lookups = { 1 },
        },
        {
          after  = { { "n" } },
          current = { { "K" } },
          lookups = { 2 },
        },
        {
          before  = { { "d" }},
          current = { { "R" } },
          lookups = { 3 },
        },
      },
    },
    }
}

% Can call these characters individually, as well
\def\Rswash{\Uchar"E0EC}
\def\Kswash{\Uchar"E0EB}
\def\Khook{\Uchar"0198}

%% test string
\def\TestString{Rather Knotty! JR? Re, Ri, Ro, Ru, Rz, dRd}
\newcommand\TryIt[1]{\textsc{\textbf{#1:}} {\addfontfeatures{RawFeature={#1}} \TestString}\par}

\begin{document}
\TestString\par
\TryIt{+ss02}\TryIt{+rkalt}\TryIt{+crkalt}
\Rswash\Kswash\Khook

\end{document}

在此处输入图片描述

相关内容