Font fallback in ConTeXt Mininals

Indubitably, I need to remix Chinese and English in one document. But English characters in Chinese fonts aren’t look good in most time as others, so font fallback is needed here.
First, make a English font as a fallback of Chinese font:

\definefontsynonym[song][name:adobesongstd]
\definefontfallback[hereos][name:texgyreherosregular][0x0000-0x0400]
\definefontsynonym[fbsong][song][fallbacks=hereos]
 
\definefont[newsong][fbsong]
\setscript[hanzi]
 
\starttext
\newsong 夹杂 English 的中文文本
\stoptext

This means we use texgyreherosregular as the fallback English font and name it as hereos, 0x0000-0x0400 is the interval of English characters in UTF-8 encoding. Then make “fbsong” the new font that is the remix of song (adobesongstd) and fallback hereos (texgyreherosregular).
But on the tutorial I am following, the author said such action will lose some feature when ConTeXt processing English, so change to using a Chinese font as a fallback of English font:

\definefontsynonym[hereos][name:texgyreherosregular]
\definefontfallback[song][name:adobesongstd][0x00400-0x2FA1F]
\definefontsynonym[fbsong][hereos][fallbacks=song]
\definefont[newsong][fbsong]
\setscript[hanzi]
 
\starttext
\newsong 夹杂~English~的中文文本
\stoptext

Then I’ve rewritten my type-myfonts.tex with fallback font:

\starttypescript[serif][fbfont]
 
\definefontsynonym[enserif][name:dejavuserif]
\definefontsynonym[enserifbd][name:dejavuserifbold]
\definefontsynonym[enserifit][name:dejavuserifitalic]
\definefontsynonym[enserifbi][name:dejavuserifbolditalic]
 
\definefontfallback[umingcn][name:arplumingcn][0x00400-0x2FA1F]
 
\definefontsynonym[Serif][enserif][fallbacks=umingcn]
\definefontsynonym[SerifBold][enserifbd][fallbacks=umingcn]
\definefontsynonym[SerifItalic][enserifit][fallbacks=umingcn]
\definefontsynonym[SerifBoldItalic][enserifbi][fallbacks=umingcn]
 
\stoptypescript
 
\starttypescript [sans][fbfont]
 
\definefontsynonym[ensans][name:dejavusans]
\definefontsynonym[ensansbd][name:dejavusansbold]
\definefontsynonym[ensansob][name:dejavusansoblique]
\definefontsynonym[ensansbo][name:dejavusansboldoblique]
 
\definefontfallback[ukaicn][name:arplukaicn][0x00400-0x2FA1F]
 
\definefontsynonym[Sans][ensans][fallbacks=ukaicn]
\definefontsynonym[SansBold][ensansbd][fallbacks=ukaicn]
\definefontsynonym[SansOblique][ensansob][fallbacks=ukaicn]
 
\definefontsynonym[SansBoldOblique][ensansbo][fallbacks=ukaicn]
 
\stoptypescript
 
\starttypescript [mono][fbfont]
 
\definefontsynonym[enmono][name:dejavusansmono]
\definefontsynonym[enmonobd][name:dejavusansmonobold]
\definefontsynonym[enmonoob][name:dejavusansmonooblique]
\definefontsynonym[enmonobo][name:dejavusansmonoboldoblique]
 
\definefontfallback[microhei][name:wenquanyimicroheimono][0x00400-0x2FA1F]
 
\definefontsynonym[Mono][enmono][fallbacks=microhei]
\definefontsynonym[MonoBold][enmonobd][fallbacks=microhei]
\definefontsynonym[MonoOblique][enmonoob][fallbacks=microhei]
 
\definefontsynonym[MonoBoldOblique][enmonobo][fallbacks=microhei]
 
\stoptypescript
 
\starttypescript[myfonts]
\definetypeface[myfonts][rm][serif][fbfont]
\definetypeface[myfonts][ss][sans][fbfont]
\definetypeface[myfonts][tt][mono][fbfont]
\stoptypescript

And here is an example:

\usetypescriptfile[type-myfonts]
\usetypescript[myfonts]
\setupbodyfont[myfonts,rm,12pt]
\setupbodyfontenvironment [default] [em=italic] % slanted is default emphasize in ConTeXt
\setscript[hanzi]
 
\pdfoptionpdfminorversion=6
\pdfcompresslevel=9
 
\starttext
 
\startcomment
this is a test comment, you shouldn't see me.
\stopcomment
 
我们在运行 \CONTEXT{}
 
\rm 这是 fbfont 里的 serif,dejavuserif+arplumingcn \\
\ss 这是 fbfont 里的 sans,dejavusans+arplukaicn \\
\tt 这是 fbfont 里的 mono,dejavusansmono+wenquanyimicroheimono \\
 
\stoptext

