Previous: , Up: Glyphs   [Contents][Index]


50.5 Glyph Examples

For many applications, displaying graphics is a simple process: you create a glyph, and then you insert it into a buffer.

The easiest way to create a glyph is to use a file that contains a graphical image, such as a JPEG, TIFF, or PNG file:

;; Create a glyph from a JPEG file:
(setq foo (make-glyph [jpeg :file "/tmp/file1.jpg"]))
;; Create a glyph from a XPM file:
(setq foo (make-glyph [xpm :file "/tmp/file2.xpm"]))
;; Create a glyph from a PNG file:
(setq foo (make-glyph [png :file "/tmp/file3.png"]))
;; Create a glyph from a TIFF file:
(setq foo (make-glyph [tiff :file "/tmp/file4.tiff"]))

The parameters passed to make-glyph are called "Image Specifiers", and can handle more image types than those shown above. You can also put the raw image data into a string (e.g., if you put the contents of a JPEG file into a string), and use that to create a glyph. See Image Specifiers, for more information.

Caution: In order for SXEmacs to read a particular graphics file format, support for that format must have been compiled into SXEmacs. It’s possible, although somewhat unlikely, for SXEmacs to have been compiled without support for any of the various graphics file formats. To see what graphics formats your particular version of SXEmacs supports, use M-x describe-installation.

To programmatically query whether or not a particular file format is supported, you can use the featurep function, with one of: gif, tiff, jpeg, xpm, xbm, png, or xface. For an up-to-date list, Image Specifiers. Example:

;; Returns `t' if TIFF is supported:
(featurep 'tiff)

Another example is:

;; Returns a list of `t' or `nil', depending on whether or not the
;; corresponding feature is supported:
(mapcar #'(lambda (format-symbol) (featurep format-symbol))
        '(gif tiff jpeg xpm png))

Once you have a glyph, you can then insert it into a buffer. Example:

;; Use this function to insert a glyph at the left edge of point in the
;; current buffer.  Any existing glyph at this location is replaced.
(defun insert-glyph (gl)
  "Insert a glyph at the left edge of point."
  (let ( (prop 'myimage)        ;; myimage is an arbitrary name, chosen
                                ;; to (hopefully) not conflict with any
                                ;; other properties.  Change it if
                                ;; necessary.
         extent )
    ;; First, check to see if one of our extents already exists at
    ;; point.  For ease-of-programming, we are creating and using our
    ;; own extents (multiple extents are allowed to exist/overlap at the
    ;; same point, and it's quite possible for other applications to
    ;; embed extents in the current buffer without your knowledge).
    ;; Basically, if an extent, with the property stored in "prop",
    ;; exists at point, we assume that it is one of ours, and we re-use
    ;; it (this is why it is important for the property stored in "prop"
    ;; to be unique, and only used by us).
    (if (not (setq extent (extent-at (point) (current-buffer) prop)))
      (progn
        ;; If an extent does not already exist, create a zero-length
        ;; extent, and give it our special property.
        (setq extent (make-extent (point) (point) (current-buffer)))
        (set-extent-property extent prop t)
        ))
    ;; Display the glyph by storing it as the extent's "begin-glyph".
    (set-extent-property extent 'begin-glyph gl)
    ))

;; You can then use this function like:
(insert-glyph (make-glyph [jpeg :file "/tmp/file1.jpg"]))
;; This will insert the glyph at point.

;; Here's an example of how to insert two glyphs side-by-side, at point
;; (using the above code):
(progn
  (insert-glyph (make-glyph [jpeg :file "/tmp/file1.jpg"]))
  ;; Create a new extent at point.  We can't simply call "insert-glyph",
  ;; as "insert-glyph" will simply replace the first glyph with the
  ;; second.
  (setq extent (make-extent (point) (point) (current-buffer)))
  ;; Here, we're only setting the 'myimage property in case we need
  ;; to later identify/locate/reuse this particular extent.
  (set-extent-property extent 'myimage t)
  (set-extent-property extent 'begin-glyph
                       (make-glyph [jpeg :file "/tmp/file2.jpg"]))
  )

Here are the gory details:


Previous: , Up: Glyphs   [Contents][Index]