ScaledFont.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf32View.h>
  7. #include <AK/Utf8View.h>
  8. #include <LibGfx/Font/Emoji.h>
  9. #include <LibGfx/Font/ScaledFont.h>
  10. namespace Gfx {
  11. ScaledFont::ScaledFont(NonnullRefPtr<VectorFont> font, float point_width, float point_height, unsigned dpi_x, unsigned dpi_y)
  12. : m_font(move(font))
  13. , m_point_width(point_width)
  14. , m_point_height(point_height)
  15. {
  16. float units_per_em = m_font->units_per_em();
  17. m_x_scale = (point_width * dpi_x) / (POINTS_PER_INCH * units_per_em);
  18. m_y_scale = (point_height * dpi_y) / (POINTS_PER_INCH * units_per_em);
  19. auto metrics = m_font->metrics(m_x_scale, m_y_scale);
  20. m_pixel_size = m_point_height * (DEFAULT_DPI / POINTS_PER_INCH);
  21. m_pixel_size_rounded_up = static_cast<int>(ceilf(m_pixel_size));
  22. m_pixel_metrics = Gfx::FontPixelMetrics {
  23. .size = (float)pixel_size(),
  24. .x_height = metrics.x_height,
  25. .advance_of_ascii_zero = (float)glyph_width('0'),
  26. .glyph_spacing = (float)glyph_spacing(),
  27. .ascent = metrics.ascender,
  28. .descent = metrics.descender,
  29. .line_gap = metrics.line_gap,
  30. };
  31. }
  32. int ScaledFont::width_rounded_up(StringView view) const
  33. {
  34. return static_cast<int>(ceilf(width(view)));
  35. }
  36. float ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); }
  37. float ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); }
  38. float ScaledFont::width(Utf32View const& view) const { return unicode_view_width(view); }
  39. template<typename T>
  40. ALWAYS_INLINE float ScaledFont::unicode_view_width(T const& view) const
  41. {
  42. if (view.is_empty())
  43. return 0;
  44. float width = 0;
  45. float longest_width = 0;
  46. u32 last_code_point = 0;
  47. for (auto it = view.begin(); it != view.end(); last_code_point = *it, ++it) {
  48. auto code_point = *it;
  49. if (code_point == '\n' || code_point == '\r') {
  50. longest_width = max(width, longest_width);
  51. width = 0;
  52. continue;
  53. }
  54. auto kerning = glyphs_horizontal_kerning(last_code_point, code_point);
  55. width += kerning + glyph_or_emoji_width(it);
  56. }
  57. longest_width = max(width, longest_width);
  58. return longest_width;
  59. }
  60. RefPtr<Gfx::Bitmap> ScaledFont::rasterize_glyph(u32 glyph_id, GlyphSubpixelOffset subpixel_offset) const
  61. {
  62. GlyphIndexWithSubpixelOffset index { glyph_id, subpixel_offset };
  63. auto glyph_iterator = m_cached_glyph_bitmaps.find(index);
  64. if (glyph_iterator != m_cached_glyph_bitmaps.end())
  65. return glyph_iterator->value;
  66. auto glyph_bitmap = m_font->rasterize_glyph(glyph_id, m_x_scale, m_y_scale, subpixel_offset);
  67. m_cached_glyph_bitmaps.set(index, glyph_bitmap);
  68. return glyph_bitmap;
  69. }
  70. bool ScaledFont::append_glyph_path_to(Gfx::Path& path, u32 glyph_id) const
  71. {
  72. return m_font->append_glyph_path_to(path, glyph_id, m_x_scale, m_y_scale);
  73. }
  74. Gfx::Glyph ScaledFont::glyph(u32 code_point) const
  75. {
  76. return glyph(code_point, GlyphSubpixelOffset { 0, 0 });
  77. }
  78. Gfx::Glyph ScaledFont::glyph(u32 code_point, GlyphSubpixelOffset subpixel_offset) const
  79. {
  80. auto id = glyph_id_for_code_point(code_point);
  81. auto bitmap = rasterize_glyph(id, subpixel_offset);
  82. auto metrics = glyph_metrics(id);
  83. return Gfx::Glyph(bitmap, metrics.left_side_bearing, metrics.advance_width, metrics.ascender, m_font->has_color_bitmaps());
  84. }
  85. float ScaledFont::glyph_left_bearing(u32 code_point) const
  86. {
  87. auto id = glyph_id_for_code_point(code_point);
  88. return glyph_metrics(id).left_side_bearing;
  89. }
  90. float ScaledFont::glyph_width(u32 code_point) const
  91. {
  92. auto id = glyph_id_for_code_point(code_point);
  93. auto metrics = glyph_metrics(id);
  94. return metrics.advance_width;
  95. }
  96. template<typename CodePointIterator>
  97. static float glyph_or_emoji_width_impl(ScaledFont const& font, CodePointIterator& it)
  98. {
  99. if (!font.has_color_bitmaps()) {
  100. if (auto const* emoji = Emoji::emoji_for_code_point_iterator(it))
  101. return font.pixel_size() * emoji->width() / emoji->height();
  102. }
  103. return font.glyph_width(*it);
  104. }
  105. float ScaledFont::glyph_or_emoji_width(Utf8CodePointIterator& it) const
  106. {
  107. return glyph_or_emoji_width_impl(*this, it);
  108. }
  109. float ScaledFont::glyph_or_emoji_width(Utf32CodePointIterator& it) const
  110. {
  111. return glyph_or_emoji_width_impl(*this, it);
  112. }
  113. float ScaledFont::glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const
  114. {
  115. if (left_code_point == 0 || right_code_point == 0)
  116. return 0.f;
  117. auto left_glyph_id = glyph_id_for_code_point(left_code_point);
  118. auto right_glyph_id = glyph_id_for_code_point(right_code_point);
  119. if (left_glyph_id == 0 || right_glyph_id == 0)
  120. return 0.f;
  121. return m_font->glyphs_horizontal_kerning(left_glyph_id, right_glyph_id, m_x_scale);
  122. }
  123. u8 ScaledFont::glyph_fixed_width() const
  124. {
  125. return glyph_metrics(glyph_id_for_code_point(' ')).advance_width;
  126. }
  127. RefPtr<Font> ScaledFont::with_size(float point_size) const
  128. {
  129. if (point_size == m_point_height && point_size == m_point_width)
  130. return const_cast<ScaledFont*>(this);
  131. return m_font->scaled_font(point_size);
  132. }
  133. Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const
  134. {
  135. return m_pixel_metrics;
  136. }
  137. float ScaledFont::pixel_size() const
  138. {
  139. return m_pixel_size;
  140. }
  141. int ScaledFont::pixel_size_rounded_up() const
  142. {
  143. return m_pixel_size_rounded_up;
  144. }
  145. float ScaledFont::point_size() const
  146. {
  147. return m_point_height;
  148. }
  149. }