This commit is contained in:
Tim Bendt
2025-11-25 00:16:35 -05:00
commit 6b9ef7ca55
6757 changed files with 1003748 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,257 @@
<?php
/**
* @package dompdf
* @link http://dompdf.github.com/
* @author Benj Carson <benjcarson@digitaljunkies.ca>
* @author Helmut Tischer <htischer@weihenstephan.org>
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
namespace Dompdf\Renderer;
use Dompdf\Helpers;
use Dompdf\Frame;
use Dompdf\Image\Cache;
use Dompdf\FrameDecorator\ListBullet as ListBulletFrameDecorator;
/**
* Renders list bullets
*
* @access private
* @package dompdf
*/
class ListBullet extends AbstractRenderer
{
/**
* @param $type
* @return mixed|string
*/
static function get_counter_chars($type)
{
static $cache = [];
if (isset($cache[$type])) {
return $cache[$type];
}
$uppercase = false;
$text = "";
switch ($type) {
case "decimal-leading-zero":
case "decimal":
case "1":
return "0123456789";
case "upper-alpha":
case "upper-latin":
case "A":
$uppercase = true;
case "lower-alpha":
case "lower-latin":
case "a":
$text = "abcdefghijklmnopqrstuvwxyz";
break;
case "upper-roman":
case "I":
$uppercase = true;
case "lower-roman":
case "i":
$text = "ivxlcdm";
break;
case "lower-greek":
for ($i = 0; $i < 24; $i++) {
$text .= Helpers::unichr($i + 944);
}
break;
}
if ($uppercase) {
$text = strtoupper($text);
}
return $cache[$type] = "$text.";
}
/**
* @param integer $n
* @param string $type
* @param integer $pad
*
* @return string
*/
private function make_counter($n, $type, $pad = null)
{
$n = intval($n);
$text = "";
$uppercase = false;
switch ($type) {
case "decimal-leading-zero":
case "decimal":
case "1":
if ($pad) {
$text = str_pad($n, $pad, "0", STR_PAD_LEFT);
} else {
$text = $n;
}
break;
case "upper-alpha":
case "upper-latin":
case "A":
$uppercase = true;
case "lower-alpha":
case "lower-latin":
case "a":
$text = chr(($n % 26) + ord('a') - 1);
break;
case "upper-roman":
case "I":
$uppercase = true;
case "lower-roman":
case "i":
$text = Helpers::dec2roman($n);
break;
case "lower-greek":
$text = Helpers::unichr($n + 944);
break;
}
if ($uppercase) {
$text = strtoupper($text);
}
return "$text.";
}
/**
* @param Frame $frame
*/
function render(Frame $frame)
{
$style = $frame->get_style();
$font_size = $style->font_size;
$line_height = $style->line_height;
$this->_set_opacity($frame->get_opacity($style->opacity));
$li = $frame->get_parent();
// Don't render bullets twice if if was split
if ($li->_splitted) {
return;
}
// Handle list-style-image
// If list style image is requested but missing, fall back to predefined types
if ($style->list_style_image !== "none" && !Cache::is_broken($img = $frame->get_image_url())) {
list($x, $y) = $frame->get_position();
//For expected size and aspect, instead of box size, use image natural size scaled to DPI.
// Resample the bullet image to be consistent with 'auto' sized images
// See also Image::get_min_max_width
// Tested php ver: value measured in px, suffix "px" not in value: rtrim unnecessary.
//$w = $frame->get_width();
//$h = $frame->get_height();
list($width, $height) = Helpers::dompdf_getimagesize($img, $this->_dompdf->getHttpContext());
$dpi = $this->_dompdf->getOptions()->getDpi();
$w = ((float)rtrim($width, "px") * 72) / $dpi;
$h = ((float)rtrim($height, "px") * 72) / $dpi;
$x -= $w;
$y -= ($line_height - $font_size) / 2; //Reverse hinting of list_bullet_positioner
$this->_canvas->image($img, $x, $y, $w, $h);
} else {
$bullet_style = $style->list_style_type;
$fill = false;
switch ($bullet_style) {
default:
/** @noinspection PhpMissingBreakStatementInspection */
case "disc":
$fill = true;
case "circle":
list($x, $y) = $frame->get_position();
$r = ($font_size * (ListBulletFrameDecorator::BULLET_SIZE /*-ListBulletFrameDecorator::BULLET_THICKNESS*/)) / 2;
$x -= $font_size * (ListBulletFrameDecorator::BULLET_SIZE / 2);
$y += ($font_size * (1 - ListBulletFrameDecorator::BULLET_DESCENT)) / 2;
$o = $font_size * ListBulletFrameDecorator::BULLET_THICKNESS;
$this->_canvas->circle($x, $y, $r, $style->color, $o, null, $fill);
break;
case "square":
list($x, $y) = $frame->get_position();
$w = $font_size * ListBulletFrameDecorator::BULLET_SIZE;
$x -= $w;
$y += ($font_size * (1 - ListBulletFrameDecorator::BULLET_DESCENT - ListBulletFrameDecorator::BULLET_SIZE)) / 2;
$this->_canvas->filled_rectangle($x, $y, $w, $w, $style->color);
break;
case "decimal-leading-zero":
case "decimal":
case "lower-alpha":
case "lower-latin":
case "lower-roman":
case "lower-greek":
case "upper-alpha":
case "upper-latin":
case "upper-roman":
case "1": // HTML 4.0 compatibility
case "a":
case "i":
case "A":
case "I":
$pad = null;
if ($bullet_style === "decimal-leading-zero") {
$pad = strlen($li->get_parent()->get_node()->getAttribute("dompdf-children-count"));
}
$node = $frame->get_node();
if (!$node->hasAttribute("dompdf-counter")) {
return;
}
$index = $node->getAttribute("dompdf-counter");
$text = $this->make_counter($index, $bullet_style, $pad);
if (trim($text) == "") {
return;
}
$spacing = 0;
$font_family = $style->font_family;
$line = $li->get_containing_line();
list($x, $y) = [$frame->get_position("x"), $line->y];
$x -= $this->_dompdf->getFontMetrics()->getTextWidth($text, $font_family, $font_size, $spacing);
// Take line-height into account
// TODO: should the line height take into account the line height of the containing block (per previous logic)
// $line_height = (float)$style->length_in_pt($style->line_height, $frame->get_containing_block("h"));
$line_height = $style->line_height;
$y += ($line_height - $font_size) / 4; // FIXME I thought it should be 2, but 4 gives better results
$this->_canvas->text($x, $y, $text,
$font_family, $font_size,
$style->color, $spacing);
case "none":
break;
}
}
$id = $frame->get_node()->getAttribute("id");
if (strlen($id) > 0) {
$this->_canvas->add_named_dest($id);
}
}
}

