PluginProbe
Depicter — Popup & Slider Builder / 1.5.2
Depicter — Popup & Slider Builder v1.5.2
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 / DocumentFontsService.php

DocumentFontsService.php in Depicter — Popup & Slider Builder 1.5.2, at app/src/Services/DocumentFontsService.php

157 lines 4.3 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 class DocumentFontsService
5 {
6 /**
7 * @var array[]
8 */
9 private $fonts = [];
10
11 /**
12 * Get font families
13 *
14 * @param int $documentId The document ID to get belonging faces
15 * @param string $type Repository type of the font. `google`, `typescript`, ..
16 *
17 * @return int[]|string[]
18 */
19 public function getFamilies( $documentId, $type = 'google' ) {
20 return array_keys( $this->getFontsInfo( $documentId, $type ) );
21 }
22
23 /**
24 * Get collected fonts list
25 *
26 * @param int $documentId The document ID to get belonging faces
27 * @param string $type Repository type of the font. `google`, `typescript`, ..
28 *
29 * @return int[]|string[]
30 */
31 public function getFontsInfo( $documentId, $type = 'google' ) {
32 return ! empty( $this->fonts[ $documentId ][ $type ] ) ? $this->fonts[ $documentId ][ $type ] : [];
33 }
34
35 /**
36 * Get font faces
37 *
38 * @param int $documentId The document ID to get belonging faces
39 * @param string $type Repository type of the font. `google`, `typescript`, ..
40 *
41 * @return array
42 */
43 public function getFontsLinkQuery( $documentId, $type = 'google' ) {
44 if( empty( $this->fonts[ $documentId ][ $type ] ) ){
45 return [];
46 }
47
48 $fontFaces = [];
49
50 foreach ( $this->fonts[ $documentId ][ $type ] as $fontFamily => $fontInfo ) {
51 $weights = [];
52 foreach ( $fontInfo['weights'] as $weight ) {
53 $weight = 'regular' == strtolower($weight) ? '400' : $weight;
54
55 // lets convert 300italic to 1,300
56 if (strpos($weight, 'italic')) {
57 $weightNumber = str_replace('italic', '', $weight);
58 $italicNumber = '1'; // 0 means normal and 1 is italic
59 } else {
60 $weightNumber = $weight;
61 $italicNumber = '0'; // 0 means normal and 1 is italic
62 }
63 $weights[$weightNumber] = $italicNumber;
64 }
65
66 // Sort weight number ascending
67 ksort( $weights );
68
69 // Make weight query string for current font
70 $weightQuery = '';
71 foreach ( $weights as $weightNumber => $italicNumber ){
72 $weightQuery .= "{$italicNumber},{$weightNumber};";
73 }
74 $weightQuery = rtrim($weightQuery, ';');
75
76 // Collect font name and weights in a query string
77 $fontFaces[] = $fontInfo['face'] . ':ital,wght@' . $weightQuery;
78 }
79
80 return $fontFaces;
81 }
82
83 /**
84 * Add a font to fonts list
85 *
86 * @param int $documentId The document ID that the font belongs to
87 * @param string $fontName Font family name
88 * @param string|array $fontWeight Font weight
89 * @param string $type Repository type of the font. `google`, `typescript`, ..
90 */
91 public function addFont( $documentId, $fontName, $fontWeight, $type = 'google' ) {
92 if ( empty( $fontName ) ) {
93 return;
94 }
95
96 $fontName = trim( $fontName );
97 $fontWeight = (array) $fontWeight;
98
99 $this->initFontForDocument( $documentId );
100
101 if ( isset( $this->fonts[ $documentId ][ $type ][ $fontName ] ) ) {
102 foreach ( $fontWeight as $key => $weight ) {
103 if ( !in_array( $weight, $this->fonts[ $documentId ][ $type ][ $fontName ]['weights'] ) ) {
104 $this->fonts[ $documentId ][ $type ][ $fontName ]['weights'][] = $weight;
105 }
106 }
107 } else {
108 $this->fonts[ $documentId ][ $type ][ $fontName ] = [
109 'face' => str_replace( ' ', '+', $fontName ),
110 'weights' => $fontWeight
111 ];
112 }
113 }
114
115 protected function initFontForDocument( $documentId ){
116 if( ! isset( $this->fonts[ $documentId ] ) ){
117 $this->fonts[ $documentId ] = [
118 'google' => []
119 ];
120 }
121 }
122
123 /**
124 * Add list of fonts to fonts list
125 *
126 * @param int $documentId The document ID that fonts belong to
127 * @param array $fontList
128 * @param string $type
129 */
130 public function addFonts( $documentId, $fontList, $type = 'google' ) {
131 if ( empty( $fontList ) ) {
132 return;
133 }
134
135 foreach ( $fontList as $fontName => $fontWeight ){
136 $this->addFont( $documentId, $fontName, $fontWeight, $type );
137 }
138 }
139
140 /**
141 * Get fonts load link
142 *
143 * @param int $documentId The document ID to get belonging faces
144 * @param string $type Repository type of the font. `google`, `typescript`, ..
145 *
146 * @return bool|string
147 */
148 public function getFontsLink( $documentId, $type = 'google' ) {
149 if ( $fontsQuery = $this->getFontsLinkQuery( $documentId, $type = 'google' ) ) {
150 $fontsQuery = implode( "&family=", $fontsQuery );
151 return '//fonts.googleapis.com/css2?family=' . $fontsQuery . '&display=swap';
152 }
153
154 return '';
155 }
156 }
157