summaryrefslogtreecommitdiff
path: root/tex/context/base/font-xtx.mkii
blob: 5f4b85879af60b73d500943f18669ba1c1eebc55 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
%D \module
%D   [       file=font-xtx,
%D        version=2004.09.11,
%D          title=\CONTEXT\ Font Macros,
%D       subtitle=\XETEX\ Hacks,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\ifnum\texengine=\xetexengine
    \writestatus{loading}{ConTeXt Font Macros / XeTeX Hacks}
\else
    \endinput
\fi

\unprotect

%D Loading:

%D for some reason xetex does not support [filename] for tfm files and
%D quotes also behave kind of strange " vs ' vs [ vs ...
%D
%D \starttyping
%D \font\myfont =  msam7         % ok
%D \font\myfont = "msam7"        % also ok
%D \font\myfont = "msam7" at 8pt % error
%D \stoptyping

\newconditional\tracexetexfonts

%D Because \XETEX\ is not that fast on locating fonts we cache lookups so
%D that we minimize the test. It saves a little bit of runtime, depending
%D on the number of fonts loaded (which is normally not that much).

\def\doiffoundXTXfontelse#1#2%
  {\ifcsname xtx@fnt@#2\somefontspec\endcsname
     \ifconditional\tracexetexfonts
       \writestatus\m!fonts{already checked #1: #2\somefontspec\space (state: \number\csname xtx@fnt@#2\somefontspec\endcsname)}%
     \fi
   \else
     \suppressfontnotfounderror\plusone
     \font\xetextempfont=#2\somefontspec\relax
     \suppressfontnotfounderror\zerocount
     \edef\xetextempfont{\fontname\xetextempfont}%
     \global\expandafter\chardef\csname xtx@fnt@#2\somefontspec\endcsname
     \ifx\xetextempfont\nullfontname
       \zerocount \ifconditional\tracexetexfonts
         \writestatus\m!fonts{not found #1: #2\somefontspec}%
       \fi
     \else
       \plusone \ifconditional\tracexetexfonts
         \writestatus\m!fonts{found #1: #2\somefontspec}%
       \fi
     \fi
   \fi
   \ifcase\csname xtx@fnt@#2\somefontspec\endcsname
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi}

\def\docheckfontfilenameprefix#1:#2:#3#4\relax
  {\edef\!!stringa{#1}%
   \edef\!!stringb{#2}%
   \ifx\!!stringb\empty
     % no prefix
     \let\checkedfontfile\!!stringa
     \doiffoundXTXfontelse{1a}{\checkedfontfile\checkedfontfeatures}
       {\edef\checkedfontfile{\checkedfontfile\checkedfontfeatures}}
       {\doiffoundXTXfontelse{1b}{"\checkedfontfile\checkedfontfeatures"}
          {\edef\checkedfontfile{"\checkedfontfile\checkedfontfeatures"}}
          {\doiffoundXTXfontelse{1c}{"[\checkedfontfile]\checkedfontfeatures"}
             {\edef\checkedfontfile{"[\checkedfontfile]\checkedfontfeatures"}}
             {}}}%
   \else\ifx\!!stringa\v!file
     % force file, only file check when no spaces
     \let\checkedfontfile\!!stringb
     \doiffoundXTXfontelse{2a}{"[\checkedfontfile]\checkedfontfeatures"}
       {\edef\checkedfontfile{"[\checkedfontfile]\checkedfontfeatures"}}
       {\doiffoundXTXfontelse{2b}{"\checkedfontfile\checkedfontfeatures"}
          {\edef\checkedfontfile{"\checkedfontfile\checkedfontfeatures"}}
          {}}%
   \else\ifx\!!stringa\v!name
     % force name, always lookup by xetex itself, "" forces otf/ttf/type1
     \edef\checkedfontfile{"\!!stringb\checkedfontfeatures"}%
     \ifconditional\tracexetexfonts
       \writestatus\m!fonts{no checking 3a: \checkedfontfile}%
     \fi
   \else
     % whatever, maybe even xetex spec, forget about features
     \edef\checkedfontfile{"\!!stringa\!!stringb"}%
     \ifconditional\tracexetexfonts
       \writestatus\m!fonts{no checking 3b: \checkedfontfile}%
     \fi
   \fi\fi\fi}

\def\checkfontfilename% -- todo: integrate so that we call do.. directly
  {\expandafter\docheckfontfilename\fontfile*\empty*\relax}

\def\docheckfontfilename#1*#2#3*#4\relax % class overrules file
  {\edef\checkedfontfeatures
     {\expandafter\ifx\csname\fontclass\s!features\endcsname\empty
        \ifx\@@fontfeatures\empty\ifx#2\empty\else#2#3\fi\else\@@fontfeatures\fi
      \else\expandafter\ifx\csname\fontclass\s!features\endcsname\relax % redundant, will go away
        \ifx\@@fontfeatures\empty\ifx#2\empty\else#2#3\fi\else\@@fontfeatures\fi
      \else
        \csname\fontclass\s!features\endcsname
      \fi\fi}%
   \ifx\checkedfontfeatures\empty
     % done
   \else
     \edef\checkedfontfeatures{\executeifdefined{\??fa\checkedfontfeatures}\empty}%
     \ifx\checkedfontfeatures\empty
       % done
     \else
       \let\convertedfontfeatures\empty
       \processcommacommand[\checkedfontfeatures]\doconvertfontfeatures % raw
       \ifx\convertedfontfeatures\empty
         \let\checkedfontfeatures\empty
       \else
         \edef\checkedfontfeatures{:\convertedfontfeatures}%
       \fi
     \fi
   \fi
   \docheckfontfilenameprefix#1:\empty:\empty\relax
   \doshowcheckedfontfeatures}

\def\dodoconvertfontfeatures#1=#2#3=#4\relax
  {\ifx#2\empty
     % invalid feature
   \else\ifcsname @xtx@#1@#2#3\endcsname
       \expandafter\ifx\csname @xtx@#1@#2#3\endcsname\empty\else
         \edef\convertedfontfeatures{\convertedfontfeatures\csname @xtx@#1@#2#3\endcsname;}%
       \fi
   \else
     \edef\!!stringa{#1}%
     \edef\!!stringb{#2#3}%
     \edef\convertedfontfeatures
       {\convertedfontfeatures
        \ifx\!!stringb\v!yes
          +\!!stringa
        \else\ifx\!!stringb\v!no
          -\!!stringa
        \else
          \!!stringa=\!!stringb
        \fi\fi;}%
   \fi\fi}

\def\doconvertfontfeatures#1%
  {\dodoconvertfontfeatures#1=\empty=\relax}

\def\remapfontfeature #1 #2 #3 {\setevalue{@xtx@#1@#2}{#3}}

% this may move to another file, maybe font-xtx

\remapfontfeature tlig          yes  mapping=tlig
%remapfontfeature tlig          no   mapping=
\remapfontfeature trep          yes  {}
\remapfontfeature trep          no   {}
\remapfontfeature texligatures  yes  mapping=tlig
%remapfontfeature texligatures  no   mapping=
%remapfontfeature texquotes     yes  mapping=tex-text
%remapfontfeature texquotes     no   mapping=

%D Variants:

\unexpanded\def\variant[#1]%
  {\dosetscaledfont
   \font\variantfont\truefontname{\fontstringA\fontstylesuffix\fontvariant\fontstringA{#1}} at \scaledfont
   \variantfont}

%D Possible optimizations:

% \def\updatefontparameters
%   {\edef\@@fontfeatures{\truefontdata\fontfile\s!features}%
%    \edef\@@fontskewchar{\truefontdata\fontfile\s!skewchar}}

% \def\setfontcharacteristics
%   {\updatefontparameters % redundant, will go away, faster too
%    \the\everyfont}

% \let\synchronizepatternswithfont\relax

%D Names:

% We need to move the feature into the filename else it may be
% overloaded by another reference. For instance the definition of
% a regular and caps variant can use the same font.

% We could use an indirect method ... store in 'array' and refer to
% slot.

\def\definefontsynonym[#1]#2[#3]%
  {\edef\@@fontname{#1}%
   \edef\@@fontfile{#3}%
   \doifnextoptionalelse\dodefinefontsynonym\nodefinefontsynonym}

\def\nodefinefontsynonym
  {\@EA\let\csname\??ff\fontclass\@@fontname\endcsname\@@fontfile}

\def\dodefinefontsynonym[#1]%
  {\edef\@@fontdata{#1}%
   \ifx\@@fontdata\empty
     \nodefinefontsynonym
   \else
     \ifx\fontclass\empty
       \getfontparameters
     \else
       \getglobalfontparameters
     \fi
     \ifcsname\??ff\@@fontfile\s!features\endcsname
       \@EA\edef\csname\??ff\fontclass\@@fontname\endcsname{\@@fontfile*\csname\??ff\@@fontfile\s!features\endcsname}%
       \@EA\let\csname\??ff\@@fontfile\s!features\endcsname\undefined
     \else
       \nodefinefontsynonym
     \fi
   \fi}

\let\definefontfile\definefontsynonym % dedicated to Taco Hoekwater

% simple version
%
% \def\truefontname#1%
%   {\@EA\dotruefontname#1*\relax}
%
% \def\dotruefontname#1*#2\relax
%   {\ifcsname\??ff\fontclass#1\endcsname
%      \@EA\truefontname\csname\??ff\fontclass#1\endcsname
%    \else\ifcsname\??ff#1\endcsname
%      \@EA\truefontname\csname\??ff#1\endcsname
%    \else
%      #1%
%    \fi\fi}
%
% last counts
%
% \def\truefontname#1%
%   {\@EA\dotruefontname#1*\empty*\relax}
%
% \def\dotruefontname#1*#2#3*#4\relax
%   {\ifcsname\??ff\fontclass#1\endcsname
%      \ifx#2\empty
%        \@EA\truefontname\csname\??ff\fontclass#1\endcsname
%      \else
%        \@EA\truefontname\csname\??ff\fontclass#1\endcsname*#2#3%
%      \fi
%    \else\ifcsname\??ff#1\endcsname
%      \ifx#2\empty
%        \@EA\truefontname\csname\??ff#1\endcsname
%      \else
%        \@EA\truefontname\csname\??ff#1\endcsname*#2#3%
%      \fi
%    \else
%      \ifx#2\empty
%        #1%
%      \else
%        #1*#2#3%
%      \fi
%    \fi\fi}
%
% first counts

\def\truefontname#1%
  {\@EA\dotruefontname#1*\empty*\relax}

\def\dotruefontname#1*#2#3*#4\relax
  {\ifcsname\??ff\fontclass#1\endcsname
     \ifx#2\empty
       \@EA\truefontname\csname\??ff\fontclass#1\endcsname
     \else
       \@EA\redotruefontname\csname\??ff\fontclass#1\endcsname*#2#3%
     \fi
   \else\ifcsname\??ff#1\endcsname
     \ifx#2\empty
       \@EA\truefontname\csname\??ff#1\endcsname
     \else
       \@EA\redotruefontname\csname\??ff#1\endcsname*#2#3%
     \fi
   \else
     #1\ifx#2\empty\else*#2#3\fi
   \fi\fi}

\def\redotruefontname#1%
  {\@EA\dodotruefontname#1*\relax}

\def\dodotruefontname#1*#2\relax
  {\ifcsname\??ff\fontclass#1\endcsname
     \@EA\redotruefontname\csname\??ff\fontclass#1\endcsname
   \else\ifcsname\??ff#1\endcsname
     \@EA\redotruefontname\csname\??ff#1\endcsname
   \else
     #1%
   \fi\fi}

%D Default:

\def\defaultfontfile{file:lmmono10-regular}

%D Maybe:

% \def\updatefontparameters
%   {\edef\@@fontfeatures{\truefontdata\fontfile    \s!features}%
%    \edef\@@fontskewchar{\truefontdata\fontfile    \s!skewchar}}

% \def\setfontcharacteristics
%   {%\updatefontparameters % redundant, will go away, faster too
%    \the\everyfont
%    \synchronizepatternswithfont}

\protect \endinput

% \starttypescript[serif] [myzhfont]
%     \definefontsynonym [Serif]           [file:SimSun]
%     \definefontsynonym [SerifBold]       [file:SimSun]
%     \definefontsynonym [SerifItalic]     [file:SimSun]
%     \definefontsynonym [SerifBoldItalic] [file:SimSun]
% \stoptypescript
% \starttypescript[sans] [myzhfont]
%     \definefontsynonym [Sans]           [file:SimSun]
%     \definefontsynonym [SansBold]       [file:SimSun]
%     \definefontsynonym [SansItalic]     [file:SimSun]
%     \definefontsynonym [SansBoldItalic] [file:SimSun]
% \stoptypescript
% \starttypescript[mono] [myzhfont]
%     \definefontsynonym [Mono]           [file:SimSun]
%     \definefontsynonym [MonoBold]       [file:SimSun]
%     \definefontsynonym [MonoItalic]     [file:SimSun]
%     \definefontsynonym [MonoBoldItalic] [file:SimSun]
% \stoptypescript
% \definetypeface [myzhfont] [rm] [serif][myzhfont]   [default]
% \definetypeface [myzhfont] [ss] [sans] [myzhfont]   [default]
% \definetypeface [myzhfont] [tt] [mono] [myzhfont]   [default]

% \starttext
%     % on windows: make sure fonts.conf has no cache mentioned
%     %
%     % 64 sec xetex, 11 sec luatex (56 sec xetex when \nobigmath)
%     %
%       \setupbodyfont[myzhfont] \dorecurse{10000}{{hello {\switchtobodyfont[myzhfont] 你好}}\par}
%     %
%     % 67 sec xetex, 11.5 sec luatex
%     %
%     % \dorecurse{10000}{{hello {\switchtobodyfont[myzhfont] 你好}}\par}
%     %
%     % 5 sec xetex, 7 sec luatex
%     %
%     % \setupbodyfont[myzhfont]  \dorecurse{10000}{{hello {你好}}\par}
%     %
%     % 5 sec xetex, 7 sec luatex
%     %
%     % \setupbodyfont[myzhfont]  \dorecurse{10000}{{\bf hello {你好}}\par}
% \stoptext