adjust code / indentation style
no functional change
This commit is contained in:
parent
d793a07037
commit
27db94a64c
4 changed files with 332 additions and 316 deletions
|
|
@ -25,11 +25,8 @@ import os
|
|||
import sys
|
||||
import glob
|
||||
import fnmatch
|
||||
import re
|
||||
import tarfile
|
||||
|
||||
from SCons import Util
|
||||
from SCons.Action import Action
|
||||
|
||||
|
||||
|
||||
|
|
@ -65,12 +62,12 @@ def scanSubtree(roots, patterns=SRCPATTERNS):
|
|||
(python generator function)
|
||||
"""
|
||||
for root in globRootdirs(roots):
|
||||
for (dir,_,files) in os.walk(root):
|
||||
if dir.startswith('./'):
|
||||
dir = dir[2:]
|
||||
for (d,_,files) in os.walk(root):
|
||||
if d.startswith('./'):
|
||||
d = d[2:]
|
||||
for p in patterns:
|
||||
for f in fnmatch.filter(files, p):
|
||||
yield os.path.join(dir,f)
|
||||
yield os.path.join(d,f)
|
||||
|
||||
|
||||
|
||||
|
|
@ -78,16 +75,16 @@ def globRootdirs(roots):
|
|||
""" helper: expand shell wildcards and filter the resulting list,
|
||||
so that it only contains existing directories
|
||||
"""
|
||||
filter = lambda f: os.path.isdir(f) and os.path.exists(f)
|
||||
isDirectory = lambda f: os.path.isdir(f) and os.path.exists(f)
|
||||
roots = glob.glob(roots)
|
||||
return (dir for dir in roots if filter(dir) )
|
||||
return (d for d in roots if isDirectory(d) )
|
||||
|
||||
|
||||
|
||||
def findSrcTrees(location, patterns=SRCPATTERNS):
|
||||
""" find possible source tree roots, starting with the given location.
|
||||
When delving down from the initial location(s), a source tree is defined
|
||||
as a directory containing source files and possibly further sub directories.
|
||||
as a directory containidsource files and possibly further sub directories.
|
||||
After having initially expanded the given location with #globRootdirs, each
|
||||
directory is examined depth first, until encountering a directory containing
|
||||
source files, which then yields a result. Especially, this can be used to traverse
|
||||
|
|
@ -95,11 +92,11 @@ def findSrcTrees(location, patterns=SRCPATTERNS):
|
|||
to be built into packages, plugins, individual tool executables etc.
|
||||
@return: the relative path names of all source root dirs found (generator function).
|
||||
"""
|
||||
for dir in globRootdirs(location):
|
||||
if isSrcDir(dir,patterns):
|
||||
yield dir
|
||||
for directory in globRootdirs(location):
|
||||
if isSrcDir (directory,patterns):
|
||||
yield directory
|
||||
else:
|
||||
for result in findSrcTrees(str(dir)+'/*'):
|
||||
for result in findSrcTrees (str(directory)+'/*'):
|
||||
yield result
|
||||
|
||||
|
||||
|
|
@ -109,7 +106,7 @@ def isSrcDir(path, patterns=SRCPATTERNS):
|
|||
@return: True if it's a directory containing any source file
|
||||
"""
|
||||
if not os.path.isdir(path):
|
||||
return False
|
||||
return False
|
||||
else:
|
||||
for p in patterns:
|
||||
if glob.glob(path+'/'+p):
|
||||
|
|
@ -124,30 +121,30 @@ def filterNodes(nlist, removeName=None):
|
|||
if removeName:
|
||||
predicate = lambda n : not fnmatch.fnmatch(os.path.basename(str(n[0])), removeName)
|
||||
else:
|
||||
predicate = lambda n : True;
|
||||
predicate = lambda n : True
|
||||
|
||||
return filter(predicate, nlist)
|
||||
|
||||
|
||||
|
||||
def getDirname(dir, basePrefix=None):
|
||||
def getDirname (d, basePrefix=None):
|
||||
""" extract directory name without leading path,
|
||||
or without the explicitly given basePrefix
|
||||
"""
|
||||
dir = os.path.realpath(dir)
|
||||
if not os.path.isdir(dir):
|
||||
dir,_ = os.path.split(dir)
|
||||
d = os.path.realpath(d)
|
||||
if not os.path.isdir(d):
|
||||
d,_ = os.path.split(d)
|
||||
if basePrefix:
|
||||
basePrefix = os.path.realpath(basePrefix)
|
||||
if str(dir).startswith(basePrefix):
|
||||
name = str(dir)[len(basePrefix):]
|
||||
if str(d).startswith(basePrefix):
|
||||
name = str(d)[len(basePrefix):]
|
||||
else:
|
||||
_, name = os.path.split(dir)
|
||||
_, name = os.path.split(d)
|
||||
return name
|
||||
|
||||
|
||||
|
||||
def createPlugins(env, dir, **kw):
|
||||
def createPlugins(env, directory, **kw):
|
||||
""" investigate the given source directory to identify all contained source trees.
|
||||
@return: a list of build nodes defining a plugin for each of these source trees.
|
||||
"""
|
||||
|
|
@ -155,7 +152,7 @@ def createPlugins(env, dir, **kw):
|
|||
, srcSubtree(tree)
|
||||
, **kw
|
||||
)
|
||||
for tree in findSrcTrees(dir)
|
||||
for tree in findSrcTrees(directory)
|
||||
]
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -54,130 +54,133 @@ artworkLayerPrefix = "artwork:"
|
|||
#
|
||||
|
||||
|
||||
def createDirectory( name ):
|
||||
try:
|
||||
if os.path.isfile(name):
|
||||
os.remove(name)
|
||||
if not os.path.exists(name):
|
||||
os.mkdir(name)
|
||||
except:
|
||||
print 'WARNING: createDirectory("%s") failed. Permission problems?' % name
|
||||
def createDirectory (name):
|
||||
try:
|
||||
if os.path.isfile (name):
|
||||
os.remove (name)
|
||||
if not os.path.exists (name):
|
||||
os.mkdir (name)
|
||||
except:
|
||||
print 'WARNING: createDirectory("%s") failed. Permission problems?' % name
|
||||
|
||||
|
||||
def copyMergeDirectory( src, dst ):
|
||||
listing = os.listdir(src)
|
||||
for file_name in listing:
|
||||
src_file_path = os.path.join(src, file_name)
|
||||
dst_file_path = os.path.join(dst, file_name)
|
||||
shutil.copyfile(src_file_path, dst_file_path)
|
||||
def copyMergeDirectory (src, dst):
|
||||
listing = os.listdir (src)
|
||||
for file_name in listing:
|
||||
src_file_path = os.path.join (src, file_name)
|
||||
dst_file_path = os.path.join (dst, file_name)
|
||||
shutil.copyfile (src_file_path, dst_file_path)
|
||||
|
||||
def getDocumentSize( svg_element ):
|
||||
width = float(svg_element.getAttribute("width"))
|
||||
height = float(svg_element.getAttribute("height"))
|
||||
return [width, height]
|
||||
def getDocumentSize (svg_element):
|
||||
width = float(svg_element.getAttribute("width"))
|
||||
height = float(svg_element.getAttribute("height"))
|
||||
return [width, height]
|
||||
|
||||
def findChildLayerElement( parent_element ):
|
||||
for node in parent_element.childNodes:
|
||||
if node.nodeType == minidom.Node.ELEMENT_NODE:
|
||||
if node.tagName == "g":
|
||||
if node.getAttribute("inkscape:groupmode") == "layer":
|
||||
return node
|
||||
return None
|
||||
|
||||
def parsePlateLayer( layer ):
|
||||
rectangles = []
|
||||
for node in layer.childNodes:
|
||||
if node.nodeType == minidom.Node.ELEMENT_NODE:
|
||||
if node.tagName == "rect":
|
||||
x = float(node.getAttribute("x"))
|
||||
y = float(node.getAttribute("y"))
|
||||
width = float(node.getAttribute("width"))
|
||||
height = float(node.getAttribute("height"))
|
||||
rectangles.append([x, y, width, height])
|
||||
return rectangles
|
||||
def findChildLayerElement (parent_element):
|
||||
for node in parent_element.childNodes:
|
||||
if node.nodeType == minidom.Node.ELEMENT_NODE:
|
||||
if node.tagName == "g":
|
||||
if node.getAttribute("inkscape:groupmode") == "layer":
|
||||
return node
|
||||
return None
|
||||
|
||||
def parseSVG( file_path ):
|
||||
print "Parsing " + file_path
|
||||
svgdoc = minidom.parse(file_path)
|
||||
for root_node in svgdoc.childNodes:
|
||||
if root_node.nodeType == minidom.Node.ELEMENT_NODE:
|
||||
if root_node.tagName == "svg":
|
||||
size = getDocumentSize( root_node )
|
||||
layer = findChildLayerElement( root_node )
|
||||
if layer != None:
|
||||
layer_name = layer.getAttribute("inkscape:label")
|
||||
if layer_name[:len(artworkLayerPrefix)] == artworkLayerPrefix:
|
||||
artwork_name = layer_name[len(artworkLayerPrefix):]
|
||||
plate = findChildLayerElement( layer )
|
||||
if plate != None:
|
||||
return artwork_name, size, parsePlateLayer( plate )
|
||||
return None
|
||||
def parsePlateLayer (layer):
|
||||
rectangles = []
|
||||
for node in layer.childNodes:
|
||||
if node.nodeType == minidom.Node.ELEMENT_NODE:
|
||||
if node.tagName == "rect":
|
||||
x = float(node.getAttribute("x"))
|
||||
y = float(node.getAttribute("y"))
|
||||
width = float(node.getAttribute("width"))
|
||||
height = float(node.getAttribute("height"))
|
||||
rectangles.append([x, y, width, height])
|
||||
return rectangles
|
||||
|
||||
def renderSvgRsvg(file_path, out_dir, artwork_name, rectangle, doc_size):
|
||||
# Prepare a Cairo context
|
||||
width = int(rectangle[2])
|
||||
height = int(rectangle[3])
|
||||
|
||||
if not os.path.exists(rsvgPath):
|
||||
print "Error: executable %s not found." % rsvgPath
|
||||
|
||||
def parseSVG (file_path):
|
||||
print "Parsing " + file_path
|
||||
svgdoc = minidom.parse (file_path)
|
||||
for root_node in svgdoc.childNodes:
|
||||
if root_node.nodeType == minidom.Node.ELEMENT_NODE:
|
||||
if root_node.tagName == "svg":
|
||||
size = getDocumentSize (root_node)
|
||||
layer = findChildLayerElement (root_node)
|
||||
if layer != None:
|
||||
layer_name = layer.getAttribute ("inkscape:label")
|
||||
if layer_name[:len(artworkLayerPrefix)] == artworkLayerPrefix:
|
||||
artwork_name = layer_name[len(artworkLayerPrefix):]
|
||||
plate = findChildLayerElement(layer)
|
||||
if plate != None:
|
||||
return artwork_name, size, parsePlateLayer(plate)
|
||||
return None
|
||||
|
||||
|
||||
def renderSvgRsvg (file_path, out_dir, artwork_name, rectangle, _doc_size):
|
||||
# Prepare a Cairo context
|
||||
width = int(rectangle[2])
|
||||
height = int(rectangle[3])
|
||||
|
||||
os.spawnlp(os.P_WAIT, rsvgPath, rsvgPath,
|
||||
"--source-rect=%g:%g:%g:%g" % (rectangle[0], rectangle[1], rectangle[2], rectangle[3]),
|
||||
"--output=" + os.path.join(out_dir, "%gx%g/%s.png" % (rectangle[2], rectangle[3], artwork_name)),
|
||||
file_path)
|
||||
if not os.path.exists(rsvgPath):
|
||||
print "Error: executable %s not found." % rsvgPath
|
||||
|
||||
os.spawnlp(os.P_WAIT, rsvgPath, rsvgPath,
|
||||
"--source-rect=%g:%g:%g:%g" % (rectangle[0], rectangle[1], width, height),
|
||||
"--output=" + os.path.join(out_dir, "%gx%g/%s.png" % (width, height, artwork_name)),
|
||||
file_path)
|
||||
|
||||
def renderSvgIcon(file_path, out_dir):
|
||||
artwork_name, doc_size, rectangles = parseSVG(file_path)
|
||||
for rectangle in rectangles:
|
||||
renderSvgRsvg(file_path, out_dir, artwork_name, rectangle, doc_size)
|
||||
def renderSvgIcon (file_path, out_dir):
|
||||
artwork_name, doc_size, rectangles = parseSVG (file_path)
|
||||
for rectangle in rectangles:
|
||||
renderSvgRsvg(file_path, out_dir, artwork_name, rectangle, doc_size)
|
||||
|
||||
def getTargetNames(file_path):
|
||||
"""get a list of target names to be rendered from the given source SVG
|
||||
usable to setup the build targets for SCons
|
||||
"""
|
||||
artwork_name, _ , rectangles = parseSVG(file_path)
|
||||
return ["%gx%g/%s.png" % (rectangle[2], rectangle[3], artwork_name) for rectangle in rectangles ]
|
||||
def getTargetNames (file_path):
|
||||
"""get a list of target names to be rendered from the given source SVG
|
||||
usable to setup the build targets for SCons
|
||||
"""
|
||||
artwork_name, _ , rectangles = parseSVG (file_path)
|
||||
return ["%gx%g/%s.png" % (rectangle[2], rectangle[3], artwork_name) for rectangle in rectangles ]
|
||||
|
||||
|
||||
def printHelp():
|
||||
print "render-icon.py SRCFILE.svg TARGETDIR"
|
||||
print "An icon rendering utility script for lumiera"
|
||||
print "render-icon.py SRCFILE.svg TARGETDIR"
|
||||
print "An icon rendering utility script for lumiera"
|
||||
|
||||
def parseArguments(argv):
|
||||
optlist, args = getopt.getopt(argv, "")
|
||||
|
||||
if len(args) == 2:
|
||||
return args[0], args[1]
|
||||
|
||||
printHelp()
|
||||
return None, None
|
||||
|
||||
def main(argv):
|
||||
in_path, out_dir = parseArguments(argv)
|
||||
|
||||
if not (in_path and out_dir):
|
||||
print "Missing arguments in_path and out_dir."
|
||||
sys.exit(1)
|
||||
|
||||
if os.path.isfile(out_dir):
|
||||
print "Unable to use '%s' as output directory, because it\'s a file." % out_dir
|
||||
sys.exit(1)
|
||||
if not os.path.isdir(out_dir):
|
||||
print "Output directory '%s' not found." % out_dir
|
||||
sys.exit(1)
|
||||
|
||||
# Create the icons folders
|
||||
createDirectory(os.path.join(out_dir, "48x48"))
|
||||
createDirectory(os.path.join(out_dir, "32x32"))
|
||||
createDirectory(os.path.join(out_dir, "24x24"))
|
||||
createDirectory(os.path.join(out_dir, "22x22"))
|
||||
createDirectory(os.path.join(out_dir, "16x16"))
|
||||
|
||||
renderSvgIcon(in_path, out_dir)
|
||||
_optlist, args = getopt.getopt(argv, "")
|
||||
|
||||
# Copy in prerendered icons
|
||||
#copyPrerenderedIcons()
|
||||
|
||||
if len(args) == 2:
|
||||
return args[0], args[1]
|
||||
|
||||
printHelp()
|
||||
return None, None
|
||||
|
||||
|
||||
def main (argv):
|
||||
in_path, out_dir = parseArguments(argv)
|
||||
|
||||
if not (in_path and out_dir):
|
||||
print "Missing arguments in_path and out_dir."
|
||||
sys.exit(1)
|
||||
|
||||
if os.path.isfile(out_dir):
|
||||
print "Unable to use '%s' as output directory, because it\'s a file." % out_dir
|
||||
sys.exit(1)
|
||||
if not os.path.isdir(out_dir):
|
||||
print "Output directory '%s' not found." % out_dir
|
||||
sys.exit(1)
|
||||
|
||||
# Create the icons folders
|
||||
createDirectory(os.path.join(out_dir, "48x48"))
|
||||
createDirectory(os.path.join(out_dir, "32x32"))
|
||||
createDirectory(os.path.join(out_dir, "24x24"))
|
||||
createDirectory(os.path.join(out_dir, "22x22"))
|
||||
createDirectory(os.path.join(out_dir, "16x16"))
|
||||
|
||||
renderSvgIcon (in_path, out_dir)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
main(sys.argv[1:])
|
||||
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@
|
|||
|
||||
from os import path
|
||||
|
||||
import SCons
|
||||
import SCons.SConf
|
||||
from SCons.Action import Action
|
||||
from SCons.Environment import Environment
|
||||
|
||||
from Buildhelper import *
|
||||
|
|
@ -157,43 +157,43 @@ def register_LumieraResourceBuilder(env):
|
|||
return (generateTargets, source)
|
||||
|
||||
def IconResource(env, source):
|
||||
"""Copy icon pixmap to corresponding icon dir. """
|
||||
subdir = getDirname(str(source))
|
||||
toBuild = env.path.buildIcon+subdir
|
||||
toInstall = env.path.installIcon+subdir
|
||||
env.Install (toInstall, source)
|
||||
return env.Install(toBuild, source)
|
||||
"""Copy icon pixmap to corresponding icon dir. """
|
||||
subdir = getDirname(str(source))
|
||||
toBuild = env.path.buildIcon+subdir
|
||||
toInstall = env.path.installIcon+subdir
|
||||
env.Install (toInstall, source)
|
||||
return env.Install(toBuild, source)
|
||||
|
||||
def GuiResource(env, source):
|
||||
subdir = getDirname(str(source))
|
||||
toBuild = env.path.buildUIRes+subdir
|
||||
toInstall = env.path.installUIRes+subdir
|
||||
env.Install (toInstall, source)
|
||||
return env.Install(toBuild, source)
|
||||
subdir = getDirname(str(source))
|
||||
toBuild = env.path.buildUIRes+subdir
|
||||
toInstall = env.path.installUIRes+subdir
|
||||
env.Install (toInstall, source)
|
||||
return env.Install(toBuild, source)
|
||||
|
||||
def ConfigData(env, source, targetDir=None):
|
||||
""" install (copy) configuration- and metadata.
|
||||
target dir is either the install location configured (in SConstruct),
|
||||
or an explicitly given absolute or relative path segment, which might refer
|
||||
to the location of the executable through the $ORIGIN token
|
||||
"""
|
||||
subdir = getDirname(str(source), env.path.srcConf) # removes source location path prefix
|
||||
if targetDir:
|
||||
if path.isabs(targetDir):
|
||||
toBuild = toInstall = path.join(targetDir,subdir)
|
||||
else:
|
||||
if targetDir.startswith('$ORIGIN'):
|
||||
targetDir = targetDir[len('$ORIGIN'):]
|
||||
toBuild = path.join(env.path.buildExe, targetDir, subdir)
|
||||
toInstall = path.join(env.path.installExe, targetDir, subdir)
|
||||
else:
|
||||
toBuild = path.join(env.path.buildConf, targetDir, subdir)
|
||||
toInstall = path.join(env.path.installConf, targetDir, subdir)
|
||||
else:
|
||||
toBuild = path.join(env.path.buildConf,subdir)
|
||||
toInstall = path.join(env.path.installConf,subdir)
|
||||
env.Install (toInstall, source)
|
||||
return env.Install(toBuild, source)
|
||||
""" install (copy) configuration- and metadata.
|
||||
target dir is either the install location configured (in SConstruct),
|
||||
or an explicitly given absolute or relative path segment, which might refer
|
||||
to the location of the executable through the $ORIGIN token
|
||||
"""
|
||||
subdir = getDirname(str(source), env.path.srcConf) # removes source location path prefix
|
||||
if targetDir:
|
||||
if path.isabs(targetDir):
|
||||
toBuild = toInstall = path.join(targetDir,subdir)
|
||||
else:
|
||||
if targetDir.startswith('$ORIGIN'):
|
||||
targetDir = targetDir[len('$ORIGIN'):]
|
||||
toBuild = path.join(env.path.buildExe, targetDir, subdir)
|
||||
toInstall = path.join(env.path.installExe, targetDir, subdir)
|
||||
else:
|
||||
toBuild = path.join(env.path.buildConf, targetDir, subdir)
|
||||
toInstall = path.join(env.path.installConf, targetDir, subdir)
|
||||
else:
|
||||
toBuild = path.join(env.path.buildConf,subdir)
|
||||
toInstall = path.join(env.path.installConf,subdir)
|
||||
env.Install (toInstall, source)
|
||||
return env.Install(toBuild, source)
|
||||
|
||||
|
||||
buildIcon = env.Builder( action = Action(invokeRenderer, "rendering Icon: $SOURCE --> $TARGETS")
|
||||
|
|
|
|||
|
|
@ -1,32 +1,28 @@
|
|||
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
|
||||
/*
|
||||
rsvg-convert.c - Command line utility for exercising rsvg with cairo.
|
||||
|
||||
Copyright (C) Red Hat, Inc.
|
||||
2005, Carl Worth <cworth@cworth.org>
|
||||
Caleb Moore <c.moore@student.unsw.edu.au>
|
||||
Dom Lachowicz <cinamod@hotmail.com>
|
||||
2008, Joel Holdsworth <joel@airwebreathe.org.uk>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
rsvg-convert.c: Command line utility for exercising rsvg with cairo.
|
||||
|
||||
Copyright (C) 2005 Red Hat, Inc.
|
||||
Copyright (C) 2005 Dom Lachowicz <cinamod@hotmail.com>
|
||||
Copyright (C) 2005 Caleb Moore <c.moore@student.unsw.edu.au>
|
||||
Copyright (C) 2008 Joel Holdsworth <joel@airwebreathe.org.uk>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this program; if not, write to the
|
||||
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
Boston, MA 02111-1307, USA.
|
||||
|
||||
Authors: Carl Worth <cworth@cworth.org>,
|
||||
Caleb Moore <c.moore@student.unsw.edu.au>,
|
||||
Dom Lachowicz <cinamod@hotmail.com>,
|
||||
Joel Holdsworth <joel@airwebreathe.org.uk>
|
||||
*/
|
||||
|
||||
#ifndef N_
|
||||
#define N_(X) X
|
||||
|
|
@ -40,183 +36,203 @@
|
|||
#include <librsvg/rsvg.h>
|
||||
#include <librsvg/rsvg-cairo.h>
|
||||
|
||||
|
||||
#ifdef CAIRO_HAS_PS_SURFACE
|
||||
#include <cairo-ps.h>
|
||||
#include <cairo-ps.h>
|
||||
#endif
|
||||
|
||||
#ifdef CAIRO_HAS_PDF_SURFACE
|
||||
#include <cairo-pdf.h>
|
||||
#include <cairo-pdf.h>
|
||||
#endif
|
||||
|
||||
#ifdef CAIRO_HAS_SVG_SURFACE
|
||||
#include <cairo-svg.h>
|
||||
#include <cairo-svg.h>
|
||||
#endif
|
||||
|
||||
#ifndef _
|
||||
#define _(X) X
|
||||
#define _(X) X
|
||||
#endif
|
||||
|
||||
struct RsvgSizeCallbackData {
|
||||
|
||||
struct RsvgSizeCallbackData
|
||||
{
|
||||
gint width;
|
||||
gint height;
|
||||
};
|
||||
};
|
||||
|
||||
struct RsvgSourceRectangle {
|
||||
struct RsvgSourceRectangle
|
||||
{
|
||||
double left;
|
||||
double top;
|
||||
double width;
|
||||
double height;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
static void
|
||||
display_error (GError * err)
|
||||
{
|
||||
if (err) {
|
||||
g_print ("%s\n", err->message);
|
||||
g_error_free (err);
|
||||
if (err)
|
||||
{
|
||||
g_print ("%s\n", err->message);
|
||||
g_error_free (err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
rsvg_cairo_size_callback (int *width, int *height, gpointer data)
|
||||
{
|
||||
RsvgDimensionData *dimensions = data;
|
||||
*width = dimensions->width;
|
||||
*height = dimensions->height;
|
||||
RsvgDimensionData *dimensions = data;
|
||||
*width = dimensions->width;
|
||||
*height = dimensions->height;
|
||||
}
|
||||
|
||||
|
||||
static cairo_status_t
|
||||
rsvg_cairo_write_func (void *closure, const unsigned char *data, unsigned int length)
|
||||
{
|
||||
if (fwrite (data, 1, length, (FILE *) closure) == length)
|
||||
return CAIRO_STATUS_SUCCESS;
|
||||
return CAIRO_STATUS_WRITE_ERROR;
|
||||
if (fwrite (data, 1, length, (FILE *) closure) == length)
|
||||
return CAIRO_STATUS_SUCCESS;
|
||||
return CAIRO_STATUS_WRITE_ERROR;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
GOptionContext *g_option_context;
|
||||
int width = -1;
|
||||
int height = -1;
|
||||
char *source_rect_string = NULL;
|
||||
char *output = NULL;
|
||||
GError *error = NULL;
|
||||
char *filename = NULL;
|
||||
|
||||
char **args = NULL;
|
||||
RsvgHandle *rsvg;
|
||||
cairo_surface_t *surface = NULL;
|
||||
cairo_t *cr = NULL;
|
||||
RsvgDimensionData dimensions;
|
||||
FILE *output_file = stdout;
|
||||
|
||||
struct RsvgSourceRectangle source_rect = {0, 0, 0, 0};
|
||||
GOptionContext *g_option_context;
|
||||
int width = -1;
|
||||
int height = -1;
|
||||
char *source_rect_string = NULL;
|
||||
char *output = NULL;
|
||||
GError *error = NULL;
|
||||
char *filename = NULL;
|
||||
|
||||
char **args = NULL;
|
||||
RsvgHandle *rsvg;
|
||||
cairo_surface_t *surface = NULL;
|
||||
cairo_t *cr = NULL;
|
||||
RsvgDimensionData dimensions;
|
||||
FILE *output_file = stdout;
|
||||
|
||||
struct RsvgSourceRectangle source_rect = {0, 0, 0, 0};
|
||||
|
||||
GOptionEntry options_table[] = {
|
||||
{"width", 'w', 0, G_OPTION_ARG_INT, &width,
|
||||
N_("width [optional; defaults to the SVG's width]"), N_("<int>")},
|
||||
{"height", 'h', 0, G_OPTION_ARG_INT, &height,
|
||||
N_("height [optional; defaults to the SVG's height]"), N_("<int>")},
|
||||
{"source-rect", 'r', 0, G_OPTION_ARG_STRING, &source_rect_string,
|
||||
N_("source rectangle [optional; defaults to rectangle of the SVG document]"), N_("left:top:width:height")},
|
||||
{"output", 'o', 0, G_OPTION_ARG_STRING, &output,
|
||||
N_("output filename"), NULL},
|
||||
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, N_("FILE")},
|
||||
{NULL}
|
||||
};
|
||||
GOptionEntry options_table[] = {
|
||||
{"width", 'w', 0, G_OPTION_ARG_INT, &width,
|
||||
N_("width [optional; defaults to the SVG's width]"), N_("<int>")},
|
||||
{"height", 'h', 0, G_OPTION_ARG_INT, &height,
|
||||
N_("height [optional; defaults to the SVG's height]"), N_("<int>")},
|
||||
{"source-rect", 'r', 0, G_OPTION_ARG_STRING, &source_rect_string,
|
||||
N_("source rectangle [optional; defaults to rectangle of the SVG document]"), N_("left:top:width:height")},
|
||||
{"output", 'o', 0, G_OPTION_ARG_STRING, &output,
|
||||
N_("output filename"), NULL},
|
||||
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, N_("FILE")},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
/* Set the locale so that UTF-8 filenames work */
|
||||
setlocale(LC_ALL, "");
|
||||
/* Set the locale so that UTF-8 filenames work */
|
||||
setlocale (LC_ALL, "");
|
||||
|
||||
g_option_context = g_option_context_new (_("- SVG Converter"));
|
||||
g_option_context_add_main_entries (g_option_context, options_table, NULL);
|
||||
g_option_context_set_help_enabled (g_option_context, TRUE);
|
||||
if (!g_option_context_parse (g_option_context, &argc, &argv, &error)) {
|
||||
display_error (error);
|
||||
exit (1);
|
||||
g_option_context = g_option_context_new (_("- SVG Converter"));
|
||||
g_option_context_add_main_entries (g_option_context, options_table, NULL);
|
||||
g_option_context_set_help_enabled (g_option_context, TRUE);
|
||||
if (!g_option_context_parse (g_option_context, &argc, &argv, &error))
|
||||
{
|
||||
display_error (error);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
g_option_context_free (g_option_context);
|
||||
g_option_context_free (g_option_context);
|
||||
|
||||
if (output != NULL) {
|
||||
output_file = fopen (output, "wb");
|
||||
if (!output_file) {
|
||||
fprintf (stderr, _("Error saving to file: %s\n"), output);
|
||||
exit (1);
|
||||
if (output != NULL)
|
||||
{
|
||||
output_file = fopen (output, "wb");
|
||||
if (!output_file)
|
||||
{
|
||||
fprintf (stderr, _("Error saving to file: %s\n"), output);
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
|
||||
if (args[0] != NULL) {
|
||||
filename = args[0];
|
||||
}
|
||||
|
||||
/* Parse the source rect */
|
||||
if(source_rect_string != NULL) {
|
||||
const int n = sscanf(source_rect_string, "%lg:%lg:%lg:%lg",
|
||||
&source_rect.left, &source_rect.top,
|
||||
&source_rect.width, &source_rect.height);
|
||||
if(n != 4 || source_rect.width <= 0.0 || source_rect.height < 0.0) {
|
||||
fprintf (stderr, _("Invalid source rect: %s\n"), source_rect_string);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
rsvg_init ();
|
||||
|
||||
rsvg = rsvg_handle_new_from_file (filename, &error);
|
||||
|
||||
if (!rsvg) {
|
||||
fprintf (stderr, _("Error reading SVG:"));
|
||||
display_error (error);
|
||||
fprintf (stderr, "\n");
|
||||
exit (1);
|
||||
|
||||
if (args[0] != NULL)
|
||||
filename = args[0];
|
||||
|
||||
/* Parse the source rect */
|
||||
if(source_rect_string != NULL)
|
||||
{
|
||||
const int n = sscanf(source_rect_string, "%lg:%lg:%lg:%lg",
|
||||
&source_rect.left, &source_rect.top,
|
||||
&source_rect.width, &source_rect.height);
|
||||
if (n != 4 || source_rect.width <= 0.0 || source_rect.height < 0.0)
|
||||
{
|
||||
fprintf (stderr, _("Invalid source rect: %s\n"), source_rect_string);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* if the user did not specify a source rectangle, get the page size from the SVG */
|
||||
if(source_rect_string == NULL) {
|
||||
rsvg_handle_set_size_callback (rsvg, rsvg_cairo_size_callback, &dimensions, NULL);
|
||||
source_rect.left = 0;
|
||||
source_rect.top = 0;
|
||||
source_rect.width = dimensions.width;
|
||||
source_rect.height = dimensions.height;
|
||||
rsvg_init ();
|
||||
|
||||
rsvg = rsvg_handle_new_from_file (filename, &error);
|
||||
|
||||
if (!rsvg)
|
||||
{
|
||||
fprintf (stderr, _("Error reading SVG:"));
|
||||
display_error (error);
|
||||
fprintf (stderr, "\n");
|
||||
exit (1);
|
||||
}
|
||||
|
||||
rsvg_handle_get_dimensions (rsvg, &dimensions);
|
||||
|
||||
if(width != -1 && height != -1) {
|
||||
dimensions.width = width;
|
||||
dimensions.height = height;
|
||||
} else if(source_rect_string != NULL) {
|
||||
dimensions.width = source_rect.width;
|
||||
dimensions.height = source_rect.height;
|
||||
}
|
||||
|
||||
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
||||
dimensions.width, dimensions.height);
|
||||
|
||||
cr = cairo_create (surface);
|
||||
|
||||
cairo_translate(cr, -source_rect.left, -source_rect.top);
|
||||
|
||||
if(width != -1 && height != -1 && source_rect_string != NULL) {
|
||||
cairo_scale(cr, (double)dimensions.width / (double)source_rect.width,
|
||||
(double)dimensions.height / (double)source_rect.height);
|
||||
/* if the user did not specify a source rectangle, get the page size from the SVG */
|
||||
if(source_rect_string == NULL)
|
||||
{
|
||||
rsvg_handle_set_size_callback (rsvg, rsvg_cairo_size_callback, &dimensions, NULL);
|
||||
source_rect.left = 0;
|
||||
source_rect.top = 0;
|
||||
source_rect.width = dimensions.width;
|
||||
source_rect.height = dimensions.height;
|
||||
}
|
||||
|
||||
rsvg_handle_render_cairo (rsvg, cr);
|
||||
rsvg_handle_get_dimensions (rsvg, &dimensions);
|
||||
|
||||
cairo_surface_write_to_png_stream (surface, rsvg_cairo_write_func, output_file);
|
||||
if (width != -1 && height != -1)
|
||||
{
|
||||
dimensions.width = width;
|
||||
dimensions.height = height;
|
||||
}
|
||||
else if(source_rect_string != NULL)
|
||||
{
|
||||
dimensions.width = source_rect.width;
|
||||
dimensions.height = source_rect.height;
|
||||
}
|
||||
|
||||
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
||||
dimensions.width,
|
||||
dimensions.height);
|
||||
|
||||
g_object_unref (G_OBJECT (rsvg));
|
||||
cr = cairo_create (surface);
|
||||
|
||||
cairo_translate (cr, -source_rect.left, -source_rect.top);
|
||||
|
||||
if (width != -1 && height != -1 && source_rect_string != NULL)
|
||||
{
|
||||
cairo_scale (cr,
|
||||
(double)dimensions.width / (double)source_rect.width,
|
||||
(double)dimensions.height / (double)source_rect.height);
|
||||
}
|
||||
|
||||
cairo_destroy (cr);
|
||||
cairo_surface_destroy (surface);
|
||||
rsvg_handle_render_cairo (rsvg, cr);
|
||||
|
||||
fclose (output_file);
|
||||
cairo_surface_write_to_png_stream (surface, rsvg_cairo_write_func, output_file);
|
||||
|
||||
rsvg_term ();
|
||||
g_object_unref (G_OBJECT (rsvg));
|
||||
|
||||
return 0;
|
||||
cairo_destroy (cr);
|
||||
cairo_surface_destroy (surface);
|
||||
|
||||
fclose (output_file);
|
||||
|
||||
rsvg_term ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue