% Plain format adapted to the Russian language in the UTF-8 encoding.
% Copyright 2023 Dmitri Pavlov.
% You may redistribute this file under the terms of GNU General Public License version 3.
% Report bugs and suggestions to me: host: math.berkeley.edu, user: pavlov.

% This macro package sets up the UTF-8 encoding.
% For single-byte Cyrillic encodings, use the macro package plain-ru.tex instead.

% This file uses the LH fonts by Olga Lapko and Andrei Hodulev.
% If you want to use some other fonts, simply change the lh prefix everywhere to the prefix of your font.

% Make sure to put a file called hyphen-ru.tex in the same directory as plain-ru-utf8.tex, in the same encoding as the fonts you are using.
% For LH fonts, the default encoding is CP866, which is only used for the fonts, not for your input file, which must be in UTF-8 for this macro package.

% For example, on Linux you can use the following command for the ruhyphal.tex hyphenation patterns with the LH fonts:
% iconv -f koi8-r -t cp866 <$(kpsewhich ruhyphal.tex) >hyphen-ru.tex

% Typical compilation command: initex '\input plain-ru-utf8 \dump'

% This macro package sets up proper codes (uc, lc, sf) and fonts (LH) for the Russian language.
% Unfortunately, the LH package does not have Russian mathematical fonts, so mathcodes are not set.

\catcode`\{=1
\catcode`\}=2
\let\xskewchar=\skewchar
\def\skewchar{\let\skewchar=\xskewchar\let\xskewchar=\undefined

\message{russian fonts,}
\font\tenrm=lhr10 % roman text
\font\preloaded=lhr9
\font\preloaded=lhr8
\font\sevenrm=lhr7
\font\preloaded=lhr6
\font\fiverm=lhr5

\font\preloaded=lhss10 % sans serif
\font\preloaded=lhssq8

\font\preloaded=lhssi10 % sans serif italic
\font\preloaded=lhssqi8

\font\tenbf=lhbx10 % boldface extended
\font\preloaded=lhbx9
\font\preloaded=lhbx8
\font\sevenbf=lhbx7
\font\preloaded=lhbx6
\font\fivebf=lhbx5

\font\tentt=lhtt10 % typewriter
\font\preloaded=lhtt9
\font\preloaded=lhtt8

\font\preloaded=lhsltt10 % slanted typewriter

\font\tensl=lhsl10 % slanted roman
\font\preloaded=lhsl9
\font\preloaded=lhsl8

\font\tenit=lhti10 % text italic
\font\preloaded=lhti9
\font\preloaded=lhti8
\font\preloaded=lhti7

\message{more russian fonts,}
\font\preloaded=lhu10 % unslanted text italic

\font\preloaded=lhcsc10 % caps and small caps

\font\preloaded=lhssbx10 % sans serif bold extended

\font\preloaded=lhdunh10 % Dunhill style

\font\preloaded=lhr7 scaled \magstep4 % for titles
\font\preloaded=lhtt10 scaled \magstep2
\font\preloaded=lhssbx10 scaled \magstep2

\let\preloaded=\undefined
\skewchar
}

\input plain

\message{russian hyphenation,}

\countdef\ch=253
\ch="80 \loop\ifnum\ch<"100 \lccode\ch=\ch \advance\ch1 \repeat % make everything lowercase letters for now to allow \patterns to work

\input hyphen-ru

\message{russian codes}

% Borrowed from plain-utf8.tex

% Setup catcodes for UTF-8 characters: legitimate initial octets are active, intermediate octets are letters, other octets are illegal.
\ch="80 \loop\ifnum\ch<"100 \lccode\ch=0 \uccode\ch=0 \advance\ch1 \repeat % for LaTeX
\ch="80 \loop\ifnum\ch<"C0 \catcode\ch=11 \advance\ch1 \repeat % make all intermediate UTF-8 octets (top 2 bits are 10) letters
\ch="C0 \loop\ifnum\ch<"100 \catcode\ch=15 \advance\ch1 \repeat % make all octets with the top 2 bits set illegal
\ch="C2 \loop\ifnum\ch<"F5 \catcode\ch=\active \advance\ch1 \repeat % make all legitimate initial UTF-8 octets active
\ch="C2 \loop\ifnum\ch<"E2 \uccode\ch=1 \advance\ch1 \repeat % make uccode=1 for all initial UTF-8 octets of letters, to allow macros to recognize Unicode letters
\let\ch\underfined

% \cdef\macro+ is like \def\macro{+}, but the catcode of + will be 11
\catcode0=12 \def\cdef#1#2{\begingroup\lccode0=`#2 \lowercase{\endgroup \def#1{^^@}}} \catcode0=9

