| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package dompdf |
| 4 |
* @link https://github.com/dompdf/dompdf |
| 5 |
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 6 |
*/ |
| 7 |
namespace Dompdf\FrameDecorator; |
| 8 |
|
| 9 |
use Dompdf\Dompdf; |
| 10 |
use Dompdf\Frame; |
| 11 |
use Dompdf\Exception; |
| 12 |
|
| 13 |
/** |
| 14 |
* Decorates Frame objects for text layout |
| 15 |
* |
| 16 |
* @package dompdf |
| 17 |
*/ |
| 18 |
class Text extends AbstractFrameDecorator |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @var float |
| 22 |
*/ |
| 23 |
protected $text_spacing; |
| 24 |
|
| 25 |
/** |
| 26 |
* Text constructor. |
| 27 |
* @param Frame $frame |
| 28 |
* @param Dompdf $dompdf |
| 29 |
* @throws Exception |
| 30 |
*/ |
| 31 |
function __construct(Frame $frame, Dompdf $dompdf) |
| 32 |
{ |
| 33 |
if (!$frame->is_text_node()) { |
| 34 |
throw new Exception("Text_Decorator can only be applied to #text nodes."); |
| 35 |
} |
| 36 |
|
| 37 |
parent::__construct($frame, $dompdf); |
| 38 |
$this->text_spacing = 0.0; |
| 39 |
} |
| 40 |
|
| 41 |
function reset() |
| 42 |
{ |
| 43 |
parent::reset(); |
| 44 |
$this->text_spacing = 0.0; |
| 45 |
} |
| 46 |
|
| 47 |
// Accessor methods |
| 48 |
|
| 49 |
/** |
| 50 |
* @return float |
| 51 |
*/ |
| 52 |
public function get_text_spacing(): float |
| 53 |
{ |
| 54 |
return $this->text_spacing; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
function get_text() |
| 61 |
{ |
| 62 |
// FIXME: this should be in a child class (and is incorrect) |
| 63 |
// if ( $this->_frame->get_style()->content !== "normal" ) { |
| 64 |
// $this->_frame->get_node()->data = $this->_frame->get_style()->content; |
| 65 |
// $this->_frame->get_style()->content = "normal"; |
| 66 |
// } |
| 67 |
|
| 68 |
// Helpers::pre_r("---"); |
| 69 |
// $style = $this->_frame->get_style(); |
| 70 |
// var_dump($text = $this->_frame->get_node()->data); |
| 71 |
// var_dump($asc = utf8_decode($text)); |
| 72 |
// for ($i = 0; $i < strlen($asc); $i++) |
| 73 |
// Helpers::pre_r("$i: " . $asc[$i] . " - " . ord($asc[$i])); |
| 74 |
// Helpers::pre_r("width: " . $this->_dompdf->getFontMetrics()->getTextWidth($text, $style->font_family, $style->font_size)); |
| 75 |
|
| 76 |
return $this->_frame->get_node()->data; |
| 77 |
} |
| 78 |
|
| 79 |
//........................................................................ |
| 80 |
|
| 81 |
/** |
| 82 |
* Vertical padding, border, and margin do not apply when determining the |
| 83 |
* height for inline frames. |
| 84 |
* |
| 85 |
* http://www.w3.org/TR/CSS21/visudet.html#inline-non-replaced |
| 86 |
* |
| 87 |
* The vertical padding, border and margin of an inline, non-replaced box |
| 88 |
* start at the top and bottom of the content area, not the |
| 89 |
* 'line-height'. But only the 'line-height' is used to calculate the |
| 90 |
* height of the line box. |
| 91 |
* |
| 92 |
* @return float |
| 93 |
*/ |
| 94 |
public function get_margin_height(): float |
| 95 |
{ |
| 96 |
// This function is also called in add_frame_to_line() and is used to |
| 97 |
// determine the line height |
| 98 |
$style = $this->get_style(); |
| 99 |
$font = $style->font_family; |
| 100 |
$size = $style->font_size; |
| 101 |
$fontHeight = $this->_dompdf->getFontMetrics()->getFontHeight($font, $size); |
| 102 |
|
| 103 |
return ($style->line_height / ($size > 0 ? $size : 1)) * $fontHeight; |
| 104 |
} |
| 105 |
|
| 106 |
public function get_padding_box(): array |
| 107 |
{ |
| 108 |
$style = $this->_frame->get_style(); |
| 109 |
$pb = $this->_frame->get_padding_box(); |
| 110 |
$pb[3] = $pb["h"] = (float) $style->length_in_pt($style->height); |
| 111 |
return $pb; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param float $spacing |
| 116 |
*/ |
| 117 |
public function set_text_spacing(float $spacing): void |
| 118 |
{ |
| 119 |
$this->text_spacing = $spacing; |
| 120 |
$this->recalculate_width(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Recalculate the text width |
| 125 |
* |
| 126 |
* @return float |
| 127 |
*/ |
| 128 |
public function recalculate_width(): float |
| 129 |
{ |
| 130 |
$fontMetrics = $this->_dompdf->getFontMetrics(); |
| 131 |
$style = $this->get_style(); |
| 132 |
$text = $this->get_text(); |
| 133 |
$font = $style->font_family; |
| 134 |
$size = $style->font_size; |
| 135 |
$word_spacing = $this->text_spacing + $style->word_spacing; |
| 136 |
$letter_spacing = $style->letter_spacing; |
| 137 |
$text_width = $fontMetrics->getTextWidth($text, $font, $size, $word_spacing, $letter_spacing); |
| 138 |
|
| 139 |
$style->set_used("width", $text_width); |
| 140 |
return $text_width; |
| 141 |
} |
| 142 |
|
| 143 |
// Text manipulation methods |
| 144 |
|
| 145 |
/** |
| 146 |
* Split the text in this frame at the offset specified. The remaining |
| 147 |
* text is added as a sibling frame following this one and is returned. |
| 148 |
* |
| 149 |
* @param int $offset |
| 150 |
* @return Frame|null |
| 151 |
*/ |
| 152 |
function split_text($offset) |
| 153 |
{ |
| 154 |
if ($offset == 0) { |
| 155 |
return null; |
| 156 |
} |
| 157 |
|
| 158 |
$split = $this->_frame->get_node()->splitText($offset); |
| 159 |
if ($split === false) { |
| 160 |
return null; |
| 161 |
} |
| 162 |
|
| 163 |
$deco = $this->copy($split); |
| 164 |
|
| 165 |
$p = $this->get_parent(); |
| 166 |
$p->insert_child_after($deco, $this, false); |
| 167 |
|
| 168 |
if ($p instanceof Inline) { |
| 169 |
$p->split($deco); |
| 170 |
} |
| 171 |
|
| 172 |
return $deco; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @param int $offset |
| 177 |
* @param int $count |
| 178 |
*/ |
| 179 |
function delete_text($offset, $count) |
| 180 |
{ |
| 181 |
$this->_frame->get_node()->deleteData($offset, $count); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @param string $text |
| 186 |
*/ |
| 187 |
function set_text($text) |
| 188 |
{ |
| 189 |
$this->_frame->get_node()->data = $text; |
| 190 |
} |
| 191 |
} |
| 192 |
|