+(defconst ps-mule-prologue
+ "%%%% Start of Mule Section
+
+%% Working dictionaly for general use.
+/MuleDict 10 dict def
+
+%% Define already scaled font for non-ASCII character sets.
+/DefFontMule { % fontname size basefont |- --
+ findfont exch scalefont definefont pop
+} bind def
+
+%% Define already scaled font for ASCII character sets.
+/DefAsciiFontMule { % fontname size basefont |-
+ MuleDict begin
+ findfont dup /Encoding get /ISOLatin1Encoding exch def
+ exch scalefont reencodeFontISO
+ end
+} def
+
+%% Set the specified non-ASCII font to use. It doesn't install
+%% Ascent, etc.
+/FM { % fontname |- --
+ findfont setfont
+} bind def
+
+%% Show vacant box for characters which don't have appropriate font.
+/SB { % count column |- --
+ SpaceWidth mul /w exch def
+ 1 exch 1 exch { %for
+ pop
+ gsave
+ 0 setlinewidth
+ 0 Descent rmoveto w 0 rlineto
+ 0 LineHeight rlineto w neg 0 rlineto closepath stroke
+ grestore
+ w 0 rmoveto
+ } for
+} bind def
+
+%% Flag to tell if we are now handling a composite character. This is
+%% defined here because both composite character handler and bitmap font
+%% handler require it.
+/Cmpchar false def
+
+%%%% End of Mule Section
+
+"
+ "PostScript code for printing multibyte characters.")
+
+(defun ps-mule-skip-same-charset (charset)
+ "Skip characters of CHARSET following the current point."
+ (while (eq (charset-after) charset) (forward-char 1)))
+
+(defun ps-mule-find-wrappoint (from to char-width)
+ "Find a longest sequence at FROM which is printable in the current line.
+
+TO limits the sequence. It is assumed that all characters between
+FROM and TO belong to a charset set in `ps-mule-current-charset'.
+
+CHAR-WIDTH is an average width of ASCII characters in the current font.
+
+The return value is a cons of ENDPOS and RUN-WIDTH, where
+ENDPOS is an end position of the sequence,
+RUN-WIDTH is the width of the sequence."
+ (let (run-width)
+ (if (eq ps-mule-current-charset 'composition)
+ ;; We must draw one char by one.
+ (let ((ch (char-after from)))
+ (setq run-width (* (char-width ch) char-width))
+ (if (> run-width ps-width-remaining)
+ (setq run-width ps-width-remaining)
+ (setq from (ps-mule-next-point from))))
+ ;; We assume that all characters in this range have the same width.
+ (let ((width (charset-width ps-mule-current-charset)))
+ (setq run-width (* (- to from) char-width width))
+ (if (> run-width ps-width-remaining)
+ (setq from (min
+ (+ from (truncate (/ ps-width-remaining char-width)))
+ to)
+ run-width ps-width-remaining)
+ (setq from to))))
+ (cons from run-width)))
+
+(defun ps-mule-plot-string (from to &optional bg-color)
+ "Generate PostScript code for ploting characters in the region FROM and TO.
+It is assumed that all characters in this region belong to the
+charset `ps-mule-current-charset'.
+Optional arg BG-COLOR specifies background color.
+The return value is a cons of ENDPOS and WIDTH of the sequence
+actually plotted by this function."
+ (let* ((wrappoint (ps-mule-find-wrappoint
+ from to (ps-avg-char-width 'ps-font-for-text)))
+ (to (car wrappoint))
+ (font-type (car (nth ps-current-font
+ (ps-font-alist 'ps-font-for-text))))
+ (font-spec (ps-mule-get-font-spec ps-mule-current-charset font-type))
+ (encoding (ps-mule-font-spec-encoding font-spec))
+ (string (buffer-substring-no-properties from to)))
+ (cond
+ ((= from to)
+ ;; We can't print any more characters in the current line.
+ nil)
+
+ (font-spec
+ ;; We surely have a font for printing this character set.
+ (if (coding-system-p encoding)
+ (setq string (encode-coding-string string encoding))
+ (if (functionp encoding)
+ (setq string (funcall encoding string))
+ (if encoding
+ (error "Invalid coding system or function: %s" encoding))))
+ (setq string (string-as-unibyte string))
+ (if (ps-mule-font-spec-src font-spec)
+ (ps-mule-prepare-font font-spec string ps-mule-current-charset)
+ (ps-set-font ps-current-font))
+ (ps-output-string string)
+ (ps-output " S\n"))
+
+ ((eq ps-mule-current-charset 'latin-iso8859-1)
+ ;; Latin-1 can be printed by a normal ASCII font.
+ (ps-set-font ps-current-font)
+ (ps-output-string
+ (string-as-unibyte (encode-coding-string string 'iso-latin-1)))
+ (ps-output " S\n"))
+
+ ((eq ps-mule-current-charset 'composition)
+ (let* ((ch (char-after from))
+ (width (char-width ch))
+ (ch-list (decompose-composite-char ch 'list t)))
+ (if (consp (nth 1 ch-list))
+ (ps-mule-plot-rule-cmpchar ch-list width font-type)
+ (ps-mule-plot-cmpchar ch-list width t font-type))))
+
+ (t
+ ;; No way to print this charset. Just show a vacant box of an
+ ;; appropriate width.
+ (ps-output (format "%d %d SB\n"
+ (length string)
+ (if (eq ps-mule-current-charset 'composition)
+ (char-width (char-after from))
+ (charset-width ps-mule-current-charset))))))
+ wrappoint))
+
+;; Composite font support
+
+(defvar ps-mule-cmpchar-prologue-generated nil)
+
+(defconst ps-mule-cmpchar-prologue
+ "%%%% Composite character handler
+/CmpcharWidth 0 def
+/CmpcharRelativeCompose 0 def
+/CmpcharRelativeSkip 0.4 def
+
+%% Get a bounding box (relative to currentpoint) of STR.
+/GetPathBox { % str |- --
+ gsave
+ currentfont /FontType get 3 eq { %ifelse
+ stringwidth pop pop
+ } {
+ currentpoint /y exch def pop
+ false charpath flattenpath pathbbox
+ y sub /URY exch def pop
+ y sub /LLY exch def pop
+ } ifelse
+ grestore
+} bind def
+
+%% Beginning of composite char.
+/BC { % str xoff width |- --
+ /Cmpchar true def
+ /CmpcharWidth exch def
+ currentfont /RelativeCompose known {
+ /CmpcharRelativeCompose currentfont /RelativeCompose get def
+ } {
+ /CmpcharRelativeCompose false def
+ } ifelse
+ /bgsave bg def /bgcolorsave bgcolor def
+ /Effectsave Effect def
+ gsave % Reflect effect only at first
+ /Effect Effect 1 2 add 4 add 16 add and def
+ /f0 findfont setfont ( ) 0 CmpcharWidth getinterval S
+ grestore
+ /Effect Effectsave 8 32 add and def % enable only shadow and outline
+ false BG
+ gsave SpaceWidth mul 0 rmoveto dup GetPathBox S grestore
+ /y currentpoint exch pop def
+ /HIGH URY y add def /LOW LLY y add def
+} bind def
+
+%% End of composite char.
+/EC { % -- |- --
+ /bg bgsave def /bgcolor bgcolorsave def
+ /Effect Effectsave def
+ /Cmpchar false def
+ CmpcharWidth SpaceWidth mul 0 rmoveto
+} bind def
+
+%% Rule base composition
+/RBC { % str xoff gref nref |- --
+ /nref exch def /gref exch def
+ gsave
+ SpaceWidth mul 0 rmoveto
+ dup
+ GetPathBox
+ [ HIGH currentpoint exch pop LOW HIGH LOW add 2 div ] gref get
+ [ URY LLY sub LLY neg 0 URY LLY sub 2 div ] nref get
+ sub /btm exch def
+ /top btm URY LLY sub add def
+ top HIGH gt { /HIGH top def } if
+ btm LOW lt { /LOW btm def } if
+ currentpoint pop btm LLY sub moveto
+ S
+ grestore
+} bind def
+
+%% Relative composition
+/RLC { % str |- --
+ gsave
+ dup GetPathBox
+ CmpcharRelativeCompose type /integertype eq {
+ LLY CmpcharRelativeCompose gt { % compose on top
+ currentpoint pop HIGH LLY sub CmpcharRelativeSkip add moveto
+ /HIGH HIGH URY LLY sub add CmpcharRelativeSkip add def
+ } { URY 0 le { % compose under bottom
+ currentpoint pop LOW LLY add CmpcharRelativeSkip sub moveto
+ /LOW LOW URY LLY sub sub CmpcharRelativeSkip sub def
+ } if } ifelse } if
+ S
+ grestore
+} bind def
+%%%% End of composite character handler
+
+"
+ "PostScript code for printing composite characters.")
+
+(defun ps-mule-plot-rule-cmpchar (ch-rule-list total-width font-type)
+ (let* ((leftmost 0.0)
+ (rightmost (float (char-width (car ch-rule-list))))
+ (l (cons '(3 . 3) ch-rule-list))
+ (cmpchar-elements nil))
+ (while l
+ (let* ((this (car l))
+ (gref (car this))
+ (nref (cdr this))
+ ;; X-axis info (0:left, 1:center, 2:right)
+ (gref-x (% gref 3))
+ (nref-x (% nref 3))
+ ;; Y-axis info (0:top, 1:base, 2:bottom, 3:center)
+ (gref-y (if (= gref 4) 3 (/ gref 3)))
+ (nref-y (if (= nref 4) 3 (/ nref 3)))
+ (width (float (char-width (car (cdr l)))))
+ left)
+ (setq left (+ leftmost
+ (/ (* (- rightmost leftmost) gref-x) 2.0)
+ (- (/ (* nref-x width) 2.0))))
+ (setq cmpchar-elements
+ (cons (list (car (cdr l)) left gref-y nref-y) cmpchar-elements))
+ (if (< left leftmost)
+ (setq leftmost left))
+ (if (> (+ left width) rightmost)
+ (setq rightmost (+ left width)))
+ (setq l (nthcdr 2 l))))
+ (if (< leftmost 0)
+ (let ((l cmpchar-elements))
+ (while l
+ (setcar (cdr (car l))
+ (- (nth 1 (car l)) leftmost))
+ (setq l (cdr l)))))
+ (ps-mule-plot-cmpchar (nreverse cmpchar-elements)
+ total-width nil font-type)))
+
+(defun ps-mule-plot-cmpchar (elements total-width relativep font-type)
+ (let* ((ch (if relativep (car elements) (car (car elements))))
+ (str (ps-mule-prepare-cmpchar-font ch font-type)))
+ (ps-output-string str)
+ (ps-output (format " %d %d BC "
+ (if relativep 0 (nth 1 (car elements)))
+ total-width)))
+ (setq elements (cdr elements))
+ (while elements
+ (let* ((elt (car elements))
+ (ch (if relativep elt (car elt)))
+ (str (ps-mule-prepare-cmpchar-font ch font-type)))
+ (if relativep
+ (progn
+ (ps-output-string str)
+ (ps-output " RLC "))
+ (ps-output-string str)
+ (ps-output (format " %d %d %d RBC "
+ (nth 1 elt) (nth 2 elt) (nth 3 elt)))))
+ (setq elements (cdr elements)))
+ (ps-output "EC\n"))
+
+(defun ps-mule-prepare-cmpchar-font (char font-type)
+ (let* ((ps-mule-current-charset (char-charset char))
+ (font-spec (ps-mule-get-font-spec ps-mule-current-charset font-type))
+ (encoding (ps-mule-font-spec-encoding font-spec))
+ (str (char-to-string char)))
+ (cond (font-spec
+ (if (coding-system-p encoding)
+ (setq str (encode-coding-string str encoding))
+ (if (functionp encoding)
+ (setq str (funcall encoding str))
+ (if encoding
+ (error "Invalid coding system or function: %s" encoding))))
+ (setq str (string-as-unibyte str))
+ (if (ps-mule-font-spec-src font-spec)
+ (ps-mule-prepare-font font-spec str ps-mule-current-charset)
+ (ps-set-font ps-current-font)))
+
+ ((eq ps-mule-current-charset 'latin-iso8859-1)
+ (ps-set-font ps-current-font)
+ (setq str
+ (string-as-unibyte (encode-coding-string str 'iso-latin-1))))
+
+ (t
+ ;; No font for CHAR.
+ (ps-set-font ps-current-font)
+ (setq str " ")))
+ str))
+
+;; Bitmap font support
+
+(defvar ps-mule-bitmap-prologue-generated nil)
+
+(defconst ps-mule-bitmap-prologue
+ "%%%% Bitmap font handler
+
+/str7 7 string def % working area
+
+%% We grow the dictionary one bunch (1024 entries) by one.
+/BitmapDictArray 256 array def
+/BitmapDictLength 1024 def
+/BitmapDictIndex -1 def
+
+/NewBitmapDict { % -- |- --
+ /BitmapDictIndex BitmapDictIndex 1 add def
+ BitmapDictArray BitmapDictIndex BitmapDictLength dict put
+} bind def
+
+%% Make at least one dictionary.
+NewBitmapDict
+
+/AddBitmap { % gloval-charname bitmap-data |- --
+ BitmapDictArray BitmapDictIndex get
+ dup length BitmapDictLength ge {
+ pop
+ NewBitmapDict
+ BitmapDictArray BitmapDictIndex get
+ } if
+ 3 1 roll put
+} bind def
+
+/GetBitmap { % gloval-charname |- bitmap-data
+ 0 1 BitmapDictIndex { BitmapDictArray exch get begin } for
+ load
+ 0 1 BitmapDictIndex { pop end } for
+} bind def
+
+%% Return a global character name which can be used as a key in the
+%% bitmap dictionary.
+/GlobalCharName { % fontidx code1 code2 |- gloval-charname
+ exch 256 mul add exch 65536 mul add 16777216 add 16 str7 cvrs 0 66 put
+ str7 cvn
+} bind def
+
+%% Character code holder for a 2-byte character.
+/FirstCode -1 def
+
+%% Glyph rendering procedure
+/BuildGlyphCommon { % fontdict charname |- --
+ 1 index /FontDimension get 1 eq { /FirstCode 0 store } if
+ NameIndexDict exch get % STACK: fontdict charcode
+ FirstCode 0 lt { %ifelse
+ %% This is the first byte of a 2-byte character. Just
+ %% remember it for the moment.
+ /FirstCode exch store
+ pop
+ 0 0 setcharwidth
+ } {
+ 1 index /FontSize get /size exch def
+ 1 index /FontSpaceWidthRatio get /ratio exch def
+ 1 index /FontIndex get exch FirstCode exch
+ GlobalCharName GetBitmap /bmp exch def
+ %% bmp == [ DWIDTH BBX-WIDTH BBX-HEIGHT BBX-XOFF BBX-YOFF BITMAP ]
+ Cmpchar { %ifelse
+ /FontMatrix get [ exch { size div } forall ] /mtrx exch def
+ bmp 3 get bmp 4 get mtrx transform
+ /LLY exch def pop
+ bmp 1 get bmp 3 get add bmp 2 get bmp 4 get add mtrx transform
+ /URY exch def pop
+ } {
+ pop
+ } ifelse
+ /FirstCode -1 store
+
+ bmp 0 get SpaceWidthRatio ratio div mul size div 0 % wx wy
+ setcharwidth % We can't use setcachedevice here.
+
+ bmp 1 get 0 gt bmp 2 get 0 gt and {
+ bmp 1 get bmp 2 get % width height
+ true % polarity
+ [ size 0 0 size neg bmp 3 get neg bmp 2 get bmp 4 get add ] % matrix
+ bmp 5 1 getinterval cvx % datasrc
+ imagemask
+ } if
+ } ifelse
+} bind def
+
+/BuildCharCommon {
+ 1 index /Encoding get exch get
+ 1 index /BuildGlyph get exec
+} bind def
+
+%% Bitmap font creater
+
+%% Common Encoding shared by all bitmap fonts.
+/EncodingCommon 256 array def
+%% Mapping table from character name to character code.
+/NameIndexDict 256 dict def
+0 1 255 { %for
+ /idx exch def
+ /idxname idx 256 add 16 (XXX) cvrs dup 0 67 put cvn def % `C' == 67
+ EncodingCommon idx idxname put
+ NameIndexDict idxname idx put
+} for
+
+/GlobalFontIndex 0 def
+
+%% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
+/BitmapFont {
+ 15 dict begin
+ /FontBBox exch def
+ /BaselineOffset exch def
+ /RelativeCompose exch def
+ /FontSize exch def
+ /FontBBox [ FontBBox { FontSize div } forall ] def
+ FontBBox 2 get FontBBox 0 get sub exch div
+ /FontSpaceWidthRatio exch def
+ /FontDimension exch def
+ /FontIndex GlobalFontIndex def
+ /FontType 3 def
+ /FontMatrix matrix def
+ /Encoding EncodingCommon def
+ /BuildGlyph { BuildGlyphCommon } def
+ /BuildChar { BuildCharCommon } def
+ currentdict end
+ definefont pop
+ /GlobalFontIndex GlobalFontIndex 1 add def
+} bind def
+
+%% Define a new bitmap font.
+%% fontname dim col fontsize relative-compose baseline-offset fbbx |- --
+/NF {
+ /fbbx exch def
+ %% Convert BDF's FontBoundingBox to PostScript's FontBBox
+ [ fbbx 2 get fbbx 3 get
+ fbbx 2 get fbbx 0 get add fbbx 3 get fbbx 1 get add ]
+ BitmapFont
+} bind def
+
+%% Define a glyph for the specified font and character.
+/NG { % fontname charcode bitmap-data |- --
+ /bmp exch def
+ exch findfont dup /BaselineOffset get bmp 4 get add bmp exch 4 exch put
+ /FontIndex get exch
+ dup 256 idiv exch 256 mod GlobalCharName
+ bmp AddBitmap
+} bind def
+%%%% End of bitmap font handler
+
+")
+
+;; External library support.
+
+;; The following three functions are to be called from external
+;; libraries which support bitmap fonts (e.g. `bdf') to get
+;; appropriate PostScript code.
+
+(defun ps-mule-generate-bitmap-prologue ()
+ (unless ps-mule-bitmap-prologue-generated
+ (setq ps-mule-bitmap-prologue-generated t)
+ (list ps-mule-bitmap-prologue)))
+
+(defun ps-mule-generate-bitmap-font (&rest args)
+ (list (apply 'format "/%s %d %d %f %S %d %S NF\n" args)))
+
+(defun ps-mule-generate-bitmap-glyph (font-name code dwidth bbx bitmap)
+ (format "/%s %d [ %d %d %d %d %d <%s> ] NG\n"
+ font-name code
+ dwidth (aref bbx 0) (aref bbx 1) (aref bbx 2) (aref bbx 3)
+ bitmap))
+
+;; Mule specific initializers.
+
+(defun ps-mule-initialize ()
+ "Produce Poscript code in the prologue part for multibyte characters."
+ (setq ps-mule-current-charset 'ascii
+ ps-mule-font-cache nil
+ ps-mule-prologue-generated nil
+ ps-mule-cmpchar-prologue-generated nil
+ ps-mule-bitmap-prologue-generated nil)
+ (mapcar (function (lambda (x) (setcar (cdr x) nil)))
+ ps-mule-external-libraries))
+
+(defun ps-mule-begin (from to)
+ (if (and (boundp 'enable-multibyte-characters)
+ enable-multibyte-characters)
+ ;; Initialize `ps-mule-charset-list'. If some characters aren't
+ ;; printable, warn it.
+ (let ((charsets (delete 'ascii (find-charset-region from to))))
+ (setq ps-mule-charset-list charsets)
+ (save-excursion
+ (goto-char from)
+ (if (search-forward "\200" to t)
+ (setq ps-mule-charset-list
+ (cons 'composition ps-mule-charset-list))))
+ (if (and (catch 'tag
+ (while charsets
+ (if (or (eq (car charsets) 'composition)
+ (ps-mule-printable-p (car charsets)))
+ (setq charsets (cdr charsets))
+ (throw 'tag t))))
+ (not (y-or-n-p "Font for some characters not found, continue anyway? ")))
+ (error "Printing cancelled"))))
+
+ (if ps-mule-charset-list
+ (let ((l ps-mule-charset-list)
+ font-spec)
+ (unless ps-mule-prologue-generated
+ (ps-output-prologue ps-mule-prologue)
+ (setq ps-mule-prologue-generated t))
+ ;; If external functions are necessary, generate prologues for them.
+ (while l
+ (if (and (eq (car l) 'composition)
+ (not ps-mule-cmpchar-prologue-generated))
+ (progn
+ (ps-output-prologue ps-mule-cmpchar-prologue)
+ (setq ps-mule-cmpchar-prologue-generated t))
+ (if (setq font-spec (ps-mule-get-font-spec (car l) 'normal))
+ (ps-mule-init-external-library font-spec)))
+ (setq l (cdr l)))))
+
+ ;; If ASCII font is also specified in ps-mule-font-info-database,
+ ;; use it istead of what specified in ps-font-info-database.
+ (let ((font-spec (ps-mule-get-font-spec 'ascii 'normal)))
+ (if font-spec
+ (progn
+ (unless ps-mule-prologue-generated
+ (ps-output-prologue ps-mule-prologue)
+ (setq ps-mule-prologue-generated t))
+ (ps-mule-init-external-library font-spec)
+ (let ((font (ps-font-alist 'ps-font-for-text))
+ (i 0))
+ (while font
+ (let ((ps-current-font i))
+ ;; Be sure to download a glyph for SPACE in advance.
+ (ps-mule-prepare-font
+ (ps-mule-get-font-spec 'ascii (car font))
+ " " 'ascii 'no-setfont))
+ (setq font (cdr font) i (1+ i))))))))