| 1 |
%%^^A%% fontspec-doc-api.tex -- part of FONTSPEC <latex3.github.io/fontspec> |
| 2 |
|
| 3 |
\documentclass[a4paper]{l3doc} |
| 4 |
\usepackage{fontspec-doc-style} |
| 5 |
\showexamplesfalse |
| 6 |
\begin{document} |
| 7 |
|
| 8 |
\part{Customisation and programming interface} |
| 9 |
|
| 10 |
This chapter describes the current interfaces and hooks that use |
| 11 |
\pkg{fontspec} for various macro programming purposes. |
| 12 |
|
| 13 |
|
| 14 |
\section{Defining new features} \label{sec:newfeatures} |
| 15 |
This package cannot hope to contain every possible font |
| 16 |
feature. Three commands are provided for selecting font features |
| 17 |
that are not provided for out of the box. If you are using |
| 18 |
them a lot, chances are I've left something out, so please let me |
| 19 |
know. |
| 20 |
|
| 21 |
\DescribeMacro{\newAATfeature} |
| 22 |
New \AAT\ features may be created with this command:\par |
| 23 |
{\centering\cmd\newAATfeature\marg{feature}\marg{option}\marg{feature code}\marg{selector code}\par}\noindent |
| 24 |
Use the \XeTeX\ file \path{AAT-info.tex} to obtain the code numbers. |
| 25 |
\begin{Verbatim} |
| 26 |
\newAATfeature{Alternate}{HoeflerSwash}{17}{1} |
| 27 |
\fontspec{Hoefler Text Italic}[Alternate=HoeflerSwash] |
| 28 |
This is XeTeX by Jonathan Kew. |
| 29 |
\end{Verbatim} |
| 30 |
|
| 31 |
|
| 32 |
\DescribeMacro{\newopentypefeature} |
| 33 |
New OpenType features may be created with this command:\par |
| 34 |
{\centering\cmd\newopentypefeature\marg{feature}\marg{option}\marg{feature tag}\par} |
| 35 |
The synonym \cs{newICUfeature} is deprecated. |
| 36 |
|
| 37 |
Here's what it would look like in practise: |
| 38 |
\begin{Verbatim} |
| 39 |
\newopentypefeature{Style}{NoLocalForms}{-locl} |
| 40 |
\end{Verbatim} |
| 41 |
|
| 42 |
\DescribeMacro{\newfontfeature} |
| 43 |
In case the above commands do not accommodate the desired font feature |
| 44 |
(perhaps a new \XeTeX\ feature that \pkg{fontspec} hasn't been updated |
| 45 |
to support), a command is provided to pass arbitrary input into the |
| 46 |
font selection string:\par |
| 47 |
{\centering\cmd{\newfontfeature}\marg{name}\marg{input string}\par} |
| 48 |
|
| 49 |
For example, Zapfino used to |
| 50 |
contain an AAT feature `Avoid d-collisions'. To access it |
| 51 |
with this package, you could do some like the following: |
| 52 |
|
| 53 |
\begin{Verbatim} |
| 54 |
\newfontfeature{AvoidD} {Special= Avoid d-collisions} |
| 55 |
\newfontfeature{NoAvoidD}{Special=!Avoid d-collisions} |
| 56 |
\fontspec{Zapfino}[AvoidD,Variant=1] |
| 57 |
sockdolager rubdown \\ |
| 58 |
\fontspec{Zapfino}[NoAvoidD,Variant=1] |
| 59 |
sockdolager rubdown |
| 60 |
\end{Verbatim} |
| 61 |
|
| 62 |
The advantage to using the \cmd\newAATfeature\ and \cmd\newopentypefeature\ |
| 63 |
commands instead of \cs{newfontfeature} is that they check if the selected font actually contains the desired font |
| 64 |
feature at load time. By contrast, \cmd\newfontfeature\ will not give a warning |
| 65 |
for improper input. |
| 66 |
|
| 67 |
|
| 68 |
\section{Defining new scripts and languages} |
| 69 |
\label{sec:newscriptlang} |
| 70 |
|
| 71 |
\DescribeMacro{\newfontscript} |
| 72 |
\DescribeMacro{\newfontlanguage} |
| 73 |
While the scripts and languages listed in \ref{tab:ot-scpt} and \ref{tab:ot-lang} |
| 74 |
are intended to be comprehensive, there may be some missing; alternatively, |
| 75 |
you might wish to use different names to access scripts/languages that are |
| 76 |
already listed. |
| 77 |
Adding scripts and languages can be performed with the \cmd\newfontscript\ |
| 78 |
and \cmd\newfontlanguage\ commands. For example, |
| 79 |
\begin{Verbatim} |
| 80 |
\newfontscript{Arabic}{arab} |
| 81 |
\newfontlanguage{Zulu}{ZUL} |
| 82 |
\end{Verbatim} |
| 83 |
The first argument is the \pkg{fontspec} name, the second the OpenType |
| 84 |
tag. The advantage to using these commands rather than \cmd\newfontfeature\ |
| 85 |
(see \vref{sec:newfeatures}) is the error-checking that is performed when |
| 86 |
the script or language is requested. |
| 87 |
|
| 88 |
Both commands accept a comma-separated list of OpenType tags in order of preference. |
| 89 |
This permits, for example, supporting both new and old versions of a language tag with |
| 90 |
a common user interface: |
| 91 |
\begin{Verbatim} |
| 92 |
\newfontlanguage{Turkish}{TRK,TUR} |
| 93 |
\end{Verbatim} |
| 94 |
Here, a font that is requested with \verb|Script=Turkish| will first be checked for the |
| 95 |
OpenType language tag \verb|TRK|, which will be selected if available. If not available, |
| 96 |
the \verb|TUR| tag will be queried and used if possible as a fallback. |
| 97 |
|
| 98 |
|
| 99 |
\section{Going behind \pkg{fontspec}'s back} |
| 100 |
|
| 101 |
Expert users may wish not to use \pkg{fontspec}'s feature handling at all, |
| 102 |
while still taking advantage of its \LaTeX\ font selection conveniences. The |
| 103 |
\feat{RawFeature} font feature allows font feature selection using a literal feature |
| 104 |
selection string if you happen to have the OpenType feature tag memorised. |
| 105 |
More importantly, this can be used to enable features for which \pkg{fontspec} |
| 106 |
does not yet have a user interface to. |
| 107 |
|
| 108 |
\begin{Xexample}{raw}{Using raw font features directly.} |
| 109 |
\fontspec{texgyrepagella-regular.otf}[RawFeature=+smcp] |
| 110 |
Pagella small caps |
| 111 |
\end{Xexample} |
| 112 |
|
| 113 |
Multiple features can either be included in a single declaration:\par |
| 114 |
{\centering|[RawFeature=+smcp;+onum]|\par} |
| 115 |
\noindent or with multiple declarations:\par |
| 116 |
{\centering|[RawFeature=+smcp, RawFeature=+onum]|\par} |
| 117 |
|
| 118 |
Note that there is no error-checking when using |RawFeature|. Where a \pkg{fontspec} |
| 119 |
interface exists to a feature it is generally better to use it. If the font lacks the feature |
| 120 |
or if it would clash with another feature, \pkg{fontspec} will attemmpt to warn and/or resolve the issues. |
| 121 |
|
| 122 |
|
| 123 |
\section{Renaming existing features \& options} |
| 124 |
\label{sec:aliasfontfeature} |
| 125 |
|
| 126 |
\DescribeMacro{\aliasfontfeature} |
| 127 |
If you don't like the name of a particular font feature, |
| 128 |
it may be aliased to another with the |
| 129 |
\cs{aliasfontfeature}\marg{existing name}\marg{new name} command, |
| 130 |
such as shown in \exref{alias}. |
| 131 |
|
| 132 |
\begin{Xexample}{alias}{Renaming font features.} |
| 133 |
\aliasfontfeature{ItalicFont}{IF} |
| 134 |
\aliasfontfeature{ItalicFeatures}{IFF} |
| 135 |
\setmainfont{ EBGaramond-Regular.otf }[ |
| 136 |
IF = EBGaramond-Italic.otf , |
| 137 |
IFF = {Style=Swash} , |
| 138 |
] |
| 139 |
Roman Letters \itshape And Swash |
| 140 |
\end{Xexample} |
| 141 |
|
| 142 |
Spaces in feature (and option names, see below) \emph{are} allowed. (You may have |
| 143 |
noticed this already in the lists of OpenType scripts and languages). |
| 144 |
|
| 145 |
\DescribeMacro{\aliasfontfeatureoption} |
| 146 |
If you wish to change the name of a font feature option, |
| 147 |
it can be aliased to another with the command |
| 148 |
\cs{aliasfontfeatureoption}\marg{font feature}\marg{existing name}\marg{new name}, such as shown in \exref{aliasopt}. |
| 149 |
|
| 150 |
\begin{Lexample}{aliasopt}{Renaming font feature options.} |
| 151 |
\aliasfontfeature{VerticalPosition}{Vert Pos} |
| 152 |
\aliasfontfeatureoption{VerticalPosition}{ScientificInferior}{Sci Inf} |
| 153 |
\fontspec{LinLibertine_R.otf}[Vert Pos=Sci Inf] |
| 154 |
Scientific Inferior: 12345 |
| 155 |
\end{Lexample} |
| 156 |
|
| 157 |
This example demonstrates an important point: when aliasing the feature |
| 158 |
options, the \emph{original} feature name must be used when declaring |
| 159 |
to which feature the option belongs. |
| 160 |
|
| 161 |
Only feature options that exist as sets of fixed strings may be altered in |
| 162 |
this way. That is, \opt{Proportional} can be aliased to \opt{Prop} in the |
| 163 |
\feat{Letters} feature, but \opt{550099BB} cannot be substituted for \opt{Purple} |
| 164 |
in a \feat{Color} specification. For this type of thing, the \cmd\newfontfeature\ |
| 165 |
command should be used to declare a new, \eg, \feat{PurpleColor} feature: |
| 166 |
\begin{Verbatim} |
| 167 |
\newfontfeature{PurpleColor}{color=550099BB} |
| 168 |
\end{Verbatim} |
| 169 |
Except that this example was written before support for named colours was |
| 170 |
implemented. But you get the idea. |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
\section{Programming interface} |
| 176 |
\label{sec:api} |
| 177 |
|
| 178 |
|
| 179 |
\subsection{Variables} |
| 180 |
|
| 181 |
\DescribeMacro{\l_fontspec_family_tl} |
| 182 |
\DescribeMacro{\l_fontspec_font} |
| 183 |
In some cases, it is useful to know what the \LaTeX\ font family |
| 184 |
of a specific \pkg{fontspec} font is. After a \cmd\fontspec-like |
| 185 |
command, this is stored inside the \cmd{\l_fontspec_family_tl}\ macro. |
| 186 |
Otherwise, \LaTeX's own \cmd{\f@family} macro can be useful here, |
| 187 |
too. |
| 188 |
The raw \TeX\ font that is defined from the `base' font in the family is stored in \cmd{\l_fontspec_font}. |
| 189 |
|
| 190 |
\DescribeMacro{\g_fontspec_encoding_tl} |
| 191 |
Package authors who need to load fonts with legacy \LaTeX\ \NFSS\ commands may also need to know what the default font encoding is. |
| 192 |
Since this has changed from \texttt{EU1}/\texttt{EU2} to \texttt{TU}, it is best to use the variable \cs{g_fontspec_encoding_tl} instead. |
| 193 |
|
| 194 |
\subsection{Functions for loading new fonts and families} |
| 195 |
|
| 196 |
\begin{function}{\fontspec_gset_family:Nnn,\fontspec_set_family:Nnn} |
| 197 |
\darg{\LaTeX\ family} |
| 198 |
\darg{fontspec features} |
| 199 |
\darg{font name} |
| 200 |
Defines a new \NFSS\ family from given \meta{features} and \meta{font}, |
| 201 |
and stores the family name in the variable \meta{family}. |
| 202 |
This font family can then be selected with standard \LaTeX\ commands |
| 203 |
\cs{fontfamily}\marg{family}\cs{selectfont}. |
| 204 |
See the standard \pkg{fontspec} user commands for applications of this |
| 205 |
function. |
| 206 |
\end{function} |
| 207 |
|
| 208 |
\begin{function}{\fontspec_gset_fontface:NNnn,\fontspec_set_fontface:NNnn} |
| 209 |
\darg{primitive font} |
| 210 |
\darg{\LaTeX\ family} |
| 211 |
\darg{fontspec features} |
| 212 |
\darg{font name} |
| 213 |
Variant of the above in which the primitive \TeX\ font command is stored in |
| 214 |
the variable \meta{primitive font}. |
| 215 |
If a family is loaded (with bold and italic shapes) the primitive font |
| 216 |
command will only select the regular face. |
| 217 |
This feature is designed for \LaTeX\ programmers who need to |
| 218 |
perform subsequent font-related tests on the \meta{primitive font}. |
| 219 |
\end{function} |
| 220 |
|
| 221 |
|
| 222 |
\subsection{Conditionals} |
| 223 |
|
| 224 |
The following functions in \pkg{expl3} syntax may be used |
| 225 |
for writing code that interfaces with \pkg{fontspec}-loaded fonts. |
| 226 |
The following conditionals are all provided in |TF|, |T|, and |F| forms. |
| 227 |
|
| 228 |
\subsubsection{Querying font families} |
| 229 |
|
| 230 |
\begin{function}{\fontspec_font_if_exist:nTF} |
| 231 |
\begin{syntax} |
| 232 |
|\fontspec_font_if_exist:nTF| \Arg{font name/file} \Arg{true code} \Arg{false code} |
| 233 |
\end{syntax} |
| 234 |
Test whether the `font name' (|#1|) exists or is loadable. |
| 235 |
\meta{true code} or \meta{false code} is executed accordingly. |
| 236 |
The syntax of |#1| is a restricted/simplified version of \pkg{fontspec}'s usual font loading syntax; fonts to be loaded by filename are detected by the presence of an appropriate extension (|.otf|, etc.), and paths should be included inline. |
| 237 |
E.g.: |
| 238 |
\begin{Verbatim} |
| 239 |
\fontspec_font_if_exist:nTF {cmr10}{T}{F} |
| 240 |
\fontspec_font_if_exist:nTF {Times~ New~ Roman}{T}{F} |
| 241 |
\fontspec_font_if_exist:nTF {texgyrepagella-regular.otf}{T}{F} |
| 242 |
\fontspec_font_if_exist:nTF {/Users/will/Library/Fonts/CODE2000.TTF}{T}{F} |
| 243 |
\end{Verbatim} |
| 244 |
The synonym \cs{IfFontExistsTF} is provided for `document authors'. |
| 245 |
\end{function} |
| 246 |
|
| 247 |
|
| 248 |
\begin{function}{\fontspec_if_fontspec_font:TF} |
| 249 |
Test whether the currently selected font has been loaded by fontspec. |
| 250 |
\end{function} |
| 251 |
|
| 252 |
|
| 253 |
\begin{function}{\fontspec_if_opentype:TF} |
| 254 |
Test whether the currently selected font is an OpenType font. |
| 255 |
Always true for \LuaTeX{} fonts. |
| 256 |
\end{function} |
| 257 |
|
| 258 |
|
| 259 |
\begin{function}{\fontspec_if_small_caps:TF} |
| 260 |
Test whether the currently selected font has a `small caps' face to be selected |
| 261 |
with |\scshape| or similar. |
| 262 |
Note that testing whether the font has the |Letters=SmallCaps| font feature is |
| 263 |
sufficient but not necessary for this command to return true, since small caps |
| 264 |
can also be loaded from separate font files. |
| 265 |
The logic of this command is complicated by the fact that \pkg{fontspec} will |
| 266 |
merge shapes together (for italic small caps, etc.). |
| 267 |
\end{function} |
| 268 |
|
| 269 |
|
| 270 |
\subsubsection{Availability of features} |
| 271 |
|
| 272 |
\begin{function}{\fontspec_if_aat_feature:nnTF} |
| 273 |
Test whether the currently selected font contains the \AAT\ |
| 274 |
feature (|#1|,|#2|). |
| 275 |
\end{function} |
| 276 |
|
| 277 |
|
| 278 |
\begin{function}{\fontspec_if_feature:nTF} |
| 279 |
Test whether the currently selected font contains the raw OpenType |
| 280 |
feature |#1|. E.g.: |\fontspec_if_feature:nTF {pnum} {True} {False}|. |
| 281 |
Returns false if the font is not loaded by fontspec or is not an OpenType |
| 282 |
font. |
| 283 |
\end{function} |
| 284 |
|
| 285 |
|
| 286 |
\begin{function}{\fontspec_if_feature:nnnTF} |
| 287 |
Test whether the currently selected font with raw OpenType script tag |#1| and raw OpenType language tag |#2| contains the raw OpenType feature tag |#3|. E.g.: |\fontspec_if_feature:nnnTF {latn} {ROM} {pnum} {True} {False}|. |
| 288 |
Returns false if the font is not loaded by fontspec or is not an OpenType |
| 289 |
font. |
| 290 |
\end{function} |
| 291 |
|
| 292 |
|
| 293 |
\begin{function}{\fontspec_if_script:nTF} |
| 294 |
Test whether the currently selected font contains the raw OpenType |
| 295 |
script |#1|. E.g.: |\fontspec_if_script:nTF {latn} {True} {False}|. |
| 296 |
Returns false if the font is not loaded by fontspec or is not an OpenType |
| 297 |
font. |
| 298 |
\end{function} |
| 299 |
|
| 300 |
|
| 301 |
\begin{function}{\fontspec_if_language:nTF} |
| 302 |
Test whether the currently selected font contains the raw OpenType language |
| 303 |
tag |#1|. E.g.: |\fontspec_if_language:nTF {ROM} {True} {False}|. |
| 304 |
Returns false if the font is not loaded by fontspec or is not an OpenType |
| 305 |
font. |
| 306 |
\end{function} |
| 307 |
|
| 308 |
|
| 309 |
\begin{function}{\fontspec_if_language:nnTF} |
| 310 |
Test whether the currently selected font contains the raw OpenType language |
| 311 |
tag |#2| in script |#1|. E.g.: |\fontspec_if_language:nnTF {cyrl} {SRB} {True} {False}|. |
| 312 |
Returns false if the font is not loaded by fontspec or is not an OpenType |
| 313 |
font. |
| 314 |
\end{function} |
| 315 |
|
| 316 |
|
| 317 |
\subsubsection{Currently selected features} |
| 318 |
|
| 319 |
\begin{function}{\fontspec_if_current_feature:nTF} |
| 320 |
Test whether the currently loaded font is using the specified raw |
| 321 |
OpenType feature tag |#1|. |
| 322 |
The tag string |#1| should be prefixed with |+| to query an active feature, and with a |-| (hyphen) to query a disabled feature. |
| 323 |
\end{function} |
| 324 |
|
| 325 |
|
| 326 |
\begin{function}{\fontspec_if_current_script:nTF} |
| 327 |
Test whether the currently loaded font is using the specified raw |
| 328 |
OpenType script tag |#1|. |
| 329 |
\end{function} |
| 330 |
|
| 331 |
|
| 332 |
\begin{function}{\fontspec_if_current_language:nTF} |
| 333 |
Test whether the currently loaded font is using the specified raw |
| 334 |
OpenType language tag |#1|. |
| 335 |
\end{function} |
| 336 |
|
| 337 |
\end{document} |
| 338 |
|
| 339 |
|
| 340 |
% /© |
| 341 |
% ------------------------------------------------ |
| 342 |
% The FONTSPEC package <latex3.github.io/fontspec> |
| 343 |
% ------------------------------------------------ |
| 344 |
% Copyright 2022-2025 The LaTeX project, LPPL "maintainer" |
| 345 |
% Copyright 2004-2022 Will Robertson |
| 346 |
% Copyright 2009-2015 Khaled Hosny |
| 347 |
% Copyright 2013 Philipp Gesang |
| 348 |
% Copyright 2013-2016 Joseph Wright |
| 349 |
% ------------------------------------------------ |
| 350 |
% This package is free software and may be redistributed and/or modified under |
| 351 |
% the conditions of the LaTeX Project Public License, version 1.3c or higher |
| 352 |
% (your choice): <http://www.latex-project.org/lppl/>. |
| 353 |
% ------------------------------------------------ |
| 354 |
% ©/ |
| 355 |
|