Cmap.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Span.h>
  8. #include <stdint.h>
  9. namespace TTF {
  10. class Cmap {
  11. public:
  12. class Subtable {
  13. public:
  14. enum class Platform {
  15. Unicode = 0,
  16. Macintosh = 1,
  17. Windows = 3,
  18. Custom = 4,
  19. };
  20. enum class Format {
  21. ByteEncoding = 0,
  22. HighByte = 2,
  23. SegmentToDelta = 4,
  24. TrimmedTable = 6,
  25. Mixed16And32 = 8,
  26. TrimmedArray = 10,
  27. SegmentedCoverage = 12,
  28. ManyToOneRange = 13,
  29. UnicodeVariationSequences = 14,
  30. };
  31. enum class WindowsEncoding {
  32. UnicodeBMP = 1,
  33. UnicodeFullRepertoire = 10,
  34. };
  35. Subtable(const ReadonlyBytes& slice, u16 platform_id, u16 encoding_id)
  36. : m_slice(slice)
  37. , m_raw_platform_id(platform_id)
  38. , m_encoding_id(encoding_id)
  39. {
  40. }
  41. // Returns 0 if glyph not found. This corresponds to the "missing glyph"
  42. u32 glyph_id_for_code_point(u32 code_point) const;
  43. Platform platform_id() const;
  44. u16 encoding_id() const { return m_encoding_id; }
  45. Format format() const;
  46. private:
  47. enum class Table4Offsets {
  48. SegCountX2 = 6,
  49. EndConstBase = 14,
  50. StartConstBase = 16,
  51. DeltaConstBase = 16,
  52. RangeConstBase = 16,
  53. GlyphOffsetConstBase = 16,
  54. };
  55. enum class Table4Sizes {
  56. Constant = 16,
  57. NonConstMultiplier = 4,
  58. };
  59. enum class Table12Offsets {
  60. NumGroups = 12,
  61. Record_StartCode = 16,
  62. Record_EndCode = 20,
  63. Record_StartGlyph = 24,
  64. };
  65. enum class Table12Sizes {
  66. Header = 16,
  67. Record = 12,
  68. };
  69. u32 glyph_id_for_code_point_table_4(u32 code_point) const;
  70. u32 glyph_id_for_code_point_table_12(u32 code_point) const;
  71. ReadonlyBytes m_slice;
  72. u16 m_raw_platform_id { 0 };
  73. u16 m_encoding_id { 0 };
  74. };
  75. static Optional<Cmap> from_slice(const ReadonlyBytes&);
  76. u32 num_subtables() const;
  77. Optional<Subtable> subtable(u32 index) const;
  78. void set_active_index(u32 index) { m_active_index = index; }
  79. // Returns 0 if glyph not found. This corresponds to the "missing glyph"
  80. u32 glyph_id_for_code_point(u32 code_point) const;
  81. private:
  82. enum class Offsets {
  83. NumTables = 2,
  84. EncodingRecord_EncodingID = 2,
  85. EncodingRecord_Offset = 4,
  86. };
  87. enum class Sizes {
  88. TableHeader = 4,
  89. EncodingRecord = 8,
  90. };
  91. Cmap(const ReadonlyBytes& slice)
  92. : m_slice(slice)
  93. {
  94. }
  95. ReadonlyBytes m_slice;
  96. u32 m_active_index { UINT32_MAX };
  97. };
  98. }