summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/fontspec/fontspec-patches.dtx
blob: ca1ac793c295a8530663f6a652c860af907f39f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
% \section{Patching code}
%
%    \begin{macrocode}
%<*fontspec>
%    \end{macrocode}
%
% \subsection{\cmd\-}
%
% \begin{macro}{\-}
% This macro is courtesy of Frank Mittelbach and the \LaTeXe\ source code.
%    \begin{macrocode}
\DeclareRobustCommand{\-}
 {
  \discretionary
   {
    \char\ifnum\hyphenchar\font<\z@
           \xlx@defaulthyphenchar
         \else
           \hyphenchar\font
         \fi
   }{}{}
 }
\def\xlx@defaulthyphenchar{`\-}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Verbatims}
%
% Many thanks to Apostolos Syropoulos for discovering this problem and writing the  redefinion of \LaTeX's |verbatim| environment and \cs{verb*} command.
%
% \begin{macro}{\fontspec_visible_space:}
% Print \unichar{2423}{Open box}, which is used to visibly display a space character.
%    \begin{macrocode}
\cs_new:Nn \fontspec_visible_space:
 {
  \@@_primitive_font_glyph_if_exist:NnTF \font {"2423}
   { \char"2423\scan_stop: }
   { \fontspec_visible_space_fallback: }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fontspec_visible_space_fallback:}
% If the current font doesn't have \unichar{2423}{Open box}, use Latin Modern Mono instead.
%    \begin{macrocode}
\cs_new:Nn \fontspec_visible_space_fallback:
 {
  {
   \usefont{\g_fontspec_encoding_tl}{lmtt}{\f@series}{\f@shape}
   \textvisiblespace
  }
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fontspec_print_visible_spaces:}
% Helper macro to turn spaces (\verb|^^20|) active and print visible space instead.
%    \begin{macrocode}
\group_begin:
\char_set_catcode_active:n{"20}%
\cs_gset:Npn\fontspec_print_visible_spaces:{%
\char_set_catcode_active:n{"20}%
\cs_set_eq:NN^^20\fontspec_visible_space:%
}%
\group_end:
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\verb}
% \begin{macro}{\verb*}
% Redefine \cmd\verb\ to use \cmd\fontspec_print_visible_spaces:.
%    \begin{macrocode}
\def\verb
 {
  \relax\ifmmode\hbox\else\leavevmode\null\fi
  \bgroup
    \verb@eol@error \let\do\@makeother \dospecials
    \verbatim@font\@noligs
    \@ifstar\@@sverb\@verb
 }
\def\@@sverb{\fontspec_print_visible_spaces:\@sverb}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% It's better to put small things into \cmd\AtBeginDocument, so here we go:
%    \begin{macrocode}
\AtBeginDocument
 {
  \fontspec_patch_verbatim:
  \fontspec_patch_moreverb:
  \fontspec_patch_fancyvrb:
  \fontspec_patch_listings:
 }
%    \end{macrocode}
%
% \begin{environment}{verbatim*}
% With the \pkg{verbatim} package.
%    \begin{macrocode}
\cs_set:Npn \fontspec_patch_verbatim:
 {
  \@ifpackageloaded{verbatim}
   {
    \cs_set:cpn {verbatim*}
     {
      \group_begin: \@verbatim \fontspec_print_visible_spaces: \verbatim@start
     }
   }
%    \end{macrocode}
% This is for vanilla \LaTeX.
%    \begin{macrocode}
   {
    \cs_set:cpn {verbatim*}
     {
      \@verbatim \fontspec_print_visible_spaces: \@sxverbatim
     }
   }
 }
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{listingcont*}
% This is for \pkg{moreverb}.
% The main |listing*| environment inherits this definition.
%    \begin{macrocode}
\cs_set:Npn \fontspec_patch_moreverb:
 {
  \@ifpackageloaded{moreverb}{
    \cs_set:cpn {listingcont*}
     {
      \cs_set:Npn \verbatim@processline
       {
        \thelisting@line \global\advance\listing@line\c_one
        \the\verbatim@line\par
       }
      \@verbatim \fontspec_print_visible_spaces: \verbatim@start
     }
  }{}
 }
%    \end{macrocode}
% \end{environment}
%
% \pkg{listings} and \pkg{fancvrb} make things nice and easy:
%    \begin{macrocode}
\cs_set:Npn \fontspec_patch_fancyvrb:
 {
  \@ifpackageloaded{fancyvrb}
   {
    \cs_set_eq:NN \FancyVerbSpace \fontspec_visible_space:
   }{}
 }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_set:Npn \fontspec_patch_listings:
 {
  \@ifpackageloaded{listings}
   {
    \cs_set_eq:NN \lst@visiblespace \fontspec_visible_space:
   }{}
 }
%    \end{macrocode}
%
% \subsection{\cs{oldstylenums}}
%
%
% \begin{macro}{\oldstylenums}
% \begin{macro}{\liningnums}
% This command obviously needs a redefinition.
% And we may as well provide the reverse command.
%    \begin{macrocode}
\RenewDocumentCommand \oldstylenums {m}
 {
  { \addfontfeature{Numbers=OldStyle} #1 }
 }
\NewDocumentCommand \liningnums {m}
 {
  { \addfontfeature{Numbers=Lining} #1 }
 }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%    \begin{macrocode}
%</fontspec>
%    \end{macrocode}