File: pdf-patches.lisp

package info (click to toggle)
cl-pdf 166-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,520 kB
  • ctags: 639
  • sloc: lisp: 6,902; makefile: 39
file content (68 lines) | stat: -rw-r--r-- 2,307 bytes parent folder | download | duplicates (2)
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
(in-package :pdf)

;;; these patches permit operation without libgz
;;; [for example, on a PC/ms windows]


;;; --------------------------------------------------
;;; for PDF.lisp:

(defmethod write-object ((obj pdf-stream) &optional root-level)
  #+(or Lispworks Allegro)
  (when (and (> (length (content obj)) +min-size-for-compression+)
	     *libgz-loaded*)
    (setf (content obj) (compress-string (content obj)))
    (let ((filter (get-dict-value obj "/Filter")))
      (if filter
	(change-dict-value obj "/Filter" (vector "/FlateDecode" filter))
	(push (cons "/Filter" "/FlateDecode")(dict-values obj)))))
  (call-next-method)
  (write-line "stream" *pdf-stream*)
  (write-sequence (content obj) *pdf-stream*)
  (write-char #\Newline *pdf-stream*)
  (write-line "endstream" *pdf-stream*))

;;; --------------------------------------------------
;;; For cl-zlib-small.lisp

;;; This variable may remain nil, if the load fails

(defvar *libgz-loaded* nil)

(defun load-libgz ()
  (restart-case
      (handler-bind
	  ((error 
	    #'(lambda (cond)
		(format *debug-io* "The libz.so file can't be loaded from ~s~%" *libgz-path*)
		(format *debug-io* "Correct *libgz-path* and retry" *libgz-path*)
		(invoke-debugger cond))))
	(let* ((result (load *libgz-path*)))  ;; result of successful load is non-nil
	  (setq *libgz-loaded* (if result t))))
    (skip-loading ()
	:report "Skip using libz, pdfs will not be compressed"
      (return-from load-libgz nil))
    )
  t)

(eval-when (:load-toplevel)
  (unless *libgz-loaded*
    (load-libgz)))

;;; note: if the libz library is not available, this just returns the source string

(defun compress-string (source)
  "Compress the string SOURCE. Returns two values: the array of bytes
representing the compressed data and the number of compressed bytes."
  (if *libgz-loaded*
      (let* ((sourcelen (length source))
	     (destsize (+ 12 (ceiling (* sourcelen 1.05))))
	     (dest (make-array destsize :element-type '(unsigned-byte 8)
			       :initial-element 0))
	     (destlen (make-array 1 :element-type '(unsigned-byte 32)
				  :initial-element destsize)))
	(let ((res (gz-string dest destlen source sourcelen)))
	  (if (zerop res)
	      (values dest (aref destlen 0))
	    (error "zlib error, code ~d" res))))
    (values source (length source))))