ScaledFont.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/ScaledFont.h>
  9. namespace Gfx {
  10. float ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); }
  11. float ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); }
  12. float ScaledFont::width(Utf32View const& view) const { return unicode_view_width(view); }
  13. template<typename T>
  14. ALWAYS_INLINE float ScaledFont::unicode_view_width(T const& view) const
  15. {
  16. if (view.is_empty())
  17. return 0;
  18. float width = 0;
  19. float longest_width = 0;
  20. u32 last_code_point = 0;
  21. for (auto code_point : view) {
  22. if (code_point == '\n' || code_point == '\r') {
  23. longest_width = max(width, longest_width);
  24. width = 0;
  25. last_code_point = code_point;
  26. continue;
  27. }
  28. u32 glyph_id = glyph_id_for_code_point(code_point);
  29. auto kerning = glyphs_horizontal_kerning(last_code_point, code_point);
  30. width += kerning + glyph_metrics(glyph_id).advance_width;
  31. last_code_point = code_point;
  32. }
  33. longest_width = max(width, longest_width);
  34. return longest_width;
  35. }
  36. RefPtr<Gfx::Bitmap> ScaledFont::rasterize_glyph(u32 glyph_id, GlyphSubpixelOffset subpixel_offset) const
  37. {
  38. GlyphIndexWithSubpixelOffset index { glyph_id, subpixel_offset };
  39. auto glyph_iterator = m_cached_glyph_bitmaps.find(index);
  40. if (glyph_iterator != m_cached_glyph_bitmaps.end())
  41. return glyph_iterator->value;
  42. auto glyph_bitmap = m_font->rasterize_glyph(glyph_id, m_x_scale, m_y_scale, subpixel_offset);
  43. m_cached_glyph_bitmaps.set(index, glyph_bitmap);
  44. return glyph_bitmap;
  45. }
  46. Gfx::Glyph ScaledFont::glyph(u32 code_point) const
  47. {
  48. return glyph(code_point, GlyphSubpixelOffset { 0, 0 });
  49. }
  50. Gfx::Glyph ScaledFont::glyph(u32 code_point, GlyphSubpixelOffset subpixel_offset) const
  51. {
  52. auto id = glyph_id_for_code_point(code_point);
  53. auto bitmap = rasterize_glyph(id, subpixel_offset);
  54. auto metrics = glyph_metrics(id);
  55. return Gfx::Glyph(bitmap, metrics.left_side_bearing, metrics.advance_width, metrics.ascender);
  56. }
  57. float ScaledFont::glyph_left_bearing(u32 code_point) const
  58. {
  59. auto id = glyph_id_for_code_point(code_point);
  60. return glyph_metrics(id).left_side_bearing;
  61. }
  62. float ScaledFont::glyph_width(u32 code_point) const
  63. {
  64. auto id = glyph_id_for_code_point(code_point);
  65. auto metrics = glyph_metrics(id);
  66. return metrics.advance_width;
  67. }
  68. float ScaledFont::glyph_or_emoji_width(u32 code_point) const
  69. {
  70. auto id = glyph_id_for_code_point(code_point);
  71. auto metrics = glyph_metrics(id);
  72. return metrics.advance_width;
  73. }
  74. float ScaledFont::glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const
  75. {
  76. if (left_code_point == 0 || right_code_point == 0)
  77. return 0.f;
  78. auto left_glyph_id = glyph_id_for_code_point(left_code_point);
  79. auto right_glyph_id = glyph_id_for_code_point(right_code_point);
  80. if (left_glyph_id == 0 || right_glyph_id == 0)
  81. return 0.f;
  82. return m_font->glyphs_horizontal_kerning(left_glyph_id, right_glyph_id, m_x_scale);
  83. }
  84. u8 ScaledFont::glyph_fixed_width() const
  85. {
  86. return glyph_metrics(glyph_id_for_code_point(' ')).advance_width;
  87. }
  88. Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const
  89. {
  90. auto metrics = m_font->metrics(m_x_scale, m_y_scale);
  91. return Gfx::FontPixelMetrics {
  92. .size = (float)pixel_size(),
  93. .x_height = (float)x_height(),
  94. .advance_of_ascii_zero = (float)glyph_width('0'),
  95. .glyph_spacing = (float)glyph_spacing(),
  96. .ascent = metrics.ascender,
  97. .descent = -metrics.descender,
  98. .line_gap = metrics.line_gap,
  99. };
  100. }
  101. }