comparison lisp/international/mule-util.el @ 26890:4cd9407a4683

(set-nested-alist): Set BRANCHES (if non-nil) at the tail of ALIST. (compose-region, decompose-region, decompse-string, reference-point-alist, compose-chars): Moved to composite.el. (compose-chars-component, compose-chars-rule, decompose-composite-char): Deleted.
author Kenichi Handa <handa@m17n.org>
date Wed, 15 Dec 1999 00:42:14 +0000
parents 1f573e26070a
children e47fb07b39d4
comparison
equal deleted inserted replaced
26889:8ce2101d81c0 26890:4cd9407a4683
170 (setcdr alist (cons slot (cdr alist))))) 170 (setcdr alist (cons slot (cdr alist)))))
171 (setq alist (cdr slot)) 171 (setq alist (cdr slot))
172 (setq i (1+ i))) 172 (setq i (1+ i)))
173 (setcar alist entry) 173 (setcar alist entry)
174 (if branches 174 (if branches
175 (if (cdr alist) 175 (setcdr (last alist) branches))))
176 (error "Can't set branches for keyseq %s" keyseq)
177 (setcdr alist branches)))))
178 176
179 ;;;###autoload 177 ;;;###autoload
180 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long) 178 (defun lookup-nested-alist (keyseq alist &optional len start nil-for-too-long)
181 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition. 179 "Look up key sequence KEYSEQ in nested alist ALIST. Return the definition.
182 Optional 1st argument LEN specifies the length of KEYSEQ. 180 Optional 1st argument LEN specifies the length of KEYSEQ.
288 (cons (coding-system-get x 'coding-category) x))) 286 (cons (coding-system-get x 'coding-category) x)))
289 coding-priority)) 287 coding-priority))
290 (detect-coding-region from to)))) 288 (detect-coding-region from to))))
291 289
292 290
293 ;;; Composite character manipulations.
294
295 ;;;###autoload
296 (defun compose-region (start end)
297 "Compose all characters in the current region into one composite character.
298 When called from a program, expects two arguments,
299 positions (integers or markers) specifying the region."
300 (interactive "r")
301 (save-excursion
302 (let ((str (buffer-substring start end)))
303 (goto-char start)
304 (insert (compose-string str))
305 (delete-char (- end start)))))
306
307 ;;;###autoload
308 (defun decompose-region (start end)
309 "Decompose all composite characters in the current region.
310 Composite characters are broken up into individual components.
311 When called from a program, expects two arguments,
312 positions (integers or markers) specifying the region."
313 (interactive "r")
314 (let ((buf (current-buffer))
315 (cmpchar-head (char-to-string leading-code-composition)))
316 (with-temp-buffer
317 (insert-buffer-substring buf start end)
318 (set-buffer-multibyte nil)
319 (goto-char (point-min))
320 (while (search-forward cmpchar-head nil t)
321 (if (looking-at "[\240-\377][\240-\377][\240-\377][\240-\377]+")
322 (let* ((from (1- (point)))
323 (to (match-end 0))
324 (str (string-as-multibyte (buffer-substring from to))))
325 (if (cmpcharp (string-to-char str))
326 (progn
327 (delete-region from to)
328 (insert (string-as-unibyte (decompose-string str))))
329 (goto-char to)))))
330 (set-buffer-multibyte t)
331 (let ((tempbuf (current-buffer)))
332 (save-excursion
333 (set-buffer buf)
334 (goto-char start)
335 (delete-region start end)
336 (insert-buffer-substring tempbuf))))))
337
338 ;;;###autoload
339 (defun decompose-string (string)
340 "Decompose all composite characters in STRING."
341 (let ((len (length string))
342 (idx 0)
343 (i 0)
344 (str-list nil)
345 ch)
346 (while (< idx len)
347 (setq ch (aref string idx))
348 (if (>= ch min-composite-char)
349 (progn
350 (if (> idx i)
351 (setq str-list (cons (substring string i idx) str-list)))
352 (setq str-list (cons (decompose-composite-char ch) str-list))
353 (setq i (1+ idx))))
354 (setq idx (1+ idx)))
355 (if (not str-list)
356 (copy-sequence string)
357 (if (> idx i)
358 (setq str-list (cons (substring string i idx) str-list)))
359 (apply 'concat (nreverse str-list)))))
360
361 ;;;###autoload
362 (defconst reference-point-alist
363 '((tl . 0) (tc . 1) (tr . 2)
364 (ml . 3) (mc . 4) (mr . 5)
365 (bl . 6) (bc . 7) (br . 8)
366 (top-left . 0) (top-center . 1) (top-right . 2)
367 (mid-left . 3) (mid-center . 4) (mid-right . 5)
368 (bottom-left . 6) (bottom-center . 7) (bottom-right . 8)
369 (0 . 0) (1 . 1) (2 . 2)
370 (3 . 3) (4 . 4) (5 . 5)
371 (6 . 6) (7 . 7) (8 . 8))
372 "Alist of reference point symbols vs reference point codes.
373 A reference point symbol is to be used to specify a composition rule
374 while making a composite character by the function `compose-chars'
375 (which see).
376
377 Meanings of reference point codes are as follows:
378
379 0----1----2 <-- ascent 0:tl or top-left
380 | | 1:tc or top-center
381 | | 2:tr or top-right
382 | | 3:ml or mid-left
383 | 4 <--+---- center 4:mc or mid-center
384 | | 5:mr or mid-right
385 --- 3 5 <-- baseline 6:bl or bottom-left
386 | | 7:bc or bottom-center
387 6----7----8 <-- descent 8:br or bottom-right
388
389 Reference point symbols are to be used to specify composition rule of
390 the form \(GLOBAL-REF-POINT . NEW-REF-POINT), where GLOBAL-REF-POINT
391 is a reference point in the overall glyphs already composed, and
392 NEW-REF-POINT is a reference point in the new glyph to be added.
393
394 For instance, if GLOBAL-REF-POINT is 8 and NEW-REF-POINT is 1, the
395 overall glyph is updated as follows:
396
397 +-------+--+ <--- new ascent
398 | | |
399 | global| |
400 | glyph | |
401 --- | | | <--- baseline (doesn't change)
402 +----+--+--+
403 | | new |
404 | |glyph|
405 +----+-----+ <--- new descent
406 ")
407
408 ;; Return a string for char CH to be embedded in multibyte form of
409 ;; composite character.
410 ;;;###autoload
411 (defun compose-chars-component (ch)
412 (if (< ch 128)
413 (format "\240%c" (+ ch 128))
414 (let ((str (string-as-unibyte (char-to-string ch))))
415 (if (cmpcharp ch)
416 (if (= (aref str 1) ?\xFF)
417 (error "Can't compose a rule-based composition character")
418 (substring str (if (= (aref str 1) ?\xFF) 2 1)))
419 (aset str 0 (+ (aref str 0) ?\x20))
420 str))))
421
422 ;; Return a string for composition rule RULE to be embedded in
423 ;; multibyte form of composite character.
424 (defsubst compose-chars-rule (rule)
425 (char-to-string (+ ?\xA0
426 (* (cdr (assq (car rule) reference-point-alist)) 9)
427 (cdr (assq (cdr rule) reference-point-alist)))))
428
429 ;;;###autoload
430 (defun compose-chars (first-component &rest args)
431 "Return one char string composed from the arguments.
432 For relative composition, each argument should be a non-composition character
433 or a relative-composition character.
434 For rule-based composition, Nth (where N is odd) argument should be
435 a non-composition character or a rule-based-composition character,
436 and Mth (where M is even) argument should be a composition rule.
437 A composition rule has the form \(GLOBAL-REF-POINT . NEW-REF-POINT).
438 See the documentation of `reference-point-alist' for more detail."
439 (if (= (length args) 0)
440 (char-to-string first-component)
441 (let* ((with-rule (consp (car args)))
442 (str (if (cmpcharp first-component)
443 (string-as-unibyte (char-to-string first-component))
444 (if with-rule
445 (concat (vector leading-code-composition ?\xFF)
446 (compose-chars-component first-component))
447 (concat (char-to-string leading-code-composition)
448 (compose-chars-component first-component))))))
449 (if (and (cmpcharp first-component)
450 (eq with-rule (/= (aref str 1) ?\xFF)))
451 (error "%s-compostion-character is not allowed in %s composition: %c"
452 (if with-rule "relative" "rule-based")
453 (if with-rule "rule-based" "relative")
454 first-component))
455 (while args
456 (if with-rule
457 (setq str (concat str (compose-chars-rule (car args)))
458 args (cdr args)))
459 (if (cmpcharp (car args))
460 (let ((cmp-str (string-as-unibyte (char-to-string (car args)))))
461 (if (eq with-rule (/= (aref cmp-str 1) ?\xFF))
462 (error "%s-compostion-character is not allowed in %s composition: %c"
463 (if with-rule "relative" "rule-based")
464 (if with-rule "rule-based" "relative")
465 (car args)))
466 (setq str (concat str (substring cmp-str
467 (if with-rule 2 1)))))
468 (setq str (concat str (compose-chars-component (car args)))))
469 (setq args (cdr args)))
470 (string-as-multibyte str))))
471
472 ;;;###autoload
473 (defun decompose-composite-char (char &optional type with-composition-rule)
474 "Convert composite character CHAR to a sequence of the components.
475 Optional 1st arg TYPE specifies the type of sequence returned.
476 It should be `string' (default), `list', or `vector'.
477 Optional 2nd arg WITH-COMPOSITION-RULE non-nil means the returned
478 sequence contains embedded composition rules if any. In this case, the
479 order of elements in the sequence is the same as arguments for
480 `compose-chars' to create CHAR.
481 If TYPE is omitted or is `string', composition rules are omitted
482 even if WITH-COMPOSITION-RULE is t."
483 (or type
484 (setq type 'string))
485 (let* ((len (composite-char-component-count char))
486 (i (1- len))
487 l)
488 (setq with-composition-rule (and with-composition-rule
489 (not (eq type 'string))
490 (composite-char-composition-rule-p char)))
491 (while (> i 0)
492 (setq l (cons (composite-char-component char i) l))
493 (if with-composition-rule
494 (let ((rule (- (composite-char-composition-rule char i) ?\xA0)))
495 (setq l (cons (cons (/ rule 9) (% rule 9)) l))))
496 (setq i (1- i)))
497 (setq l (cons (composite-char-component char 0) l))
498 (cond ((eq type 'string)
499 (apply 'string l))
500 ((eq type 'list)
501 l)
502 (t ; i.e. TYPE is vector
503 (vconcat l)))))
504
505 ;;; mule-util.el ends here 291 ;;; mule-util.el ends here
292