Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 118 additions & 28 deletions src/Mpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Mpdf\Utils\NumericString;
use Mpdf\Utils\UtfString;
use Mpdf\Utils\Path;
use Mpdf\Writer\OptionalContentWriter;
use Psr\Log\NullLogger;
use Mpdf\Unicode\Ucdn;
use Mpdf\Unicode\Bidi;
Expand Down Expand Up @@ -50,6 +51,11 @@ class Mpdf implements \Psr\Log\LoggerAwareInterface

const OBJECT_IDENTIFIER = "\xbb\xa4\xac";

/**
* The optional content group each visibility draws in, by its resource name in OptionalContentWriter
*/
const VISIBILITY_GROUPS = ['printonly' => 'OC1', 'screenonly' => 'OC2', 'hidden' => 'OC3'];

var $useFixedNormalLineHeight; // mPDF 6
var $useFixedTextBaseline; // mPDF 6
var $adjustFontDescLineheight; // mPDF 6
Expand Down Expand Up @@ -2150,32 +2156,91 @@ function AddExtGState($parms)

function SetVisibility($v)
{
$dropped = $this->visibilityDropped($v);
// PDF/A-2 forbids the /AS that switches print-only and screen-only content, but hidden content needs none
if ($v !== 'visible' && (!$this->optionalContentAllowed() || ($this->PDFA && $v !== 'hidden'))) {
$this->PDFAXwarnings[] = "Cannot set visibility to " . $v . " when using PDFA or PDFX";
return '';
if (!$dropped) {
return '';
}
} elseif (!$this->PDFA && !$this->PDFX) {
$this->setMinPdfVersion('1.5');
}
if ($this->visibility != 'visible') {
$this->writer->write('EMC');
if ($this->visibilityDropped($this->visibility)) {
$this->writer->write('___DROPPED___END' . $this->uniqstr);
$this->resumeAfterDroppedContent();
} else {
$this->writer->write('EMC');
}
$this->hasOC = intval($this->hasOC);
}
if ($v == 'printonly') {
$this->writer->write('/OC /OC1 BDC');
$this->hasOC = ($this->hasOC | 1);
} elseif ($v == 'screenonly') {
$this->writer->write('/OC /OC2 BDC');
$this->hasOC = ($this->hasOC | 2);
} elseif ($v == 'hidden') {
$this->writer->write('/OC /OC3 BDC');
$this->hasOC = ($this->hasOC | 4);
if ($dropped) {
$this->writer->write('___DROPPED___START' . $this->uniqstr);
} elseif ($v != 'visible') {
throw new \Mpdf\MpdfException('Incorrect visibility: ' . $v);
$this->writer->write($this->visibilityBegins($v));
$this->hasOC = ($this->hasOC | OptionalContentWriter::VISIBILITY_GROUPS[self::VISIBILITY_GROUPS[$v]][0]);
}
$this->visibility = $v;
}

/**
* Whether content with this visibility is left out: under PDFAauto and PDFXauto, what would not print is dropped,
* which is screen-only content, and hidden content where optional content is not allowed
*
* @param string $v
*
* @return bool
*/
private function visibilityDropped($v)
{
if ($v !== 'screenonly' && ($v !== 'hidden' || $this->optionalContentAllowed())) {
return false;
}

return ($this->PDFA && $this->PDFAauto) || ($this->PDFX && $this->PDFXauto);
}

/**
* The operator that begins content drawn in the optional content group of a visibility other than visible
*
* @param string $v
*
* @return string
*/
private function visibilityBegins($v)
{
if (!array_key_exists($v, self::VISIBILITY_GROUPS)) {
throw new \Mpdf\MpdfException('Incorrect visibility: ' . $v);
}

return '/OC /' . self::VISIBILITY_GROUPS[$v] . ' BDC';
}

/**
* Write the font, colours, line width and spacing in force again, since any set inside left-out content went
* with it
*/
private function resumeAfterDroppedContent()
{
$this->pageoutput[$this->page] = [];

if ($this->FontFamily) {
$this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt, true, true);
}

$this->SetLineWidth($this->LineWidth);

foreach (['DrawColor', 'FillColor'] as $color) {
if ($this->$color) {
$this->writer->write($this->$color);
$this->pageoutput[$this->page][$color] = $this->$color;
}
}

$this->writer->write(sprintf('BT %.3F Tc %.3F Tw ET', $this->charspacing, $this->ws));
}

