TextLayout.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "TextLayout.h"
  8. #include "Font/Emoji.h"
  9. #include <AK/TypeCasts.h>
  10. #include <LibGfx/Font/ScaledFont.h>
  11. #include <LibUnicode/Emoji.h>
  12. #include <harfbuzz/hb.h>
  13. namespace Gfx {
  14. void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, Function<void(DrawGlyph const&)> callback, Optional<float&> width)
  15. {
  16. hb_buffer_t* buffer = hb_buffer_create();
  17. ScopeGuard destroy_buffer = [&]() { hb_buffer_destroy(buffer); };
  18. hb_buffer_add_utf8(buffer, reinterpret_cast<char const*>(string.bytes()), string.byte_length(), 0, -1);
  19. hb_buffer_guess_segment_properties(buffer);
  20. u32 glyph_count;
  21. auto* glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
  22. Vector<hb_glyph_info_t> const input_glyph_info({ glyph_info, glyph_count });
  23. if (input_glyph_info.is_empty())
  24. return;
  25. auto* hb_font = font.harfbuzz_font();
  26. hb_shape(hb_font, buffer, nullptr, 0);
  27. glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
  28. auto* positions = hb_buffer_get_glyph_positions(buffer, &glyph_count);
  29. FloatPoint point = baseline_start;
  30. for (size_t i = 0; i < glyph_count; ++i) {
  31. auto position = point
  32. - FloatPoint { 0, font.pixel_metrics().ascent }
  33. + FloatPoint { positions[i].x_offset, positions[i].y_offset } / text_shaping_resolution;
  34. callback(DrawGlyph {
  35. .position = position,
  36. .glyph_id = glyph_info[i].codepoint,
  37. });
  38. point += FloatPoint { positions[i].x_advance, positions[i].y_advance } / text_shaping_resolution;
  39. }
  40. if (width.has_value())
  41. *width = point.x();
  42. }
  43. float measure_text_width(Utf8View const& string, Gfx::Font const& font)
  44. {
  45. float width = 0;
  46. for_each_glyph_position({}, string, font, [&](DrawGlyph const&) {}, width);
  47. return width;
  48. }
  49. }