rougier / freetype-py

Python binding for the freetype library
Other
304 stars 88 forks source link

Implement Face loading from memory #106

Closed madig closed 5 years ago

madig commented 5 years ago

The fourth PR to tackle loading a font from memory instead of from disk.

Not sure what to do about Face::_filename. it doesn't seem to be used anywhere.

Working on attach_file, I need a test case.

Closes https://github.com/rougier/freetype-py/pull/33, https://github.com/rougier/freetype-py/pull/103, https://github.com/rougier/freetype-py/pull/104

AppVeyorBot commented 5 years ago

:white_check_mark: Build freetype-py 1.0.114 completed (commit https://github.com/rougier/freetype-py/commit/e23a551703 by @madig)

madig commented 5 years ago

Bah. Can't get attaching bytes to work because FT_Open_Args -> memory_base expects c_ubyte when it should be POINTER(c_ubyte), which also doesn't work. Did this ever work?

anthrotype commented 5 years ago

from the CI failures, I think you need to update the multibuild submodule

AppVeyorBot commented 5 years ago

:white_check_mark: Build freetype-py 1.0.115 completed (commit https://github.com/rougier/freetype-py/commit/d301b4d0b1 by @madig)

madig commented 5 years ago

I lost patience with attach_file, no idea what the problem is. Maybe in another PR.

Edit: I have this code:

    def attach_file( self, path_or_stream ):
        if hasattr(path_or_stream, "read"):
            error = self._attach_memory(path_or_stream.read())
        else:
            try:
                u_filename = c_char_p(_encode_filename(path_or_stream))
                error = FT_Attach_File(self._FT_Face, u_filename)
            except UnicodeError:
                with open(path_or_stream, mode="rb") as f:
                    filebody = f.read()
                error = self._attach_memory(filebody)
        if error:
            raise FT_Exception(error)

    def _attach_memory(self, byte_stream):
        parameters = FT_Open_Args()
        parameters.flags = FT_OPEN_MEMORY
        parameters.memory_base = POINTER(FT_Byte)(byte_stream)
        parameters.memory_size = len(byte_stream)
        parameters.stream = None
        error = FT_Attach_Stream(self._FT_Face, parameters)
        if error:
            raise FT_Exception(error)
        self._filebodys.append(byte_stream)  # prevent gc
        return error

This errors with "Invalid argument". Meh.

AppVeyorBot commented 5 years ago

:white_check_mark: Build freetype-py 1.0.116 completed (commit https://github.com/rougier/freetype-py/commit/eb4a387387 by @madig)

madig commented 5 years ago

@rougier Please have a look. Can merge and tag if you give your ok. Do you know what Face::_filename is used for?

rougier commented 5 years ago

@madig what Face::_filename are you referring to exactly?

madig commented 5 years ago

https://github.com/rougier/freetype-py/pull/106/files#diff-c981959bb975a2ca7046d119aeb25b02L990

I removed it in this PR, but maybe you know what it's used for?

rougier commented 5 years ago

I can imagine the idea was to keep the reference to the filename but I'm not sure it is actually used elsewhere.

madig commented 5 years ago

I couldn't find a reference to it in the code 🤔 I can leave it in if it's unclear, but only assign when I think a path is passed in?

rougier commented 5 years ago

Well, if it is not used, maybe it's better to remove it completely.

madig commented 5 years ago

I like your attitude. So! Anything else to do here?

rougier commented 5 years ago

Nope. Let's pull the trigger.