ScaledFont.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 * 1.33333333f;
  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 = (float)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. float ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); }
  33. float ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); }
  34. float ScaledFont::width(Utf32View const& view) const { return unicode_view_width(view); }
  35. template<typename T>
  36. ALWAYS_INLINE float ScaledFont::unicode_view_width(T const& view) const
  37. {
  38. if (view.is_empty())
  39. return 0;
  40. float width = 0;
  41. float longest_width = 0;
  42. u32 last_code_point = 0;
  43. for (auto it = view.begin(); it != view.end(); last_code_point = *it, ++it) {
  44. auto code_point = *it;
  45. if (code_point == '\n' || code_point == '\r') {
  46. longest_width = max(width, longest_width);
  47. width = 0;
  48. continue;
  49. }
  50. auto kerning = glyphs_horizontal_kerning(last_code_point, code_point);
  51. width += kerning + glyph_or_emoji_width(it);
  52. }
  53. longest_width = max(width, longest_width);
  54. return longest_width;
  55. }
  56. RefPtr<Gfx::Bitmap> ScaledFont::rasterize_glyph(u32 glyph_id, GlyphSubpixelOffset subpixel_offset) const
  57. {
  58. GlyphIndexWithSubpixelOffset index { glyph_id, subpixel_offset };
  59. auto glyph_iterator = m_cached_glyph_bitmaps.find(index);
  60. if (glyph_iterator != m_cached_glyph_bitmaps.end())
  61. return glyph_iterator->value;
  62. auto glyph_bitmap = m_font->rasterize_glyph(glyph_id, m_x_scale, m_y_scale, subpixel_offset);
  63. m_cached_glyph_bitmaps.set(index, glyph_bitmap);
  64. return glyph_bitmap;
  65. }
  66. Gfx::Glyph ScaledFont::glyph(u32 code_point) const
  67. {
  68. return glyph(code_point, GlyphSubpixelOffset { 0, 0 });
  69. }
  70. Gfx::Glyph ScaledFont::glyph(u32 code_point, GlyphSubpixelOffset subpixel_offset) const
  71. {
  72. auto id = glyph_id_for_code_point(code_point);
  73. auto bitmap = rasterize_glyph(id, subpixel_offset);
  74. auto metrics = glyph_metrics(id);
  75. return Gfx::Glyph(bitmap, metrics.left_side_bearing, metrics.advance_width, metrics.ascender, m_font->has_color_bitmaps());
  76. }
  77. float ScaledFont::glyph_left_bearing(u32 code_point) const
  78. {
  79. auto id = glyph_id_for_code_point(code_point);
  80. return glyph_metrics(id).left_side_bearing;
  81. }
  82. float ScaledFont::glyph_width(u32 code_point) const
  83. {
  84. auto id = glyph_id_for_code_point(code_point);
  85. auto metrics = glyph_metrics(id);
  86. return metrics.advance_width;
  87. }
  88. template<typename CodePointIterator>
  89. static float glyph_or_emoji_width_impl(ScaledFont const& font, CodePointIterator& it)
  90. {
  91. if (auto const* emoji = Emoji::emoji_for_code_point_iterator(it))
  92. return font.pixel_size() * emoji->width() / emoji->height();
  93. return font.glyph_width(*it);
  94. }
  95. float ScaledFont::glyph_or_emoji_width(Utf8CodePointIterator& it) const
  96. {
  97. return glyph_or_emoji_width_impl(*this, it);
  98. }
  99. float ScaledFont::glyph_or_emoji_width(Utf32CodePointIterator& it) const
  100. {
  101. return glyph_or_emoji_width_impl(*this, it);
  102. }
  103. float ScaledFont::glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const
  104. {
  105. if (left_code_point == 0 || right_code_point == 0)
  106. return 0.f;
  107. auto left_glyph_id = glyph_id_for_code_point(left_code_point);
  108. auto right_glyph_id = glyph_id_for_code_point(right_code_point);
  109. if (left_glyph_id == 0 || right_glyph_id == 0)
  110. return 0.f;
  111. return m_font->glyphs_horizontal_kerning(left_glyph_id, right_glyph_id, m_x_scale);
  112. }
  113. u8 ScaledFont::glyph_fixed_width() const
  114. {
  115. return glyph_metrics(glyph_id_for_code_point(' ')).advance_width;
  116. }
  117. RefPtr<Font> ScaledFont::with_size(float point_size) const
  118. {
  119. return adopt_ref(*new Gfx::ScaledFont(*m_font, point_size, point_size));
  120. }
  121. Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const
  122. {
  123. return m_pixel_metrics;
  124. }
  125. float ScaledFont::pixel_size() const
  126. {
  127. return m_pixel_size;
  128. }
  129. int ScaledFont::pixel_size_rounded_up() const
  130. {
  131. return m_pixel_size_rounded_up;
  132. }
  133. }