View File

@@ -0,0 +1,225 @@
<?php
/**
* @package dompdf
* @link http://dompdf.github.com/
* @author Benj Carson <benjcarson@digitaljunkies.ca>
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
namespace Dompdf\Renderer;
use Dompdf\Frame;
use Dompdf\FrameDecorator\Table;
/**
* Renders table cells
*
* @package dompdf
*/
class TableCell extends Block
{
/**
* @param Frame $frame
*/
function render(Frame $frame)
{
$style = $frame->get_style();
if (trim($frame->get_node()->nodeValue) === "" && $style->empty_cells === "hide") {
return;
}
$id = $frame->get_node()->getAttribute("id");
if (strlen($id) > 0) {
$this->_canvas->add_named_dest($id);
}
$this->_set_opacity($frame->get_opacity($style->opacity));
list($x, $y, $w, $h) = $frame->get_border_box();
$table = Table::find_parent_table($frame);
if ($table->get_style()->border_collapse !== "collapse") {
if (($bg = $style->background_color) !== "transparent") {
$this->_canvas->filled_rectangle($x, $y, (float)$w, (float)$h, $bg);
}
if (($url = $style->background_image) && $url !== "none") {
$this->_background_image($url, $x, $y, $w, $h, $style);
}
$this->_render_border($frame);
$this->_render_outline($frame);
return;
}
// The collapsed case is slightly complicated...
// @todo Add support for outlines here
$background_position_x = $x;
$background_position_y = $y;
$background_width = (float)$w;
$background_height = (float)$h;
$border_right_width = 0;
$border_left_width = 0;
$border_top_width = 0;
$border_bottom_width = 0;
$cellmap = $table->get_cellmap();
$cells = $cellmap->get_spanned_cells($frame);
if (is_null($cells)) {
return;
}
$num_rows = $cellmap->get_num_rows();
$num_cols = $cellmap->get_num_cols();
// Determine the top row spanned by this cell
$i = $cells["rows"][0];
$top_row = $cellmap->get_row($i);
// Determine if this cell borders on the bottom of the table. If so,
// then we draw its bottom border. Otherwise the next row down will
// draw its top border instead.
if (in_array($num_rows - 1, $cells["rows"])) {
$draw_bottom = true;
$bottom_row = $cellmap->get_row($num_rows - 1);
} else {
$draw_bottom = false;
}
// Draw the horizontal borders
$border_function_calls = [];
foreach ($cells["columns"] as $j) {
$bp = $cellmap->get_border_properties($i, $j);
$col = $cellmap->get_column($j);
$x = $col["x"] - $bp["left"]["width"] / 2;
$y = $top_row["y"] - $bp["top"]["width"] / 2;
$w = $col["used-width"] + ($bp["left"]["width"] + $bp["right"]["width"]) / 2;
if ($bp["top"]["width"] > 0) {
$widths = [
(float)$bp["top"]["width"],
(float)$bp["right"]["width"],
(float)$bp["bottom"]["width"],
(float)$bp["left"]["width"]
];
$border_top_width = max($border_top_width, $widths[0]);
$method = "_border_" . $bp["top"]["style"];
$border_function_calls[] = [$method, [$x, $y, $w, $bp["top"]["color"], $widths, "top", "square"]];
}
if ($draw_bottom) {
$bp = $cellmap->get_border_properties($num_rows - 1, $j);
if ($bp["bottom"]["width"] <= 0) {
continue;
}
$widths = [
(float)$bp["top"]["width"],
(float)$bp["right"]["width"],
(float)$bp["bottom"]["width"],
(float)$bp["left"]["width"]
];
$y = $bottom_row["y"] + $bottom_row["height"] + $bp["bottom"]["width"] / 2;
$border_bottom_width = max($border_bottom_width, $widths[2]);
$method = "_border_" . $bp["bottom"]["style"];
$border_function_calls[] = [$method, [$x, $y, $w, $bp["bottom"]["color"], $widths, "bottom", "square"]];
} else {
$adjacent_bp = $cellmap->get_border_properties($i+1, $j);
$border_bottom_width = max($border_bottom_width, $adjacent_bp["top"]["width"]);
}
}
$j = $cells["columns"][0];
$left_col = $cellmap->get_column($j);
if (in_array($num_cols - 1, $cells["columns"])) {
$draw_right = true;
$right_col = $cellmap->get_column($num_cols - 1);
} else {
$draw_right = false;
}
// Draw the vertical borders
foreach ($cells["rows"] as $i) {
$bp = $cellmap->get_border_properties($i, $j);
$row = $cellmap->get_row($i);
$x = $left_col["x"] - $bp["left"]["width"] / 2;
$y = $row["y"] - $bp["top"]["width"] / 2;
$h = $row["height"] + ($bp["top"]["width"] + $bp["bottom"]["width"]) / 2;
if ($bp["left"]["width"] > 0) {
$widths = [
(float)$bp["top"]["width"],
(float)$bp["right"]["width"],
(float)$bp["bottom"]["width"],
(float)$bp["left"]["width"]
];
$border_left_width = max($border_left_width, $widths[3]);
$method = "_border_" . $bp["left"]["style"];
$border_function_calls[] = [$method, [$x, $y, $h, $bp["left"]["color"], $widths, "left", "square"]];
}
if ($draw_right) {
$bp = $cellmap->get_border_properties($i, $num_cols - 1);
if ($bp["right"]["width"] <= 0) {
continue;
}
$widths = [
(float)$bp["top"]["width"],
(float)$bp["right"]["width"],
(float)$bp["bottom"]["width"],
(float)$bp["left"]["width"]
];
$x = $right_col["x"] + $right_col["used-width"] + $bp["right"]["width"] / 2;
$border_right_width = max($border_right_width, $widths[1]);
$method = "_border_" . $bp["right"]["style"];
$border_function_calls[] = [$method, [$x, $y, $h, $bp["right"]["color"], $widths, "right", "square"]];
} else {
$adjacent_bp = $cellmap->get_border_properties($i, $j+1);
$border_right_width = max($border_right_width, $adjacent_bp["left"]["width"]);
}
}
// Draw our background, border and content
if (($bg = $style->background_color) !== "transparent") {
$this->_canvas->filled_rectangle(
$background_position_x + ($border_left_width/2),
$background_position_y + ($border_top_width/2),
(float)$background_width - (($border_left_width + $border_right_width)/2),
(float)$background_height - (($border_top_width + $border_bottom_width)/2),
$bg
);
}
if (($url = $style->background_image) && $url !== "none") {
$this->_background_image(
$url,
$background_position_x + ($border_left_width/2),
$background_position_y + ($border_top_width/2),
(float)$background_width - (($border_left_width + $border_right_width)/2),
(float)$background_height - (($border_top_width + $border_bottom_width)/2),
$style
);
}
foreach ($border_function_calls as $border_function_call_params)
{
call_user_func_array([$this, $border_function_call_params[0]], $border_function_call_params[1]);
}
}
}

