judoshiai-en:lua-en
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
judoshiai-en:lua-en [2025/01/04 00:01] – hjokinen | judoshiai-en:lua-en [2025/01/04 00:20] (current) – [SVG + Lua scripts] hjokinen | ||
---|---|---|---|
Line 58: | Line 58: | ||
</ | </ | ||
Again you will display text Hello World! File must be in one of the directories | Again you will display text Hello World! File must be in one of the directories | ||
+ | < | ||
+ | HOMEDIR/ | ||
+ | / | ||
+ | C: | ||
- | //HOMEDIR/ | + | HOMEDIR/ |
- | /// | + | (for example |
- | C: | + | |
- | // | + | JudoShiai/ |
- | (for example /// | + | (for example / |
- | + | ||
- | //JudoShiai/ | + | |
- | (for example | + | |
- | + | ||
- | // | + | |
- | (for example /// | + | |
- | // | + | |
+ | JudoShiai/ | ||
+ | (for example / | ||
+ | | ||
+ | </ | ||
You can have Lua file anywhere, but then you must give the whole path: | You can have Lua file anywhere, but then you must give the whole path: | ||
+ | < | ||
Hello %[@/ | Hello %[@/ | ||
+ | </ | ||
Note: @-character indicates that rest of the text is a path to a file. Also on Windows you must use forward slashes. | Note: @-character indicates that rest of the text is a path to a file. Also on Windows you must use forward slashes. | ||
Line 82: | Line 82: | ||
There are some predefined read-only variables that you can use after their values are set. To set match related variables call your Lua function from an SVG page like this: | There are some predefined read-only variables that you can use after their values are set. To set match related variables call your Lua function from an SVG page like this: | ||
+ | < | ||
%[require ' | %[require ' | ||
+ | </ | ||
m23 sets the following global predefined read-only variables for the match #23 before running Lua file // | m23 sets the following global predefined read-only variables for the match #23 before running Lua file // | ||
Line 105: | Line 105: | ||
|JS_legend | |JS_legend | ||
|JS_roundnum | |JS_roundnum | ||
- | |||
To set competitor related variables call your Lua function like this: | To set competitor related variables call your Lua function like this: | ||
+ | < | ||
%[require ' | %[require ' | ||
+ | </ | ||
This will first set white competitor’s name and other data to global variables and then run your Lua file. The following competitor’s variables are set: | This will first set white competitor’s name and other data to global variables and then run your Lua file. The following competitor’s variables are set: | ||
Line 134: | Line 133: | ||
For the blue competitor the Lua call would be: | For the blue competitor the Lua call would be: | ||
+ | < | ||
%[require ' | %[require ' | ||
+ | </ | ||
Lua code is the one surrounded by brackets. You can have any name and arguments. | Lua code is the one surrounded by brackets. You can have any name and arguments. | ||
Line 147: | Line 146: | ||
There are useful functions that start with JS.: | There are useful functions that start with JS.: | ||
+ | < | ||
JS.sqlrows(sqlcmd) | JS.sqlrows(sqlcmd) | ||
- | |||
Run SQL command. | Run SQL command. | ||
- | |||
Parameters | Parameters | ||
- | |||
sqlrow String. Sql command to perform. | sqlrow String. Sql command to perform. | ||
- | |||
Returns | Returns | ||
- | |||
Table of tables. | Table of tables. | ||
- | |||
- | |||
JS.writesvg(str) | JS.writesvg(str) | ||
- | |||
Writes SVG code on canvas. | Writes SVG code on canvas. | ||
- | |||
Parameters | Parameters | ||
- | |||
str String. SVG code to write to the document. | str String. SVG code to write to the document. | ||
- | |||
Returns | Returns | ||
- | |||
Nothing. | Nothing. | ||
- | |||
- | |||
JS.getdata(index) | JS.getdata(index) | ||
- | |||
Get data of competitor or category using index. | Get data of competitor or category using index. | ||
- | |||
Parameters | Parameters | ||
- | |||
index Integer. Index of the competitor or category. | index Integer. Index of the competitor or category. | ||
- | |||
Returns | Returns | ||
- | |||
Table of data. | Table of data. | ||
- | |||
- | |||
JS.getmatchbytatami(tatami, | JS.getmatchbytatami(tatami, | ||
- | |||
Get data about match. | Get data about match. | ||
- | |||
Parameters | Parameters | ||
- | |||
tatami | tatami | ||
- | |||
number | number | ||
- | |||
Returns | Returns | ||
- | |||
Table of data. | Table of data. | ||
- | |||
- | |||
JS.getroundname(round) | JS.getroundname(round) | ||
- | |||
Get round name of a match. | Get round name of a match. | ||
- | |||
Parameters | Parameters | ||
- | |||
round Integer. Round code from database or getmatchbytatami(). | round Integer. Round code from database or getmatchbytatami(). | ||
- | |||
Returns | Returns | ||
- | |||
String that describes the round. | String that describes the round. | ||
- | |||
- | |||
JS.setpages(numpages) | JS.setpages(numpages) | ||
- | |||
Set total number of pages. | Set total number of pages. | ||
- | |||
Parameters | Parameters | ||
- | |||
numpages | numpages | ||
- | |||
Returns | Returns | ||
- | |||
Nothing. | Nothing. | ||
- | |||
- | |||
JS.getpage() | JS.getpage() | ||
- | |||
Get the current page number. First page is 1. | Get the current page number. First page is 1. | ||
- | |||
Parameters | Parameters | ||
- | |||
None | None | ||
- | |||
Returns | Returns | ||
- | |||
Current page number (integer) starting from 1. | Current page number (integer) starting from 1. | ||
- | |||
- | |||
JS.flag2base64(country) | JS.flag2base64(country) | ||
- | |||
Writes flag of a country as base64 string to SVG document. Data is from\\ | Writes flag of a country as base64 string to SVG document. Data is from\\ | ||
JudoShiai/ | JudoShiai/ | ||
- | |||
Parameters | Parameters | ||
- | |||
country | country | ||
- | |||
Returns | Returns | ||
- | |||
Nothing. | Nothing. | ||
- | |||
- | |||
JS.error(message) | JS.error(message) | ||
- | |||
Opens a window and shows error text. | Opens a window and shows error text. | ||
- | |||
Parameters | Parameters | ||
- | |||
message | message | ||
- | |||
Returns | Returns | ||
- | |||
Nothing. | Nothing. | ||
- | |||
- | |||
JS.print(text) | JS.print(text) | ||
- | |||
Prints texts to an open window. | Prints texts to an open window. | ||
- | |||
Parameters | Parameters | ||
- | |||
text | text | ||
- | |||
Returns | Returns | ||
- | |||
Nothing. | Nothing. | ||
- | |||
- | |||
JS.getcatdefs() | JS.getcatdefs() | ||
- | |||
Get defined category names. | Get defined category names. | ||
- | |||
Parameters | Parameters | ||
- | |||
None. | None. | ||
- | |||
Returns | Returns | ||
- | |||
Table of categories for men and women. | Table of categories for men and women. | ||
- | |||
Example: | Example: | ||
- | |||
m, w = JS.getcatdefs() | m, w = JS.getcatdefs() | ||
- | |||
returns table m for men: | returns table m for men: | ||
- | |||
{ | { | ||
- | |||
[1] = " | [1] = " | ||
- | |||
[2] = " | [2] = " | ||
- | |||
[3] = " | [3] = " | ||
- | |||
numentries = 3, | numentries = 3, | ||
- | |||
} | } | ||
- | |||
and w for women: | and w for women: | ||
- | |||
{ | { | ||
- | |||
[1] = " | [1] = " | ||
- | |||
[2] = " | [2] = " | ||
- | |||
[3] = " | [3] = " | ||
- | |||
numentries = 3, | numentries = 3, | ||
- | |||
} | } | ||
- | |||
Tables have entries ' | Tables have entries ' | ||
- | |||
- | |||
JS.getsvgbyid(filename, | JS.getsvgbyid(filename, | ||
- | |||
Returns group whose id is id. | Returns group whose id is id. | ||
- | |||
Parameters | Parameters | ||
- | |||
filename | filename | ||
- | |||
id | id | ||
- | |||
Returns | Returns | ||
- | |||
String that contains the group data. | String that contains the group data. | ||
- | |||
Example | Example | ||
- | |||
svg = JS.getsvgbyid(InstallationDir .. '/ | svg = JS.getsvgbyid(InstallationDir .. '/ | ||
' | ' | ||
- | |||
- | |||
JS.getsvggeometry(filename, | JS.getsvggeometry(filename, | ||
- | |||
Returns position and size of a svg element. | Returns position and size of a svg element. | ||
- | |||
Parameters | Parameters | ||
- | |||
filename | filename | ||
- | |||
id | id | ||
- | |||
Returns | Returns | ||
- | |||
X and y coordinates of the svg element. | X and y coordinates of the svg element. | ||
- | |||
Width and height of the svg element. | Width and height of the svg element. | ||
- | |||
Example | Example | ||
- | |||
local x, y, w, h = JS.getsvggeometry(InstallationDir ..\\ | local x, y, w, h = JS.getsvggeometry(InstallationDir ..\\ | ||
'/ | '/ | ||
- | |||
- | |||
JS.askoption(title, | JS.askoption(title, | ||
- | + | | |
- | | + | While asking all the other functionality is blocked. |
- | While asking all the other functionality is blocked.\\ | + | Please make your selection without delay. |
- | Please make your selection without delay. | + | |
Parameters | Parameters | ||
- | |||
title String. Question to ask. | title String. Question to ask. | ||
- | |||
options | options | ||
separated by slashes. | separated by slashes. | ||
- | |||
Returns | Returns | ||
- | |||
Number of the selected option starting from 1. | Number of the selected option starting from 1. | ||
- | |||
Example | Example | ||
- | |||
local color = JS.askoption(' | local color = JS.askoption(' | ||
- | |||
Color will be 1 for Red, 2 for Green, and 3 for Blue. | Color will be 1 for Red, 2 for Green, and 3 for Blue. | ||
+ | </ | ||
===== Example ===== | ===== Example ===== | ||
Line 419: | Line 294: | ||
Make a file // | Make a file // | ||
+ | < | ||
function pts(who) | function pts(who) | ||
- | |||
if JS_points_1 > 0 or JS_points_2 > 0 then -- line 2 | if JS_points_1 > 0 or JS_points_2 > 0 then -- line 2 | ||
- | |||
if JS_points_1 == JS_points_2 then -- line 3 | if JS_points_1 == JS_points_2 then -- line 3 | ||
- | |||
JS.writesvg(' | JS.writesvg(' | ||
- | |||
else -- line 5 | else -- line 5 | ||
- | |||
if who == 1 then -- line 6 | if who == 1 then -- line 6 | ||
- | |||
print_points(JS_points_1) | print_points(JS_points_1) | ||
- | |||
else -- line 8 | else -- line 8 | ||
- | |||
print_points(JS_points_2) | print_points(JS_points_2) | ||
- | |||
end | end | ||
- | |||
end | end | ||
- | |||
end | end | ||
- | |||
end | end | ||
+ | </ | ||
Explanation line by line: | Explanation line by line: | ||
Line 464: | Line 327: | ||
Finally implement function // | Finally implement function // | ||
+ | < | ||
function print_points(p) | function print_points(p) | ||
- | |||
if p == 0 then -- line 2 | if p == 0 then -- line 2 | ||
- | |||
JS.writesvg(' | JS.writesvg(' | ||
- | |||
elseif p == 7 then -- line 4 | elseif p == 7 then -- line 4 | ||
- | |||
JS.writesvg(' | JS.writesvg(' | ||
- | |||
elseif p == 11 then -- line 6 | elseif p == 11 then -- line 6 | ||
- | |||
JS.writesvg(' | JS.writesvg(' | ||
- | |||
else -- line 8 | else -- line 8 | ||
- | |||
JS.writesvg(tostring(p)) | JS.writesvg(tostring(p)) | ||
- | |||
end | end | ||
- | |||
end | end | ||
+ | </ | ||
Explanation: | Explanation: | ||
Line 521: | Line 374: | ||
There are two ways to run Lua scripts: | There are two ways to run Lua scripts: | ||
- | * Embed code to SVG sheets. Lua code is embedded to text by using format\\ | + | * Embed code to SVG sheets. Lua code is embedded to text by using format // |
- | // | + | |
* Run Lua file. There will be a default SVG file where all the printing goes. Note: The default file has no Inkscape specific definitions. When creating SVG files to include them save them as plain SVG. | * Run Lua file. There will be a default SVG file where all the printing goes. Note: The default file has no Inkscape specific definitions. When creating SVG files to include them save them as plain SVG. | ||
Line 548: | Line 400: | ||
You can run a Lua script by selecting //Results → Run Script.// Everything you write goes between main //< | You can run a Lua script by selecting //Results → Run Script.// Everything you write goes between main //< | ||
+ | < | ||
JS.writesvg("< | JS.writesvg("< | ||
+ | </ | ||
Normally there are many more parameters, like colors, line style, etc. However, it is not necessary to know the SVG format. You can draw a file and name the objects and import the objects one by one. In the following example we assume you have your Lua code in // | Normally there are many more parameters, like colors, line style, etc. However, it is not necessary to know the SVG format. You can draw a file and name the objects and import the objects one by one. In the following example we assume you have your Lua code in // | ||
Use Inkscape to draw a rectangle. Set its id = myrect. Save it using //Save As → Plain SVG// (important!) to // | Use Inkscape to draw a rectangle. Set its id = myrect. Save it using //Save As → Plain SVG// (important!) to // | ||
+ | < | ||
<?xml version=" | <?xml version=" | ||
- | |||
<!-- Created with Inkscape (http: | <!-- Created with Inkscape (http: | ||
- | |||
- | |||
<svg | <svg | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
<defs | <defs | ||
- | |||
| | ||
- | |||
<g | <g | ||
- | |||
| | ||
- | |||
<rect | <rect | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
| | ||
- | |||
</g> | </g> | ||
- | |||
</ | </ | ||
+ | </ | ||
Style is a long line. Note the // | Style is a long line. Note the // | ||
+ | < | ||
local rect = JS.getsvgbyid(LuaScriptDir .. '/ | local rect = JS.getsvgbyid(LuaScriptDir .. '/ | ||
+ | </ | ||
Function // | Function // | ||
+ | < | ||
JS.writesvg(rect) | JS.writesvg(rect) | ||
+ | </ | ||
It will appear at position 11.9, 11.5. Maybe you want to have another one next to it. First you have to know its dimensions: | It will appear at position 11.9, 11.5. Maybe you want to have another one next to it. First you have to know its dimensions: | ||
+ | < | ||
local x, y, width, height = JS.getsvggeometry(LuaScriptDir .. '/ | local x, y, width, height = JS.getsvggeometry(LuaScriptDir .. '/ | ||
' | ' | ||
+ | </ | ||
Now you can draw a copy shifted by //width//: | Now you can draw a copy shifted by //width//: | ||
+ | < | ||
JS.writesvg(string.format('< | JS.writesvg(string.format('< | ||
- | |||
JS.writesvg(rect) | JS.writesvg(rect) | ||
- | |||
JS.writesvg('</ | JS.writesvg('</ | ||
+ | </ | ||
This way you can make lists just by creating one line that is copied. | This way you can make lists just by creating one line that is copied. | ||
Line 635: | Line 463: | ||
In many countries this will print 3.14, but in the others 3,14. SVG accepts only the dot as a decimal separator. To be on the safe side it is better to convert the float to a string and then replace possible comma with a dot. Function FloatToStr does that: | In many countries this will print 3.14, but in the others 3,14. SVG accepts only the dot as a decimal separator. To be on the safe side it is better to convert the float to a string and then replace possible comma with a dot. Function FloatToStr does that: | ||
+ | < | ||
function FloatToStr(a) | function FloatToStr(a) | ||
- | |||
local r = tostring(a) | local r = tostring(a) | ||
- | |||
return r: | return r: | ||
- | |||
end | end | ||
+ | </ | ||
Now, instead of //%f// you will use //%s// for a string. The previous translate line | Now, instead of //%f// you will use //%s// for a string. The previous translate line | ||
+ | < | ||
JS.writesvg(string.format('< | JS.writesvg(string.format('< | ||
+ | </ | ||
changes to | changes to | ||
+ | < | ||
JS.writesvg(string.format('< | JS.writesvg(string.format('< | ||
FloatToStr(width)) | FloatToStr(width)) | ||
+ | </ | ||
==== Page size ==== | ==== Page size ==== | ||
The default page size is A4 (210mm x 297mm). You can define your own size by having a comment on the first line of the Lua file: | The default page size is A4 (210mm x 297mm). You can define your own size by having a comment on the first line of the Lua file: | ||
+ | < | ||
-- paper width=300 height=200 | -- paper width=300 height=200 | ||
+ | </ | ||
It is possible to have many sizes for the same Lua code. Create a base file. Then include that in the other files: | It is possible to have many sizes for the same Lua code. Create a base file. Then include that in the other files: | ||
+ | < | ||
-- paper width=300 height=200 | -- paper width=300 height=200 | ||
- | |||
paper_w = 300 | paper_w = 300 | ||
- | |||
paper_h = 200\\ | paper_h = 200\\ | ||
require ' | require ' | ||
+ | </ | ||
Use //paper_w// and //paper_h// for layout design in // | Use //paper_w// and //paper_h// for layout design in // | ||
Line 675: | Line 499: | ||
Set the total number of pages first: | Set the total number of pages first: | ||
+ | < | ||
JS.setpages(3) | JS.setpages(3) | ||
+ | </ | ||
Next draw you page based on the current page number: | Next draw you page based on the current page number: | ||
+ | < | ||
current_page = JS.getpage() | current_page = JS.getpage() | ||
- | |||
if current_page == 1 then | if current_page == 1 then | ||
- | |||
print_first_page() | print_first_page() | ||
- | |||
else | else | ||
- | |||
print_something_else() | print_something_else() | ||
- | |||
end | end | ||
+ | </ | ||
Page changes when you click the visible SVG page with the left or right mouse button. In printer dialog you can select the pages as usual. | Page changes when you click the visible SVG page with the left or right mouse button. In printer dialog you can select the pages as usual. | ||
Line 697: | Line 516: | ||
Example: Print members of category //M-90//: | Example: Print members of category //M-90//: | ||
+ | < | ||
local numrows, tbl = JS.sqlrows( | local numrows, tbl = JS.sqlrows( | ||
- | |||
' | ' | ||
- | + | </ | |
//Numrows// tells how many rows //tbl// has. //Tbl// looks like this: | //Numrows// tells how many rows //tbl// has. //Tbl// looks like this: | ||
+ | < | ||
{ | { | ||
- | |||
1= { | 1= { | ||
- | |||
last= " | last= " | ||
- | |||
first= " | first= " | ||
- | |||
}, | }, | ||
- | |||
2= { | 2= { | ||
- | |||
last= " | last= " | ||
- | |||
first= " | first= " | ||
- | |||
}, | }, | ||
- | |||
3= { | 3= { | ||
- | |||
last= " | last= " | ||
- | |||
first= " | first= " | ||
- | |||
}, | }, | ||
- | |||
} | } | ||
+ | </ | ||
There are three rows. You can get last name of the second competitor like this: | There are three rows. You can get last name of the second competitor like this: | ||
+ | < | ||
last_name = tbl[' | last_name = tbl[' | ||
+ | </ | ||
judoshiai-en/lua-en.1735974100.txt.gz · Last modified: 2025/01/04 00:01 by hjokinen