tests: Add test_astbuilder to the skip list of nitcg niti nitvm
[nit.git] / contrib / inkscape_tools / src / svg_to_png_and_nit.nit
index 80ba5c4..b8a3157 100644 (file)
@@ -92,71 +92,6 @@ abstract class ImageSetSrc
        var images: Array[Image]
 end
 
-# Nit module targeting the MNit framework
-class MnitImageSetSrc
-       super ImageSetSrc
-
-       redef fun rendering
-       do
-               # Known array of images
-               var arrays_of_images = new Array[String]
-
-               # Attributes of the generated class
-               var attributes = new Array[String]
-
-               # Statements for the generated `load_all` method
-               var load_exprs = new Array[String]
-
-               # Add images to Nit source file
-               for image in images do
-                       # Adapt coordinates to new top left and scale
-                       var coordinates = document.coordinates(image)
-
-                       var nit_name = image.name
-                       var last_char = nit_name.chars.last
-                       if last_char.to_s.is_numeric then
-                               # Array of images
-                               # TODO support more than 10 images in an array
-
-                               nit_name = nit_name.substring(0, nit_name.length-1)
-                               if not arrays_of_images.has(nit_name) then
-                                       # Create class attribute to store Array
-                                       arrays_of_images.add(nit_name)
-                                       attributes.add "\tvar {nit_name} = new Array[Image]\n"
-                               end
-                               load_exprs.add "\t\t{nit_name}.add(main_image.subimage({coordinates}))\n"
-                       else
-                               # Single image
-                               attributes.add "\tvar {nit_name}: Image is noinit\n"
-                               load_exprs.add "\t\t{nit_name} = main_image.subimage({coordinates})\n"
-                       end
-               end
-
-               add """
-# File generated by svg_to_png_and_nit, do not modify, redef instead
-
-import mnit::image_set
-
-class {{{document.nit_class_name}}}
-       super ImageSet
-
-       private var main_image: Image is noinit
-"""
-               add_all attributes
-               add """
-
-       redef fun load_all(app: App)
-       do
-               main_image = app.load_image(\"images/{{{document.drawing_name}}}.png\")
-"""
-               add_all load_exprs
-               add """
-       end
-end
-"""
-       end
-end
-
 # Nit module targeting the Gamnit framework
 #
 # Gamnit's `Texture` already manage the lazy loading, no need to do it here.
@@ -191,14 +126,14 @@ class GamnitImageSetSrc
 
                # Attributes of the class
                var attributes = new Array[String]
-               attributes.add "\tprivate var main_image = new Texture(\"images/{document.drawing_name}.png\")\n"
+               attributes.add "\tprivate var root_texture = new Texture(\"images/{document.drawing_name}.png\")\n"
 
                # Add single images to Nit source file
                for image in single_images do
                        # Adapt coordinates to new top left and scale
                        var coordinates = document.coordinates(image)
 
-                       attributes.add "\tvar {image.name}: Texture = main_image.subtexture({coordinates})\n"
+                       attributes.add "\tvar {image.name}: Texture = root_texture.subtexture({coordinates})\n"
                end
 
                # Add array of images too
@@ -207,7 +142,7 @@ class GamnitImageSetSrc
                        var lines = new Array[String]
                        for image in images do
                                var coordinates = document.coordinates(image)
-                               lines.add "\t\tmain_image.subtexture({coordinates})"
+                               lines.add "\t\troot_texture.subtexture({coordinates})"
                        end
 
                        attributes.add """
@@ -224,7 +159,7 @@ class GamnitImageSetSrc
                add """
 # File generated by svg_to_png_and_nit, do not modify, redef instead
 
-import gamnit::display
+import gamnit::textures
 
 class {{{document.nit_class_name}}}
 
@@ -255,12 +190,13 @@ end
 
 var opt_out_src = new OptionString("Path to output source file (folder or file)", "--src", "-s")
 var opt_assets = new OptionString("Path to assert dir where to put PNG files", "--assets", "-a")
