Arabic and Hebrew

From ConTeXt wiki

Arabic fonts >


TODO: overview page for the use of middle-eastern scripts (See: To-Do List)


Arabic

This is an example environment for typesetting Arabic documents in Mark IV (ConTeXt with LuaTeX). It won't work at all in Mark II (with either pdfTeX or XeTeX).

Save it as "ara-sty.tex" and use "\environment ara-sty" in your document.

\startenvironment ara-sty

\mainlanguage[arabic]

% Font setup

\definefontfeature
   [arabic]
   [mode=node,language=dflt,script=arab,
    init=yes,medi=yes,fina=yes,isol=yes,
    liga=yes,dlig=yes,rlig=yes,clig=yes,
    mark=yes,mkmk=yes,kern=yes,curs=yes]

\starttypescript [serif] [arabic]
 \definefontsynonym [Arabic-Light]       [name:arabtype] [features=arabic]
 \definefontsynonym [Arabic-Bold]        [name:arabtype] [features=arabic]
 \definefontsynonym [Arabic-Italic]      [name:arabtype] [features=arabic]
 \definefontsynonym [Arabic-Bold-Italic] [name:arabtype] [features=arabic]
\stoptypescript

\starttypescript [serif] [arabic] [name]
 \usetypescript[serif][fallback]
 \definefontsynonym [Serif]           [Arabic-Light]       [features=arabic]
 \definefontsynonym [SerifItalic]     [Arabic-Italic]      [features=arabic]
 \definefontsynonym [SerifBold]       [Arabic-Bold]        [features=arabic]
 \definefontsynonym [SerifBoldItalic] [Arabic-Bold-Italic] [features=arabic]
\stoptypescript

\starttypescript [Arabic]
  \definetypeface [Arabic] [rm] [serif] [arabic] [default] 
\stoptypescript 

\def\ArabicGlobalDir {\pagedir TRT\bodydir TRT\pardir TRT\textdir TRT}
\def\ArabicParDir    {\textdir TRT\pardir TRT}
\def\ArabicTextDir   {\textdir TRT}
\def\LatinParDir     {\textdir TLT\pardir TLT}
\def\LatinTextDir    {\textdir TLT}
\def\LatinGlobalDir  {\pagedir TLT\bodydir TLT\pardir TLT\textdir TLT}

\define\setarabic
  {\ArabicGlobalDir%
   \usetypescript[Arabic]%
   \setupbodyfont[Arabic,20pt]}

\definestartstop
  [arabicpar]
  [commands=\Arabic\ArabicParDir]