View File

@@ -0,0 +1,167 @@
<?php
/**
* @package dompdf
* @link http://dompdf.github.com/
* @author Benj Carson <benjcarson@digitaljunkies.ca>
* @author Helmut Tischer <htischer@weihenstephan.org>
* @author Fabien Ménager <fabien.menager@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
namespace Dompdf\Renderer;
use Dompdf\Adapter\CPDF;
use Dompdf\Frame;
/**
* Renders text frames
*
* @package dompdf
*/
class Text extends AbstractRenderer
{
/** Thickness of underline. Screen: 0.08, print: better less, e.g. 0.04 */
const DECO_THICKNESS = 0.02;
//Tweaking if $base and $descent are not accurate.
//Check method_exists( $this->_canvas, "get_cpdf" )
//- For cpdf these can and must stay 0, because font metrics are used directly.
//- For other renderers, if different values are wanted, separate the parameter sets.
// But $size and $size-$height seem to be accurate enough
/** Relative to bottom of text, as fraction of height */
const UNDERLINE_OFFSET = 0.0;
/** Relative to top of text */
const OVERLINE_OFFSET = 0.0;
/** Relative to centre of text. */
const LINETHROUGH_OFFSET = 0.0;
/** How far to extend lines past either end, in pt */
const DECO_EXTENSION = 0.0;
/**
* @param \Dompdf\FrameDecorator\Text $frame
*/
function render(Frame $frame)
{
$text = $frame->get_text();
if (trim($text) === "") {
return;
}
$style = $frame->get_style();
list($x, $y) = $frame->get_position();
$cb = $frame->get_containing_block();
if (($ml = $style->margin_left) === "auto" || $ml === "none") {
$ml = 0;
}
if (($pl = $style->padding_left) === "auto" || $pl === "none") {
$pl = 0;
}
if (($bl = $style->border_left_width) === "auto" || $bl === "none") {
$bl = 0;
}
$x += (float)$style->length_in_pt([$ml, $pl, $bl], $cb["w"]);
$font = $style->font_family;
$size = $style->font_size;
$frame_font_size = $frame->get_dompdf()->getFontMetrics()->getFontHeight($font, $size);
$word_spacing = $frame->get_text_spacing() + (float)$style->length_in_pt($style->word_spacing);
$char_spacing = (float)$style->length_in_pt($style->letter_spacing);
$width = $style->width;
/*$text = str_replace(
array("{PAGE_NUM}"),
array($this->_canvas->get_page_number()),
$text
);*/
$this->_canvas->text($x, $y, $text,
$font, $size,
$style->color, $word_spacing, $char_spacing);
$line = $frame->get_containing_line();
// FIXME Instead of using the tallest frame to position,
// the decoration, the text should be well placed
if (false && $line->tallest_frame) {
$base_frame = $line->tallest_frame;
$style = $base_frame->get_style();
$size = $style->font_size;
}
$line_thickness = $size * self::DECO_THICKNESS;
$underline_offset = $size * self::UNDERLINE_OFFSET;
$overline_offset = $size * self::OVERLINE_OFFSET;
$linethrough_offset = $size * self::LINETHROUGH_OFFSET;
$underline_position = -0.08;
if ($this->_canvas instanceof CPDF) {
$cpdf_font = $this->_canvas->get_cpdf()->fonts[$style->font_family];
if (isset($cpdf_font["UnderlinePosition"])) {
$underline_position = $cpdf_font["UnderlinePosition"] / 1000;
}
if (isset($cpdf_font["UnderlineThickness"])) {
$line_thickness = $size * ($cpdf_font["UnderlineThickness"] / 1000);
}
}
$descent = $size * $underline_position;
$base = $frame_font_size;
// Handle text decoration:
// http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration
// Draw all applicable text-decorations. Start with the root and work our way down.
$p = $frame;
$stack = [];
while ($p = $p->get_parent()) {
$stack[] = $p;
}
while (isset($stack[0])) {
$f = array_pop($stack);
if (($text_deco = $f->get_style()->text_decoration) === "none") {
continue;
}
$deco_y = $y; //$line->y;
$color = $f->get_style()->color;
switch ($text_deco) {
default:
continue 2;
case "underline":
$deco_y += $base - $descent + $underline_offset + $line_thickness / 2;
break;
case "overline":
$deco_y += $overline_offset + $line_thickness / 2;
break;
case "line-through":
$deco_y += $base * 0.7 + $linethrough_offset;
break;
}
$dx = 0;
$x1 = $x - self::DECO_EXTENSION;
$x2 = $x + $width + $dx + self::DECO_EXTENSION;
$this->_canvas->line($x1, $deco_y, $x2, $deco_y, $color, $line_thickness);
}
if ($this->_dompdf->getOptions()->getDebugLayout() && $this->_dompdf->getOptions()->getDebugLayoutLines()) {
$text_width = $this->_dompdf->getFontMetrics()->getTextWidth($text, $font, $size);
$this->_debug_layout([$x, $y, $text_width + ($line->wc - 1) * $word_spacing, $frame_font_size], "orange", [0.5, 0.5]);
}
}
}