<?php
/**
 * svg Image Plugin
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     i-net software <tools@inetsoftware.de>
 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');

class helper_plugin_svgimg extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin

	var $database = null;
	var $dbType = '';

    function getInfo(){
        return array_merge(confToHash(dirname(__FILE__).'/info.txt'), array(
				'name' => 'svg Image Helper Plugin',
		));
    }

    function getMethods() {
        $result = array();
		return $result;
	}

	function create_svg_image($data, $ext, $cacheFile, $progType=null) {
			global $conf;

			$cmd = '';
			$width = intval($data['width']);
			$height = intval($data['height']);

			if ( empty($progType) ) $progType = $this->getConf('use_programm');

			switch ( $progType ) {
				case 'inkscape': 

							$width = empty( $width ) ? '' : ' -w' . intval($width);
							$height = empty( $height ) ? '' : ' -h' . intval($height);
						
							$cmd =	sprintf("%s %s -z%s%s --export-%s=%s",
									'inkscape',
									escapeshellarg($data['file']),
									$width,
									$height,
									$ext,
									escapeshellarg($cacheFile));
				break;
				case 'imagemagick':

							$param = '';
							$cmd = '';
							if ( !empty($width) ) $param .= intval($width);
							if ( !empty($width) && !empty($data['height']) ) $param .= 'x';
							if ( !empty($height) ) $param .= intval($height);
							if ( !empty($param) ) $cmd = " -resize " . $param;
		
							if ( !empty( $data['border'] )) { $cmd .= ' -border ' . intval($data['border']); }
							if ( !empty( $data['bordercolor'] )) { $cmd .= ' -bordercolor "' . escapeshellarg($data['bordercolor']) . '"'; }
							$cmd .= " -strip -coalesce -quality " . intval($conf['jpg_quality']);
							$cmd =	sprintf("{$conf['im_convert']} %s %s %s >/dev/null 2>&1",
									$cmd,
									escapeshellarg($data['file']),
									escapeshellarg($cacheFile));
				break;
				case 'rsvg':
				
					$width = empty( $width ) ? '' : ' -w ' . intval($width);
					$height = empty( $height ) ? '' : ' -h ' . intval($height);

					$cmd =	sprintf("rsvg %s %s --format=%s %s %s",
							$width,
							$height,
							$ext,
							escapeshellarg($data['file']),
							escapeshellarg($cacheFile));
					break;
			}
			
			// Alte Datei löschen, bevor neue kommt
			@unlink(escapeshellarg($cacheFile));
			$last_line = system($cmd, $retval);
			if ( @file_exists($cacheFile)) return true;
			return false;
	}

	// Return a cache File Name
	// $data has:
	// width, height, background
	function _get_cache_file($data, $ext=null) {
	
		if ( empty( $data['file'] ) ) return null;
		$param = ".media.svgimg";
		if ( !empty($data['width']) || !empty($data['height']) ) $param .= '.';
		if ( !empty($data['width']) ) $param .= intval($data['width']);
		if ( !empty($data['width']) && !empty($data['height']) ) $param .= 'x';
		if ( !empty($data['height']) ) $param .= intval($data['height']);
		if ( !empty($data['background']) ) $param .= '.' . ( ! is_array($data['background']) ? $data['background'] : $data['background']['orig'] );
		if ( empty($ext) ) $ext = $this->getConf('ext');
		$param .= '.' . $ext;
		
		return getCacheName($data['file'],$param);
	}
	
	// get RGB from HEX Color
	function _calc_bgcolor( $bgcolor ) {
	
		if ( empty($bgcolor) ) { $bgcolor = $this->getConf('bgc'); }

		//	Does it start with a hash? If so then strip it
		$bgcolor = str_replace('#', '', $bgcolor);

		switch (strlen($bgcolor))
		{
			case 6:
				$red = hexdec(substr($bgcolor, 0, 2));
				$green = hexdec(substr($bgcolor, 2, 2));
				$blue = hexdec(substr($bgcolor, 4, 2));
				break;
				
			case 3:
				$red = substr($bgcolor, 0, 1);
				$green = substr($bgcolor, 1, 1);
				$blue = substr($bgcolor, 2, 1);
				$red = hexdec($red . $red);
				$green = hexdec($green . $green);
				$blue = hexdec($blue . $blue);
				break;
				
			default:
				//	Wrong values passed, default to black
				$red = 0;
				$green = 0;
				$blue = 0;
		}

		return array('red' => $red, 'green' => $green, 'blue' => $blue, 'orig' => $bgcolor );
	}
}
  
//Setup VIM: ex: et ts=4 enc=utf-8 :