Comments (12)
Here is the corrected code:
import freetype
font = freetype.Face(r"simsun.ttc")
# 绘制字符
font.set_pixel_sizes(19, 19)
font.load_char('1', freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
#font.load_char('字', freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
bitmap = font.glyph.bitmap
print(bitmap.rows, bitmap.width, bitmap.pitch)
assert(font.glyph.format == freetype.FT_GLYPH_FORMAT_BITMAP)
assert(font.glyph.bitmap.pixel_mode == freetype.FT_PIXEL_MODE_MONO)
print(bitmap.buffer)
print(len(bitmap.buffer))
left = []
right = []
for b, j in enumerate(bitmap.buffer):
b_res = list(format(j, "08b"))
for r in b_res:
if r == "1":
print("\033[1;30;46m \033[0m", end="")
else:
print("\033[1;30;40m \033[0m", end="")
if (b + 1) % bitmap.pitch == 0 and b > 0:
right.append(format(j, "02X"))
print()
else:
left.append(format(j, "02X"))
print(' '.join(left))
print(' '.join(right))
Your code also has the unfortunate problem of using pixel size 16. This particular font has some embedded bitmaps at size 12 to 17. Thus some glyph returns the embedded bitmap without needing FT_LOAD_RENDER
, some without embedded bitmp just return garbage. Anyway, it has multiple problems, and it would have save both you and me a lot of time if you start from one of the examples in the example directory... (most of them have FT_LOAD_RENDER, except those which draws outlines and obviously want the outlines returned rather than a rendered bitmap!).
from freetype-py.
I have put the code under
https://github.com/rougier/freetype-py/blob/master/examples/user-code-from-issue-193.py
from freetype-py.
As for your question in #191, you can modify the 'load_chars' line in the example to do this (it just encode from utf-8 to gb18030 then decode it back... obviously you can decode directly if your input is in gb18030):
gb18030code='字'.encode('gb18030')
print(gb18030code) # b'\xd7\xd6' , as below
font.load_char(b'\xd7\xd6'.decode('gb18030'), freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
from freetype-py.
Already answered the set_chairmap
question in #191 - there are two example scripts in the example directory, one called font-info.py, one called ftdump
. Both of them display font file info. Both of them show you what charmaps simsun.ttc
was shipped with. It was shipped with two unicode charmaps only (one for apple, one for microsoft). Hence you cannot set charmap to a big5 one, since there isn't one for that encoding.
You need to do the same as above: "big5input".decode('big5')
.
Your IDE is broken/buggy if it doesn't show freetype.FT_LOAD_RENDER
... even python's own repr gives you that..
from freetype-py.
https://github.com/rougier/freetype-py/blob/master/examples/font-info.py
and
https://github.com/rougier/freetype-py/blob/master/examples/ftdump.py
from freetype-py.
Your code has a few bugs:
- you did not specify
FT_LOAD_RENDER | FT_LOAD_TARGET_MONO
, nor checkfont.glyph.format
. Thus some glyphs have bitmap and return bitmap, some don't and just return garbage. - there is no need to do both
set_char_size
andset_pixel_sizes
- the 2nd one takes effect. - you are not reading or using
bitmap.pitch
.
from freetype-py.
As for your question in #191, you can modify the 'load_chars' line in the example to do this (it just encode from utf-8 to gb18030 then decode it back... obviously you can decode directly if your input is in gb18030):
gb18030code='字'.encode('gb18030') print(gb18030code) # b'\xd7\xd6' , as below font.load_char(b'\xd7\xd6'.decode('gb18030'), freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
Oh,Jesus.I can't believe you also linked the issue with 191, thank you so much!
from freetype-py.
As for your question in #191, you can modify the 'load_chars' line in the example to do this (it just encode from utf-8 to gb18030 then decode it back... obviously you can decode directly if your input is in gb18030):
gb18030code='字'.encode('gb18030') print(gb18030code) # b'\xd7\xd6' , as below font.load_char(b'\xd7\xd6'.decode('gb18030'), freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
In the latest version, "freetype.FT_LOAD_TARGETS['FT_LOAD_TARGET_MONO']" is used.
from freetype-py.
freetype.FT_LOAD_TARGETS['FT_LOAD_TARGET_MONO']
is a bit confusing - that argument is a bit-field, so you set them by doing a | b | c | d
. Combo flags for commonly used flags are nice, but it hides the fact that it is a bit field. For example, you can disable usage of embedded bitmaps with FT_LOAD_NO_BITMAP | otherflags
to force it to ignore embedded bitmaps. In which case, any character (Chinese and ascii) in your original code would return a garbage bitmap at size 16, rather than just the ascii characters returning garbage.
from freetype-py.
freetype.FT_LOAD_TARGETS['FT_LOAD_TARGET_MONO']
有点令人困惑 - 该参数是一个位字段,因此您可以通过执行来设置它们a | b | c | d
。常用标志的组合标志很好,但它隐藏了它是一个位字段的事实。例如,您可以禁用嵌入位图的使用,以FT_LOAD_NO_BITMAP | otherflags
强制它忽略嵌入位图。在这种情况下,原始代码中的任何字符(中文和 ascii)都会返回大小为 16 的垃圾位图,而不仅仅是返回垃圾的 ascii 字符。
I use python 3.9
In my version, the correct way to writefreetype.FT_LOAD_RENDE
is freetype.FT_LOAD_FLAGS['FT_LOAD_RENDER']
, and the correct way to write freetype.FT_LOAD_TARGET_MONO
is freetype.FT_LOAD_TARGETS['FT_LOAD_TARGET_MONO']
.
However, only IDLE raises an error. In practice, you can still use freetype.FT_LOAD_RENDER
and freetype.FT_LOAD_TARGET_MONO
despite the error.
from freetype-py.
As for your question in #191, you can modify the 'load_chars' line in the example to do this (it just encode from utf-8 to gb18030 then decode it back... obviously you can decode directly if your input is in gb18030):
gb18030code='字'.encode('gb18030') print(gb18030code) # b'\xd7\xd6' , as below font.load_char(b'\xd7\xd6'.decode('gb18030'), freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
I have a question: how should I use Set_Charmap? This piece of code doesn't seem to work:
def control_size(res):
surplus = 16 - len(res) // 2
if surplus % 2 == 0 and surplus > 0:
add_num = surplus // 2
for j in range(add_num):
res.insert(0, 0)
res.insert(0, 0)
res.append(0)
res.append(0)
elif surplus % 2 != 0 and surplus > 0:
add_num = surplus // 2
for j in range(add_num):
res.insert(0, 0)
res.insert(0, 0)
res.append(0)
res.append(0)
res.append(0)
res.append(0)
font = freetype.Face(r"C:\Windows\Fonts\simsun.ttc")
font.set_charmap(freetype.FT_ENCODINGS['FT_ENCODING_BIG5'])
# 绘制字符
font.set_pixel_sizes(16, 16)
font.load_char('2', freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGETS['FT_LOAD_TARGET_MONO'])
bitmap = font.glyph.bitmap
left = []
right = []
b_b = bitmap.buffer
control_size(b_b)
for i in range(len(b_b) // bitmap.pitch):
res = b_b[i * 2] << 8 | b_b[i * 2 + 1]
# print(b_b[i], b_b[i + 1])
b_res = list(format(res >> 5, "016b"))
for r in b_res:
if r == "1":
print("\033[1;30;43m \033[0m", end="")
else:
print("\033[1;30;40m \033[0m", end="")
print()
print(' '.join(left))
print(' '.join(right))
from freetype-py.
Already answered the
set_chairmap
question in #191 - there are two example scripts in the example directory, one called font-info.py, one calledftdump
. Both of them display font file info. Both of them show you what charmapssimsun.ttc
was shipped with. It was shipped with two unicode charmaps only (one for apple, one for microsoft). Hence you cannot set charmap to a big5 one, since there isn't one for that encoding. You need to do the same as above:"big5input".decode('big5')
.Your IDE is broken/buggy if it doesn't show
freetype.FT_LOAD_RENDER
... even python's own repr gives you that..
:) No problem, I just noticed it myself. Thank you!
from freetype-py.
Related Issues (20)
- FT_Exception: (cannot open resource) HOT 21
- Two-factor identification on PyPi HOT 5
- get_cbox() raise "invalid argument" error HOT 3
- Wrong horizontal character positioning in hello world examples HOT 9
- New release with pyinstaller fix HOT 5
- feature request: expose FreeType's COLRv1 API HOT 6
- Please publish .tar.gz sdist archives
- on exit, freetype/__init__.py", line 1233, in __del__ TypeError: 'NoneType' object is not callable HOT 10
- `segfault` with version `2.4.0` HOT 10
- possible memory leaks and issues during finalizer HOT 1
- `load_char` failed for custom fonts with right input HOT 28
- Pyinstaller cannot import module HOT 5
- AttributeError: module 'freetype' has no attribute 'Face' HOT 2
- Please provide non-bundled wheels HOT 7
- Remove the pyinstaller code if there is any more trouble
- Move freetype-py to Freetype organisation ? HOT 2
- How to change the default encoding? HOT 4
- Check upstream and sync FT_ENCODING_* naming and additional aliases.
- Add some support for CMAP format 14 (IVS/SVS characters) HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freetype-py.