OSDN Git Service

lltjext.sty: baselineshift of JAchar may be not zero
[luatex-ja/luatexja.git] / src / patches / lltjfont.sty
index 3c7ad64..cfff0f0 100644 (file)
@@ -3,7 +3,34 @@
 %
 
 \NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{lltjfont}[2014/01/23 Patch to NFSS2 for LuaTeX-ja]
+\ProvidesPackage{lltjfont}[2017/05/05 Patch to NFSS2 for LuaTeX-ja]
+
+\def\ltj@chardef@text@cmd#1{%
+  \let\@ifdefinable\@@ifdefinable%
+  \edef\ltj@chardef@text@cmd@name{\expandafter\@gobble\string#1}%
+  \afterassignment\ltj@chardef@text@cmd@\ltj@tempcnta
+}
+\def\ltj@chardef@text@cmd@{%
+  \ifnum\ltj@tempcnta>"7F
+    \expandafter\edef\csname \ltj@chardef@text@cmd@name\endcsname{%
+      \noexpand\ltjalchar\the\ltj@tempcnta\space
+    }%
+  \else
+    \expandafter\chardef\csname \ltj@chardef@text@cmd@name\endcsname\ltj@tempcnta
+  \fi
+}
+\def\DeclareTextSymbol#1#2#3{%
+  \@dec@text@cmd\ltj@chardef@text@cmd#1{#2}#3\relax
+}
+
+\ifdefined\UnicodeEncodingName
+  \let\ltj@temp@DeclareTextComposite=\DeclareTextComposite
+  \def\DeclareTextComposite#1#2#3#4{}%
+  \input{tuenc.def}
+  \let\DeclareTextComposite=\ltj@temp@DeclareTextComposite
+  \expandafter\patchcmd\csname TU\string\textasteriskcentered\endcsname{\char}{\ltjalchar}{}{}
+\fi
+
 
 \let\k@encoding\@empty
 \let\ck@encoding\@empty
 \ifdefined\cHT\else\newdimen\cHT\fi
 %\let\afont\font
 
+\let\ystrutbox\strutbox
 \newbox\dstrutbox
 \newbox\tstrutbox
 \newbox\zstrutbox
+\def\strutbox{%
+  \ifnum\ltjgetparameter{direction}=1 \dstrutbox       % dtou
+  \else\ifnum\ltjgetparameter{direction}=4 \ystrutbox  % yoko
+  \else\tstrutbox  % tate and utod
+  \fi\fi
+}
 \def\strut{\relax
   \ifnum\ltjgetparameter{direction}=1 % dtou
     \ifmmode\copy\dstrutbox\else\unhcopy\dstrutbox\fi
   \else\ifnum\ltjgetparameter{direction}=4 % yoko
-    \ifmmode\copy\strutbox\else\unhcopy\strutbox\fi
+    \ifmmode\copy\ystrutbox\else\unhcopy\ystrutbox\fi
   \else % tate and utod
     \ifmmode\copy\tstrutbox\else\unhcopy\tstrutbox\fi
   \fi\fi
 }
+
+\def\ystrut{\relax\hbox{\yoko
+   \ifmmode\copy\ystrutbox\else\unhcopy\ystrutbox\fi}}
 \def\tstrut{\relax\hbox{\tate
    \ifmmode\copy\tstrutbox\else\unhcopy\tstrutbox\fi}}
-\def\zstrut{\relax\hbox{\tate
+\def\dstrut{\relax\hbox{\dtou
+   \ifmmode\copy\dstrutbox\else\unhcopy\dstrutbox\fi}}
+\def\zstrut{\relax\hbox{\utod
    \ifmmode\copy\zstrutbox\else\unhcopy\zstrutbox\fi}}
+
+{%
+  \def\cdp@elt#1#2#3#4{%
+    \directlua{luatexja.jfont.add_fenc_list('\luatexluaescapestring{#1}')}}%
+  \cdp@list%
+}
+
 \def\DeclareFontEncoding{%
   \begingroup
   \nfss@catcodes
                     {\default@family}{\default@series}%
                     {\default@shape}}%
      \expandafter\let\csname#1-cmd\endcsname\@changed@cmd
-     \def\enc@elt{\noexpand\enc@elt}%
+     \directlua{luatexja.jfont.add_fenc_list('\luatexluaescapestring{#1}')}%
   \else
      \@font@info{Redeclaring font encoding #1}%
   \fi
                     {\default@k@family}{\default@k@series}%
                     {\default@k@shape}}%
     \expandafter\let\csname#1-cmd\endcsname\@changed@kcmd
-     \directlua{luatexja.jfont.add_kyenc_list('\luatexluaescapestring{#1}')}%
+    \directlua{luatexja.jfont.add_kyenc_list('\luatexluaescapestring{#1}')}%
   \else
     \@font@info{Redeclaring KANJI (yoko) font encoding #1}%
   \fi
               \@empty
             \else \reserved@a
             \fi
+     \directlua{luatexja.jfont.add_ffam('\luatexluaescapestring{#2}')}%
     }%
 }
 \def\DeclareKanjiFamily#1#2#3{%
               \@empty
             \else \reserved@a
             \fi
