PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / DocumentFontsV1Service.php

DocumentFontsV1Service.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/DocumentFontsV1Service.php

237 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Services;
3
4 use Averta\Core\Utility\Arr;
5 use Averta\WordPress\Utility\Sanitize;
6
7 class DocumentFontsV1Service
8 {
9 /**
10 * @var array[]
11 */
12 private $fonts = [];
13
14 /**
15 * Get font families
16 *
17 * @param int $documentId The document ID to get belonging faces
18 * @param string $type Repository type of the font. `google`, `typescript`, ..
19 *
20 * @return int[]|string[]
21 */
22 public function getFamilies( $documentId, $type = 'google' ) {
23 return array_keys( $this->getFontsInfo( $documentId, $type ) );
24 }
25
26 /**
27 * Get collected fonts list
28 *
29 * @param int $documentId The document ID to get belonging faces
30 * @param string $type Repository type of the font. `google`, `typescript`, ..
31 *
32 * @return int[]|string[]
33 */
34 public function getFontsInfo( $documentId, $type = 'google' ) {
35 return ! empty( $this->fonts[ $documentId ][ $type ] ) ? $this->fonts[ $documentId ][ $type ] : [];
36 }
37
38 /**
39 * Get font faces
40 *
41 * @param int $documentId The document ID to get belonging faces
42 * @param string $type Repository type of the font. `google`, `typescript`, ..
43 *
44 * @return array
45 */
46 public function getFontsLinkQuery( $documentId, $type = 'google' ) {
47 if( empty( $this->fonts[ $documentId ][ $type ] ) ){
48 return [];
49 }
50
51 $fontFaces = [];
52
53 foreach ( $this->fonts[ $documentId ][ $type ] as $fontNameSlug => $fontInfo ) {
54
55 if ( $fontNameSlug == 'inherit' ) {
56 continue;
57 }
58
59 if( 'google' === $type && isset( $this->fonts[ $documentId ]['local'][ $fontNameSlug ] ) ){
60 continue;
61 }
62
63 $weights = [];
64 foreach ( $fontInfo['weights'] as $key => $weight ) {
65 $weight = 'regular' == strtolower($weight) ? '400' : $weight;
66 $weights[ $key ] = $weight;
67 }
68
69 // Sort weight number ascending
70 ksort( $weights );
71
72 // Make weight query string for current font
73 $weightQuery = '';
74 foreach ( $weights as $weightKey => $weight ){
75 $weightQuery .= $weight . ',';
76 }
77 $weightQuery = rtrim($weightQuery, ',');
78
79 // Collect font name and weights in a query string
80 $fontFaces[] = $fontInfo['face'] . ':' . $weightQuery;
81 }
82
83 return $fontFaces;
84 }
85
86 /**
87 * Add a font to fonts list
88 *
89 * @param int $documentId The document ID that the font belongs to
90 * @param string $fontName Font family name
91 * @param string|array $fontWeight Font weight
92 * @param string $type Repository type of the font. `google`, `typescript`, ..
93 */
94 public function addFont( $documentId, $fontName, $fontWeight, $type = 'google' ) {
95 if ( empty( $fontName ) ) {
96 return;
97 }
98
99 $systemFonts = $this->systemFonts();
100 if ( in_array( $fontName, $systemFonts ) ) {
101 $type = 'system';
102 }
103
104 $fontName = str_replace( ['\"', '"'], ['', ''], trim( $fontName ) );
105 $fontNameSlug = Sanitize::slug( $fontName );
106 $fontWeight = (array) $fontWeight;
107
108 $this->initFontForDocument( $documentId );
109
110 if ( isset( $this->fonts[ $documentId ][ $type ][ $fontNameSlug ] ) ) {
111 foreach ( $fontWeight as $weight ) {
112 if ( !in_array( $weight, $this->fonts[ $documentId ][ $type ][ $fontNameSlug ]['weights'] ) ) {
113 $this->fonts[ $documentId ][ $type ][ $fontNameSlug ]['weights'][] = $weight;
114 }
115 }
116 } else {
117 $this->fonts[ $documentId ][ $type ][ $fontNameSlug ] = [
118 'family' => $fontName,
119 'face' => str_replace( ' ', '+', $fontName ),
120 'weights' => $fontWeight
121 ];
122 }
123 }
124
125 public function addLocalFont( $documentId, $fontName, $variants = [] ) {
126 if ( empty( $fontName ) ) {
127 return;
128 }
129
130 $fontName = str_replace( ['\"', '"'], ['', ''], trim( $fontName ) );
131 $fontNameSlug = Sanitize::slug( $fontName );
132 $variants = (array) $variants;
133
134 if ( isset( $this->fonts[ $documentId ][ 'local' ][ $fontNameSlug ] ) ) {
135 $this->fonts[ $documentId ][ 'local' ][ $fontNameSlug ]['variants'] = Arr::merge( $this->fonts[ $documentId ][ 'local' ][ $fontNameSlug ]['variants'], $variants );
136 } else {
137 $this->fonts[ $documentId ][ 'local' ][ $fontNameSlug ] = [
138 'family' => $fontName,
139 'variants' => $variants
140 ];
141 }
142 }
143
144 /**
145 * Generate Css for loading local css fonts
146 *
147 * @param $documentId
148 *
149 * @return string
150 */
151 public function getLocalFontsCss( $documentId ){
152 $fontsCss = "";
153
154 if( ! empty( $this->fonts[ $documentId ][ 'local' ] ) && is_array( $this->fonts[ $documentId ][ 'local' ] ) ){
155 foreach( $this->fonts[ $documentId ][ 'local' ] as $key => $fontInfo ){
156 // Generate font face css style only for local/website fonts which used in the document
157 if ( ! empty( $this->fonts[ $documentId ]['google'] ) && ! in_array( $key, array_keys( $this->fonts[ $documentId ]['google'] ) ) ) {
158 continue;
159 }
160
161 if( is_array( $fontInfo['variants'] ) ){
162 foreach( $fontInfo['variants'] as $fontVariant ){
163 $fontsCss .= "@font-face{\n";
164 $fontsCss .= !empty( $fontInfo['family'] ) ? "\tfont-family:\"" . $fontInfo['family'] ."\";\n" : "";
165 $fontsCss .= !empty( $fontVariant['style'] ) ? "\tfont-style:" . $fontVariant['style'] .";\n" : "";
166 $fontsCss .= !empty( $fontVariant['weight'] ) ? "\tfont-weight:" . $fontVariant['weight'] .";\n" : "";
167 $fontsCss .= "\tfont-display:fallback;\n";
168 $fontsCss .= !empty( $fontVariant['src'] ) ? "\tsrc:". str_replace( '"', "'", $fontVariant['src'] ) .";\n" : "";
169 $fontsCss .= "\tfont-stretch:normal;\n}\n";
170 }
171 }
172 }
173 }
174
175 return $fontsCss;
176 }
177
178 protected function initFontForDocument( $documentId ){
179 if( ! isset( $this->fonts[ $documentId ] ) ){
180 $this->fonts[ $documentId ] = [
181 'google' => []
182 ];
183 }
184 }
185
186 /**
187 * Add list of fonts to fonts list
188 *
189 * @param int $documentId The document ID that fonts belong to
190 * @param array $fontList
191 * @param string $type
192 */
193 public function addFonts( $documentId, $fontList, $type = 'google' ) {
194 if ( empty( $fontList ) ) {
195 return;
196 }
197
198 foreach ( $fontList as $fontName => $fontWeight ){
199 $this->addFont( $documentId, $fontName, $fontWeight, $type );
200 }
201 }
202
203 /**
204 * Get system Fonts
205 *
206 * @return array
207 */
208 public function systemFonts() {
209 return [
210 'Arial',
211 'Helvetica',
212 'Times New Roman',
213 'Georgia',
214 'Courier New',
215 'Verdana',
216 'Tahoma'
217 ];
218 }
219
220 /**
221 * Get fonts load link
222 *
223 * @param int $documentId The document ID to get belonging faces
224 * @param string $type Repository type of the font. `google`, `typescript`, ..
225 *
226 * @return bool|string
227 */
228 public function getFontsLink( $documentId, $type = 'google' ) {
229 if ( $fontsQuery = $this->getFontsLinkQuery( $documentId, $type = 'google' ) ) {
230 $fontsQuery = implode( "|", $fontsQuery );
231 return 'https://fonts.googleapis.com/css?family=' . $fontsQuery . '&display=swap';
232 }
233
234 return '';
235 }
236 }
237