It is currently Thu Jul 04, 2024 4:42 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 27 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: How to update deprecated functions? Part 1: the easy way
PostPosted: Thu Nov 18, 2021 3:18 pm  (#21) 
Offline
GimpChat Member
User avatar

Joined: Oct 23, 2021
Posts: 67
1. Change of name of deprecated functions: level easy

Firstly, we list the deprecated API found in Aqua btn.
Then, in the second line, we align the parameters of the modern function of replacement found in PDB. :coolthup

(gimp-blend      drawable blend-mode paint-mode gradient-type opacity offset repeat reverse supersample max-depth threshold dither x1 y1 x2 y2)
(gimp-edit-blend drawable blend-mode paint-mode gradient-type opacity offset repeat reverse supersample max-depth threshold dither x1 y1 x2 y2)

(gimp-drawable-set-visible item visible)
(gimp-item-set-visible     item visible)

(gimp-gradients-set-gradient name)
(gimp-context-set-gradient   name)

(gimp-palette-set-background background)
(gimp-context-set-background background)

(gimp-palette-set-foreground foreground)
(gimp-context-set-foreground foreground)

(gimp-patterns-set-pattern name)
(gimp-context-set-pattern  name)

(gimp-drawable-set-name item name)
(gimp-item-set-name     item name)

(gimp-image-raise-layer-to-top image item)
(gimp-image-raise-item-to-top  image item)

(gimp-image-lower-layer-to-bottom image item)
(gimp-image-lower-item-to-bottom  image item)

(gimp-image-lower-layer image item)
(gimp-image-lower-item  image item)

Notepad++ menu "Search" > "Replace..."
Find what: copy and paste the name of the deprecated function without parameters
Replace with: copy and paste the name of the modern function without parameters

:geekon We call this basic replacement a rule of rewriting. :keybdtype

Be sure to process the longest functions before any short functions.
For example: replace "gimp-image-lower-layer-to-bottom" before "gimp-image-lower-layer" that begins with the same tokens. :otb


Top
 Post subject: Introducing regular expressions
PostPosted: Fri Nov 19, 2021 3:26 pm  (#22) 
Offline
GimpChat Member
User avatar

Joined: Oct 23, 2021
Posts: 67
2. Insert a new parameter: level average

This article is a short introduction of so called "regular expression" to capture symbolically tokens inside a string.
Consider the couple of deprecated/modern functions:
(gimp-image-add-layer    image layer        position)
(gimp-image-insert-layer image layer parent position)

The parent parameter has been added in the modern function between layer and position.

Notepad++ menu "Search" > "Replace..."

[x] Regular expression :yesnod check this option that changes the semantic of letter preceded with backslash.

The rule of rewriting becomes:
Find what: :geek:
gimp-image-add-layer\s+(\w+\s+\w+)

Replace with: :sglasses
gimp-image-insert-layer \1 0

In Find what:
"\s" captures one white-space
"\s+" captures one white-space or more

"\w+" captures one or more letter or number that is to say a word. Minus "-" will be processed soon.
In our case, the first "\w+" captures the first parameter for example "image" or often "img" in Aqua btn.
So the second "\w+" captures the second parameter corresponding to "layer".

:arrow: We build our first group between parenthesis: "(\w+\s+\w+)" to capture both "image" + "layer"
because we wish to insert a new parameter called parent after this group.
Its meaning is the first parameter image + one or more white-space + the second parameter layer.

:idea: The idea is that we can symbolically reuse this group in the "Replace with" input area.
The name of the first group --that is to say the first expression between parenthesis-- is automatically named "\1".

Hence the 'Replace with' input string that includes:
  • the modern function gimp-image-insert-layer;
  • a single space before the first parameter;
  • our first group code-named "\1" containing both the first parameter and the second parameter;
  • "0" which is the compatible parent since we do not need to create any group of layers.

:help With PDB, consult the help of the modern function for more information about the new parameter parent.

Take in consideration that we do not process the last parameter "position" :nono
because it does not change of position :pipe relatively to the new parameter "parent". :)


Last edited by AlSchemist on Sat Nov 20, 2021 6:18 am, edited 1 time in total.

Top
 Post subject: Minus in the name of Scheme variable
PostPosted: Sat Nov 20, 2021 6:13 am  (#23) 
Offline
GimpChat Member
User avatar

Joined: Oct 23, 2021
Posts: 67
3. Minus-in-the-name-of-Scheme variable: level average++

In Scheme, minus "-" is both:
  • the operator of subtraction
  • an acceptable character in the name of a parameter.
The entire Gimp API uses minus as separator.

It is also true for the parameters. For example, the Aqua btn variable "acopy-mask" include minus inside its name.
The only problem is that the regular expression "\w+" captures only the first part "acopy" of the name before minus.

To be compatible with the Scheme syntax of variable, we need to add minus in the definition of a element of word.
To do that, we search the definition of "\w" as an interval of regular expressions: "[_\d\l\u]"
with:
  • "_" underscore is valid character of variable
  • "\d" standing for digit
  • "\l" standing for lower letter
  • "\u" standing for upper letter


In addition of underscore "_", we add in the interval the minus "-" character.
However minus "-" is also a specific operator :hoh to define an interval between characters. For example "[0-9]" captures a digit.
That is why, we need to use a backslash before minus. :gaah

So the rule of rewriting becomes more complex: :geek
Find what:
gimp-image-add-layer\s+([_\-\d\l\u]+\s+[_\-\d\l\u]+)

Replace with:
gimp-image-insert-layer \1 0


Before running this rule of rewriting in Notepad++,
try to only find the deprecated function with the group of its two first --full-- parameters as an exercice to validate the improved regular expression. :mcof


Top
 Post subject: Moving parameters in the modern Script-Fu function
PostPosted: Sun Nov 21, 2021 9:49 am  (#24) 
Offline
GimpChat Member
User avatar

Joined: Oct 23, 2021
Posts: 67
4. Move parameter: level difficult

Let us progress in the Notepad++ regular expressions to replace more complex deprecated functions.

In the deprecated function gimp-ellipse-select, the 6th parameter operation is moved after image.
(gimp-ellipse-select       image           x y width height operation antialias feather feather-radius)
(gimp-image-select-ellipse image operation x y width height)

Not only operation is moved before its actual position :wh
but the three last parameters antialias, feather and feather-radius disappear in the modern function. :bye

In Aqua btn, a real example could be moving the operation "CHANNEL-OP-REPLACE" just after "img":
(gimp-ellipse-select       img                    xscale40 yscale40 radius4 radius4 CHANNEL-OP-REPLACE TRUE 0 0)
(gimp-image-select-ellipse img CHANNEL-OP-REPLACE xscale40 yscale40 radius4 radius4)

To capture all constants beginning with "CHANNEL", the regular expression starts with:
CHANNEL.*

Since the multiple joker ".*" matches everything such as CHANNEL-OP-REPLACE but also CHANNEL-OP-SUBTRACT, etc.,
it is usual to indicate a separator after the constant to indicate the end of the constant.
We already know the separator "\s+" capturing at least one white-space or more.

The regular expression to capture the entire constant and its next separator becomes:
CHANNEL.*\s+


Hereafter enclosed is the "simplified" rule of rewriting using "\w+" without minus in the name of parameter:

Notepad++ menu "Search" > "Replace..."
[x] Regular expression

Find what: :geek:
gimp-ellipse-select\s+(\w+)\s+(.*)\s+(CHANNEL.*)\s+\w+\s+\d+\s+\d+\)


Replace with: :sglasses
gimp-image-select-ellipse \1 \3 \2\)

We have three groups and the third group "\3" has been moved between the two first groups "\1" and "\2". :clap


Top
 Post subject: Regex ".*" is greedy
PostPosted: Sun Nov 21, 2021 10:31 am  (#25) 
Offline
GimpChat Member
User avatar

Joined: Oct 23, 2021
Posts: 67
4.1 Scope of the capture of the multiple joker ".*"

The second group is delimited between two separators:
\s+(.*)\s+

This regular expression (regex) could be interpreted as capturing " xscale40 " that is to say the first parameter.
However this regular expression is greedy or gluttonous. :pizza
It captures the maximum possible of input until the supplied constant "(CHANNEL.*)\s+".
That is why group2 captures " xscale40 yscale40 radius4 radius4 ".

:arrow: Always before replacing, test your regex-based searching until you reach exactly the wished capture.

The third group code-named operation that is to say "(CHANNEL.*)" will be inserted between the two first groups.

Please, pay attention that the rewriting rule takes in consideration --for the very first time-- the three last parameters --a word and two numbers--
"TRUE 0 0)" until the closing parenthesis. Always focus on the separators of the language.

:idea: The idea is to remove those three last parameters in the modern function replacing them only with the closing parenthesis.

\s+\w+\s+\d+\s+\d+\)

  • "\w+" matches "TRUE"
  • "\d+" matches "0"
  • the last "\d+" matches also "0"
  • "\)" matches ")". We need to backslash the closing parenthesis to avoid the known semantic of group.

Moreover let us indicate another similar example:
Again in the legacy function gimp-rect-select, the two last parameters "feather feather-radius" will be forgotten. :wizwand

Existing deprecated function: :geek:
(gimp-rect-select            image           x y width height operation feather feather-radius)


Goal of the replacement with the modern function: :sglasses
(gimp-image-select-rectangle image operation x y width height)


The rewriting rule could be defined by the following:

Notepad++ menu "Search" > "Replace..."
[x] Regular expression

Find what: :geek:
gimp-rect-select\s+(\w+)\s+(.*)\s+(CHANNEL.*)\s+\d+\s+\d+\)


