1 ;;; lao-util.el --- utilities for Lao -*- coding: utf-8; -*-
3 ;; Copyright (C) 2001-2017 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
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 <https://www.gnu.org/licenses/>.
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
"CHICKEN")
44 (?ຄ consonant
"BUFFALO")
47 (?ງ consonant
"SNAKE")
48 (?ຈ consonant
"BUDDHIST NOVICE")
50 (?ຊ consonant
"ELEPHANT")
53 (?ຍ consonant
"MOSQUITO")
62 (?ຖ consonant
"TO ASK,QUESTION")
66 (?ບ consonant
"FISHHOOK")
70 (?ພ consonant
"MOUNTAIN")
73 (?ມ consonant
"HORSE")
74 (?ຢ consonant
"MEDICINE")
77 (?ລ consonant
"MONKEY")
79 (?ວ consonant
"HAND FAN")
82 (?ສ consonant
"TIGER")
132 (?ໜ consonant
"MOUSE")
137 (setq elm
(car l
) l
(cdr l
))
138 (let ((char (car 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
))
147 (modify-category-entry char ?t lao-category-table
)))
148 (put-char-code-property char
'phonetic-type ptype
)
149 (put-char-code-property char
'meaning
(nth 2 elm
)))))
151 ;; The general composing rules are as follows:
155 ;; CV -> C, CT -> C, CVT -> C, Cv -> C, CvT -> C
159 ;; CsV -> C, CsT -> C, CsVT -> C, Csv -> C, CsvT -> C
164 ;; where C: consonant, V: vowel upper, v: vowel lower,
165 ;; T: tone mark, s: semivowel lower
167 (defvar lao-composition-pattern
168 "\\cc\\(\\ct\\|\\cv\\ct?\\|\\cs\\(\\ct\\|\\cv\\ct?\\)?\\)"
169 "Regular expression matching a Lao composite sequence.")
172 (defun lao-compose-string (str)
173 (with-category-table lao-category-table
175 (while (setq idx
(string-match lao-composition-pattern str idx
))
176 (compose-string str idx
(match-end 0))
177 (setq idx
(match-end 0))))
180 ;;; LRT: Lao <-> Roman Transcription
182 ;; Upper vowels and tone-marks are put on the letter.
183 ;; Semi-vowel-sign-lo and lower vowels are put under the letter.
185 (defconst lao-transcription-consonant-alist
186 (sort '(;; single consonants
227 (function (lambda (x y
) (> (length (car x
)) (length (car y
)))))))
229 (defconst lao-transcription-semi-vowel-alist
232 (defconst lao-transcription-vowel-alist
261 (function (lambda (x y
) (> (length (car x
)) (length (car y
)))))))
263 ;; Maa-sakod is put at the tail.
264 (defconst lao-transcription-maa-sakod-alist
276 (defconst lao-transcription-tone-alist
283 (defconst lao-transcription-punctuation-alist
297 (defconst lao-transcription-pattern
300 (mapconcat 'car lao-transcription-consonant-alist
"\\|")
302 (mapconcat 'car lao-transcription-semi-vowel-alist
"\\|")
304 (mapconcat 'car lao-transcription-vowel-alist
"\\|")
306 (mapconcat 'car lao-transcription-maa-sakod-alist
"\\|")
308 (mapconcat (lambda (x) (regexp-quote (car x
)))
309 lao-transcription-tone-alist
"\\|")
311 (mapconcat (lambda (x) (regexp-quote (car x
)))
312 lao-transcription-punctuation-alist
"\\|")
314 "Regexp of Roman transcription pattern for one Lao syllable.")
316 (defconst lao-transcription-pattern
319 (regexp-opt (mapcar 'car lao-transcription-consonant-alist
))
321 (regexp-opt (mapcar 'car lao-transcription-semi-vowel-alist
))
323 (regexp-opt (mapcar 'car lao-transcription-vowel-alist
))
325 (regexp-opt (mapcar 'car lao-transcription-maa-sakod-alist
))
327 (regexp-opt (mapcar 'car lao-transcription-tone-alist
))
329 (regexp-opt (mapcar 'car lao-transcription-punctuation-alist
))
331 "Regexp of Roman transcription pattern for one Lao syllable.")
333 (defconst lao-vowel-reordering-rule
334 '(("ະ" (0 ?ະ
) (0 ?ັ
))
342 ("ເະ" (?ເ
0 ?ະ
) (?ເ
0 ?ັ
))
344 ("ແະ" (?ແ
0 ?ະ
) (?ແ
0 ?ັ
))
346 ("ໂະ" (?ໂ
0 ?ະ
) (0 ?ົ
) (?ຍ ?ໂ
0 ?ັ
) (?ວ ?ໂ
0 ?ັ
))
348 ("ເາະ" (?ເ
0 ?າ ?ະ
) (0 ?ັ ?ອ
))
352 ("ົວະ" (0 ?ົ ?ວ ?ະ
) (0 ?ັ ?ວ
))
353 ("ົວ" (0 ?ົ ?ວ
) (0 ?ວ
))
354 ("ເັຽະ" (?ເ
0 ?ັ ?ຽ ?ະ
) (0 ?ັ ?ຽ
))
355 ("ເັຽ" (?ເ
0 ?ັ ?ຽ
) (0 ?ຽ
))
362 "Alist of Lao vowel string vs the corresponding re-ordering rule.
363 Each element has this form:
364 (VOWEL NO-MAA-SAKOD-RULE WITH-MAA-SAKOD-RULE (MAA-SAKOD-0 RULE-0) ...)
366 VOWEL is a vowel string (e.g. \"ເັຽະ\").
368 NO-MAA-SAKOD-RULE is a rule to re-order and modify VOWEL following a
369 consonant. It is a list vowel characters or 0. The element 0
370 indicate the place to embed a consonant.
372 Optional WITH-MAA-SAKOD-RULE is a rule to re-order and modify VOWEL
373 following a consonant and preceding a maa-sakod character. If it is
374 nil, NO-MAA-SAKOD-RULE is used. The maa-sakod character is always
375 appended at the tail.
377 For instance, rule `(\"ເືອ\" (?ເ t ?ື ?ອ))' tells that this vowel
378 string following a consonant `ກ' should be re-ordered as \"ເກືອ\".
380 Optional (MAA-SAKOD-n RULE-n) are rules specially applied to maa-sakod
381 character MAA-SAKOD-n.")
384 (defun lao-transcribe-single-roman-syllable-to-lao (from to
&optional str
)
385 "Transcribe a Romanized Lao syllable in the region FROM and TO to Lao string.
386 Only the first syllable is transcribed.
387 The value has the form: (START END LAO-STRING), where
388 START and END are the beginning and end positions of the Roman Lao syllable,
389 LAO-STRING is the Lao character transcription of it.
391 Optional 3rd arg STR, if non-nil, is a string to search for Roman Lao
392 syllable. In that case, FROM and TO are indexes to STR."
394 (if (setq from
(string-match lao-transcription-pattern str from
))
398 (setq to
(match-end 0)))))
401 (if (setq to
(re-search-forward lao-transcription-pattern to t
))
402 (setq from
(match-beginning 0))
405 (let* ((consonant (match-string 1 str
))
406 (semivowel (match-string 3 str
))
407 (vowel (match-string 5 str
))
408 (maa-sakod (match-string 8 str
))
409 (tone (match-string 9 str
))
410 lao-consonant lao-semivowel lao-vowel lao-maa-sakod lao-tone
412 (setq to
(match-end 0))
414 (setq str
(cdr (assoc (match-string 0 str
)
415 lao-transcription-punctuation-alist
)))
417 (cdr (assoc consonant lao-transcription-consonant-alist
)))
418 (if (vectorp lao-consonant
)
419 (setq lao-consonant
(aref lao-consonant
0)))
420 (setq clen
(length lao-consonant
))
422 ;; Include semivowel in STR.
424 (cdr (assoc semivowel lao-transcription-semi-vowel-alist
))
426 (concat lao-consonant lao-semivowel
)
427 (concat (substring lao-consonant
0 1) lao-semivowel
428 (substring lao-consonant
1))))
429 (setq str lao-consonant
))
433 (cdr (assoc vowel lao-transcription-vowel-alist
)))
434 (if (vectorp lao-vowel
)
435 (setq lao-vowel
(aref lao-vowel
0)))
436 (setq rule
(assoc lao-vowel lao-vowel-reordering-rule
))
438 (setq rule
(nth 1 rule
))
440 (cdr (assoc maa-sakod lao-transcription-maa-sakod-alist
))
442 (or (cdr (assq (aref lao-maa-sakod
0) (nthcdr 2 rule
)))
446 (error "Lao vowel %S has no re-ordering rule" lao-vowel
))
447 (setq lao-consonant str str
"")
450 (setq str
(concat str lao-consonant
)
452 (setq str
(concat str
(list (car rule
)))))
453 (setq rule
(cdr rule
)))
455 (error "Lao vowel %S has malformed re-ordering rule" vowel
))
456 ;; Set CIDX to after upper or lower vowel if any.
457 (let ((len (length str
)))
458 (while (and (< cidx len
)
459 (memq (get-char-code-property (aref str cidx
)
461 '(vowel-lower vowel-upper
)))
462 (setq cidx
(1+ cidx
))))
464 (setq str
(concat str lao-maa-sakod
)))
467 (cdr (assoc tone lao-transcription-tone-alist
))
468 str
(concat (substring str
0 cidx
) lao-tone
469 (substring str cidx
)))))))
470 (list from to
(lao-compose-string str
)))))
473 (defun lao-transcribe-roman-to-lao-string (str)
474 "Transcribe Romanized Lao string STR to Lao character string."
479 (while (setq val
(lao-transcribe-single-roman-syllable-to-lao from to str
))
480 (let ((start (car val
))
484 (setq lao-str
(concat lao-str
(substring str from start
) lao
))
485 (setq lao-str
(concat lao-str lao
)))
488 (concat lao-str
(substring str from to
))
492 (defun lao-composition-function (gstring)
493 (if (= (lgstring-char-len gstring
) 1)
494 (compose-gstring-for-graphic gstring
)
495 (or (font-shape-gstring gstring
)
496 (let ((glyph-len (lgstring-glyph-len gstring
))
499 (while (and (< i glyph-len
)
500 (setq glyph
(lgstring-glyph gstring i
)))
502 (compose-glyph-string-relative gstring
0 i
0.1)))))
505 (defun lao-compose-region (from to
)
508 (narrow-to-region from to
)
509 (goto-char (point-min))
510 (with-category-table lao-category-table
511 (while (re-search-forward lao-composition-pattern nil t
)
512 (compose-region (match-beginning 0) (point))))))
517 ;;; lao-util.el ends here