+     \directlua{luatexja.jfont.add_kfam('\luatexluaescapestring{#2}')}%
      }%
 }
 \def\DeclareKanjiSubstitution#1#2#3#4{%
       \baselineskip\f@linespread\baselineskip
       \normalbaselineskip\baselineskip
       \adjustbaseline
-      \setbox\strutbox\hbox{\yoko
+      \setbox\ystrutbox\hbox{\yoko
           \vrule\@width\z@
                 \@height.7\baselineskip \@depth.3\baselineskip}%
       \setbox\dstrutbox\hbox{\dtou
       \setbox\tstrutbox\hbox{\tate
           \vrule\@width\z@
                 \@height.5\baselineskip \@depth.5\baselineskip}%
-      \setbox\zstrutbox\hbox{\tate
+      \setbox\zstrutbox\hbox{\utod
           \vrule\@width\z@
                 \@height.7\baselineskip \@depth.3\baselineskip}%
-        \let\size@update\relax}}
-\newbox\adjust@box
-\newdimen\adjust@dimen
-\def\adjustbaseline{%
-    \setbox\adjust@box\hbox{%
-      \ltj@ykblshift=0 \ltj@tkblshift=0漢}%
-    \cht\ht\adjust@box
-    \cdp\dp\adjust@box
-    \cwd\wd\adjust@box
-    \cvs\normalbaselineskip
-    \chs\cwd
-    \cHT\cht \advance\cHT\cdp
-    \setbox0\hbox{\tate\ltj@tkblshift=0漢}%
-    \setbox\adjust@box\hbox{\tate\ltj@tablshift=0 M}%
-    \adjust@dimen\ht\adjust@box
-    \advance\adjust@dimen\dp\adjust@box
-    \advance\adjust@dimen-\ht0
-    \divide\adjust@dimen\tw@
-    \advance\adjust@dimen0.5\dp0
-    \advance\adjust@dimen-\dp\adjust@box
-    \ltj@tablshift=\adjust@dimen}
+     \let\size@update\relax}}
+
+\newluafunction\ltj@@adjust@baseline@inner
+\begingroup\catcode`\~=12
+\directlua{%
+  local nulltable = {}
+  local t = lua.get_functions_table()
+  local getfont, dir_tate = font.getfont, luatexja.dir_table.dir_tate
+  local fmt, get_attr = luatexja.jfont.font_metric_table, tex.getattribute
+  local setdimen, set_attr = tex.setdimen, tex.setattribute
+  local dir = luatexja.unary_pars.direction
+  t[\the\ltj@@adjust@baseline@inner] = function()
+    local ft = fmt[get_attr('ltj@curtfnt')] or nulltable
+    ft = ft and ft.char_type or nulltable
+    local fk = ft and ft[0] or nulltable
+    local ht, dp, wd = fk.height or 0, fk.depth or 0,
+                       fk.width or ft.zw or 0
+    local fm 
+      = ((getfont(font.current()) or nulltable)
+        .characters or nulltable)[77] or nulltable %% 'M'
+    set_attr('ltj@tablshift',
+      0.5*((fm.height or 0)- (fm.depth or 0) - ht+ dp))
+    if dir() ~= dir_tate then
+      ft = fmt[get_attr('ltj@curjfnt')] or nulltable
+      ft = ft and ft.char_type or nulltable
+      fk = ft and ft[0] or nulltable
+      ht, dp, wd = fk.height or 0, fk.depth or 0,
+                   fk.width or ft.zw or 0
+    end
+    setdimen('cht', ht); setdimen('cdp', dp)
+    setdimen('cwd', wd); setdimen('chs', wd)
+    setdimen('cHT', ht+dp)
+  end
+}\endgroup
+\def\adjustbaseline{\luafunction\ltj@@adjust@baseline@inner
+   \cvs\normalbaselineskip}
 \DeclareRobustCommand\romanencoding[1]{%
     \expandafter\ifx\csname T@#1\endcsname\relax
       \@latex@error{Encoding scheme `#1' unknown}\@eha
   \csname D@\k@encoding\endcsname
   \let\kenc@update\relax
   \let\ck@encoding\k@encoding
