Quoting Kevin <for...@gimpusers.com>:

> I'm trying to write a script to create text layers using the currently
> selected font.
> :
> :
> Is it possible to get the current font size, or to create a text   
> layer without having to specify a font size?

Not to my knowledge.

If your ultimate goal is to fit text within a certain area -- for  
example sizing a label to fit on a button -- then you might find the  
following function useful for determining your font size. It's not  
pretty (but it works).

The fontsize% argument is a percentage value that allows you to  
effectively specify a "padding" around the text. For example, 100%  
means determine the largest fontsize possible such that the text will  
fit within the boundaries. Note that most fonts already include some  
padding around the characters (so you might find a value larger than  
100 desirable).

Example: (set! fontsize (calc-fontsize "Hello" "Sans" 80 60 20))

   (define (calc-fontsize text font fontsize% width height)
     (let* (
         (fontsize 6) ;; minimum possible fontsize
         (extents nil)
         (last-extents nil)
         (last-fontsize 3)
         (adjust 2)
       (set! extents (gimp-text-get-extents-fontname text fontsize  
PIXELS font))
       (set! width (* width fontsize% 0.01))
       (set! height (* height fontsize% 0.01))
       (while (and (<> last-fontsize fontsize) (not (equal? extents  
         (if (or (> (car extents) width) (> (cadr extents) height))
             (set! fontsize last-fontsize)
             (set! adjust (+ (* (- adjust 1) 0.5) 1))
             (set! last-extents extents)
             (set! last-fontsize fontsize)
         (set! fontsize (truncate (* fontsize adjust)))
         (set! extents (gimp-text-get-extents-fontname text fontsize  
PIXELS font))
       (max fontsize 6)

Gimp-user mailing list

Reply via email to