[cairo] problems with text rendering, 1.6.4

Tamas K Papp tpapp at Princeton.EDU
Mon Apr 21 18:03:03 PDT 2008


On Mon, Apr 21, 2008 at 08:47:46PM -0400, Behdad Esfahbod wrote:
> On Mon, 2008-04-21 at 20:32 -0400, Tamas K Papp wrote:
> > 
> > I can provide the example code that generated these (it is in Lisp,
> > but maps into C commands straightforwardly).
> 
> If you can, then just straight do.  We don't read minds!

Sorry, here it is.  It just uses the simple text functions.  In the
functions below, *context* is the default context.

;;;;
;;;; helper functions
;;;;

(defun show-text-aligned (text x y &optional (x-align 0.5) (y-align 0.5)
			  (context *context*))
  "Show text aligned relative to (x,y)."
  (let ((*context* context))
    (multiple-value-bind (x-bearing y-bearing width height)
	(text-extents text)
      (move-to (- x (* width x-align) x-bearing)
	       (- y (* height y-align) y-bearing))
      (show-text text))))

;;;;
;;;;  text placement example
;;;;
;;;;  This example demonstrates the use of text-extents, by placing
;;;;  text aligned relative to a red marker.

(defun mark-at (x y d red green blue)
  "Make a rectangle of size 2d around x y with the given colors,
  50% alpha.  Used for marking points."
  (rectangle (- x d) (- y d) (* 2 d) (* 2 d))
  (set-source-rgba red green blue 0.5)
  (fill-path))

(defun show-text-with-marker (text x y x-align y-align)
  "Show text aligned relative to a red market at (x,y)."
  (mark-at x y 2 1 0 0)
  (set-source-rgba 0 0 0 0.6)
  (show-text-aligned text x y x-align y-align))

(defparameter width 500)
(defparameter height 500)
(defparameter text "Fog")		; contains g, which goes below baseline
(defparameter size 50)
(defparameter x 20)
(defparameter y 50)
(setf *context* (create-ps-context "text.ps" width height))
;; white background
(set-source-rgb 1 1 1)
(paint)
;; setup font
(select-font-face "Arial" 'font-slant-normal 'font-weight-normal)
(set-font-size size)
;; starting point
(mark-at x y 2 1 0 0)			; red
;; first text in a box
(multiple-value-bind (x-bearing y-bearing text-width text-height)
    (text-extents text)
  (let ((rect-x (+ x x-bearing))
	(rect-y (+ y y-bearing)))
    (rectangle rect-x rect-y text-width text-height)
    (set-source-rgba 0 0 1 0.3)		; blue
    (set-line-width 1)
    (set-dash 0 '(5 5))
    (stroke)))
(set-source-rgba 0 0 0 0.6)
(move-to x y)
(show-text text)
;; text automatically aligned
(dolist (x-align '(0 0.5 1))
  (dolist (y-align '(0 0.5 1))
    (show-text-with-marker text (+ x (* x-align 300)) (+ y (* y-align 300) 100)
			   x-align y-align)))
;; done
(destroy *context*)

Strangely enough, I haven't been able to get this with a simpler
example, if I remove the double loop at the end, destroy just works
correctly.

Tamas


More information about the cairo mailing list