<?php
    class PrinterUtilities{
        function getDotsPosition($mmX, $mmY, $dpiX, $dpiY, $marginX = 0, $marginY = 0){
            $mmInch =  0.0393701;
            return array(
                'X'=>   round((($mmX - $marginX) * $mmInch) * $dpiX), 	
                'Y' =>  round((($mmY - $marginY) * $mmInch) * $dpiY)
            );
        }
        /** convert milimeter to dots*/
        function mmToDots($mm, $dpi){
            $mmInch =  0.0393701;
            return  round(($mm * $mmInch) * $dpi);
        }
        /** */
        function sortProducts($products, $maxWidthDesc){
            $lines = array();
            if($products && count($products)){
                foreach($products as $product){
                    $desc = $product->desc;
                    $descArray = explode(' ',$desc);
                    $descLines = array();
                    $descLinesCounter = 0;
                    for($i = 0; $i < count($descArray); $i++){
                        if($i == 0){
                            $descLines[$descLinesCounter] = $descArray[$i];
                        }else{
                            if(strlen($descLines[$descLinesCounter]." ".$descArray[$i]) > $maxWidthDesc){
                                $descLinesCounter ++;
                                $descLines[$descLinesCounter] = $descArray[$i];
                            }else{
                                $descLines[$descLinesCounter] .= " ".$descArray[$i];
                            }
                        }
                    }
                    /*echo "<pre>";
                    print_r($descLines);
                    echo "</pre>";*/
                    /** */
                    for($ii = 0; $ii < count($descLines); $ii++){
                        if($ii == 0){
                            $lines[] = (object) array(
                                "cant" => $product->cant,
                                "ref" => (isset($product->ref) && $product->ref ? $product->ref : null),
                                "desc" => $descLines[$ii],
                                "precio" => $product->costo,
                                "total" => $product->costo * $product->cant,
                            );
                        }else{
                            $lines[] = (object) array(
                                "cant" => null,
                                "ref" => null,
                                "desc" => $descLines[$ii],
                                "precio" =>null,
                                "total" => null,
                            );
                        }
                    }
                }
            }
            return (object) $lines;
        }
        /** center text*/
        function centertext($text, $paperWidth, $fontHeight, $xPosAjustmen = 0){ /**Mesures in MM */
          

            $textArray = explode(' ', $text);
            $textLines = array();
            $textLinesCounter = 0;
            for($i = 0; $i < count($textArray); $i++){
                if($i == 0)
                {
                    $textLines[$textLinesCounter]['text'] = $textArray[$i];
                }
                else
                {
                    if( $this->getStrMMLength($textLines[$textLinesCounter]['text']." ".$textArray[$i], $fontHeight) > $paperWidth)
                    {
                        $textLinesCounter ++;
                        $textLines[$textLinesCounter]['text'] = $textArray[$i];
                    }
                    else
                    {
                        $textLines[$textLinesCounter]['text'] .= " ".$textArray[$i]; 
                    }
                }
                $textLines[$textLinesCounter]['xPos'] = (($paperWidth - $this->getStrMMLength($textLines[$textLinesCounter]['text'], $fontHeight)) / 2) + $xPosAjustmen;
            }
            return (object) $textLines;
        }
        /** */
        private function getStrMMLength($string, $fontHeight){
            $fontWidth = $fontHeight * 0.60;
            $fontCapitalWidth = $fontHeight * 0.75;

            $capital = preg_match_all("/[A-Z]/", $string);
            $lower = (strlen($string) - $capital);

            return ($capital * $fontCapitalWidth) + ($lower * $fontWidth);
        }
    }
?>