Replace with: :sglasses
gimp-image-select-rectangle \1 \3 \2\)

Please note that in the "Replace with", the closing parenthesis does not need to be backslashed.


Top
 Post subject: Capturing one newline
PostPosted: Sun Nov 21, 2021 10:51 am  (#26) 
Offline
GimpChat Member
User avatar

Joined: Oct 23, 2021
Posts: 67
4.2 Newline inside the parameters

In Aqua btn, the previous regular expressions work if the deprecated function is on only one line.
What appends if the list of parameters is split on two lines.
             (gimp-ellipse-select img (*  40 xscale) (*  40 yscale)
                             (* 2 radius2) (* 2 radius2) CHANNEL-OP-REPLACE TRUE 0 0)

After the Notepad++ option [x] Regular expression, checking [x] . matches newline
will capture to many functions until the last constant "CHANNEL" of the file. The regular expression is too greedy. :pac

Of course, you can replace manually the deprecated functions without using regular expressions.
It depends of the number of the replacements.

However searching in the online help of Notepad++ regular expression,
you may wish to try the "/R" regular expression that captures EOL (End of line).
EOL depends on the operating system:
  • Windows: "\r\n"
  • Linux and macOS: "\n"
  • classic Mac OS: "\r"

Notepad++ menu "Search" > "Replace..."
[x] Regular expression

Find what: :geek:
gimp-ellipse-select\s+(\w+)\s+(.*\R{1}.*)\s+(CHANNEL.*)\s+\d+\s+\d+\)

