rougier / freetype-py

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

can't load_char() for FT_LOAD_TARGET_MONO #105

Open kanryu opened 5 years ago

kanryu commented 5 years ago

Hi all,

I found this library for monochrome font glyphs. If it is a standard, Gray 8 is set, which can be obtained. However monochrome seems impossible.

import freetype

face = freetype.Face("mplus-1p-black.ttf")
face.set_char_size( 1500 )
face.load_char('S', freetype.FT_LOAD_TARGETS['FT_LOAD_TARGET_MONO'])
bitmap = face.glyph.bitmap
print(bitmap.buffer) # error!

Comments?

rougier commented 5 years ago

What is the error you get? Did you check the example with TARGET_MONO?

kanryu commented 5 years ago

@rougier What is the error you get? Did you check the example with TARGET_MONO? bitmap.buffer is None.

I noticed that the program should be modified in this way.

import freetype

face = freetype.Face("mplus-1p-black.ttf")
face.set_char_size( 1500 )
flags = freetype.FT_LOAD_FLAGS['FT_LOAD_RENDER'] | freetype.FT_LOAD_FLAGS['FT_LOAD_MONOCHROME'] | freetype.FT_LOAD_TARGETS['FT_LOAD_TARGET_MONO']
face.load_char('S', flags)
bitmap = face.glyph.bitmap
print(bitmap.buffer) # OK

It certainly works if I fix this way, but since this fact can not be understood by reading freetype-py document, please introduce it in 'samples'. (The above source code will be donated to this project)

HinTak commented 5 years ago

The Freetype documentation has this paragraph:

"If FT_LOAD_RENDER is also set, the glyph is rendered in the corresponding mode (i.e., the mode that matches the used algorithm best). An exception is FT_LOAD_TARGET_MONO since it implies FT_LOAD_MONOCHROME."

It implies the converse also: if you don't have FT_LOAD_RENDER, bitmap is not generated and you need to call FT_Render_Glyph manually to generate a bitmap.

This is at the bottom of FT_LOADTARGET* 's description.

kanryu commented 5 years ago

@HinTak I am discussing freetype-py, where freetype has nothing to do with it.

HinTak commented 5 years ago

freetype-py allows you to interact with freetype... it is based on ctypes and basically uses freetype's C API . freetype-py provides a FT_Render_Glyph which uses freetype's FT_Render_Glyph too. Anyway, you need to use freetype-py's FT_Render_Glyph, if you want to skip FT_LOAD_RENDER in FT_Load_Glyph. There are usage where loading without rendering is useful.

HinTak commented 16 hours ago

I should explain this in python terms, if you do face.load_char('S', flags) where flags does not include ...LOAD_RENDER then face.glyph.bitmap is not populated, but face.glyph.outline is. There are usage where you want the outlines (and draw the outlines), or you want to do some manipulation before calling face.glyph.render(...). We have a few examples of the former for the former cases, in the /examples directory - look at the ...outline... ones, where NOT having ...LOAD_RENDER is intentional.

As I already explained, this behavior is documented. If you feel this is inadequately documented, please free free to submit a pull.