reutenauer / polyglossia

An alternative to Babel for XeLaTeX and LuaLaTeX
http://www.ctan.org/pkg/polyglossia
MIT License
185 stars 52 forks source link

Different fonts for language variants #636

Open pcdi opened 2 months ago

pcdi commented 2 months ago

Simplified and traditional Chinese characters are different in detail, even though they often share the same basic glyphs (see Han Unification). Therefore, I would like to be able to select two different fonts that reflect these graphical differences between simplified and traditional characters, even if used within the same document. Currently, simplified and traditional are treated as different variants of the language chinese. However, because it is only possible to define a font for a certain language (or script, for that matter) and not for a language variant, it is not possible to use dedicated fonts for both simplified and traditional characters at the same time.

I tried going through language aliases, as follows, which does not work, as \chinesefont never gets defined.

\setotherlanguage{chinese}

\setlanguagealias[variant=simplified]{chinese}{simplified}
\setlanguagealias[variant=traditional]{chinese}{traditional}

\newfontfamily\simplifiedfont[Script=CJK,Language=Chinese Simplified]{Source Han Sans SC}
\newfontfamily\traditionalfont[Script=CJK,Language=Chinese Traditional]{Source Han Sans TC}

If you have a font that is able to switch glyphs based on the language it is invoked with (Such as the Source Han Sans/Serif series), then it is possible to get the correct glyph shapes by adding the language as a fontfeature, but this makes it extremely dependent on the font used and totally different fonts for simplified/traditional characters are not possible at all.

\documentclass{article}

\usepackage{polyglossia}

\newcommand*{\teststring}{%
    傑僭割劘匾叟喝塌姿嬴幰廋扇扉搨摩榻溲潛瀛瘦瞎磨窖竇箭篠簉糙綢纛羸翁翦%
}

\setdefaultlanguage{english}

\setotherlanguage{chinese}
\newfontfamily\chinesefont[Script=CJK]{Source Han Sans}

\begin{document}

\begin{chinese}
    {\addfontfeature{Language=Chinese Simplified}%
    \teststring\par}
\end{chinese}

\begin{chinese}
    {\addfontfeature{Language=Chinese Traditional}%
    \teststring\par}
\end{chinese}

\end{document}

addfontfeature

jspitz commented 2 months ago

It's not possible at the moment. Since the fontspec script tag is ambiguous in this case (CJK in both cases), one way is to check additionally for bcp47-script. Then you could set:

\newfontfamily\hansfont[Script=CJK,Language=Chinese Simplified]{Source Han Sans SC}
\newfontfamily\hantfont[Script=CJK,Language=Chinese Traditional]{Source Han Sans TC}
jspitz commented 2 months ago

I have implemented this in master. If you can test the development version, that would be great (note, though, that due to our major code rewrite, you would need to download the whole tex directory).

jspitz commented 2 months ago

@Udi-Fogiel I am getting an automated test run failure after this commit, but I cannot make sense of it.

Udi-Fogiel commented 2 months ago

It's not possible at the moment. Since the fontspec script tag is ambiguous in this case (CJK in both cases), one way is to check additionally for bcp47-script.

Maybe we can add an option for the user to decide on the name of the font switch command associated with a language/variant/alias, it seems more general.

@Udi-Fogiel I am getting an automated test run failure after this commit, but I cannot make sense of it.

This is due a change in the format of messages from fontspec, see: https://github.com/latex3/fontspec/commit/844c653ad9326bda004d2ad0e36470141b33cc07 You can run the tests locally by l3build check (for more options see l3build's documentation), and if there are failures, there should be .diff files in build/test showing the changes.

I'll update the tests in the following hour.

jspitz commented 2 months ago

Maybe we can add an option for the user to decide on the name of the font switch command associated with a language/variant/alias, it seems more general.

At the moment I do not see the need for that.

I'll update the tests in the following hour.

Thanks!

Udi-Fogiel commented 1 month ago

Maybe we can add an option for the user to decide on the name of the font switch command associated with a language/variant/alias, it seems more general.

At the moment I do not see the need for that.

Well, this ticket was about different fonts for different variants, and it is not really possible right now...