ADDED doc/schemey-fu-devnotes.txt Index: doc/schemey-fu-devnotes.txt ================================================================== --- doc/schemey-fu-devnotes.txt +++ doc/schemey-fu-devnotes.txt @@ -0,0 +1,137 @@ +This document is intended for developers who might wish to make changes to Schemey-fu, or those who just wish to understand how it works. It is not necessary to understand the contents of this document to use Schemey-fu. + +WHAT IS SCHEMEY-FU? + +Schemey-fu is wrapper for the GIMP Procedural DataBase (PDB) that is intended to allow coding of Script-fu scripts in a more Scheme-like manner. GIMP's PDB is intended to interface with a wide range of programming languages (C, Scheme, Python, Perl, Ruby, to name a few) and thus the arguments passed to PDB procedures, and the values returned by the procedures, are specified using low level data objects such as simple numbers, strings, and uncounted arrays. All plug-in languages --except C itself -- provide a wrapper interface that will convert arguments from the native formats to the format expected by the PDB; likewise converting the return values of the procedure into the corresponding native format. + +In some cases, the conversion between PDB format and the native language format may be quite simple. While the PDB uses an integer value of "1" for logical true, and "0" for logical false, Python uses special boolean objects True and False, respectively. + +As a more complicated example, GIMP's Python plug-in is largely object-oriented and so an 'image' is an object with certain values (e.g., width, height, list of layers) and methods (e.g., duplicate, rotate, get width) associated with it, while to the PDB an 'image' is just a unique integer identifier. Python-fu has an extensive wrapper that converts such objects while interacting with the PDB. + +Script-fu's interface to the PDB is performed at a lower level. Boolean arguments passed to the PDB must be converted (by the script) to the numbers "1" and "0", while images, paths, and drawables are passed as simple integer identifiers. Lists and vectors need to be converted to counted vectors -- in other words, while the length of a Scheme list or vector is inherently known, that length has to be passed to the PDB as a separate argument. + +In addition, Script-fu procedures only return a single value, though that value may be a list (of values). Multiple assignment such as Python's "x,y = pdb.gimp_layer_get_offset(layer)" is not possible in Script-fu. Whenever a PDB procedure returns more than one value, it is necessary that those returns be provided as a list. + +Furthermore, to simplify maintainability of Script-fu and provide consistency between GIMP-supplied and third-party procedures, even PDB procedures that return only a single value have that value placed inside a list. This is why it is typically necessary to use 'car' on the return value of PDB procedures such as 'gimp-image-get-active-layer'. + +Schemey-fu attempts to address some of these issues and allow script coding to benefit from the conventions of the Scheme language. + +IS SCHEMEY-FU BETTER THAN SCRIPT-FU? + +No, it is not. + +SCHEMEY-FU INTERNALS + +The PDB procedures are divided up into different namespaces, with the namespace supplied as a prefix in the procedure name. The main namespaces are gimp, file, script-fu, and plug-in. Schemey-fu is mostly limited to the procedures within the gimp namespace. The gimp namespace generally follows the naming convention of gimp-object-action, where the object might be an image, a layer, a tool, or somesuch. + +In order to maintain a consistent mapping with the PDB names, Schemey-fu procedures are generally divided into packages based upon the object being accessed. For example, the Schemey-fu call for gimp-layer-add-alpha becomes Layer::add-alpha, the Schemey-fu call for gimp-image-select-rectangle becomes Image::select-rectangle. gimp-selection-invert becomes Selection::invert, gimp-brush-set-radius becomes Brush::set-radius, et cetera. Schemey-fu package names begin with a capital letter. + +The trick to implementing this mapping has to do with how Script-fu handles variable names containing two parts separate by double colons. Given an expression such as prefix::function, Script-fu converts that into (eval function prefix), where the 'prefix' specifies the environment in which the expression is to be evaluated. This allows us to define a function (or variable) within a localized environment and then easily access that function from a different environment. + +A local environment is created whenever a new lambda is introduced. To define our functions locally (so that they are not found when invoked from a different environment), we do the following: + +(lambda () + (define (base-type image) + (car (gimp-image-base-type image))) + (define (clean-all image) + (gimp-image-clean-all image)) + ) + +We can then access the two functions 'base-type' and 'clean-all' from within , but they are not accessible (by those names) from outside the lambda. In order to have access from outside the lambda, we return the local environment by having the last (in fact, only) thing does is call the function 'current-environment'. + +Of course, we have to actually execute the lambda to create the definitions and return the environment. We do that by applying the lambda (to no arguments). We then just need to assign that returned environment to a variable (that will become our 'prefix'). + +(define Image + (apply (lambda () + (define (base-type image) + (car (gimp-image-base-type image))) + (define (clean-all image) + (gimp-image-clean-all image)) + ; more definitions can go here + (current-environment)))) + +After this is done, it is possible to call 'base-type' using eval and passing the environment: + +(eval '(base-type x) Image) + +Or using the double-colon reader transformation: + +(Image::base-type x) + +Similarly for 'clean-all'. + +All that is left is to define a macro that simplifies all that. + +(macro (make-environment form) + `(apply (lambda () + ,@(cdr form) + (current-environment)))) + +Which allows stating the same thing as: + +(define Image + (make-environment + (define (base-type image) + (car (gimp-image-base-type image))) + (define (clean-all image) + (gimp-image-clean-all image)) + ; more definitions can go here + )) + +Note that the 'make-environment' macro is included in Script-fu's script-fu.init file. + +HELPER MACROS + +Schemey-fu defines a few helper routines that make defining packages a bit easier and more readable. First there is 'pdb-dehyphen' which will return a new symbol after stripping the prefixes. For example, (pdb-deyphen 2 'gimp-image-base-type) will return 'base-type (the first parameter is the number of hyphens to skip). + +There are also some macros that create definitions for the most common types of PDB functions automatically. For example, (pdb-pass 2 gimp-image-clean-all) will create the appropriate definition as described above. Likewise, (pdb-decar 2 gimp-image-base-type) will create the above defintion for 'base-type such that the car of the PDB return value is extracted. + +There is also a macro for converting the PDB return values from boolean tests to Scheme's #t and #f, ready for testing with Scheme conditionals. A question mark is appended to the new name following the Scheme convention for boolean procedures. For example, (pdb-bool 3 gimp-item-get-visible) will provide a suitable definition for (Item::visible? x). + +Note that not all PDB procedures are handled by the above three macros. The above macros only handle the most common transformations of return values; sometimes it is necessary to transform the arguments being passed to the procedure. Also, the data types that get transformed (booleans and counted arrays) are sometimes returned along with other values in a list (see Vectors::stroke-get-points for one such example). It would be a waste of effort to have a macro for such exceptional cases. + +TRANSFORMATIONS + +In addition to "decar"ing the returns of procedures that return a single value and converting TRUE/FALSE returns to Scheme booleans, Schemey-fu converts counted PDB arrays to lists. For example, the PDB procedure, gimp-image-get-layers, returns the number of layers plus a vector of those layers; this gets converts to a simple list of the layers. + +(set! layers (Image::get-layers x)) + +This transformation of counted arrays applies also to parameters passed to procedures expecting counted arrays. The Schemey-fu binding will automatically insert the number of elements in the list and convert the list to the appropriate array. + +(set! stroke-id (Vectors::stroke-new-from-points path + VECTORS-STROKE-TYPE-BEZIER + '(34 5 40 10 42 15 54 15 41 20 68 24) + 1)) + +Note that when booleans parameters are expected by PDB procedures, they should be passed as TRUE/FALSE (1/0) rather than as Scheme's #f and #t. It is only when they are returned as values from PDB procedures that booleans get transformed. The reasoning behind adopting this inconsistency is two-fold. + +First, the majority of third-party plug-ins/scripts do not return meaningful values and hardly any return booleans. However, there are third-party scripts that accept TRUE/FALSE as parameters and using the same convention for both the Gimp namespace and third-party parameters simplifies things somewhat. + +Second, if the script writer makes a mistake and passes #t or #f to a procedure that is expecting a TRUE or FALSE, an error will be produced about the wrong type being supplied. However, if Schemey-fu had chosen the opposite approach (and functions expected #t/#f parameters) then should a scripter pass, by mistake, a TRUE/FALSE to the procedure, no error would be generated and in the case of FALSE being passed, that FALSE would be treated as a TRUE. It would be much harder to track down the cause of such an unflagged error after it produces the faulty behavior. + +Finally, there are basically two different forms of boolean procedures in the PDB: where the procedure name includes "is/has" (e.g., gimp-image-is-valid, gimp-drawable-has-alpha), and where the procedure name includes "get" and there is a corresponding "set" procedure (e.g., gimp-item-get-visible). For the first form, the Schemey-fu function starts with the "is-" (or "has-") while for the second form the "-get-" is dropped altogether. For both forms, a question mark is appended to the function name: e.g., Image::is-valid?, Drawable::has-alpha?, and Item::visible?. + + +APPENDIX + +Useful means of querying the PDB while excluding deprecated procedures: + (gimp-procedural-db-query ".*" + "^((?!deprecated).)*$" ; exclude deprecated + ".*" + ".*" + ".*" + ".*" + "Internal GIMP procedure") + +The following function was used to separate the procedures based upon the number of values they return: + + (define (filter-by-nb-values pdb-names nb-values) + (let loop ((procs pdb-names) + (result '())) + (if (null? procs) + (reverse result) + (loop (cdr procs) + (if (= (cadddr (cddddr (gimp-procedural-db-proc-info (car procs)))) nb-values) + (cons (car procs) result) + result))))) + ADDED doc/schemey-fu-manual.txt Index: doc/schemey-fu-manual.txt ================================================================== --- doc/schemey-fu-manual.txt +++ doc/schemey-fu-manual.txt @@ -0,0 +1,215 @@ +This document describes general usage of Schemey-fu. + +WHAT IS SCHEMEY-FU? + +Schemey-fu is a wrapper for the GIMP Procedural DataBase (PDB) that is intended to allow coding of Script-fu scripts in a more Scheme-like manner. Schemey-fu provides substitute functions for all of the internally defined PDB procedures, with the substitute functions returning individual values, pure lists, and true Scheme booleans, and accepting lists as arguments when counted arrays are expected. + +INSTALLATION + +Copy the schemey-fu.scm file into your ~/.gimp-2.8/scripts directory. + +SINGLE RETURN VALUES + +When a PDB procedure returns a single (typically numeric) value, that value is always placed inside a list, and requires applying 'car' to that list to extract the value. The corresponding Schemey-fu function returns the desired value directly. + + Examples: + (Image::width image) ==> width + (Layer::new-from-drawable drawable dest-image) ==> layer-copy + +BOOLEANS + +When a PDB procedure returns a FALSE/TRUE, not only is that return placed inside a list, it follows the convention that zero indicates boolean false and "1" indicates boolean true. Schemey-fu's boolean functions directly return either #f of #t respectively. + +Furthermore, the name of the Schemey-fu function has a question mark (?) appended, following the Scheme convention for predicate functions. + + Examples: + (Image::is-valid? x) ==> #t/#f + (Item::is-channel? x) ==> #t/#f + (Drawable::has-alpha? x) ==> #t/#f + +For boolean procedures that include "get" in the name and have a corresponding "set" procedure, Schemey-fu naming convention omits the "get" (and appends the question mark). + + Examples: + (Layer::visible? x) ==> #t/#f + (Context::antialias?) ==> #t/#f + +Note that when booleans are passed as arguments, they should follow the PDB convention of TRUE=1/FALSE=0. It is only when they are returned as values from PDB procedures that booleans get transformed. The reasoning behind adopting this assymetry is two-fold. + +First, the majority of third-party plug-ins/scripts do not return meaningful values and hardly any return booleans. However, there are third-party scripts that accept TRUE/FALSE as parameters and using the same convention for both the Schemey-fu parameters and third-party parameters provides better consistency. + +Second, if the script writer makes a mistake and passes #t or #f to a procedure that is expecting a TRUE or FALSE, an error will be produced about the wrong type being supplied. However, if Schemey-fu had chosen the opposite approach (and functions expected #t/#f parameters) then should a scripter mistakenly provide a TRUE/FALSE to the procedure, no error would be generated and, in the case of FALSE being passed, that FALSE would be treated as TRUE. It would be much harder to track down bugs caused by such an unflagged error. + +COUNTED ARRAYS + +In addition to "decar"ing the returns of procedures that return a single value and converting TRUE/FALSE returns to Scheme booleans, Schemey-fu converts counted PDB arrays to lists. For example, the PDB procedure, gimp-image-get-layers, returns the number of layers plus a vector of those layers; the Schemey-fu function returns a simple list of the layers. + + (Image::get-layers x) ==> list of layers + +This transformation of counted arrays applies also to parameters passed to procedures expecting counted arrays. The Schemey-fu binding will automatically insert the number of elements in the list and convert the list to the appropriate array type expected (i.e., either a list or a vector). + + (Vectors::stroke-new-from-points path + VECTORS-STROKE-TYPE-BEZIER + '(34 5 40 10 42 15 54 15 41 20 68 24) + 1) + ==> stroke-id + +Note that this transformation of counted arrays only applies to GIMP internal procedures; plug-ins with counted array parameters (such as 'plug-in-film') need to be handled in the traditional manner. Fortunately, there are very few instances where this is a concern. + +NAMESPACES + +By now you've probably realized that Schemey-fu functions follow the naming convention of "Namespace::function", which consistly maps to the corresponding PDB procedure's name. + +Within the PDB, procedures are divided up into different namespaces with the namespace supplied as a prefix in the procedure name. The main top-level namespaces are 'gimp', 'file', 'script-fu', and 'plug-in'. Schemey-fu is mostly limited to the procedures within the 'gimp' namespace. The 'gimp' namespace itself generally follows the naming convention of gimp-object-action, where the object might be an image, a layer, a tool, or somesuch. + +In order to maintain a consistent mapping with the PDB names, Schemey-fu functions are generally divided into namespaces based upon the object being accessed. For example, the Schemey-fu call for gimp-layer-add-alpha becomes Layer::add-alpha, the Schemey-fu call for gimp-image-select-rectangle becomes Image::select-rectangle. gimp-selection-invert becomes Selection::invert, gimp-brush-set-radius becomes Brush::set-radius, et cetera. + +Note that Schemey-fu namespaces all begin with a capital letter, while the remainder of the name is lowercase. Though this breaks with traditional Scheme naming convention, it permits use of variable names that might otherwise conflict with a Schemey-fu namespace name (e.g., image, layer, drawable). + +The Schemey-fu namespaces comprise the following (in alphabetical order): + namespace prefix + --------- ------ + Brush gimp-brush- + Brushes gimp-brushes- + Buffer gimp-buffer- + Buffers gimp-buffers- + Channel gimp-channel- + Context gimp-context- + Drawable gimp-drawable- + Edit gimp-edit- + Floating-sel gimp-floating-sel- + Gradient gimp-gradient- + Image gimp-image- + Item gimp-item- + Layer gimp-layer- + Palette gimp-palette- + Palettes gimp-palettes- + Plugin gimp-plug-in- + Pdb gimp-procedural-db- + Progress gimp-progress- + Selection gimp-selection- + Text gimp-text- + Unit gimp-unit- + Vectors gimp-vectors- + +In addition, there are a few gimp-* PDB procedures that do not fit into the above categories. First, there are the procedures that implement some of the tools found in the Toolbox and in the Colors menu; these are consolidated into the "Tool" namespace. + + namespace prefix PDB procedure (+ indicates there is an additional -default version) + --------- ------ ------------------------------------------------------------------- + Tool gimp- airbrush+, clone+, convolve+, curves-explicit, curves-spline, + dodgeburn+, eraser+, heal+, histogram, hue-saturation, invert, + paintbrush+, pencil+, posterize, smudge+ + +There are also a handful of procedures that address GIMP "sessions". These are incorporated into the Gimp namespace. + + namespace prefix PDB procedure + --------- ------ ------------------------------------------------------------------- + Gimp gimp- attach-parasite, gimp-detach-parasite, get-parasite-list, get-parasite name, quit + +FU UTILITY PROCEDURES + +Finally, there is a special Fu namespace, where I've placed some utility functions. + + Fu::list-head (lis k) + return the first 'k' elements in the list + Fu::list-tail (lis k) + return all element after the first 'k' items (Script-fu already provides + 'list-tail' with the same functionality but it is replicated in the Fu + namespace for consistency with Fu::list-head) + Fu::fold (op accum lis + fold (left) per SRFI-1 (http://srfi.schemers.org/srfi-1/srfi-1.html) + Note that there exists a 'foldr' in Script-fu, however, 'foldr' + is inconsistent with traditional 'fold' (as the arguments expected + by 'op' are in reversed order). + Fu::fold-right (op accum lis) + Per SRFI-1 + Fu::partition (pred? lis) + Per SRFI-1. Split 'lis' into two lists: one containing the elements + that satisfy the predicate function and one the elements that do not. + Fu::remove (lis items) + Remove elements from list that equal those in items. items can be + either one item or a list of several items. Note that this remove + is NOT per SRFI-1 + Fu::number->string (z . args) + Per R5RS + Fu::string-reverse (str) + Reverse the order of the characters in a string. + Fu::string-trim (str [pred]) + Per SRFI-13 without optional start/end. + Skip over initial characters matching the 'pred'. + 'pred' can be either a procedure or a char; if + not supplied, pred defaults to #\space. + Fu::string-pad (str n [char]) + Per SRFI-13 without start/end (also does not truncate str). + Prepends enough 'char's to string to fill field of size 'n' + If not supplied, 'char' is #\space. + Fu::string-delete (pred str) + Per SRFI-13 without optional start/end. + Remove all chars from a string that match 'pred' + 'pred' can be either a procedure or a char. + + Fu::progression (start end [num-elements [growth]]) + Return a list of N elements from start to end with either + algebraic/linear growth or geometric/exponential growth. + If 'num-elements' is not specified then a simple integer sequence is + generated. + If 'growth' is not specified or is #f then a linear progression is + generated, otherwise a geometric progression. For geometric + progressions, 'start' must be greater than 0. + Examples: + (Fu::progression 1 5) ==> (1 2 3 4 5) + (Fu::progression 1 5 5) ==> (1 2 3 4 5) + (Fu::progression 1 5 6) ==> (1 1.8 2.6 3.4 4.2 5) + (Fu::progression 1 1000 4 TRUE) ==> (1 10 100 1000) + Fu::sort (compare? lis) + Sort a list based on compare? Optimal for short lists (< 50 items) + Example: + (Fu::sort < '(30 20 50 40 10)) ==> (10 20 30 40 50) + Fu::msort (compare? lis) + Merge sort a list based on compare? Optimal for longer lists (> 50 items) + Example: + (Fu::msort < '(30 20 50 40 10)) ==> (10 20 30 40 50) + + Fu::get-bounds (drawables) + Accepts either the ID of a single drawable or a list of drawable IDs. + If a list then the bounds of all drawables in the list is returned. + + Fu::grow-while (pred? . args) + Returns the largest integer that satisfies the 'pred?' function. + The 'pred?' function should accept a single "size" argument + and return #t if the size fits the criteria. (Se Fu::calc-fontsize + for an example of usage.) + + Fu::calc-fontsize (text font width height) + Calculate the largest size of the given font that will fit within + the given dimensions. + +USEFUL MACROS + + Schemey-fu also provides some useful macros. These do not follow the + double-colon namespace syntax. + + (with-context ) + + will automatically push the context before executing the code in + and pop it afterward. + + (with-undo image ) + + will automatically start an undo group before executing and + end it afterward. + + (with-selection image ) + + will save the current selection to a channel before executing + and restore it afterward. If you want to have access to the saved + selection within then you should provide a list for the + first argument, with the second item in the list being a channel + name that can be used from within the body. + + (with-xywh (name-x name-y name-w name-h) drawable ) + + will create bindings (whose scope are local to ). It accepts + as its first argument a list of four symbols respectively correspond + to the variable names for the x-offset, y-offset, width, and height + of the 'drawable'. If 'bindings' is empty then the default variable + names are x, y, w, and h. ADDED doc/sg-split-text-into-layers.scm Index: doc/sg-split-text-into-layers.scm ================================================================== --- doc/sg-split-text-into-layers.scm +++ doc/sg-split-text-into-layers.scm @@ -0,0 +1,142 @@ +; License: GPLv3 +; This program is free software: you can redistribute it and/or modify +; it under the terms of the GNU General Public License as published by +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; To view a copy of the GNU General Public License +; visit: http://www.gnu.org/licenses/gpl.html + +(define (schemey-fu-decompose-text-layer image layer granularity) + (define (split-text-into-lines layer) + (let ((image (Item::get-image layer)) + (text (Text::get-text layer))) + (if (> (length (strbreakup text "\n")) 1) + (let* ((lines (with-xywh () layer + (let loop ((text-lines (strbreakup text "\n")) + (y-offset y) + (line-layers '())) + (if (null? text-lines) + line-layers + (let ((line-layer (Layer::copy layer TRUE))) + (Image::insert-layer image line-layer 0 -1) + (Text::set-text line-layer (car text-lines)) + (Layer::set-offsets line-layer x y-offset) + (loop (cdr text-lines) + (+ y-offset + (Text::get-line-spacing layer) + (Drawable::height line-layer)) + (cons line-layer line-layers))))))) + (max-width (apply max (map Drawable::width lines)))) + (if (= (Text::get-justification layer) TEXT-JUSTIFY-CENTER) + (map (lambda (layer) + (with-xywh () layer + (Layer::set-offsets layer + (+ x (ceiling (/ (- max-width w) 2))) + y))) + lines)) + (Image::remove-layer image layer) + lines) + (list layer)))) + + (define (split-line-into-words layer) + (let* ((image (Item::get-image layer)) + (text (Text::get-text layer)) + (space (+ (* 2 (Text::get-letter-spacing layer)) + (car (Text::get-extents-fontname " " + (car (Text::get-font-size layer)) + PIXELS + (Text::get-font layer)))))) + (if (> (length (strbreakup text " ")) 1) + (let* ((words (with-xywh () layer + (let loop ((text-words (strbreakup text " ")) + (x-offset x) + (word-layers '())) + (if (null? text-words) + word-layers + (let ((word-layer (Layer::copy layer TRUE))) + (Image::insert-layer image word-layer 0 -1) + (Text::set-text word-layer (car text-words)) + (Layer::set-offsets word-layer x-offset y) + (loop (cdr text-words) + (+ x-offset + space + (Drawable::width word-layer)) + (cons word-layer word-layers)))))))) + (Image::remove-layer image layer) + words) + (list layer)))) + + (define (split-word-into-letters layer) + (let* ((image (Item::get-image layer)) + (text (Text::get-text layer)) + (space (Text::get-letter-spacing layer))) + (if (> (string-length text) 1) + (let* ((letters (with-xywh () layer + (let loop ((text-letters (string->list text)) + (x-offset x) + (letter-layers '())) + (cond + ((null? text-letters) + letter-layers) + ((null? (cdr text-letters)) + (let ((letter-layer (Layer::copy layer TRUE)) + (letter (make-string 1 (car text-letters)))) + (Image::insert-layer image letter-layer 0 -1) + (Text::set-text letter-layer letter) + (Layer::set-offsets letter-layer x-offset y) + (cons letter-layer letter-layers))) + (else + (let ((letter-layer (Layer::copy layer TRUE)) + (letter (make-string 1 (car text-letters)))) + (Image::insert-layer image letter-layer 0 -1) + (Text::set-text letter-layer letter) + (Layer::set-offsets letter-layer x-offset y) + (let* ((next (make-string 1 (cadr text-letters))) + (sub-width (car (Text::get-extents-fontname + next + (car (Text::get-font-size layer)) + PIXELS + (Text::get-font layer)))) + (tot-width (car (Text::get-extents-fontname + (string-append letter next) + (car (Text::get-font-size layer)) + PIXELS + (Text::get-font layer))))) + (loop (cdr text-letters) + (+ x-offset space (- tot-width sub-width)) + (cons letter-layer letter-layers)))))))))) + (Image::remove-layer image layer) + letters) + (list layer)))) + + (when (Item::is-text-layer? layer) + (with-undo image + (let ((lines (split-text-into-lines layer))) + (if (zero? granularity) + (map split-word-into-letters + (Fu::fold append '() (map split-line-into-words lines))) + (map split-line-into-words lines))))) + (gimp-displays-flush)) + + +(script-fu-register "schemey-fu-decompose-text-layer" + "Decompose Text Layer..." + "Split a text layer into lines, words, or letters" + "saulgoode" + "saulgoode" + "March 2015" + "RGB*, GRAY*" + SF-IMAGE "Image" 0 + SF-DRAWABLE "Drawable" 0 + SF-OPTION "Granularity" '("Letters" "Words" "Lines") + ) +(script-fu-menu-register "schemey-fu-decompose-text-layer" + "/Filters/Misc" + ) + ADDED schemey-fu.scm Index: schemey-fu.scm ================================================================== --- schemey-fu.scm +++ schemey-fu.scm @@ -0,0 +1,1310 @@ +(define schemey-fu '(2 8)) ; to match GIMP version + +; pdb-dehyphen accepts a symbol and returns the string that remains +; after everything up to and including 'cnt' hyphens have been +; removed. For example, (pdb-dehyphen 'gimp-selection-none 2) ==> "none" +; +(define (pdb-dehyphen sym cnt) + (let loop ((lis (string->list (symbol->string sym))) + (cnt cnt)) + (if (zero? cnt) + (list->string lis) + (if (equal? (car lis) #\-) + (loop (cdr lis) (- cnt 1)) + (loop (cdr lis) cnt))))) + +;; The following macros automatically convert PDB procedures +;; to their Schemey-fu equivalents. They work on the bulk of the +;; PDB procedures, for example by taking the car of a single +;; returned value, but some procedures require special processing. + +;; Note that 'pdb-pass ' does not incur the overhead of duplicating +;; the closure code, and so it is used for renaming PDB procedures +;; that return no values (but which actually return one value that +;; should be ignored). This results in return values such as the +;; list (#t), rather than #t. +;; 'cons' is used to avoid using 'list' (which might conflict with +;; certain procedures such as 'gimp-image-list' being redefined and +;; replacing Scheme's standard 'list' operation for evaluation within +;; the local namespace). +; +(macro (pdb-pass form) ; for functions that return 0 values or a list + (cons 'define + (cons (string->symbol (pdb-dehyphen (caddr form) (cadr form))) + (cons (eval (caddr form) (current-environment)) + '())))) +;; PDB procedures that return one value need to have that value +;; extracted from the returned list. +; +(macro (pdb-decar form) ; for 1 return value + (cons 'define + (cons (string->symbol (pdb-dehyphen (caddr form) (cadr form))) + (cons (eval (append '(lambda x) + (cons (cons 'car + (cddr (get-closure-code (eval (caddr form) + (interaction-environment))))) + '())) + (current-environment)) + '())))) + +;; PDB procedures that return a boolean value need to have that value +;; extracted from the returned list and converted to #t or #f. +; +(macro (pdb-bool form) ; for PDB funcs that return TRUE/FALSE + (cons 'define + (cons (string->symbol (string-append (pdb-dehyphen (caddr form) (cadr form)) + "?")) + (cons (eval (append '(lambda x) + (cons (cons 'not + (cons (cons 'zero? + (cons (cons 'car + (cddr (get-closure-code (eval (caddr form) + (interaction-environment))))) + '())) + '())) + '())) + (current-environment)) + '())))) + +(define Gimp + (make-environment + (define (attach-parasite name . rest) + (gimp-attach-parasite (if (pair? rest) + (cons name rest) + name ))) + (define (convert-set-dither-matrix width height matrix) + (gimp-image-convert-set-dither-matrix width + height + (length matrix) + (list->vector matrix))) + (pdb-pass 1 gimp-detach-parasite) + (define (get-parasite-list) + (cadr (gimp-get-parasite-list))) + (define (get-parasite name) + (catch #f + (car (gimp-get-parasite name)))) + (pdb-pass 1 gimp-quit) + )) + +(define Image + (make-environment + (pdb-decar 2 gimp-image-add-hguide) + (pdb-decar 2 gimp-image-add-vguide) + (pdb-pass 2 gimp-image-attach-parasite) + (pdb-decar 2 gimp-image-base-type) + (pdb-pass 2 gimp-image-clean-all) + (pdb-pass 2 gimp-image-convert-grayscale) + (pdb-pass 2 gimp-image-convert-indexed) + (pdb-pass 2 gimp-image-convert-rgb) + (pdb-pass 2 gimp-image-convert-set-dither-matrix) + (pdb-pass 2 gimp-image-crop) + (pdb-pass 2 gimp-image-delete) + (pdb-pass 2 gimp-image-delete-guide) + (pdb-pass 2 gimp-image-detach-parasite) + (pdb-decar 2 gimp-image-duplicate) + (pdb-decar 2 gimp-image-find-next-guide) + (pdb-decar 2 gimp-image-flatten) + (pdb-pass 2 gimp-image-flip) + (pdb-decar 2 gimp-image-floating-sel-attached-to) + (pdb-decar 2 gimp-image-get-active-channel) + (pdb-decar 2 gimp-image-get-active-drawable) + (pdb-decar 2 gimp-image-get-active-layer) + (pdb-decar 2 gimp-image-get-active-vectors) + (pdb-decar 2 gimp-image-get-channel-by-name) + (pdb-decar 2 gimp-image-get-channel-by-tattoo) + (define (get-channels image) + (vector->list (cadr (gimp-image-get-channels image)))) + (define (get-colormap image) + (vector->list (cadr (gimp-image-get-colormap image)))) + (pdb-decar 2 gimp-image-get-component-active) + (pdb-decar 2 gimp-image-get-component-visible) + (pdb-decar 2 gimp-image-get-exported-uri) + (pdb-decar 2 gimp-image-get-filename) + (pdb-decar 2 gimp-image-get-floating-sel) + (pdb-decar 2 gimp-image-get-guide-orientation) + (pdb-decar 2 gimp-image-get-guide-position) + (pdb-decar 2 gimp-image-get-imported-uri) + (pdb-decar 2 gimp-image-get-item-position) + (pdb-decar 2 gimp-image-get-layer-by-name) + (pdb-decar 2 gimp-image-get-layer-by-tattoo) + (pdb-decar 2 gimp-image-get-name) + (pdb-decar 2 gimp-image-get-parasite) + (define (get-parasite-list image) + (cadr (gimp-image-get-parasite-list image))) + (pdb-pass 2 gimp-image-get-resolution) + (pdb-decar 2 gimp-image-get-selection) + (pdb-decar 2 gimp-image-get-tattoo-state) + (pdb-decar 2 gimp-image-get-unit) + (pdb-decar 2 gimp-image-get-uri) + (define (get-vectors image) + (vector->list (cadr (gimp-image-get-vectors image)))) + (pdb-decar 2 gimp-image-get-vectors-by-name) + (pdb-decar 2 gimp-image-get-vectors-by-tattoo) + (pdb-decar 2 gimp-image-get-xcf-uri) + (pdb-decar 2 gimp-image-grid-get-background-color) + (pdb-decar 2 gimp-image-grid-get-foreground-color) + (pdb-pass 2 gimp-image-grid-get-offset) + (pdb-pass 2 gimp-image-grid-get-spacing) + (pdb-decar 2 gimp-image-grid-get-style) + (pdb-pass 2 gimp-image-grid-set-background-color) + (pdb-pass 2 gimp-image-grid-set-foreground-color) + (pdb-pass 2 gimp-image-grid-set-offset) + (pdb-pass 2 gimp-image-grid-set-spacing) + (pdb-pass 2 gimp-image-grid-set-style) + (pdb-decar 2 gimp-image-height) + (pdb-pass 2 gimp-image-insert-channel) + (pdb-pass 2 gimp-image-insert-layer) + (pdb-pass 2 gimp-image-insert-vectors) + (pdb-bool 2 gimp-image-is-dirty) + (pdb-bool 2 gimp-image-is-valid) + ;; gimp-image-list is defined later to avoid possible + ;; conflicts from re-defining list within this package + (pdb-pass 2 gimp-image-lower-item) + (pdb-pass 2 gimp-image-lower-item-to-bottom) + (pdb-decar 2 gimp-image-merge-down) + (pdb-decar 2 gimp-image-merge-visible-layers) + (pdb-decar 2 gimp-image-new) + (pdb-decar 2 gimp-image-pick-color) + (pdb-decar 2 gimp-image-pick-correlate-layer) + (pdb-pass 2 gimp-image-raise-item) + (pdb-pass 2 gimp-image-raise-item-to-top) + (pdb-pass 2 gimp-image-remove-channel) + (pdb-pass 2 gimp-image-remove-layer) + (pdb-pass 2 gimp-image-remove-vectors) + (pdb-pass 2 gimp-image-reorder-item) + (pdb-pass 2 gimp-image-resize) + (pdb-pass 2 gimp-image-resize-to-layers) + (pdb-pass 2 gimp-image-rotate) + (pdb-pass 2 gimp-image-scale) + (pdb-pass 2 gimp-image-select-color) + (pdb-pass 2 gimp-image-select-contiguous-color) + (pdb-pass 2 gimp-image-select-ellipse) + (pdb-pass 2 gimp-image-select-item) + (define (select-polygon image operation segs) + (gimp-image-select-polygon image operation (length segs) (list->vector segs))) + (pdb-pass 2 gimp-image-select-rectangle) + (pdb-pass 2 gimp-image-select-round-rectangle) + (pdb-pass 2 gimp-image-set-active-channel) + (pdb-pass 2 gimp-image-set-active-layer) + (pdb-pass 2 gimp-image-set-active-vectors) + (define (set-colormap image colormap) + (gimp-image-set-colormap image (length colormap) (list->vector colormap))) + (pdb-pass 2 gimp-image-set-component-active) + (pdb-pass 2 gimp-image-set-component-visible) + (pdb-pass 2 gimp-image-set-filename) + (pdb-pass 2 gimp-image-set-resolution) + (pdb-pass 2 gimp-image-set-tattoo-state) + (pdb-pass 2 gimp-image-set-unit) + (define (thumbnail image width height) + (let ((info (gimp-image-thumbnail image width height))) + (list (car info) + (cadr info) + (caddr info) + (vector->list (car (cdddd info)))))) + (pdb-decar 2 gimp-image-undo-disable) + (pdb-decar 2 gimp-image-undo-enable) + (pdb-decar 2 gimp-image-undo-freeze) + (pdb-pass 2 gimp-image-undo-group-end) + (pdb-pass 2 gimp-image-undo-group-start) + (pdb-bool 2 gimp-image-undo-is-enabled) + (pdb-decar 2 gimp-image-undo-thaw) + (pdb-pass 2 gimp-image-unset-active-channel) + (pdb-decar 2 gimp-image-width) + + ; Image::get-layers takes an option 'mode' argument (that is not + ; available with the 'gimp-image-get-layers' procedure). + ; if 'mode' is either 0 or omitted altogether return just the + ; top-level layers and fundaments + ; If 'mode' is 1 then a nested list of all real layers (no + ; fundaments) is returned. + ; If 'mode' is 2 then a nested list is returned, where the first + ; item in each sub-list is the fundament of the group. + ; + (define (get-layers image . mode) + (let ((top-layers (vector->list (cadr (gimp-image-get-layers image))))) + (if (or (null? mode) (zero? (car mode))) + top-layers + (let loop ((top-layers top-layers) + (layers '())) + (if (null? top-layers) + (reverse layers) + (loop (cdr top-layers) + (cons (if (zero? (car (gimp-item-is-group (car top-layers)))) + (car top-layers) + (if (= (car mode) 2) + (cons (car top-layers) + (Item::get-children (car top-layers) (car mode))) + (Item::get-children (car top-layers) (car mode)))) + layers))))))) + (define (list) + (vector->list (cadr (gimp-image-list)))) + )) + +(define Layer + (make-environment + (pdb-pass 2 gimp-layer-add-alpha) + (pdb-pass 2 gimp-layer-add-mask) + (pdb-decar 2 gimp-layer-copy) + (pdb-decar 2 gimp-layer-create-mask) + (pdb-pass 2 gimp-layer-flatten) + (pdb-decar 2 gimp-layer-from-mask) + (pdb-bool 3 gimp-layer-get-apply-mask) + (pdb-bool 3 gimp-layer-get-edit-mask) + (pdb-bool 3 gimp-layer-get-lock-alpha) + (pdb-decar 2 gimp-layer-get-mask) + (pdb-decar 2 gimp-layer-get-mode) + (pdb-decar 2 gimp-layer-get-opacity) + (pdb-bool 3 gimp-layer-get-show-mask) + (pdb-decar 2 gimp-layer-group-new) + (pdb-bool 2 gimp-layer-is-floating-sel) + (pdb-decar 2 gimp-layer-new) + (pdb-decar 2 gimp-layer-new-from-drawable) + (pdb-decar 2 gimp-layer-new-from-visible) + (pdb-pass 2 gimp-layer-remove-mask) + (pdb-pass 2 gimp-layer-resize) + (pdb-pass 2 gimp-layer-resize-to-image-size) + (pdb-pass 2 gimp-layer-scale) + (pdb-pass 2 gimp-layer-set-apply-mask) + (pdb-pass 2 gimp-layer-set-edit-mask) + (pdb-pass 2 gimp-layer-set-lock-alpha) + (pdb-pass 2 gimp-layer-set-mode) + (pdb-pass 2 gimp-layer-set-offsets) + (pdb-pass 2 gimp-layer-set-opacity) + (pdb-pass 2 gimp-layer-set-show-mask) + (pdb-pass 2 gimp-layer-translate) + )) + +(define Drawable + (make-environment + (pdb-decar 2 gimp-drawable-bpp) + (pdb-pass 2 gimp-drawable-fill) + (pdb-pass 2 gimp-drawable-foreground-extract) + (pdb-pass 2 gimp-drawable-free-shadow) + (pdb-decar 2 gimp-drawable-get-pixel) + (pdb-bool 2 gimp-drawable-has-alpha) + (pdb-decar 2 gimp-drawable-height) + (pdb-bool 2 gimp-drawable-is-gray) + (pdb-bool 2 gimp-drawable-is-indexed) + (pdb-bool 2 gimp-drawable-is-rgb) + (pdb-pass 2 gimp-drawable-mask-bounds) + (pdb-pass 2 gimp-drawable-mask-intersect) + (pdb-pass 2 gimp-drawable-merge-shadow) + (pdb-pass 2 gimp-drawable-offset) + (pdb-pass 2 gimp-drawable-offsets) + (pdb-pass 2 gimp-drawable-set-pixel) + (define (sub-thumbnail drawable x y w h dest-w dest-h) + (let ((info (gimp-drawable-sub-thumbnail drawable x y w h dest-w dest-h))) + (list (car info) + (cadr info) + (caddr info) + (vector->list (car (cddddr info)))))) + (define (thumbnail drawable dest-w dest-h) + (let ((info (gimp-drawable-sub-thumbnail drawable dest-w dest-h))) + (list (car info) + (cadr info) + (caddr info) + (vector->list (car (cddddr info)))))) + (pdb-decar 2 gimp-drawable-type) + (pdb-decar 2 gimp-drawable-type-with-alpha) + (pdb-pass 2 gimp-drawable-update) + (pdb-decar 2 gimp-drawable-width) + + )) + +(define Item + (make-environment + (pdb-pass 2 gimp-item-attach-parasite) + (pdb-pass 2 gimp-item-delete) + (pdb-pass 2 gimp-item-detach-parasite) + (pdb-decar 2 gimp-item-get-image) + (pdb-bool 3 gimp-item-get-linked) + (pdb-bool 3 gimp-item-get-lock-content) + (pdb-decar 2 gimp-item-get-name) + (pdb-decar 2 gimp-item-get-parasite) + (define (get-parasite-list item) + (cadr (gimp-item-parasite-list item))) + (pdb-decar 2 gimp-item-get-parent) + (pdb-decar 2 gimp-item-get-tattoo) + (pdb-bool 3 gimp-item-get-visible) + (pdb-bool 2 gimp-item-is-channel) + (pdb-bool 2 gimp-item-is-drawable) + (pdb-bool 2 gimp-item-is-group) + (pdb-bool 2 gimp-item-is-layer) + (pdb-bool 2 gimp-item-is-layer-mask) + (pdb-bool 2 gimp-item-is-selection) + (pdb-bool 2 gimp-item-is-text-layer) + (pdb-bool 2 gimp-item-is-valid) + (pdb-bool 2 gimp-item-is-vectors) + (pdb-pass 2 gimp-item-set-linked) + (pdb-pass 2 gimp-item-set-lock-content) + (pdb-pass 2 gimp-item-set-name) + (pdb-pass 2 gimp-item-set-tattoo) + (pdb-pass 2 gimp-item-set-visible) + (pdb-decar 2 gimp-item-transform-2d) + (pdb-decar 2 gimp-item-transform-flip) + (pdb-decar 2 gimp-item-transform-flip-simple) + (pdb-decar 2 gimp-item-transform-matrix) + (pdb-decar 2 gimp-item-transform-perspective) + (pdb-decar 2 gimp-item-transform-rotate) + (pdb-decar 2 gimp-item-transform-rotate-simple) + (pdb-decar 2 gimp-item-transform-scale) + (pdb-decar 2 gimp-item-transform-shear) + + ; Item::get-children takes an option 'mode' argument (that is not + ; available with the 'gimp-item-get-children' procedure). + ; if 'mode' is either 0 or omitted altogether return just the + ; top-level layers and fundaments + ; If 'mode' is 1 then a nested list of all real layers (no + ; fundaments) is returned. + ; If 'mode' is 2 then a nested list is returned, where the first + ; item in each sub-list is the fundament of the group. + ; + ; similar to 'get-layers' but for a group fundament + ; + (define (get-children fundament . mode) + (let ((top-layers (vector->list (cadr (gimp-item-get-children fundament))))) + (if (or (null? mode) (zero? (car mode))) + top-layers + (let loop ((top-layers top-layers) + (layers '())) + (if (null? top-layers) + (reverse layers) + (loop (cdr top-layers) + (cons (if (zero? (car (gimp-item-is-group (car top-layers)))) + (car top-layers) + (if (= (car mode) 2) + (cons (car top-layers) + (get-children (car top-layers) (car mode))) + (get-children (car top-layers) (car mode)))) + layers))))))) + )) + +(define Floating-sel + (make-environment + (pdb-pass 3 gimp-floating-sel-anchor) + (pdb-pass 3 gimp-floating-sel-attach) + (define (attached-to image) + (car (gimp-image-floating-sel-attached-to image))) + (pdb-pass 3 gimp-floating-sel-relax) + (pdb-pass 3 gimp-floating-sel-remove) + (pdb-pass 3 gimp-floating-sel-rigor) + (pdb-pass 3 gimp-floating-sel-to-layer) + )) + +(define Edit + (make-environment + (pdb-pass 2 gimp-edit-blend) + (pdb-pass 2 gimp-edit-bucket-fill) + (pdb-pass 2 gimp-edit-bucket-fill-full) + (pdb-pass 2 gimp-edit-clear) + (define (copy drawable) + (not (zero? (car (gimp-edit-copy drawable))))) + (define (copy-visible drawable) + (not (zero? (car (gimp-edit-copy-visible drawable))))) + (define (cut drawable) + (not (zero? (car (gimp-edit-cut drawable))))) + (pdb-pass 2 gimp-edit-fill) + (pdb-decar 2 gimp-edit-named-copy) + (pdb-decar 2 gimp-edit-named-copy-visible) + (pdb-decar 2 gimp-edit-named-cut) + (pdb-decar 2 gimp-edit-named-paste) + (pdb-decar 2 gimp-edit-named-paste-as-new) + (pdb-decar 2 gimp-edit-paste) + (pdb-decar 2 gimp-edit-paste-as-new) + (pdb-pass 2 gimp-edit-stroke) + (pdb-pass 2 gimp-edit-stroke-vectors) + )) + +(define Context + (make-environment + (pdb-bool 3 gimp-context-get-antialias) + (pdb-decar 2 gimp-context-get-background) + (pdb-decar 2 gimp-context-get-brush) + (pdb-decar 2 gimp-context-get-brush-angle) + (pdb-decar 2 gimp-context-get-brush-aspect-ratio) + (pdb-decar 2 gimp-context-get-brush-size) + (pdb-decar 2 gimp-context-get-dynamics) + (pdb-decar 2 gimp-context-get-feather) + (pdb-pass 2 gimp-context-get-feather-radius) + (pdb-decar 2 gimp-context-get-font) + (pdb-decar 2 gimp-context-get-foreground) + (pdb-decar 2 gimp-context-get-gradient) + (pdb-decar 2 gimp-context-get-ink-angle) + (pdb-decar 2 gimp-context-get-ink-blob-angle) + (pdb-decar 2 gimp-context-get-ink-blob-aspect-ratio) + (pdb-decar 2 gimp-context-get-ink-blob-type) + (pdb-decar 2 gimp-context-get-ink-size) + (pdb-decar 2 gimp-context-get-ink-size-sensitivity) + (pdb-decar 2 gimp-context-get-ink-speed-sensitivity) + (pdb-decar 2 gimp-context-get-ink-tilt-sensitivity) + (pdb-decar 2 gimp-context-get-interpolation) + (pdb-decar 2 gimp-context-get-opacity) + (pdb-decar 2 gimp-context-get-paint-method) + (pdb-decar 2 gimp-context-get-paint-mode) + (pdb-decar 2 gimp-context-get-palette) + (pdb-decar 2 gimp-context-get-pattern) + (pdb-bool 3 gimp-context-get-sample-merged) + (pdb-decar 2 gimp-context-get-sample-criterion) + (pdb-decar 2 gimp-context-get-sample-threshold) + (pdb-decar 2 gimp-context-get-sample-threshold-int) + (pdb-bool 3 gimp-context-get-sample-transparent) + (pdb-decar 2 gimp-context-get-transform-direction) + (pdb-decar 2 gimp-context-get-transform-recursion) + (pdb-decar 2 gimp-context-get-transform-resize) + (define (list-paint-methods) + (cadr (gimp-context-list-paint-methods))) + (pdb-pass 2 gimp-context-pop) + (pdb-pass 2 gimp-context-push) + (pdb-pass 2 gimp-context-set-antialias) + (pdb-pass 2 gimp-context-set-background) + (pdb-pass 2 gimp-context-set-brush) + (pdb-pass 2 gimp-context-set-brush-angle) + (pdb-pass 2 gimp-context-set-brush-aspect-ratio) + (pdb-pass 2 gimp-context-set-brush-default-size) + (pdb-pass 2 gimp-context-set-brush-size) + (pdb-pass 2 gimp-context-set-default-colors) + (pdb-pass 2 gimp-context-set-defaults) + (pdb-pass 2 gimp-context-set-dynamics) + (pdb-pass 2 gimp-context-set-feather) + (pdb-pass 2 gimp-context-set-feather-radius) + (pdb-pass 2 gimp-context-set-font) + (pdb-pass 2 gimp-context-set-foreground) + (pdb-pass 2 gimp-context-set-gradient) + (pdb-pass 2 gimp-context-set-ink-angle) + (pdb-pass 2 gimp-context-set-ink-blob-angle) + (pdb-pass 2 gimp-context-set-ink-blob-aspect-ratio) + (pdb-pass 2 gimp-context-set-ink-blob-type) + (pdb-pass 2 gimp-context-set-ink-size) + (pdb-pass 2 gimp-context-set-ink-size-sensitivity) + (pdb-pass 2 gimp-context-set-ink-speed-sensitivity) + (pdb-pass 2 gimp-context-set-ink-tilt-sensitivity) + (pdb-pass 2 gimp-context-set-interpolation) + (pdb-pass 2 gimp-context-set-opacity) + (pdb-pass 2 gimp-context-set-paint-method) + (pdb-pass 2 gimp-context-set-paint-mode) + (pdb-pass 2 gimp-context-set-palette) + (pdb-pass 2 gimp-context-set-pattern) + (pdb-pass 2 gimp-context-set-sample-criterion) + (pdb-pass 2 gimp-context-set-sample-merged) + (pdb-pass 2 gimp-context-set-sample-threshold) + (pdb-pass 2 gimp-context-set-sample-threshold-int) + (pdb-pass 2 gimp-context-set-sample-transparent) + (pdb-pass 2 gimp-context-set-transform-direction) + (pdb-pass 2 gimp-context-set-transform-recursion) + (pdb-pass 2 gimp-context-set-transform-resize) + (pdb-pass 2 gimp-context-swap-colors) + )) + +(define Channel + (make-environment + (pdb-pass 2 gimp-channel-combine-masks) + (pdb-decar 2 gimp-channel-copy) + (pdb-decar 2 gimp-channel-get-color) + (pdb-decar 2 gimp-channel-get-opacity) + (pdb-bool 3 gimp-channel-get-show-masked) + (pdb-decar 2 gimp-channel-new) + (pdb-decar 2 gimp-channel-new-from-component) + (pdb-pass 2 gimp-channel-set-color) + (pdb-pass 2 gimp-channel-set-opacity) + (pdb-pass 2 gimp-channel-set-show-masked) + )) + +(define Brush + (make-environment + (pdb-pass 2 gimp-brush-delete) + (pdb-decar 2 gimp-brush-duplicate) + (pdb-decar 2 gimp-brush-get-angle) + (pdb-decar 2 gimp-brush-get-aspect-ratio) + (pdb-decar 2 gimp-brush-get-hardness) + (pdb-pass 2 gimp-brush-get-info) + (define (get-pixels name) + (let ((value (gimp-brush-get-pixels name))) + (list (car value) ; width + (cadr value) ;height + (caddr value) ; mask bpp + (vector->list (car (cddddr value))) ; mask-bytes + (cadr (cddddr value)) ; color bpp + (vector->list (cadddr (cddddr value)))))) + (pdb-decar 2 gimp-brush-get-radius) + (pdb-decar 2 gimp-brush-get-shape) + (pdb-decar 2 gimp-brush-get-spacing) + (pdb-decar 2 gimp-brush-get-spikes) + (pdb-bool 2 gimp-brush-is-editable) + (pdb-bool 2 gimp-brush-is-generated) + (pdb-decar 2 gimp-brush-new) + (pdb-decar 2 gimp-brush-rename) + (pdb-decar 2 gimp-brush-set-angle) + (pdb-decar 2 gimp-brush-set-aspect-ratio) + (pdb-decar 2 gimp-brush-set-hardness) + (pdb-decar 2 gimp-brush-set-radius) + (pdb-decar 2 gimp-brush-set-shape) + (pdb-pass 2 gimp-brush-set-spacing) + (pdb-decar 2 gimp-brush-set-spikes) + )) + +(define Brushes + (make-environment + (pdb-pass 2 gimp-brushes-close-popup) + (define (get-list filter) + (cadr (gimp-brushes-get-list filter))) + (pdb-pass 2 gimp-brushes-popup) + (pdb-pass 2 gimp-brushes-refresh) + (pdb-pass 2 gimp-brushes-set-popup) + )) + +(define Buffer + (make-environment + (pdb-pass 2 gimp-buffer-delete) + (pdb-decar 2 gimp-buffer-get-bytes) + (pdb-decar 2 gimp-buffer-get-height) + (pdb-decar 2 gimp-buffer-get-image-type) + (pdb-decar 2 gimp-buffer-get-width) + (pdb-decar 2 gimp-buffer-rename) + )) + +(define Buffers + (make-environment + (define (get-list filter) + (cadr (gimp-buffers-get-list filter))) + )) + +(define Palette + (make-environment + (pdb-decar 2 gimp-palette-add-entry) + (pdb-pass 2 gimp-palette-delete) + (pdb-pass 2 gimp-palette-delete-entry) + (pdb-decar 2 gimp-palette-duplicate) + (pdb-decar 2 gimp-palette-entry-get-color) + (pdb-decar 2 gimp-palette-entry-get-name) + (pdb-pass 2 gimp-palette-entry-set-color) + (pdb-pass 2 gimp-palette-entry-set-name) + (define (get-colors name) + (vector->list (cadr (gimp-palette-get-colors name)))) + (pdb-decar 2 gimp-palette-get-columns) + (pdb-decar 2 gimp-palette-get-info) + (pdb-bool 2 gimp-palette-is-editable) + (pdb-decar 2 gimp-palette-new) + (pdb-decar 2 gimp-palette-rename) + (pdb-pass 2 gimp-palette-set-columns) + )) + +(define Palettes + (make-environment + (pdb-pass 2 gimp-palettes-close-popup) + (define (get-list filter) + (cadr (gimp-palettes-get-list filter))) + (pdb-pass 2 gimp-palettes-popup) + (pdb-pass 2 gimp-palettes-refresh) + (pdb-pass 2 gimp-palettes-set-popup) + )) + +(define Patterns + (make-environment + (pdb-pass 2 gimp-patterns-close-popup) + (define (get-list filter) + (cadr (gimp-patterns-get-list filter))) + (pdb-pass 2 gimp-patterns-popup) + (pdb-pass 2 gimp-patterns-refresh) + (pdb-pass 2 gimp-patterns-set-popup) + )) + +(define Pdb + (make-environment + (pdb-pass 3 gimp-procedural-db-dump) + (define (get-data identifier) + (vector->list (cadr (gimp-procedural-db-get-data identifier)))) + (pdb-decar 3 gimp-procedural-db-get-data-size) + (pdb-pass 3 gimp-procedural-db-proc-arg) + (pdb-bool 3 gimp-procedural-db-proc-exists) + (pdb-pass 3 gimp-procedural-db-proc-info) + (pdb-pass 3 gimp-procedural-db-proc-val) + (define (query name blurb help author copyright date type) + (cadr (gimp-procedural-db-query name blurb help author copyright date type))) + (define (set-data name lis) + (gimp-procedural-db-set-data name (length lis) (list->vector lis))) + (pdb-decar 3 gimp-procedural-db-temp-name) + )) + +(define Plugin + (make-environment + (pdb-pass 2 gimp-plugin-domain-register) + (pdb-pass 2 gimp-plugin-get-pdb-error-handler) + (pdb-pass 2 gimp-plugin-help-register) + (pdb-pass 2 gimp-plugin-icon-register) + (pdb-pass 2 gimp-plugin-menu-branch-register) + (pdb-pass 2 gimp-plugin-menu-register) + (pdb-decar 2 gimp-plugin-set-pdb-error-handler) + )) + +(define Progress + (make-environment + (pdb-pass 2 gimp-progress-cancel) + (pdb-pass 2 gimp-progress-end) + (pdb-decar 2 gimp-progress-get-window-handle) + (pdb-pass 2 gimp-progress-init) + (pdb-pass 2 gimp-progress-install) + (pdb-pass 2 gimp-progress-pulse) + (pdb-pass 2 gimp-progress-set-text) + (pdb-pass 2 gimp-progress-uninstall) + (pdb-pass 2 gimp-progress-update) + )) + +(define Selection + (make-environment + (pdb-pass 2 gimp-selection-all) + (pdb-pass 2 gimp-selection-border) + (pdb-pass 2 gimp-selection-bounds) + (pdb-pass 2 gimp-selection-feather) + (pdb-decar 2 gimp-selection-float) + (pdb-pass 2 gimp-selection-grow) + (pdb-pass 2 gimp-selection-invert) + (pdb-bool 2 gimp-selection-is-empty) + (pdb-pass 2 gimp-selection-none) + (pdb-decar 2 gimp-selection-save) + (pdb-pass 2 gimp-selection-sharpen) + (pdb-pass 2 gimp-selection-shrink) + (pdb-pass 2 gimp-selection-translate) + (pdb-decar 2 gimp-selection-value) + )) + +(define Text + (make-environment + (pdb-decar 2 gimp-text-fontname) ; note the prefix + (define (antialias? layer) + (not (zero? (car (gimp-text-layer-get-antialias layer))))) + (pdb-decar 3 gimp-text-layer-get-base-direction) + (pdb-decar 3 gimp-text-layer-get-color) + (pdb-pass 2 gimp-text-get-extents-fontname) + (pdb-decar 3 gimp-text-layer-get-font) + (pdb-pass 3 gimp-text-layer-get-font-size) + (pdb-decar 3 gimp-text-layer-get-hint-style) + (pdb-decar 3 gimp-text-layer-get-indent) + (pdb-decar 3 gimp-text-layer-get-justification) + (define (kerning? layer) + (not (zero? (car (gimp-text-layer-get-kerning layer))))) + (pdb-decar 3 gimp-text-layer-get-language) + (pdb-decar 3 gimp-text-layer-get-letter-spacing) + (pdb-decar 3 gimp-text-layer-get-line-spacing) + (pdb-decar 3 gimp-text-layer-get-markup) + (pdb-decar 3 gimp-text-layer-get-text) + (pdb-decar 3 gimp-text-layer-new) + (pdb-pass 3 gimp-text-layer-resize) + (pdb-pass 3 gimp-text-layer-set-antialias) + (pdb-pass 3 gimp-text-layer-set-base-direction) + (pdb-pass 3 gimp-text-layer-set-color) + (pdb-pass 3 gimp-text-layer-set-font) + (pdb-pass 3 gimp-text-layer-set-font-size) + (pdb-pass 3 gimp-text-layer-set-hint-style) + (pdb-pass 3 gimp-text-layer-set-hinting) + (pdb-pass 3 gimp-text-layer-set-indent) + (pdb-pass 3 gimp-text-layer-set-justification) + (pdb-pass 3 gimp-text-layer-set-kerning) + (pdb-pass 3 gimp-text-layer-set-language) + (pdb-pass 3 gimp-text-layer-set-letter-spacing) + (pdb-pass 3 gimp-text-layer-set-line-spacing) + (pdb-pass 3 gimp-text-layer-set-text) + )) + +(define Unit + (make-environment + (pdb-decar 2 gimp-unit-get-abbreviation) + (pdb-decar 2 gimp-unit-get-deletion-flag) + (pdb-decar 2 gimp-unit-get-digits) + (pdb-decar 2 gimp-unit-get-factor) + (pdb-decar 2 gimp-unit-get-identifier) + (pdb-decar 2 gimp-unit-get-number-of-built-in-units) + (pdb-decar 2 gimp-unit-get-number-of-units) + (pdb-decar 2 gimp-unit-get-plural) + (pdb-decar 2 gimp-unit-get-singular) + (pdb-decar 2 gimp-unit-get-symbol) + (pdb-decar 2 gimp-unit-new) + (pdb-pass 2 gimp-unit-set-deletion-flag) + )) + +(define Vectors + (make-environment + (pdb-pass 2 gimp-vectors-bezier-stroke-conicto) + (pdb-pass 2 gimp-vectors-bezier-stroke-cubicto) + (pdb-pass 2 gimp-vectors-bezier-stroke-lineto) + (pdb-decar 2 gimp-vectors-bezier-stroke-new-ellipse) + (pdb-decar 2 gimp-vectors-bezier-stroke-new-moveto) + (pdb-decar 2 gimp-vectors-copy) + (pdb-pass 2 gimp-vectors-export-to-file) + (pdb-pass 2 gimp-vectors-export-to-string) + (define (get-strokes vectors) + (vector->list (cadr (gimp-vectors-get-strokes vectors)))) + (define (import-from-file image filename merge scale) + (vector->list (cadr (gimp-vectors-import-from-file image + filename + merge + scale)))) + (define (import-from-string image string merge scale) + (vector->list (cadr (gimp-vectors-import-from-string image + string + (string-length string) + merge + scale)))) + (pdb-decar 2 gimp-vectors-new) + (pdb-pass 2 gimp-vectors-new-from-text-layer) + (pdb-pass 2 gimp-vectors-remove-stroke) + (pdb-pass 2 gimp-vectors-stroke-close) + (pdb-pass 2 gimp-vectors-stroke-flip) + (pdb-pass 2 gimp-vectors-stroke-flip-free) + (define (stroke-new-from-points vectors type points closed) + (car (gimp-vectors-stroke-new-from-points vectors + type + (length points) + (list->vector points) + closed))) + (pdb-decar 2 gimp-vectors-stroke-get-length) + (pdb-pass 2 gimp-vectors-stroke-get-point-at-dist) + (define (stroke-get-points vectors stroke-id) + (let ((info (gimp-vectors-stroke-get-points vectors stroke-id))) + (list (car info) + (vector->list (caddr info)) + (not (zero? (cadddr info)))))) + (define (stroke-interpolate vectors stroke-id precision) + (let ((info (gimp-vectors-stroke-interpolate vectors stroke-id precision))) + (list (vector->list (cadr info)) + (not (zero? (caddr info)))))) + (pdb-pass 2 gimp-vectors-stroke-rotate) + (pdb-pass 2 gimp-vectors-stroke-scale) + (pdb-pass 2 gimp-vectors-stroke-translate) + )) + +(define Tool + (make-environment + (define (airbrush drawable pressure strokes) + (gimp-airbrush drawable pressure (length strokes) (list->vector strokes))) + (define (airbrush-default drawable strokes) + (gimp-airbrush drawable (length strokes) (list->vector strokes))) + (pdb-pass 1 gimp-brightness-contrast) + (define (clone drawable src-drawable type src-x src-y strokes) + (gimp-clone drawable src-drawable type src-x src-y (length strokes) (list->vector strokes))) + (define (clone-default drawable strokes) + (gimp-clone-default drawable (length strokes) (list->vector strokes))) + (pdb-pass 1 gimp-color-balance) + (pdb-pass 1 gimp-colorize) + (define (convolve drawable pressure type strokes) + (gimp-convolve drawable pressure type (length strokes) (list->vector strokes))) + (define (convolve-default drawable strokes) + (gimp-convolve-default drawable (length strokes) (list->vector strokes))) + (define (curves-explicit drawable channel curve) + (gimp-curves-explicit drawable channel (length curve) (list->vector curve))) + (define (curves-spline drawable channel control-pts) + (gimp-curves-spline drawable channel (length control-pts) (list->vector control-pts))) + (define (dodgeburn drawable exposure type mode strokes) + (gimp-dodgeburn drawable exposure type mode (length strokes) (list->vector strokes))) + (define (dodgeburn-default drawable strokes) + (gimp-dodgeburn-default drawable (length strokes) (list->vector strokes))) + (define (eraser drawable strokes hardness method) + (gimp-eraser drawable (length strokes) (list->vector strokes) hardness method)) + (define (eraser-default drawable strokes) + (gimp-eraser-default drawable (length strokes) (list->vector strokes))) + (pdb-pass 1 gimp-equalize) ; Note: https://bugzilla.gnome.org/show_bug.cgi?id=745313 + (define (heal drawable src-drawable src-x src-y strokes) + (gimp-heal drawable src-drawable src-x src-y (length strokes) (list->vector strokes))) + (define (heal-default drawable strokes) + (gimp-heal-default drawable (length strokes) (list->vector strokes))) + (pdb-pass 1 gimp-histogram) + (pdb-pass 1 gimp-hue-saturation) + (pdb-pass 1 gimp-invert) + (define (paintbrush drawable fade strokes method gradient-length) + (gimp-paintbrush drawable fade (length strokes) (list->vector strokes) method gradient-length)) + (define (paintbrush-default drawable strokes) + (gimp-paintbrush-default drawable (length strokes) (list->vector strokes))) + (define (pencil drawable strokes) + (gimp-pencil drawable (length strokes) (list->vector strokes))) + (define (posterize levels) + (gimp-posterize levels)) + (define (smudge drawable pressure strokes) + (gimp-smudge drawable pressure (length strokes) (list->vector strokes))) + (define (smudge-default drawable strokes) + (gimp-smudge-default drawable (length strokes) (list->vector strokes))) + )) + +(define Gradient + (make-environment + (pdb-pass 2 gimp-gradient-delete) + (pdb-decar 2 gimp-gradient-duplicate) + (define (get-custom-samples name positions reverse) + (vector->list (cadr (gimp-gradient-get-custom-samples name + (length positions) + (list->vector positions) + reverse)))) + (define (get-list filter) + (cadr (gimp-gradients-get-list filter))) + (pdb-decar 2 gimp-gradient-get-number-of-segments) + (define (get-uniform-samples name num-samples reverse) + (vector->list (cadr (gimp-gradient-get-uniform-samples name + num-samples + reverse)))) + (pdb-bool 2 gimp-gradient-is-editable) + (pdb-decar 2 gimp-gradient-new) + (pdb-decar 2 gimp-gradient-rename) + (pdb-decar 2 gimp-gradient-segment-get-blending-function) + (pdb-decar 2 gimp-gradient-segment-get-coloring-type) + (pdb-pass 2 gimp-gradient-segment-get-left-color) + (pdb-decar 2 gimp-gradient-segment-get-left-pos) + (pdb-decar 2 gimp-gradient-segment-get-middle-pos) + (pdb-pass 2 gimp-gradient-segment-get-right-color) + (pdb-decar 2 gimp-gradient-segment-get-right-pos) + (pdb-pass 2 gimp-gradient-segment-range-blend-colors) + (pdb-pass 2 gimp-gradient-segment-range-blend-opacity) + (pdb-pass 2 gimp-gradient-segment-range-delete) + (pdb-pass 2 gimp-gradient-segment-range-flip) + (pdb-decar 2 gimp-gradient-segment-range-move) + (pdb-pass 2 gimp-gradient-segment-range-redistribute-handles) + (pdb-pass 2 gimp-gradient-segment-range-replicate) + (pdb-pass 2 gimp-gradient-segment-range-set-blending-function) + (pdb-pass 2 gimp-gradient-segment-range-set-coloring-type) + (pdb-pass 2 gimp-gradient-segment-range-split-midpoint) + (pdb-pass 2 gimp-gradient-segment-range-split-uniform) + (pdb-pass 2 gimp-gradient-segment-set-left-color) + (pdb-decar 2 gimp-gradient-segment-set-left-pos) + (pdb-decar 2 gimp-gradient-segment-set-middle-pos) + (pdb-decar 2 gimp-gradient-segment-set-right-pos) + (pdb-pass 2 gimp-gradient-segment-set-right-color) + (pdb-pass 2 gimp-gradients-close-popup) + )) +(define Fu + (make-environment + (define (list-head lis k) + (let recur ((lis lis) (k k)) + (if (zero? k) + '() + (cons (car lis) + (recur (cdr lis) (- k 1)))))) + (define list-tail list-tail) + + ;; Note that there exists a 'foldr' in Script-fu by default (defined in + ;; the "script-fu-init.scm" file); however, this 'foldr' function is + ;; inconsistent with either right or left folding per the traditional + ;; definitions of these structural transformations. ('foldr' is similar + ;; to 'fold' except that the arguments expected by 'op' are in + ;; reversed order). + + ; per SRFI-1 (http://srfi.schemers.org/srfi-1/srfi-1.html) + (define (fold op accum lis) + (if (null? lis) + accum + (fold op (op (car lis) accum) (cdr lis)) )) + ; per SRFI-1 + (define (fold-right op accum lis) + (if (null? lis) + accum + (op (fold-right op accum (cdr lis)) (car lis)) )) + ; per SRFI-1 + (define (partition pred? lis) + (let loop ((lis lis) + (result '(() ()))) + (if (null? lis) + (map reverse result) + (loop (cdr lis) + (if (pred? (car lis)) + (list (cons (car lis) (car result)) + (cadr result)) + (list (car result) + (cons (car lis) (cadr result)))))))) + ; per R5RS (Script-fu's number->string only supports decimal radix + (define (number->string z . args) + (let ((radix (if (null? args) + 10 + (car args)))) + (cond + ((= radix 10) + (anyatom->string z number?)) + ((not (member radix '(2 8 16))) + (error "Unsupported numeric radix")) + ((inexact? z) + (error "Conversion of inexact number to non-decimal radix")) + (else + (let ((digits (vector "0" "1" "2" "3" "4" "5" "6" "7" + "8" "9" "a" "b" "c" "d" "e" "f")) + (val (abs z)) + (sign (if (< z 0) "-" ""))) + (if (< val radix) + (string-append sign (vector-ref digits val)) + (let loop ((num val) + (order (inexact->exact (truncate (/ (log val) + (log radix))))) + (result sign)) + (let* ((power (expt radix order)) + (digit (quotient num power))) + (if (zero? order) + (string-append result (vector-ref digits digit)) + (loop (- num (* digit power)) + (pred order) + (string-append result (vector-ref digits digit)))))))))))) + ; per SRFI-1 + (define (filter pred lis) + (let loop ((lis lis)) + (if (null? lis) + lis + (let ((head (car lis)) + (tail (cdr lis))) + (if (pred head) + (let ((new-tail (loop tail))) ; Replicate 'loop' so + (if (eq? tail new-tail) + lis + (cons head new-tail))) + (loop tail)))))) ; this one can be a tail call. + ; per SRFI-13 + (define (string-reverse str) + (let loop ((i (string-length str)) + (result "")) + (if (zero? i) + result + (loop (pred i) + (string-append result (make-string 1 (string-ref str (pred i)))))))) + ; per SRFI-13 without optional start/end + ; skip over all characters matching the pred + (define (string-trim str . args) + (let ((pred (if (pair? args) (car args) #\space)) + (end (string-length str))) + (cond + ((char? pred) + (let loop ((i 0)) + (if (or (>= i end) (not (char=? pred (string-ref str i)))) + (substring str i end) + (loop (succ i))))) + (else + (let loop ((i 0)) + (if (or (>= i end) (not (pred (string-ref str i)))) + (substring str i end) + (loop (succ i)))))))) + ; per SRFI-13 without start/end (also does not truncate str) + (define (string-pad str n . args) + (let* ((char (if (pair? args) (car args) #\space)) + (len (string-length str))) + (if (<= n len) + str + (string-append (make-string (- n len) char) + str)))) + ; per SRFI-13 without start/end + (define (string-delete pred str) + (let ((end (string-length str))) + (when (char? pred) + (set! pred + (let ((char pred)) + (lambda (x) (char=? x char))))) + (let loop ((i 0) + (result "")) + (if (>= i end) + result + (loop (succ i) + (let ((char (string-ref str i))) + (if (pred char) ; for 'string-filter' swap consequents + result + (string-append result (make-string 1 char))))))))) + ; NOT per SRFI-1 + (define (remove lis items) + (if (not (pair? items)) + (set! items (list items))) + (let loop ((items items) + (lis lis)) + (if (null? items) + lis + (loop (cdr items) + (cadr (Fu::partition (lambda (x) (equal? (car items) x)) + lis)))))) + ; Return a list of every nth item (n specified by 'step') + ; For example to get a list of the x-coordinates of the anchors + ; of a path stroke: + ; (Fu::nths points 6 2) + ; The y-coordinates of the anchors would be: + ; (Fu::nths points 6 3) + (define (nths lis step . offset) + (define (butn lis n) + (if (or (zero? n) (null? lis)) + lis + (butn (cdr lis) (- n 1)))) + (if (pair? offset) + (nths (butn lis (car offset)) step) + (if (null? lis) + lis + (cons (car lis) (nths (butn lis step) step))))) + + ; 'Fu::get-bounds' accepts either the ID of a single drawable or a list + ; of IDs. If a list then the bounds of all drawables in the list + ; is returned. + (define (get-bounds drawables) + (unless (list? drawables) + (set! drawables (list drawables))) + (let* ((x1s (map car (map gimp-drawable-offsets drawables))) + (y1s (map cadr (map gimp-drawable-offsets drawables))) + (x2s (map + (map car (map gimp-drawable-width drawables)) + x1s)) + (y2s (map + (map car (map gimp-drawable-height drawables)) + y1s))) + (list (apply min x1s) + (apply min y1s) + (apply max x2s) + (apply max y2s)))) + + ;; 'Fu::grow-while' returns the largest integer that satisfies + ;; the 'pred'. The pred function should accept a single "size" + ;; argument and return #t if the size fits the criteria. (See + ;; Fu::calc-fontsize for an example of usage.) + ; + (define (grow-while pred . args) + (define (avg a b) + (inexact->exact (truncate (/ (+ a b) 2)))) + (let ((start (if (null? args) 1 (car args)))) + (let loop ((size start) + (lower start) + (upper (* 2 start)) + (last 0) + (unbounded? #t) ) + (if (= last size) + size + (if (pred size) + (if unbounded? + (loop (* 2 size) + size + (* size 2) + size + #t) + (loop (avg size upper) + size + upper + size + #f)) + (loop (avg lower size) + lower + size + size + #f)))))) + + ;; 'calc-fontsize' finds the largest size of the given font that + ;; will fit within the given dimensions. + ; + (define (calc-fontsize text font width height) + (define (fits? fontsize) + (let ((extents (gimp-text-get-extents-fontname text fontsize PIXELS font))) + (and (<= (car extents) width) (<= (cadr extents) height)))) + (grow-while fits? 6)) + + ; align layer with the image, the selection, a base layer, a pair (x . y), + ; or list (x y w h). + ; 'base' specification: + ; 0 image + ; 1 selection + ; >1 a layer + ; pair (x . y) + ; list (x y w h) + ; + ; reference locations + ; 1 2 3 1=top-left 2=top-center 3=top-right + ; \ / + ; 4-5-6 4=center-left 5=center 6 center-right + ; / \ + ; 7 8 9 7=bottom-left 8=bottom-center 9=bottom-right + ; + ; There are two reference location specifications and both are + ; optional. If neither the source reference or the target (base) + ; references are provided, the source layer will be center aligned + ; with the target center. If just one reference is provided, it is + ; used for both the source and the target (e.g., if the ref argument + ; is "2" then the top-center of the layer will be aligned with the + ; top-center of the base. + ; If both references are provided then the first ref specifies the + ; location on the source and the second specifies the location on the + ; target. This allows more advanced alignments such as having the + ; top-left of the source layer align with the bottom-right of the + ; target. + ; Note:: it is possible (and sometimes useful) to align a layer + ; with itself. For example, to move a corner to the center: + ; (Fu::align layer layer 1 5) + ; + (define (align layer base . refs) + (let* ((image (Item::get-image layer)) + (lx (car (Drawable::offsets layer))) + (ly (cadr (Drawable::offsets layer))) + (aref (if (pair? refs) + (car refs) + 5)) + (bref (if (and (pair? refs) (pair? (cdr refs))) + (cadr refs) + aref)) + (bx (cond + ((list? base) + (list (car base) + (truncate (+ (car base) (/ (caddr base) 2))) + (+ (car base) (caddr base)))) + ((pair? base) + (make-list 3 (car base))) + ((zero? base) + (let ((width (Image::width image))) + (list 0 (truncate (/ width 2)) width))) + ((= base 1) + (let ((bounds (cdr (Selection::bounds image)))) + (list (car bounds) + (truncate (/ (+ (car bounds) (caddr bounds)) 2)) + (caddr bounds)))) + (else + (let ((x (car (Drawable::offsets base))) + (w (Drawable::width base))) + (list x (+ x (truncate (/ w 2))) (+ x w)))))) + (by (cond + ((list? base) + (list (cadr base) + (truncate (+ (cadr base) (/ (cadddr base) 2))) + (+ (cadr base) (cadddr base)))) + ((pair? base) + (make-list 3 (cdr base))) + ((zero? base) + (let ((height (Image::height image))) + (list 0 (truncate (/ height 2)) height))) + ((= base 1) + (let ((bounds (cdr (Selection::bounds image)))) + (list (cadr bounds) + (truncate (/ (+ (cadr bounds) (cadddr bounds)) 2)) + (cadddr bounds)))) + (else + (let ((y (cadr (Drawable::offsets base))) + (h (Drawable::height base))) + (list y (+ y (truncate (/ h 2))) (+ y h)))))) + (ax (case aref + ((1 4 7) + lx) + ((2 5 8) + (let ((w (Drawable::width layer))) + (+ lx (truncate (/ w 2))))) + ((3 6 9) + (+ lx (Drawable::width layer))))) + (ay (case aref + ((1 2 3) + ly) + ((4 5 6) + (let ((h (Drawable::height layer))) + (+ ly (truncate (/ h 2))))) + ((7 8 9) + (+ ly (Drawable::height layer)))))) + (set! bx (case bref + ((1 4 7) + (car bx)) + ((2 5 8) + (cadr bx)) + ((3 6 9) + (caddr bx)))) + (set! by (case bref + ((1 2 3) + (car by)) + ((4 5 6) + (cadr by)) + ((7 8 9) + (caddr by)))) + (gimp-layer-set-offsets layer + (- bx (- ax lx)) + (- by (- ay ly))))) + + ;; 'progression' returns a list of N elements from start to end with either + ;; algebraic/linear growth or geometric/exponential growth + ;; + ;; (progression start end num-elements growth) + ;; the num-elements and growth arguments are optional + ;; If 'num-elements' is not specified then a simple integer sequence is + ;; generated. + ;; If 'growth' is not specified or is #f then a linear progression is + ;; generated, otherwise a geometric progression. For geometric + ;; progressions, 'start must be > 0. + ;; Examples: + ;; (progression 1 5) ==> (1 2 3 4 5) + ;; (progression 1 5 5) ==> (1 2 3 4 5) + ;; (progression 1 5 6) ==> (1 1.8 2.6 3.4 4.2 5) + ;; (progression 1 1000 4 TRUE) ==> (1 10 100 1000) + ; + (define (progression start end . args) + (when (null? args) + (set! start (truncate start)) + (set! end (truncate end)) ) + (let ((num-elements (if (null? args) (+ (- end start) 1) (car args))) + (growth (and (pair? args) + (and (pair? (cdr args)) + (cadr args) )))) + (let ((adjustment (if growth ; #f=linear, else exponential + (pow (/ end start) (/ (pred num-elements))) ; nth root + (/ (- end start) (pred num-elements)) )) ; step size + (op (if growth * +)) ) + (let loop ((count (- num-elements 2)) + (result (list start))) + (if (<= count 0) + (let ((result (reverse (cons end result)))) + (if (null? args) + (map inexact->exact result) + result)) + (loop (pred count) + (cons (op adjustment (car result)) result) )))))) + + ; This sort is an optimized merge sort. + ; + (define (sort < lis) + (define (dosort < lis n) + (if (= n 1) + (list (car lis)) + (let ((i (quotient n 2))) + (domerge < + (dosort < lis i) + (dosort < (list-tail lis i) (- n i)))))) + (define (domerge < lis1 lis2) + (cond + ((null? lis1) + lis2) + ((null? lis2) + lis1) + ((< (car lis2) (car lis1)) + (cons (car lis2) (domerge < lis1 (cdr lis2) ))) + (else + (cons (car lis1) (domerge < (cdr lis1) lis2))))) + + (if (null? lis) + lis + (dosort < lis (length lis)))) + )) + +;; Schemey-fu also provides some useful macros +(define-macro (with-context . body) + `(begin + (gimp-context-push) + ,@body + (gimp-context-pop) )) + +(define-macro (with-undo image . body) + `(begin + (gimp-image-undo-group-start ,image) + ,@body + (gimp-image-undo-group-end ,image) )) + +; If provided a list for the first argument, 'with-selection' will +; use second item in the list as a channel name, which can be used +; from within the body to reload the selection. +; If provided an image-id for the first argument, then the saved +; selection will be unnamed. +(define-macro (with-selection image . body) + (let ((channel (if (pair? image) + (cadr image) + (gensym)))) + (if (pair? image) + (set! image (car image))) + `(let ((,channel (car (gimp-selection-save ,image)))) + ,@body + (gimp-selection-load ,channel) + (gimp-image-remove-channel ,image ,channel) ))) + +;; 'with-xywh' creates bindings whose scope is local to the +;; 'body'. It accepts as its first argument a list of four symbols +;; that respectively correspond to the variable names for the +;; x-offset, y-offset, width, and height of the 'drawable'. +;; If 'bindings' is empty then the default variable names are +;; x, y, w, and h. +; +(define-macro (with-xywh bindings drawable . body) + (if (null? bindings) + (set! bindings '(x y w h))) + `(let ((,(car bindings) (car (Drawable::offsets ,drawable))) + (,(cadr bindings) (cadr (Drawable::offsets ,drawable))) + (,(caddr bindings) (Drawable::width ,drawable)) + (,(cadddr bindings) (Drawable::height ,drawable))) + ,@body)) +