<?php
namespace Pdf\Lib;

use Dever;
Dever::apply('fpdf/chinese', 'pdf');
Dever::apply('tcpdf/tcpdf', 'pdf');

class Core
{
    public function init($page = 'P', $size = 'mm', $type = 'A4')
    {
        $this->pdf = new Pdf($page, $size, $type);
        $this->pdf->SetCreator('dever'); 
        $this->pdf->SetAuthor('dever'); 
        $this->pdf->SetFont('stsongstdlight', '', 10);
        $this->pdf->setHeaderFont(Array('stsongstdlight', '', '10')); 
        $this->pdf->setFooterFont(Array('stsongstdlight', '', '8'));  
        # 删除预定义的打印 页眉/页尾
        $this->pdf->setPrintHeader(false);
        $this->pdf->setPrintFooter(false);
        $this->pdf->AddPage();

        return $this->pdf;
    }
}

class Pdf extends \TCPDF
{
    public function __construct($name, $size, $page)
    {
        parent::__construct($name, $size, $page, true, 'UTF-8', false);
    }

    /*
    font:表示字体;
    style:可选参数,表示样式,默认为普通样式;
        取值:B:粗体     I:斜体        U:下划线
    size:可选参数,表示字体大小,默认为12pt;
    */
    public function font($size, $style = '', $font = '')
    {
        $this->SetFont($font, $style, $size);
        return $this;
    }

    # 段落
    public function content($name, $content, $size = 15)
    {
        if ($name) {
            $this->br(1, 1);
            $this->font($size, 'B');
            $this->left($name, 100);
            $this->br();
        }
        
        $this->font($size, '');
        $this->mleft($content);

        return $this;
    }

     /*设置单行单元格:
       W:设置单元格的宽
       H:设置单元格的高
       Text:单元格文本
       Border:设置单元格的边框。0,无边框,1,一个框,L,左边框,R,右边框,B, 底边框,T,顶边框,LTRB指四个边都显示
       Ln:0,单元格后的内容插到表格右边或左边,1,单元格的下一行,2,在单元格下面
       Align:文本位置。L,左对齐,R,右对齐,C,居中,J,自动对齐
       Fill:填充。false,单元格的背景为透明,true,单元格必需被填充
       Link:设置单元格文本的链接。
       $pdf->Cell(0, 10, 'test', 1, 1, 'C');
       */
    public function add($text, $align = 'L', $width = 0, $height = 8, $border = 0, $ln = 0, $link = '', $fill = false)
    {
        if (!$height) {
            $height = 8;
        }
        if (!$width) {
            $this->br();
        }
        $this->Cell($width, $height, $text, $border, $ln, $align, $fill, $link);
        return $this;
    }

    public function left($text, $width = 0, $border = 0, $height = 8)
    {
        $this->add($text, 'L', $width, $height, $border);
        return $this;
    }

    public function right($text, $width = 0, $border = 0, $height = 8)
    {
        $this->add($text, 'R', $width, $height, $border);
        return $this;
    }

    public function center($text, $width = 0, $border = 0, $height = 8)
    {
        $this->add($text, 'C', $width, $height, $border);
        return $this;
    }

     /*设置多行单元格。注意跟Cell的参数位置有些差别,Cell是用来输出单行文本的,MultiCell就能用来输出多行文本
       W:设置多行单元格的宽
       H: 设置多行单元格的单行的高
       Text:文本
       Border:边框
       Align:文本位置
       Fill:填充
       Ln:0,单元格后的内容插到表格右边或左边,1,单元格的下一行,2,在单元格下面
       X:设置多行单元格的行坐标
       Y:设置多行单元格的纵坐标
       Reseth:true,重新设置最后一行的高度
       Stretch:调整文本宽度适应单元格的宽度
       Ishtml:true,可以输出html文本,有时很有用的
       Autopadding:true,自动调整文本与单元格之间的距离
       Maxh:设置单元格最大的高度
       Valign:设置文本在纵坐标中的位置,T,偏上,M,居中,B,偏下
       Fillcell:自动调整文本字体大小来适应单元格大小。 

       $pdf->MultiCell($w, $h, $txt, $border=0, $align='J',$fill=false, $ln=1, $x='', $y='',  $reseth=true, $stretch=0,$ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false);
       */
    public function madd($text, $align = 'L', $width = 0, $height = 8, $border = 0, $ln = 1, $fill = false, $x = '', $y = '', $reseth = true, $stretch = 0,$ishtml = false, $autopadding = false, $maxh = 0, $valign = 'T', $fitcell = false)
    {
        if (!$height) {
            $height = 8;
        }
        if (!$width) {
            //$this->br();
        }
        $this->MultiCell($width, $height, $text, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, $ishtml, $autopadding, $maxh, $valign, $fitcell);
        return $this;
    }

    public function mleft($text, $width = 0, $border = 0, $height = 8)
    {
        $this->madd($text, 'L', $width, $height, $border);
        return $this;
    }

    public function mright($text, $width = 0, $border = 0, $height = 8)
    {
        $this->madd($text, 'R', $width, $height, $border);
        return $this;
    }

    public function mcenter($text, $width = 0, $border = 0, $height = 8)
    {
        $this->madd($text, 'C', $width, $height, $border);
        return $this;
    }

    public function img($file, $x, $y = false, $w = 30, $h = 30)
    {
        if (!$y) {
            $y = $this->y();
        }
        $this->Image(Dever::pic($file), $x, $y, $w, $h);
        return $this;
    }

    public function hr($str = '-', $name = '')
    {
        $text = str_pad($name, 142, $str, STR_PAD_BOTH);
        $this->add($text);

        return $this;
    }

    public function br($num = 1, $size = null)
    {
        for ($i = 0; $i < $num; $i++) {
            $this->Ln($size);
        }
        
        return $this;
    }

    public function y()
    {
        return $this->GetY();
    }

    public function x()
    {
        return $this->GetX();
    }

    public function convert($string)
    {
        return Dever::convert($string);
    }

    /*
    name:可选参数,表示要储存的文件名。
    dest:可选参数,操作内容。
        取值:
        I,默认值,在浏览器中打开;D,点击下载按钮, PDF文件会被下载下来;F,文件会被保存在服务器中;S,PDF会以字符串形式输出;E:PDF以邮件的附件输出
    */
    public function out($name, $dest = 'I')
    {
        $this->Output($name, $dest);
    }
}