Skip to content

Instantly share code, notes, and snippets.

@virus-warnning
Last active April 17, 2019 01:11
Show Gist options
  • Save virus-warnning/211846ef8c407a13170ae52da48687eb to your computer and use it in GitHub Desktop.
Save virus-warnning/211846ef8c407a13170ae52da48687eb to your computer and use it in GitHub Desktop.
讓 pycairo 可以正常顯示中文的賤招,需要利用 wxPython
import subprocess
import cairo
import wx
import wx.lib.wxcairo
WIDTH = 400
HEIGHT = 300
surface = cairo.ImageSurface(
cairo.FORMAT_RGB24,
WIDTH,
HEIGHT
)
ctx = cairo.Context(surface)
ctx.rectangle(0, 0, WIDTH, HEIGHT)
ctx.set_source_rgb(0.8, 0.8, 1)
ctx.fill()
# Drawing code
app = wx.App()
wfont = wx.Font(
13,
wx.FONTFAMILY_DEFAULT,
wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL,
False,
'蘋果儷細宋'
)
cfont = wx.lib.wxcairo.FontFaceFromFont(wfont)
ctx.set_font_face(cfont)
ctx.set_font_size(13)
ctx.set_source_rgb(0, 0, 0)
ctx.move_to(20, 80)
ctx.show_text('Your grand mom, 你啊罵勒')
# End of drawing code
surface.write_to_png('text.png')
subprocess.run(['open', 'text.png'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment