<?php
namespace Pdf\Lib;

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

class Base
{
    public function init($page = 'P', $size = 'mm', $type = 'A4')
    {
        $this->pdf = new Pdf($page, $size, $type);
        $this->pdf->AddGBFont();
        $this->pdf->Open();
        $this->pdf->AddPage();
        $this->pdf->font(10);

        return $this->pdf;
    }
}

class Pdf extends \PDF_Chinese
{
    public function __construct($name, $size, $page)
    {
        parent::__construct($name, $size, $page);
    }

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

    /*
    width:增加单元格宽度。
    height:增加单元格高度。
    text:放置在单元格中的文本。
    border:单元格边框。
    ln:换行高度,默认为0,即换一行。
    align:对齐方式,默认居左,R时居右,C时居中。
    fill:是否颜色填充,默认false。
    link:添加链接,默认无链接.

    * Cell()函数是FPDF中输出文字的主要方式之一。
    */
    public function text($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,  $this->convert($text), $border, $ln, $align, $fill, $link);
        return $this;
    }

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

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

    public function center($text, $width = 0, $border = 0, $height = 8)
    {
        $this->text($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->text($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:将PDF文档直接在浏览器中显示。
        D:下载PDF文档。
        F:保存为本地文件。
        S:返回一个字符串值。
    */
    public function out($name, $dest = 'I')
    {
        $this->Output($this->convert($name), $dest);
    }
}