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

Support SkFontArguments::Palette #297

Closed
wants to merge 1 commit into from

Conversation

khaledhosny
Copy link

To allow selecting color fonts palette.

@HinTak
Copy link
Collaborator

HinTak commented Jan 2, 2025

I have let CI started, but I wish you have got in touch earlier, as I have already got some of this overlapping in my hard disk already. See #259

@HinTak
Copy link
Collaborator

HinTak commented Jan 2, 2025

@khaledhosny ci failed on mac os (which has python 3.13.1). You need to pull in the fix for #295 , which is #296 , to pass ci.

@HinTak
Copy link
Collaborator

HinTak commented Jan 2, 2025

FWIW, even namespacing these classes under FontArguments is troublesome - there is aSkFontArguments class upstream in core, and a skia::textlayout::FontArguments class in the paragraph module.

@khaledhosny
Copy link
Author

I’m not sure I get the namespacing comments. The classes are already nested in Python, skia.FontArguments.Palette(), skia.FontArguments.Palette.Overrides(), and skia.FontArguments.Palette.Override(), so I’m not sure what should I do other than that.

@khaledhosny khaledhosny force-pushed the fontargument-palette branch from 0441314 to 3bab798 Compare January 2, 2025 17:54
@HinTak
Copy link
Collaborator

HinTak commented Jan 2, 2025

This #298 isn't how it is on my hard disk (I have neighbouring code with does something else, so there are collisions while cherry-picking), nor ready for used or tested, but I think if it works, together with your pull, should mostly just be the other half of allowing one to address #259 , loading COLRv1 fonts with custom / non-default palettes.

@HinTak
Copy link
Collaborator

HinTak commented Jan 2, 2025

With the two together, you should be able to do something like this to load a colrv1 font with custom palette:

# Read font file into fontdata
scanner = skia.FontScanner()
# do stuff with FontArguments.Palette
newface = scanner.MakeFromStream(fontdata, fontargs)

@khaledhosny
Copy link
Author

khaledhosny commented Jan 2, 2025

With this PR I can already select non-default palettes and override their colors in COLR (v0 and v1) fonts, but I’m testing only with FreeType fonts:

fontMgr = skia.FontMgr.New_Custom_Empty()

face1 = fontMgr.makeFromFile(fontpath)
font1 = skia.Font(face, face.getUnitsPerEm())

fontArgs2 = skia.FontArguments()
fontArgs2.setPalette(1)
face2 = face1.makeClone(fontArgs2)
font2 = skia.Font(face2, size)

fontArgs3 = skia.FontArguments()
palette3 = skia.FontArguments.Palette(
    1,
    skia.FontArguments.Palette.Overrides(
        [
            skia.FontArguments.Palette.Override(0, skia.ColorMAGENTA),
            skia.FontArguments.Palette.Override(6, skia.ColorBLUE),
            skia.FontArguments.Palette.Override(7, skia.ColorGREEN),
            skia.FontArguments.Palette.Override(8, skia.ColorCYAN),
        ]
    ),
)
fontArgs3.setPalette(palette3)
face3 = face1.makeClone(fontArgs3)
font3 = skia.Font(face3, size)

string = "\u06DD"
text1 = skia.TextBlob.MakeFromShapedText(string, font1)
text2 = skia.TextBlob.MakeFromShapedText(string, font2)
text3 = skia.TextBlob.MakeFromShapedText(string, font3)

bounds = text.bounds()

width = math.floor(bounds.width() * 3)
height = math.floor(bounds.height())

surface = skia.Surface(width, height)
with surface as canvas:
    canvas.drawColor(skia.ColorWHITE)
    paint = skia.Paint()
    canvas.drawTextBlob(text1, -bounds.x(), -bounds.y(), paint)
    canvas.drawTextBlob(text2, bounds.width() - bounds.x(), -bounds.y(), paint)
    canvas.drawTextBlob(text3, bounds.width() * 2 - bounds.x(), -bounds.y(), paint)

output

@HinTak
Copy link
Collaborator

HinTak commented Jan 2, 2025

