| 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\Renderer; |
| 8 |
|
| 9 |
use Dompdf\Adapter\CPDF; |
| 10 |
use Dompdf\Frame; |
| 11 |
|
| 12 |
/** |
| 13 |
* Renders text frames |
| 14 |
* |
| 15 |
* @package dompdf |
| 16 |
*/ |
| 17 |
class Text extends AbstractRenderer |
| 18 |
{ |
| 19 |
/** Thickness of underline. Screen: 0.08, print: better less, e.g. 0.04 */ |
| 20 |
const DECO_THICKNESS = 0.02; |
| 21 |
|
| 22 |
//Tweaking if $base and $descent are not accurate. |
| 23 |
//Check method_exists( $this->_canvas, "get_cpdf" ) |
| 24 |
//- For cpdf these can and must stay 0, because font metrics are used directly. |
| 25 |
//- For other renderers, if different values are wanted, separate the parameter sets. |
| 26 |
// But $size and $size-$height seem to be accurate enough |
| 27 |
|
| 28 |
/** Relative to bottom of text, as fraction of height */ |
| 29 |
const UNDERLINE_OFFSET = 0.0; |
| 30 |
|
| 31 |
/** Relative to top of text */ |
| 32 |
const OVERLINE_OFFSET = 0.0; |
| 33 |
|
| 34 |
/** Relative to centre of text. */ |
| 35 |
const LINETHROUGH_OFFSET = 0.0; |
| 36 |
|
| 37 |
/** How far to extend lines past either end, in pt */ |
| 38 |
const DECO_EXTENSION = 0.0; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param \Dompdf\FrameDecorator\Text $frame |
| 42 |
*/ |
| 43 |
function render(Frame $frame) |
| 44 |
{ |
| 45 |
$style = $frame->get_style(); |
| 46 |
$text = $frame->get_text(); |
| 47 |
|
| 48 |
if (trim($text) === "") { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$this->_set_opacity($frame->get_opacity($style->opacity)); |
| 53 |
|
| 54 |
list($x, $y) = $frame->get_position(); |
| 55 |
$cb = $frame->get_containing_block(); |
| 56 |
|
| 57 |
$ml = $style->margin_left; |
| 58 |
$pl = $style->padding_left; |
| 59 |
$bl = $style->border_left_width; |
| 60 |
$x += (float) $style->length_in_pt([$ml, $pl, $bl], $cb["w"]); |
| 61 |
|
| 62 |
$font = $style->font_family; |
| 63 |
$size = $style->font_size; |
| 64 |
$frame_font_size = $frame->get_dompdf()->getFontMetrics()->getFontHeight($font, $size); |
| 65 |
$word_spacing = $frame->get_text_spacing() + $style->word_spacing; |
| 66 |
$letter_spacing = $style->letter_spacing; |
| 67 |
$width = (float) $style->width; |
| 68 |
|
| 69 |
/*$text = str_replace( |
| 70 |
array("{PAGE_NUM}"), |
| 71 |
array($this->_canvas->get_page_number()), |
| 72 |
$text |
| 73 |
);*/ |
| 74 |
|
| 75 |
$this->_canvas->text($x, $y, $text, |
| 76 |
$font, $size, |
| 77 |
$style->color, $word_spacing, $letter_spacing); |
| 78 |
|
| 79 |
$line = $frame->get_containing_line(); |
| 80 |
|
| 81 |
// FIXME Instead of using the tallest frame to position, |
| 82 |
// the decoration, the text should be well placed |
| 83 |
if (false && $line->tallest_frame) { |
| 84 |
$base_frame = $line->tallest_frame; |
| 85 |
$style = $base_frame->get_style(); |
| 86 |
$size = $style->font_size; |
| 87 |
} |
| 88 |
|
| 89 |
$line_thickness = $size * self::DECO_THICKNESS; |
| 90 |
$underline_offset = $size * self::UNDERLINE_OFFSET; |
| 91 |
$overline_offset = $size * self::OVERLINE_OFFSET; |
| 92 |
$linethrough_offset = $size * self::LINETHROUGH_OFFSET; |
| 93 |
$underline_position = -0.08; |
| 94 |
|
| 95 |
if ($this->_canvas instanceof CPDF) { |
| 96 |
$cpdf_font = $this->_canvas->get_cpdf()->fonts[$style->font_family]; |
| 97 |
|
| 98 |
if (isset($cpdf_font["UnderlinePosition"])) { |
| 99 |
$underline_position = $cpdf_font["UnderlinePosition"] / 1000; |
| 100 |
} |
| 101 |
|
| 102 |
if (isset($cpdf_font["UnderlineThickness"])) { |
| 103 |
$line_thickness = $size * ($cpdf_font["UnderlineThickness"] / 1000); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
$descent = $size * $underline_position; |
| 108 |
$base = $frame_font_size; |
| 109 |
|
| 110 |
// Handle text decoration: |
| 111 |
// http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration |
| 112 |
|
| 113 |
// Draw all applicable text-decorations. Start with the root and work our way down. |
| 114 |
$p = $frame; |
| 115 |
$stack = []; |
| 116 |
while ($p = $p->get_parent()) { |
| 117 |
$stack[] = $p; |
| 118 |
} |
| 119 |
|
| 120 |
while (isset($stack[0])) { |
| 121 |
$f = array_pop($stack); |
| 122 |
|
| 123 |
if (($text_deco = $f->get_style()->text_decoration) === "none") { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
$deco_y = $y; //$line->y; |
| 128 |
$color = $f->get_style()->color; |
| 129 |
|
| 130 |
switch ($text_deco) { |
| 131 |
default: |
| 132 |
continue 2; |
| 133 |
|
| 134 |
case "underline": |
| 135 |
$deco_y += $base - $descent + $underline_offset + $line_thickness / 2; |
| 136 |
break; |
| 137 |
|
| 138 |
case "overline": |
| 139 |
$deco_y += $overline_offset + $line_thickness / 2; |
| 140 |
break; |
| 141 |
|
| 142 |
case "line-through": |
| 143 |
$deco_y += $base * 0.7 + $linethrough_offset; |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
$dx = 0; |
| 148 |
$x1 = $x - self::DECO_EXTENSION; |
| 149 |
$x2 = $x + $width + $dx + self::DECO_EXTENSION; |
| 150 |
$this->_canvas->line($x1, $deco_y, $x2, $deco_y, $color, $line_thickness); |
| 151 |
} |
| 152 |
|
| 153 |
if ($this->_dompdf->getOptions()->getDebugLayout() && $this->_dompdf->getOptions()->getDebugLayoutLines()) { |
| 154 |
$text_width = $this->_dompdf->getFontMetrics()->getTextWidth($text, $font, $size, $word_spacing, $letter_spacing); |
| 155 |
$this->_debug_layout([$x, $y, $text_width, $frame_font_size], "orange", [0.5, 0.5]); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|