<?php
namespace Veno\QrCdr\Lib;

/**
 * QR Code Vector Output Class (Private Fork for Veno QrCdr)
 *
 * Handles generation of SVG vector outputs.
 * EPS logic has been stripped as it is unused in this plugin.
 *
 * @package   QrCdr
 * @version   1.1.4-patched
 */

if ( ! defined( 'ABSPATH' ) && ! defined( 'QRCDR_OPEN_ACCESS' ) ) {
	exit;
}

if ( ! defined( 'QRCDR_VECT' ) ) {
	define( 'QRCDR_VECT', true );
}

class QrcdrVect {

	/**
	 * Output SVG.
	 *
	 * @param array       $frame           QR data frame.
	 * @param string|bool $filename        Output filename.
	 * @param int         $pixel_per_point Pixel size.
	 * @param int         $outer_frame     Margin.
	 * @param bool        $save_and_print  Save and output to browser.
	 * @param int         $back_color      Background hex.
	 * @param int         $fore_color      Foreground hex.
	 */
	public static function svg( $frame, $filename = false, $pixel_per_point = 4, $outer_frame = 4, $save_and_print = false, $back_color = 0xFFFFFF, $fore_color = 0x000000 ) {
		return self::vectSVG( $frame, $pixel_per_point, $outer_frame, $back_color, $fore_color );
	}

	/**
	 * Generate SVG data string.
	 * Protected so it can be overridden by child classes if needed.
	 *
	 * @param array $frame           QR data frame.
	 * @param int   $pixel_per_point Pixel size.
	 * @param int   $outer_frame     Margin.
	 * @param int   $back_color      Background hex.
	 * @param int   $fore_color      Foreground hex.
	 * @return string SVG XML.
	 */
	protected static function vectSVG( $frame, $pixel_per_point = 4, $outer_frame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000 ) {
		$height = count( $frame );
		$width  = strlen( $frame[0] );

		$img_width  = $width + 2 * $outer_frame;
		$img_height = $height + 2 * $outer_frame;

		$total_width  = $img_width * $pixel_per_point;
		$total_height = $img_height * $pixel_per_point;

		$output =
			'<?xml version="1.0" encoding="utf-8"?>' . "\n" .
			'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">' . "\n" .
			'<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" xmlns:xlink="http://www.w3.org/1999/xlink" width="' . $total_width . '" height="' . $total_height . '" viewBox="0 0 ' . $total_width . ' ' . $total_height . '">' . "\n" .
			'<desc>QR Code generated by QrCdr</desc>' . "\n";

		if ( ! empty( $back_color ) ) {
			$background_color = str_pad( dechex( $back_color ), 6, '0', STR_PAD_LEFT );
			$output .= '<rect width="' . $total_width . '" height="' . $total_height . '" fill="#' . $background_color . '" cx="0" cy="0" />' . "\n";
		}

		$output .=
			'<defs>' . "\n" .
			'<rect id="p" width="' . $pixel_per_point . '" height="' . $pixel_per_point . '" />' . "\n" .
			'</defs>' . "\n" .
			'<g fill="#' . str_pad( dechex( $fore_color ), 6, '0', STR_PAD_LEFT ) . '">' . "\n";

		for ( $i = 0; $i < $height; $i++ ) {
			for ( $j = 0; $j < $width; $j++ ) {
				if ( '1' == $frame[ $i ][ $j ] ) {
					$y = ( $i + $outer_frame ) * $pixel_per_point;
					$x = ( $j + $outer_frame ) * $pixel_per_point;
					$output .= '<use x="' . $x . '" y="' . $y . '" xlink:href="#p" />' . "\n";
				}
			}
		}

		$output .=
			'</g>' . "\n" .
			'</svg>';

		return $output;
	}
}
