| @@ -1,7 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Depicter\Services; |
| 3 | 3 | |
| 4 | +use Averta\Core\Utility\Arr; | |
| 5 | +use Averta\WordPress\Utility\Sanitize; | |
| 6 | + | |
| 4 | 7 | class DocumentFontsV1Service |
| 5 | 8 | { |
| 6 | 9 | /** |
| 7 | 10 | * @var array[] |
| @@ -46,14 +49,18 @@ | ||
| 46 | 49 | } |
| 47 | 50 | |
| 48 | 51 | $fontFaces = []; |
| 49 | 52 | |
| 50 | - foreach ( $this->fonts[ $documentId ][ $type ] as $fontFamily => $fontInfo ) { | |
| 51 | - | |
| 52 | - if ( $fontFamily == 'inherit' ) { | |
| 53 | + foreach ( $this->fonts[ $documentId ][ $type ] as $fontNameSlug => $fontInfo ) { | |
| 54 | + | |
| 55 | + if ( $fontNameSlug == 'inherit' ) { | |
| 53 | 56 | continue; |
| 54 | 57 | } |
| 55 | - | |
| 58 | + | |
| 59 | + if( 'google' === $type && isset( $this->fonts[ $documentId ]['local'][ $fontNameSlug ] ) ){ | |
| 60 | + continue; | |
| 61 | + } | |
| 62 | + | |
| 56 | 63 | $weights = []; |
| 57 | 64 | foreach ( $fontInfo['weights'] as $key => $weight ) { |
| 58 | 65 | $weight = 'regular' == strtolower($weight) ? '400' : $weight; |
| 59 | 66 | $weights[ $key ] = $weight; |
| @@ -88,25 +95,75 @@ | ||
| 88 | 95 | if ( empty( $fontName ) ) { |
| 89 | 96 | return; |
| 90 | 97 | } |
| 91 | 98 | |
| 92 | - $fontName = trim( $fontName ); | |
| 99 | + $fontName = str_replace( ['\"', '"'], ['', ''], trim( $fontName ) ); | |
| 100 | + $fontNameSlug = Sanitize::slug( $fontName ); | |
| 93 | 101 | $fontWeight = (array) $fontWeight; |
| 94 | 102 | |
| 95 | 103 | $this->initFontForDocument( $documentId ); |
| 96 | 104 | |
| 97 | - if ( isset( $this->fonts[ $documentId ][ $type ][ $fontName ] ) ) { | |
| 98 | - foreach ( $fontWeight as $key => $weight ) { | |
| 99 | - if ( !in_array( $weight, $this->fonts[ $documentId ][ $type ][ $fontName ]['weights'] ) ) { | |
| 100 | - $this->fonts[ $documentId ][ $type ][ $fontName ]['weights'][] = $weight; | |
| 105 | + if ( isset( $this->fonts[ $documentId ][ $type ][ $fontNameSlug ] ) ) { | |
| 106 | + foreach ( $fontWeight as $weight ) { | |
| 107 | + if ( !in_array( $weight, $this->fonts[ $documentId ][ $type ][ $fontNameSlug ]['weights'] ) ) { | |
| 108 | + $this->fonts[ $documentId ][ $type ][ $fontNameSlug ]['weights'][] = $weight; | |
| 101 | 109 | } |
| 102 | 110 | } |
| 103 | 111 | } else { |
| 104 | - $this->fonts[ $documentId ][ $type ][ $fontName ] = [ | |
| 112 | + $this->fonts[ $documentId ][ $type ][ $fontNameSlug ] = [ | |
| 113 | + 'family' => $fontName, | |
| 105 | 114 | 'face' => str_replace( ' ', '+', $fontName ), |
| 106 | 115 | 'weights' => $fontWeight |
| 107 | 116 | ]; |
| 108 | 117 | } |
| 118 | + } | |
| 119 | + | |
| 120 | + public function addLocalFont( $documentId, $fontName, $variants = [] ) { | |
| 121 | + if ( empty( $fontName ) ) { | |
| 122 | + return; | |
| 123 | + } | |
| 124 | + | |
| 125 | + $fontName = str_replace( ['\"', '"'], ['', ''], trim( $fontName ) ); | |
| 126 | + $fontNameSlug = Sanitize::slug( $fontName ); | |
| 127 | + $variants = (array) $variants; | |
| 128 | + | |
| 129 | + if ( isset( $this->fonts[ $documentId ][ 'local' ][ $fontNameSlug ] ) ) { | |
| 130 | + $this->fonts[ $documentId ][ 'local' ][ $fontNameSlug ]['variants'] = Arr::merge( $this->fonts[ $documentId ][ 'local' ][ $fontNameSlug ]['variants'], $variants ); | |
| 131 | + } else { | |
| 132 | + $this->fonts[ $documentId ][ 'local' ][ $fontNameSlug ] = [ | |
| 133 | + 'family' => $fontName, | |
| 134 | + 'variants' => $variants | |
| 135 | + ]; | |
| 136 | + } | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Generate Css for loading local css fonts | |
| 141 | + * | |
| 142 | + * @param $documentId | |
| 143 | + * | |
| 144 | + * @return string | |
| 145 | + */ | |
| 146 | + public function getLocalFontsCss( $documentId ){ | |
| 147 | + $fontsCss = ""; | |
| 148 | + | |
| 149 | + if( ! empty( $this->fonts[ $documentId ][ 'local' ] ) && is_array( $this->fonts[ $documentId ][ 'local' ] ) ){ | |
| 150 | + foreach( $this->fonts[ $documentId ][ 'local' ] as $fontInfo ){ | |
| 151 | + if( is_array( $fontInfo['variants'] ) ){ | |
| 152 | + foreach( $fontInfo['variants'] as $fontVariant ){ | |
| 153 | + $fontsCss .= "@font-face{\n"; | |
| 154 | + $fontsCss .= !empty( $fontInfo['family'] ) ? "\tfont-family:\"" . $fontInfo['family'] ."\";\n" : ""; | |
| 155 | + $fontsCss .= !empty( $fontVariant['style'] ) ? "\tfont-style:" . $fontVariant['style'] .";\n" : ""; | |
| 156 | + $fontsCss .= !empty( $fontVariant['weight'] ) ? "\tfont-weight:" . $fontVariant['weight'] .";\n" : ""; | |
| 157 | + $fontsCss .= "\tfont-display:fallback;\n"; | |
| 158 | + $fontsCss .= !empty( $fontVariant['src'] ) ? "\tsrc:". str_replace( '"', "'", $fontVariant['src'] ) .";\n" : ""; | |
| 159 | + $fontsCss .= "\tfont-stretch:normal;\n}\n"; | |
| 160 | + } | |
| 161 | + } | |
| 162 | + } | |
| 163 | + } | |
| 164 | + | |
| 165 | + return $fontsCss; | |
| 109 | 166 | } |
| 110 | 167 | |
| 111 | 168 | protected function initFontForDocument( $documentId ){ |
| 112 | 169 | if( ! isset( $this->fonts[ $documentId ] ) ){ |