function Open()
{
// Begin document
Expand Down Expand Up @@ -2528,6 +2593,13 @@ function PrintPageBackgrounds($adjustmenty = 0)

foreach ($this->pageBackgrounds as $bl => $pbs) {

// A background left out is not drawn, so it registers no shading, pattern or image either
foreach ($pbs as $i => $pb) {
if ($this->visibilityDropped($pb['visibility'])) {
unset($pbs[$i]);
}
}

foreach ($pbs as $pb) {

if ((!isset($pb['image_id']) && !isset($pb['gradient'])) || isset($pb['shadowonly'])) { // Background colour or boxshadow
Expand All @@ -2538,13 +2610,7 @@ function PrintPageBackgrounds($adjustmenty = 0)
}

if ($pb['visibility'] != 'visible') {
if ($pb['visibility'] == 'printonly') {
$s .= '/OC /OC1 BDC' . "\n";
} elseif ($pb['visibility'] == 'screenonly') {
$s .= '/OC /OC2 BDC' . "\n";
} elseif ($pb['visibility'] == 'hidden') {
$s .= '/OC /OC3 BDC' . "\n";
}
$s .= $this->visibilityBegins($pb['visibility']) . "\n";
}

// Box shadow
Expand Down Expand Up @@ -2592,13 +2658,7 @@ function PrintPageBackgrounds($adjustmenty = 0)
}

if ($pb['visibility'] != 'visible') {
if ($pb['visibility'] == 'printonly') {
$s .= '/OC /OC1 BDC' . "\n";
} elseif ($pb['visibility'] == 'screenonly') {
$s .= '/OC /OC2 BDC' . "\n";
} elseif ($pb['visibility'] == 'hidden') {
$s .= '/OC /OC3 BDC' . "\n";
}
$s .= $this->visibilityBegins($pb['visibility']) . "\n";
}

}
Expand Down Expand Up @@ -4777,6 +4837,10 @@ function SetLink($link, $y = 0, $page = -1)

function Link($x, $y, $w, $h, $link)
{
if ($this->visibilityDropped($this->visibility)) {
return;
}

$l = [$x * Mpdf::SCALE, $this->hPt - $y * Mpdf::SCALE, $w * Mpdf::SCALE, $h * Mpdf::SCALE, $link];
if ($this->table_rotate) { // *TABLES*
$this->tbrot_Links[$this->page][] = $l; // *TABLES*
Expand Down Expand Up @@ -7375,6 +7439,7 @@ function finishFlowingBlock($endofblock = false, $next = '')
if (isset($this->textparam['visibility']) && $this->textparam['visibility'] && $this->textparam['visibility'] != $this->visibility) {
$this->SetVisibility($this->textparam['visibility']);
}
$this->spanVisibilityToObject($k);

// *********** SPAN BACKGROUND COLOR ***************** //
if (isset($this->spanbgcolor) && $this->spanbgcolor) {
Expand Down Expand Up @@ -7560,8 +7625,15 @@ function printobjectbuffer($is_table = false, $blockdir = false)
$rtlalign = 'L';
}

// An object left out is not drawn, and registers no link, field, bookmark or entry
$blockDropped = $this->visibilityDropped($this->visibility);

foreach ($this->objectbuffer as $ib => $objattr) {

if ($blockDropped || (isset($objattr['visibility']) && $this->visibilityDropped($objattr['visibility']))) {
continue;
}

if ($objattr['type'] == 'bookmark' || $objattr['type'] == 'indexentry' || $objattr['type'] == 'toc') {
$x = $objattr['OUTER-X'];
$y = $objattr['OUTER-Y'];
Expand Down Expand Up @@ -7641,6 +7713,7 @@ function printobjectbuffer($is_table = false, $blockdir = false)
if (isset($objattr['z-index']) && $objattr['z-index'] > 0 && $this->current_layer == 0) {
$this->BeginLayer($objattr['z-index']);
}
$save_vis = $this->visibility;
if (isset($objattr['visibility']) && $objattr['visibility'] != 'visible' && $objattr['visibility']) {
$this->SetVisibility($objattr['visibility']);
}
Expand Down Expand Up @@ -7834,8 +7907,8 @@ function printobjectbuffer($is_table = false, $blockdir = false)
$this->restoreGraphicsState();
}

if (isset($objattr['visibility']) && $objattr['visibility'] != 'visible' && $objattr['visibility']) {
$this->SetVisibility('visible');
if ($this->visibility != $save_vis) {
$this->SetVisibility($save_vis);
}
if (isset($objattr['z-index']) && $objattr['z-index'] > 0 && $this->current_layer == 0) {
$this->EndLayer();
Expand Down Expand Up @@ -8123,6 +8196,22 @@ function ($col) {
unset($content);
}

/**
* Give an object in a span the span's visibility, as printobjectbuffer() draws it after the span's text
*
* @param int $k The object's index in the object buffer
*/
private function spanVisibilityToObject($k)
{
if (!isset($this->objectbuffer[$k]) || empty($this->textparam['visibility']) || $this->textparam['visibility'] === 'visible') {
return;
}

if (empty($this->objectbuffer[$k]['visibility']) || $this->objectbuffer[$k]['visibility'] === 'visible') {
$this->objectbuffer[$k]['visibility'] = $this->textparam['visibility'];
}
}

function _printListBullet($x, $y, $size, $type, $color)
{
// x and y are the centre of the bullet; size is the width and/or height in mm
Expand Down Expand Up @@ -9038,6 +9127,7 @@ function WriteFlowingBlock($s, $sOTLdata)
if (isset($this->textparam['visibility']) && $this->textparam['visibility'] && $this->textparam['visibility'] != $this->visibility) {
$this->SetVisibility($this->textparam['visibility']);
}
$this->spanVisibilityToObject($k);
// *********** SPAN BACKGROUND COLOR ***************** //
if ($this->spanbgcolor) {
$cor = $this->spanbgcolorarray;
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function open($attr, &$ahtml, &$ihtml)
$objattr['POPUP'] = true;
}
}
$e = Mpdf::OBJECT_IDENTIFIER . "type=annot,objattr=" . serialize($objattr) . Mpdf::OBJECT_IDENTIFIER;
$e = Mpdf::OBJECT_IDENTIFIER . "type=annot,objattr=" . serialize($this->withSpanVisibility($objattr)) . Mpdf::OBJECT_IDENTIFIER;
if ($this->mpdf->tableLevel) {
$this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['textbuffer'][] = [$e];
} // *TABLES*
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function open($attr, &$ahtml, &$ihtml)
} else {
$objattr['bklevel'] = 0;
}
$e = Mpdf::OBJECT_IDENTIFIER . "type=bookmark,objattr=" . serialize($objattr) . Mpdf::OBJECT_IDENTIFIER;
$e = Mpdf::OBJECT_IDENTIFIER . "type=bookmark,objattr=" . serialize($this->withSpanVisibility($objattr)) . Mpdf::OBJECT_IDENTIFIER;
if ($this->mpdf->tableLevel) {
$this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['textbuffer'][] = [$e];
} // *TABLES*
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/Img.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public function open($attr, &$ahtml, &$ihtml)
$objattr['SUBJECT'] = '';
$objattr['OPACITY'] = $this->mpdf->annotOpacity;
$objattr['COLOR'] = $this->colorConverter->convert('yellow', $this->mpdf->PDFAXwarnings);
$e = Mpdf::OBJECT_IDENTIFIER . "type=annot,objattr=" . serialize($objattr) . Mpdf::OBJECT_IDENTIFIER;
$e = Mpdf::OBJECT_IDENTIFIER . "type=annot,objattr=" . serialize($this->withSpanVisibility($objattr)) . Mpdf::OBJECT_IDENTIFIER;
if ($this->mpdf->tableLevel) { // *TABLES*
$this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['textbuffer'][] = [$e]; // *TABLES*
} // *TABLES*
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/IndexEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function open($attr, &$ahtml, &$ihtml)
$objattr['CONTENT'] = htmlspecialchars_decode($attr['CONTENT'], ENT_QUOTES);
$objattr['type'] = 'indexentry';
$objattr['vertical-align'] = 'T';
$e = Mpdf::OBJECT_IDENTIFIER . "type=indexentry,objattr=" . serialize($objattr) . Mpdf::OBJECT_IDENTIFIER;
$e = Mpdf::OBJECT_IDENTIFIER . "type=indexentry,objattr=" . serialize($this->withSpanVisibility($objattr)) . Mpdf::OBJECT_IDENTIFIER;
if ($this->mpdf->tableLevel) {
$this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['textbuffer'][] = [$e];
} // *TABLES*
Expand Down
17 changes: 17 additions & 0 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ protected function getAlign($property)
return array_key_exists($property, self::ALIGN) ? self::ALIGN[$property] : '';
}

/**
* An object's attributes with the visibility of the span it is in. Printing reads a span's visibility from its
* text buffer entry, and an object put straight into the buffer has none.
*
* @param mixed[] $objattr
*
* @return mixed[]
*/
protected function withSpanVisibility(array $objattr)
{
if (!empty($this->mpdf->textparam['visibility'])) {
$objattr['visibility'] = $this->mpdf->textparam['visibility'];
}

return $objattr;
}

abstract public function open($attr, &$ahtml, &$ihtml);

abstract public function close(&$ahtml, &$ihtml);
Expand Down
2 changes: 1 addition & 1 deletion src/Tag/TocEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function open($attr, &$ahtml, &$ihtml)
} else {
$objattr['toc_id'] = 0;
}
$e = Mpdf::OBJECT_IDENTIFIER . "type=toc,objattr=" . serialize($objattr) . Mpdf::OBJECT_IDENTIFIER;
$e = Mpdf::OBJECT_IDENTIFIER . "type=toc,objattr=" . serialize($this->withSpanVisibility($objattr)) . Mpdf::OBJECT_IDENTIFIER;
if ($this->mpdf->tableLevel) {
$this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['textbuffer'][] = [$e];
} // *TABLES*
Expand Down
6 changes: 6 additions & 0 deletions src/Writer/PageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public function writePages() // _putpages
}
}

// Remove content left out of a PDF/A or PDF/X document, then any unpaired marker
if (strpos($thispage, '___DROPPED___') !== false) {
$thispage = preg_replace('/___DROPPED___START' . $this->mpdf->uniqstr . '.*?___DROPPED___END' . $this->mpdf->uniqstr . '/s', '', $thispage);
$thispage = str_replace(['___DROPPED___START' . $this->mpdf->uniqstr, '___DROPPED___END' . $this->mpdf->uniqstr], '', $thispage);
}

// Clean up repeated /GS1 gs statements
// For some reason using + for repetition instead of {2,20} crashes PHP Script Interpreter ???
$thispage = preg_replace('/(\/GS1 gs\n){2,20}/', "/GS1 gs\n", $thispage);
Expand Down
Loading
Loading