1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
;;; This file is part of Cedilla.
;;; Copyright (C) 2002 by Juliusz Chroboczek.
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
(in-package "CEDILLA")
(defvar *known-glyphs*)
(defvar *currently-computed-glyphs* '())
(defvar *currently-computed-simple-glyphs* '())
(defun recall-glyph (ccs)
"Recall a previously computed glyph for CCS."
(let ((val (gethash ccs *known-glyphs*)))
(if (eql val :unknown) nil val)))
(defun remember-glyph (ccs glyph)
"Remember GLYPH as being associated to CCS. Returns GLYPH."
(when ccs
(setf (gethash ccs *known-glyphs*) (or glyph :unknown)))
glyph)
(defun compute-stream-glyphs (in font-list)
"Compute and setup all glyphs needed to typeset the character stream IN."
(loop
(let ((ccs (next-ccs in)))
(when (eql ccs in) (return nil))
(compute-glyph ccs font-list))))
(defun compute-glyph (ccs font-list &optional (fallback #\?))
"Compute and setup the glyph needed to typeset CCS."
(cond
((and (characterp ccs) (<= (char-code ccs) 31)) nil)
(t
(let ((glyph (or (recall-glyph ccs)
(find-glyph ccs font-list))))
(etypecase glyph
(null (when-verbose (warn "Giving up on ~S" ccs)))
(glyph (ensure-instance glyph))
(list (mapc #'ensure-instance glyph)))
(or glyph
(if fallback
(compute-glyph fallback font-list nil)))))))
(eval-when (load compile eval)
(defmacro with-circularity-detection ((ccs &optional font) &body body)
(let ((tested (gensym "TESTED")))
`(let ((,tested (list ,font ,ccs)))
(unless (member ,tested *currently-computed-glyphs* :test 'equal)
(let ((*currently-computed-glyphs*
(cons ,tested *currently-computed-glyphs*)))
,@body)))))
(defmacro with-simple-circularity-detection ((ccs &optional font) &body body)
(let ((tested (gensym "TESTED")))
`(let ((,tested (list ,font ,ccs)))
(unless (member ,tested *currently-computed-simple-glyphs* :test 'equal)
(let ((*currently-computed-simple-glyphs*
(cons ,tested *currently-computed-simple-glyphs*)))
,@body)))))
)
(defun find-glyph (ccs font-list)
"Memoised version of FIND-GLYPH*."
(or (recall-glyph ccs)
(remember-glyph ccs (find-glyph* ccs font-list))))
(defun find-glyph* (ccs font-list)
"Compute the glyph needed to typeset CCS."
(with-circularity-detection (ccs)
(or
(cond
((vectorp ccs)
(let ((l (map 'list
#'(lambda (ccs)
(let ((glyph (find-glyph ccs font-list)))
(unless glyph
(return-from find-glyph* nil))
(if (listp glyph) glyph (list glyph))))
ccs)))
(apply #'append l)))
(t
(or
(dolist (font font-list)
(let ((g (find-glyph-with-font ccs font font-list)))
(when g (return-from find-glyph* g))))
(when (and (consp ccs) (or (symbolp (car ccs))
(eql 0 (ccs-combining-class (car ccs)))))
(find-magic-glyph (car ccs) (cdr ccs) font-list))
(some #'(lambda (fallback) (find-glyph fallback font-list))
(ccs-fallbacks ccs))
;; This is needed in order to find the fallback of an alternative
(some #'(lambda (alt) (find-glyph alt font-list))
(ccs-alternatives ccs))
(when (consp ccs)
(some #'(lambda (fallback)
(find-glyph (cons (car ccs) fallback) font-list))
(ccs-fallbacks (cdr ccs)))
(some #'(lambda (alt)
(find-glyph (cons (car ccs) alt) font-list))
(ccs-alternatives (cdr ccs))))))))))
(defun find-simple-glyph (ccs font-list)
"Compute a simple (not composite) glyph that can be used to typeset CCS."
(with-simple-circularity-detection (ccs)
(or
(dolist (font font-list)
(let ((g (find-simple-glyph-with-font ccs font font-list)))
(when g (return-from find-simple-glyph g))))
(when (and (consp ccs) (or (symbolp (car ccs))
(eql 0 (ccs-combining-class (car ccs)))))
(find-magic-glyph (car ccs) (cdr ccs) font-list nil t))
(some #'(lambda (fallback) (find-simple-glyph fallback font-list))
(ccs-fallbacks ccs)))))
(defun find-simple-glyph-with-font (ccs font font-list)
"Find a simple glyph for CCS in FONT."
(with-simple-circularity-detection (ccs font)
(cond
((and (consp ccs) (or (symbolp (car ccs))
(eql 0 (ccs-combining-class (car ccs)))))
(find-magic-glyph (car ccs) (cdr ccs) font-list font t))
(t
(or (find-font-glyph ccs font)
(some
#'(lambda (alt) (find-simple-glyph-with-font alt font font-list))
(ccs-alternatives ccs))
;; This is needed in order to properly find precomposed glyphs
;; in the presence of the varia/grave and psili/acute nonsense.
(and (consp ccs)
(some #'(lambda (alt)
(find-simple-glyph-with-font
(cons (car ccs) alt) font font-list))
(ccs-alternatives (cdr ccs)))))))))
(defun find-glyph-with-font (ccs font font-list)
"Find a glyph suitable for typesetting CCS that uses FONT for its base part"
(with-circularity-detection (ccs font)
(or
(cond
((vectorp ccs)
(map 'list
#'(lambda (ccs)
(or (find-glyph-with-font ccs font font-list)
(return-from find-glyph-with-font nil)))
ccs))
(t
(or
(find-simple-glyph-with-font ccs font font-list)
(when (consp ccs)
(or
;; Special-case double combining characters that sometimes
;; come precomposed -- e.g. dialytika-tonos. Yuck.
(when (and (characterp (car ccs)) (consp (cdr ccs))
(characterp (cadr ccs)) (not (null (cddr ccs))))
(find-composite-glyph (cons (car ccs) (cadr ccs))
(cddr ccs)
font-list font))
(find-composite-glyph (car ccs) (cdr ccs) font-list font)))
(some
#'(lambda (alt) (find-glyph-with-font alt font font-list))
(ccs-alternatives ccs))))))))
(defun find-magic-glyph (magic ccs font-list &optional font simple)
"Compute a magic glyph."
(flet ((f-g (ccs)
(cond
((and font simple)
(find-simple-glyph-with-font ccs font font-list))
((and font (not simple))
(find-glyph-with-font ccs font font-list))
((and (not font) simple)
(find-simple-glyph ccs font-list))
(t
(find-glyph ccs font-list)))))
(cond
((and (eql magic ':dotless)
(or (not (dotted-character-p ccs))
(consp ccs)))
(f-g ccs))
(t
(or
(and (eql magic ':dotless)
font
(find-font-glyph ccs font t))
(let ((glyph (ensure-glyph (f-g ccs))))
(and glyph (make-magic-glyph magic ccs glyph))))))))
(defun find-composite-glyph (c-ccs b-ccs font-list &optional font)
"Compose C-CCS over B-CCS."
(cond
((or (symbolp c-ccs) (eql 0 (ccs-combining-class c-ccs)))
(find-magic-glyph c-ccs b-ccs font-list font))
(t
(let* ((b-dotless
(if (member (ccs-combining-class c-ccs)
'(212 214 216 228 230 232))
(cons ':dotless b-ccs)
b-ccs))
(b-glyph
(ensure-glyph
(if font
(find-glyph-with-font b-dotless font font-list)
(find-glyph b-dotless font-list)))))
(and b-glyph
(let* ((preferred-font
(or font
(and (font-glyph-p b-glyph) (glyph-font b-glyph))))
(c-glyph
(ensure-glyph
(or
(and preferred-font
(find-simple-glyph-with-font
c-ccs preferred-font font-list))
(find-simple-glyph c-ccs font-list)
(find-special-case-combining-glyph
c-ccs font-list font)))))
(and c-glyph
(compute-composite-glyph c-ccs c-glyph b-ccs b-glyph))))))))
(defun find-special-case-combining-glyph (ccs font-list &optional font)
(labels ((g (ccs)
(or
(and font (find-simple-glyph-with-font ccs font font-list))
(find-simple-glyph ccs font-list)))
(mcg (g1 g2 dx dy)
(make-composite-glyph
:components
(list
(make-composite-component :glyph g1 :dx 0 :dy 0)
(make-composite-component :glyph g2 :dx dx :dy dy))))
(side-by-side (g1 g2)
(let ((dx 0)
(x1-1 (glyph-x1 g1)) (x0-2 (glyph-x0 g2))
(y1 (/ (+ (glyph-y0 g1) (glyph-y1 g1)) 2.0))
(y2 (/ (+ (glyph-y0 g2) (glyph-y1 g2)) 2.0)))
(mcg g1 g2 (+ dx (- x1-1 x0-2)) (- y1 y2))))
(stack (g1 g2)
(let ((xc-1 (/ (+ (glyph-x0 g1) (glyph-x1 g1)) 2.0))
(xc-2 (/ (+ (glyph-x0 g2) (glyph-x1 g2)) 2.0))
(y1-1 (glyph-y1 g1)) (y0-2 (glyph-y0 g2)))
(mcg g2 g1 (- xc-2 xc-1) (- y0-2 y1-1)))))
(when (and (consp ccs))
(let* ((ccs1 (car ccs)) (ccs2 (cdr ccs))
(c1 (and (characterp ccs1) (char-code ccs1)))
(c2 (and (characterp ccs1) (char-code ccs2))))
(cond
;; Greek
((and
(member c1 '(#x300 #x301 #x302 #x303))
(member c2 '(#x313 #x314 #x342))
(let* ((g1 (g ccs1)) (g2 (and g1 (g ccs2))))
(and g2 (side-by-side g2 g1)))))
((and
(member c1 '(#x300 #x301 #x302 #x342))
(equal c2 #x308)
(let* ((g1 (g ccs1)) (g2 (and g1 (g ccs2))))
(and g2 (stack g2 g1)))))
;; Vietnamese
((and (member c1 '(#x300 #x301)) (eql c2 #x302))
(let ((g1 (g ccs1)) (g2 (g ccs2)))
(when g2
(let* ((x0-1 (glyph-x0 g1)) (y0-1 (glyph-y0 g1))
(x1-1 (glyph-x1 g1))
(x0-2 (glyph-x0 g2)) (x1-2 (glyph-x1 g2))
(ym-2 (+ (glyph-y0 g2)
(* 0.5 (- (glyph-y1 g2) (glyph-y0 g2))))))
(mcg g2 g1
(if (eql c1 #x300) ; grave
(+ (- x0-2 x1-1) (* (- x1-2 x0-2) 0.15))
(+ (- x0-2 x0-1) (* (- x1-2 x0-2) 0.85)))
(- ym-2 y0-1))))))
((and
(member c1 '(#x300 #x301 #x303 #x309 #x313))
(member c2 '(#x302 #x306)))
(let* ((g1 (g ccs1)) (g2 (and g1 (g ccs2))))
(and g2 (stack g2 g1))))
(t nil))))))
(defun compute-composite-glyph (cc gc ccs gb)
"Position a composite component over a base glyph.
The base glyph is GB (associated to CCS); the component is GC (CC)."
(flet ((av (x y) (* 0.5 (+ x y))))
(let* ((class (ccs-combining-class cc))
(gbb (glyph-base gb)) ; hereditary base glyph
(eb (or (= (glyph-x0 gb) (glyph-x1 gb)) ; empty base
(= (glyph-y0 gb) (glyph-y1 gb))))
(x0-bb (+ (glyph-base-dx gb) (glyph-x0 gbb)))
(y0-bb (+ (glyph-base-dy gb) (glyph-y0 gbb)))
(x1-bb (+ (glyph-base-dx gb) (glyph-x1 gbb)))
(y1-bb (+ (glyph-base-dy gb) (glyph-y1 gbb)))
(h (not (member class '(202 214 220 230 233 234 240 1))))
(v (not (member class '(200 202 204))))
(x0-b (if eb 0 (if h (glyph-x0 gb) x0-bb)))
(y0-b (if eb 0 (if v (glyph-y0 gb) y0-bb)))
(x1-b (if eb 600 (if h (glyph-x1 gb) x1-bb)))
(y1-b (if eb 430 (if v (glyph-y1 gb) y1-bb)))
(x12-b (av x0-b x1-b))
(y12-b (av y0-b y1-b))
(x0-c (glyph-x0 gc)) (x1-c (glyph-x1 gc))
(y0-c (glyph-y0 gc)) (y1-c (glyph-y1 gc))
(x12-c (av x0-c x1-c))
(y12-c (av y0-c y1-c))
dx dy)
(case class
((202 214 220 230 233 234 240 1)
(cond
((eql cc (code-char #x328)) ; ogonek
(case (ccs-base ccs)
((#\a #\A #\u) (setf dx (+ (- x12-b x12-c) 80)))
((#\e #\E) (setf dx (+ (- x12-b x12-c) 20)))
(t (setf dx (- x12-b x12-c)))))
((and (eql class 230) (eql (ccs-base ccs) #\j))
(setf dx (+ (- x12-b x12-c) 80)))
((and (consp cc)
(eql (car cc) (code-char #x300))
(eql (cdr cc) (code-char #x302))) ; circumflex-grave
(setf dx (+ (- x12-b x12-c) -65)))
((and (consp cc)
(eql (car cc) (code-char #x301))
(eql (cdr cc) (code-char #x302))) ; circumflex-acute
(setf dx (+ (- x12-b x12-c) +65)))
(t (setf dx (- x12-b x12-c)))))
((222 226 232)
(setf dx (+ (- x1-b x0-c) 20)))
((216)
(setf dx (+ (- x1-b x0-c) -80)))
(t (warn "Unhandled combining class ~A (dx)" class) (setf dx 0)))
(case class
((200 202 204) (setf dy (+ (- y0-b y1-c) 10)))
((214) (setf dy (+ (- y1-b y0-c) -10)))
((212 216) (setf dy (+ (- y1-b y1-c) 30)))
((1 208 210 224 226) (setf dy (- y12-b y12-c)))
((218 220 222 233 240) (setf dy (+ (- y0-b y1-c) -70)))
((228 230 232 234) (setf dy (+ (- y1-b y0-c) 70)))
(t (warn "Unhandled combining class ~A (dy)" class) (setf dy 0)))
(make-composite-glyph
:components
(list (make-composite-component :glyph gb :dx 0 :dy 0)
(make-composite-component :glyph gc :dx dx :dy dy))))))
(defmethod ensure-glyph (list)
"Given a glyph or list of glyphs, return an equivalent glyph."
(cond
((glyph-p list) list)
((null (cdr list)) (car list))
(t
(let ((components nil) (width 0))
(dolist (g list)
(push (make-composite-component :glyph g :dx width :dy 0) components)
(incf width (glyph-width g)))
(make-composite-glyph :width width
:base :self
:components (nreverse components))))))
(defmethod ensure-instance ((glyph font-glyph))
(unless (glyph-instance glyph)
(ensure-instance-with-font glyph (glyph-font glyph))))
(defmethod ensure-instance ((glyph composite-glyph))
(dolist (c (composite-glyph-components glyph))
(ensure-instance (component-glyph c))))
(defmethod put-glyph :before ((font font) (glyph font-glyph) &optional index)
(declare (ignore index))
(setf (glyph-font glyph) font))
(defun header-string (string)
(with-output-to-string (s)
(do ((i 0))
((= i (length string)))
(cond
((eql (aref string i) #\%)
(ecase (aref string (+ i 1))
((#\%) (princ #\%))
((#\,) (princ #\,))
((#\p) (format s "~A" *page-number*))
((#\P) (format s "~R" *page-number*))
((#\r) (format s "~(~@R~)" *page-number*))
((#\R) (format s "~@R" *page-number*)))
(incf i 2))
(t (princ (aref string i) s)
(incf i))))))
(defun compute-header-glyphs (string font-list)
(flet ((compute-digit-glyphs ()
(dolist (c '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
(compute-glyph c font-list)))
(compute-lower-roman-glyphs ()
(dolist (c '(#\i #\v #\x #\c #\m))
(compute-glyph c font-list)))
(compute-upper-roman-glyphs ()
(dolist (c '(#\I #\V #\X #\C #\M))
(compute-glyph c font-list)))
(compute-lower-glyphs ()
(loop for i from (char-code #\a) upto (char-code #\z)
do (compute-glyph (code-char i) font-list)))
(compute-upper-glyphs ()
(loop for i from (char-code #\A) upto (char-code #\Z)
do (compute-glyph (code-char i) font-list))))
(do ((i 0))
((= i (length string)))
(cond
((eql (aref string i) #\%)
(ecase (aref string (+ i 1))
((#\%) (compute-glyph #\% font-list))
((#\,) (compute-glyph #\, font-list))
((#\p) (compute-digit-glyphs))
((#\P) (compute-lower-glyphs) (compute-glyph #\- font-list))
((#\r) (compute-lower-roman-glyphs))
((#\R) (compute-upper-roman-glyphs)))
(incf i 2))
(t (compute-glyph (normalise-ccs (aref string i)) font-list)
(incf i))))))
|