1. --!movie
  2. --!encoding=utf-8
  3.  
  4. --****************************************************************************
  5. -- @file      SVG to Image Converter (for both OS X and Windows)
  6. -- @author    Valentin Schmidt
  7. -- @version   0.4
  8. -- @requires  xtra("QtWidgets")
  9. --
  10. -- Usage:
  11. --
  12. -- -- required args
  13. -- svgFile = _movie.path & "test.svg"
  14. -- imgFile = _movie.path & "test.jpg"
  15. --
  16. -- -- optional args
  17. -- imgWidth = 480
  18. -- imgHeight = VOID -- if VOID, calculated automatically keeping SVG's ratio
  19. -- imgBgColor = rgb(255,255,255)
  20. -- imgFormat = "jpg" -- bmp | gif | jpg | pbm | pgm | png | ppm | tga | tiff | xbm | xpm
  21. -- imgQuality = 100 -- 0-100, only used for jpg
  22. --
  23. -- svgToImage (svgFile, imgFile, imgWidth, imgHeight, imgBgColor, imgFormat, imgQuality)
  24. --
  25. --****************************************************************************
  26.  
  27. global gQt
  28.  
  29. ----------------------------------------
  30. -- Converts SVG to image file.
  31. -- Requires QtWidgets Xtra instance in global variable 'gQt'.
  32. --
  33. -- @param {string} svgFile
  34. -- @param {string} imgFile
  35. -- @param {integer} [imgWidth] - optional, if VOID, calculated automatically keeping SVG's ratio
  36. -- @param {integer} [imgHeight] - optional, if VOID, calculated automatically keeping SVG's ratio
  37. -- @param {color} [imgBgColor] - optional, default = current default widget bgColor
  38. -- @param {string} [imgFormat] - optional, default = type extracted from imgFile
  39. -- @param {integer} [imgQuality] - optional, only used for jpg, default = Qt's default JPEG compression
  40. ----------------------------------------
  41. on svgToImage (svgFile, imgFile, imgWidth, imgHeight, imgBgColor, imgFormat, imgQuality)
  42.  
  43.   if voidP(imgFormat) then
  44.     od = the itemdelimiter
  45.     the itemdelimiter = "."
  46.     imgFormat = the last item of imgFile
  47.     the itemdelimiter = od
  48.   end if
  49.   if voidP(imgQuality) then imgQuality = -1
  50.  
  51.   if the platform contains "mac" then
  52.     -- QtWidgets Xtra for mac needs POSIX pathes
  53.     svgFile = qx_hfs2posix(svgFile)
  54.     imgFile = qx_hfs2posix(imgFile)
  55.   end if
  56.  
  57.   -- create Qt Image object
  58.   imageID = gQt.newObject(#Image)
  59.  
  60.   -- if BOTH imgWidth AND imgHeight are VOID, the SVG is rendered in its original size.
  61.   -- For this we don't need a SVG widget, but simply load the SVG into an image object
  62.   if voidP(imgWidth) AND voidP(imgHeight) then
  63.  
  64.     -- load SVG file into image object
  65.     gQt.callObject(imageID, #load, svgFile)
  66.  
  67.     -- if a custom bgColor is specified, we use a Painter object to compose a background layer with
  68.     -- specified bgColor and the (transparent) SVG image
  69.     if not voidP(imgBgColor) then
  70.  
  71.       -- create Qt Painter object
  72.       painterID = gQt.newObject(#Painter)
  73.       gQt.callObject(painterID, #begin, imageID)
  74.  
  75.       -- fill background with imgBgColor
  76.       w = gQt.callObject(imageID, #width)
  77.       h = gQt.callObject(imageID, #height)
  78.       gQt.callObject(painterID, #fillRect, rect(0,0,w,h), imgBgColor)
  79.  
  80.       -- draw SVG image on top of background
  81.       gQt.callObject(painterID, #drawImage, point(0,0), svgFile)
  82.  
  83.       -- release the Painter object
  84.       gQt.callObject(painterID, #destroy)
  85.  
  86.     end if
  87.  
  88.     -- save image as file
  89.     gQt.callObject(imageID, #save, imgFile, imgFormat, imgQuality)
  90.  
  91.     -- release the image object
  92.     gQt.callObject(imageID, #destroy)
  93.  
  94.     -- exit function
  95.     return
  96.  
  97.   end if
  98.  
  99.   -- if EITHER imgWidth OR imgHeight are VOID, the other dimension is calculated, keeping SVG's original ratio
  100.   if voidP(imgWidth) OR voidP(imgHeight) then
  101.  
  102.     -- load SVG file into image object
  103.     gQt.callObject(imageID, #load, svgFile)
  104.  
  105.     w = gQt.callObject(imageID, #width)
  106.     h = gQt.callObject(imageID, #height)
  107.  
  108.     if voidP(imgHeight) then imgHeight = imgWidth * h / w
  109.     else if voidP(imgWidth) then imgWidth = imgHeight * w / h
  110.  
  111.   end if
  112.  
  113.   -- QtWidgets Xtra for mac needs a region for grabbing widget images
  114.   if the platform contains "mac" then
  115.     regionID = gQt.createWidgetRegion(rect(0,0,0,0))
  116.   else
  117.     regionID = 0
  118.   end if
  119.  
  120.   -- create a SVG widget (hidden)
  121.   widgetID = gQt.newWidget(#SvgWidget, regionID)
  122.  
  123.   if not voidP(imgBgColor) then
  124.     -- set widget BG color (used for tranparent regions in SVG)
  125.     paletteID = gQt.callWidget(widgetID, #getPalette)
  126.     gQt.callObject(paletteID, #setColor, 10, imgBgColor) -- 10 = color role for bgColor
  127.     gQt.callWidget(widgetID, #setPalette, paletteID)
  128.   end if
  129.  
  130.   -- set widget rect (=size of exported image)
  131.   gQt.callWidget(widgetID, #setGeometry, rect(0,0,imgWidth,imgHeight))
  132.  
  133.   -- load SVG file into SVG widget
  134.   gQt.callWidget(widgetID, #load, svgFile)
  135.  
  136.   -- required only for mac
  137.   if the platform contains "mac" then gQt.callWidget(widgetID, #show)
  138.  
  139.   -- grab image of SVG widget
  140.   gQt.callObject(imageID, #grabWidget, widgetID)
  141.  
  142.   -- save image as file
  143.   gQt.callObject(imageID, #save, imgFile, imgFormat, imgQuality)
  144.  
  145.   -- release the image object
  146.   gQt.callObject(imageID, #destroy)
  147.  
  148.   if regionID<>0 then
  149.     -- mac: release the widget region (which also releases its SVG child widget)
  150.     gQt.callWidget(regionID, #destroy)
  151.   else
  152.     -- win: release the SVG widget
  153.     gQt.callWidget(widgetID, #destroy)
  154.   end if
  155.  
  156. end
  157.  
[raw code]