Interesting. Thanks for the code - some of it can go into the tests directory. (skia's resources/fonts/ is available for tests).

FWIW, I have some incomplete code which supposedly let you do face.getFontDescriptor().setPaletteIndex(N) (and short-circuiting to face.setPaletteIndex(N)) to switch. Does not work (yet).

@HinTak
Copy link
Collaborator

HinTak commented Jan 3, 2025

If the asserts are not logically independent I.e. you just want an exact match of a composite structure, it might be clearer to do assert x and y and z a single mulit-line assert, any mis-match is considered a failure. If there are multiple independent/stages ways of failure, you do a fixture then multiple tests.

@HinTak
Copy link
Collaborator

HinTak commented Jan 3, 2025

I think face.getFontDescriptor().setPaletteIndex(N) not working is an upstream bug but I have not gotten round to file it yet. Basically the paletteindex is stored in an intermediate structure but never passed onto the font renderer. In the case of colrv1, freetype never gets it set. But similar comments applies to the colrv0 path in the directwrite rendering path too.

@HinTak
Copy link
Collaborator

HinTak commented Jan 3, 2025

assert isinstance... is typically one per test, and the object itself done as a re-used fixture. See the other tests.

@HinTak
Copy link
Collaborator

HinTak commented Jan 3, 2025

See for example the fixture at

@pytest.fixture(scope='session')
and the init test below.

@HinTak
Copy link
Collaborator

HinTak commented Jan 3, 2025

And all the fxtures finally get passed to the usage test at

def test_Paragraph_linebreak(paragraph_builder, textlayout_text_style, textlayout_font_collection, paragraph_style):

To allow selecting color fonts palette.
@khaledhosny khaledhosny force-pushed the fontargument-palette branch from 3bab798 to 6e18951 Compare January 3, 2025 12:54
@khaledhosny
Copy link
Author

Interesting. Thanks for the code - some of it can go into the tests directory. (skia's resources/fonts/ is available for tests).

That would be nice, but testing palette overrides will requiring checking the rendered output (possibly against a reference image), but I don’t see any tests doing something like this.

@HinTak
Copy link
Collaborator

HinTak commented Jan 3, 2025

That would be nice, but testing palette overrides will requiring checking the rendered output (possibly against a reference image), but I don’t see any tests doing something like this.

Well, setting and getting it back should work. It is probably possible to render to a surface and detect it is "mostly red", say, just by count the red pixels. I have a test in the paragraph part, which used to have trouble with rendering "\n" as a new line rather than .notdef. The test is simply that the rendered area should be tall than wide, whichever the default font is.

@@ -46,6 +46,44 @@ def test_FontArguments_setCollectionIndex(fontarguments):
fontarguments.setCollectionIndex(0)


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there needs to be a dozen more tests for each of the new properties/methods of the new classes added.

return std::vector<Override>(
self.overrides, self.overrides + self.overrideCount);
},
&SetPaletteOverrides)
Copy link
Collaborator

@HinTak HinTak Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need a keep_Alive policy here too? On the set method.

@HinTak
Copy link
Collaborator

HinTak commented Jan 5, 2025

I am starting to see the variationposition code from which this modelled from a bit buggy about life times and zero length arguments...

assert palette.index == index
assert len(palette.overrides) == len_overrides
assert palette.overrideCount == len_overrides
assert repr(palette) == f"Palette(index={index}, overrideCount={len_overrides})"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably not have a repr test - since the __repr__ method itself is somewhat a personal choice and subjected to changes later too.

assert palette.overrideCount == len_overrides
assert repr(palette) == f"Palette(index={index}, overrideCount={len_overrides})"
for i, override in enumerate(palette.overrides):
assert repr(override) == f"Override(index={override.index}, color={override.color})"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto on testing the precise form of repr outcome.

Specify a palette to use and overrides for palette entries.
)docstring",
py::arg("palette"))
.def("setPalette",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some setPaletteIndex/getPaletteIndex methods in other classes. Perhaps do both?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wonder whether this should be a read/write property instead.

@HinTak
Copy link
Collaborator

HinTak commented Jan 8, 2025

You added 3 classes, so there should be 3 init tests, in addition to the class method tests...

self.overrides, self.overrides + self.overrideCount);
},
&SetPaletteOverrides)
.def_readwrite("index", &SkFontArguments::Palette::index)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PaletteIndex might be a better choice of name, given font argument might have other indices.

@HinTak
Copy link
Collaborator

