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

Re: string-width error?

Thanks for advice for everybody. The width problem was endeed caused
by string exceeding the build-in Mac OS limit, which was undocumented 
in the MCL manual.

Natural-size works fine when the text to be sized contains newlines at 
the end of each row. But the string I'm sizing is "a very long string 
without newlines". Its width must be calculated in chunks. Here is the 
unlimited length version of string-width.

(defun string-width-unlimited (string font)
  (let* ((strlen (length string))
         (buffsize 255)
         (total (string-width 
                 (subseq string (- strlen (mod strlen buffsize)) strlen)
         (startpos 0))
    (dotimes (i (truncate (/ strlen buffsize)))
      (setq startpos (* i buffsize))
      (setq total (+ total (string-width 
                            (subseq string startpos (+ startpos buffsize))

Text item can now be sized to match a given horisontal width, here 400. 
Note that 370 approximates the shorter lines due wrapping when the text
is to be shown with wilma mixin. 

(defun wrapped-text-item-size (string font)
   (make-point 400
               (* (line-heigth font)
                  (truncate (1+ (/ (string-width-unlimited string font)

(defun line-heigth (font-spec)
  (let (ascent descent maxwidth leading)
    (multiple-value-setq (ascent descent maxwidth leading)
                         (font-info font-spec))
    (+ ascent descent leading)))