| 1 |
;;; -*- Mode: LISP; Syntax: Ansi-Common-Lisp; Base: 10; Package: UAX-15 -*- |
| 2 |
|
| 3 |
(in-package :uax-15) |
| 4 |
|
| 5 |
(defun get-canonical-combining-class (ch) |
| 6 |
(gethash ch *canonical-combining-class* 0)) |
| 7 |
|
| 8 |
(defun decompose-char (char &optional (type :canonical)) |
| 9 |
(let ((decomped-chars (or (gethash char *canonical-decomp-map*) |
| 10 |
(and (eq type :compatible) |
| 11 |
(gethash char *compatible-decomp-map*))))) |
| 12 |
(if decomped-chars |
| 13 |
(mapcan (lambda (c) (decompose-char c type)) decomped-chars) |
| 14 |
(list char)))) |
| 15 |
|
| 16 |
(let* ((s-base #xAC00) |
| 17 |
(l-base #x1100) |
| 18 |
(v-base #x1161) |
| 19 |
(t-base #x11A7) |
| 20 |
(l-count 19) |
| 21 |
(v-count 21) |
| 22 |
(t-count 28) |
| 23 |
(n-count (* v-count t-count)) |
| 24 |
(s-count (* l-count n-count))) |
| 25 |
;; split |
| 26 |
(defun decompose-hangul-char (ch) |
| 27 |
(let ((s-index (- ch s-base))) |
| 28 |
(unless (<= 0 s-index (1- s-count)) |
| 29 |
(return-from decompose-hangul-char (list ch))) |
| 30 |
|
| 31 |
(let ((lc (+ l-base (floor s-index n-count))) |
| 32 |
(vc (+ v-base (floor (mod s-index n-count) t-count))) |
| 33 |
(tc (+ t-base (mod s-index t-count)))) |
| 34 |
(if (/= tc t-base) |
| 35 |
(list lc vc tc) |
| 36 |
(list lc vc))))) |
| 37 |
|
| 38 |
;; synthesis |
| 39 |
(defun compose-hangul (str &aux (len (length str))) |
| 40 |
(if (zerop len) |
| 41 |
str |
| 42 |
(let* ((last (aref str 0)) |
| 43 |
(new-chars (list last))) |
| 44 |
(loop for i from 1 below len |
| 45 |
for ch = (aref str i) |
| 46 |
for l-index = (- last l-base) |
| 47 |
for s-index = (- last s-base) |
| 48 |
do |
| 49 |
(tagbody |
| 50 |
;; 1. check to see if two current characters are L and V |
| 51 |
(when (<= 0 l-index (1- l-count)) |
| 52 |
(let ((v-index (- ch v-base))) |
| 53 |
(when (<= 0 v-index (1- v-count)) |
| 54 |
;; make syllable of form LV |
| 55 |
(setf last |
| 56 |
(+ s-base (* (+ (* l-index v-count) v-index) t-count))) |
| 57 |
(setf (car new-chars) last) ; reset last |
| 58 |
(go :end)))) ; discard ch |
| 59 |
|
| 60 |
;; 2. check to see if two current characters are LV and T |
| 61 |
(when (and (<= 0 s-index (1- s-count)) |
| 62 |
(zerop (mod s-index t-count))) |
| 63 |
(let ((t-index (- ch t-base))) |
| 64 |
(when (< 0 t-index t-count) |
| 65 |
;; make syllable of form LVT |
| 66 |
(setf last (+ last t-index)) |
| 67 |
(setf (car new-chars) last) ; reset last |
| 68 |
(go :end)))) ; discard ch |
| 69 |
|
| 70 |
;; if neigher case was true, just add the character |
| 71 |
(push (setf last ch) new-chars) |
| 72 |
:end)) |
| 73 |
(coerce (nreverse new-chars) 'unicode-string))))) |
| 74 |
|
| 75 |
(defun decompose (s type) |
| 76 |
(loop for c across s |
| 77 |
append |
| 78 |
(mapcan #'decompose-hangul-char (decompose-char c type)) |
| 79 |
into new-s |
| 80 |
finally |
| 81 |
(return (coerce new-s 'unicode-string)))) |
| 82 |
|
| 83 |
(defun canonical-ordering (decomposed-string &aux (s decomposed-string)) |
| 84 |
(let ((starter-indices |
| 85 |
(loop for i from 1 below (length s) |
| 86 |
for ccc = (get-canonical-combining-class (aref s i)) |
| 87 |
when (zerop ccc) |
| 88 |
collect i))) |
| 89 |
(loop for (beg end) on (cons 0 starter-indices) do |
| 90 |
(setf #1=(subseq s beg end) |
| 91 |
(stable-sort #1# #'< :key #'get-canonical-combining-class)))) |
| 92 |
s) |
| 93 |
|
| 94 |
(defun compose (decomposed-string) |
| 95 |
(let* ((s decomposed-string) |
| 96 |
(to-cs (coerce s 'simple-vector))) |
| 97 |
(loop for i from 1 below (length s) |
| 98 |
for ch-right = (aref s i) ; right character |
| 99 |
for ccc-right = (get-canonical-combining-class ch-right) |
| 100 |
do |
| 101 |
(loop for j from (1- i) downto 0 |
| 102 |
for ch-left = (aref to-cs j) ; left character |
| 103 |
for ccc-left = (and ch-left (get-canonical-combining-class ch-left)) |
| 104 |
when ch-left |
| 105 |
do |
| 106 |
(when (zerop ccc-left) |
| 107 |
;; ch-left + ch-right if there is a composite character ch-left replace |
| 108 |
(let ((comped-char (gethash (list ch-left ch-right) *canonical-comp-map*))) |
| 109 |
(when comped-char |
| 110 |
(setf (aref to-cs j) comped-char |
| 111 |
(aref to-cs i) nil))) |
| 112 |
(return)) |
| 113 |
|
| 114 |
(unless (< ccc-left ccc-right) |
| 115 |
(return)))) |
| 116 |
(compose-hangul (coerce (remove nil to-cs) 'unicode-string)))) |
| 117 |
|
| 118 |
;;;;;;;;;;;;;;;;;;;;;; |
| 119 |
;;;; NFD/NFKD/NFC/NFKC |
| 120 |
(defun nfd (s) |
| 121 |
(canonical-ordering (decompose s :canonical))) |
| 122 |
|
| 123 |
(defun nfkd (s) |
| 124 |
(canonical-ordering (decompose s :compatible))) |
| 125 |
|
| 126 |
(defun nfc (s) |
| 127 |
(compose (nfd s))) |
| 128 |
|
| 129 |
(defun nfkc (s) |
| 130 |
(compose (nfkd s))) |