5a859a0531262f49638115ae7dece74fecc1de64
[yaffs-website] / web / modules / contrib / fontyourface / modules / adobe_edge_fonts / adobe_edge_fonts.module
1 <?php
2
3 /**
4  * @file
5  * Adobe Edge Fonts module file.
6  */
7
8 /**
9  * Implements hook_fontyourface_api().
10  */
11 function adobe_edge_fonts_fontyourface_api() {
12   return [
13     'version' => '3',
14     'name' => 'Adobe Edge',
15   ];
16 }
17
18 /**
19  * Implements hook_page_attachments().
20  */
21 function adobe_edge_fonts_page_attachments(&$page) {
22   $enabled_fonts = &drupal_static('fontyourface_fonts', []);
23   $fonts = [];
24   foreach ($enabled_fonts as $font) {
25     if ($font->pid->value == 'adobe_edge_fonts') {
26       $fonts[] = $font;
27     }
28   }
29   $url = adobe_edge_fonts_generate_font_family_css($fonts);
30   if (!empty($url)) {
31     $page['#attached']['html_head'][] = [
32       [
33         '#type' => 'html_tag',
34         '#tag' => 'script',
35         '#attributes' => [
36           'src' => $url,
37         ],
38       ], 'fontyourface-adobe-edge-fonts',
39     ];
40   }
41 }
42
43 /**
44  * Implements hook_fontyourface_import().
45  */
46 function adobe_edge_fonts_fontyourface_import($font_context = []) {
47   $context = $font_context;
48   if (empty($context['sandbox'])) {
49     $context['sandbox']['fonts'] = _adobe_edge_fonts_get_fonts_from_api();
50     $context['sandbox']['progress'] = 0;
51     $context['sandbox']['max'] = count($context['sandbox']['fonts']);
52   }
53   $font = array_pop($context['sandbox']['fonts']);
54   if (!empty($font)) {
55     fontyourface_save_font($font);
56     $context['message'] = "Imported {$context['sandbox']['progress']} of {$context['sandbox']['max']}";
57     $context['sandbox']['progress'] = $context['sandbox']['max'] - count($context['sandbox']['fonts']);
58     $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
59   }
60   else {
61     drupal_set_message(t('Imported @count fonts from Adobe Edge', ['@count' => $context['sandbox']['progress']]));
62   }
63   return $context;
64 }
65
66 /**
67  * Generates font family css for multiple fonts.
68  *
69  * @param array $fonts
70  *   Array of FontInterface objects.
71  *
72  * @return string
73  *   URL to load fonts on page.
74  */
75 function adobe_edge_fonts_generate_font_family_css(array $fonts) {
76   $font_list = $families = [];
77   $url = '';
78   foreach ($fonts as $font) {
79     $metadata = $font->getMetadata();
80     $font_list[$font->css_family->value][] = $metadata['subset'];
81   }
82   foreach ($font_list as $key => $font) {
83     $families[] = $key . ':' . implode(',', $font);
84   }
85   if (!empty($families)) {
86     $url = 'https://use.edgefonts.net/' . implode(';', $families) . '.js';
87   }
88   return $url;
89 }
90
91 /**
92  * Retrieves fonts from api and parses them for consumption.
93  *
94  * @return array
95  *   List of fonts ready for ingesting as FontInterface objects.
96  */
97 function _adobe_edge_fonts_get_fonts_from_api() {
98   try {
99     $uri = 'https://edgewebfonts.adobe.com/data/fontData.json';
100     $response = \Drupal::httpClient()->get($uri, ['headers' => ['Accept' => 'text/plain'], 'verify' => FALSE]);
101     $data = (string) $response->getBody();
102   }
103   catch (RequestException $e) {
104     drupal_set_message(t('The list of Adobe Edge Fonts could not be fetched. Verify that your server can connect the Adobe Edge Servers (https://edgewebfonts.adobe.com). Error: %error', ['%error' => $e->error]), 'error');
105     return FALSE;
106   }
107
108   $json_fonts_results = json_decode($data);
109   return _adobe_edge_fonts_convert_api_results($json_fonts_results->families);
110 }
111
112 /**
113  * Converts Adobe Edge Fonts json data into font-compatible format.
114  *
115  * @param array $json_fonts
116  *   Fonts returned from Adobe Edge json endpoint.
117  *
118  * @return array
119  *   Array of objects compatible with Fontyourface interface.
120  */
121 function _adobe_edge_fonts_convert_api_results(array $json_fonts) {
122   $fonts = [];
123   foreach ($json_fonts as $json_font) {
124     foreach ($json_font->variations as $json_font_variant) {
125       $font_id = $json_font->name . ' ' . $json_font_variant->name;
126       $css_style = substr($json_font_variant->fvd, 0, 1);
127       $css_weight = substr($json_font_variant->fvd, 1, 1) * 100;
128
129       // Defaults for css style.
130       if ($css_style == 'i') {
131         $css_style = 'italic';
132       }
133       else {
134         $css_style = 'normal';
135       }
136
137       if ($css_weight == '400') {
138         $css_weight = 'normal';
139       }
140       elseif ($css_weight == '700') {
141         $css_weight = 'bold';
142       }
143
144       $font = new stdClass();
145       $font->name = $font_id;
146       $font->url = 'https://edgewebfonts.adobe.com/?font=' . $json_font->slug . '&fvd=' . $json_font_variant->fvd;
147       $font->provider = 'adobe_edge_fonts';
148       $font->css_family = $json_font->slug;
149       $font->css_style = $css_style;
150       $font->css_weight = $css_weight;
151       $font->designer = '';
152       $font->designer_url = '';
153       $font->foundry = '';
154       $font->foundry_url = '';
155       $font->license = 'Font license';
156       $font->license_url = $json_font_variant->eula;
157       $font->classification = $json_font->classification;
158       $font->language = _adobe_edge_fonts_human_languages($json_font->language);
159       $font->tags = [
160         $json_font->contrast,
161       ];
162       $font->metadata = [
163         'id' => $json_font->id,
164         'subset' => $json_font_variant->fvd,
165         'capitals' => $json_font->capitals,
166         'classification' => $json_font->classification,
167         'contrast' => $json_font->contrast,
168         'language' => $json_font->language,
169         'number_style' => $json_font->number_style,
170       ];
171       $fonts[$font_id] = $font;
172     }
173   }
174   return $fonts;
175 }
176
177 /**
178  * Returns list of human-readable languages.
179  *
180  * @param array $list
181  *   List of allowed languages.
182  *
183  * @return array
184  *   List of allowed lanaguages.
185  */
186 function _adobe_edge_fonts_human_languages(array $list) {
187   $languages = [
188     'ca' => 'Catalan',
189     'cs' => 'Czech',
190     'de' => 'German',
191     'en' => 'English',
192     'es' => 'Spanish',
193     'fr' => 'French',
194     'it' => 'Italian',
195     'nl' => 'Dutch',
196     'pl' => 'Polish',
197     'pt' => 'Portuguese',
198     'ru' => 'Russian',
199     'sv' => 'Swedish',
200   ];
201   $new_list = [];
202   foreach ($list as $item) {
203     if (isset($languages[$item])) {
204       $new_list[$item] = $languages[$item];
205     }
206   }
207   return $new_list;
208 }