summaryrefslogtreecommitdiffstats
path: root/modules/fontsquirrel/fontsquirrel.module
blob: cb48b61ae70be7ad56eb4bd607c97c096e5e4cda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php

/**
 * Implements hook_fontyourface_info().
 */
function fontsquirrel_fontyourface_info() {

  return array(
    'name' => 'Font Squirrel',
    'url' => 'http://www.fontsquirrel.com/',
  );

} // fontsquirrel_fonts_api_fontyourface_info

/**
 * Implements hook_fontyourface_preview().
 */
function fontsquirrel_fontyourface_preview($font) {

  $metadata = unserialize($font->metadata);

  return '<img src="http://www.fontsquirrel.com/utils/makeFont.php?font=' . $metadata['id'] . '/' . $metadata['font_filename'] . '&width=300&size=18&text=' . urlencode($font->name) . '" />';

} // fontsquirrel_fontyourface_preview

/**
 * Implements hook_fontyourface_view().
 */
function fontsquirrel_fontyourface_view($font, $text) {

  $metadata = unserialize($font->metadata);

  $output = '<div><img src="http://www.fontsquirrel.com/utils/makeFont.php?font=' . $metadata['id'] . '/' . $metadata['font_filename'] . '&width=800&size=14&text=' . urlencode($text) . '" /></div>';

  $output .= '<div><img src="http://www.fontsquirrel.com/utils/makeSolotypeSample.php?font=' . $metadata['id'] . '/' . $metadata['font_filename'] . '&case=all" /></div>';

  return $output;

} // fontsquirrel_fontyourface_preview

/**
 * Implements template_preprocess_page().
 */
function fontsquirrel_preprocess_page(&$vars) {

  if (!empty($vars['fontyourface']) && $_GET['q'] != 'admin/build/themes/fontyourface/add') {

    $paths = array();

    foreach ($vars['fontyourface'] as $used_font) {

      if ($used_font->provider == 'fontsquirrel') {
        $metadata = unserialize($used_font->metadata);
        $paths[] = $metadata['path'];
      } // if

    } // foreach

    if (count($paths) > 0) {

      foreach ($paths as $path) {

        fontyourface_add_css_in_preprocess($vars, file_directory_path() . '/fontyourface/fontsquirrel/' . $path . '-fontfacekit/stylesheet.css');

      } // foreach

    } // if

  } // if

} // fontsquirrel_preprocess_page

/**
 * Implements hook_fontyourface_enable().
 */
function fontsquirrel_fontyourface_enable($font) {

  $success = TRUE;
  $metadata = unserialize($font->metadata);

  if ($font->css_family == '') {

    $api_result = drupal_http_request('http://www.fontsquirrel.com/api/familyinfo/' . $metadata['path']);

    if ($api_result->code == '200') {

      $decoded = json_decode($api_result->data);

      if (is_array($decoded) && is_object($decoded[0])) {

        $font->css_family = "'" . $decoded[0]->{'fontface_name'} . "'";
        fontyourface_save_font($font);

      } // if
      else {

        drupal_set_message(t('There was an error importing font information for %fontname from Font Squirrel. Font not found in API.', array('%fontname' => $font->name)), 'error');
        $success = FALSE;

      } // else

    } // if
    else {

      drupal_set_message(t('There was an error importing font information for %fontname from Font Squirrel. API not responding.', array('%fontname' => $font-name)), 'error');
      $success = FALSE;

    } // else

  } // if

  $directory_location = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_directory_path() . '/fontyourface/fontsquirrel/' . $metadata['path'] . '-fontfacekit';

  if (
    (!file_check_directory($directory_location))
  ) {

    $zip_location = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_directory_path() . '/fontyourface/fontsquirrel/' . $metadata['path'] . '-fontfacekit.zip';

    // Download file .zip file

    if (!file_exists($zip_location)) {

      $kit_url = 'http://www.fontsquirrel.com/fontfacekit/' . $metadata['path'];

      $kit_result = drupal_http_request($kit_url);

      if ($kit_result->code == 200) {

        // Save the .zip file

        if (file_check_directory(dirname($zip_location),  FILE_CREATE_DIRECTORY)) {

          file_save_data($kit_result->data, $zip_location, FILE_EXISTS_REPLACE);

        } // if
        else {

          drupal_set_message(t('There was an error saving font %fontname from Font Squirrel.', array('%fontname' => $font->name)), 'error');
          $success = FALSE;

        } // else

      } // if
      else {

        drupal_set_message(t('There was an error downloading font %fontname from Font Squirrel.', array('%fontname' => $font->name)), 'error');
        $success = FALSE;

      } // else

    } // if

    if ($success) {

      // Unzip the .zip file

      if (function_exists('zip_open') && $zip = zip_open($zip_location)) {

        file_check_directory($directory_location,  FILE_CREATE_DIRECTORY);

        while ($entry = zip_read($zip)) {

          if (zip_entry_open($zip, $entry, 'r') && ($zip_entry_filesize = zip_entry_filesize($entry))) {

            $entry_name = zip_entry_name($entry);
            $data = zip_entry_read($entry, $zip_entry_filesize);
            file_save_data($data, $directory_location .'/'. $entry_name);

          } // if

          zip_entry_close($entry);

        } // while

        zip_close($zip);

      } // if
      else {

        drupal_set_message(t('Unable to unzip font %fontname at <code>@zip_location</code>. See !zipdocs to enable unzipping, or unzip the file manually and enable this font again.', array(
          '%fontname' => $font->name,
          '@zip_location' => $zip_location,
          '!zipdocs' => l(t('PHP documentation on zip'), 'http://www.php.net/manual/en/zip.installation.php'),
        )), 'error');
        $success = FALSE;

      } // else

    } // if

    if (! $success) {
      fontyourface_disable_font($font);
    } // if

  } // if

  return $success;

} // fontsquirrel_fontyourface_enable

/**
 * Implements hook_fontyourface_import().
 */
function fontsquirrel_fontyourface_import() {

  $api_result = drupal_http_request('http://www.fontsquirrel.com/api/fontlist/all');

  if ($api_result->code == '200') {

    $decoded = json_decode($api_result->data);

    foreach ($decoded as $font_import) {

      $metadata = array(
        'id' => $font_import->id,
        'path' => $font_import->family_urlname,
        'font_filename' => $font_import->font_filename,
      );

      $font = new StdClass;
      $font->name = $font_import->family_name;
      $font->url = 'http://www.fontsquirrel.com/fonts/' . $font_import->family_urlname;
      $font->provider = 'fontsquirrel';
      $font->foundry = $font_import->foundry_name;
      $font->foundry_url = 'http://www.fontsquirrel.com/foundry/' . $font_import->foundry_urlname;
      $font->license = 'See Font Squirrel license page';
      $font->license_url = 'http://www.fontsquirrel.com/fonts/' . $font_import->family_urlname . '#eula';
      $font->tags = array($font_import->classification);
      $font->metadata = serialize($metadata);

      fontyourface_save_font($font);

    } // foreach

  } // if

} // fontsquirrel_fontyourface_import