summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/iffont/iffont.dtx
blob: b550caebea86c540dbfd557700ec37ffef679e82 (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
% \iffalse meta-comment
%
% Copyright (C) 2015 by Benjamin Weiss <weissbenjamin@me.com>
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Benjamin Weiss.
%
% This work consists of the files iffont.dtx and iffont.ins and the derived
% file iffont.sty.
%
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{iffont.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{iffont}
%<*package>
    [2015/11/11 1.0.0 Iffont package]
%</package>
%<*driver>
\documentclass{ltxdoc}
%\EnableCrossrefs
%\CodelineIndex
%\DisableCrossrefs
%\RecordChanges
%\OnlyDescription
\usepackage[parfill]{parskip}
\usepackage{setspace}
\onehalfspacing

\usepackage[no-math]{fontspec}
\setmainfont[BoldFont={Fira Sans}]{Fira Sans Light}
\setmonofont{Fira Mono}
\defaultfontfeatures{Ligatures=TeX}

\usepackage{metalogo}

\usepackage{enumitem}
\setlist[itemize]{noitemsep}
\setlist[enumerate]{noitemsep}

\usepackage[showerrors,table,hyperref]{xcolor}
\usepackage{iffont}

\definecolor{mDarkBrown}{HTML}{604c38}
\definecolor{mDarkTeal}{HTML}{23373b}
\definecolor{mLightBrown}{HTML}{EB811B}
\definecolor{mLightGreen}{HTML}{14B03D}
\definecolor{mBackground}{HTML}{FFFFFF}

\usepackage{listings}
\lstset{%
  gobble=2,
  language=[LaTeX]{TeX},
  basicstyle=\ttfamily\small,
  keywordstyle=\color{mLightBrown}\bfseries,
  commentstyle=\color{mLightGreen},
  stringstyle=\color{mLightGreen},
  backgroundcolor=\color{mBackground},
  numbers=none,
  numberstyle=\tiny\ttfamily,
  stepnumber=2,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  frame=none,
  framerule=1pt,
  tabsize=2,
  rulesep=5em,
  captionpos=b,
  breaklines=true,
  breakatwhitespace=false,
  framexleftmargin=0em,
  framexrightmargin=0em,
  xleftmargin=0em,
  xrightmargin=0em,
  aboveskip=1em,
  belowskip=1em,
  morekeywords={settofirstfound, setmainfont, boldfont, mainfont,
                FrutigerLight, newfontfamily, lightfont},
}
\lstMakeShortInline|

\usepackage[colorlinks=true,
            linkcolor=mLightBrown,
            menucolor=mLightBrown,
            pagecolor=mLightBrown,
            urlcolor=mLightBrown]{hyperref}

\usepackage{tabu}
\usepackage{longtable}
\usepackage{xstring}

\begin{document}
\DocInput{iffont.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \GetFileInfo{iffont.sty}
% \title{The \textsc{iffont} package}
% \author{Benjamin Weiss\\ \url{weissbenjamin@me.com}}
% \date{Version \fileversion~from \filedate}
%
% \maketitle
%
% \section{Introduction}
%
% Using OpenType fonts within \XeLaTeX\ or \LuaTeX\ comes with a lot of
% benefits, but also the problem, that desired fonts may not be installed on
% the system, the user choses for the compilation. In case they are, there can
% still be different revisions of a font with variant names, such as
% \textit{Frutiger LT Std}, \textit{Frutiger Neue LT Pro} and \textit{Frutiger
% Next Pro}.
%
% Inspired by a question on \href{http://tex.stackexchange.com/q/53443/}
% {Stack Exchange} and a discussion on the
% \href{http://tug.org/pipermail/xetex/2010-August/017833.html}{\XeTeX\
% Mailinglist}, this package addresses the problem with a set of easy to use
% macros: a macro to select the first font \XeLaTeX\ or \LuaTeX\ can find in a
% comma separated list and, additionally, a number of macro tests.
%
% \section{Select a Font from a List of Fonts}
%
% \DescribeMacro{\settofirstfound} This command defines a new macro with the
% first font found in the comma separated list. The list is provided as second
% argument. The macro can then be used to set the main font, set the sans
% font, define a new font family and so on. The following code shows how to
% set the main font as the Frutiger Font with the black font weight as bold
% font.
% \begin{lstlisting}
% \documentclass{article}
% \usepackage{fontspec}
% \usepackage{iffont}
% \settofirstfound{\mainfont}{Frutiger Next Pro,
%                             Frutiger Neue LT Pro,
%                             Frutiger LT Std}
% \settofirstfound{\boldfont}{Frutiger Next Pro Black,
%                             Frutiger Neue LT Pro Black,
%                             Frutiger LT Std 75 Black}
% \setmainfont[BoldFont=\boldfont]{\mainfont}
% \begin{document}
% \begin{itemize}
%   \item \mainfont
%   \item \textbf{\boldfont}
% \end{itemize}
% \end{document}
% \end{lstlisting}
% Defining a new font family works just the same.
% \begin{lstlisting}
% \documentclass{article}
% \usepackage{fontspec}
% \usepackage{iffont}
% \settofirstfound{\lightfont}{Frutiger Next Pro Light,
%                              Frutiger Neue LT Pro Light,
%                              Frutiger LT Std 45 Light}
% \newfontfamily\FrutigerLight{\lightfont}
% \begin{document}
% \begin{itemize}
%   \item {\FrutigerLight \lightfont}
% \end{itemize}
% \end{document}
% \end{lstlisting}
% \StopEventually{}
%
% \section{Macro Tests}
% In most cases the \lstinline|\settofirstfound| command is sufficient, but
% for the more demanding user there is also a set of macro tests. They are
% useful, if one wants to set specific font features only if the first choice
% font cannot be found. The original \textit{Frutiger LT Std} font for example
% does not support small caps. So in the following example the
% \lstinline|\textsc|command is redefined as uppercase with increased letter
% spacing, but only if the \textit{Frutiger Next Pro} font cannot be found.
% \begin{lstlisting}
% \documentclass{article}
% \usepackage{fontspec}
% \usepackage{iffont}
% \settofirstfound{\mainfont}{Frutiger Next Pro,
%                             Frutiger LT Std}
% \setmainfont{\mainfont}
% \ifxfontexists{Frutiger Next Pro}{%
%   \def\textsc#1{\addfontfeature{LetterSpace=5.0}%
%                 \MakeUppercase{#1}}
% }{}
% \begin{document}
% \begin{itemize}
%   \item \mainfont
%   \item \textsc{\mainfont}
% \end{itemize}
% \end{document}
% \end{lstlisting}
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
%
%
% \subsection{Required Packages}
% Load required packages.
%    \begin{macrocode}
\RequirePackage{fontspec}
\RequirePackage{etoolbox}
%    \end{macrocode}
%
% Init required variables.
%    \begin{macrocode}
\newtoggle{@iffont@fontfound}
\newcommand{\@iffont@firstfont}{Fira Sans}
\newcounter{@iffont@fontsnotfound}
%    \end{macrocode}
%
% \begin{macro}{\iffontsexist}
% If all fonts are found the commands in the second argument will be executed,
% otherwise the commands in the third argument.
%    \begin{macrocode}
\newcommand{\iffontsexist}[3]{
  \setcounter{@iffont@fontsnotfound}{0}
  \expandafter\forcsvlist\expandafter\@iffont@checkfont\expandafter{#1}
  \ifnumequal{\value{@iffont@fontsnotfound}}{0}{%
    #2
  }{%
    #3
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifxfontsexist}
% Same as \lstinline|\iffontsexist|, but negated.
%    \begin{macrocode}
\newcommand{\ifxfontsexist}[3]{
  \iffontsexist{#1}{#3}{#2}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\iffontexists}
% A simpler and therefore faster if clause, that only checks for a single font.
%    \begin{macrocode}
\newcommand{\iffontexists}[3]{
  \suppressfontnotfounderror=1
  \font\x = "#1" at 10pt
  \ifx\x\nullfont
    #3
  \else
    #2
  \fi
  \suppressfontnotfounderror=0
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifxfontexists}
% Same as \lstinline|\iffontexists|, but negated.
%    \begin{macrocode}
\newcommand{\ifxfontexists}[3]{
  \iffontexists{#1}{#3}{#2}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\settofirstfound}
% Sets the macro in the first argument to the first font in the comma separated
% list of fonts in the second argument that is found.
%    \begin{macrocode}
\newcommand{\settofirstfound}[2]{
  \togglefalse{@iffont@fontfound}
  \expandafter\forcsvlist\expandafter\@iffont@checkfont\expandafter{#2}
  \let#1\@iffont@firstfont
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@iffont@checkfont}
% Checks if a font is found and increases the \lstinline|@iffont@fontsnotfound|
% counter if not. The first font found will be saved in
% \lstinline|\@iffont@firstfont|.
%    \begin{macrocode}
\newcommand{\@iffont@checkfont}[1]{
  \suppressfontnotfounderror=1
  \font\x = "#1" at 10pt
  \ifx\x\nullfont
    \stepcounter{@iffont@fontsnotfound}
  \else
    \nottoggle{@iffont@fontfound}{%
      \renewcommand{\@iffont@firstfont}{#1}
      \toggletrue{@iffont@fontfound}
    }{}
  \fi
  \suppressfontnotfounderror=0
}
%    \end{macrocode}
% \end{macro}
%
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput