Created
October 5, 2018 21:33
-
-
Save sagax/064c051b6fb339779bafd3666e6360f2 to your computer and use it in GitHub Desktop.
awk script to generation svg document with text inside rect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GNU Awk 4.2.1, API: 2.0 | |
# | |
# before run - set variable values, after | |
# awk -f generator_svg_with_rect_and_text.awk > document.svg | |
# | |
# after you can convert svg to png with inkscape | |
# inkscape --export-png=document.png document.svg | |
function print_start( page_width, page_height, unit_size) { | |
printf "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" | |
printf "<svg" | |
printf " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"" | |
printf " xmlns:cc=\"http://creativecommons.org/ns#\"" | |
printf " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" | |
printf " xmlns:svg=\"http://www.w3.org/2000/svg\"" | |
printf " xmlns=\"http://www.w3.org/2000/svg\"" | |
printf " xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"" | |
printf " xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"" | |
printf " width=\""page_width unit_size"\"" | |
printf " height=\""page_height unit_size"\"" | |
printf " viewBox=\"0 0 "page_width " "page_height"\"" | |
printf " version=\"1.1\"" | |
printf " id=\"svg8\">" | |
printf " <defs" | |
printf " id=\"defs2\" />" | |
printf " <sodipodi:namedview" | |
printf " id=\"base\"" | |
printf " pagecolor=\"#ffffff\"" | |
printf " bordercolor=\"#666666\"" | |
printf " borderopacity=\"1.0\"" | |
printf " inkscape:document-units=\""unit_size"\"" | |
printf " inkscape:current-layer=\"layer1\"" | |
printf " showgrid=\"false\"" | |
printf " />" | |
printf " <metadata" | |
printf " id=\"metadata5\">" | |
printf " <rdf:RDF>" | |
printf " <cc:Work" | |
printf " rdf:about=\"\">" | |
printf " <dc:format>image/svg+xml</dc:format>" | |
printf " <dc:type" | |
printf " rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />" | |
printf " <dc:title></dc:title>" | |
printf " </cc:Work>" | |
printf " </rdf:RDF>" | |
printf " </metadata>" | |
printf " <g" | |
printf " inkscape:label=\"Layer 1\"" | |
printf " inkscape:groupmode=\"layer\"" | |
printf " id=\"layer1\">" | |
} | |
function print_end() { | |
printf " </g>" | |
printf "</svg>" | |
} | |
function id() { | |
_id += 1 | |
return _id | |
} | |
function gen_rect_with_text( i, j, text) { | |
if (i % 2 == 0) { | |
if (color_trigger == 0) { color = color1; color_trigger = 1 } | |
else { color = color2; color_trigger = 0} | |
} else { | |
if (color_trigger == 1) { color = color1; color_trigger = 0 } | |
else { color = color2; color_trigger = 1} | |
} | |
x = i * width | |
y = j * height | |
printf "<rect style=\"opacity:1;fill:"color";fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\" id=\""id()"\" width=\""width"\" height=\""height"\" x=\""x"\" y=\""y"\" />" | |
printf "<flowRoot xml:space=\"preserve\" id=\""id()"\" style=\"font-style:normal;font-weight:normal;font-size:"fontsize"px;line-height:"lineheight"px;font-family:sans-serif;text-align:center;letter-spacing:0px;word-spacing:0px;fill:"fontcolor";fill-opacity:1;stroke:none\"><flowRegion id=\""id()"\"><rect id=\""id()"\" width=\""width"\" height=\""height"\" x=\""x"\" y=\""y"\" /></flowRegion><flowPara id=\""id()"\">"text"</flowPara></flowRoot>\n" | |
} | |
BEGIN { | |
_id = 0 | |
color1 = "#f0f0f0" # first rect | |
color2 = "#a0a0a0" # second rect | |
color_trigger = 0 | |
width = 10 # rect width | |
height = 10 # rect height | |
fontsize = 3 | |
lineheight = height | |
fontcolor = "#505050" | |
left = 10 | |
right = 10 | |
page_width = width * left | |
page_height = height * right | |
unit_size = "mm" | |
print_start(page_width, page_height, unit_size) | |
for (i=0; i < left; ++i) { | |
for (j=0; j < right; ++j) { | |
t = i "." j | |
gen_rect_with_text(i, j, t) | |
} | |
} | |
print_end() | |
} |
Author
sagax
commented
Oct 5, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment