summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/fontspec/fontspec-code-patches.dtx
blob: 14037be0757c63ff1119eeeb029d49739206748e (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
%%^^A%%  fontspec-code-patches.dtx -- part of FONTSPEC <wspr.io/fontspec>

% \section{Patching code}
%
%    \begin{macrocode}
%<*fontspec>
%    \end{macrocode}
%
%
% \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_protected: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_protected: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}
%
% In 2019 there will be a new kernel-supported command for generalising `verbatim visible spaces'.
% For now we check if the new command is defined and disable all patching if so.
% In 2020, I suppose, I'll remove all the patching code here.
%
%    \begin{macrocode}
\AtBeginDocument
  {
    \cs_if_exist:NF \verbvisiblespace
      {
				\@@_patch_verb:
				\@@_patch_verbatim:
				\@@_patch_moreverb:
				\@@_patch_fancyvrb:
				\@@_patch_listings:
			}
  }
%    \end{macrocode}
%
%
% \begin{macro}{\verb}
% \begin{macro}{\verb*}
% Redefine \cmd\verb\ to use \cmd\fontspec_print_visible_spaces:.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_patch_verb:
  {
		\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}
%
%
% \begin{environment}{verbatim*}
% With the \pkg{verbatim} package.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_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_new_protected:Npn \@@_patch_moreverb:
  {
    \@ifpackageloaded{moreverb}
      {
        \cs_set:cpn {listingcont*}
          {
            \cs_set:Npn \verbatim@processline
              {
                \thelisting@line \global\advance\listing@line1\relax
                \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_new_protected:Npn \@@_patch_fancyvrb:
  {
    \@ifpackageloaded{fancyvrb}
      {
        \cs_set_eq:NN \FancyVerbSpace \fontspec_visible_space:
      }{}
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new_protected:Npn \@@_patch_listings:
  {
    \@ifpackageloaded{listings}
      {
        \cs_set_eq:NN \lst@visiblespace \fontspec_visible_space:
      }{}
  }
%    \end{macrocode}
%
%
%
%    \begin{macrocode}
%</fontspec>
%    \end{macrocode}


\endinput

% /©
% ------------------------------------------------
% The FONTSPEC package  <wspr.io/fontspec>
% ------------------------------------------------
% Copyright  2004-2019  Will Robertson, LPPL "maintainer"
% Copyright  2009-2015  Khaled Hosny
% Copyright  2013       Philipp Gesang
% Copyright  2013-2016  Joseph Wright
% ------------------------------------------------
% This package is free software and may be redistributed and/or modified under
% the conditions of the LaTeX Project Public License, version 1.3c or higher
% (your choice): <http://www.latex-project.org/lppl/>.
% ------------------------------------------------
% ©/