% Preliminary macros for UTF-8: every legitimate UTF-8 sequence produces a control sequence whose name consists precisely of the octets in the sequence.
\catcode`@=\active
\def@#1{\cdef\chr#1 \edef#1##1{\noexpand\csname\chr##1\noexpand\endcsname}} % 2-byte
@^^c2@^^c3@^^c4@^^c5@^^c6@^^c7@^^c8@^^c9@^^ca@^^cb@^^cc@^^cd@^^ce@^^cf@^^d0@^^d1@^^d2@^^d3@^^d4@^^d5@^^d6@^^d7@^^d8@^^d9@^^da@^^db@^^dc@^^dd@^^de@^^df
\def@#1{\cdef\chr#1 \edef#1##1##2{\noexpand\csname\chr##1##2\noexpand\endcsname}} % 3-byte
@^^e0@^^e1@^^e2@^^e3@^^e4@^^e5@^^e6@^^e7@^^e8@^^e9@^^ea@^^eb@^^ec@^^ed@^^ee@^^ef
\def@#1{\cdef\chr#1 \edef#1##1##2##3{\noexpand\csname\chr##1##2##3\noexpand\endcsname}} % 4-byte
@^^f0@^^f1@^^f2@^^f3@^^f4
\let\chr\undefined
\let\cdef\undefined

% Macros for mapping Unicode code points to font characters
\def\set#1{\expandafter\setswitch#1\end}
\def\gobble#1{}
\def\setswitch{\futurelet\next\xsetswitch}
\def\xsetswitch{\ifx\next\csname\expandafter\setprocess\else\expandafter\gobble\fi}
\def\setprocess\csname#1\endcsname{\expandafter\chardef\csname#1\endcsname\count255 \advance\count255 1 \expandafter\setswitch}

% Map Cyrillic Unicode code points to LH font characters
\count255="80
\set{АБВГДЕЖЗИЙКЛМНОП}
\set{РСТУФХЦЧШЩЪЫЬЭЮЯ}
\set{абвгдежзийклмноп}

\count255="C4
\set{‒¤}

\count255="E0
\set{рстуфхцчшщъыьэюя}
\set{ЁёҐґЄєІіЇїЎў№«»„}

\count255=92

\let\set\undefined
\let\setswitch\undefined
\let\setprocess\undefined
\let\xsetprocess\undefined

% Generic macros for UTF-8: every legitimate UTF-8 sequence produces a control sequence whose name consists precisely of the octets in the sequence.
% An undefined control sequence produces an error message.
\let\xcsname=\csname
\let\xendcsname=\endcsname
\def@#1{\def#1##1{\expandafter\ifx\csname\string#1##1\endcsname\relax\errmessage{Undefined UTF-8 sequence \string#1##1}\else\xcsname\string#1##1\xendcsname\fi}}
@^^c2@^^c3@^^c4@^^c5@^^c6@^^c7@^^c8@^^c9@^^ca@^^cb@^^cc@^^cd@^^ce@^^cf@^^d0@^^d1@^^d2@^^d3@^^d4@^^d5@^^d6@^^d7@^^d8@^^d9@^^da@^^db@^^dc@^^dd@^^de@^^df
\def@#1{\def#1##1##2{\expandafter\ifx\csname\string#1##1##2\endcsname\relax\errmessage{Undefined UTF-8 sequence \string#1##1##2}\else\xcsname\string#1##1##2\xendcsname\fi}}
@^^e0@^^e1@^^e2@^^e3@^^e4@^^e5@^^e6@^^e7@^^e8@^^e9@^^ea@^^eb@^^ec@^^ed@^^ee@^^ef
\def@#1{\def#1##1##2##3{\expandafter\ifx\csname\string#1##1##2##3\endcsname\relax\errmessage{Undefined UTF-8 sequence \string#1##1##2##3}\else\xcsname\string#1##1##2##3\xendcsname\fi}}
@^^f0@^^f1@^^f2@^^f3@^^f4
\let@\undefined
\catcode`@=12

% Macro for setting lccode, uccode, and sfcode for letters in LH fonts
\def\setuls#1#2{\lccode#1=#2 \uccode#1=#1 \lccode#2=#2 \uccode#2=#1 \sfcode#1=999 \sfcode#2=1000 }

% Set lccode, uccode, and sfcode for all letters in LH fonts
\setuls{А}{а}
\setuls{Б}{б}
\setuls{В}{в}
\setuls{Г}{г}
\setuls{Д}{д}
\setuls{Е}{е}
\setuls{Ё}{ё}
\setuls{Ж}{ж}
\setuls{З}{з}
\setuls{И}{и}
\setuls{Й}{й}
\setuls{К}{к}
\setuls{Л}{л}
\setuls{М}{м}
\setuls{Н}{н}
\setuls{О}{о}
\setuls{П}{п}
\setuls{Р}{р}
\setuls{С}{с}
\setuls{Т}{т}
\setuls{У}{у}
\setuls{Ф}{ф}
\setuls{Х}{х}
\setuls{Ц}{ц}
\setuls{Ч}{ч}
\setuls{Ш}{ш}
\setuls{Щ}{щ}
\setuls{Ъ}{ъ}
\setuls{Ы}{ы}
\setuls{Ь}{ь}
\setuls{Э}{э}
\setuls{Ю}{ю}
\setuls{Я}{я}
\setuls{Ё}{ё}
\setuls{Ґ}{ґ}
\setuls{Є}{є}
\setuls{І}{і}
\setuls{Ї}{ї}
\setuls{Ў}{ў}

\let\setuls\undefined

\def\fmtname{russian-plain}
