[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string-width error?



Try something like this.

(defun natural-size (text &optional (font (view-font (front-window))))
  "Computes size of box needed to display text in font."
  ;; number of lines is number of Newlines + 1, except we don't count a
  ;; Newline at the end of the text.  We also discount the leading after the
  ;; last line.
  (if (string= text "") #@(0 0)
      (multiple-value-bind (ascent descent maxwidth leading)
                           (font-info font)
        (let ((line-height (+ ascent descent leading))
              (maxlinew 0)
              (nlines 0)
              (ll (length text)))
          (let ((l (if (char= #\Newline (char text (1- ll)))
                     (1- ll)
                     ll)))
            (do ((i (position #\Newline text :end l)
                    (and i (position #\Newline text :start (1+ i) :end l)))
                 (j 0 (and i (1+ i))))
                ((null j) (make-point (if (memq ':italic font)
                                        ;; italic correction
                                        (+ maxlinew (floor maxwidth 4))
                                        maxlinew)
                                      (- (* nlines line-height)
                                         (if (plusp nlines) leading 0))))
              (setq maxlinew
                    (max maxlinew
                         (string-width (subseq text j (or i l)) font)))
              (incf nlines)))))))