0b72c807dff61baf9afe9411e4f072e1a0703d8c
[chise/concord-images.git] / concord-kaisei.el
1 (concord-assign-genre 'glyph-image "/usr/local/var/photo/db")
2
3 (concord-assign-genre 'hng-card "/usr/local/var/hng-card/db")
4 (mount-char-attribute-table '->HNG)
5 (mount-char-attribute-table '<-HNG)
6
7 (defun concord-kaisei-add-takuhon (source-id file-prefix taku-num
8                                              source-name
9                                              source-short-name ccs)
10   (let ((taku-file (format "%s%03d" file-prefix taku-num))
11         glyph-num
12         base-cobj
13         base-id
14         x y w h ucs seg-cobj char
15         seg-glyph-cobj seg-glyph-id
16         char-num-alist ret char-num ucs-hng-chars hng-code hng-cards)
17     (setq base-cobj
18           (concord-images-add-url
19            (format
20             "http://hng.chise.org/images/zinbun/takuhon/kaisei/%s.jpg"
21             taku-file)
22            (format
23             "http://hng.chise.org/images/iiif/zinbun/takuhon/kaisei/%s.tif"
24             taku-file)
25            (format
26             "http://hng.chise.org/iipsrv/iipsrv.fcgi?FIF=/zinbun/takuhon/kaisei/%s.tif&CVT=jpeg"
27             taku-file)))
28     (setq base-id (concord-object-get base-cobj '=id))
29     (concord-object-put
30      base-cobj 'name (format "%s%d" source-name taku-num))
31     (concord-object-put
32      base-cobj
33      '=location@djvu
34      (format
35       "http://coe21.zinbun.kyoto-u.ac.jp/db-machine/imgsrv/djvu/kaisei/%s.djvu"
36       taku-file))
37     (with-current-buffer
38         (find-file-noselect
39          (format "/opt/photo/zinbun/djvuchar/takuhonkaisei/%s.csv"
40                  taku-file))
41       (goto-char (point-min))
42       (setq glyph-num 0)
43       (while (re-search-forward
44               "^\\([0-9]+\\),\\([0-9]+\\),\\([0-9]+\\),\\([0-9]+\\),\\([0-9]+\\)"
45               nil t)
46         (setq x (string-to-int (match-string 1))
47               y (string-to-int (match-string 2))
48               w (string-to-int (match-string 3))
49               h (string-to-int (match-string 4))
50               ucs (string-to-int (match-string 5)))
51         (setq seg-cobj (concord-images-add-segments base-cobj x y w h))
52         (setq seg-glyph-id (intern (format "%s/char=%d" base-id glyph-num)))
53         (unless (setq seg-glyph-cobj
54                       (concord-decode-object '=id seg-glyph-id))
55           (setq seg-glyph-cobj
56                 (concord-make-object 'glyph-image seg-glyph-id))
57           (concord-object-adjoin*
58            seg-glyph-cobj '->image-resource seg-cobj)
59           (concord-object-adjoin*
60            seg-glyph-cobj '<-segmented-glyph-image base-cobj)
61           )
62         (setq char (decode-char '=ucs ucs))
63         (concord-object-put seg-glyph-cobj 'character (list char))
64         (cond ((setq ret (assq char char-num-alist))
65                (setq char-num (1+ (cdr ret)))
66                (setcdr ret char-num)
67                )
68               (t
69                (setq char-num 0)
70                (setq char-num-alist (cons (cons char 0)
71                                           char-num-alist))
72                ))
73         (concord-object-put
74          seg-glyph-cobj
75          'name (format "%s%d-%d (%c-%d)"
76                        source-short-name taku-num glyph-num char char-num))
77         (concord-object-put
78          seg-cobj
79          '=location@djvuchar
80          (format "http://coe21.zinbun.kyoto-u.ac.jp/djvuchar?jpg=data/takuhonkaisei/%s&coords=%d,%d,%d,%d"
81                  taku-file x y (+ x w) (+ y h)))
82         (setq ucs-hng-chars (char-feature char '->HNG))
83         (setq hng-cards nil)
84         (dolist (hng-char ucs-hng-chars)
85           (when (and (setq hng-code (encode-char hng-char ccs))
86                      (setq ret (concord-decode-object
87                                 '=hng-card
88                                 (intern (format "%d-%d"
89                                                 source-id
90                                                 (/ hng-code 10)))
91                                 'hng-card))
92                      (not (member ret hng-cards)))
93             (setq hng-cards (append hng-cards (list ret)))))
94         (concord-object-put seg-glyph-cobj
95                             '<-glyph-image@zinbun/takuhon hng-cards)
96         (setq glyph-num (1+ glyph-num))
97         ))))
98
99 (defun concord-kaisei-fix-buffer (buffer)
100   (interactive "bBuffer ")
101   (goto-char (point-min))
102   (let (beg end code)
103     (while (re-search-forward
104             ",38 35 120 \\(\\([0-9]+\\)\\( [0-9]+\\)+\\) 59$" nil t)
105       (setq beg (1+ (match-beginning 0))
106             end (match-end 0))
107       (setq code (string-to-int
108                   (mapconcat
109                    (lambda (unit)
110                      (char-to-string
111                       (decode-char 'ascii
112                                    (string-to-number unit))))
113                    (split-string (match-string 1) " ")
114                    "")
115                   16))
116       (delete-region beg end)
117       (insert (format "%d" code)))))