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
12 changes: 7 additions & 5 deletions src/Fonts/FontSubsetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,22 +403,23 @@ private function buildSubset(array $subset, $useOTL)
* @param int $TTCfontID Which font of a TrueType Collection, or 0 for a plain font
* @param bool $debug Whether to check the font's own tables as they are read
* @param int $useOTL Whether the document laid the font out with its OTL tables
* @param bool $oneCmapSubtable Whether the cmap may hold only its (3,0) subtable, as PDF/A-1 requires
*
* @return string The font program to embed
*
* @throws \Mpdf\Exception\FontException Where the font carries no Unicode cmap to read, or subsets
* into more segments than the format 4 subtable's length
* field can state
*/
public function makeSubsetSIP($file, array $subset, $TTCfontID = 0, $debug = false, $useOTL = 0)
public function makeSubsetSIP($file, array $subset, $TTCfontID = 0, $debug = false, $useOTL = 0, $oneCmapSubtable = false)
{
$this->open($file);

// Closed however the build ends: see makeSubset
try {
$this->readTableDirectory($TTCfontID, $debug);

return $this->buildSubsetSIP($subset, $TTCfontID, $useOTL);
return $this->buildSubsetSIP($subset, $TTCfontID, $useOTL, $oneCmapSubtable);
} finally {
$this->reader->close();
}
Expand All @@ -427,7 +428,7 @@ public function makeSubsetSIP($file, array $subset, $TTCfontID = 0, $debug = fal
/**
* @return string See makeSubsetSIP
*/
private function buildSubsetSIP(array $subset, $TTCfontID, $useOTL)
private function buildSubsetSIP(array $subset, $TTCfontID, $useOTL, $oneCmapSubtable)
{
list($indexToLocFormat, $numberOfHMetrics, $numGlyphs) = $this->readHeaders();

Expand Down Expand Up @@ -558,11 +559,12 @@ private function buildSubsetSIP(array $subset, $TTCfontID, $useOTL)
// cmap - Character to glyph mapping
$format6Length = 10 + 2 * count($subset);

if ($format6Length > 0xFFFF) {
if ($format6Length > 0xFFFF || $oneCmapSubtable) {
// A format 6 subtable states its length in a uint16, so past 32,762 characters it cannot be
// written. It maps the same codes to the same glyphs as the format 4 subtable, and a reader
// looks a symbolic TrueType font's codes up in (3,0) before (1,0) (ISO 32000-1, 9.6.6.4),
// so leave (1,0) out rather than refuse a font the format 4 subtable maps whole.
// so leave (1,0) out rather than refuse a font the format 4 subtable maps whole, or one
// PDF/A-1 allows a single subtable.
$cmapstr = TableWriter::uint16s([0, 1, 3, 0]) . TableWriter::uint32(12) . $cmapstr4;
} else {
$cmapstr = TableWriter::uint16s([0, 2, 1, 0]) . TableWriter::uint32(20)
Expand Down
33 changes: 21 additions & 12 deletions src/Image/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -1512,9 +1512,7 @@ function svgStyle($critere_style, $attribs, $element)
} elseif ($critere_style['fill-opacity'] < 0) {
$opacity = 0;
}
$gs = $this->mpdf->AddExtGState(['ca' => $opacity, 'BM' => '/Normal']);
$this->mpdf->extgstates[$gs]['fo'] = true;
$path_style .= sprintf(' /GS%d gs ', $gs);
$path_style .= $this->opacityState(['ca' => $opacity]);
}

if (isset($critere_style['stroke-opacity'])) {
Expand All @@ -1528,9 +1526,7 @@ function svgStyle($critere_style, $attribs, $element)
} elseif ($critere_style['stroke-opacity'] < 0) {
$opacity = 0;
}
$gs = $this->mpdf->AddExtGState(['CA' => $opacity, 'BM' => '/Normal']);
$this->mpdf->extgstates[$gs]['fo'] = true;
$path_style .= sprintf(' /GS%d gs ', $gs);
$path_style .= $this->opacityState(['CA' => $opacity]);
}

switch ($style) {
Expand Down Expand Up @@ -2436,9 +2432,7 @@ function svgText()
}
}

$gs = $this->mpdf->AddExtGState(['ca' => $fopacity, 'CA' => $sopacity, 'BM' => '/Normal']);
$this->mpdf->extgstates[$gs]['fo'] = true;
$opacitystr = sprintf(' /GS%d gs ', $gs);
$opacitystr = $this->opacityState(['ca' => $fopacity, 'CA' => $sopacity]);

$fillstr = '';
if (isset($current_style['fill']) && $current_style['fill'] != 'none') {
Expand Down Expand Up @@ -2612,9 +2606,7 @@ function svgText()
}
}

