kyamagu / skia-python

Python binding to Skia Graphics Library
https://kyamagu.github.io/skia-python/
BSD 3-Clause "New" or "Revised" License
245 stars 43 forks source link

Binding the skparagraph module #225

Closed HinTak closed 1 month ago

HinTak commented 10 months ago

Underline and strikeout were removed as SkPaint style some years ago, but seem to have been re-added in the skparagraph module as a settable text decoration style.

See #224 also

olebedev commented 3 months ago

Hi @HinTak, The SkParagraph is a really useful module. Is there a chance you can share some insights on what is the current state of this issue, is it being worked on or what sort of help is required to push it forward?

Kind regards, Oleg

HinTak commented 3 months ago

@olebedev nothing has been done so far. As you see this issue is more one or two statements of what possible benefits skparagraph module brings. I just had a quick look at the upstream code, and it is a pretty large module. It is probably a good idea to identify a good usage example in c (or other language?) for it and start adding those as a small usable subset? You can use the svg module as an example to see what's done and how https://github.com/kyamagu/skia-python/blob/main/src/skia/SVGDOM.cpp . That's the only module we bind so far - we also provide a few (one, I think) methods in the TextBlob class which accesses the skshaper module: https://github.com/kyamagu/skia-python/blob/832cb4e733548dedc6fa93f59066e149329ba4d3/src/skia/TextBlob.cpp#L255 . (I have some not-pushed code snippet doing a python skia.shaper class but decided that wasn't needed) . So probably either approaches: exposing SkParagraph as a python skia.Paragraph class etc, or additional methods to some of the other existing classes.

If you have a business need you can always get in touch privately about funding/ commissioning the work...

HinTak commented 3 months ago

The most recent changes in the svg module binding was to make this example work : https://github.com/rougier/freetype-py/blob/da3c14961f2e672bf487264e1e77dcbdb2344816/examples/skia_ot_svg_module.py#L96 https://github.com/rougier/freetype-py/blob/da3c14961f2e672bf487264e1e77dcbdb2344816/examples/skia_ot_svg_module.py#L126

The c version of this python code is at https://github.com/HinTak/freetype2-demos-skia . Historically it exists before the python one, only as a patch to freetype2-demos, posted at https://github.com/HinTak/harfbuzz-python-demos/tree/master/skia-adventure . The repository to host the patch is created more recently. That's how I came to skia-python a year ago.

HinTak commented 3 months ago

AFAIC only three modules are non-experimental at the moment: svg, skottie, and skresources. SkShaper and SkUnicode are with a stable API. (Look for SK_API, SK_SPI and ..._API in the code). That said, svg was included in the m87 series before it was declared non-experimental (in m88). That's not a forbidding point on skparagraph, but we just should only expose to python what is used/ maintained, rather than doing any wholesale addition, as the api of skparagraph is likely still subjected to substantial changes.

olebedev commented 3 months ago

I see, thank you for the comprehensive explanation, @HinTak.

Understood. I will have to try to resolve my multiline rendering with the existing API and if I see I get blocked without have SkParagraph in place I will try to bring it in and contribute upstream. And thanks for the guidance, btw!

Best, Oleg

HinTak commented 3 months ago

@olebedev an appropriate example in c++ trying to port to python is https://github.com/google/skia/blob/main/example/external_client/src/shape_text.cpp , I think. That would come to 3x to 5x the size of https://github.com/kyamagu/skia-python/blob/main/src/skia/SVGDOM.cpp .

HinTak commented 3 months ago

@olebedev example python code based on the upstream c++ example, and the pull in #258 . You'll need to install the python wheels when CI finishes https://github.com/kyamagu/skia-python/actions/runs/10337416324 to try it out, of course. Consider making a donation with the link in my profile https://hintak.github.io/ , especially if you want this for a business need and / or want to go further either yourself or have questions.

258 will likely get merged in a about 2 months' time - we typically make a release N or N +1 (if updating skia-python N to N+1 is simple) when upstream N+1 comes out. Since we just did 128 a week after upstream 128, we'll be doing either 129 or 130 when upstream 130 comes out.

Note this is subjected to how upstream responds to https://issues.skia.org/358587937 - basically NONE of skparagraph is meant to be used by non-google software at the moment!

olebedev commented 3 months ago

Hi @HinTak, this is awesome, thank you so much for the effort!

I am not sure at the moment if we are going to use the SkParagraph, as I mentioned earlier, however, I would be more than happy to put yourself onto a list of open-source projects/contributors to support to, when the next round of sponsorship proposals will be announced within @Canva.

HinTak commented 3 months ago

@olebedev after spending a good part of the weekend with skparagraph, I think it is quite neat - the c++ example is quite comprehensive, and it gets better with the python port as one can add/change rendering options quickly to try out without recompile. Justified / left / right / centred multi-line text, with decorations (under/over/strikethrough lines, dash, wavy, colored etc). The python example is the main "documentation" at the moment. Upstream documentation is quite sparse, actually, but having the c++ example useful.

At the moment the python code uses platform fonts (while the c++ original example is capable of loading specific font file) - I think using platform fonts is sufficient, but the specific font file capability is probably useful for pixel-accurate agreement across platforms. I think I can add that soon. Anyway, #258 will grow a bit and #256 get included when we move to release m129/130 in about 2 months.

HinTak commented 3 months ago

Added the single font loading code as well as updated the example and comments within.

HinTak commented 3 months ago

The example is called shaped_text, and is able to do multi-line paragraphs in Arabic correctly...