TestCmap.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2022, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/TrueTypeFont/Cmap.h>
  7. #include <LibTest/TestCase.h>
  8. TEST_CASE(test_cmap_format_4)
  9. {
  10. // clang-format off
  11. // Big endian.
  12. u8 cmap_table[] =
  13. {
  14. // https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#cmap-header
  15. 0, 0, // uint16 version
  16. 0, 1, // uint16 numTables
  17. // https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#encoding-records-and-encodings
  18. 0, 0, // uint16 platformID, 0 means "Unicode"
  19. 0, 3, // uint16 encodingID, 3 means "BMP only" for platformID==0.
  20. 0, 0, 0, 12, // Offset32 to encoding subtable.
  21. // https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-4-segment-mapping-to-delta-values
  22. 0, 4, // uint16 format = 4
  23. 0, 42, // uint16 length in bytes
  24. 0, 0, // uint16 language, must be 0
  25. 0, 6, // segCount * 2
  26. 0, 4, // searchRange
  27. 0, 1, // entrySelector
  28. 0, 2, // rangeShift
  29. // endCode array, last entry must be 0xffff.
  30. 0, 128,
  31. 1, 0,
  32. 0xff, 0xff,
  33. 0, 0, // uint16 reservedPad
  34. // startCode array
  35. 0, 16,
  36. 1, 0,
  37. 0xff, 0xff,
  38. // delta array
  39. 0, 0,
  40. 0, 10,
  41. 0, 0,
  42. // glyphID array
  43. 0, 0,
  44. 0, 0,
  45. 0, 0,
  46. };
  47. // clang-format on
  48. auto cmap = TTF::Cmap::from_slice({ cmap_table, sizeof cmap_table }).value();
  49. cmap.set_active_index(0);
  50. // Format 4 can't handle code points > 0xffff.
  51. // First range is 16..128.
  52. EXPECT_EQ(cmap.glyph_id_for_code_point(15), 0u);
  53. EXPECT_EQ(cmap.glyph_id_for_code_point(16), 16u);
  54. EXPECT_EQ(cmap.glyph_id_for_code_point(128), 128u);
  55. EXPECT_EQ(cmap.glyph_id_for_code_point(129), 0u);
  56. // Second range is 256..256, with delta 10.
  57. EXPECT_EQ(cmap.glyph_id_for_code_point(255), 0u);
  58. EXPECT_EQ(cmap.glyph_id_for_code_point(256), 266u);
  59. EXPECT_EQ(cmap.glyph_id_for_code_point(257), 0u);
  60. // Third range is 0xffff..0xffff.
  61. // From https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-4-segment-mapping-to-delta-values:
  62. // "the final start code and endCode values must be 0xFFFF. This segment need not contain any valid mappings.
  63. // (It can just map the single character code 0xFFFF to missingGlyph). However, the segment must be present."
  64. // FIXME: Make TTF::Cmap::from_slice() reject inputs where this isn't true.
  65. EXPECT_EQ(cmap.glyph_id_for_code_point(0xfeff), 0u);
  66. EXPECT_EQ(cmap.glyph_id_for_code_point(0xffff), 0xffffu);
  67. EXPECT_EQ(cmap.glyph_id_for_code_point(0x1'0000), 0u);
  68. }