--!movie
--!encoding=utf-8
--****************************************************************************
-- @file SVG to Image Converter (for both OS X and Windows)
-- @author Valentin Schmidt
-- @version 0.4
-- @requires xtra("QtWidgets")
--
-- Usage:
--
-- -- required args
-- svgFile = _movie.path & "test.svg"
-- imgFile = _movie.path & "test.jpg"
--
-- -- optional args
-- imgWidth = 480
-- imgHeight = VOID -- if VOID, calculated automatically keeping SVG's ratio
-- imgBgColor = rgb(255,255,255)
-- imgFormat = "jpg" -- bmp | gif | jpg | pbm | pgm | png | ppm | tga | tiff | xbm | xpm
-- imgQuality = 100 -- 0-100, only used for jpg
--
-- svgToImage (svgFile, imgFile, imgWidth, imgHeight, imgBgColor, imgFormat, imgQuality)
--
--****************************************************************************
global gQt
----------------------------------------
-- Converts SVG to image file.
-- Requires QtWidgets Xtra instance in global variable 'gQt'.
--
-- @param {string} svgFile
-- @param {string} imgFile
-- @param {integer} [imgWidth] - optional, if VOID, calculated automatically keeping SVG's ratio
-- @param {integer} [imgHeight] - optional, if VOID, calculated automatically keeping SVG's ratio
-- @param {color} [imgBgColor] - optional, default = current default widget bgColor
-- @param {string} [imgFormat] - optional, default = type extracted from imgFile
-- @param {integer} [imgQuality] - optional, only used for jpg, default = Qt's default JPEG compression
----------------------------------------
on svgToImage (svgFile, imgFile, imgWidth, imgHeight, imgBgColor, imgFormat, imgQuality)
if voidP(imgFormat) then
od = the itemdelimiter
the itemdelimiter = "."
imgFormat = the last item of imgFile
the itemdelimiter = od
end if
if voidP(imgQuality) then imgQuality = -1
if the platform contains "mac" then
-- QtWidgets Xtra for mac needs POSIX pathes
svgFile = qx_hfs2posix(svgFile)
imgFile = qx_hfs2posix(imgFile)
end if
-- create Qt Image object
imageID = gQt.newObject(#Image)
-- if BOTH imgWidth AND imgHeight are VOID, the SVG is rendered in its original size.
-- For this we don't need a SVG widget, but simply load the SVG into an image object
if voidP(imgWidth) AND voidP(imgHeight) then
-- load SVG file into image object
gQt.callObject(imageID, #load, svgFile)
-- if a custom bgColor is specified, we use a Painter object to compose a background layer with
-- specified bgColor and the (transparent) SVG image
if not voidP(imgBgColor) then
-- create Qt Painter object
painterID = gQt.newObject(#Painter)
gQt.callObject(painterID, #begin, imageID)
-- fill background with imgBgColor
w = gQt.callObject(imageID, #width)
h = gQt.callObject(imageID, #height)
gQt.callObject(painterID, #fillRect, rect(0,0,w,h), imgBgColor)
-- draw SVG image on top of background
gQt.callObject(painterID, #drawImage, point(0,0), svgFile)
-- release the Painter object
gQt.callObject(painterID, #destroy)
end if
-- save image as file
gQt.callObject(imageID, #save, imgFile, imgFormat, imgQuality)
-- release the image object
gQt.callObject(imageID, #destroy)
-- exit function
return
end if
-- if EITHER imgWidth OR imgHeight are VOID, the other dimension is calculated, keeping SVG's original ratio
if voidP(imgWidth) OR voidP(imgHeight) then
-- load SVG file into image object
gQt.callObject(imageID, #load, svgFile)
w = gQt.callObject(imageID, #width)
h = gQt.callObject(imageID, #height)
if voidP(imgHeight) then imgHeight = imgWidth * h / w
else if voidP(imgWidth) then imgWidth = imgHeight * w / h
end if
-- QtWidgets Xtra for mac needs a region for grabbing widget images
if the platform contains "mac" then
regionID = gQt.createWidgetRegion(rect(0,0,0,0))
else
regionID = 0
end if
-- create a SVG widget (hidden)
widgetID = gQt.newWidget(#SvgWidget, regionID)
if not voidP(imgBgColor) then
-- set widget BG color (used for tranparent regions in SVG)
paletteID = gQt.callWidget(widgetID, #getPalette)
gQt.callObject(paletteID, #setColor, 10, imgBgColor) -- 10 = color role for bgColor
gQt.callWidget(widgetID, #setPalette, paletteID)
end if
-- set widget rect (=size of exported image)
gQt.callWidget(widgetID, #setGeometry, rect(0,0,imgWidth,imgHeight))
-- load SVG file into SVG widget
gQt.callWidget(widgetID, #load, svgFile)
-- required only for mac
if the platform contains "mac" then gQt.callWidget(widgetID, #show)
-- grab image of SVG widget
gQt.callObject(imageID, #grabWidget, widgetID)
-- save image as file
gQt.callObject(imageID, #save, imgFile, imgFormat, imgQuality)
-- release the image object
gQt.callObject(imageID, #destroy)
if regionID<>0 then
-- mac: release the widget region (which also releases its SVG child widget)
gQt.callWidget(regionID, #destroy)
else
-- win: release the SVG widget
gQt.callWidget(widgetID, #destroy)
end if
end