-  \edef\tmp@enc{'\luatexluaescapestring{\k@encoding}'}
+  \edef\tmp@enc{'\luatexluaescapestring{\k@encoding}'}%
   \directlua{luatexja.jfont.is_kyenc(\tmp@enc)}\ifin@%
     \let\cy@encoding\k@encoding
   \else
 \newif\if@notkfam
 \newif\if@notffam
 \newif\if@tempswz
+\def\@ltj@use@fd{true}
 \DeclareRobustCommand\romanfamily[1]{\edef\f@family{#1}}
 \DeclareRobustCommand\kanjifamily[1]{\edef\k@family{#1}}
 \DeclareRobustCommand\fontfamily[1]{%
   \@notkfamfalse\@notffamfalse
   \edef\tmp@item{'\luatexluaescapestring{#1}'}%
-  \edef\tmp@enc{'\luatexluaescapestring{\k@encoding}'}%
-  \ifcsname \k@encoding+#1\endcsname % if \k@encoding+#1 is already defined
-    \edef\k@family{#1}%
-  \else % search font definition file...
-     \directlua{luatexja.jfont.is_kfam(\tmp@enc, \tmp@item)}%
-     \ifin@\edef\k@family{#1}\else%
-       \directlua{luatexja.jfont.is_Nkfam(\tmp@enc, \tmp@item)}%
-       \ifin@\@notkfamtrue\else%
-         % filename of font definition file is always lowercase
-         \edef\@temp{\lowercase{\noexpand\IfFileExists{\k@encoding#1.fd}}}%
-         \message{(I search kanjifont definition file)}%
-         \@temp{\@tempswztrue}{\@tempswzfalse}%
-         \if@tempswz
-           \directlua{luatexja.jfont.add_kfam_list(\tmp@enc, \tmp@item)}%
-          \edef\k@family{#1}%
-         \else
-           \directlua{luatexja.jfont.add_Nkfam_list(\tmp@enc, \tmp@item)}%
-          \@notkfamtrue%
-         \fi
-       \fi
-     \fi
-  \fi
-  \edef\tmp@enc{'\luatexluaescapestring{\f@encoding}'}%
-  \ifcsname \f@encoding+#1\endcsname
-    \edef\f@family{#1}%
-  \else % search font definition file...
-     \directlua{luatexja.jfont.is_ffam(\tmp@enc, \tmp@item)}%
-     \ifin@\edef\f@family{#1}\else
-       \directlua{luatexja.jfont.is_Nffam(\tmp@enc, \tmp@item)}%
-       \ifin@\@notffamtrue\else
-         % filename of font definition file is always lowercase
-         \edef\@temp{\lowercase{\noexpand\IfFileExists{\f@encoding#1.fd}}}%
-         \message{(I search font definition file)}%
-         \@temp{\@tempswztrue}{\@tempswzfalse}%
-         \if@tempswz
-           \directlua{luatexja.jfont.add_ffam_list(\tmp@enc, \tmp@item)}%
-          \edef\f@family{#1}%
-         \else
-           \directlua{luatexja.jfont.add_Nffam_list(\tmp@enc, \tmp@item)}%
-           \@notffamtrue%
-         \fi
-       \fi
-     \fi
+  %
+  \directlua{luatexja.jfont.search_kfam(\tmp@item, \@ltj@use@fd)}%
+  \ifin@\edef\k@family{#1}\else\@notkfamtrue\fi
+  %
+  % alphabetic fonts part
+  \directlua{luatexja.jfont.is_ffam(\tmp@item)}%
+  \ifin@\edef\f@family{#1}\else
+    % checking is_stored_ffam is not sufficient,
+    % since LuaTeX-ja doesn't know whats fonts are defined before LuaTeX-ja is loaded.
+    \ifcsname \f@encoding+#1\endcsname % easy case
+      \directlua{luatexja.jfont.add_ffam(\tmp@item)}\edef\f@family{#1}%
+    \else
+      \@tempswafalse
+      \def\cdp@elt##1{\ifcsname ##1+#1\endcsname\@tempswatrue\fi}%
+      \directlua{luatexja.jfont.search_ffam_declared()}%
+      \if@tempswz
+        \directlua{luatexja.jfont.add_ffam(\tmp@item)}\edef\f@family{#1}%
+      \else
+        % now we search Nffam_list and font definition files.
+        \directlua{luatexja.jfont.search_ffam_fd(\tmp@item)}%
+        \ifin@\edef\f@family{#1}\else\@notffamtrue\fi
+      \fi
+    \fi
   \fi
+  %
   \if@notkfam\if@notffam
-      {\ifcsname D@\k@encoding\endcsname\@nameuse{D@\k@encoding}\fi
-        \xdef\@@temp{\default@family}}\edef\k@family{\@@temp}%
-      {\ifcsname D@\f@encoding\endcsname\@nameuse{D@\f@encoding}\fi
-        \xdef\@@temp{\default@family}}\edef\f@family{\@@temp}%
+    \edef\k@family{#1}\edef\f@family{#1}%
   \fi\fi
 }
 \DeclareRobustCommand\romanseries[1]{\edef\f@series{#1}}
 }
 \@onlypreamble\romanprocess@table
 \@onlypreamble\kanjiprocess@table
-\DeclareTextCommandDefault{\textunderscore}{%
-  \leavevmode\kern.06em
-  \vbox{\hrule\@width.3em}}
 
 %%%%%% LuaTeX-ja specific
 
 \def\ltj@@does@alt@set#1{%
   \directlua{luatexja.jfont.does_alt_set'\luatexluaescapestring{#1}'}}
 
+%%%% patch \@text@composite because of {y,t}albaselineshift
+
+% %% \pltx@isletter from plfonts.dtx
+% \def\pltx@mark{\pltx@mark@}
+% \let\pltx@scanstop\relax
+% \long\def\pltx@cond#1\fi{%
+%   #1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
+% \long\def\pltx@isletter#1{%
+%   \expandafter\pltx@isletter@i#1\pltx@scanstop}
+% \long\def\pltx@isletter@i#1\pltx@scanstop{%
+%   \pltx@cond\ifx\pltx@mark#1\pltx@mark\fi{\@firstoftwo}%
+%     {\pltx@isletter@ii\pltx@scanstop#1\pltx@scanstop{}#1\pltx@mark}}
+% \long\def\pltx@isletter@ii#1\pltx@scanstop#{%
+%   \pltx@cond\ifx\pltx@mark#1\pltx@mark\fi%
+%     {\pltx@isletter@iii}{\pltx@isletter@iv}}
+% \long\def\pltx@isletter@iii#1\pltx@mark{\@secondoftwo}
+% \long\def\pltx@isletter@iv#1#2#3\pltx@mark{%
+%   \pltx@cond\ifx\pltx@mark#3\pltx@mark\fi{%
+%     \pltx@cond{\ifnum0\ifcat A\noexpand#21\fi\ifcat=\noexpand#21\fi>\z@}\fi
+%       {\@firstoftwo}{\@secondoftwo}%
+%   }{\@secondoftwo}}
+%
+% \def\@text@composite@x#1#2{%
+%   \ifx#1\relax
+%     #2%
+%   \else\pltx@isletter{#1}{#1}{%
+%     \begingroup
+%     \setbox\z@\hbox\bgroup%
+%       \ltj@yablshift\z@ \ltj@tablshift\z@
+%       #1%
+%       \@tempcntb\directlua{luatexja.pltx_composite_last_node_char()}%)
+%       \xdef\pltx@composite@temp{\noexpand\@tempcntb=\the\@tempcntb\relax}%
+%       \aftergroup\pltx@composite@temp
+%     \egroup
+%     \ifnum\ltjgetparameter{direction}=3 %
+%       \@tempdima=\ltj@tablshift sp\else\@tempdima=\ltj@yablshift sp\fi
+%     \ifnum\@tempcntb<\z@\@tempcnta\z@\else
+%       \@tempcnta=\ltjgetparameter{jacharrange}{\ltjgetparameter{chartorange}{\@tempcntb}}\relax
+%       \ifnum\@tempcnta=\z@\@tempdima\z@\fi
+%     \fi
+%     \ifnum\@tempcnta>\z@
+%       \ifodd\ltjgetparameter{alxspmode}{\@tempcntb}\else\leavevmode\hbox{}\fi
+%       \begingroup\mathsurround\z@$%
+%         \hbox{\lower\@tempdima\box\z@}
+%       $\endgroup%
+%       \ifnum\ltjgetparameter{alxspmode}{\@tempcntb}<2\hbox{}\fi
+%     \else
+%       \ifdim\@tempdima=\z@{\ltj@yablshift\z@ \ltj@tablshift\z@#1}%
+%       \else\lower\@tempdima\box\z@\fi
+%     \fi
+%     \endgroup}%
+%   \fi
+% }
+
 \endinput