| 1 |
%%^^A%% fontspec-doc-enc.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{Commands for accents and symbols (`encodings')} |
| 9 |
\label{part:enc} |
| 10 |
|
| 11 |
\textbf{The functionality described in this section is experimental.} |
| 12 |
|
| 13 |
In the pre-Unicode era, significant work was required by \LaTeX\ to ensure that |
| 14 |
input characters in the source could be interpreted correctly depending on file encoding, |
| 15 |
and that glyphs in the output were selected correctly depending on the font encoding. |
| 16 |
With Unicode, we have the luxury of a single file and font encoding that is used for both input |
| 17 |
and output. |
| 18 |
|
| 19 |
While this may provide some illusion that we could get away simply with typing |
| 20 |
Unicode text and receive correct output, this is not always the case. |
| 21 |
For a start, hyphenation in particular is language-specific, so tags should be used |
| 22 |
when switch between languages in a document. |
| 23 |
The \pkg{babel} and \pkg{polyglossia} packages both provide features for this. |
| 24 |
|
| 25 |
Multilingual documents will often use different fonts for different languages, |
| 26 |
not just for style, but for the more pragmatic reason that fonts do not all contain |
| 27 |
the same glyphs. (In fact, only test fonts such as Code2000 provide |
| 28 |
anywhere near the full Unicode coverage.) |
| 29 |
Indeed, certain fonts may be perfect for a certain application but miss a handful |
| 30 |
of necessary diacritics or accented letters. |
| 31 |
In these cases, \pkg{fontspec} can leverage the font encoding technology built |
| 32 |
into \LaTeX2\ to provide on a per-font basis either provide fallback options or |
| 33 |
error messages when a desired accent or symbol is not available. |
| 34 |
However, at present |
| 35 |
these features can only be provided for input using \LaTeX\ commands rather |
| 36 |
than Unicode input; for example, typing |\`e| instead of |è| or |\textcopyright| |
| 37 |
instead of |©| in the source file. |
| 38 |
|
| 39 |
The most widely-used encoding in \LaTeXe\ was |T1| with companion `|TS1|' symbols |
| 40 |
provided by the \pkg{textcomp} package. |
| 41 |
These encodings provided glyphs to typeset text in a variety of western European languages. |
| 42 |
As with most legacy \LaTeXe\ input methods, accents and symbols were input using |
| 43 |
encoding-dependent commands such as |\`e| as described above. |
| 44 |
As of 2017, in \LaTeXe\ on \XeTeX\ and \LuaTeX, the default encoding is |TU|, |
| 45 |
which uses Unicode for input and output. |
| 46 |
The |TU| encoding provides appropriate encoding-dependent definitions for input commands |
| 47 |
to match the coverage of the |T1+TS1| encodings. |
| 48 |
Wider coverage is not provided by default since (a)~each font will provide different glyph coverage, and |
| 49 |
(b)~it is expected that most users will be writing with direct Unicode input. |
| 50 |
|
| 51 |
For those users who do need finer-grained control, \pkg{fontspec} provides an |
| 52 |
interface for a more extensible system. |
| 53 |
|
| 54 |
|
| 55 |
\section{A new Unicode-based encoding from scratch} |
| 56 |
|
| 57 |
Let's say you need to provide support for a document originally written with fonts |
| 58 |
in the |OT2| encoding, which contains encoding-dependent commands for Cyrillic letters. |
| 59 |
An example from the |OT2| encoding definition file (|ot2enc.def|) reads: |
| 60 |
\begin{Verbatim}[numbers=left,firstnumber=57] |
| 61 |
\DeclareTextSymbol{\CYRIE}{OT2}{5} |
| 62 |
\DeclareTextSymbol{\CYRDJE}{OT2}{6} |
| 63 |
\DeclareTextSymbol{\CYRTSHE}{OT2}{7} |
| 64 |
\DeclareTextSymbol{\cyrnje}{OT2}{8} |
| 65 |
\DeclareTextSymbol{\cyrlje}{OT2}{9} |
| 66 |
\DeclareTextSymbol{\cyrdzhe}{OT2}{10} |
| 67 |
\end{Verbatim} |
| 68 |
|
| 69 |
To recreate this encoding in a form suitable for \pkg{fontspec}, create a new file |
| 70 |
named, say, |fontrange-cyr.def| and populate it with |
| 71 |
\begin{Verbatim} |
| 72 |
... |
| 73 |
\DeclareTextSymbol{\CYRIE} {\LastDeclaredEncoding}{"0404} |
| 74 |
\DeclareTextSymbol{\CYRDJE} {\LastDeclaredEncoding}{"0402} |
| 75 |
\DeclareTextSymbol{\CYRTSHE}{\LastDeclaredEncoding}{"040B} |
| 76 |
\DeclareTextSymbol{\cyrnje} {\LastDeclaredEncoding}{"045A} |
| 77 |
\DeclareTextSymbol{\cyrlje} {\LastDeclaredEncoding}{"0459} |
| 78 |
\DeclareTextSymbol{\cyrdzhe}{\LastDeclaredEncoding}{"045F} |
| 79 |
... |
| 80 |
\end{Verbatim} |
| 81 |
The numbers |"0404|, |"0402|, \dots, are the Unicode slots (in hexadecimal) |
| 82 |
of each glyph respectively. |
| 83 |
The \pkg{fontspec} package provides a number of shorthands to simplify this style of input; in this case, |
| 84 |
you could also write |
| 85 |
\begin{Verbatim} |
| 86 |
\EncodingSymbol{\CYRIE}{"0404} |
| 87 |
... |
| 88 |
\end{Verbatim} |
| 89 |
|
| 90 |
To use this encoding in a \pkg{fontspec} font, you would first add this to your preamble: |
| 91 |
\begin{Verbatim} |
| 92 |
\DeclareUnicodeEncoding{unicyr}{ |
| 93 |
\input{fontrange-cyr.def} |
| 94 |
} |
| 95 |
\end{Verbatim} |
| 96 |
Then follow it up with a font loading call such as |
| 97 |
\begin{Verbatim} |
| 98 |
\setmainfont{...}[NFSSEncoding=unicyr] |
| 99 |
\end{Verbatim} |
| 100 |
The first argument |unicyr| is the name of the `encoding' to use in the |
| 101 |
font family. (There's nothing special about the name chosen but it must be unique.) |
| 102 |
The second argument to |\DeclareUnicodeEncoding| also allows adjustments to be made |
| 103 |
for per-font changes. |
| 104 |
We'll cover this use case in the next section. |
| 105 |
|
| 106 |
|
| 107 |
\section{Adjusting a pre-existing encoding} |
| 108 |
|
| 109 |
There are three reasons to adjust a pre-existing encoding: |
| 110 |
to add, to remove, and to redefine some symbols, letters, and/or accents. |
| 111 |
|
| 112 |
When adding symbols, etc., simply write |
| 113 |
\begin{Verbatim} |
| 114 |
\DeclareUnicodeEncoding{unicyr}{ |
| 115 |
\input{tuenc.def} |
| 116 |
\input{fontrange-cyr.def} |
| 117 |
\EncodingSymbol{\textruble}{"20BD} |
| 118 |
} |
| 119 |
\end{Verbatim} |
| 120 |
Of course if you consistently add a number of symbols to an encoding it would be |
| 121 |
a good idea to create a new |fontrange-XX.def| file to suit your needs. |
| 122 |
|
| 123 |
When removing symbols, use the |\UndeclareSymbol|\marg{cmd} command. |
| 124 |
For example, if you a loading a font that you know is missing, say, the interrobang |
| 125 |
(not that unusual a situation), you might write: |
| 126 |
\begin{Verbatim} |
| 127 |
\DeclareUnicodeEncoding{nobang}{ |
| 128 |
\input{tuenc.def} |
| 129 |
\UndeclareSymbol\textinterrobang |
| 130 |
} |
| 131 |
\end{Verbatim} |
| 132 |
Provided that you use the command |\textinterrobang| to typeset this symbol, |
| 133 |
it will appear in fonts with the default encoding, while in any font loaded with |
| 134 |
the |nobang| encoding an attempt to access the symbol will either use the default |
| 135 |
fallback definition or return an error, depending on the symbol being undeclared. |
| 136 |
|
| 137 |
|
| 138 |
The third use case is to redefine a symbol or accent. The most common use case |
| 139 |
in this scenario is to adjust a specific accent command to either fine-tune its placement |
| 140 |
or to `fake' it entirely. |
| 141 |
For example, the underdot diacritic is used in typeset Sanskrit, |
| 142 |
but it is not necessarily included as an accent symbol is all fonts. |
| 143 |
By default the underdot is defined in |TU| as: |
| 144 |
\begin{Verbatim} |
| 145 |
\EncodingAccent{\d}{"0323} |
| 146 |
\end{Verbatim} |
| 147 |
For fonts with a missing (or poorly-spaced) |"0323| accent glyph, the `traditional' \TeX\ fake accent |
| 148 |
construction could be used instead: |
| 149 |
\begin{Verbatim} |
| 150 |
\DeclareUnicodeEncoding{fakeacc}{ |
| 151 |
\input{tuenc.def} |
| 152 |
\EncodingCommand{\d}[1]{% |
| 153 |
\hmode@bgroup |
| 154 |
\o@lign{\relax#1\crcr\hidewidth\ltx@sh@ft{-1ex}.\hidewidth}% |
| 155 |
\egroup |
| 156 |
} |
| 157 |
} |
| 158 |
\end{Verbatim} |
| 159 |
This would be set up in a document as such: |
| 160 |
\begin{Verbatim} |
| 161 |
\newfontfamily\sanskitfont{CharisSIL} |
| 162 |
\newfontfamily\titlefont{Posterama}[NFSSEncoding=fakeacc] |
| 163 |
\end{Verbatim} |
| 164 |
Then later in the document, no additional work is needed: |
| 165 |
\begin{Verbatim} |
| 166 |
...{\titlefont kalita\d m}... % <- uses fake accent |
| 167 |
...{\sanskitfont kalita\d m}... % <- uses real accent |
| 168 |
\end{Verbatim} |
| 169 |
To reiterate from above, typing this input with Unicode text (`|kalitaṃ|') |
| 170 |
will \emph{bypass} this encoding mechanism and you will receive only what is contained |
| 171 |
literally within the font. |
| 172 |
|
| 173 |
|
| 174 |
\section{Summary of commands} |
| 175 |
|
| 176 |
The \LaTeXe\ kernel provides the following font encoding commands suitable for Unicode encodings: |
| 177 |
\begin{quote}\obeylines |
| 178 |
\cs{DeclareTextCommand}\marg{command}\marg{encoding}\oarg{num}\oarg{default}\marg{code} |
| 179 |
\cs{DeclareUnicodeAccent}\marg{command}\marg{encoding}\marg{slot} |
| 180 |
\cs{DeclareTextSymbol}\marg{command}\marg{encoding}\marg{slot} |
| 181 |
\cs{DeclareTextComposite}\marg{command}\marg{encoding}\marg{letter}\marg{slot} |
| 182 |
\cs{DeclareTextCompositeCommand}\marg{command}\marg{encoding}\marg{letter}\marg{code} |
| 183 |
\cs{UndeclareTextCommand}\marg{command}\marg{encoding} |
| 184 |
\end{quote} |
| 185 |
See |fntguide.pdf| for full documentation of these. |
| 186 |
As shown above, the following shorthands are provided by \pkg{fontspec} to simplify |
| 187 |
the process of defining Unicode font range encodings: |
| 188 |
\begin{quote}\obeylines |
| 189 |
\cs{EncodingCommand}\marg{command}\oarg{num}\oarg{default}\marg{code} |
| 190 |
\cs{EncodingAccent}\marg{command}\marg{code} |
| 191 |
\cs{EncodingSymbol}\marg{command}\marg{code} |
| 192 |
\cs{EncodingComposite}\marg{command}\marg{letter}\marg{slot} |
| 193 |
\cs{EncodingCompositeCommand}\marg{command}\marg{letter}\marg{code} |
| 194 |
\cs{UndeclareSymbol}\marg{command} |
| 195 |
\cs{UndeclareAccent}\marg{command} |
| 196 |
\cs{UndeclareCommand}\marg{command} |
| 197 |
\cs{UndeclareComposite}\marg{command}\marg{letter} |
| 198 |
\end{quote} |
| 199 |
|
| 200 |
\end{document} |
| 201 |
|
| 202 |
|
| 203 |
% /© |
| 204 |
% ------------------------------------------------ |
| 205 |
% The FONTSPEC package <latex3.github.io/fontspec> |
| 206 |
% ------------------------------------------------ |
| 207 |
% Copyright 2022-2025 The LaTeX project, LPPL "maintainer" |
| 208 |
% Copyright 2004-2022 Will Robertson |
| 209 |
% Copyright 2009-2015 Khaled Hosny |
| 210 |
% Copyright 2013 Philipp Gesang |
| 211 |
% Copyright 2013-2016 Joseph Wright |
| 212 |
% ------------------------------------------------ |
| 213 |
% This package is free software and may be redistributed and/or modified under |
| 214 |
% the conditions of the LaTeX Project Public License, version 1.3c or higher |
| 215 |
% (your choice): <http://www.latex-project.org/lppl/>. |
| 216 |
% ------------------------------------------------ |
| 217 |
% ©/ |