“setupbodyfontenvironment” is a command that tell ConTeXt our body’s font environment, here we change nothing but emphasis to italic instead of default slanted face.
“%” is used to take comments and the TeX engine won’t do anything with items after it to the line’s ending. Multi-lines comments can be achieved by using “\startcomment … \stopcomment”.
“\pdfoptionpdfminorversion” is the setting for pdf option in minor version, and we can have a table to list it:
* (1993) – PDF 1.0 / Acrobat 1.0
* (1994) – PDF 1.1 / Acrobat 2.0
* (1996) – PDF 1.2 / Acrobat 3.0
* (1999) – PDF 1.3 / Acrobat 4.0
* (2001) – PDF 1.4 / Acrobat 5.0
* (2003) – PDF 1.5 / Acrobat 6.0
* (2005) – PDF 1.6 / Acrobat 7.0
* (2006) – PDF 1.7 / Acrobat 8.0
* (2008) – PDF 1.7, Adobe Extension Level 3 / Acrobat 9.0
The value of “pdfoptionpdfminorversion” is that x in “1.x”, in this example, “\pdfoptionpdfminorversion=6”, we set the version to PDF 1.6, correspond to Acrobat 7.0. The default setting is version 5 – PDF 1.5 for Acrobat 6.0.
“pdfcompresslevel” is the compress level of output pdf file, 0 is the least and 9 highest. Default setting is 0.
“\CONTEXT” is a sequence to produce the effect of sinking e in TEX. Other interesting are “\LATEX” “\TEX”, also “\LaTeX” and “\TeX” is acceptable, but I didn’t find how to produce a LaTeX2e, not “\LaTeXe” in LaTeX.
The {} after “\CONTEXT{}” tell the TeX engine don’t eat all extra whitespace and empty lines.
“\\” means force line breaking, we cannot use “\newline” as LaTeX does.

Font model in ConTeXt – quick view from a starter

There are three basic fonts in Chinese, Song, Hei and Fangsong, corresponding to Serif, Sans and Monospace in Latin characters. Another category is numbers, it is recognized as a separate category by ConTeXt.
Using Adobe’s three fonts as a example, we can define type-myfonts.tex, a typescript file:

\starttypescript[serif][zhfont]
    \definefontsynonym[Serif][name:adobesongstd]
    \definefontsynonym[SerifBold][name:adobesongstd]
    \definefontsynonym[SerifItalic][name:adobesongstd]
    \definefontsynonym[SerifBoldItalic][name:adobesongstd]
\stoptypescript
 
\starttypescript [sans][zhfont]
    \definefontsynonym[Sans][name:adobeheitistd]
    \definefontsynonym[SansBold][name:adobeheitistd]
    \definefontsynonym[SansItalic][name:adobeheitistd]
    \definefontsynonym[SansBoldItalic][name:adobeheitistd]
\stoptypescript
 
\starttypescript [mono][zhfont]
    \definefontsynonym[Mono][name:adobefangsongstd]
    \definefontsynonym[MonoBold][name:adobefangsongstd]
    \definefontsynonym[MonoItalic][name:adobefangsongstd]
    \definefontsynonym[MonoBoldItalic][name:adobefangsongstd]
\stoptypescript
 
\starttypescript[myfonts]
    \definetypeface[myfonts][rm][serif][zhfont]
    \definetypeface[myfonts][ss][sans][zhfont]
    \definetypeface[myfonts][tt][mono][zhfont]
\stoptypescript

Explanation by line:
“\starttypescript[serif][zhfont]” means starting a typescript profile, named “serif” and classify it in “zhfont”;
“\definefontsynonym[Serif][name:adobesongstd]” means start a font synonym definition, and assign “Serif” to “adobesongstd”;
“\stoptypescript” means finishing a profile.
We need to pay some attention at the last profile, that is to say “\definetypeface[myfonts][rm][serif][zhfont]”, we define the typeface series “myfonts” in this profile, and this line represent the “rm”, roman, face is assigned to serif, which we have defined in previous profiles, and “ss” fo “sans”, “tt” for “teletype” or rather “monospace” as we know.
Then write another file, save as test.tex:

\usetypescriptfile[type-myfonts]
\usetypescript[myfonts]
\setupbodyfont[myfonts,rm,12pt]
 
\starttext
所有动物都生而平等,但有些动物比其他动物更平等
\ss 所有动物都生而平等,但有些动物比其他动物更平等
\tt 所有动物都生而平等,但有些动物比其他动物更平等
\stoptext

For explanation, “\usetypescriptfile[type-myfonts]” tell the TeX engine to find a typescript file named type-myfonts.tex in the same directory as current file, and include the contents here; \usetypescript[myfonts]” tell it we are using “myfonts” typescript set as defined in type-myfonts.tex; “\setupbodyfont[myfonts,rm,12pt]” is telling that set the body font to myfonts’s rm style, with 12pt as the size.
Between “\starttext”…”\stoptext” is the text, first line using default font, \rm, and second line change to \ss finaly \tt.
Here is a list to tell font model:
Font model from ConTeXt wiki
Next we need to make ConTeXt know how to wrap lines for Chinese, that is, to break a long sentence into lines in a page. What we need to do is add “\setscript[hanzi]” to the header. Another example of test.tex:

\usetypescriptfile[type-myfonts]\usetypescript[myfonts]\setupbodyfont[myfonts,rm,12pt]
 
\setscript[hanzi]
 
\starttext
明亡后,朝鲜和日本认为中国已亡,日本从此蔑称中国为“支那”。明治维新后,自认为华夏正统的日本曾打着“攘夷主义”的旗号发动甲午战争等对清战争。


\stoptext
This work by Aron Xu is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported.