1<?php
2
3namespace Mpdf\Writer;
4
5use Mpdf\Strict;
6use Mpdf\Mpdf;
7
8final class OptionalContentWriter
9{
10
11	use Strict;
12
13	/**
14	 * @var \Mpdf\Mpdf
15	 */
16	private $mpdf;
17
18	/**
19	 * @var \Mpdf\Writer\BaseWriter
20	 */
21	private $writer;
22
23	public function __construct(Mpdf $mpdf, BaseWriter $writer)
24	{
25		$this->mpdf = $mpdf;
26		$this->writer = $writer;
27	}
28
29	public function writeOptionalContentGroups() // _putocg Optional Content Groups
30	{
31		if ($this->mpdf->hasOC) {
32
33			$this->writer->object();
34			$this->mpdf->n_ocg_print = $this->mpdf->n;
35			$this->writer->write('<</Type /OCG /Name ' . $this->writer->string('Print only'));
36			$this->writer->write('/Usage <</Print <</PrintState /ON>> /View <</ViewState /OFF>>>>>>');
37			$this->writer->write('endobj');
38
39			$this->writer->object();
40			$this->mpdf->n_ocg_view = $this->mpdf->n;
41			$this->writer->write('<</Type /OCG /Name ' . $this->writer->string('Screen only'));
42			$this->writer->write('/Usage <</Print <</PrintState /OFF>> /View <</ViewState /ON>>>>>>');
43			$this->writer->write('endobj');
44
45			$this->writer->object();
46			$this->mpdf->n_ocg_hidden = $this->mpdf->n;
47			$this->writer->write('<</Type /OCG /Name ' . $this->writer->string('Hidden'));
48			$this->writer->write('/Usage <</Print <</PrintState /OFF>> /View <</ViewState /OFF>>>>>>');
49			$this->writer->write('endobj');
50		}
51
52		if (count($this->mpdf->layers)) {
53
54			ksort($this->mpdf->layers);
55			foreach ($this->mpdf->layers as $id => $layer) {
56				$this->writer->object();
57				$this->mpdf->layers[$id]['n'] = $this->mpdf->n;
58
59				if (isset($this->mpdf->layerDetails[$id]['name']) && $this->mpdf->layerDetails[$id]['name']) {
60					$name = $this->mpdf->layerDetails[$id]['name'];
61				} else {
62					$name = $layer['name'];
63				}
64
65				$this->writer->write('<</Type /OCG /Name ' . $this->writer->utf16BigEndianTextString($name) . '>>');
66				$this->writer->write('endobj');
67			}
68		}
69	}
70
71}
72