Merge from gnulib
[emacs.git] / lisp / language / lao-util.el
blob3bd0394024b3d2c29ecf61fec3f68931f1a5ed88
1 ;;; lao-util.el --- utilities for Lao -*- coding: utf-8; -*-
3 ;; Copyright (C) 2001-2015 Free Software Foundation, Inc.
4 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 ;; 2007, 2008, 2009, 2010, 2011
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
8 ;; Copyright (C) 2003
9 ;; National Institute of Advanced Industrial Science and Technology (AIST)
10 ;; Registration Number H13PRO009
12 ;; Keywords: multilingual, Lao, i18n
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;;; Commentary:
31 ;;; Code:
33 ;; Setting information of Thai characters.
35 (defconst lao-category-table (make-category-table))
36 (define-category ?c "Lao consonant" lao-category-table)
37 (define-category ?s "Lao semi-vowel" lao-category-table)
38 (define-category ?v "Lao upper/lower vowel" lao-category-table)
39 (define-category ?t "Lao tone" lao-category-table)
41 (let ((l '((?ກ consonant "LETTER KOR KAI'" "CHICKEN")
42 (?ຂ consonant "LETTER KHOR KHAI'" "EGG")
43 (?຃ invalid nil)
44 (?ຄ consonant "LETTER QHOR QHWARGN" "BUFFALO")
45 (?຅ invalid nil)
46 (? invalid nil)
47 (?ງ consonant "LETTER NGOR NGUU" "SNAKE")
48 (?ຈ consonant "LETTER JOR JUA" "BUDDHIST NOVICE")
49 (?ຉ invalid nil)
50 (?ຊ consonant "LETTER XOR X\"ARNG" "ELEPHANT")
51 (?຋ invalid nil)
52 (?ຌ invalid nil)
53 (?ຍ consonant "LETTER YOR YUNG" "MOSQUITO")
54 (?ຎ invalid nil)
55 (?ຎ invalid nil)
56 (?ຎ invalid nil)
57 (?ຎ invalid nil)
58 (?ຎ invalid nil)
59 (?ຎ invalid nil)
60 (?ດ consonant "LETTER DOR DANG" "NOSE")
61 (?ຕ consonant "LETTER TOR TAR" "EYE")
62 (?ຖ consonant "LETTER THOR THUNG" "TO ASK,QUESTION")
63 (?ທ consonant "LETTER DHOR DHARM" "FLAG")
64 (?ຘ invalid nil)
65 (?ນ consonant "LETTER NOR NOK" "BIRD")
66 (?ບ consonant "LETTER BOR BED" "FISHHOOK")
67 (?ປ consonant "LETTER POR PAR" "FISH")
68 (?ຜ consonant "LETTER HPOR HPER\"" "BEE")
69 (?ຝ consonant "LETTER FHOR FHAR" "WALL")
70 (?ພ consonant "LETTER PHOR PHUU" "MOUNTAIN")
71 (?ຟ consonant "LETTER FOR FAI" "FIRE")
72 (?ຠ invalid nil)
73 (?ມ consonant "LETTER MOR MAR\"" "HORSE")
74 (?ຢ consonant "LETTER GNOR GNAR" "MEDICINE")
75 (?ຣ consonant "LETTER ROR ROD" "CAR")
76 (?຤ invalid nil)
77 (?ລ consonant "LETTER LOR LIING" "MONKEY")
78 (?຦ invalid nil)
79 (?ວ consonant "LETTER WOR WII" "HAND FAN")
80 (?ຨ invalid nil)
81 (?ຩ invalid nil)
82 (?ສ consonant "LETTER SOR SEA" "TIGER")
83 (?ຫ consonant "LETTER HHOR HHAI" "JAR")
84 (?ຬ invalid nil)
85 (?ອ consonant "LETTER OR OOW" "TAKE")
86 (?ຮ consonant "LETTER HOR HEA" "BOAT")
87 (?ຯ special "ELLIPSIS")
88 (?ະ vowel-base "VOWEL SIGN SARA A")
89 (?ັ vowel-upper "VOWEL SIGN MAI KAN")
90 (?າ vowel-base "VOWEL SIGN SARA AR")
91 (?ຳ vowel-base "VOWEL SIGN SARA AM")
92 (?ິ vowel-upper "VOWEL SIGN SARA I")
93 (?ີ vowel-upper "VOWEL SIGN SARA II")
94 (?ຶ vowel-upper "VOWEL SIGN SARA EU")
95 (?ື vowel-upper "VOWEL SIGN SARA UR")
96 (?ຸ vowel-lower "VOWEL SIGN SARA U")
97 (?ູ vowel-lower "VOWEL SIGN SARA UU")
98 (?຺ invalid nil)
99 (?ົ vowel-upper "VOWEL SIGN MAI KONG")
100 (?ຼ semivowel-lower "SEMIVOWEL SIGN LO")
101 (?ຽ vowel-base "SEMIVOWEL SIGN SARA IA")
102 (?຾ invalid nil)
103 (?຿ invalid nil)
104 (?ເ vowel-base "VOWEL SIGN SARA EE")
105 (?ແ vowel-base "VOWEL SIGN SARA AA")
106 (?ໂ vowel-base "VOWEL SIGN SARA OO")
107 (?ໃ vowel-base "VOWEL SIGN SARA EI MAI MUAN\"")
108 (?ໄ vowel-base "VOWEL SIGN SARA AI MAI MAY")
109 (?໅ invalid nil)
110 (?ໆ special "KO LA (REPETITION)")
111 (?໇ invalid nil)
112 (?່ tone "TONE MAI EK")
113 (?້ tone "TONE MAI THO")
114 (?໊ tone "TONE MAI TI")
115 (?໋ tone "TONE MAI JADTAWAR")
116 (?໌ tone "CANCELLATION MARK")
117 (?ໍ vowel-upper "VOWEL SIGN SARA OR")
118 (?໎ invalid nil)
119 (?໏ invalid nil)
120 (?໐ special "DIGIT ZERO")
121 (?໑ special "DIGIT ONE")
122 (?໒ special "DIGIT TWO")
123 (?໓ special "DIGIT THREE")
124 (?໔ special "DIGIT FOUR")
125 (?໕ special "DIGIT FIVE")
126 (?໖ special "DIGIT SIX")
127 (?໗ special "DIGIT SEVEN")
128 (?໘ special "DIGIT EIGHT")
129 (?໙ special "DIGIT NINE")
130 (?໚ invalid nil)
131 (?໛ invalid nil)
132 (?ໜ consonant "LETTER NHOR NHUU" "MOUSE")
133 (?ໝ consonant "LETTER MHOR MHAR" "DOG")
134 (?ໞ invalid nil)))
135 elm)
136 (while l
137 (setq elm (car l) l (cdr l))
138 (let ((char (car elm))
139 (ptype (nth 1 elm)))
140 (cond ((eq ptype 'consonant)
141 (modify-category-entry char ?c lao-category-table))
142 ((memq ptype '(vowel-upper vowel-lower))
143 (modify-category-entry char ?v lao-category-table))
144 ((eq ptype 'semivowel-lower)
145 (modify-category-entry char ?s lao-category-table))
146 ((eq ptype 'tone)
147 (modify-category-entry char ?t lao-category-table)))
148 (put-char-code-property char 'phonetic-type ptype)
149 (put-char-code-property char 'name (nth 2 elm))
150 (put-char-code-property char 'meaning (nth 3 elm)))))
152 ;; The general composing rules are as follows:
154 ;; T
155 ;; V T V T
156 ;; CV -> C, CT -> C, CVT -> C, Cv -> C, CvT -> C
157 ;; v v
158 ;; T
159 ;; V T V T
160 ;; CsV -> C, CsT -> C, CsVT -> C, Csv -> C, CsvT -> C
161 ;; s s s s s
162 ;; v v
165 ;; where C: consonant, V: vowel upper, v: vowel lower,
166 ;; T: tone mark, s: semivowel lower
168 (defvar lao-composition-pattern
169 "\\cc\\(\\ct\\|\\cv\\ct?\\|\\cs\\(\\ct\\|\\cv\\ct?\\)?\\)"
170 "Regular expression matching a Lao composite sequence.")
172 ;;;###autoload
173 (defun lao-compose-string (str)
174 (with-category-table lao-category-table
175 (let ((idx 0))
176 (while (setq idx (string-match lao-composition-pattern str idx))
177 (compose-string str idx (match-end 0))
178 (setq idx (match-end 0))))
179 str))
181 ;;; LRT: Lao <-> Roman Transcription
183 ;; Upper vowels and tone-marks are put on the letter.
184 ;; Semi-vowel-sign-lo and lower vowels are put under the letter.
186 (defconst lao-transcription-consonant-alist
187 (sort '(;; single consonants
188 ("k" . "ກ")
189 ("kh" . "ຂ")
190 ("qh" . "ຄ")
191 ("ng" . "ງ")
192 ("j" . "ຈ")
193 ("s" . "ສ")
194 ("x" . "ຊ")
195 ("y" . "ຍ")
196 ("d" . "ດ")
197 ("t" . "ຕ")
198 ("th" . "ຖ")
199 ("dh" . "ທ")
200 ("n" . "ນ")
201 ("b" . "ບ")
202 ("p" . "ປ")
203 ("hp" . "ຜ")
204 ("fh" . "ຝ")
205 ("ph" . "ພ")
206 ("f" . "ຟ")
207 ("m" . "ມ")
208 ("gn" . "ຢ")
209 ("l" . "ລ")
210 ("r" . "ຣ")
211 ("v" . "ວ")
212 ("w" . "ວ")
213 ("hh" . "ຫ")
214 ("O" . "ອ")
215 ("h" . "ຮ")
216 ("nh" . "ໜ")
217 ("mh" . "ໝ")
218 ("lh" . ["ຫຼ"])
219 ;; double consonants
220 ("ngh" . ["ຫງ"])
221 ("yh" . ["ຫຽ"])
222 ("wh" . ["ຫວ"])
223 ("hl" . ["ຫລ"])
224 ("hy" . ["ຫຍ"])
225 ("hn" . ["ຫນ"])
226 ("hm" . ["ຫມ"])
228 (function (lambda (x y) (> (length (car x)) (length (car y)))))))
230 (defconst lao-transcription-semi-vowel-alist
231 '(("r" . "ຼ")))
233 (defconst lao-transcription-vowel-alist
234 (sort '(("a" . "ະ")
235 ("ar" . "າ")
236 ("i" . "ິ")
237 ("ii" . "ີ")
238 ("eu" . "ຶ")
239 ("ur" . "ື")
240 ("u" . "ຸ")
241 ("uu" . "ູ")
242 ("e" . ["ເະ"])
243 ("ee" . "ເ")
244 ("ae" . ["ແະ"])
245 ("aa" . "ແ")
246 ("o" . ["ໂະ"])
247 ("oo" . "ໂ")
248 ("oe" . ["ເາະ"])
249 ("or" . "ໍ")
250 ("er" . ["ເິ"])
251 ("ir" . ["ເີ"])
252 ("ua" . ["ົວະ"])
253 ("uaa" . ["ົວ"])
254 ("ie" . ["ເັຽະ"])
255 ("ia" . ["ເັຽ"])
256 ("ea" . ["ເຶອ"])
257 ("eaa" . ["ເືອ"])
258 ("ai" . "ໄ")
259 ("ei" . "ໃ")
260 ("ao" . ["ເົາ"])
261 ("aM" . "ຳ"))
262 (function (lambda (x y) (> (length (car x)) (length (car y)))))))
264 ;; Maa-sakod is put at the tail.
265 (defconst lao-transcription-maa-sakod-alist
266 '(("k" . "ກ")
267 ("g" . "ງ")
268 ("y" . "ຍ")
269 ("d" . "ດ")
270 ("n" . "ນ")
271 ("b" . "ບ")
272 ("m" . "ມ")
273 ("v" . "ວ")
274 ("w" . "ວ")
277 (defconst lao-transcription-tone-alist
278 '(("'" . "່")
279 ("\"" . "້")
280 ("^" . "໊")
281 ("+" . "໋")
282 ("~" . "໌")))
284 (defconst lao-transcription-punctuation-alist
285 '(("\\0" . "໐")
286 ("\\1" . "໑")
287 ("\\2" . "໒")
288 ("\\3" . "໓")
289 ("\\4" . "໔")
290 ("\\5" . "໕")
291 ("\\6" . "໖")
292 ("\\7" . "໗")
293 ("\\8" . "໘")
294 ("\\9" . "໙")
295 ("\\\\" . "ໆ")
296 ("\\$" . "ຯ")))
298 (defconst lao-transcription-pattern
299 (concat
300 "\\("
301 (mapconcat 'car lao-transcription-consonant-alist "\\|")
302 "\\)\\("
303 (mapconcat 'car lao-transcription-semi-vowel-alist "\\|")
304 "\\)?\\(\\("
305 (mapconcat 'car lao-transcription-vowel-alist "\\|")
306 "\\)\\("
307 (mapconcat 'car lao-transcription-maa-sakod-alist "\\|")
308 "\\)?\\("
309 (mapconcat (lambda (x) (regexp-quote (car x)))
310 lao-transcription-tone-alist "\\|")
311 "\\)?\\)?\\|"
312 (mapconcat (lambda (x) (regexp-quote (car x)))
313 lao-transcription-punctuation-alist "\\|")
315 "Regexp of Roman transcription pattern for one Lao syllable.")
317 (defconst lao-transcription-pattern
318 (concat
319 "\\("
320 (regexp-opt (mapcar 'car lao-transcription-consonant-alist))
321 "\\)\\("
322 (regexp-opt (mapcar 'car lao-transcription-semi-vowel-alist))
323 "\\)?\\(\\("
324 (regexp-opt (mapcar 'car lao-transcription-vowel-alist))
325 "\\)\\("
326 (regexp-opt (mapcar 'car lao-transcription-maa-sakod-alist))
327 "\\)?\\("
328 (regexp-opt (mapcar 'car lao-transcription-tone-alist))
329 "\\)?\\)?\\|"
330 (regexp-opt (mapcar 'car lao-transcription-punctuation-alist))
332 "Regexp of Roman transcription pattern for one Lao syllable.")
334 (defconst lao-vowel-reordering-rule
335 '(("ະ" (0 ?ະ) (0 ?ັ))
336 ("າ" (0 ?າ))
337 ("ິ" (0 ?ີ))
338 ("ີ" (0 ?ີ))
339 ("ຶ" (0 ?ຶ))
340 ("ື" (0 ?ື))
341 ("ຸ" (0 ?ຸ))
342 ("ູ" (0 ?ູ))
343 ("ເະ" (?ເ 0 ?ະ) (?ເ 0 ?ັ))
344 ("ເ" (?ເ 0))
345 ("ແະ" (?ແ 0 ?ະ) (?ແ 0 ?ັ))
346 ("ແ" (?ແ 0))
347 ("ໂະ" (?ໂ 0 ?ະ) (0 ?ົ) (?ຍ ?ໂ 0 ?ັ) (?ວ ?ໂ 0 ?ັ))
348 ("ໂ" (?ໂ 0))
349 ("ເາະ" (?ເ 0 ?າ ?ະ) (0 ?ັ ?ອ))
350 ("ໍ" (0 ?ໍ) (0 ?ອ))
351 ("ເິ" (?ເ 0 ?ິ))
352 ("ເີ" (?ເ 0 ?ີ))
353 ("ົວະ" (0 ?ົ ?ວ ?ະ) (0 ?ັ ?ວ))
354 ("ົວ" (0 ?ົ ?ວ) (0 ?ວ))
355 ("ເັຽະ" (?ເ 0 ?ັ ?ຽ ?ະ) (0 ?ັ ?ຽ))
356 ("ເັຽ" (?ເ 0 ?ັ ?ຽ) (0 ?ຽ))
357 ("ເຶອ" (?ເ 0 ?ຶ ?ອ))
358 ("ເືອ" (?ເ 0 ?ື ?ອ))
359 ("ໄ" (?ໄ 0))
360 ("ໃ" (?ໃ 0))
361 ("ເົາ" (?ເ 0 ?ົ ?າ))
362 ("ຳ" (0 ?ຳ)))
363 "Alist of Lao vowel string vs the corresponding re-ordering rule.
364 Each element has this form:
365 (VOWEL NO-MAA-SAKOD-RULE WITH-MAA-SAKOD-RULE (MAA-SAKOD-0 RULE-0) ...)
367 VOWEL is a vowel string (e.g. \"ເັຽະ\").
369 NO-MAA-SAKOD-RULE is a rule to re-order and modify VOWEL following a
370 consonant. It is a list vowel characters or 0. The element 0
371 indicate the place to embed a consonant.
373 Optional WITH-MAA-SAKOD-RULE is a rule to re-order and modify VOWEL
374 following a consonant and preceding a maa-sakod character. If it is
375 nil, NO-MAA-SAKOD-RULE is used. The maa-sakod character is always
376 appended at the tail.
378 For instance, rule `(\"ເືອ\" (?ເ t ?ື ?ອ))' tells that this vowel
379 string following a consonant `ກ' should be re-ordered as \"ເກືອ\".
381 Optional (MAA-SAKOD-n RULE-n) are rules specially applied to maa-sakod
382 character MAA-SAKOD-n.")
384 ;;;###autoload
385 (defun lao-transcribe-single-roman-syllable-to-lao (from to &optional str)
386 "Transcribe a Romanized Lao syllable in the region FROM and TO to Lao string.
387 Only the first syllable is transcribed.
388 The value has the form: (START END LAO-STRING), where
389 START and END are the beginning and end positions of the Roman Lao syllable,
390 LAO-STRING is the Lao character transcription of it.
392 Optional 3rd arg STR, if non-nil, is a string to search for Roman Lao
393 syllable. In that case, FROM and TO are indexes to STR."
394 (if str
395 (if (setq from (string-match lao-transcription-pattern str from))
396 (progn
397 (if (>= from to)
398 (setq from nil)
399 (setq to (match-end 0)))))
400 (save-excursion
401 (goto-char from)
402 (if (setq to (re-search-forward lao-transcription-pattern to t))
403 (setq from (match-beginning 0))
404 (setq from nil))))
405 (if from
406 (let* ((consonant (match-string 1 str))
407 (semivowel (match-string 3 str))
408 (vowel (match-string 5 str))
409 (maa-sakod (match-string 8 str))
410 (tone (match-string 9 str))
411 lao-consonant lao-semivowel lao-vowel lao-maa-sakod lao-tone
412 clen cidx)
413 (setq to (match-end 0))
414 (if (not consonant)
415 (setq str (cdr (assoc (match-string 0 str)
416 lao-transcription-punctuation-alist)))
417 (setq lao-consonant
418 (cdr (assoc consonant lao-transcription-consonant-alist)))
419 (if (vectorp lao-consonant)
420 (setq lao-consonant (aref lao-consonant 0)))
421 (setq clen (length lao-consonant))
422 (if semivowel
423 ;; Include semivowel in STR.
424 (setq lao-semivowel
425 (cdr (assoc semivowel lao-transcription-semi-vowel-alist))
426 str (if (= clen 1)
427 (concat lao-consonant lao-semivowel)
428 (concat (substring lao-consonant 0 1) lao-semivowel
429 (substring lao-consonant 1))))
430 (setq str lao-consonant))
431 (if vowel
432 (let (rule)
433 (setq lao-vowel
434 (cdr (assoc vowel lao-transcription-vowel-alist)))
435 (if (vectorp lao-vowel)
436 (setq lao-vowel (aref lao-vowel 0)))
437 (setq rule (assoc lao-vowel lao-vowel-reordering-rule))
438 (if (null maa-sakod)
439 (setq rule (nth 1 rule))
440 (setq lao-maa-sakod
441 (cdr (assoc maa-sakod lao-transcription-maa-sakod-alist))
442 rule
443 (or (cdr (assq (aref lao-maa-sakod 0) (nthcdr 2 rule)))
444 (nth 2 rule)
445 (nth 1 rule))))
446 (or rule
447 (error "Lao vowel %S has no re-ordering rule" lao-vowel))
448 (setq lao-consonant str str "")
449 (while rule
450 (if (= (car rule) 0)
451 (setq str (concat str lao-consonant)
452 cidx (length str))
453 (setq str (concat str (list (car rule)))))
454 (setq rule (cdr rule)))
455 (or cidx
456 (error "Lao vowel %S has malformed re-ordering rule" vowel))
457 ;; Set CIDX to after upper or lower vowel if any.
458 (let ((len (length str)))
459 (while (and (< cidx len)
460 (memq (get-char-code-property (aref str cidx)
461 'phonetic-type)
462 '(vowel-lower vowel-upper)))
463 (setq cidx (1+ cidx))))
464 (if lao-maa-sakod
465 (setq str (concat str lao-maa-sakod)))
466 (if tone
467 (setq lao-tone
468 (cdr (assoc tone lao-transcription-tone-alist))
469 str (concat (substring str 0 cidx) lao-tone
470 (substring str cidx)))))))
471 (list from to (lao-compose-string str)))))
473 ;;;###autoload
474 (defun lao-transcribe-roman-to-lao-string (str)
475 "Transcribe Romanized Lao string STR to Lao character string."
476 (let ((from 0)
477 (to (length str))
478 (lao-str "")
479 val)
480 (while (setq val (lao-transcribe-single-roman-syllable-to-lao from to str))
481 (let ((start (car val))
482 (end (nth 1 val))
483 (lao (nth 2 val)))
484 (if (> start from)
485 (setq lao-str (concat lao-str (substring str from start) lao))
486 (setq lao-str (concat lao-str lao)))
487 (setq from end)))
488 (if (< from to)
489 (concat lao-str (substring str from to))
490 lao-str)))
492 ;;;###autoload
493 (defun lao-composition-function (gstring)
494 (if (= (lgstring-char-len gstring) 1)
495 (compose-gstring-for-graphic gstring)
496 (or (font-shape-gstring gstring)
497 (let ((glyph-len (lgstring-glyph-len gstring))
498 (i 0)
499 glyph)
500 (while (and (< i glyph-len)
501 (setq glyph (lgstring-glyph gstring i)))
502 (setq i (1+ i)))
503 (compose-glyph-string-relative gstring 0 i 0.1)))))
505 ;;;###autoload
506 (defun lao-compose-region (from to)
507 (interactive "r")
508 (save-restriction
509 (narrow-to-region from to)
510 (goto-char (point-min))
511 (with-category-table lao-category-table
512 (while (re-search-forward lao-composition-pattern nil t)
513 (compose-region (match-beginning 0) (point))))))
516 (provide 'lao-util)
518 ;;; lao-util.el ends here