TextLayout.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 <AK/TypeCasts.h>
  9. #include <LibGfx/Font/ScaledFont.h>
  10. #include <harfbuzz/hb.h>
  11. namespace Gfx {
  12. RefPtr<GlyphRun> shape_text(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, GlyphRun::TextType text_type)
  13. {
  14. hb_buffer_t* buffer = hb_buffer_create();
  15. ScopeGuard destroy_buffer = [&]() { hb_buffer_destroy(buffer); };
  16. hb_buffer_add_utf8(buffer, reinterpret_cast<char const*>(string.bytes()), string.byte_length(), 0, -1);
  17. hb_buffer_guess_segment_properties(buffer);
  18. u32 glyph_count;
  19. auto* glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
  20. Vector<hb_glyph_info_t> const input_glyph_info({ glyph_info, glyph_count });
  21. auto* hb_font = font.harfbuzz_font();
  22. hb_shape(hb_font, buffer, nullptr, 0);
  23. glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
  24. auto* positions = hb_buffer_get_glyph_positions(buffer, &glyph_count);
  25. Vector<Gfx::DrawGlyph> glyph_run;
  26. FloatPoint point = baseline_start;
  27. for (size_t i = 0; i < glyph_count; ++i) {
  28. auto position = point
  29. - FloatPoint { 0, font.pixel_metrics().ascent }
  30. + FloatPoint { positions[i].x_offset, positions[i].y_offset } / text_shaping_resolution;
  31. glyph_run.append({ position, glyph_info[i].codepoint });
  32. point += FloatPoint { positions[i].x_advance, positions[i].y_advance } / text_shaping_resolution;
  33. }
  34. return adopt_ref(*new Gfx::GlyphRun(move(glyph_run), font, text_type, point.x()));
  35. }
  36. float measure_text_width(Utf8View const& string, Gfx::Font const& font)
  37. {
  38. auto glyph_run = shape_text({}, string, font, GlyphRun::TextType::Common);
  39. return glyph_run->width();
  40. }
  41. }