test-subset-hmtx.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright © 2018 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Roderick Sheeter
  25. */
  26. #include "hb-test.h"
  27. #include "hb-subset-test.h"
  28. /* Unit tests for hmtx subsetting */
  29. static void check_num_hmetrics(hb_face_t *face, uint16_t expected_num_hmetrics)
  30. {
  31. hb_blob_t *hhea_blob = hb_face_reference_table (face, HB_TAG ('h','h','e','a'));
  32. hb_blob_t *hmtx_blob = hb_face_reference_table (face, HB_TAG ('h','m','t','x'));
  33. // TODO I sure wish I could just use the hmtx table struct!
  34. unsigned int hhea_len;
  35. uint8_t *raw_hhea = (uint8_t *) hb_blob_get_data(hhea_blob, &hhea_len);
  36. uint16_t num_hmetrics = (raw_hhea[hhea_len - 2] << 8) + raw_hhea[hhea_len - 1];
  37. g_assert_cmpuint(expected_num_hmetrics, ==, num_hmetrics);
  38. hb_blob_destroy (hhea_blob);
  39. hb_blob_destroy (hmtx_blob);
  40. }
  41. static void
  42. test_subset_hmtx_simple_subset (void)
  43. {
  44. hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
  45. hb_face_t *face_ac = hb_test_open_font_file ("fonts/Roboto-Regular.ac.ttf");
  46. hb_set_t *codepoints = hb_set_create ();
  47. hb_face_t *face_abc_subset;
  48. hb_set_add (codepoints, 'a');
  49. hb_set_add (codepoints, 'c');
  50. face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
  51. hb_set_destroy (codepoints);
  52. check_num_hmetrics(face_abc_subset, 3); /* nothing has same width */
  53. hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('h','m','t','x'));
  54. hb_face_destroy (face_abc_subset);
  55. hb_face_destroy (face_abc);
  56. hb_face_destroy (face_ac);
  57. }
  58. static void
  59. test_subset_hmtx_monospace (void)
  60. {
  61. hb_face_t *face_abc = hb_test_open_font_file ("fonts/Inconsolata-Regular.abc.ttf");
  62. hb_face_t *face_ac = hb_test_open_font_file ("fonts/Inconsolata-Regular.ac.ttf");
  63. hb_set_t *codepoints = hb_set_create ();
  64. hb_face_t *face_abc_subset;
  65. hb_set_add (codepoints, 'a');
  66. hb_set_add (codepoints, 'c');
  67. face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
  68. hb_set_destroy (codepoints);
  69. check_num_hmetrics(face_abc_subset, 1); /* everything has same width */
  70. hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('h','m','t','x'));
  71. hb_face_destroy (face_abc_subset);
  72. hb_face_destroy (face_abc);
  73. hb_face_destroy (face_ac);
  74. }
  75. static void
  76. test_subset_hmtx_keep_num_metrics (void)
  77. {
  78. hb_face_t *face_abc = hb_test_open_font_file ("fonts/Inconsolata-Regular.abc.widerc.ttf");
  79. hb_face_t *face_ac = hb_test_open_font_file ("fonts/Inconsolata-Regular.ac.widerc.ttf");
  80. hb_set_t *codepoints = hb_set_create ();
  81. hb_face_t *face_abc_subset;
  82. hb_set_add (codepoints, 'a');
  83. hb_set_add (codepoints, 'c');
  84. face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
  85. hb_set_destroy (codepoints);
  86. check_num_hmetrics(face_abc_subset, 3); /* c is wider */
  87. hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('h','m','t','x'));
  88. hb_face_destroy (face_abc_subset);
  89. hb_face_destroy (face_abc);
  90. hb_face_destroy (face_ac);
  91. }
  92. static void
  93. test_subset_hmtx_decrease_num_metrics (void)
  94. {
  95. hb_face_t *face_abc = hb_test_open_font_file ("fonts/Inconsolata-Regular.abc.widerc.ttf");
  96. hb_face_t *face_ab = hb_test_open_font_file ("fonts/Inconsolata-Regular.ab.ttf");
  97. hb_set_t *codepoints = hb_set_create ();
  98. hb_face_t *face_abc_subset;
  99. hb_set_add (codepoints, 'a');
  100. hb_set_add (codepoints, 'b');
  101. face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
  102. hb_set_destroy (codepoints);
  103. check_num_hmetrics(face_abc_subset, 1); /* everything left has same width */
  104. hb_subset_test_check (face_ab, face_abc_subset, HB_TAG ('h','m','t','x'));
  105. hb_face_destroy (face_abc_subset);
  106. hb_face_destroy (face_abc);
  107. hb_face_destroy (face_ab);
  108. }
  109. static void
  110. test_subset_hmtx_noop (void)
  111. {
  112. hb_face_t *face_abc = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
  113. hb_set_t *codepoints = hb_set_create();
  114. hb_face_t *face_abc_subset;
  115. hb_set_add (codepoints, 'a');
  116. hb_set_add (codepoints, 'b');
  117. hb_set_add (codepoints, 'c');
  118. face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
  119. hb_set_destroy (codepoints);
  120. check_num_hmetrics(face_abc_subset, 4); /* nothing has same width */
  121. hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('h','m','t','x'));
  122. hb_face_destroy (face_abc_subset);
  123. hb_face_destroy (face_abc);
  124. }
  125. static void
  126. test_subset_invalid_hmtx (void)
  127. {
  128. hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/crash-e4e0bb1458a91b692eba492c907ae1f94e635480");
  129. hb_face_t *subset;
  130. hb_subset_input_t *input = hb_subset_input_create_or_fail ();
  131. hb_set_t *codepoints = hb_subset_input_unicode_set (input);
  132. hb_set_add (codepoints, 'a');
  133. hb_set_add (codepoints, 'b');
  134. hb_set_add (codepoints, 'c');
  135. subset = hb_subset_or_fail (face, input);
  136. g_assert (!subset);
  137. hb_subset_input_destroy (input);
  138. hb_face_destroy (subset);
  139. hb_face_destroy (face);
  140. }
  141. int
  142. main (int argc, char **argv)
  143. {
  144. hb_test_init (&argc, &argv);
  145. hb_test_add (test_subset_hmtx_simple_subset);
  146. hb_test_add (test_subset_hmtx_monospace);
  147. hb_test_add (test_subset_hmtx_keep_num_metrics);
  148. hb_test_add (test_subset_hmtx_decrease_num_metrics);
  149. hb_test_add (test_subset_hmtx_noop);
  150. hb_test_add (test_subset_invalid_hmtx);
  151. return hb_test_run();
  152. }