\define[1]\RT
  {{\Arabic\ArabicTextDir#1}}

\define\setlatin
  {\LatinGlobalDir%
   \usetypescript[lm]%
   \setupbodyfont[lm,20pt]}

\definestartstop
  [latinpar]
  [commands=\Arabic\LatinParDir]

\define[1]\LT
  {{\LatinTextDir#1}}

\setcharactermirroring[1]

\stopenvironment

Description

Here is some description:

\mainlanguage[arabic]

Sets the main language to Arabic, so that translatable titles are translated to Arabic.

\definefontfeature
   [arabic]
   [mode=node,language=dflt,script=arab,
    init=yes,medi=yes,fina=yes,isol=yes,
    liga=yes,dlig=yes,rlig=yes,clig=yes,
    mark=yes,mkmk=yes,kern=yes,curs=yes]

Here we define OpenType font features needed to render Arabic properly.

\starttypescript [serif] [arabic]
 \definefontsynonym [Arabic-Light]       [name:arabtype] [features=arabic]
 \definefontsynonym [Arabic-Bold]        [name:arabtype] [features=arabic]
 \definefontsynonym [Arabic-Italic]      [name:arabtype] [features=arabic]
 \definefontsynonym [Arabic-Bold-Italic] [name:arabtype] [features=arabic]
 \stoptypescript

\starttypescript [serif] [arabic] [name]
 \usetypescript[serif][fallback]
 \definefontsynonym [Serif]           [Arabic-Light]       [features=arabic]
 \definefontsynonym [SerifItalic]     [Arabic-Italic]      [features=arabic]
 \definefontsynonym [SerifBold]       [Arabic-Bold]        [features=arabic]
 \definefontsynonym [SerifBoldItalic] [Arabic-Bold-Italic] [features=arabic]
\stoptypescript

\starttypescript [Arabic]
  \definetypeface [Arabic] [rm] [serif] [arabic] [default] 
\stoptypescript 

Then, we define "Arabic" typescript, here we used a font named "arabtype". Since this font has only regular weight, we set bold and italic to use the same font.

\def\ArabicGlobalDir {\pagedir TRT\bodydir TRT\pardir TRT\textdir TRT}
\def\ArabicParDir    {\textdir TRT\pardir TRT}
\def\ArabicTextDir   {\textdir TRT}
\def\LatinParDir     {\textdir TLT\pardir TLT}
\def\LatinTextDir    {\textdir TLT}
\def\LatinGlobalDir  {\pagedir TLT\bodydir TLT\pardir TLT\textdir TLT}

Here we define some directional commands to use it in the next parts.

\define\setarabic
  {\ArabicGlobalDir%
   \usetypescript[Arabic]%
   \setupbodyfont[Arabic,20pt]}

\definestartstop
  [arabicpar]
  [commands=\Arabic\ArabicParDir]

\define[1]\RT
  {{\Arabic\ArabicTextDir#1}}

Here we define "arabicpar" environment for Arabic paragraphs in Latin context, and "\RT" for short Arabic sentences and "\setarabic" command to set the main document direction and font to Arabic.

\define\setlatin
  {\LatinGlobalDir%
   \usetypescript[lm]%
   \setupbodyfont[lm,20pt]}

\definestartstop
  [latinpar]
  [commands=\Arabic\LatinParDir]

\define[1]\LT
  {{\LatinTextDir#1}}

The counter Latine commands, "latinpar", "\LT" and "\setlatin".

\setcharactermirroring[1]

To enable mirroring of BiDi mirrored characters, like () and []. This also enables "implicit bidi", so that you don't need to explicitly specify the direction of individual Arabic sentences inside Latin context in vise versa.

Using it

Now, lets try a "Hello World" example:

% engine=luatex

\environment ara-sty

\starttext
\setarabic

أهلا بالعالم!
\stoptext

Hebrew

(Example by Rik Kabel on the mailing list, 2017-12-15)

Depending on the font, correct niqqud placement requires some combination (sometimes all) of the following font features: lang, ccmp, and script.

\definefontfeature [hebrew] [oldstyle] [
  lang=heb,
  ccmp=yes,
  script=hebr,
]
\starttext
\definedfont[name:EzraSIL*hebrew at 72pt]
\setupalign[r2l]
טְרוֹפוֹתִי
\stoptext

The same settings work well for Narkisim. For David_CLM you only need the script setting.

Correction by Hans, 2017-12-16:

There is a font feature hebrew already predefined

\definefontfeature [hebrewoldstyle] [oldstyle] [...]

if you have to set a language depends on the font (often dflt is ok).

This is the predefined set:

\definefontfeature
 [semitic-complete]
 [mode=node,analyze=yes,language=dflt,ccmp=yes,
  autoscript=position,autolanguage=position,
  init=yes,medi=yes,fina=yes,isol=yes,
  mark=yes,mkmk=yes,kern=yes,curs=yes,
  liga=yes,dlig=yes,rlig=yes,clig=yes,calt=yes]

\definefontfeature
 [semitic-simple]
 [mode=node,analyze=yes,language=dflt,ccmp=yes,
  autoscript=position,autolanguage=position,
  init=yes,medi=yes,fina=yes,isol=yes,
  mark=yes,mkmk=yes,kern=yes,curs=yes,
  rlig=yes,calt=yes]

\definefontfeature
 [arabic]
 [semitic-complete]
 [script=arab]

\definefontfeature
 [syriac]
 [arabic]
 [fin2=yes,fin3=yes,med2=yes]

\definefontfeature
 [hebrew]
 [semitic-complete]
 [script=hebr]

\definefontfeature
 [simplearabic]
 [semitic-simple]
 [script=arab]

\definefontfeature
 [simplehebrew]
 [semitic-simple]
 [script=hebr]