Glyf.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <AK/Vector.h>
  9. #include <LibGfx/AffineTransform.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibTTF/Tables.h>
  12. #include <math.h>
  13. namespace TTF {
  14. class Rasterizer {
  15. public:
  16. Rasterizer(Gfx::IntSize);
  17. void draw_path(Gfx::Path&);
  18. RefPtr<Gfx::Bitmap> accumulate();
  19. private:
  20. void draw_line(Gfx::FloatPoint, Gfx::FloatPoint);
  21. Gfx::IntSize m_size;
  22. Vector<float> m_data;
  23. };
  24. class Loca {
  25. public:
  26. static Optional<Loca> from_slice(const ReadonlyBytes&, u32 num_glyphs, IndexToLocFormat);
  27. u32 get_glyph_offset(u32 glyph_id) const;
  28. private:
  29. Loca(const ReadonlyBytes& slice, u32 num_glyphs, IndexToLocFormat index_to_loc_format)
  30. : m_slice(slice)
  31. , m_num_glyphs(num_glyphs)
  32. , m_index_to_loc_format(index_to_loc_format)
  33. {
  34. }
  35. ReadonlyBytes m_slice;
  36. u32 m_num_glyphs { 0 };
  37. IndexToLocFormat m_index_to_loc_format;
  38. };
  39. class Glyf {
  40. public:
  41. class Glyph {
  42. public:
  43. Glyph(const ReadonlyBytes& slice, i16 xmin, i16 ymin, i16 xmax, i16 ymax, i16 num_contours = -1)
  44. : m_xmin(xmin)
  45. , m_ymin(ymin)
  46. , m_xmax(xmax)
  47. , m_ymax(ymax)
  48. , m_num_contours(num_contours)
  49. , m_slice(slice)
  50. {
  51. if (m_num_contours >= 0) {
  52. m_type = Type::Simple;
  53. }
  54. }
  55. template<typename GlyphCb>
  56. RefPtr<Gfx::Bitmap> raster(float x_scale, float y_scale, GlyphCb glyph_callback) const
  57. {
  58. switch (m_type) {
  59. case Type::Simple:
  60. return raster_simple(x_scale, y_scale);
  61. case Type::Composite:
  62. return raster_composite(x_scale, y_scale, glyph_callback);
  63. }
  64. VERIFY_NOT_REACHED();
  65. }
  66. int ascender() const { return m_ymax; }
  67. int descender() const { return m_ymin; }
  68. private:
  69. enum class Type {
  70. Simple,
  71. Composite,
  72. };
  73. class ComponentIterator {
  74. public:
  75. struct Item {
  76. u16 glyph_id;
  77. Gfx::AffineTransform affine;
  78. };
  79. ComponentIterator(const ReadonlyBytes& slice)
  80. : m_slice(slice)
  81. {
  82. }
  83. Optional<Item> next();
  84. private:
  85. ReadonlyBytes m_slice;
  86. bool m_has_more { true };
  87. u32 m_offset { 0 };
  88. };
  89. void raster_inner(Rasterizer&, Gfx::AffineTransform&) const;
  90. RefPtr<Gfx::Bitmap> raster_simple(float x_scale, float y_scale) const;
  91. template<typename GlyphCb>
  92. RefPtr<Gfx::Bitmap> raster_composite(float x_scale, float y_scale, GlyphCb glyph_callback) const
  93. {
  94. u32 width = (u32)(ceilf((m_xmax - m_xmin) * x_scale)) + 1;
  95. u32 height = (u32)(ceilf((m_ymax - m_ymin) * y_scale)) + 1;
  96. Rasterizer rasterizer(Gfx::IntSize(width, height));
  97. auto affine = Gfx::AffineTransform().scale(x_scale, -y_scale).translate(-m_xmin, -m_ymax);
  98. ComponentIterator component_iterator(m_slice);
  99. while (true) {
  100. auto opt_item = component_iterator.next();
  101. if (!opt_item.has_value()) {
  102. break;
  103. }
  104. auto item = opt_item.value();
  105. auto affine_here = affine.multiply(item.affine);
  106. auto glyph = glyph_callback(item.glyph_id);
  107. glyph.raster_inner(rasterizer, affine_here);
  108. }
  109. return rasterizer.accumulate();
  110. }
  111. Type m_type { Type::Composite };
  112. i16 m_xmin { 0 };
  113. i16 m_ymin { 0 };
  114. i16 m_xmax { 0 };
  115. i16 m_ymax { 0 };
  116. i16 m_num_contours { -1 };
  117. ReadonlyBytes m_slice;
  118. };
  119. Glyf(const ReadonlyBytes& slice)
  120. : m_slice(slice)
  121. {
  122. }
  123. Glyph glyph(u32 offset) const;
  124. private:
  125. enum class Offsets {
  126. XMin = 2,
  127. YMin = 4,
  128. XMax = 6,
  129. YMax = 8,
  130. };
  131. enum class Sizes {
  132. GlyphHeader = 10,
  133. };
  134. ReadonlyBytes m_slice;
  135. };
  136. }