PDFedit
PDF editor pro UNIX / PDF editor for UNIX

Wiki : ScriptingAddNewScript

HomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register

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 simple 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 obtain 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.
  */

function drawArrow(_lx,_ly,_rx,_ry,wantedit) {
    // Get the end points
    var _a1 = _rx+(_lx-_ry+_ly-_rx)/10;
    var _a2 = _ry+(_ly+_rx-_lx-_ry)/10;
    var _b1 = _rx+(_lx+_ry-_ly-_rx)/10;
    var _b2 = _ry+(_ly-_rx+_lx-_ry)/10;

    if (undefined != _lx && undefined != _ry) {
       
        // Convert main line points
        lx = convertPixmapPosToPdfPos_x(_lx,_ly);
        ly = convertPixmapPosToPdfPos_y(_lx,_ly);
        rx = convertPixmapPosToPdfPos_x(_rx,_ry);
        ry = convertPixmapPosToPdfPos_y(_rx,_ry);
       
        // Convert arrow end points
        a1 = convertPixmapPosToPdfPos_x(_a1,_a2);
        a2 = convertPixmapPosToPdfPos_y(_a1,_a2);
        b1 = convertPixmapPosToPdfPos_x(_b1,_b2);
        b2 = convertPixmapPosToPdfPos_y(_b1,_b2);
       
    }else
            return;

    operatorDrawLine ([ [lx,ly,rx,ry], [rx,ry,a1,a2], [rx,ry,b1,b2]], getNumber("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, alternative 1: Connecting button and mouse events with our function


In callback.qs add

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

/** Callback when selection mode is changed to draw new arrow */
function onSelectMode_drawArrowe() {
 PageSpace.setSelectionMode("new_object","line","drawArrow(%1,%2,%3,%4,true)");
 setModeButton('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
  drawarrow        = item Draw arrow        , onSelectMode_drawArrow()        , , draw_arrow.png    ,need_page,mode_button
  drawarrowe    = item Draw arrow        , onSelectMode_drawArrowe()        , , draw_arrowe.png    ,need_page,mode_button

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
DrawToolbar        = list Draw Toolbar,drawlinee,drawrecte,addtext,striketrough,highlightingtext,-,_color_tool fg,_color_tool bg,_number_tool 

the result can look like this
DrawToolbar        = list Draw Toolbar,drawlinee,drawarrowe,drawrecte,addtext,striketrough,highlightingtext,-,_color_tool fg,_color_tool bg,_number_tool 

this shows the button in the DrawToolbar menu.

Step 4, alternative 2: Creating the script as a plugin script


Take the code from step 1 and append this code to the end of the script.

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

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

createMenuItem('DrawToolbar','drawarrow',tr('Draw arrow'),'onSelectMode_drawArrow()','','draw_arrow.png',['need_page','mode_button']);
createMenuItem('DrawToolbar','drawarrowe',tr('Draw arrow'),'onSelectMode_drawArrowe()','','draw_arrowe.png',['need_page','mode_button']);


Save it as "arrow.qs" and put the script in "scripts" dicrectory

Installation
You can use easier method of installation by downloading the script file and associated icons.
Put the script file in your home directory in subdirectory .pdfedit/scripts
Put the associated icons into .pdfedit/icon subdirectory in your home directory
No other installation is necessary, the function will be available next time you start the editor. If you do not want to restart editor, you can load the new functions while the editor is running by typing run("scripts/name_of_script.qs") in editor command line.

If you have root privileges, you can install this script for everybody - put the script file into /usr/local/share/pdfedit/scripts and icons into /usr/local/share/pdfedit/icon directory
(Assuming you have not changed default installation path)
  Attachment Size Date Added
      arrow.qs   1.73 KB   15.9.2006 04:32:17
 
 
add new attachment:

Categories
Scripting
Howto

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