-var opt_scale = new OptionFloat("Apply scaling to exported images (default at 1.0 of 90dpi)", 1.0, "--scale", "-x")
-var opt_gamnit = new OptionBool("Target the Gamnit framework (by default it targets Mnit)", "--gamnit", "-g")
+var opt_scale = new OptionFloat("Apply scaling to exported images (default at 1.0)", 1.0, "--scale", "-x")
+var opt_gamnit = new OptionBool("Target the Gamnit framework (the default, kept for compatibility)", "--gamnit", "-g")
+var opt_pow2 = new OptionBool("Round the image size to the next power of 2", "--pow2")
 var opt_help = new OptionBool("Print this help message", "--help", "-h")
 
 var opt_context = new OptionContext
-opt_context.add_option(opt_out_src, opt_assets, opt_scale, opt_gamnit, opt_help)
+opt_context.add_option(opt_out_src, opt_assets, opt_scale, opt_gamnit, opt_pow2, opt_help)
 
 opt_context.parse(args)
 var rest = opt_context.rest
@@ -279,6 +215,23 @@ if not "inkscape".program_is_in_path then
        exit 1
 end
 
+# Get the inkscape version
+var p = new ProcessReader("inkscape", "--version")
+var version_string = p.read_all
+p.wait
+p.close
+var version_re = "([0-9]+)\\.([0-9]+)".to_re
+var match = version_string.search(version_re)
+assert match != null
+var major = match[1]
+var minor = match[2]
+assert major != null and minor != null
+
+# Set the default API using the version as heuristic
+var default_dpi = 96.0
+if major.to_s.to_i == 0 and minor.to_s.to_i < 92 then default_dpi = 90.0
+
+# Collect source files
 var drawings = rest
 for drawing in drawings do
        if not drawing.file_exists then
@@ -328,8 +281,10 @@ for drawing in drawings do
        end
        svg_file.close
 
-       assert page_width != -1
-       assert page_height != -1
+       if page_width == -1 or page_height == -1 then
+               stderr.write "Source drawing file '{drawing}' doesn't look like an SVG file\n"
+               exit 1
+       end
 
        # Query Inkscape
        var prog = "inkscape"
@@ -378,12 +333,7 @@ for drawing in drawings do
        var document = new Document(drawing_name, scale, min_x, max_x, min_y, max_y)
 
        # Nit class
-       var nit_src: ImageSetSrc
-       if opt_gamnit.value then
-               nit_src = new GamnitImageSetSrc(document, images)
-       else
-               nit_src = new MnitImageSetSrc(document, images)
-       end
+       var nit_src = new GamnitImageSetSrc(document, images)
 
        if not src_path.file_extension == "nit" then
                src_path = src_path/drawing_name+".nit"
@@ -394,12 +344,14 @@ for drawing in drawings do
        nit_src.write_to(src_file)
        src_file.close
 
-       # Find closest power of 2
-       var dx = max_x - min_x
-       max_x = dx.next_pow2 + min_x
+       # Find next power of 2
+       if opt_pow2.value then
+               var dx = max_x - min_x
+               max_x = dx.next_pow2 + min_x
 
-       var dy = max_y - min_y
-       max_y = dy.next_pow2 + min_y
+               var dy = max_y - min_y
+               max_y = dy.next_pow2 + min_y
+       end
 
        # Inkscape's --export-area inverts the Y axis. It uses the lower left corner of
        # the drawing area where as queries return coordinates from the top left.
@@ -409,7 +361,7 @@ for drawing in drawings do
        # Output png file to assets
        var png_path = "{assets_path}/images/{drawing_name}.png"
        var proc2 = new Process.from_a(prog, [drawing, "--without-gui",
-               "--export-dpi={(90.0*scale).to_i}",
+               "--export-dpi={(default_dpi*scale).to_i}",
                "--export-png={png_path}",
                "--export-area={min_x}:{y0}:{max_x}:{y1}",
                "--export-background=#000000", "--export-background-opacity=0.0"])