Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DefaultText
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 render
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 drawTitle
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
30
 drawAuthor
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Default cover text layer
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Cover_Generator
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
28 */
29
30namespace VuFind\Cover\Layer;
31
32use function count;
33
34/**
35 * Default cover text layer
36 *
37 * @category VuFind
38 * @package  Cover_Generator
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
42 */
43class DefaultText extends AbstractTextLayer
44{
45    /**
46     * Render the layer
47     *
48     * @param resource $im       Image resource to draw on
49     * @param array    $details  Cover details array (with title/author/call_number)
50     * @param object   $settings Settings object
51     *
52     * @return void
53     */
54    public function render($im, $details, $settings)
55    {
56        if (null !== $details['title']) {
57            $lineHeight = $settings->height / 8;
58            $this->drawTitle($im, $settings, $details['title'], $lineHeight);
59        }
60        if (null !== $details['author']) {
61            $this->drawAuthor($im, $settings, $details['author']);
62        }
63    }
64
65    /**
66     * Render title in wrapped, black text with white border
67     *
68     * @param resource $im         Image resource to draw on
69     * @param object   $settings   Settings object
70     * @param string   $title      Title to write
71     * @param int      $lineHeight Pixels we move down each line
72     *
73     * @return void
74     */
75    protected function drawTitle($im, $settings, $title, $lineHeight)
76    {
77        $titleFillColor = $this->getColor($im, $settings->titleFillColor);
78        $titleBorderColor = $this->getColor($im, $settings->titleBorderColor);
79        $words = explode(' ', $title);
80        // Wrap words into image
81        // Add words until off image, go back and print
82        $line = '';
83        $lineCount = 0;
84        $i = 0;
85        while (
86            $i < count($words)
87            && $lineCount < $settings->maxTitleLines - 1
88        ) {
89            $pline = $line;
90            // Format
91            $text = $words[$i];
92            $line .= $text . ' ';
93            $textWidth = $this->textWidth(
94                rtrim($line, ' '),
95                $settings->titleFont,
96                $settings->titleFontSize
97            );
98            if ($textWidth > $settings->wrapWidth) {
99                // Print black with white border
100                $this->drawText(
101                    $im,
102                    $settings,
103                    rtrim($pline, ' '),
104                    $settings->topPadding + $lineHeight * $lineCount,
105                    $settings->titleFont,
106                    $settings->titleFontSize,
107                    $titleFillColor,
108                    $titleBorderColor
109                );
110                $line = $text . ' ';
111                $lineCount++;
112            }
113            $i++;
114        }
115        // Print the last words
116        $this->drawText(
117            $im,
118            $settings,
119            rtrim($line, ' '),
120            $settings->topPadding + $lineHeight * $lineCount,
121            $settings->titleFont,
122            $settings->titleFontSize,
123            $titleFillColor,
124            $titleBorderColor
125        );
126        // Add ellipses if we've truncated
127        if ($i < count($words) - 1) {
128            $this->drawText(
129                $im,
130                $settings,
131                '...',
132                $settings->topPadding + $settings->maxTitleLines * $lineHeight,
133                $settings->titleFont,
134                $settings->titleFontSize + 1,
135                $titleFillColor,
136                $titleBorderColor
137            );
138        }
139    }
140
141    /**
142     * Render author at bottom in wrapped, white text with black border
143     *
144     * @param resource $im       Image resource to draw on
145     * @param object   $settings Settings object
146     * @param string   $author   Author to write
147     *
148     * @return void
149     */
150    protected function drawAuthor($im, $settings, $author)
151    {
152        $authorFillColor = $this->getColor($im, $settings->authorFillColor);
153        $authorBorderColor = $this->getColor($im, $settings->authorBorderColor);
154        // Scale author to fit by incrementing fontsizes down
155        $fontSize = $settings->authorFontSize + 1;
156        do {
157            $fontSize--;
158            $textWidth = $this->textWidth(
159                $author,
160                $settings->authorFont,
161                $fontSize
162            );
163        } while (
164            $textWidth > $settings->wrapWidth &&
165              $fontSize > $settings->minAuthorFontSize
166        );
167        // Too small to read? Align left
168        $textWidth = $this->textWidth(
169            $author,
170            $settings->authorFont,
171            $fontSize
172        );
173        $align = $textWidth > $settings->width ? 'left' : null;
174        $this->drawText(
175            $im,
176            $settings,
177            $author,
178            $settings->height - $settings->bottomPadding,
179            $settings->authorFont,
180            $fontSize,
181            $authorFillColor,
182            $authorBorderColor,
183            $align
184        );
185    }
186}