We accept only one newline "\R{1}" inside the list of parameters before the constant "CHANNEL".
The number of the previous character is indicated between brace parenthesis "{1}".

There is probably a better, shorter solution. It is up to you to improve this solution replying to this thread.

According to a previous chapter 3. Minus-in-the-name-of-Scheme variable, if the Scheme code uses minus inside the name of variable,
feel free to replace each word "\w+" by the interval "[_\-\d\l\u]+" including minus "-" in the name of a parameter.


Top
 Post subject: Upgrading deprecated functions could add new parameters
PostPosted: Mon Nov 22, 2021 2:33 pm  (#27) 
Offline
GimpChat Member
User avatar

Joined: Oct 23, 2021
Posts: 67
5. New parameters in the modern function: level difficult

Finally the last family of deprecated functions introduces new parameters often unknown parameters.

As usual, the first function is the deprecated function:
(gimp-invert          drawable)
(gimp-drawable-invert drawable linear) ; linear: Whether to invert in linear space (TRUE or FALSE)

The second function is the associated modern function having linear as new last parameter.

Since there is only one occurrence of gimp-invert in Aqua btn, you can manually replace it with the modern function.
However, the rewriting rule with regex could be:

Notepad++ menu "Search" > "Replace..."
[x] Regular expression

Find what: :geek:
gimp-invert\s+(.*)\)

The hypothesis is that the parameter drawable is a variable but not the result of the call of a function including parenthesis.

Replace with: :sglasses
gimp-drawable-invert \1 TRUE)

In line 167 of AquaBou2_10_28.scm, AlSchemist preferred TRUE for linear however this is an open discussion.
The final result of the generated aqua pill does not suffer to this new parameter with the modern function. :hi5

In the final example, PDB could not help anymore.
The difficulty increases with the number of options for the new parameter code-named operation: :roll:
(gimp-selection-layer-alpha                 layer)
(gimp-image-select-item     image operation item) ; operation: CHANNEL-OP-ADD (0), CHANNEL-OP-SUBTRACT (1), CHANNEL-OP-REPLACE (2), CHANNEL-OP-INTERSECT (3)

The alignment indicates that layer = item.
According to the following rewriting rule, AlSchemist chose "CHANNEL-OP-REPLACE" for operation
however the relationship with the selection of layer alpha is not patent. :gaah

Notepad++ menu "Search" > "Replace..."
[x] Regular expression

Find what: :geek:
gimp-selection-layer-alpha\s+(\w+)


Replace with: :sglasses
gimp-image-select-item img CHANNEL-OP-REPLACE \1

There are four occurrences: lines 39, 52, 58 and 168.
It is up to you to test the other alternatives if you prefer them. :geek

This post :party concludes :grooving the series of articles about:
migrating Script-Fu deprecated functions in Gimp 2.10.28


Top
Post new topic Reply to topic  [ 27 posts ]  Go to page Previous  1, 2

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Error with ICCII's Random Blends Script

6



* Login  



Powered by phpBB3 © phpBB Group