SVGImport.wiki
author kyberneticist@gmail.com
Mon, 25 Jun 2012 16:32:46 +0000
changeset 236 51ce3b455ad7
parent 235 231b0431e947
child 237 97e0d612316c
permissions -rw-r--r--
Edited wiki page SVGImport through web user interface.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     1
#summary Importing an SVG into Hedgewars
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     2
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     3
= Introduction =
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     4
230
9584a3da2b87 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 229
diff changeset
     5
This is a quick and dirty description of converting an SVG into hedgewars HWMAP format.  It is not at all pretty. Hopefully someone will make a prettier process.  It currently makes use of Inkscape, vim, perl, g++ and a bit of manual labour.  It looks best in 0.9.18 which makes use of the variable brush size (the examples below use the smallest size, or 16px wide)
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     6
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     7
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     8
= Details =
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
     9
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    10
Open Inkscape.
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    11
Go to File->Inkscape Preferences and make sure that
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    12
    * SVG Output->Allow relative coordinates is unchecked
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    13
    * Transforms->Store transformation  is set to Optimized
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    14
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    15
Open an SVG. Ideally one of simple line art, without too much use of fill or filters.
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    16
231
b5780c560285 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 230
diff changeset
    17
1) Select all in the image and choose ungroup, then combine all paths in the drawing (select them, then choose Path->Combine). 
b5780c560285 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 230
diff changeset
    18
Note. I had some difficulty doing that with some images even after repeated use of ungroup. I ended up just going into the SVG file and deleting all the groups.  This usually happens if there are filters in place. Removing all the <g> tags in the SVG is faster than cleaning up the filters in inkscape.
234
88f4f75b381c Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 233
diff changeset
    19
Also, some paths might be worth eliminating altogether.  In order to get a better idea of what it'll look like, try: View->Display Mode->Outline.  To simulate occluding, you can try combining individual paths first, and using union to combine into larger groups. This takes a bit more work.  If it still doesn't look right, you're going to have to go in and delete nodes, and generally rework the shape to simulate occlusion.
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    20
231
b5780c560285 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 230
diff changeset
    21
2) Click on the path, and choose dimensions for W and H that would look good in the game (no more than 4096 for W and 2048 for H).  The Lock button may be helpful here
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    22
231
b5780c560285 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 230
diff changeset
    23
3) Go to File->Document Properties and specify 4096 for W and 2048 for H.  You may want to then reposition the art to be more centred vertically or horizontally.
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    24
231
b5780c560285 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 230
diff changeset
    25
4) Save and Quit
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    26
236
51ce3b455ad7 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 235
diff changeset
    27
5) Open in an editor and verify there is one path. Change sodipodi:namedview's ID attribute to id="base" - this is to work around a bug in the current stable Inkscape extensions tool which was crashing it. Note that I haven't tested this workaround. su_v says this is the correct way to do it because apparently id="base" is a reserved name for namedview in sodipodi - which seems like a rather common and collision-likely name.  I had misread him and changed the path's id to base, and that seemed to work fine.  If changing namedview doesn't work, try renaming the path instead.
51ce3b455ad7 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 235
diff changeset
    28
Make sure all those groups are removed.
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    29
236
51ce3b455ad7 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 235
diff changeset
    30
6) Open the file in Inkscape again, Click on the path again, then go to Extensions->Modify Path->Flatten Beziers and flatten out the curves to your taste. Default of 10 seems fine most of the time, but for small curved objects you might want something like 5.  Keep in mind, the more the approximation, the more points that Hedgewars has to draw, which can be rough on the engine and network communication.
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    31
232
a0ce457b93f2 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 231
diff changeset
    32
7) Save and Quit again
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    33
232
a0ce457b93f2 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 231
diff changeset
    34
8) Edit the file, and delete everything but the path data.  You should have a one-line file starting with something like  M1234.3 456.78L3298.3 9023.34 and so on.  The coordinates should now be rounded unless you plan to handle that yourself in some way.  Here is a vim one-liner to do it.
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    35
{{{:s/[0-9][0-9.]*/\=float2nr(floor(submatch(0)*1))/g}}}
235
231b0431e947 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 234
diff changeset
    36
If instead you have a format like M 1234.678,9875.323 2345.0,123.45  - you'll want to convert if you want to try the crude script in (10) - otherwise a smarter script would be needed.  Here's some Vim commands for that syntax {{{s/\(\d\) \(\d\)/\1 L\2/g}}}  and {{{s/,/ /g}}} and {{{s/\([LM]\s*\)/\1/g}}}
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    37
232
a0ce457b93f2 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 231
diff changeset
    38
9) Convert the path data.  Here is a crude script to do that.  Note this one uses a line size of 1 (that's the 0x01 business).
231
b5780c560285 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 230
diff changeset
    39
If you want larger lines you can pick anything between 0x01 and 0x3F.  That's 16-636.  See the map format wiki page.
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    40
{{{
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    41
#!/usr/bin/perl
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    42
# just a one-line list of points. at least, it had better be one-line
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    43
open FILE, $ARGV[0];
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    44
while(<FILE>)
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    45
{
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    46
    chomp;
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    47
    s/([LM])(\d+) (\d+)\s*/point($1,$2,$3)/eg;
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    48
    print;
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    49
} 
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    50
sub point
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    51
{
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    52
    ($t, $x, $y) = @_;
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    53
    $x+=0;
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    54
    $y+=0; # just in case
231
b5780c560285 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 230
diff changeset
    55
    printf("%c%c%c%c%c",$x>>8,$x&0xff,$y>>8,$y&0xff,($t=~m/M/)?(0x80|0x01):0x01);
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    56
    return;
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    57
}
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    58
}}}
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    59
{{{script pointdata > hwpointdata}}}
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    60
232
a0ce457b93f2 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 231
diff changeset
    61
10) qCompress the data.
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    62
{{{g++ -I /usr/include/qt4 -I /usr/include/qt4/QtCore qcompress.cpp -lQtCore}}}
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    63
{{{
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    64
#include <QFile>
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    65
#include <QByteArray>
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    66
using namespace std;
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    67
int main(int argc, char **argv) 
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    68
{
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    69
    QFile fromFile(argv[1]);
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    70
    QFile toFile(argv[2]);
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    71
    if(fromFile.open(QIODevice::ReadOnly) && toFile.open(QIODevice::WriteOnly))
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    72
    {
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    73
        toFile.write(qCompress(fromFile.readAll()));
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    74
    }
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    75
}
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    76
}}}
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    77
{{{./a.out hwpointdata hwpointdata.Z}}}
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    78
232
a0ce457b93f2 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 231
diff changeset
    79
11) Convert to base64 and you're done!
227
c7897b5d10c5 Created wiki page through web user interface.
kyberneticist@gmail.com
parents:
diff changeset
    80
228
13f07e078365 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 227
diff changeset
    81
{{{base64 -w0 hwpointdata.Z > mynewhedgewars.hwmap}}}
13f07e078365 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 227
diff changeset
    82
13f07e078365 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 227
diff changeset
    83
Enjoy.
13f07e078365 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 227
diff changeset
    84
13f07e078365 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 227
diff changeset
    85
Here are some example hwmap files from the process above.  They look better in 0.9.18.
13f07e078365 Edited wiki page SVGImport through web user interface.
kyberneticist@gmail.com
parents: 227
diff changeset
    86
http://m8y.org/hw/drawn/