From 741797453bddb7691981bf8ff38ddb294384688b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Wed, 14 Nov 2012 22:21:18 +0100 Subject: [PATCH] MDL-36436 add simple svg hacking tool for icon designers This should solve some IE9 issues with icon cut-off. --- theme/base/cli/svgtool.php | 165 +++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 theme/base/cli/svgtool.php diff --git a/theme/base/cli/svgtool.php b/theme/base/cli/svgtool.php new file mode 100644 index 00000000000..a40f8cd2a18 --- /dev/null +++ b/theme/base/cli/svgtool.php @@ -0,0 +1,165 @@ +. + +/** + * This script implements some useful svg manipulation tricks. + * + * @package theme_base + * @subpackage cli + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define('CLI_SCRIPT', true); + +require(__DIR__.'/../../../config.php'); +require_once($CFG->libdir.'/clilib.php'); + +// Now get cli options. +list($options, $unrecognized) = cli_get_params(array('help'=>false, 'ie9fix'=>false, 'noaspectratio'=>false, 'path'=>$CFG->dirroot), + array('h'=>'help')); + +if ($unrecognized) { + $unrecognized = implode("\n ", $unrecognized); + cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); +} + +// If necessary add files that should be ignored - such as in 3rd party plugins. +$blacklist = array(); +$path = $options['path']; +if (!file_exists($path)) { + cli_error("Invalid path $path"); +} + +if ($options['ie9fix']) { + theme_base_recurse_svgs($path, '', 'theme_base_svgtool_ie9fix', $blacklist); + +} else if ($options['noaspectratio']) { + theme_base_recurse_svgs($path, '', 'theme_base_svgtool_noaspectratio', $blacklist); + +} else { + $help = + "Some svg image tweaks for icon designers. + +Options: + +-h, --help Print out this help +--ie9fix Adds preserveAspectRatio=\"xMinYMid meet\" to every svg image +--noaspectratio Removes preserveAspectRatio from svg files +--path=PATH Path to directory or file to be converted, by default \$CFG->dirroot + +Examples: +\$ php svgtool.php --ie9fix +\$ php svgtool.php --ie9fix --path=../../../pix +\$ php svgtool.php --noaspectratio +"; + + echo $help; + die; +} + +exit(0); + + +function theme_base_svgtool_ie9fix($file) { + global $CFG; + + if (strpos($file, $CFG->dirroot.DIRECTORY_SEPARATOR) === 0) { + $relfile = substr($file, strlen($CFG->dirroot)); + } else { + $relfile = $file; + } + + $content = file_get_contents($file); + + if (!preg_match('/]*>/', $content, $matches)) { + echo " skipping $relfile (invalid format)\n"; + return; + } + $svg = $matches[0]; + if (strpos($svg, 'preserveAspectRatio') !== false) { + return; + } + + if (!is_writable($file)) { + echo " skipping $relfile (can not modify file)\n"; + return; + } + + $newsvg = rtrim($svg, '>').' preserveAspectRatio="xMinYMid meet">'; + + $content = str_replace($svg, $newsvg, $content); + echo "converting $relfile\n"; + file_put_contents($file, $content); +} + +function theme_base_svgtool_noaspectratio($file) { + global $CFG; + + if (strpos($file, $CFG->dirroot.DIRECTORY_SEPARATOR) === 0) { + $relfile = substr($file, strlen($CFG->dirroot)); + } else { + $relfile = $file; + } + + $content = file_get_contents($file); + + if (!preg_match('/]*>/', $content, $matches)) { + echo " skipping $relfile (invalid format)\n"; + return; + } + $svg = $matches[0]; + if (strpos($svg, 'preserveAspectRatio="xMinYMid meet"') === false) { + return; + } + + if (!is_writable($file)) { + echo " skipping $relfile (can not modify file)\n"; + return; + } + + $newsvg = preg_replace('/ ?preserveAspectRatio="xMinYMid meet"/', '', $svg); + + $content = str_replace($svg, $newsvg, $content); + echo "resetting $relfile\n"; + file_put_contents($file, $content); +} + +function theme_base_recurse_svgs($base, $sub, $filecallback, $blacklist) { + if (is_dir("$base/$sub")) { + $items = new DirectoryIterator("$base/$sub"); + foreach ($items as $item) { + if ($item->isDot()) { + continue; + } + $file = $item->getFilename(); + theme_base_recurse_svgs("$base/$sub", $file, $filecallback, $blacklist); + } + unset($item); + unset($items); + return; + + } else if (is_file("$base/$sub")) { + if (substr($sub, -4) !== '.svg') { + return; + } + $file = realpath("$base/$sub"); + if (in_array($file, $blacklist)) { + return; + } + $filecallback($file); + } +}