Пример #1
0
 /**
  * this is not a real TAG, it is just to write texts
  *
  * @param  array $param
  * @return boolean
  */
 protected function _tag_open_WRITE($param)
 {
     $fill = $this->parsingCss->value['background']['color'] !== null && $this->parsingCss->value['background']['image'] === null;
     if (in_array($this->parsingCss->value['id_tag'], array('fieldset', 'legend', 'div', 'table', 'tr', 'td', 'th'))) {
         $fill = false;
     }
     // get the text to write
     $txt = $param['txt'];
     if ($this->_isAfterFloat) {
         $txt = ltrim($txt);
         $this->_isAfterFloat = false;
     }
     $txt = str_replace('[[page_nb]]', $this->pdf->getMyAliasNbPages(), $txt);
     $txt = str_replace('[[page_cu]]', $this->pdf->getMyNumPage($this->_page), $txt);
     if ($this->parsingCss->value['text-transform'] != 'none') {
         if ($this->parsingCss->value['text-transform'] == 'capitalize') {
             $txt = ucwords($txt);
         } else {
             if ($this->parsingCss->value['text-transform'] == 'uppercase') {
                 $txt = strtoupper($txt);
             } else {
                 if ($this->parsingCss->value['text-transform'] == 'lowercase') {
                     $txt = strtolower($txt);
                 }
             }
         }
     }
     // size of the text
     $h = 1.08 * $this->parsingCss->value['font-size'];
     $dh = $h * $this->parsingCss->value['mini-decal'];
     $lh = $this->parsingCss->getLineHeight();
     // identify the align
     $align = 'L';
     if ($this->parsingCss->value['text-align'] == 'li_right') {
         $w = $this->parsingCss->value['width'];
         $align = 'R';
     }
     // calculate the width of each words, and of all the sentence
     $w = 0;
     $words = explode(' ', $txt);
     foreach ($words as $k => $word) {
         $words[$k] = array($word, $this->pdf->GetStringWidth($word));
         $w += $words[$k][1];
     }
     $space = $this->pdf->GetStringWidth(' ');
     $w += $space * (count($words) - 1);
     // position in the text
     $currPos = 0;
     // the bigger width of the text, after automatic break line
     $maxX = 0;
     // position of the text
     $x = $this->pdf->getX();
     $y = $this->pdf->getY();
     $dy = $this->_getElementY($lh);
     // margins
     list($left, $right) = $this->_getMargins($y);
     // number of lines after automatic break line
     $nb = 0;
     // while we have words, and the text does not fit on the line => we cut the sentence
     while ($x + $w > $right && $x < $right + $space && count($words)) {
         // adding words 1 by 1 to fit on the line
         $i = 0;
         $old = array('', 0);
         $str = $words[0];
         $add = false;
         while ($x + $str[1] < $right) {
             $i++;
             $add = true;
             array_shift($words);
             $old = $str;
             if (!count($words)) {
                 break;
             }
             $str[0] .= ' ' . $words[0][0];
             $str[1] += $space + $words[0][1];
         }
         $str = $old;
         // if  nothing fit on the line, and if the first word does not fit on the line => the word is too long, we put it
         if ($i == 0 && $left + $words[0][1] >= $right) {
             $str = $words[0];
             array_shift($words);
             $i++;
             $add = true;
         }
         $currPos += ($currPos ? 1 : 0) + strlen($str[0]);
         // write the extract sentence that fit on the page
         $wc = $align == 'L' ? $str[1] : $this->parsingCss->value['width'];
         if ($right - $left < $wc) {
             $wc = $right - $left;
         }
         if (strlen($str[0])) {
             $this->pdf->setXY($this->pdf->getX(), $y + $dh + $dy);
             $this->pdf->Cell($wc, $h, $str[0], 0, 0, $align, $fill, $this->_isInLink);
             $this->pdf->setXY($this->pdf->getX(), $y);
         }
         $this->_maxH = max($this->_maxH, $lh);
         // max width
         $maxX = max($maxX, $this->pdf->getX());
         // new position and new width for the "while"
         $w -= $str[1];
         $y = $this->pdf->getY();
         $x = $this->pdf->getX();
         $dy = $this->_getElementY($lh);
         // if we have again words to write
         if (count($words)) {
             // remove the space at the end
             if ($add) {
                 $w -= $space;
             }
             // if we don't add any word, and if the first word is empty => useless space to skip
             if (!$add && $words[0][0] === '') {
                 array_shift($words);
             }
             // if it is just to calculate for one line => adding the number of words
             if ($this->_isForOneLine) {
                 $this->_maxE += $i;
                 $this->_maxX = max($this->_maxX, $maxX);
                 return null;
             }
             // automatic line break
             $this->_tag_open_BR(array('style' => ''), $currPos);
             // new position
             $y = $this->pdf->getY();
             $x = $this->pdf->getX();
             $dy = $this->_getElementY($lh);
             // if the next line does  not fit on the page => new page
             if ($y + $h >= $this->pdf->getH() - $this->pdf->getbMargin()) {
                 if (!$this->_isInOverflow && !$this->_isInFooter) {
                     $this->_setNewPage(null, '', null, $currPos);
                     $y = $this->pdf->getY();
                     $x = $this->pdf->getX();
                     $dy = $this->_getElementY($lh);
                 }
             }
             // if more than 10000 line => error
             $nb++;
             if ($nb > 10000) {
                 $txt = '';
                 foreach ($words as $k => $word) {
                     $txt .= ($k ? ' ' : '') . $word[0];
                 }
                 throw new HTML2PDF_exception(2, array($txt, $right - $left, $w));
             }
             // new margins for the new line
             list($left, $right) = $this->_getMargins($y);
         }
     }
     // if we have words after automatic cut, it is because they fit on the line => we write the text
     if (count($words)) {
         $txt = '';
         foreach ($words as $k => $word) {
             $txt .= ($k ? ' ' : '') . $word[0];
         }
         $w += $this->pdf->getWordSpacing() * count($words);
         $this->pdf->setXY($this->pdf->getX(), $y + $dh + $dy);
         $this->pdf->Cell($align == 'L' ? $w : $this->parsingCss->value['width'], $h, $txt, 0, 0, $align, $fill, $this->_isInLink);
         $this->pdf->setXY($this->pdf->getX(), $y);
         $this->_maxH = max($this->_maxH, $lh);
         $this->_maxE += count($words);
     }
     $maxX = max($maxX, $this->pdf->getX());
     $maxY = $this->pdf->getY() + $h;
     $this->_maxX = max($this->_maxX, $maxX);
     $this->_maxY = max($this->_maxY, $maxY);
     return true;
 }