Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Resources
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
4 / 4
19
100.00% covered (success)
100.00%
1 / 1
 addFont
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addXObject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addExtGState
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toPdf
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
1 / 1
16
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Content;
6
7use Phpdftk\Pdf\Core\PdfArray;
8use Phpdftk\Pdf\Core\PdfDictionary;
9use Phpdftk\Pdf\Core\PdfName;
10use Phpdftk\Pdf\Core\PdfReference;
11use Phpdftk\Pdf\Core\Serializable;
12
13/**
14 * Resource dictionary used by Page and FormXObject.
15 * Maps resource names to their referenced objects.
16 */
17class Resources implements Serializable
18{
19    /** @var array<string, PdfReference> */
20    public array $extGState = [];   // /ExtGState
21    /** @var array<string, PdfReference> */
22    public array $colorSpace = [];  // /ColorSpace
23    /** @var array<string, PdfReference> */
24    public array $pattern = [];     // /Pattern
25    /** @var array<string, PdfReference> */
26    public array $shading = [];     // /Shading
27    /** @var array<string, PdfReference> */
28    public array $xObject = [];     // /XObject
29    /** @var array<string, PdfReference> */
30    public array $font = [];        // /Font - key => resource name, value => PdfReference
31    /** @var array<int, string> */
32    public array $procSet = [];     // /ProcSet
33    /** @var array<string, PdfReference> */
34    public array $properties = [];  // /Properties
35
36    public function addFont(string $name, PdfReference $ref): void
37    {
38        $this->font[$name] = $ref;
39    }
40
41    public function addXObject(string $name, PdfReference $ref): void
42    {
43        $this->xObject[$name] = $ref;
44    }
45
46    public function addExtGState(string $name, PdfReference $ref): void
47    {
48        $this->extGState[$name] = $ref;
49    }
50
51    public function toPdf(): string
52    {
53        $dict = new PdfDictionary();
54
55        // /ProcSet
56        if (!empty($this->procSet)) {
57            $procItems = array_map(fn($p) => new PdfName($p), $this->procSet);
58            $dict->set('ProcSet', new PdfArray($procItems));
59        } else {
60            // Default ProcSet for most documents
61            $dict->set('ProcSet', new PdfArray([
62                new PdfName('PDF'),
63                new PdfName('Text'),
64                new PdfName('ImageB'),
65                new PdfName('ImageC'),
66                new PdfName('ImageI'),
67            ]));
68        }
69
70        // /Font
71        if (!empty($this->font)) {
72            $fontDict = new PdfDictionary();
73            foreach ($this->font as $name => $ref) {
74                $fontDict->set($name, $ref);
75            }
76            $dict->set('Font', $fontDict);
77        }
78
79        // /XObject
80        if (!empty($this->xObject)) {
81            $xoDict = new PdfDictionary();
82            foreach ($this->xObject as $name => $ref) {
83                $xoDict->set($name, $ref);
84            }
85            $dict->set('XObject', $xoDict);
86        }
87
88        // /ExtGState
89        if (!empty($this->extGState)) {
90            $gsDict = new PdfDictionary();
91            foreach ($this->extGState as $name => $ref) {
92                $gsDict->set($name, $ref);
93            }
94            $dict->set('ExtGState', $gsDict);
95        }
96
97        // /ColorSpace
98        if (!empty($this->colorSpace)) {
99            $csDict = new PdfDictionary();
100            foreach ($this->colorSpace as $name => $ref) {
101                $csDict->set($name, $ref);
102            }
103            $dict->set('ColorSpace', $csDict);
104        }
105
106        // /Pattern
107        if (!empty($this->pattern)) {
108            $patDict = new PdfDictionary();
109            foreach ($this->pattern as $name => $ref) {
110                $patDict->set($name, $ref);
111            }
112            $dict->set('Pattern', $patDict);
113        }
114
115        // /Shading
116        if (!empty($this->shading)) {
117            $shadDict = new PdfDictionary();
118            foreach ($this->shading as $name => $ref) {
119                $shadDict->set($name, $ref);
120            }
121            $dict->set('Shading', $shadDict);
122        }
123
124        // /Properties
125        if (!empty($this->properties)) {
126            $propDict = new PdfDictionary();
127            foreach ($this->properties as $name => $ref) {
128                $propDict->set($name, $ref);
129            }
130            $dict->set('Properties', $propDict);
131        }
132
133        return $dict->toPdf();
134    }
135}