Backport from trunk.
[emacs.git] / lisp / ps-def.el
blob5e045bccf9a47bc414b1b34484d0c90d21aeb46b
1 ;;; ps-def.el --- XEmacs and Emacs definitions for ps-print
3 ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
6 ;; Kenichi Handa <handa@m17n.org> (multi-byte characters)
7 ;; Maintainer: Kenichi Handa <handa@m17n.org> (multi-byte characters)
8 ;; Vinicius Jose Latorre <viniciusjl@ig.com.br>
9 ;; Keywords: wp, print, PostScript
10 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; See ps-print.el for documentation.
31 ;;; Code:
33 (eval-and-compile
34 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
36 (declare-function ps-plot-with-face "ps-print" (from to face))
37 (declare-function ps-plot-string "ps-print" (string))
39 (defvar ps-bold-faces) ; in ps-print.el
40 (defvar ps-italic-faces)
44 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 ;; XEmacs Definitions
49 (cond
50 ((featurep 'xemacs) ; XEmacs
53 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54 ;; ps-bdf
56 (defvar installation-directory nil)
57 (defvar coding-system-for-read)
60 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;; ps-mule
63 (defvar leading-code-private-22 157)
65 (or (fboundp 'charset-bytes)
66 (defun charset-bytes (charset) 1)) ; ascii
68 (or (fboundp 'charset-dimension)
69 (defun charset-dimension (charset) 1)) ; ascii
71 (or (fboundp 'charset-id)
72 (defun charset-id (charset) 0)) ; ascii
74 (or (fboundp 'charset-width)
75 (defun charset-width (charset) 1)) ; ascii
77 (or (fboundp 'find-charset-region)
78 (defun find-charset-region (beg end &optional table)
79 (list 'ascii)))
81 (or (fboundp 'char-width)
82 (defun char-width (char) 1)) ; ascii
84 (or (fboundp 'chars-in-region)
85 (defun chars-in-region (beg end)
86 (- (max beg end) (min beg end))))
88 (or (fboundp 'forward-point)
89 (defun forward-point (arg)
90 (save-excursion
91 (let ((count (abs arg))
92 (step (if (zerop arg)
94 (/ arg arg))))
95 (while (and (> count 0)
96 (< (point-min) (point)) (< (point) (point-max)))
97 (forward-char step)
98 (setq count (1- count)))
99 (+ (point) (* count step))))))
101 (or (fboundp 'decompose-composite-char)
102 (defun decompose-composite-char (char &optional type
103 with-composition-rule)
104 nil))
106 (or (fboundp 'encode-coding-string)
107 (defun encode-coding-string (string coding-system &optional nocopy)
108 (if nocopy
109 string
110 (copy-sequence string))))
112 (or (fboundp 'coding-system-p)
113 (defun coding-system-p (obj) nil))
115 (or (fboundp 'ccl-execute-on-string)
116 (defun ccl-execute-on-string (ccl-prog status str
117 &optional contin unibyte-p)
118 str))
120 (or (fboundp 'define-ccl-program)
121 (defmacro define-ccl-program (name ccl-program &optional doc)
122 `(defconst ,name nil ,doc)))
124 (or (fboundp 'multibyte-string-p)
125 (defun multibyte-string-p (str)
126 (let ((len (length str))
127 (i 0)
128 multibyte)
129 (while (and (< i len) (not (setq multibyte (> (aref str i) 255))))
130 (setq i (1+ i)))
131 multibyte)))
133 (or (fboundp 'string-make-multibyte)
134 (defalias 'string-make-multibyte 'copy-sequence))
136 (or (fboundp 'encode-char)
137 (defun encode-char (ch ccs)
138 ch))
141 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;; ps-print
144 ;; GNU Emacs
145 (or (fboundp 'line-beginning-position)
146 (defun line-beginning-position (&optional n)
147 (save-excursion
148 (and n (/= n 1) (forward-line (1- n)))
149 (beginning-of-line)
150 (point))))
153 ;; GNU Emacs
154 (or (fboundp 'find-composition)
155 (defalias 'find-composition 'ignore))
158 (defun ps-xemacs-color-name (color)
159 (if (color-specifier-p color)
160 (color-name color)
161 color))
164 (defalias 'ps-mark-active-p 'region-active-p)
167 (defun ps-face-foreground-name (face)
168 (ps-xemacs-color-name (face-foreground face)))
171 (defun ps-face-background-name (face)
172 (ps-xemacs-color-name (face-background face)))
175 (defalias 'ps-frame-parameter 'frame-property)
178 ;; Return t if the device (which can be changed during an emacs session)
179 ;; can handle colors.
180 (defun ps-color-device ()
181 (eq (device-class) 'color))
183 (defun ps-mapper (extent list)
184 (nconc list
185 (list (list (extent-start-position extent) 'push extent)
186 (list (extent-end-position extent) 'pull extent)))
187 nil)
190 (defun ps-extent-sorter (a b)
191 (< (extent-priority a) (extent-priority b)))
194 (defun ps-xemacs-face-kind-p (face kind kind-regex)
195 (let* ((frame-font (or (face-font-instance face)
196 (face-font-instance 'default)))
197 (kind-cons
198 (and frame-font
199 (assq kind
200 (font-instance-properties frame-font))))
201 (kind-spec (cdr-safe kind-cons))
202 (case-fold-search t))
203 (and kind-spec (string-match kind-regex kind-spec))))
206 ;; to avoid XEmacs compilation gripes
207 (defvar coding-system-for-write)
208 (defvar buffer-file-coding-system)
211 (and (fboundp 'find-coding-system)
212 (or (funcall 'find-coding-system 'raw-text-unix)
213 (funcall 'copy-coding-system 'no-conversion-unix 'raw-text-unix)))
216 (defun ps-color-values (x-color)
217 (let ((color (ps-xemacs-color-name x-color)))
218 (cond
219 ((fboundp 'x-color-values)
220 (funcall 'x-color-values color))
221 ((and (fboundp 'color-instance-rgb-components)
222 (ps-color-device))
223 (funcall 'color-instance-rgb-components
224 (if (color-instance-p x-color)
225 x-color
226 (make-color-instance color))))
228 (error "No available function to determine X color values")))))
231 (defun ps-face-bold-p (face)
232 (or (ps-xemacs-face-kind-p face 'WEIGHT_NAME "bold\\|demibold")
233 (memq face ps-bold-faces))) ; Kludge-compatible
236 (defun ps-face-italic-p (face)
237 (or (ps-xemacs-face-kind-p face 'ANGLE_NAME "i\\|o")
238 (ps-xemacs-face-kind-p face 'SLANT "i\\|o")
239 (memq face ps-italic-faces))) ; Kludge-compatible
242 (defalias 'ps-face-strikeout-p 'ignore)
245 (defalias 'ps-face-overline-p 'ignore)
248 (defalias 'ps-face-box-p 'ignore)
251 ;; XEmacs will have to make do with %s (princ) for floats.
252 (defvar ps-color-format "%s %s %s")
253 (defvar ps-float-format "%s ")
256 (defun ps-generate-postscript-with-faces1 (from to)
257 ;; Generate some PostScript.
258 (let ((face 'default)
259 (position to)
260 ;; XEmacs
261 ;; Build the list of extents...
262 (a (cons 'dummy nil))
263 record type extent extent-list)
264 (map-extents 'ps-mapper nil from to a)
265 (setq a (sort (cdr a) 'car-less-than-car)
266 extent-list nil)
268 ;; Loop through the extents...
269 (while a
270 (setq record (car a)
271 position (car record)
273 record (cdr record)
274 type (car record)
276 record (cdr record)
277 extent (car record))
279 ;; Plot up to this record.
280 ;; XEmacs 19.12: for some reason, we're getting into a
281 ;; situation in which some of the records have
282 ;; positions less than 'from'. Since we've narrowed
283 ;; the buffer, this'll generate errors. This is a hack,
284 ;; but don't call ps-plot-with-face unless from > point-min.
285 (and (>= from (point-min))
286 (ps-plot-with-face from (min position (point-max)) face))
288 (cond
289 ((eq type 'push)
290 (and (extent-face extent)
291 (setq extent-list (sort (cons extent extent-list)
292 'ps-extent-sorter))))
294 ((eq type 'pull)
295 (setq extent-list (sort (delq extent extent-list)
296 'ps-extent-sorter))))
298 (setq face (if extent-list
299 (extent-face (car extent-list))
300 'default)
301 from position
302 a (cdr a)))
304 (ps-plot-with-face from to face)))
307 (t ; Emacs
308 ;; Do nothing
309 )) ; end cond featurep
313 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
314 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
315 ;; Emacs Definitions
318 (cond
319 ((featurep 'xemacs) ; XEmacs
320 ;; Do nothing
322 (t ; Emacs
325 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326 ;; ps-print
329 (defun ps-mark-active-p ()
330 mark-active)
333 (defun ps-face-foreground-name (face)
334 (face-foreground face nil t))
337 (defun ps-face-background-name (face)
338 (face-background face nil t))
341 (defalias 'ps-frame-parameter 'frame-parameter)
344 ;; Return t if the device (which can be changed during an emacs session) can
345 ;; handle colors. This function is not yet implemented for GNU emacs.
346 (defun ps-color-device ()
347 (if (fboundp 'color-values)
348 (funcall 'color-values "Green")
352 (defun ps-color-values (x-color)
353 (cond
354 ((fboundp 'color-values)
355 (funcall 'color-values x-color))
356 ((fboundp 'x-color-values)
357 (funcall 'x-color-values x-color))
359 (error "No available function to determine X color values"))))
362 (defun ps-face-bold-p (face)
363 (or (face-bold-p face)
364 (memq face ps-bold-faces)))
367 (defun ps-face-italic-p (face)
368 (or (face-italic-p face)
369 (memq face ps-italic-faces)))
372 (defun ps-face-strikeout-p (face)
373 (eq (face-attribute face :strike-through) t))
376 (defun ps-face-overline-p (face)
377 (eq (face-attribute face :overline) t))
380 (defun ps-face-box-p (face)
381 (not (memq (face-attribute face :box) '(nil unspecified))))
384 ;; Emacs understands the %f format; we'll use it to limit color RGB values
385 ;; to three decimals to cut down some on the size of the PostScript output.
386 (defvar ps-color-format "%0.3f %0.3f %0.3f")
387 (defvar ps-float-format "%0.3f ")
390 (defun ps-generate-postscript-with-faces1 (from to)
391 ;; Generate some PostScript.
392 (let ((face 'default)
393 (position to)
394 ;; Emacs
395 (property-change from)
396 (overlay-change from)
397 (save-buffer-invisibility-spec buffer-invisibility-spec)
398 (buffer-invisibility-spec nil)
399 before-string after-string)
400 (while (< from to)
401 (and (< property-change to) ; Don't search for property change
402 ; unless previous search succeeded.
403 (setq property-change (next-property-change from nil to)))
404 (and (< overlay-change to) ; Don't search for overlay change
405 ; unless previous search succeeded.
406 (setq overlay-change (min (next-overlay-change from)
407 to)))
408 (setq position (min property-change overlay-change)
409 before-string nil
410 after-string nil)
411 ;; The code below is not quite correct,
412 ;; because a non-nil overlay invisible property
413 ;; which is inactive according to the current value
414 ;; of buffer-invisibility-spec nonetheless overrides
415 ;; a face text property.
416 (setq face
417 (cond ((let ((prop (get-text-property from 'invisible)))
418 ;; Decide whether this invisible property
419 ;; really makes the text invisible.
420 (if (eq save-buffer-invisibility-spec t)
421 (not (null prop))
422 (or (memq prop save-buffer-invisibility-spec)
423 (assq prop save-buffer-invisibility-spec))))
424 'emacs--invisible--face)
425 ((get-text-property from 'face))
426 (t 'default)))
427 (let ((overlays (overlays-at from))
428 (face-priority -1)) ; text-property
429 (while (and overlays
430 (not (eq face 'emacs--invisible--face)))
431 (let* ((overlay (car overlays))
432 (overlay-invisible
433 (overlay-get overlay 'invisible))
434 (overlay-priority
435 (or (overlay-get overlay 'priority) 0)))
436 (and (> overlay-priority face-priority)
437 (setq before-string
438 (or (overlay-get overlay 'before-string)
439 before-string)
440 after-string
441 (or (and (<= (overlay-end overlay) position)
442 (overlay-get overlay 'after-string))
443 after-string)
444 face-priority overlay-priority
445 face
446 (cond
447 ((if (eq save-buffer-invisibility-spec t)
448 (not (null overlay-invisible))
449 (or (memq overlay-invisible
450 save-buffer-invisibility-spec)
451 (assq overlay-invisible
452 save-buffer-invisibility-spec)))
453 'emacs--invisible--face)
454 ((overlay-get overlay 'face))
455 (t face)
456 ))))
457 (setq overlays (cdr overlays))))
458 ;; Plot up to this record.
459 (and before-string
460 (ps-plot-string before-string))
461 (ps-plot-with-face from position face)
462 (and after-string
463 (ps-plot-string after-string))
464 (setq from position))
465 (ps-plot-with-face from to face)))
467 )) ; end cond featurep
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
472 (provide 'ps-def)
474 ;; arch-tag: 4edde45b-af10-4685-b8ee-7cd0f951095a
475 ;;; ps-def.el ends here