5 * Adobe Edge Fonts module file.
9 * Implements hook_fontyourface_api().
11 function adobe_edge_fonts_fontyourface_api() {
14 'name' => 'Adobe Edge',
19 * Implements hook_page_attachments().
21 function adobe_edge_fonts_page_attachments(&$page) {
22 $enabled_fonts = &drupal_static('fontyourface_fonts', []);
24 foreach ($enabled_fonts as $font) {
25 if ($font->pid->value == 'adobe_edge_fonts') {
29 $url = adobe_edge_fonts_generate_font_family_css($fonts);
31 $page['#attached']['html_head'][] = [
33 '#type' => 'html_tag',
38 ], 'fontyourface-adobe-edge-fonts',
44 * Implements hook_fontyourface_import().
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']);
53 $font = array_pop($context['sandbox']['fonts']);
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'];
61 drupal_set_message(t('Imported @count fonts from Adobe Edge', ['@count' => $context['sandbox']['progress']]));
67 * Generates font family css for multiple fonts.
70 * Array of FontInterface objects.
73 * URL to load fonts on page.
75 function adobe_edge_fonts_generate_font_family_css(array $fonts) {
76 $font_list = $families = [];
78 foreach ($fonts as $font) {
79 $metadata = $font->getMetadata();
80 $font_list[$font->css_family->value][] = $metadata['subset'];
82 foreach ($font_list as $key => $font) {
83 $families[] = $key . ':' . implode(',', $font);
85 if (!empty($families)) {
86 $url = 'https://use.edgefonts.net/' . implode(';', $families) . '.js';
92 * Retrieves fonts from api and parses them for consumption.
95 * List of fonts ready for ingesting as FontInterface objects.
97 function _adobe_edge_fonts_get_fonts_from_api() {
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();
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');
108 $json_fonts_results = json_decode($data);
109 return _adobe_edge_fonts_convert_api_results($json_fonts_results->families);
113 * Converts Adobe Edge Fonts json data into font-compatible format.
115 * @param array $json_fonts
116 * Fonts returned from Adobe Edge json endpoint.
119 * Array of objects compatible with Fontyourface interface.
121 function _adobe_edge_fonts_convert_api_results(array $json_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;
129 // Defaults for css style.
130 if ($css_style == 'i') {
131 $css_style = 'italic';
134 $css_style = 'normal';
137 if ($css_weight == '400') {
138 $css_weight = 'normal';
140 elseif ($css_weight == '700') {
141 $css_weight = 'bold';
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 = '';
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);
160 $json_font->contrast,
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,
171 $fonts[$font_id] = $font;
178 * Returns list of human-readable languages.
181 * List of allowed languages.
184 * List of allowed lanaguages.
186 function _adobe_edge_fonts_human_languages(array $list) {
197 'pt' => 'Portuguese',
202 foreach ($list as $item) {
203 if (isset($languages[$item])) {
204 $new_list[$item] = $languages[$item];