$gs = $this->mpdf->AddExtGState(['ca' => $fopacity, 'CA' => $sopacity, 'BM' => '/Normal']);
$this->mpdf->extgstates[$gs]['fo'] = true;
$opacitystr = sprintf(' /GS%d gs ', $gs);
$opacitystr = $this->opacityState(['ca' => $fopacity, 'CA' => $sopacity]);

$fillstr = '';

Expand Down Expand Up @@ -4186,6 +4178,23 @@ function xml_svg2pdf_end($parser, $name)
}
}

/**
* The operator that sets the fill (ca) and stroke (CA) opacities given, painting opaque where the document does
* not allow transparency
*
* @param float[] $alphas
*
* @return string
*/
private function opacityState(array $alphas)
{
$alphas = array_map([$this->mpdf, 'allowedAlpha'], $alphas);
$gs = $this->mpdf->AddExtGState($alphas + ['BM' => '/Normal']);
$this->mpdf->extgstates[$gs]['fo'] = true;

return sprintf(' /GS%d gs ', $gs);
}

private function computeBezierBoundingBox($start, $c)
{
$P0 = [$start[0], $start[1]];
Expand Down
58 changes: 47 additions & 11 deletions src/Mpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2025,17 +2025,53 @@ public function pdfaConformance()
*/
public function transparencyAllowed()
{
if ($this->PDFX) {
return false;
}
return !$this->PDFX && $this->pdfaPart() !== '1';
}

/**
* The PDF/A part the document conforms to, or null when it is not PDF/A
*
* @return string|null
*/
public function pdfaPart()
{
if (!$this->PDFA) {
return true;
return null;
}

list($part) = $this->pdfaConformance();

return $part !== '1';
return $part;
}

/**
* The alpha the document may paint with: the one given, or opaque where transparency is not allowed
*
* @param float $alpha from 0 (transparent) to 1 (opaque)
*
* @return float
*/
public function allowedAlpha($alpha)
{
if ($alpha == 1 || $this->transparencyAllowed()) {
return $alpha;
}

$this->pdfaxWarning('Image opacity must be 100% (Opacity changed to 100%)');

return 1;
}

/**
* Records what a PDF/A or PDF/X document had to change, unless it was asked to make such changes silently
*
* @param string $message
*/
private function pdfaxWarning($message)
{
if (($this->PDFA && !$this->PDFAauto) || ($this->PDFX && !$this->PDFXauto)) {
$this->PDFAXwarnings[] = $message;
}
}

/**
Expand All @@ -2057,12 +2093,7 @@ function SetAlpha($alpha, $bm = 'Normal', $return = false, $mode = 'B')
// HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
// set alpha for stroking (CA) and non-stroking (ca) operations
// mode determines F (fill) S (stroke) B (both)
if (!$this->transparencyAllowed() && $alpha != 1) {
if (($this->PDFA && !$this->PDFAauto) || ($this->PDFX && !$this->PDFXauto)) {
$this->PDFAXwarnings[] = "Image opacity must be 100% (Opacity changed to 100%)";
}
$alpha = 1;
}
$alpha = $this->allowedAlpha($alpha);
$a = ['BM' => '/' . $bm];
if ($mode == 'F' || $mode == 'B') {
$a['ca'] = $alpha; // mPDF 5.7.2
Expand Down Expand Up @@ -17704,6 +17735,11 @@ function PaintDivBB($divider = '', $blockstate = 0, $blvl = 0)
$shadow .= 'W n' . "\n";

$sh['blur'] = abs($sh['blur']); // cannot have negative blur value
// A blur fades out through a soft mask, so without transparency the shadow keeps the hard edge at the blur's midpoint
if ($sh['blur'] && !$this->transparencyAllowed()) {
$this->pdfaxWarning('A box-shadow cannot be blurred without transparency (Shadow drawn without blur)');
$sh['blur'] = 0;
}
// Ensure spread/blur do not make effective shadow width/height < 0
// Could do more complex things but this just adjusts spread value
if (-$sh['spread'] + $sh['blur'] / 2 > min($w / 2, $h / 2)) {
Expand Down
31 changes: 30 additions & 1 deletion src/Writer/FontWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function writeFonts()

$subset = $font['subsets'][$sfid];
unset($subset[0]);
$ttfontstream = $subsetter->makeSubsetSIP($font['ttffile'], $subset, $font['TTCfontID'], $this->mpdf->debugfonts, $font['useOTL']); // mPDF 5.7.1
$ttfontstream = $subsetter->makeSubsetSIP($font['ttffile'], $subset, $font['TTCfontID'], $this->mpdf->debugfonts, $font['useOTL'], $this->mpdf->pdfaPart() === '1'); // mPDF 5.7.1
$ttfontsize = strlen($ttfontstream);
$fontstream = gzcompress($ttfontstream);
$widthstring = '';
Expand Down Expand Up @@ -416,6 +416,12 @@ public function writeFonts()
$this->writer->write(' /Style << /Panose ' . $this->writer->hexString($font['panose']) . ' >>');
}

// PDF/A-1 requires a CIDSet of a subset, written after the font file
$cidSet = $asSubset && $this->mpdf->pdfaPart() === '1';
if ($cidSet) {
$this->writer->write('/CIDSet ' . ($this->mpdf->n + 3) . ' 0 R');
}

if ($asSubset) {
$this->writer->write('/FontFile2 ' . ($this->mpdf->n + 2) . ' 0 R');
} elseif ($font['fontkey']) {
Expand Down Expand Up @@ -470,12 +476,35 @@ public function writeFonts()
$this->writer->write('endobj');
unset($subsetter);
}

if ($cidSet) {
$this->writeCidSet(array_keys($codeToGlyph));
}
} else {
throw new \Mpdf\MpdfException(sprintf('Unsupported font type: %s (%s)', $type, $name));
}
}
}

/**
* Writes a CIDSet stream: one bit per CID from 0, the high bit of the first byte first, set for each CID the
* font program has a glyph for. CID 0 is always there, as .notdef.
*
* @param int[] $cids
*/
private function writeCidSet(array $cids)
{
$bits = str_repeat("\x00", (max($cids) >> 3) + 1);
foreach (array_merge([0], $cids) as $cid) {
$bits[$cid >> 3] = chr(ord($bits[$cid >> 3]) | (0x80 >> ($cid & 7)));
}

$this->writer->object();
$this->writer->write('<</Length ' . $this->writer->streamLength($bits) . '>>');
$this->writer->stream($bits);
$this->writer->write('endobj');
}

/**
* How much of a font the document drew, counted the way the font's own character count is.
*
Expand Down
7 changes: 5 additions & 2 deletions src/Writer/MetadataWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,10 @@ public function writeAnnotations() // _putannots
}

$fill = $this->annotationFill($pl['opt']);
$annot .= ' /C [' . preg_replace('/ (rg|g|k)$/', '', $fill) . ']';
// PDF/A-1 allows /C only with an RGB output intent; the appearance still carries the colour
if ($this->mpdf->pdfaPart() !== '1' || $this->mpdf->restrictColorSpace !== 3) {
$annot .= ' /C [' . preg_replace('/ (rg|g|k)$/', '', $fill) . ']';
}

// Kept apart, as a popup reuses $w and $h for its own size
$appearanceWidth = $w;
Expand Down Expand Up @@ -776,7 +779,7 @@ public function writeAnnotations() // _putannots

if (!$fileAttachment) {
// Subj is PDF 1.5 spec, after PDF/X-1a (PDF 1.3) and PDF/A-1 (PDF 1.4)
if (isset($pl['opt']['subj']) && !$this->mpdf->PDFX && (!$this->mpdf->PDFA || $this->mpdf->pdfaConformance()[0] !== '1')) {
if (isset($pl['opt']['subj']) && !$this->mpdf->PDFX && $this->mpdf->pdfaPart() !== '1') {
$annot .= ' /Subj ' . $this->writer->utf16BigEndianTextString($pl['opt']['subj']);
}
if ($this->writesPopup($pl)) {
Expand Down
27 changes: 27 additions & 0 deletions tests/Mpdf/Fonts/FontSubsetterCmapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ public function format6Provider()
];
}

/**
* PDF/A-1 allows a symbolic TrueType font one cmap subtable, so makeSubsetSIP can be asked to leave out the
* format 6 one under (1,0) and keep the format 4 one readers look up first
*
* @dataProvider oneCmapSubtableProvider
*/
public function testMakeSubsetSipWritesOneSubtableWhenAsked($oneCmapSubtable, array $encodings)
{
$file = $this->file('angerthas');
$program = $this->subsetter()->makeSubsetSIP($file, $this->pangram('angerthas'), 0, false, false, $oneCmapSubtable);

$this->assertSame($encodings, array_keys($this->encodingRecords($this->table($program, 'cmap'))));
}

/**
* Whether one subtable is asked for, and the encodings the cmap then lists
*
* @return mixed[][]
*/
public function oneCmapSubtableProvider()
{
return [
'both subtables' => [false, ['1,0', '3,0']],
'one subtable' => [true, ['3,0']],
];
}

/**
* The three builders share one format 4 subtable, so its bytes are pinned here directly rather
* than only through the golden masters' hashes of whole programs.
Expand Down
Loading
Loading