HinTak commented Jan 10, 2025

I have had a better look at the example code. I think this pull is substantially incomplete (in addition to all the detailed comments so far) - to be of any use to somebody who isn't the font author, it really needs the face.getFontDescriptor().get*Palette*Override* method to find out what is settable (or use another tool to examine the font to find out what is settable). Hence this is of no use without those.

So this is not going to be merged without substantially additional work.

@khaledhosny khaledhosny deleted the fontargument-palette branch January 10, 2025 16:39
@HinTak
Copy link
Collaborator

HinTak commented Jan 10, 2025

I see your example must have used one of your own authored arabic fonts (Amiri Quran?). That's fair enough, but an example with one of the English fonts would be preferred; as well as some tests with the font files in skia/resources/fonts/ .

I"d have preferred that this stays open until you or somebody else improves on it. Nevermind.

@khaledhosny
Copy link
Author

Well, I added bindings for existing Skia APIs, but you are asking for palette discoverability APIs that Skia does not currently have as a prerequisite of merging this PR, so I don’t know what I was supposed to do.

@HinTak
Copy link
Collaborator

HinTak commented Mar 5, 2025

I wrote earlier that palette discovery looks to be possible binding around face.getFontDescriptor().get*Palette*Override* (the equvalent c++ code behind).

@HinTak
Copy link
Collaborator

HinTak commented Mar 5, 2025

I have the python bits for face.getFontDescriptor()... but I haven't got time to finish it yet.

@khaledhosny
Copy link
Author

I wrote earlier that palette discovery looks to be possible binding around face.getFontDescriptor().get*Palette*Override* (the equvalent c++ code behind).

This does not query the font, it returns whatever the user have set.

@HinTak
Copy link
Collaborator

HinTak commented Mar 5, 2025

Yes, I believe that is an upstream bug. Skia-python is not against carry interim fixes for upstream bugs, before/during they are being accepted.

@khaledhosny
Copy link
Author

Skia has no code whatsoever to query font palettes, so again you blocked this PR on developing code that upstream does not have. If Skia is fine for its users with its API as it is, why can’t this Python bindings.

@HinTak
Copy link
Collaborator

HinTak commented Mar 5, 2025

Example - #290 - it is an upstream bug for which a proposed fix was posted a while ago by somebody else, and I intend to take it on here at some point, even if upstream hasn't accepted it yet. That fix helps avoiding people filing bug reports with spurious and misleading information here, so we should have it even if upstream hasn't accepted it.

@HinTak
Copy link
Collaborator

HinTak commented Mar 6, 2025

Note I am not "blocking" this PR. I am merely saying it is incomplete / need more work, because as it is, it requires special input font files and example uses of the additional code requires intimate knowledge of the special test font files. Until it becomes more generally useful to other input font files, the new code is only useful to you, the supplier/author of the special input font file.

IMHO, PR (in general, not specifically for this project) are just NOT merged by default, and nobody (including me) should assume any pull submitted will be merged. It just isn't, and shouldn't.

@HinTak
Copy link
Collaborator

HinTak commented Mar 6, 2025

Also, FWIW, other pulls have stayed opened for months / years without a demand to merge. Typically mine get merged here after after 2 months when I say it is ready. You are demanding a merge or withdrawal in 10 days?

@khaledhosny
Copy link
Author

So this PR is incomplete because Skia, the upstream, is missing an API you think it important to use the existing Skia API that this PR exposes to Python users?

CSS allow selecting/overriding font palettes (just like the API this PR binds), but browsers lack any API to query font palettes, so CSS support for font palettes is unusable, too?

Skia/CSS allow setting OpenType font features, but lack APIs to query fonts for supported features, so Skia/CSS support for font features is unusable, too?

To be frank, it feels like you are just making things up, and while no one should expect their code contribution to be accepted, you should have stated your (made up) objection up front, instead of wasting my time with minute details while then later stating this PR can never be merged until Skia develop some API you think are a prerequisite for using this existing API.

@HinTak
Copy link
Collaborator

HinTak commented Mar 6, 2025

FWIW, #181 stayed open for 18 months, before it was superceded and closed. Personally I think I would prefer to spend my time (and you spend yours) actually coding, than continuing this exchange. Sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants