From 2ec3f28f98391ee3a1844dd5999053e1e3976d40 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Fri, 1 Jul 2011 01:26:27 +0200 Subject: switching to rsvg, add drop shadows --- index.php | 36 +++++---- qsvg.cc | 59 --------------- sampleicon.png | Bin 0 -> 6614 bytes sampleicon.svg | 72 ++++++++++++++++++ samplesrc.svg | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ template.svg | 26 +++++-- 6 files changed, 348 insertions(+), 79 deletions(-) delete mode 100644 qsvg.cc create mode 100644 sampleicon.png create mode 100644 sampleicon.svg create mode 100644 samplesrc.svg diff --git a/index.php b/index.php index 79a4428..a57606d 100644 --- a/index.php +++ b/index.php @@ -22,13 +22,14 @@ define('SVGNS', 'http://www.w3.org/2000/svg'); define('COLOR_EXTRACT_RENDER_SIZE', 128); define('HARMATTAN_ICON_SIZE', 80); -define('HARMATTAN_FOCAL_SIZE', 54); +define('HARMATTAN_FOCAL_SIZE', 56); function svg2png($svg, $w, $h, $png) { $output = array(); $exitcode = -1; - exec(sprintf("~/bin/qsvg %s %d %d %s", escapeshellarg($svg), $w, $h, escapeshellarg($png)),$output,$exitcode); + $cmd = sprintf("~/bin/rsvg-convert --format=png --width=%d --height=%d --output=%s %s", $w, $h, escapeshellarg($png), escapeshellarg($svg)); + exec($cmd, $output, $exitcode); return $exitcode == 0; } @@ -117,7 +118,7 @@ $colfilters = array('none', 'pure', 'grey', 'grey5'); function extract_dominant_color($svg, $algo = 'average', $filter = 'grey') { global $domalgos, $colfilters; - $png = '/tmp/hicg-icon.png'; + $png = '/tmp/hicg-icon-' . uniqid() . '.png'; if (!in_array($algo, $domalgos)) { return false; } @@ -133,6 +134,7 @@ function extract_dominant_color($svg, $algo = 'average', $filter = 'grey') } $color = call_user_func('domcolor_'. $algo, $img, 'colfilter_'.$filter); imagedestroy($img); + unlink($png); return $color; } @@ -217,12 +219,14 @@ function calc_scale_ratios($w, $h, &$s, &$tx, &$ty) $ty = HARMATTAN_ICON_SIZE / 2 - $sh / 2; } -function make_icon($src, $template, $dst, $color) +function make_icon($src, $template, $dst, $color, $shadow) { $srcDoc = new DOMDocument(); $srcDoc->load($src); $doc = new DOMDocument(); $doc->load($template); + $xpath = new DOMXPath($doc); + $xpath->registerNamespace('svg', SVGNS); /* Get scaling ratios from source SVG. */ $srcNode = $srcDoc->documentElement; @@ -230,15 +234,19 @@ function make_icon($src, $template, $dst, $color) $h = intval(get_svg_attribute($srcNode, 'height')); calc_scale_ratios($w, $h, $s, $tx, $ty); - /* Fix gradient colors. */ - $grd = $doc->getElementsByTagName('stop'); - $grd->item(0)->setAttribute('style', 'stop-color:#' . color2hex(get_icon_light_color($color)) . ';stop-opacity:1'); - $grd->item(1)->setAttribute('style', 'stop-color:#' . color2hex(get_icon_dark_color($color)) . ';stop-opacity:1'); + /* Set background color */ + $bkg = $xpath->query('/svg:svg/svg:g/svg:path[@id="hicg_background"]'); + $bkg->item(0)->setAttribute('fill', '#' . color2hex($color)); /* Create the scaling & centering transform. */ $g = $doc->createElement('g'); $g->setAttribute('transform', "translate($tx, $ty) scale($s)"); + /* Add drop shadow filter if selected. */ + if ($shadow) { + $g->setAttribute('filter', 'hicg_drop_shadow'); + } + foreach ($srcNode->childNodes as $node) { $n = $doc->importNode($node, TRUE); $g->appendChild($n); @@ -266,6 +274,7 @@ if (@isset($_POST['send'])) { } $domalgo = $_POST['domalgo']; $colfilter = $_POST['colfilter']; + $dropshadow = $_POST['dropshadow'] == 'dropshadow'; $iconfile = $prefix . 'icon.svg'; $pngfile = $prefix . 'icon.png'; @@ -280,7 +289,7 @@ if (@isset($_POST['send'])) { $prscolor = get_pressed_color($domcolor); } - make_icon($srcfile, 'template.svg', $iconfile, $domcolor); + make_icon($srcfile, 'template.svg', $iconfile, $domcolor, $dropshadow); make_png($iconfile, $pngfile); $srcurl = $srcfile; @@ -290,6 +299,7 @@ if (@isset($_POST['send'])) { /* Load some sample data. */ $srcfile = 'samplesrc.svg'; $domalgo = 'mode'; + $dropshadow = true; $colfilter = 'grey5'; $iconfile = 'sampleicon.svg'; $pngfile = 'sampleicon.png'; @@ -299,7 +309,7 @@ if (@isset($_POST['send'])) { $prscolor = get_pressed_color($domcolor); /* This should not get into production. ;) */ - make_icon($srcfile, 'template.svg', $iconfile, $domcolor); + make_icon($srcfile, 'template.svg', $iconfile, $domcolor, $dropshadow); make_png($iconfile, $pngfile); $srcurl = $srcfile; @@ -345,8 +355,7 @@ echo '';
Drop shadow - - +
@@ -379,12 +388,13 @@ echo '';
-

Download SVG file. Or a 80x80 PNG file.

+

Download the prerendered 80x80 PNG file. Or a SVG file, if you want to make more changes.

Suggested reading

diff --git a/qsvg.cc b/qsvg.cc deleted file mode 100644 index 2eb737e..0000000 --- a/qsvg.cc +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include -#include -#include - -/* - hicg -- Harmattan Icon/Color Generator - Copyright (C) 2011 Javier S. Pedro - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -using std::cerr; -using std::endl; - -int main(int argc, char *argv[]) -{ - QCoreApplication a(argc, argv); - QStringList args = a.arguments(); - if (args.size() != 5) { - cerr << "usage: qsvg " << endl; - return 1; - } - - QSvgRenderer r(args[1]); - - if (!r.isValid()) { - cerr << "Could not open source file." << endl; - return 1; - } - - int w, h; - w = args[2].toInt(); - h = args[3].toInt(); - - QImage i(w, h, QImage::Format_ARGB32); - i.fill(QColor(Qt::transparent).rgba()); - - QPainter p(&i); - r.render(&p); - - if (!i.save(args[4], "png")) { - cerr << "Could not save to destination file." << endl; - } - - return 0; -} diff --git a/sampleicon.png b/sampleicon.png new file mode 100644 index 0000000..4ee554c Binary files /dev/null and b/sampleicon.png differ diff --git a/sampleicon.svg b/sampleicon.svg new file mode 100644 index 0000000..b2a05da --- /dev/null +++ b/sampleicon.svg @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samplesrc.svg b/samplesrc.svg new file mode 100644 index 0000000..e7f9556 --- /dev/null +++ b/samplesrc.svg @@ -0,0 +1,234 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/template.svg b/template.svg index a9f3ebc..5bef241 100644 --- a/template.svg +++ b/template.svg @@ -1,12 +1,24 @@ - - + + - - - + + + + + + + + + - - + + + + + + -- cgit v1.2.3