Explainer
How a text gets from Unicode to Romenagri and back, and what the nukta layer adds. This page summarises Romenagri/TRANSLITERATION.md, which has the full definition, the grammar of the parser and the measurements. A table or an output on this page was read from the headers, or written by the compiled programs, when the site was built.
The three layers
- Unicode: the Devanagari block, U+0900 to U+097F. The converters read and write UTF-16LE; files are UTF-8 elsewhere.
- ACII: an 8-bit encoding. The upper half (
80toFF) follows ISCII-91's layout for Devanagari, with extensions of this project; the lower half is ASCII. - Romenagri: the ASCII transliteration of ACII text, made of bare letters, digits and punctuation. This is the text that programs,
sedscripts and Hindawi's language styles see.
Unicode text (UTF-8 in files)
| iconv -f utf8 -t utf16
v
UTF-16LE
| uni2acii table: unicode.h, nukta.h
v
ACII bytes
| acii2rmn table: acii.h (the decoder)
v
Romenagri: ASCII letters, digits, punctuation
| rmn2acii table: acii.h (the parser)
v
ACII bytes
| acii2uni table: unicode.h, nukta.h
v
UTF-16LE with a byte-order mark -> iconv -f utf16 -t utf8
| Program | Reads, writes | Table |
|---|---|---|
uni2acii | UTF-16LE, ACII bytes | unicode.h, nukta.h |
acii2uni | ACII bytes, UTF-16LE with a byte-order mark (a line feed becomes CR LF) | unicode.h, nukta.h |
acii2rmn | ACII, Romenagri (a filter: Latin words are wrapped as "e_word") | acii.h |
rmn2acii | Romenagri, ACII (a filter: it unwraps those and hands every run of [A-Za-z_] to the parser) | acii.h |
acii2hin | ACII, “saral” Latin: one way and lossy by design | aciihin.h |
fixuninum | UTF-8, UTF-8 with the digits of the ten Indic scripts rewritten to ASCII digits | none |
acii2cf and acii2pcf are the filters for program text: Latin words and string and character literals are left as they are. Hindawi's own h2c and c2h, which sit in such pipelines, are not part of Romenagri.
The tables
unicode.hhas 85 rows{low, high, byte}: the code point islow + 256 * high.uni2aciiwrites the byte of the first row with the code point, andacii2unithe code point of the first row with the byte.acii.hhas 96 rows{key, bytes}, wherebytesis one to three bytes. The parser looks up the first row whose key is the one it wants; the decoder takes the first row whose first byte is the byte it reads. Row order is therefore part of the definition.aciihin.hhas 87 rows of the same shape, read only byacii2hin.
The layout of the upper half of ACII:
| Bytes | What |
|---|---|
| 80 | U+0979 (ZHA), an earlier extension of this project |
| 81 to 8C | the twelve Sanskrit and Vedic additions (Characters) |
| A1 to A3 | candrabindu, anusvara, visarga |
| A4 to B2 | the independent vowels |
| B3 to D8 | the consonants KA to HA |
| DA to E7 | the dependent vowel signs |
| E8, E9 | halant, nukta |
| EA, EB | danda; avagraha |
| EF | ISCII's attribute code (row 0 of acii.h: the decoder drops the byte that follows it) |
ACII to Romenagri: the decoder
acii2rmn is a stack machine. Its state is a stack of characters, and for each input byte b it does this:
r := the first row (table order) whose first byte is b
if there is no such row:
unless b follows the byte EF (attribute code): push the character b
otherwise, if r.key starts with '^' and the stack is not empty: -- a dependent sign, or the halant
pop one character -- the 'a' of the consonant before
if r.key starts with "^z": -- the nukta
t1 := pop one character
if t1 = 'h': t2 := pop one character
if t2 = 'd': push "_rdha" else: push t2, "hza"
elif t1 = 'd': push "_rda"
else: push t1, "za"
else: push r.key without its '^'
otherwise: push r.key
output the stack
- The ‘a’ protocol. Every consonant key ends in
a. A vowel sign, a nukta or a halant that follows pops thata:kaand sign I iski, andkaand a halant isk. - The nukta. A consonant and a nukta is the stem,
zanda:kza. Two stems have spellings of their own: DA and a nukta is_rda(ड़) and DHA and a nukta is_rdha(ढ़). A stem that ends inhkeeps itshin front of thez, as inkhza. - A dependent sign pops whatever is on top. The decoder does not check that the top is the
aof a consonant; ACII outside the grammar of the contract below is garbage in, garbage out, and a nukta on an almost empty stack crashes the program.
Romenagri to ACII: the parser
rmn2acii reads a string once, from the left, with no backtracking. In the C code it is five mutually recursive functions, level1, level2a, level2b, level2c and matra; every call is the last thing its branch does, so it is really a loop over five states, and the specification writes it down as a grammar of decision lists that a test program reads and runs.
- Level 1 (L1) is the start of a unit. A consonant that can stand alone (
n m y w x r) is written as its row and the parser goes to L2a; a consonant that may take an aspirate (g t d p b s l k jandc) goes to L2b;_opens a family (L2c);^opens a dependent sign (M). Anything else is copied to the output as it is, which is why a vowel always needs its_and whyhcan follow only a letter that takes it. - Level 2 binds what attaches to a consonant. L2a has written the consonant with its inherent vowel; if a new unit begins it appends a halant, and otherwise a vowel sign follows (M). L2b looks for the
hof an aspirate or thevoflvra; L2c reads the letter after_that selects a family; M writes the vowel signs. - Inherent vowels are always written.
kais क,kis क्,kiis कि andraamais राम. Schwa deletion is not supported, on purpose. - The order of the rules is part of the definition, and twelve guard rules stop the parser from jumping over an underscore or the end of the string.
The decision of the nukta layer, which the specification records as the one for cases C1 and C6, rests on this split: a consonant is recognised and its sound mapped at level 1, before level 2 binds a halant, an adjunct consonant or a vowel sign to it, so a letter with a nukta has to be a consonant and a nukta before level 2 sees it.
The round-trip contract
For every text in the set W of well-formed ACII, acii2rmn followed by rmn2acii returns the same bytes (with the filters' reading: the parser sees each run of [A-Za-z_] separately). At the Unicode end, every code point that unicode.h maps survives uni2acii and acii2uni. W is a text of syllables and spaces:
text = { syllable | SPACE } ;
syllable = body [ modifier ] ;
body = vowel [ accent ]
| AVAGRAHA | DOUBLE-DANDA | OM
| cluster ( HALANT | [ accent ] [ sign ] [ accent ] ) ;
cluster = consonant [ nukta ] { HALANT consonant [ nukta ] } ;
A syllable that ends in a halant is followed by a space or by the end of the text, and a nukta follows only one of the eleven consonants KA KHA GA JA DDA DDHA PHA YA NA RA LLA. Outside W things may work, and nothing is promised. The cases that are easy to meet:
- a halant followed directly by an independent vocalic vowel comes back as the vowel's sign;
- a vowel sign, a halant or a nukta where the grammar does not allow it (after a space, after a vowel, after another sign) pops whatever is on top of the decoder's stack;
- ISCII's explicit and soft halants,
E8 E8andE8 E9, are read as a halant after a halant and as a nukta after a halant; the second makesacii2rmncrash; - the danda: the decoder writes
|, and the filters pass|through as the ASCII character7C(Challenges); - ASCII letters, digits and punctuation are the lexers' business, not the translators'.
The nukta layer
ACII writes a letter with a nukta as the byte of the consonant and E9, which is U+093C. Unicode has eleven letters that are such a pair in one code point. The nukta layer is a third patch, to the two Unicode converters only: uni2acii and acii2uni. It has three parts.
- The rule is always applied and is not an option. A letter with a nukta is a consonant and a nukta, however it arrives: as one code point, as two, as the ACII bytes (
BF E9) or as Romenagri (_rda). Souni2aciiwrites a code point thatunicode.hhas no row for andnukta.hlists (U+0934 and the eight of U+0958 to U+095F) as the byte of its consonant and thenE9. The converters stand in front of the parser in every chain that starts with Unicode, so this happens where the text enters, before level 2 binds anything, andrmn2acii.cis not changed. - The form is the Unicode layer, and it belongs to the user. It says how the two converters write the family at the Unicode end, as one code point or as two.
asis, the default, is what the converters always wrote;nfcis Unicode's normalisation form C;nfdis form D. Unicode's normalisation is kept apart from the rule. - The warning is off by default.
uni2acii -w(orROMENAGRI_NUKTA_WARN=1) reports each code point that has no ACII byte and is written as its low byte, once, on stderr. The bytes written are the same with and without it.
What each form writes, from runs of the compiled converters:
| Program | In | asis (default) | nfc | nfd |
|---|---|---|---|---|
acii2uni | C7 | ऩ U+0929 | ऩ U+0929 | ऩ U+0928 U+093C |
acii2uni | D0 | ऱ U+0931 | ऱ U+0931 | ऱ U+0930 U+093C |
acii2uni | C6 E9 | ऩ U+0928 U+093C | ऩ U+0929 | ऩ U+0928 U+093C |
acii2uni | CF E9 | ऱ U+0930 U+093C | ऱ U+0931 | ऱ U+0930 U+093C |
acii2uni | D2 E9 | ऴ U+0933 U+093C | ऴ U+0934 | ऴ U+0933 U+093C |
acii2uni | BF E9 | ड़ U+0921 U+093C | ड़ U+0921 U+093C | ड़ U+0921 U+093C |
uni2acii | ऩ U+0929 | C7 | C7 | C6 E9 |
uni2acii | ऱ U+0931 | D0 | D0 | CF E9 |
uni2acii | न and a nukta U+0928 U+093C | C6 E9 | C7 | C6 E9 |
uni2acii | र and a nukta U+0930 U+093C | CF E9 | D0 | CF E9 |
uni2acii | ऴ U+0934 | D2 E9 | D2 E9 | D2 E9 |
uni2acii | ड़ U+095C | BF E9 | BF E9 | BF E9 |
uni2acii | ड and a nukta U+0921 U+093C | BF E9 | BF E9 | BF E9 |
No form writes the eight letters of U+0958 to U+095F as single code points, because any normaliser would split them again. Unicode calls them composition exclusions. Only U+0929, U+0931 and U+0934 are composed by NFC.
Choosing the form
The form can be chosen at four levels. Where more than one is given, the later row wins.
| Level | How | Example |
|---|---|---|
| the build | the macro NUKTA_FORM, given when a converter is compiled | -DNUKTA_FORM=NUKTA_NFC |
| the environment | the variable ROMENAGRI_NUKTA_FORM | ROMENAGRI_NUKTA_FORM=nfc uni2acii |
| the command line | -f FORM or --form=FORM, on both converters | acii2uni -f nfd in.acii out.utf16 |
| a function call | a program that includes nukta.h calls nukta_set_form ("nfc"), or sets nukta_form | if (nukta_set_form (argv[1]) < 0) ... |
A name that is not a form is an error: a message on stderr, exit status 1, and nothing is converted. The two converters should be given the same form, or a letter changes spelling on its way through.
A worked example
The cluster ड़्व typed with the single code point (U+095C DEVANAGARI LETTER DDDHA, U+094D VIRAMA, U+0935 VA):
uni2aciiwrites BF E9 E8 D4. U+095C has no row inunicode.h, so the rule writes the consonant,BF, and the nukta,E9.acii2rmnreads those bytes with its stack (the table shows the stack after each byte) and writes_rdwa.rmn2aciiparses_rdwaand writes BF E9 E8 D4, the same bytes.acii2uniwrites, in each form:- form
asis: ड़्वU+0921 U+093C U+094D U+0935 - form
nfc: ड़्वU+0921 U+093C U+094D U+0935 - form
nfd: ड़्वU+0921 U+093C U+094D U+0935
- form
| Byte | Row of acii.h | Stack after it |
|---|---|---|
| BF | da | da |
| E9 | ^za | _rda |
| E8 | ^ | _rd |
| D4 | wa | _rdwa |
Where the layer stops
- It does not reorder. NFC and NFD move a nukta in front of a halant or an accent. The converters never reorder anything, and the layer does not start: U+0928 U+094D U+093C goes through
uni2aciiandacii2unias it is, in every form. - The round trip is exact only up to canonical equivalence. ड़ goes in and U+0921 U+093C comes out, in every form. The two look the same and are the same text for Unicode, but a byte comparison does not find the other.
- Romenagri is not extended. The decoder reads the pair as before, and
acii2rmnstill crashes on a halant followed by a nukta. - The low-byte problem remains for everything else. U+00E9 (an e with an acute accent) is written as
E9, which is the nukta. The warning layer makes it visible; the cure is to deal with characters that are not in the tables beforeuni2acii. - The joiners are not handled. ZWJ and ZWNJ still fall back to their low bytes.