summaryrefslogtreecommitdiff
path: root/3.0/przem-add-background-under-text-v3.scm
blob: 943e00ceb1ad5d0533cb6c11854fc6571ab58c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
; przem-add-background-under-text-v3 - Script-fu dodający podpis z tłem
; author: PRP
; licencja: Public Domain
; Gdańsk - 06-02-2025
; ver. 2.250215-1~alpha for Gimp 3.0

(define (przem-add-background-under-text-v3 image drawable text font font-size fg-color bg-color background-transparency opacity)

  (gimp-image-undo-group-start image)

  (let* (
        (image-width (car (gimp-image-get-width image)))
        (image-height (car (gimp-image-get-height image)))
        ;(text-extents (gimp-text-get-extents-fontname text font-size 0 font))
        (text-extents (gimp-text-get-extents-font text font-size font))
        (text-width (car text-extents))
        (text-height (cadr text-extents))
        (pos-x (- (- image-width text-width) 40))
        (pos-y (- image-height text-height))
        (bg-x-left (- pos-x 10))
        (bg-x-right (+ text-width 10))
        ;(text-layer (car (gimp-text-fontname image -1 0 0 text 0 TRUE font-size 0 font)))
        (text-layer (car(gimp-text-font image -1 0 0 text 0 TRUE font-size font)))
        (bg-layer (car (gimp-layer-new image "Background" bg-x-right text-height RGB-IMAGE  100 LAYER-MODE-NORMAL)))
        ;(is-nocolorful (car (gimp-drawable-is-gray drawable))) ; czy obraz jest w odcieniach szarości
        )

	; Ustawienie koloru tła
    (gimp-context-set-background bg-color)
    (gimp-context-set-foreground fg-color)

    (gimp-drawable-fill bg-layer FILL-BACKGROUND)
    (gimp-layer-set-opacity bg-layer opacity)

	; jeśli obraz jest szary to konwertuje go na RGB - tak na sztukę
    ;(if (= is-nocolorful TRUE)
     ;   (gimp-image-convert-rgb image)
    ;)
    ; TODO
    ; jeśli obraz jest szary to konwertuje go na RGB - w RC2 ten numer nie przedzie
    ;(if (= type GRAY-IMAGE)
    ;   (gimp-image-convert-rgb image) )
    ; lub lepiej ...
    ;(if (> (car (gimp-drawable-type (vector-ref drawable 0))) 1 )
     ;  (gimp-image-convert-rgb image))
     
    ; Dodanie warstwy tła pod tekstem
    (gimp-image-insert-layer image bg-layer 0 -1)

    ;(gimp-layer-translate bg-layer bg-x-left pos-y)
    ;(gimp-selection-translate image bg-layer bg-x-left pos-y)
    (gimp-layer-set-offsets bg-layer bg-x-left pos-y)
    (if (= background-transparency TRUE)
	    (begin
            (gimp-drawable-fill bg-layer FILL-TRANSPARENT)
        )
        (begin  ; else
        )
    )

	; Ustawienie pozycji tekstu
    (gimp-layer-set-offsets text-layer pos-x pos-y)

    ; Przesuń warstwę tekstową na górę stosu warstw
	(gimp-image-raise-item-to-top image text-layer)

    ; Scalanie warstw
    (gimp-image-merge-down image text-layer CLIP-TO-IMAGE)

    ; Aktualizacja widoku
    (gimp-displays-flush)

    (gimp-image-undo-group-end image) ; Zakończ grupę operacji undo
  )
)

(script-fu-register "przem-add-background-under-text-v3"
  _"Add Background Under Text"
  _"Dodaje kolorowe tło pod tekstem z możliwością wyboru czcionki i rozmiaru."
  "Przemysław R. Pietraszczyk"
  "Public Domain"
  "2025-02-06"
  "RGB* GRAY*"
  SF-IMAGE "Image" 0
  SF-DRAWABLE "Drawable" 0
  SF-STRING "Text" "Doda Elektroda"
  SF-FONT "Font" "Monospace"
  SF-ADJUSTMENT "Font Size" (list 50 1 1000 1 10 0 1)
  SF-COLOR "Foreground Color" (list 100 100 100)
  SF-COLOR "Background Color" (list 255 215 0)
  SF-TOGGLE "Background Transparency" FALSE
  SF-ADJUSTMENT "Transparency Value" (list 100 1 100 1 10 0 1)
)

(script-fu-menu-register "przem-add-background-under-text-v3"
                         "<Image>/Filters/PIETRASZCZYK/")