Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrutStyle for leading/lineheight adjustment on ParagraphStyle + TextStyle.setLetterSpacing and TextStyle.setWordSpacing #299

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
21 changes: 21 additions & 0 deletions src/skia/Paragraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void initParagraph(py::module &m) {
py::class_<FontCollection, sk_sp<FontCollection>, SkRefCnt> font_collection(m, "textlayout_FontCollection");
py::class_<ParagraphBuilder> paragraph_builder(m, "textlayout_ParagraphBuilder");
py::class_<ParagraphStyle> paragraph_style(m, "textlayout_ParagraphStyle");
py::class_<StrutStyle> strut_style(m, "textlayout_StrutStyle");
py::class_<TextStyle> text_style(m, "textlayout_TextStyle");
py::class_<Paragraph> paragraph(m, "textlayout_Paragraph");

Expand Down Expand Up @@ -82,6 +83,20 @@ paragraph_builder
.def("Build", &ParagraphBuilder::Build)
;

strut_style
.def(py::init())
.def("setStrutEnabled",
py::overload_cast<const bool>(&StrutStyle::setStrutEnabled),
R"docstring(
)docstring",
py::arg("strutenabled"))
.def("setLeading",
py::overload_cast<const SkScalar>(&StrutStyle::setLeading),
R"docstring(
)docstring",
py::arg("leading"))
;

paragraph_style
.def(py::init())
.def("setTextStyle",
Expand All @@ -94,6 +109,11 @@ paragraph_style
R"docstring(
)docstring",
py::arg("align"))
.def("setStrutStyle",
py::overload_cast<StrutStyle>(&ParagraphStyle::setStrutStyle),
R"docstring(
)docstring",
py::arg("strutstyle"))
;

font_collection
Expand Down Expand Up @@ -208,6 +228,7 @@ m.attr("textlayout").attr("FontCollection") = m.attr("textlayout_FontCollection"
m.attr("textlayout").attr("ParagraphBuilder") = m.attr("textlayout_ParagraphBuilder");
m.attr("textlayout").attr("ParagraphStyle") = m.attr("textlayout_ParagraphStyle");
m.attr("textlayout").attr("Paragraph") = m.attr("textlayout_Paragraph");
m.attr("textlayout").attr("StrutStyle") = m.attr("textlayout_StrutStyle");
m.attr("textlayout").attr("TextStyle") = m.attr("textlayout_TextStyle");
m.attr("textlayout").attr("TextAlign") = m.attr("textlayout_TextAlign");
m.attr("textlayout").attr("TextDecoration") = m.attr("textlayout_TextDecoration");
Expand Down
49 changes: 49 additions & 0 deletions tests/test_paragraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def test_ParagraphStyle_init0(paragraph_style):
assert isinstance(paragraph_style, skia.textlayout_ParagraphStyle)


@pytest.fixture(scope='session')
def strut_style():
return skia.textlayout.StrutStyle()

def test_StrutStyle_init0(strut_style):
assert isinstance(strut_style, skia.textlayout_StrutStyle)


@pytest.fixture(scope='session')
def textlayout_text_style():
return skia.textlayout.TextStyle()
Expand Down Expand Up @@ -61,3 +69,44 @@ def test_Paragraph_linebreak(paragraph_builder, textlayout_text_style, textlayou
paragraph = builder.Build()
paragraph.layout(300)
assert (paragraph.Height > 0) and (paragraph.Height > paragraph.LongestLine * 2)


stenson marked this conversation as resolved.
Show resolved Hide resolved
def test_Paragraph_strutHeightDifference(paragraph_builder, textlayout_text_style, textlayout_font_collection, paragraph_style, strut_style):
paint = skia.Paint()
paint.setColor(skia.ColorBLACK)
paint.setAntiAlias(True)

textlayout_text_style.setFontSize(50)
textlayout_text_style.setForegroundPaint(paint)

textlayout_font_collection.setDefaultFontManager(skia.FontMgr())

strut_style.setStrutEnabled(False)
paragraph_style.setStrutStyle(strut_style)

builder = skia.textlayout.ParagraphBuilder.make(
paragraph_style, textlayout_font_collection, skia.Unicodes.ICU.Make()
)
builder.pushStyle(textlayout_text_style)

builder.addText("o\no")
paragraph = builder.Build()
paragraph.layout(300)
assert paragraph.Height > 0

nostrut_paragraph_height = paragraph.Height

strut_style.setStrutEnabled(True)
strut_style.setLeading(2.0)

paragraph_style.setStrutStyle(strut_style)

builder = skia.textlayout.ParagraphBuilder.make(
paragraph_style, textlayout_font_collection, skia.Unicodes.ICU.Make()
)
builder.pushStyle(textlayout_text_style)

builder.addText("o\no")
paragraph = builder.Build()
paragraph.layout(300)
assert paragraph.Height > nostrut_paragraph_height