René Nyffenegger's collection of things on the web | |
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
|
Script-FU example 13: Left-right justifying text | ||
Justifies the passed text to 250 pixels.
The following helper function returns the width of the passed string (for the passed font and font-size):
get_text_width.scm
(define (get-text-width text font-name font-size) (let* ( (text-width) (text-extents) ) (set! text-extents (gimp-text-get-extents-fontname text font-size PIXELS font-name)) (set! text-width (car text-extents)) ; return the width of the text text-width ) ) ex_13.scm
(define (ex_13 text) (let* ( (nof-chars (string-length text)) (cur-pos 0) (cur-letter) (width 250) (my-image (car (gimp-image-new width width RGB ))) (my-layer (car (gimp-layer-new my-image width width RGB-IMAGE "my layer" 100 NORMAL))) (font-name "Verdana") (font-size 24) ;total-text-width is the width of the text if it were not justified to width (total-text-width (get-text-width text font-name font-size)) (space-between-letters (/ (- width total-text-width) (- nof-chars 1))) (x-pos-next-letter 0) (char-layer) ) (gimp-image-add-layer my-image my-layer 0) (gimp-context-set-background '(123 231 012)) (gimp-context-set-foreground '(012 123 231)) (gimp-drawable-fill my-layer BACKGROUND-FILL) (while (< cur-pos nof-chars) (set! cur-letter (substring text cur-pos (+ cur-pos 1))) (set! char-layer (car (gimp-text-fontname my-image my-layer x-pos-next-letter 10 cur-letter 0 TRUE font-size PIXELS font-name))) (gimp-floating-sel-anchor char-layer) (set! x-pos-next-letter (+ x-pos-next-letter space-between-letters (get-text-width cur-letter font-name font-size))) (set! cur-pos (+ cur-pos 1)) ) ; (gimp-display-new my-image) (gimp-file-save RUN-NONINTERACTIVE my-image my-layer "c:\\temp\\ex_13.jpg" "") ) )
For some reason, spaces don't work, therefore, I use a hyphen.
c:\> start gimp-2.2.exe -d -i -b "(ex_13 \"Hello-World\")" "(gimp-quit 0)"
See also other examples
|