From b4331dc56cb82e0d13f4aa255be889f35a614a11 Mon Sep 17 00:00:00 2001 From: Hin-Tak Leung Date: Tue, 7 Nov 2023 01:20:15 +0000 Subject: [PATCH] New TextBlob::MakeFromShapedText, for making shaped text Fixes #195 --- src/skia/TextBlob.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/skia/TextBlob.cpp b/src/skia/TextBlob.cpp index 71b80a2b..5c3857b9 100644 --- a/src/skia/TextBlob.cpp +++ b/src/skia/TextBlob.cpp @@ -1,7 +1,9 @@ #include "common.h" #include #include +#include #include +#include // FLT_MAX template<> struct py::detail::has_operator_delete : std::false_type {}; @@ -247,6 +249,26 @@ textblob )docstring", py::arg("string"), py::arg("font"), py::arg("encoding") = SkTextEncoding::kUTF8) + .def_static("MakeFromShapedText", + [] (const std::string& utf8text, const SkFont& font, + bool leftToRight) { + std::unique_ptr shaper = SkShaper::Make(); + SkTextBlobBuilderRunHandler textBlobBuilder(utf8text.c_str(), {0, 0}); + shaper->shape( + utf8text.c_str(), utf8text.size(), font, leftToRight, FLT_MAX, &textBlobBuilder); + return textBlobBuilder.makeBlob(); + }, + R"docstring( + Creates :py:class:`TextBlob` in a single run, with shaping, for a text-run direction. + + :param str utf8text: character code points drawn + :param skia.Font font: text size, typeface, text scale, and so on, used + to draw + :param leftToRight bool: text-run direction + :return: :py:class:`TextBlob` constructed from one run + )docstring", + py::arg("utf8text"), py::arg("font"), + py::arg("leftToRight") = true) .def_static("MakeFromPosTextH", [] (const std::string& text, py::iterable xpos, SkScalar constY, const SkFont& font, SkTextEncoding encoding) {