PDFedit
PDF editor pro UNIX / PDF editor for UNIX

Wiki : ScriptingAddNewScript

HomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
This is an old revision of ScriptingAddNewScript from 2006-09-14 01:39:27.

How to add your script to PDFedit (step by step example)



Example 1 - drawing arrows


Step 1: Writing the script


Our aim is to be able to draw simple arrows. We use a stupid algorithm which takes 2 coordinates specifying the start and the end of a line and gives us 2
coordinates specifying left and right end point of an arrow. The end point is computed as the 1/10 of the main line width in 45 degree angle.
We use built-in script function operatorDrawLine which takes an array of lines to draw, line width and line colour.
We obtaing the line width and the line colour from default menu selectors using built-in functions.

Input coordinates and output coordinates may differ because page can use arbitrary transformation of coordinates. That is why every
coordinate needs to be converted using either convertPixmapPosToPdfPos_x or convertPixmapPosToPdfPos_y function.
An array consists of 3 lines. The main one connecting first two points, then one connecting left end point with
the end of the line, and likewise the right end point with the end of line. We use the input points as the start and end of the main line which is drawn first. Then
the line connecting left end and then the one connecting right end.

Finally reload the page to see the result.

/**
 * Draw an arrow.
  */
// Get the end points
// Convert main line points
// Convert arrow end points
"linewidth"), getColor("fg"));
    // Reload page
    go()
}


Step 2: Adding the script


Open dialogs.qs which should be in your install directory. Add the function drawArrow to the end (optionally write a comment describing the function in doxygen-like style to add the function in command line documentation which is automatically generated) and save the file.
Then after either restarting the application or reloading the script file calling
run ("dialogs.qs")
in command line
we can manually call the function from the command line.

Step 3: (optional) Finding new icon


Try e.g. kde look&feel; to get hicolor icons (32x32).
Add the icon to either icon or icon/something directory to match your actual icon set directory.


Step 4: Connecting button and mouse events with our function


In callback.qs add

/** Callback when selection mode is changed to draw new arrow */"new_object","line","drawArrow(%1,%2,%3,%4,false)"'drawarrow');
}

/** Callback when selection mode is changed to draw new arrow */"new_object","line","drawArrow(%1,%2,%3,%4,true)"'drawarrowe');
}

These two functions are almost identical (only first is really needed the latter one is for functions that can be even more customized and require a dialog). They set a drawing mode (we use an existing one which draws lines) and when it finished (the user releases the mouse) the function drawArrow is called with two points marking the start and the end
of the input sequence (when the mouse was pressed and when the mouse was released).

In pdfeditrc add
 

which means that an item is created which can be referenced from other items. The item is described by a name, action to take when clicked, button icon, what is needed to
make the button enabled, the mode of the button.

And add the button to the toolbar by adding drawarrowe to
 

the result can look like this
 

this shows the button in the DrawToolbar menu.


Categories
Scripting

There are no comments on this page. [Add comment]