edge_fonts.module 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @file
  4. * Edge Fonts module main file.
  5. */
  6. /**
  7. * Implements hook_fontyourface_info().
  8. */
  9. function edge_fonts_fontyourface_info() {
  10. return array(
  11. 'name' => 'Edge Fonts',
  12. 'url' => 'http://www.edgefonts.com/',
  13. 'base_path' => 'http://www.edgefonts.com/?font=',
  14. );
  15. }
  16. /**
  17. * Implements hook_fontyourface_import().
  18. */
  19. function edge_fonts_fontyourface_import() {
  20. include_once 'edge_fonts.list.inc';
  21. // Get available fonts.
  22. $families = edge_fonts_list();
  23. $variants = 0;
  24. foreach ($families as $family_name => $family) {
  25. foreach ($family['variants'] as $variant => $variant_name) {
  26. $font = new stdClass();
  27. $font->provider = 'edge_fonts';
  28. $font->name = $family['fontname'] . ' ' . $variant_name;
  29. $font->css_family = $family_name;
  30. $font->css_weight = $variant[1] . '00';
  31. if ($variant[0] == 'i') {
  32. $font->css_style = 'italic';
  33. }
  34. $metadata = array(
  35. 'variant' => $variant,
  36. );
  37. $font->metadata = serialize($metadata);
  38. $font->url = 'http://www.edgefonts.com/?font=' . $family_name . ':' . $variant . '#list-of-available-fonts';
  39. $font->license = 'Terms of Use';
  40. $font->license_url = 'http://www.edgefonts.com/#terms';
  41. fontyourface_save_font($font);
  42. $variants++;
  43. }
  44. }
  45. drupal_set_message(t('!fonts Edge Fonts of !families families imported/updated.', array('!fonts' => $variants, '!families' => count($families))));
  46. fontyourface_log('!fonts Edge Fonts of !families families imported/updated.', array('!fonts' => $variants, '!families' => count($families)));
  47. // Return TRUE if we succeeded in importing something, FALSE if not.
  48. return (bool) count($families);
  49. }
  50. /**
  51. * Implements template_preprocess_html().
  52. */
  53. function edge_fonts_preprocess_html(&$vars) {
  54. if (!empty($vars['fontyourface'])) {
  55. $fonts = array();
  56. foreach ($vars['fontyourface'] as $active_font) {
  57. if ($active_font->provider == 'edge_fonts') {
  58. $metadata = unserialize($active_font->metadata);
  59. $fonts[$active_font->css_family][] = $metadata['variant'];
  60. }
  61. }
  62. if (count($fonts) > 0) {
  63. if (variable_get('edge_fonts_base', 'naked') == 'naked') {
  64. $base = '//use.edgefonts.net/';
  65. }
  66. elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  67. $base = 'https://use.edgefonts.net/';
  68. }
  69. else {
  70. $base = 'http://use.edgefonts.net/';
  71. }
  72. $families = array();
  73. $all_subsets = (variable_get('edge_fonts_subsets', 'default') == 'all') ? ':all' : '';
  74. foreach ($fonts as $family => $variants) {
  75. $families[] = $family . ':' . implode(',', $variants) . $all_subsets;
  76. }
  77. $js = $base . implode(';', $families) . '.js';
  78. drupal_add_js($js, array('type' => 'external'));
  79. }
  80. }
  81. }
  82. /**
  83. * Implements hook_fontyourface_preview().
  84. */
  85. function edge_fonts_fontyourface_preview($font, $text = NULL, $size = 18) {
  86. $output = '';
  87. if ($text == NULL) {
  88. $text = $font->name;
  89. }
  90. if ($size == 'all') {
  91. // Display variety of sizes.
  92. $sizes = array(32, 24, 18, 14, 12, 10);
  93. foreach ($sizes as $size) {
  94. $output .= '<div style="' . fontyourface_font_css($font) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</div>';
  95. }
  96. }
  97. else {
  98. // Display single size.
  99. $output = '<span style="' . fontyourface_font_css($font) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</span>';
  100. }
  101. return $output;
  102. }
  103. /**
  104. * Implements hook_form_FORM_ID_alter().
  105. */
  106. function edge_fonts_form_fontyourface_ui_settings_form_alter(&$form, &$form_state, $form_id) {
  107. $form['edge_fonts'] = array(
  108. '#type' => 'fieldset',
  109. '#title' => 'Edge Fonts',
  110. '#weight' => -1,
  111. 'edge_fonts_base' => array(
  112. '#type' => 'radios',
  113. '#title' => t('Script URLs'),
  114. '#options' => array(
  115. 'naked' => t('// (schema-less - recommended by the provider)'),
  116. 'http' => t('http:// or https:// (ask the server)'),
  117. ),
  118. '#default_value' => variable_get('edge_fonts_base', 'naked'),
  119. ),
  120. 'edge_fonts_subsets' => array(
  121. '#type' => 'radios',
  122. '#title' => t('Subsets to use'),
  123. '#options' => array(
  124. 'default' => t('Default'),
  125. 'all' => t('All'),
  126. ),
  127. '#default_value' => variable_get('edge_fonts_subsets', 'default'),
  128. ),
  129. 'edge_fonts_save_settings' => array(
  130. '#type' => 'submit',
  131. '#value' => t('Save settings'),
  132. ),
  133. );
  134. $form['#submit'][] = 'edge_fonts_save_settings';
  135. // Move the default update/import button to the Edge Fonts fieldset.
  136. if (isset($form['providers']['edge_fonts_import'])) {
  137. $form['edge_fonts']['edge_fonts_import'] = $form['providers']['edge_fonts_import'];
  138. unset($form['providers']['edge_fonts_import']);
  139. }
  140. }
  141. /**
  142. * Custom submit handler for fontyourface_ui_settings_form.
  143. */
  144. function edge_fonts_save_settings($form, &$form_state) {
  145. if ($form_state['clicked_button']['#parents'][0] == 'edge_fonts_save_settings') {
  146. variable_set('edge_fonts_base', $form_state['values']['edge_fonts_base']);
  147. variable_set('edge_fonts_subsets', $form_state['values']['edge_fonts_subsets']);
  148. }
  149. }