Difference between revisions of "Use the fonts you want"

From Wiki
Jump to navigation Jump to search
m (Corrected a mistake in the mtxrun command (it was fot it should be fonts))
 
(70 intermediate revisions by 9 users not shown)
Line 3: Line 3:
 
The first step is to declare the storage locations of the fonts you want ConTeXt to consider.
 
The first step is to declare the storage locations of the fonts you want ConTeXt to consider.
  
Either way, ConTeXt will use fonts properly stored in its tree structure (for example, any fonts you would have downloaded from [https://www.fontsquirrel.com Fonts Squirrel] [https://fonts.google.com Google Fonts]).
+
Either way, ConTeXt will use fonts properly stored in its tree structure (for example, any fonts you would have downloaded from [https://www.fontsquirrel.com Fonts Squirrel] [https://fonts.google.com Google Fonts], or see [[Available Free Fonts]]).
 
* '''TeX users create a new folder for each new font in <code>tex/texmf-fonts/fonts/</code>''', following [http://tug.org/tds/ TeX Directory Structure].
 
* '''TeX users create a new folder for each new font in <code>tex/texmf-fonts/fonts/</code>''', following [http://tug.org/tds/ TeX Directory Structure].
 
** it helps the algorithms to deal with the incredibly wide variety of fonts's variables and parameters.
 
** it helps the algorithms to deal with the incredibly wide variety of fonts's variables and parameters.
** People who handle a lot of fonts can be more structured by using <code>tex/texmf-fonts/fonts/truetype/vendor/fontfamily</code>.
+
** People who handle a lot of fonts can be more structured by using
 
+
*: <code>tex/texmf-fonts/fonts/truetype/vendor/fontfamily</code>.
  
 
But it it's very likely that you also want to use the '''fonts already available on your Operating System''':
 
But it it's very likely that you also want to use the '''fonts already available on your Operating System''':
 
;1. Specify where ConTeXt should looked for fonts, by setting the '''OSFONTDIR''' environment variable.
 
;1. Specify where ConTeXt should looked for fonts, by setting the '''OSFONTDIR''' environment variable.
 
:* WINDOWS: <code>set OSFONTDIR=c:/windows/fonts/</code>
 
:* WINDOWS: <code>set OSFONTDIR=c:/windows/fonts/</code>
:* MAC: <code>export OSFONTDIR=/Library/Fonts/;/System/Library/Fonts;$HOME/Library/Fonts</code>
+
:* MAC: <code>export OSFONTDIR=/Library/Fonts/:/System/Library/Fonts:$HOME/Library/Fonts</code>
:* GNU/LINUX: <code>export OSFONTDIR=/Library/Fonts/;/System/Library/Fonts;$HOME/Library/Fonts</code>
+
:* GNU/LINUX: <code>export OSFONTDIR=$HOME/.fonts:/usr/share/fonts</code>
 
:* Add it to your .bashrc or shell equivalent to make the declaration permanent.
 
:* Add it to your .bashrc or shell equivalent to make the declaration permanent.
;2. Update ConTeXt databases
+
;2. Run ConTeXt to index the files and the fonts :
:* the file database: <code>mtxrun --generate</code>
+
<texcode>mtxrun --generate
:* the fonts database: <code>mtxrun --script font --reload</code>
+
mtxrun --script fonts --reload
;3. Check by looking for the specific font you want to use next
+
</texcode>
:* a very common example <code>mtxrun --script font --list --file -pattern=*helvetica*</code>
+
;3. Check by looking for the specific font you want to use next. A common example:
 +
<texcode>mtxrun --script fonts --list --all --pattern='*helvetica*'</texcode>
 +
 
 +
'''NOTE''' : In recent versions of ConTeXt (2023), the use of wildcards is no longer necessary to check for the presence of a particular font.
 +
 
 +
In addition, it is no longer necessary to tell ConTeXt where the fonts are located (on Windows or Apple Mac), as a routine has been implemented in <texcode>mtxrun.lua</texcode> that automates the font search. If no value for OSFONTDIR is set, then mtxrun.lua looks to the directory. Here is the code of mtxrun.lua (given here as information) :
 +
 
 +
<texcode>
 +
do
 +
local osfontdir=osgetenv("OSFONTDIR")
 +
if osfontdir and osfontdir~="" then
 +
elseif osname=="windows" then
 +
  ossetenv("OSFONTDIR","c:/windows/fonts//")
 +
elseif osname=="macosx" then
 +
ossetenv("OSFONTDIR","$HOME/Library/Fonts//;/Library/Fonts//;/System/Library/Fonts//")
 +
end
 +
end
 +
</texcode>
 +
 
 +
On the other hand, if the routine seeks to locate fonts on Windows and Mac, insofar as we are seeking to install new fonts for the Linux system, we must ask ConTeXt — as indicated above — for the path that leads to the directory where the new fonts are deposited; in this case <texcode>tex/texmf-fonts/fonts/</texcode>. Then you'll need to empty the font cache and regenerate it, so that the <texcode>mtxrun --script fonts --list --all --pattern=didot</texcode> command can return something (if you've installed a GFS Didot, or Theano Didot font, for example).
 +
 
 +
To empty the cache and regenerate it, enter the following command (in the directory : /home/johndoe/context/tex/texmf-linux64/bin):
 +
<texcode>mtxrun --script cache --erase && mtxrun --generate</texcode>
 +
 
 +
Now you can search for the font you've just installed.
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
Now, let's learn how to use them for typesetting.
  
 +
= Practical application #1 - Quick use of a new font =
  
You have now:
+
Let's take an example: we want to use [https://fonts.google.com/specimen/Noto+Serif Noto Serif].  
* to define [[Use_fonts_found_on_the_web#Use_of_the_different_font_variations|a new typescript]] or use one of the [[Typescripts_examples| many typescripts already available]]
 
* and then [[Use_fonts_found_on_the_web#Install_and_use_your_new_typescript_definition| install and use it]] in you input file.
 
  
= Quick use of a new font =
+
If it is already installed on your computer, and you already updated the ConTeXt databases like shown previously, go directly to '''#2'''.
  
Let's take a example, we want to use [https://fonts.google.com/specimen/Noto+Serif Noto Serif]. The google website enable us to download a zip file with the 4 alternative variations (Regular 400, Regular 400 italic, Bold 700, Bold 700 italic).
+
It not, you have to download and store it first. The Google website provides a zip file with the 4 alternative variations (Regular 400, Regular 400 italic, Bold 700, Bold 700 italic).
  
; #1. Store them is a dedicated folder
+
; #1. Store them in a dedicated folder [[#Fonts location on your computer|indexed by ConTeXt]].
:* create a "Noto-serif" in the ConTeXt distribution <code>tex/texmf-fonts</code>.
+
:* for example, create a "Noto-serif" in the ConTeXt distribution <code>tex/texmf-fonts/fonts/</code>.
:* unzip and store the .ttf file in <code>tex/texmf-fonts/Noto-serif/</code>.
+
:* unzip and store the .ttf file in <code>tex/texmf-fonts/fonts/Noto-serif/</code>.
 +
:* Regenerate ConTeXt databases
 +
:** the file database:  <code>mtxrun --generate</code>
 +
:** the fonts database: <code>mtxrun --script font --reload</code>
  
; #2. Regenerate ConTeXt databases
+
; #2. Now you can check the ''fontname'' used to identify the fonts,
:* the file database:  <code>mtxrun --generate</code>
+
:*run <code>mtxrun --script font --list --file -pattern=*noto*</code>. You should obtain something like
:* the fonts database: <code>mtxrun --script font --reload</code>
+
<texcode>
:* and check the situation <code>mtxrun --script font --list --file -pattern=*noto*</code>:
+
familyname weight style   width   variant fontname             filename subfont fontweight
<texcode>familyname   weight  style    width   variant   fontname             filename                   subfont   fontweight
 
  
notoserif    bold    normal   normal  normal    notoserifbold         NotoSerif-Bold.ttf
+
notoserif   bold   normal normal  normal  notoserifbold       NotoSerif-Bold.ttf
notoserif    bold    italic   normal  normal    notoserifbolditalic   NotoSerif-BoldItalic.ttf
+
notoserif   bold   italic normal  normal  notoserifbolditalic NotoSerif-BoldItalic.ttf
notoserif   normal   italic   normal  normal    notoserifitalic       NotoSerif-Italic.ttf
+
notoserif   normal italic normal  normal  notoserifitalic     NotoSerif-Italic.ttf
notoserif   normal   normal   normal  normal    notoserif             NotoSerif-Regular.ttf
+
notoserif   normal  normal normal normal  notoserif           NotoSerif-Regular.ttf
 
</texcode>
 
</texcode>
  
; #3. Your are done! You can use the font anywhere in your input files with {{cmd|definedfont}}
+
; #3. You are done! You can use the font anywhere in your input files with {{cmd|definedfont}}  
 +
:* it's good to add <code>*default</code> to benefit from default features, like for example [http://www.pragma-ade.nl/general/manuals/fonts-mkiv.pdf#&#91;169,{%22name%22:%22Fit%22}&#93;  kerning].
 
<texcode>
 
<texcode>
\definedfont[file:NotoSerif-BoldItalic at 12pt]
+
\definedfont[name:notoserifbolditalic*default at 12 pt]
 
The quick brown fox jumps over the lazy dog
 
The quick brown fox jumps over the lazy dog
 
</texcode>
 
</texcode>
 
[[File:result_of_test_with_noto-serif_one.png]]
 
[[File:result_of_test_with_noto-serif_one.png]]
  
 +
= Practical application #2 - Use the different font variations =
  
 
+
* It's not nice to have to write \definedfont[name:yoursuperfont-weightstyle*default at xxpt] each time you want to use a particular font.  
= Use of the different font variations =
+
* That why it's worth defining a '''typescript'''. It's just 3 steps, and less than 5 minutes. Then, you will be able to easily switch between fonts with {{cmd|tf}}, {{cmd|it}}, {{cmd|bf}}, {{cmd|bi}} (see [[Font_Switching#Font_styles_and_alternatives|Font styles and alternatives]]), and all the typesetting of your document will use a consistent set of fonts.
 
+
* [[Typescripts_examples| Many typescripts are ready]] for use on usual free and commercial fonts, and obviously for the [[ConTeXt_distribution's_Fonts|ConTeXt distribution's Fonts]].  
* It's not nice to have to write \definedfont[file:YourSuperFont-WeightStyle at xxpt] each time you want to use a particular font.  
 
* That why it's worth defining a '''typescript'''. It's just 3 steps, and less than 5 minutes. Then, you will be able to easily switch between fonts with {{cmd|tf}}, {{cmd|it}}, {{cmd|bf}}, {{cmd|bi}} (see [[Font_Switching#Font_styles_and_alternatives]]), and all the typesetting of your document will use a consistent set of fonts.
 
* [[Typescripts_examples| Many typescripts are ready for use on usual free and commercial fonts]].  
 
  
  
Line 77: Line 108:
 
</texcode>
 
</texcode>
  
; #2. The boring step, define the links between ''ConTeXt basics names'' and the ''People-readable names''.
+
; #2. The boring step, define the links between ''[https://github.com/contextgarden/context-mirror/blob/a0270f13065d116355a953c6f246cbba26289fc2/tex/context/base/mkiv/font-sel.lua#L514 ConTeXt basics names]'' and the ''People-readable names''.
 
:* It's good to add a fallback.
 
:* It's good to add a fallback.
 
<texcode>
 
<texcode>
 
\starttypescript [mynotoserif]
 
\starttypescript [mynotoserif]
   \setups[font:fallback:serif]          % security: if not found==> bask to defaults
+
   \setups[font:fallback:serif]          % security: if not found==> back to defaults
% \definefontsynonym[ConTeXt bascics name] [Human readable]      [features=default]
+
% \definefontsynonym[ConTeXt basics name] [Human readable]      [features=default]
 
   \definefontsynonym[Serif]                [NotoSerif-Regular]    [features=default]
 
   \definefontsynonym[Serif]                [NotoSerif-Regular]    [features=default]
 
   \definefontsynonym[SerifItalic]          [NotoSerif-Italic]    [features=default]
 
   \definefontsynonym[SerifItalic]          [NotoSerif-Italic]    [features=default]
Line 131: Line 162:
 
[[File:result_of_test_with_noto-serif.png]]
 
[[File:result_of_test_with_noto-serif.png]]
  
= Install and use your new typescript definition =
+
= Install your new typescript to use it anywhere =
  
You will probably want to use your new customized typescript definition across different documents, so you have to install them in the distribution. Here is the definition.
+
You will probably want to use your new customized typescript definition across different documents, so you have to install them in the distribution. Here, we remind the definition:
  
 
<texcode>
 
<texcode>
Line 157: Line 188:
  
  
# Save your file as <code>type-(fontname|foundry).tex</code>, for example here <code>type-mynotoserif.tex</code>
+
# Save your file as <code>type-(some-name-you-like).tex</code>, for example here <code>type-mynotoserif.tex</code>
 
# Copy the typescript files into <code>tex/texmf-fonts/tex/context/user/</code>
 
# Copy the typescript files into <code>tex/texmf-fonts/tex/context/user/</code>
 
# Run <code>context --generate</code> to update ConTeXt file database
 
# Run <code>context --generate</code> to update ConTeXt file database
# You are done!
+
# It's done! Now, two lines at the beginning of any input will declare to typeset with the new fonts:
#* you can add \usetypescriptfile[mynotoserif] at the beginning of any input file
+
<texcode>
#* and \setupbodyfont[mynotoserif] will declare to typeset with your new font.
+
\usetypescriptfile[mynotoserif] % this is the 'some-name-you-like' part of the saved filename
 +
\setupbodyfont[mynotoserif] % this is the first argument to \definetypeface
 +
</texcode>
 +
 
 +
====Note:====
 +
 
 +
The naming convention for typescript files has changed in LMTX
 +
 
 +
<blockquote>
 +
There has been a change in the naming of typescript file, a simple <code>"type-"</code> prefix isn't anough anymore, you have to change it to <code>"type-imp-" </code>(e.g. type-imp-sourceserif.tex) to be found by ConTeXt.
 +
 
 +
Valid file extensions for a typescript file in LMTX (and MkIV) are "tex", "mkiv" and "mkxl".
 +
 
 +
Wolfgang [20 Dec 2020]
 +
</blockquote>
 +
 
 +
 
 +
[[Category:Fonts]]

Latest revision as of 17:17, 26 September 2023

Fonts location on your computer

The first step is to declare the storage locations of the fonts you want ConTeXt to consider.

Either way, ConTeXt will use fonts properly stored in its tree structure (for example, any fonts you would have downloaded from Fonts Squirrel Google Fonts, or see Available Free Fonts).

  • TeX users create a new folder for each new font in tex/texmf-fonts/fonts/, following TeX Directory Structure.
    • it helps the algorithms to deal with the incredibly wide variety of fonts's variables and parameters.
    • People who handle a lot of fonts can be more structured by using
    tex/texmf-fonts/fonts/truetype/vendor/fontfamily.

But it it's very likely that you also want to use the fonts already available on your Operating System:

1. Specify where ConTeXt should looked for fonts, by setting the OSFONTDIR environment variable.
  • WINDOWS: set OSFONTDIR=c:/windows/fonts/
  • MAC: export OSFONTDIR=/Library/Fonts/:/System/Library/Fonts:$HOME/Library/Fonts
  • GNU/LINUX: export OSFONTDIR=$HOME/.fonts:/usr/share/fonts
  • Add it to your .bashrc or shell equivalent to make the declaration permanent.
2. Run ConTeXt to index the files and the fonts 
mtxrun --generate
mtxrun --script fonts --reload
3. Check by looking for the specific font you want to use next. A common example
mtxrun --script fonts --list --all --pattern='*helvetica*'

NOTE : In recent versions of ConTeXt (2023), the use of wildcards is no longer necessary to check for the presence of a particular font.

In addition, it is no longer necessary to tell ConTeXt where the fonts are located (on Windows or Apple Mac), as a routine has been implemented in

mtxrun.lua

that automates the font search. If no value for OSFONTDIR is set, then mtxrun.lua looks to the directory. Here is the code of mtxrun.lua (given here as information) :

do
 local osfontdir=osgetenv("OSFONTDIR")
 if osfontdir and osfontdir~="" then
 elseif osname=="windows" then
  ossetenv("OSFONTDIR","c:/windows/fonts//")
 elseif osname=="macosx" then
ossetenv("OSFONTDIR","$HOME/Library/Fonts//;/Library/Fonts//;/System/Library/Fonts//")
 end
end 

On the other hand, if the routine seeks to locate fonts on Windows and Mac, insofar as we are seeking to install new fonts for the Linux system, we must ask ConTeXt — as indicated above — for the path that leads to the directory where the new fonts are deposited; in this case

tex/texmf-fonts/fonts/

. Then you'll need to empty the font cache and regenerate it, so that the

mtxrun --script fonts --list --all --pattern=didot

command can return something (if you've installed a GFS Didot, or Theano Didot font, for example).

To empty the cache and regenerate it, enter the following command (in the directory : /home/johndoe/context/tex/texmf-linux64/bin):

mtxrun --script cache --erase && mtxrun --generate

Now you can search for the font you've just installed.




Now, let's learn how to use them for typesetting.

Practical application #1 - Quick use of a new font

Let's take an example: we want to use Noto Serif.

If it is already installed on your computer, and you already updated the ConTeXt databases like shown previously, go directly to #2.

It not, you have to download and store it first. The Google website provides a zip file with the 4 alternative variations (Regular 400, Regular 400 italic, Bold 700, Bold 700 italic).

#1. Store them in a dedicated folder indexed by ConTeXt.
  • for example, create a "Noto-serif" in the ConTeXt distribution tex/texmf-fonts/fonts/.
  • unzip and store the .ttf file in tex/texmf-fonts/fonts/Noto-serif/.
  • Regenerate ConTeXt databases
    • the file database: mtxrun --generate
    • the fonts database: mtxrun --script font --reload
#2. Now you can check the fontname used to identify the fonts,
  • run mtxrun --script font --list --file -pattern=*noto*. You should obtain something like
familyname  weight  style   width   variant  fontname             filename  subfont  fontweight

notoserif   bold    normal  normal  normal   notoserifbold        NotoSerif-Bold.ttf
notoserif   bold    italic  normal  normal   notoserifbolditalic  NotoSerif-BoldItalic.ttf
notoserif   normal  italic  normal  normal   notoserifitalic      NotoSerif-Italic.ttf
notoserif   normal  normal  normal  normal   notoserif            NotoSerif-Regular.ttf
#3. You are done! You can use the font anywhere in your input files with \definedfont
  • it's good to add *default to benefit from default features, like for example kerning.
\definedfont[name:notoserifbolditalic*default at 12 pt]
The quick brown fox jumps over the lazy dog

result of test with noto-serif one.png

Practical application #2 - Use the different font variations

  • It's not nice to have to write \definedfont[name:yoursuperfont-weightstyle*default at xxpt] each time you want to use a particular font.
  • That why it's worth defining a typescript. It's just 3 steps, and less than 5 minutes. Then, you will be able to easily switch between fonts with \tf, \it, \bf, \bi (see Font styles and alternatives), and all the typesetting of your document will use a consistent set of fonts.
  • Many typescripts are ready for use on usual free and commercial fonts, and obviously for the ConTeXt distribution's Fonts.


#1. Define a new typescript in your input file, with \starttypescript.
  • Define the links between filenames and People-readable names with \definefontsynonym.
  • In this example, the typescript is called "mynotoserif".
  • Reminder: you find the filenames for the Noto Serif fonts with mtxrun --script font --list --file -pattern=*noto*:
\starttypescript [mynotoserif]
% \definefontsynonym[Human readable]       [file:filename without extension]
  \definefontsynonym[NotoSerif-Regular]    [file:NotoSerif-Regular]
  \definefontsynonym[NotoSerif-Italic]     [file:NotoSerif-Italic]
  \definefontsynonym[NotoSerif-Bold]       [file:NotoSerif-Bold]
  \definefontsynonym[NotoSerif-BoldItalic] [file:NotoSerif-BoldItalic]
\stoptypescript
#2. The boring step, define the links between ConTeXt basics names and the People-readable names.
  • It's good to add a fallback.
\starttypescript [mynotoserif]
  \setups[font:fallback:serif]          % security: if not found==> back to defaults
% \definefontsynonym[ConTeXt basics name] [Human readable]       [features=default]
  \definefontsynonym[Serif]                [NotoSerif-Regular]    [features=default]
  \definefontsynonym[SerifItalic]          [NotoSerif-Italic]     [features=default]
  \definefontsynonym[SerifBold]            [NotoSerif-Bold]       [features=default]
  \definefontsynonym[SerifBoldItalic]      [NotoSerif-BoldItalic] [features=default]
\stoptypescript
#3. Define the pack of the 4 variations as the roman typeface of the typescript "mynotoserif" 
\starttypescript [mynotoserif]
  \definetypeface [mynotoserif]    [rm] [serif] [mynotoserif]    [default]
\stoptypescript
#4. You are done! Finally, we have the following input file
\starttypescript [mynotoserif]
  \definefontsynonym[NotoSerif-Regular]    [file:NotoSerif-Regular]
  \definefontsynonym[NotoSerif-Italic]     [file:NotoSerif-Italic]
  \definefontsynonym[NotoSerif-Bold]       [file:NotoSerif-Bold]
  \definefontsynonym[NotoSerif-BoldItalic] [file:NotoSerif-BoldItalic]
\stoptypescript

\starttypescript [mynotoserif]
  \setups[font:fallback:serif]
  \definefontsynonym[Serif]                [NotoSerif-Regular]    [features=default]
  \definefontsynonym[SerifItalic]          [NotoSerif-Italic]     [features=default]
  \definefontsynonym[SerifBold]            [NotoSerif-Bold]       [features=default]
  \definefontsynonym[SerifBoldItalic]      [NotoSerif-BoldItalic] [features=default]
\stoptypescript

\starttypescript [mynotoserif]
  \definetypeface [mynotoserif]    [rm] [serif] [mynotoserif]    [default]
\stoptypescript

\setupbodyfont[mynotoserif]
\setupbodyfont[12pt]
{    The quick brown fox jumps over the lazy dog}\\
{\it The quick brown fox jumps over the lazy dog}\\
{\bf The quick brown fox jumps over the lazy dog}\\
{\bi The quick brown fox jumps over the lazy dog}\\

And it gives:

result of test with noto-serif.png

Install your new typescript to use it anywhere

You will probably want to use your new customized typescript definition across different documents, so you have to install them in the distribution. Here, we remind the definition:

\starttypescript [mynotoserif]
  \definefontsynonym[NotoSerif-Regular]    [file:NotoSerif-Regular]
  \definefontsynonym[NotoSerif-Italic]     [file:NotoSerif-Italic]
  \definefontsynonym[NotoSerif-Bold]       [file:NotoSerif-Bold]
  \definefontsynonym[NotoSerif-BoldItalic] [file:NotoSerif-BoldItalic]
\stoptypescript

\starttypescript [mynotoserif]
  \setups[font:fallback:serif]
  \definefontsynonym[Serif]                [NotoSerif-Regular]    [features=default]
  \definefontsynonym[SerifItalic]          [NotoSerif-Italic]     [features=default]
  \definefontsynonym[SerifBold]            [NotoSerif-Bold]       [features=default]
  \definefontsynonym[SerifBoldItalic]      [NotoSerif-BoldItalic] [features=default]
\stoptypescript

\starttypescript [mynotoserif]
  \definetypeface [mynotoserif]    [rm] [serif] [mynotoserif]    [default]
\stoptypescript


  1. Save your file as type-(some-name-you-like).tex, for example here type-mynotoserif.tex
  2. Copy the typescript files into tex/texmf-fonts/tex/context/user/
  3. Run context --generate to update ConTeXt file database
  4. It's done! Now, two lines at the beginning of any input will declare to typeset with the new fonts:
\usetypescriptfile[mynotoserif] % this is the 'some-name-you-like' part of the saved filename
\setupbodyfont[mynotoserif] % this is the first argument to \definetypeface

Note:

The naming convention for typescript files has changed in LMTX

There has been a change in the naming of typescript file, a simple "type-" prefix isn't anough anymore, you have to change it to "type-imp-" (e.g. type-imp-sourceserif.tex) to be found by ConTeXt.

Valid file extensions for a typescript file in LMTX (and MkIV) are "tex", "mkiv" and "mkxl".

Wolfgang [20 Dec 2020]