Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractTextLayer
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 textWidth
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 textHeight
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 drawText
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3/**
4 * Abstract 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
32/**
33 * Abstract cover text layer
34 *
35 * @category VuFind
36 * @package  Cover_Generator
37 * @author   Demian Katz <demian.katz@villanova.edu>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
40 */
41abstract class AbstractTextLayer extends AbstractLayer
42{
43    /**
44     * Returns the width a string would render to
45     *
46     * @param string $text Text to test
47     * @param string $font Full font path
48     * @param string $size Size of the font
49     *
50     * @return int
51     */
52    protected function textWidth($text, $font, $size)
53    {
54        $p = imagettfbbox($size, 0, $font, $text);
55        return $p[2] - $p[0];
56    }
57
58    /**
59     * Returns the height a string would render to
60     *
61     * @param string $text Text to test
62     * @param string $font Full font path
63     * @param string $size Size of the font
64     *
65     * @return int
66     */
67    protected function textHeight($text, $font, $size)
68    {
69        $p = imagettfbbox($size, 0, $font, $text);
70        return $p[1] - $p[5];
71    }
72
73    /**
74     * Simulate outlined text
75     *
76     * @param resource $im       Active image resource
77     * @param object   $settings Generator settings object
78     * @param string   $text     Text to render
79     * @param int      $y        Top position
80     * @param string   $font     Full path to font
81     * @param int      $fontSize Size of the font
82     * @param int      $mcolor   Main text color
83     * @param int      $scolor   Secondary border color
84     * @param string   $align    'left','center','right'
85     *
86     * @return void
87     */
88    protected function drawText(
89        $im,
90        $settings,
91        $text,
92        $y,
93        $font,
94        $fontSize,
95        $mcolor,
96        $scolor = false,
97        $align = null
98    ) {
99        // In case the text contains non-normalized UTF-8, fix that for proper
100        // display:
101        $text = \Normalizer::normalize($text);
102
103        // If the wrap width is smaller than the image width, we want to account
104        // for this when right or left aligning to maintain padding on the image.
105        $wrapGap = (int)(($settings->width - $settings->wrapWidth) / 2);
106
107        $textWidth = $this->textWidth($text, $font, $fontSize);
108        if ($textWidth > $settings->width) {
109            $align = 'left';
110            $wrapGap = 0; // kill wrap gap to maximize text fit
111        }
112        switch ($align ?? $settings->textAlign) {
113            case 'left':
114                $x = $wrapGap;
115                break;
116            case 'right':
117                $x = $settings->width - ($textWidth + $wrapGap);
118                break;
119            case 'center':
120            default:
121                $x = (int)(($settings->width - $textWidth) / 2);
122        }
123
124        // Generate 5 lines of text, 4 offset in a border color
125        if ($scolor) {
126            imagettftext($im, $fontSize, 0, $x, $y + 1, $scolor, $font, $text);
127            imagettftext($im, $fontSize, 0, $x, $y - 1, $scolor, $font, $text);
128            imagettftext($im, $fontSize, 0, $x + 1, $y, $scolor, $font, $text);
129            imagettftext($im, $fontSize, 0, $x - 1, $y, $scolor, $font, $text);
130        }
131        // 1 centered in main color
132        imagettftext($im, $fontSize, 0, $x, (int)$y, $mcolor, $font, $text);
133    }
134}