<commit_before><?php

namespace PHPPdf\Formatter;

use PHPPdf\Formatter\BaseFormatter,
    PHPPdf\Glyph as Glyphs,
    PHPPdf\Document;

/**
 * Convert values of some attributes
 *
 * @todo change name *
 * @author <NAME> <<EMAIL>>
 */
class ConvertAttributesFormatter extends BaseFormatter
{
    public function format(Glyphs\Glyph $glyph, Document $document)
    {
        if(!$glyph instanceof Glyphs\Page)
        {
            $this->convertPercentageDimensions($glyph);
            $this->convertAutoMargins($glyph);
        }
        $this->convertColorAttributes($glyph);
        $this->convertFontType($glyph, $document);
    }

    private function convertPercentageDimensions(Glyphs\Glyph $glyph)
    {
        $parent = $glyph->getParent();

        $parentWidth = $parent->getWidthWithoutPaddings();
        $parentHeight = $parent->getHeightWithoutPaddings();

        $width = $glyph->getWidth();
        $height = $glyph->getHeight();

        $glyph->setWidth($this->convertFromPercentageValue($parentWidth, $width));
        $glyph->setHeight($this->convertFromPercentageValue($parentHeight, $height));
    }

    private function convertFromPercentageValue($value, $percent)
    {
        $len = strlen($percent);
        if(substr($percent, $len-1) === '%')
        {
            $percent = (double) substr($percent, 0, $len-1);

            $percent = $value*$percent / 100;
        }
        return $percent;
    }

    private function convertAutoMargins(Glyphs\Glyph $glyph)
    {
        $parent = $glyph->getParent();

        $parentWidth = $parent->getWidthWithoutPaddings();

        if($this->hasAutoMargins($glyph))
        {
            $glyph->hadAutoMargins(true);
            $width = $glyph->getWidth() === null ? $parentWidth : $glyph->getWidth();

            //adds horizontal paddings, becouse dimension formatter hasn't executed yet
            $width += $glyph->getPaddingLeft() + $glyph->getPaddingRight();
            
            $margin = ($parentWidth - $width)/2;
            $glyph->setMarginLeft($margin);
            $glyph->setMarginRight($margin);
        }
    }

    private function hasAutoMargins(Glyphs\Glyph $glyph)
    {
        $marginLeft = $glyph->getMarginLeft();
        $marginRight = $glyph->getMarginRight();

        return ($marginLeft === Glyphs\AbstractGlyph::MARGIN_AUTO && $marginRight === Glyphs\AbstractGlyph::MARGIN_AUTO);
    }

    private function convertColorAttributes(Glyphs\Glyph $glyph)
    {
        $colorAttributes = array('color');

        foreach($colorAttributes as $attribute)
        {
            $this->convertColorAttribute($glyph, $attribute);
        }
    }

    private function convertColorAttribute(Glyphs\Glyph $glyph, $attribute)
    {
        if($glyph->hasAttribute($attribute))
        {
            $color = $glyph->getAttribute($attribute);
            if(is_string($color))
            {
                $color = new \Zend_Pdf_Color_Html($color);
                $glyph->setAttribute($attribute, $color);
            }<commit_msg>ConvertAttributesFormatter refactor<commit_after><?php

namespace PHPPdf\Formatter;

use PHPPdf\Formatter\BaseFormatter,
    PHPPdf\Glyph as Glyphs,
    PHPPdf\Document;

/**
 * Convert values of some attributes
 *
 * @author <NAME> <<EMAIL>>
 */
class ConvertAttributesFormatter extends BaseFormatter
{
    public function format(Glyphs\Glyph $glyph, Document $document)
    {
        $this->convertPercentageDimensions($glyph);
        $this->convertAutoMargins($glyph);
        $this->convertColorAttributes($glyph);
        $this->convertFontType($glyph, $document);
    }

    private function convertPercentageDimensions(Glyphs\Glyph $glyph)
    {
        $parent = $glyph->getParent();

        if($parent !== null)
        {
            $parentWidth = $parent->getWidthWithoutPaddings();
            $parentHeight = $parent->getHeightWithoutPaddings();

            $width = $glyph->getWidth();
            $height = $glyph->getHeight();

            $glyph->setWidth($this->convertFromPercentageValue($parentWidth, $width));
            $glyph->setHeight($this->convertFromPercentageValue($parentHeight, $height));
        }
    }

    private function convertFromPercentageValue($value, $percent)
    {
        $len = strlen($percent);
        if(substr($percent, $len-1) === '%')
        {
            $percent = (double) substr($percent, 0, $len-1);

            $percent = $value*$percent / 100;
        }
        return $percent;
    }

    private function convertAutoMargins(Glyphs\Glyph $glyph)
    {
        $parent = $glyph->getParent();

        if($parent !== null)
        {
            $parentWidth = $parent->getWidthWithoutPaddings();

            if($this->hasAutoMargins($glyph))
            {
                $glyph->hadAutoMargins(true);
                $width = $glyph->getWidth() === null ? $parentWidth : $glyph->getWidth();

                //adds horizontal paddings, becouse dimension formatter hasn't executed yet
                $width += $glyph->getPaddingLeft() + $glyph->getPaddingRight();

                $margin = ($parentWidth - $width)/2;
                $glyph->setMarginLeft($margin);
                $glyph->setMarginRight($margin);
            }
        }
    }

    private function hasAutoMargins(Glyphs\Glyph $glyph)
    {
        $marginLeft = $glyph->getMarginLeft();
        $marginRight = $glyph->getMarginRight();

        return ($marginLeft === Glyphs\AbstractGlyph::MARGIN_AUTO && $marginRight === Glyphs\AbstractGlyph::MARGIN_AUTO);
    }

    private function convertColorAttributes(Glyphs\Glyph $glyph)
    {
        $colorAttributes = array('color');

        foreach($colorAttributes as $attribute)
        {
            $this->convertColorAttribute($glyph, $attribute);
        }
    }

    private function convertColorAttribute(Glyphs\Glyph $glyph, $attribute)
    {
        if($glyph->hasAttribute($attribute))
        {
            $color = $glyph->getAttribute($attribute);
            if(is_string($color))
            {
                $color = new \Zend_Pdf_Color_Html($color);
                $glyph->setAttribute($attribute, $color);
            }
{'fraction_non_alphanumeric': 0.13659961064243997, 'fraction_numerical': 0.0022712524334847503, 'mean_word_length': 2.467379077615298, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '130204', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1700, 'n_words': 287}