html-font.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "html-imp.h"
  24. #include <string.h>
  25. static fz_font *
  26. fz_load_html_default_font(fz_context *ctx, fz_html_font_set *set, const char *family, int is_bold, int is_italic)
  27. {
  28. int is_mono = !strcmp(family, "monospace");
  29. int is_sans = !strcmp(family, "sans-serif");
  30. const char *real_family = is_mono ? "Courier" : is_sans ? "Helvetica" : "Charis SIL";
  31. const char *backup_family = is_mono ? "Courier" : is_sans ? "Helvetica" : "Times";
  32. int idx = (is_mono ? 8 : is_sans ? 4 : 0) + is_bold * 2 + is_italic;
  33. if (!set->fonts[idx])
  34. {
  35. const unsigned char *data;
  36. int size;
  37. data = fz_lookup_builtin_font(ctx, real_family, is_bold, is_italic, &size);
  38. if (!data)
  39. data = fz_lookup_builtin_font(ctx, backup_family, is_bold, is_italic, &size);
  40. if (!data)
  41. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "cannot load html font: %s", real_family);
  42. set->fonts[idx] = fz_new_font_from_memory(ctx, NULL, data, size, 0, 1);
  43. fz_font_flags(set->fonts[idx])->is_serif = !is_sans;
  44. }
  45. return set->fonts[idx];
  46. }
  47. void
  48. fz_add_html_font_face(fz_context *ctx, fz_html_font_set *set,
  49. const char *family, int is_bold, int is_italic, int is_small_caps,
  50. const char *src, fz_font *font)
  51. {
  52. fz_html_font_face *custom = fz_malloc_struct(ctx, fz_html_font_face);
  53. fz_font_flags_t *flags;
  54. flags = fz_font_flags(font);
  55. if (is_bold && !flags->is_bold)
  56. flags->fake_bold = 1;
  57. if (is_italic && !flags->is_italic)
  58. flags->fake_italic = 1;
  59. fz_try(ctx)
  60. {
  61. custom->font = fz_keep_font(ctx, font);
  62. custom->src = fz_strdup(ctx, src);
  63. custom->family = fz_strdup(ctx, family);
  64. custom->is_bold = is_bold;
  65. custom->is_italic = is_italic;
  66. custom->is_small_caps = is_small_caps;
  67. custom->next = set->custom;
  68. set->custom = custom;
  69. }
  70. fz_catch(ctx)
  71. {
  72. fz_drop_font(ctx, custom->font);
  73. fz_free(ctx, custom->src);
  74. fz_free(ctx, custom->family);
  75. fz_rethrow(ctx);
  76. }
  77. }
  78. fz_font *
  79. fz_load_html_font(fz_context *ctx, fz_html_font_set *set,
  80. const char *family, int is_bold, int is_italic, int is_small_caps)
  81. {
  82. fz_html_font_face *custom;
  83. const unsigned char *data;
  84. int best_score = 0;
  85. fz_font *best_font = NULL;
  86. fz_font *font;
  87. int size;
  88. for (custom = set->custom; custom; custom = custom->next)
  89. {
  90. if (!strcmp(family, custom->family))
  91. {
  92. int score =
  93. 1 * (is_bold == custom->is_bold) +
  94. 2 * (is_italic == custom->is_italic) +
  95. 4 * (is_small_caps == custom->is_small_caps);
  96. if (score > best_score)
  97. {
  98. best_score = score;
  99. best_font = custom->font;
  100. }
  101. }
  102. }
  103. // We found a perfect match!
  104. if (best_font && best_score == 1 + 2 + 4)
  105. return best_font;
  106. // Try to load a perfect match.
  107. data = fz_lookup_builtin_font(ctx, family, is_bold, is_italic, &size);
  108. if (!data)
  109. data = fz_lookup_builtin_font(ctx, family, 0, 0, &size);
  110. if (data)
  111. {
  112. font = fz_new_font_from_memory(ctx, NULL, data, size, 0, 0);
  113. fz_try(ctx)
  114. fz_add_html_font_face(ctx, set, family, is_bold, is_italic, 0, "<builtin>", font);
  115. fz_always(ctx)
  116. fz_drop_font(ctx, font);
  117. fz_catch(ctx)
  118. fz_rethrow(ctx);
  119. return font;
  120. }
  121. else
  122. {
  123. font = fz_load_system_font(ctx, family, is_bold, is_italic, 0);
  124. if (font)
  125. {
  126. fz_try(ctx)
  127. fz_add_html_font_face(ctx, set, family, is_bold, is_italic, 0, "<system>", font);
  128. fz_always(ctx)
  129. fz_drop_font(ctx, font);
  130. fz_catch(ctx)
  131. fz_rethrow(ctx);
  132. return font;
  133. }
  134. }
  135. // Use the imperfect match from before.
  136. if (best_font)
  137. return best_font;
  138. // Handle the "default" font aliases.
  139. if (!strcmp(family, "monospace") || !strcmp(family, "sans-serif") || !strcmp(family, "serif"))
  140. return fz_load_html_default_font(ctx, set, family, is_bold, is_italic);
  141. return NULL;
  142. }
  143. fz_html_font_set *fz_new_html_font_set(fz_context *ctx)
  144. {
  145. return fz_malloc_struct(ctx, fz_html_font_set);
  146. }
  147. void fz_drop_html_font_set(fz_context *ctx, fz_html_font_set *set)
  148. {
  149. fz_html_font_face *font, *next;
  150. int i;
  151. if (!set)
  152. return;
  153. font = set->custom;
  154. while (font)
  155. {
  156. next = font->next;
  157. fz_drop_font(ctx, font->font);
  158. fz_free(ctx, font->src);
  159. fz_free(ctx, font->family);
  160. fz_free(ctx, font);
  161. font = next;
  162. }
  163. for (i = 0; i < (int)nelem(set->fonts); ++i)
  164. fz_drop_font(ctx, set->fonts[i]);
  165. fz_free(ctx, set);
  166. }