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

Optional cross-platform outline rendering mode #451

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

stenson
Copy link

@stenson stenson commented Dec 11, 2024

In order to use FontGoggles as a code-only and cross-platform dependency in coldtype, I’ve refactored some code to use a new wrapper for all the mac-only operations in Lib/fontgoggles/compile and Lib/fontgoggles/font. The main outcome is that there’s now an additional RecordingPen-based code path for all of the outline-building (which I use in coldtype). Most of the new code here is in Lib/fontgoggles/misc/platform.py.

One odd thing / TODO here is that the code is now cross-platform, but the test I wrote assumes the test only runs on macOS (since it’s assuming Platform.UseCocoa will be True in the default setup). I’m not sure if this should be spelled out explicitly, since the tests only run on macOS anyway.

I’ve also wrapped the Turbo build step in a try/except since the darwin test isn't fully accurate, i.e. if you install this fork of fontgoggles into DrawBot, the build_lib.sh step fails even though it's building on mac. Obviously that's not ideal since the build step is necessary for the GUI version to work, though does ensure a more constrained environment like DrawBot or Blender python can install this without issue.

Thanks for taking a look!

Copy link
Owner

@justvanrossum justvanrossum left a comment

Choose a reason for hiding this comment

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

I'm not sure what to do about the "Turbo" build. Maybe it is fine for now. Ideally you'd distribute a wheel for macOS that includes the Turbo binary, but that is a pain to set up (and TBH I wouldn't know where to start at this point).

Let me know what you think about my other comments.

CAN_COCOA = False


class Platform():
Copy link
Owner

Choose a reason for hiding this comment

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

I would prefer to get rid of this additional class. All its methods are static, so might as well be functions in this module. If you like the "[Pp]latform" prefix in the client code, you could import the module "platform" instead of its contents, but I don't mind if you'd just import the needed names, ie. from ..misc.platform import pathFromGlyph.

(Although then perhaps it's nicer to stick with the original funtion names, eg. makePathFromGlyph)

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense! I went with just importing the names. The idea of the Platform class was just to have an easy way to set the global variable (Platform.UseCocoa) but it’s just as easy to import as platform and then set platform.USE_COCOA (as in the updated noCocoa test)

Tests/test_noCocoa.py Outdated Show resolved Hide resolved
Tests/test_noCocoa.py Outdated Show resolved Hide resolved
Lib/fontgoggles/misc/platform.py Outdated Show resolved Hide resolved
requirements.txt Outdated
@@ -4,15 +4,15 @@ py2app==0.28.8
pyobjc==10.3.1
corefoundationasyncio==0.0.1
cocoa-vanilla==0.6.0
blackrenderer==0.6.0
fonttools[woff,lxml,unicode,ufo,type1]==4.53.1
### blackrenderer==0.6.0 # moved to setup.py
Copy link
Owner

Choose a reason for hiding this comment

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

These should stay, as they get updated by dependabot. Also, in setup.py you should ideally not pin dependencies, although you are right they should be mentioned (but either without version, or with a minimal version).

Copy link
Author

Choose a reason for hiding this comment

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

Totally makes sense. I did keep the pinned == for python-bidi since there’s the issue of the interface change there, does that work?

@justvanrossum
Copy link
Owner

I've fixed the ufo2ft problem, so if you rebase/merge main you should be able to unpin it.

@justvanrossum
Copy link
Owner

(I messed up some other dependencies in the meantime, working on it)

@justvanrossum
Copy link
Owner

Should be all good after #454

@justvanrossum
Copy link
Owner

justvanrossum commented Jan 11, 2025

Applogies for my slow progress on this.

I have one more request regarding the platform module: that all functions/classes that need alternate implementations aren't wrapped, but left as is, and that compatible implementations are provided in the not USE_COCOA case. This avoids some function call overhead.

For example, for the pathFromGlyph() case, I would prefer this:

if USE_COCOA:
    from ..mac.makePathFromOutline import makePathFromGlyph
else:
    def makePathFromGlyph(font, gid):
        rp = RecordingPen()
        font.draw_glyph_with_pen(gid, rp)
        return rp

There are two cases where you don't provide an alternative implementation, yet don't raise NotImplementedError either. Is that intentional? I would prefer to raise the error:

if USE_COCOA:
    from ..mac.drawing import nsColorFromRGBA
else:
    def nsColorFromRGBA(c):
        raise NotImplementedError()

makePathFromArrays() is special: your implementation uses different arguments. I would suggest to solve that like this:

if USE_COCOA:
    from ..mac.makePathFromOutline import makePathFromArrays
else:
    makePathFromArrays = None  # caller should use pen protocol instead

And then in VarGlyph.getOutline():

    def getOutline(self):
        if makePathFromArrays is not None:
            return makePathFromArrays(self.getPoints(), self.tags, self.contours)
        else:
            rp = RecordingPen()
            self.draw(rp)
            return rp

For the PlatformPen I would suggest this:

if USE_COCOA:
    PlatformPen = CocoaPen
else:
    class PlatformPen(RecordingPen):
        @property
        def path(self):
            return self

It could be one big if USE_COCOA: ... else: ... statement.

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