python - How to use PIL (pillow) to draw text in any language? -
i'm rendering user input text on background image python pil(i'm using pillow).
the code simple:
draw = imagedraw.draw(im) draw.text((x, y), text, font=font, fill=font_color)
the problem is, user may input in language, how determine font
use?
ps: know have have font files first, searched , found google noto, downloaded fonts, put them in /usr/local/share/fonts/
, these fonts separated language, still can't load font can render user input texts.
noto (which literally adobe's source pro fonts different name because it's easier google market way) isn't single font, it's family of fonts. when go download them, google explicitly tells there lots of different versions lots of different target languages, 2 simple reasons that:
- if need typeset entire planet's set of known scripts, there vastly more glyphs fit in single font (opentype fonts have hard limit of 65535 glyphs per file due fact glyph ids encoded ushort fields. , fonts compositional: "letter"
℃
can letterc
, symbol°
, relies on three glyphs: 2 real glyphs, , 1 virtual composition. run out of space real fast way) , and - even if font fit glyphs, same script may need rendered rather different depending on language it's used for, having single font both chinese , japanese, or arabic , urdu, doesn't work. while opentype fonts can cope being told variation sets use, , compositional rules based on specific language tags, kind of control works great in indesign or latex, , worst thing fonts going used in control-less context (like android webview, instance).
so proper solution grab all fonts, , pick right 1 based on {script, language}
pair you're generating text for. more complicated you're trying do? yes. necessary? equally yes =)
Comments
Post a Comment