]> code.delx.au - gnu-emacs/blob - lisp/language/lao-util.el
Add a new function `svg-embed'
[gnu-emacs] / lisp / language / lao-util.el
1 ;;; lao-util.el --- utilities for Lao -*- coding: utf-8; -*-
2
3 ;; Copyright (C) 2001-2016 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
11
12 ;; Keywords: multilingual, Lao, i18n
13
14 ;; This file is part of GNU Emacs.
15
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.
20
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.
25
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/>.
28
29 ;;; Commentary:
30
31 ;;; Code:
32
33 ;; Setting information of Thai characters.
34
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)
40
41 (let ((l '((?ກ consonant "CHICKEN")
42 (?ຂ consonant "EGG")
43 (?຃ invalid)
44 (?ຄ consonant "BUFFALO")
45 (?຅ invalid)
46 (? invalid)
47 (?ງ consonant "SNAKE")
48 (?ຈ consonant "BUDDHIST NOVICE")
49 (?ຉ invalid)
50 (?ຊ consonant "ELEPHANT")
51 (?຋ invalid)
52 (?ຌ invalid)
53 (?ຍ consonant "MOSQUITO")
54 (?ຎ invalid)
55 (?ຎ invalid)
56 (?ຎ invalid)
57 (?ຎ invalid)
58 (?ຎ invalid)
59 (?ຎ invalid)
60 (?ດ consonant "NOSE")
61 (?ຕ consonant "EYE")
62 (?ຖ consonant "TO ASK,QUESTION")
63 (?ທ consonant "FLAG")
64 (?ຘ invalid)
65 (?ນ consonant "BIRD")
66 (?ບ consonant "FISHHOOK")
67 (?ປ consonant "FISH")
68 (?ຜ consonant "BEE")
69 (?ຝ consonant "WALL")
70 (?ພ consonant "MOUNTAIN")
71 (?ຟ consonant "FIRE")
72 (?ຠ invalid)
73 (?ມ consonant "HORSE")
74 (?ຢ consonant "MEDICINE")
75 (?ຣ consonant "CAR")
76 (?຤ invalid)
77 (?ລ consonant "MONKEY")
78 (?຦ invalid)
79 (?ວ consonant "HAND FAN")
80 (?ຨ invalid)
81 (?ຩ invalid)
82 (?ສ consonant "TIGER")
83 (?ຫ consonant "JAR")
84 (?ຬ invalid)
85 (?ອ consonant "TAKE")
86 (?ຮ consonant "BOAT")
87 (?ຯ special)
88 (?ະ vowel-base)
89 (?ັ vowel-upper)
90 (?າ vowel-base)
91 (?ຳ vowel-base)
92 (?ິ vowel-upper)
93 (?ີ vowel-upper)
94 (?ຶ vowel-upper)
95 (?ື vowel-upper)
96 (?ຸ vowel-lower)
97 (?ູ vowel-lower)
98 (?຺ invalid)
99 (?ົ vowel-upper)
100 (?ຼ semivowel-lower)
101 (?ຽ vowel-base)
102 (?຾ invalid)
103 (?຿ invalid)
104 (?ເ vowel-base)
105 (?ແ vowel-base)
106 (?ໂ vowel-base)
107 (?ໃ vowel-base)
108 (?ໄ vowel-base)
109 (?໅ invalid)
110 (?ໆ special)
111 (?໇ invalid)
112 (?່ tone)
113 (?້ tone)
114 (?໊ tone)
115 (?໋ tone)
116 (?໌ tone)
117 (?ໍ vowel-upper)
118 (?໎ invalid)
119 (?໏ invalid)
120 (?໐ special)
121 (?໑ special)
122 (?໒ special)
123 (?໓ special)
124 (?໔ special)
125 (?໕ special)
126 (?໖ special)
127 (?໗ special)
128 (?໘ special)
129 (?໙ special)
130 (?໚ invalid)
131 (?໛ invalid)
132 (?ໜ consonant "MOUSE")
133 (?ໝ consonant "DOG")
134 (?ໞ invalid)))
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 'meaning (nth 2 elm)))))
150
151 ;; The general composing rules are as follows:
152 ;;
153 ;; T
154 ;; V T V T
155 ;; CV -> C, CT -> C, CVT -> C, Cv -> C, CvT -> C
156 ;; v v
157 ;; T
158 ;; V T V T
159 ;; CsV -> C, CsT -> C, CsVT -> C, Csv -> C, CsvT -> C
160 ;; s s s s s
161 ;; v v
162
163
164 ;; where C: consonant, V: vowel upper, v: vowel lower,
165 ;; T: tone mark, s: semivowel lower
166
167 (defvar lao-composition-pattern
168 "\\cc\\(\\ct\\|\\cv\\ct?\\|\\cs\\(\\ct\\|\\cv\\ct?\\)?\\)"
169 "Regular expression matching a Lao composite sequence.")
170
171 ;;;###autoload
172 (defun lao-compose-string (str)
173 (with-category-table lao-category-table
174 (let ((idx 0))
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))))
178 str))
179
180 ;;; LRT: Lao <-> Roman Transcription
181
182 ;; Upper vowels and tone-marks are put on the letter.
183 ;; Semi-vowel-sign-lo and lower vowels are put under the letter.
184
185 (defconst lao-transcription-consonant-alist
186 (sort '(;; single consonants
187 ("k" . "ກ")
188 ("kh" . "ຂ")
189 ("qh" . "ຄ")
190 ("ng" . "ງ")
191 ("j" . "ຈ")
192 ("s" . "ສ")
193 ("x" . "ຊ")
194 ("y" . "ຍ")
195 ("d" . "ດ")
196 ("t" . "ຕ")
197 ("th" . "ຖ")
198 ("dh" . "ທ")
199 ("n" . "ນ")
200 ("b" . "ບ")
201 ("p" . "ປ")
202 ("hp" . "ຜ")
203 ("fh" . "ຝ")
204 ("ph" . "ພ")
205 ("f" . "ຟ")
206 ("m" . "ມ")
207 ("gn" . "ຢ")
208 ("l" . "ລ")
209 ("r" . "ຣ")
210 ("v" . "ວ")
211 ("w" . "ວ")
212 ("hh" . "ຫ")
213 ("O" . "ອ")
214 ("h" . "ຮ")
215 ("nh" . "ໜ")
216 ("mh" . "ໝ")
217 ("lh" . ["ຫຼ"])
218 ;; double consonants
219 ("ngh" . ["ຫງ"])
220 ("yh" . ["ຫຽ"])
221 ("wh" . ["ຫວ"])
222 ("hl" . ["ຫລ"])
223 ("hy" . ["ຫຍ"])
224 ("hn" . ["ຫນ"])
225 ("hm" . ["ຫມ"])
226 )
227 (function (lambda (x y) (> (length (car x)) (length (car y)))))))
228
229 (defconst lao-transcription-semi-vowel-alist
230 '(("r" . "ຼ")))
231
232 (defconst lao-transcription-vowel-alist
233 (sort '(("a" . "ະ")
234 ("ar" . "າ")
235 ("i" . "ິ")
236 ("ii" . "ີ")
237 ("eu" . "ຶ")
238 ("ur" . "ື")
239 ("u" . "ຸ")
240 ("uu" . "ູ")
241 ("e" . ["ເະ"])
242 ("ee" . "ເ")
243 ("ae" . ["ແະ"])
244 ("aa" . "ແ")
245 ("o" . ["ໂະ"])
246 ("oo" . "ໂ")
247 ("oe" . ["ເາະ"])
248 ("or" . "ໍ")
249 ("er" . ["ເິ"])
250 ("ir" . ["ເີ"])
251 ("ua" . ["ົວະ"])
252 ("uaa" . ["ົວ"])
253 ("ie" . ["ເັຽະ"])
254 ("ia" . ["ເັຽ"])
255 ("ea" . ["ເຶອ"])
256 ("eaa" . ["ເືອ"])
257 ("ai" . "ໄ")
258 ("ei" . "ໃ")
259 ("ao" . ["ເົາ"])
260 ("aM" . "ຳ"))
261 (function (lambda (x y) (> (length (car x)) (length (car y)))))))
262
263 ;; Maa-sakod is put at the tail.
264 (defconst lao-transcription-maa-sakod-alist
265 '(("k" . "ກ")
266 ("g" . "ງ")
267 ("y" . "ຍ")
268 ("d" . "ດ")
269 ("n" . "ນ")
270 ("b" . "ບ")
271 ("m" . "ມ")
272 ("v" . "ວ")
273 ("w" . "ວ")
274 ))
275
276 (defconst lao-transcription-tone-alist
277 '(("'" . "່")
278 ("\"" . "້")
279 ("^" . "໊")
280 ("+" . "໋")
281 ("~" . "໌")))
282
283 (defconst lao-transcription-punctuation-alist
284 '(("\\0" . "໐")
285 ("\\1" . "໑")
286 ("\\2" . "໒")
287 ("\\3" . "໓")
288 ("\\4" . "໔")
289 ("\\5" . "໕")
290 ("\\6" . "໖")
291 ("\\7" . "໗")
292 ("\\8" . "໘")
293 ("\\9" . "໙")
294 ("\\\\" . "ໆ")
295 ("\\$" . "ຯ")))
296
297 (defconst lao-transcription-pattern
298 (concat
299 "\\("
300 (mapconcat 'car lao-transcription-consonant-alist "\\|")
301 "\\)\\("
302 (mapconcat 'car lao-transcription-semi-vowel-alist "\\|")
303 "\\)?\\(\\("
304 (mapconcat 'car lao-transcription-vowel-alist "\\|")
305 "\\)\\("
306 (mapconcat 'car lao-transcription-maa-sakod-alist "\\|")
307 "\\)?\\("
308 (mapconcat (lambda (x) (regexp-quote (car x)))
309 lao-transcription-tone-alist "\\|")
310 "\\)?\\)?\\|"
311 (mapconcat (lambda (x) (regexp-quote (car x)))
312 lao-transcription-punctuation-alist "\\|")
313 )
314 "Regexp of Roman transcription pattern for one Lao syllable.")
315
316 (defconst lao-transcription-pattern
317 (concat
318 "\\("
319 (regexp-opt (mapcar 'car lao-transcription-consonant-alist))
320 "\\)\\("
321 (regexp-opt (mapcar 'car lao-transcription-semi-vowel-alist))
322 "\\)?\\(\\("
323 (regexp-opt (mapcar 'car lao-transcription-vowel-alist))
324 "\\)\\("
325 (regexp-opt (mapcar 'car lao-transcription-maa-sakod-alist))
326 "\\)?\\("
327 (regexp-opt (mapcar 'car lao-transcription-tone-alist))
328 "\\)?\\)?\\|"
329 (regexp-opt (mapcar 'car lao-transcription-punctuation-alist))
330 )
331 "Regexp of Roman transcription pattern for one Lao syllable.")
332
333 (defconst lao-vowel-reordering-rule
334 '(("ະ" (0 ?ະ) (0 ?ັ))
335 ("າ" (0 ?າ))
336 ("ິ" (0 ?ີ))
337 ("ີ" (0 ?ີ))
338 ("ຶ" (0 ?ຶ))
339 ("ື" (0 ?ື))
340 ("ຸ" (0 ?ຸ))
341 ("ູ" (0 ?ູ))
342 ("ເະ" (?ເ 0 ?ະ) (?ເ 0 ?ັ))
343 ("ເ" (?ເ 0))
344 ("ແະ" (?ແ 0 ?ະ) (?ແ 0 ?ັ))
345 ("ແ" (?ແ 0))
346 ("ໂະ" (?ໂ 0 ?ະ) (0 ?ົ) (?ຍ ?ໂ 0 ?ັ) (?ວ ?ໂ 0 ?ັ))
347 ("ໂ" (?ໂ 0))
348 ("ເາະ" (?ເ 0 ?າ ?ະ) (0 ?ັ ?ອ))
349 ("ໍ" (0 ?ໍ) (0 ?ອ))
350 ("ເິ" (?ເ 0 ?ິ))
351 ("ເີ" (?ເ 0 ?ີ))
352 ("ົວະ" (0 ?ົ ?ວ ?ະ) (0 ?ັ ?ວ))
353 ("ົວ" (0 ?ົ ?ວ) (0 ?ວ))
354 ("ເັຽະ" (?ເ 0 ?ັ ?ຽ ?ະ) (0 ?ັ ?ຽ))
355 ("ເັຽ" (?ເ 0 ?ັ ?ຽ) (0 ?ຽ))
356 ("ເຶອ" (?ເ 0 ?ຶ ?ອ))
357 ("ເືອ" (?ເ 0 ?ື ?ອ))
358 ("ໄ" (?ໄ 0))
359 ("ໃ" (?ໃ 0))
360 ("ເົາ" (?ເ 0 ?ົ ?າ))
361 ("ຳ" (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) ...)
365
366 VOWEL is a vowel string (e.g. \"ເັຽະ\").
367
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.
371
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.
376
377 For instance, rule `(\"ເືອ\" (?ເ t ?ື ?ອ))' tells that this vowel
378 string following a consonant `ກ' should be re-ordered as \"ເກືອ\".
379
380 Optional (MAA-SAKOD-n RULE-n) are rules specially applied to maa-sakod
381 character MAA-SAKOD-n.")
382
383 ;;;###autoload
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.
390
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."
393 (if str
394 (if (setq from (string-match lao-transcription-pattern str from))
395 (progn
396 (if (>= from to)
397 (setq from nil)
398 (setq to (match-end 0)))))
399 (save-excursion
400 (goto-char from)
401 (if (setq to (re-search-forward lao-transcription-pattern to t))
402 (setq from (match-beginning 0))
403 (setq from nil))))
404 (if from
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
411 clen cidx)
412 (setq to (match-end 0))
413 (if (not consonant)
414 (setq str (cdr (assoc (match-string 0 str)
415 lao-transcription-punctuation-alist)))
416 (setq lao-consonant
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))
421 (if semivowel
422 ;; Include semivowel in STR.
423 (setq lao-semivowel
424 (cdr (assoc semivowel lao-transcription-semi-vowel-alist))
425 str (if (= clen 1)
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))
430 (if vowel
431 (let (rule)
432 (setq lao-vowel
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))
437 (if (null maa-sakod)
438 (setq rule (nth 1 rule))
439 (setq lao-maa-sakod
440 (cdr (assoc maa-sakod lao-transcription-maa-sakod-alist))
441 rule
442 (or (cdr (assq (aref lao-maa-sakod 0) (nthcdr 2 rule)))
443 (nth 2 rule)
444 (nth 1 rule))))
445 (or rule
446 (error "Lao vowel %S has no re-ordering rule" lao-vowel))
447 (setq lao-consonant str str "")
448 (while rule
449 (if (= (car rule) 0)
450 (setq str (concat str lao-consonant)
451 cidx (length str))
452 (setq str (concat str (list (car rule)))))
453 (setq rule (cdr rule)))
454 (or cidx
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)
460 'phonetic-type)
461 '(vowel-lower vowel-upper)))
462 (setq cidx (1+ cidx))))
463 (if lao-maa-sakod
464 (setq str (concat str lao-maa-sakod)))
465 (if tone
466 (setq lao-tone
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)))))
471
472 ;;;###autoload
473 (defun lao-transcribe-roman-to-lao-string (str)
474 "Transcribe Romanized Lao string STR to Lao character string."
475 (let ((from 0)
476 (to (length str))
477 (lao-str "")
478 val)
479 (while (setq val (lao-transcribe-single-roman-syllable-to-lao from to str))
480 (let ((start (car val))
481 (end (nth 1 val))
482 (lao (nth 2 val)))
483 (if (> start from)
484 (setq lao-str (concat lao-str (substring str from start) lao))
485 (setq lao-str (concat lao-str lao)))
486 (setq from end)))
487 (if (< from to)
488 (concat lao-str (substring str from to))
489 lao-str)))
490
491 ;;;###autoload
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))
497 (i 0)
498 glyph)
499 (while (and (< i glyph-len)
500 (setq glyph (lgstring-glyph gstring i)))
501 (setq i (1+ i)))
502 (compose-glyph-string-relative gstring 0 i 0.1)))))
503
504 ;;;###autoload
505 (defun lao-compose-region (from to)
506 (interactive "r")
507 (save-restriction
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))))))
513
514 ;;
515 (provide 'lao-util)
516
517 ;;; lao-util.el ends here