Debian Codegraph @

texmf-dist/tex/optex/base/fonts-resize.opm

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \setfontsize {Font resizing macros <2022-11-08>} % preloaded in format
4
5 \_doc -----------------------------
6 \`\initunifonts` macro extends \LuaTeX's font capabilities,
7 in order to be able to load Unicode fonts. Unfortunately, this part of
8 \OpTeX/ depends on the `luaotfload` package, which adapts \ConTeXt's generic
9 font loader for plain \TeX/ and \LaTeX. `luaotfload` uses Lua functions
10 from \LaTeX's `luatexbase` namespace, we provide our own replacements.
11 \^`\initunifonts` sets itself to relax because we don't want to do
12 this work twice.
13 \`\ufont` is a shortcut of \^`\initunifonts`~`\font`.
14 \_cod -----------------------------
15
16 \_protected\_def \_initunifonts {%
17 \_directlua{%
18 require('luaotfload-main')
19 luaotfload.main()
20 optex.hook_into_luaotfload()
21 }%
22 \_glet \_fmodtt=\_unifmodtt % use \_ttunifont for \tt
23 \_glet \_initunifonts=\_relax % we need not to do this work twice
24 \_glet \initunifonts=\_relax
25 }
26 \_protected\_def \_ufont {\_initunifonts \_font}
27
28 \_public \initunifonts \ufont ;
29
30 \_doc -----------------------------
31 The \`\setfontsize` `{<size spec>}` saves the `<size spec>` to the \`\_sizespec` macro.
32 The \`\_optsize` value is calculated from the `<size spec>`.
33 If the <size spec> is in the format `scaled<factor>` then
34 `\_optsize` is set from \`\defaultoptsize`.
35 If the `<size spec>` is in the `mag<number>` format then the contents of
36 the `\_sizespec` macro is re-calculated to the `at<dimen>` format using
37 previous `\_optsize` value.
38 \par \goodbreak
39 \_cod -----------------------------
40
41 \_newdimen \_optsize \_optsize=10pt
42 \_newdimen \_defaultoptsize \_defaultoptsize=10pt
43 \_newdimen\_lastmagsize
44
45 \_def\_setfontsize #1{%
46 \_edef\_sizespec{#1}%
47 \_ea \_setoptsize \_sizespec\_relax
48 }
49 \_def\_setoptsize {\_isnextchar a{\_setoptsizeA}
50 {\_isnextchar m{\_setoptsizeC}{\_setoptsizeB}}}
51 \_def\_setoptsizeA at#1\_relax{\_optsize=#1\_relax\_lastmagsize=\_optsize} % at<dimen>
52 \_def\_setoptsizeB scaled#1\_relax{\_optsize=\_defaultoptsize\_relax} % scaled<scalenum>
53 \_def\_setoptsizeC mag#1\_relax{%
54 \_ifdim\_lastmagsize>\_zo \_optsize=\_lastmagsize \_else \_optsize=\_pdffontsize\_font \_fi
55 \_optsize=#1\_optsize
56 \_lastmagsize=\_optsize
57 \_edef\_sizespec{at\_the\_optsize}%
58 }
59 \_public \setfontsize \defaultoptsize ;
60
61 \_doc -----------------------------
62 The `\fontname` primitive returns the <font file name> optionally
63 followed by <size spec>. The \`\xfontname` macro expands to <font file name>
64 without <size spec>. We need to remove the part `<space>at<dimen>` from
65 `\fontname` output. The letters `at` have category 12 in the \`\_stringat`
66 macro.
67 \_cod -----------------------------
68
69 \_edef\_stringat{\_string a\_string t}
70 \_edef\_xfontname#1{\_unexpanded{\_ea\_xfontnameA\_fontname}#1 \_stringat\_relax}
71 \_expanded{\_def\_noexpand\_xfontnameA#1 \_stringat#2\_relax}{#1}
72
73 \_doc -----------------------------
74 \`\fontlet` `<font switch A> <font switch B> <size spec>` does
75 \begtt \catcode`\<=13
76 \font <font switch A> = {<font file name>} <size spec>
77 \endtt
78 Note, that the `\_xfontname` output is converted due to optical size data using `\_optfn`.
79 \_cod -----------------------------
80
81 \_protected\_def \_fontlet #1#2{\_ifx #2=\_ea\_fontlet \_ea#1\_else
82 \_ea\_font \_ea#1\_expanded{{\_optfn{\_xfontname#2}}}\_fi}
83 \_public \xfontname \fontlet ;
84
85 \_doc -----------------------------
86 \`\newcurrfontsize` `<size spec>` does
87 \^`\fontlet` `<saved switch>=\font <size spec>\_relax <saved switch>`.
88 It changes the current font at the given <size spec>.
89 \nl
90 \`\resizethefont` is implemented by \^`\newcurrfontsize` using data from
91 the \^`\_sizespec` macro.\nl
92 \`\sfont` has the same syntax like `\font` primitive, but declares a
93 macro which selects the font and sets its size properly dependent on the
94 current size.
95 \_cod -----------------------------
96
97 % \newcurrfontsize{at25pt}
98 \_def \_newcurrfontsize {\_ea\_newcurrfontsizeA \_csname \_ea\_csstring \_the\_font \_endcsname}
99 \_def \_newcurrfontsizeA #1#2{\_fontlet #1\_font #2\_relax \_fontloaded#1#1}
100 \_protected\_def \_resizethefont {\_newcurrfontsize\_sizespec}
101 \_protected\_def \_sfont #1{%
102 \_protected\_edef #1{\_csname _sfont:\_csstring#1\_endcsname \_resizethefont}%
103 \_initunifonts \_ea\_font \_csname _sfont:\_csstring#1\_endcsname
104 }
105 \_public \newcurrfontsize \resizethefont \sfont ;
106
107 \_doc -----------------------------
108 The \`\_regtfm` `<font id> <optical size data>`
109 registers optical sizes data directly by the font file names.
110 This can be used for `tfm` files or OpenType files without
111 various font features. See also \^`\_regoptsizes` in section~\ref[optsizes].
112 The `\_regtfm` command saves the <optical size data> concerned to the `<font id>`.
113 The `<optical size data>` is in the form as shown below in the code where
114 `\_regtfm` is used.
115 \nl
116 The \`\_optfn` `<fontname>` expands to the `<fontname>` or to the corrected
117 `<fontname>` read from the `<optical size data>` registered by `\_regtfm`.
118 It is used in the \^`\fontlet` macro.
119
120 The implementation detail: The `\_reg:<font id>` is defined as the
121 `<optical size data>` and all control sequences `\_reg:<fontname>`
122 from this data line have the same meaning because of the
123 \`\_reversetfm` macro. The `\_optfn` expands this data line and apply
124 \`\_runoptfn`. This macro selects the right result from the data line
125 by testing with the current `\_optsize` value.
126 \_cod -----------------------------
127
128 \_def\_regtfm #1 0 #2 *{\_ea\_def \_csname _reg:#1\_endcsname{#2 16380 \_relax}%
129 \_def\_tmpa{#1}\_reversetfm #2 * %
130 }
131 \_def\_reversetfm #1 #2 {% we need this data for \_setmathfamily
132 \_ea\_let\_csname _reg:#1\_ea\_endcsname
133 \_csname _reg:\_tmpa\_endcsname
134 \_if*#2\_else \_ea\_reversetfm \_fi
135 }
136 \_def\_optfn #1{%
137 \_ifcsname _reg:#1\_endcsname
138 \_ea\_ea\_ea \_runoptfn
139 \_csname _reg:#1\_ea\_endcsname
140 \_else
141 #1%
142 \_fi
143 }
144 \_def\_runoptfn #1 #2 {%
145 \_ifdim\_optsize<#2pt #1\_ea\_ignoretfm\_else \_ea\_runoptfn
146 \_fi
147 }
148 \_def\_ignoretfm #1\_relax{}
149
150 \_doc -----------------------------
151 Optical sizes data for preloaded 8bit Latin Modern fonts:
152 \_cod -----------------------------
153
154 \_regtfm lmr 0 ec-lmr5 5.5 ec-lmr6 6.5 ec-lmr7 7.5 ec-lmr8 8.5 ec-lmr9 9.5
155 ec-lmr10 11.1 ec-lmr12 15 ec-lmr17 *
156 \_regtfm lmbx 0 ec-lmbx5 5.5 ec-lmbx6 6.5 ec-lmbx7 7.5 ec-lmbx8 8.5 ec-lmbx9 9.5
157 ec-lmbx10 11.1 ec-lmbx12 *
158 \_regtfm lmri 0 ec-lmri7 7.5 ec-lmri8 8.5 ec-lmri9 9.5 ec-lmri10 11.1 ec-lmri12 *
159 \_regtfm lmtt 0 ec-lmtt8 8.5 ec-lmtt9 9.5 ec-lmtt10 11.1 ec-lmtt12 *
160
161 \_endcode %---------------------------------------------------
162
163
164 \sec[fontprimitive] Using `\font` primitive directly
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166
167 You can declare a new {\em font switch} by `\font` primitive:
168 \begtt \catcode`\<=13
169 \font \<font switch> = <font file name> <size spec>
170 % for example:
171 \font \tipa = tipa10 at12pt % the font tipa10 at 12pt is loaded
172 % usage:
173 {\tipa TEXT} % the TEXT is printed in the loaded font.
174 \endtt
175 The <size spec> can be empty or `at<dimen>` or `scaled<scale factor>`. The
176 <font file name> must be terminated by space or surrounded in the braces.
177
178 \OpTeX/ starts with `\font` primitive which is able to read only `tfm` files.
179 i.e.\ the `<font file name>.tfm` (and additional data for glyphs)
180 must be correctly installed in your system. If you want to load
181 OpenType `otf` or `ttf` font files, use the declarator \^`\initunifonts`
182 before first `\font` primitive.
183 This command adds additional features to the `\font` primitive which
184 gives the extended syntax:
185 \begtt \catcode`\<=13
186 \font \<font switch> = {[<font file name>]:<font features>} <size spec>
187 % or
188 \font \<font switch> = {<font name>:<font features>} <size spec>
189 \endtt
190 where <font file name> is name of the OpenType font file with the extension
191 `.otf` or `.ttf` or without it. The braces in the syntax
192 are optional, use them when the <font file name> or <font name> includes spaces.
193 The original syntax for `tfm` files is also available. Example:
194 \begtt
195 \initunifonts
196 \font\crimson=[Crimson-Roman] at11pt % the font Crimson-Regular.otf is loaded
197 \font\crimsonff=[Crimson-Roman]:+smcp;+onum at11pt % The same font is re-loaded
198 % with font features
199 {\crimson Text 12345} % normal text in Crimson-Regular
200 {\crimsonff Text 12345} % Crimson-Regular with small capitals and old digits
201 \endtt
202 \^`\initunifonts` loads the implementation of the `\font`
203 primitive from `luaotfload` package. More information is available
204 in the `luaotfload-latex.pdf` file.
205
206 You can use \^`\ufont` macro which runs \^`\initunifonts` followed by `\font`
207 primitive. And \^`\fontfam` does (among other things) \^`\initunifonts`
208 too. You need not to specify \^`\initunifonts` if \^`\fontfam` or \^`\ufont` is used.
209
210 When \^`\initunifonts` is declared then the `\font` primitive is ready to read
211 Type1 fonts too. If you have `file.afm` and `file.pfb` then you can
212 declare `\font\f=file.afm` and use `\f`. It means that you needn't to create
213 `tfm` files nor `vf` files, you can use Type1 fonts directly.
214 They behave as Unicode fonts if the `afm` metrics are implemented correctly
215 (with correct names of all included glyphs). But we must to say that Type1
216 font format is old technology, the loading of Type1 fonts is not optimized.
217 Use OpenType fonts (`otf` of `ttf`) if it is possible.
218
219 Let's sum it up. Suppose that \^`\initunifonts` was used.
220 The `\font` primitive is able to load OpenType
221 fonts (`otf` or `ttf`), Type1 fonts (`afm` and `pfb`) or classical `tfm` fonts.
222 We strongly recommend to prefer OpenType format over Type1 format
223 over `tfm` format. The last one doesn't support Unicode. If there is nothing
224 else left and you must to use `tfm`, then you must to implement re-encoding from
225 Unicode to the `tfm` encoding at macro level, see the
226 \ulink[http://petr.olsak.net/optex/optex-tricks.html\#nonunicode]{\OpTeX/ trick 0018}
227 for example.
228
229
230 \secc[setfontsize] The `\setfontsize` macro
231
232 It seems that you must decide about final size of the font before it is
233 loaded by the `\font` primitive. It is not exactly true; \OpTeX/ offers
234 powerful possibility to resize the font already loaded on demand.
235
236 The \^`\setfontsize` `{<size spec>}`
237 saves the information about `<size spec>`. This information is taken into
238 account when a variant selector (for example `\rm`, `\bf`, `\it`, `\bi`)
239 or `\resizethefont` is used. The `<size spec>` can be:
240 \begitems
241 * `at<dimen>`, for example `\setfontsize{at12pt}`. It gives the desired font size directly.
242 * `scaled<scale factor>`, for example `\setfontsize{scaled1200}`. The font is
243 scaled in respect to its native size (which is typically 10\,pt). It
244 behaves like `\font\... scaled<number>`.
245 * `mag<decimal number>`, for example `\setfontsize{mag1.2}`. The font is
246 scaled in respect to the current size of the fonts given by the previous
247 \^`\setfontsize` command.
248 \enditems
249 The initial value in \OpTeX/ is given by `\setfontsize{at10pt}`.
250
251 The \^`\resizethefont` resizes the currently selected font to the size given by previous
252 \^`\setfontsize`. For example
253
254 \begtt
255 The 10 pt text is here,
256 \setfontsize{at12pt} the 10 pt text is here unchanged...
257 \resizethefont and the 12 pt text is here.
258 \endtt
259 %
260 The \^`\setfontsize` command acts like {\em font modifier}. It means that it
261 saves information about fonts but does not change the font actually until
262 variant selector or \^`\resizethefont` is used.
263
264 The following example demonstrates the `mag` format of \^`\setfontsize`
265 parameter. It is only a curious example probably not used in practical
266 typography.
267
268 \begtt
269 \def\smaller{\setfontsize{mag.9}\resizethefont}
270 Text \smaller text \smaller text \smaller text.
271 \endtt
272
273 The \^`\resizethefont` works with arbitrary current font, for example with the
274 font loaded directly by `\font` primitive. For example:
275
276 \begtt
277 \ufont\tencrimson=[Crimson-Roman]:+onum % font Crimson-Regular at 10 pt is loaded
278 \def\crimson{\tencrimson\resizethefont} % \crimson uses the font size on demand
279
280 \crimson The 10 pt text is here.
281 \setfontsize{at12pt}
282 \crimson The 12 pt text is here.
283 \endtt
284 This is not only an academical example. The `\csrimson` command defined here
285 behaves like variant selector in the Font Selection System (section~\ref[fontsystem]).
286 It takes only information about size from the font context, but it is
287 sufficient. You can use it in titles, footnotes, etc. The font size
288 depending on surrounding size is automatically selected.
289 There is a shortcut \^`\sfont` with the same syntax like `\font` primitive,
290 it declares a macro which selects the font and does resizing depending on
291 the current size. So, the example above can be realized by
292 `\sfont\crimson=[Crimson-Roman]:+onum`.
293
294 \secc The `\font`-like commands summary
295
296 \begitems
297 * `\font` is \TeX/ primitive. When \OpTeX/ starts, then it accepts only
298 classical \TeX/ syntax and doesn't allow to load Unicode fonts.
299 Once \^`\initunifonts` (or \^`\fontfam`) is used, the `\font` primitive is
300 re-initialized: now it accepts extended syntax and it is able to load Unicode
301 OpenType fonts.
302 * \^`\ufont` is a shortcut of `\initunifonts \font`. I.e.\ it behaves like
303 `\font` and accepts extended syntax immediately.
304 * \^`\sfont` has syntax like extended `\font`. It declares a macro which
305 selects the given font and resizes it to the current size (given by
306 \^`\setfontsize`). In various part of document (text, footnotes, titles), the
307 size of this font is selected by the declared macro properly.
308 \enditems
309
310
311 \secc[fontlet] The \code{\\fontlet} declarator
312
313 We have another command for scaling: \^`\fontlet` which can resize
314 arbitrary font given by its font switch.
315 \begtt \catcode`\<=13
316 \fontlet \<new font switch> = \<given font switch> <size spec>
317 example:
318 \fontlet \bigfont = \_tenbf at15pt
319 \endtt
320 The `\<given font switch>` must be declared previously by `\font` or
321 \^`\fontlet` or \~`\fontdef`. The `\<new font switch>` is declared as the
322 same font at given <size spec>. The equal sign in the syntax is optional.
323 You can declare `\<new font switch>` as the scaled current font by
324 \begtt \catcode`\<=13
325 \fontlet \<new font switch> = \font <size spec>
326 \endtt
327
328
329 \secc Optical sizes
330
331 There are font families with more font files where almost the same font is
332 implemented in various design sizes: `cmr5`, `cmr6`, `cmr7`, `cmr8`, `cmr9`,
333 `cmr10`, `cmr12`, `cmr17` for example. This feature is called \"optical
334 sizes". Each design size is implemented in its individual font file and \OpTeX/ is
335 able to choose right file if various optical sizes and corresponding file
336 names are declared for the font by \^`\_regtfm` or \^`\_regoptsizes` command.
337 The command \^`\setfontsize` sets the internal requirements for
338 optical size if the parameter is in the format `at<dimen>` or `mag<factor>`.
339 Then the command \^`\resizethefont` or \^`\fontlet` or variant selectors try to choose
340 the font suitable for the required optical size. For example
341 \begtt
342 \fontfam[lm]
343 The text is printed in font [lmroman10-regular] at 10 pt.
344 \setfontsize{at13pt}\rm
345 Now, the text is printed in [lmroman12-regular] at 13 pt.
346 \endtt
347 See also section~\ref[optsizes].
348
349
350 \secc Font rendering
351
352 If \^`\initunifonts` isn't declared then \OpTeX/ uses classical font
353 renderer (like in `pdftex`). The extended font renderer implemented in the
354 Luaotfload package is started after \^`\initunifonts`.
355
356 The \OpTeX/ format uses `luahbtex` engine by default (from 2025).
357 It means that the harfbuzz library is
358 ready to use for font rendering as an alternative to built-in font renderer
359 from Luaotfload. The harfbuzz library gives more features for rendering
360 Indic and Arabic scripts. But it is not used as default, you need to specify
361 `mode=harf` in the fontfeatures field when `\font` is used. Moreover, when
362 `mode=harf` is used, then you must specify `script` too. For example
363
364 \begtt
365 \font\devafont=[NotoSansDevanagari-Regular]:mode=harf;script=dev2
366 \endtt
367 %
368 If the `luahbtex` engine is not used then `mode=harf` is ignored. See
369 Luaotfload documentation for more information.
370
371 \_endinput
372
373 2022-11-08 \sfont introduced, \protected: \initunifont, \ufont, \fontlet
374 2022-11-07 \fontlet \a\a: bug fixed
375 2022-10-14 \ufont introduced
376 2022-02-22 Font Selection System reimplemented, \fontsel introduced
377 2021-05-02 better concept of doc, moving parts to fonts-select
378 2021-04-17 \_fontloaded, \_newfontloaded introduced
379 2020-04-17 \resizethefont introduced
380 2020-03-17 released