Created
April 2, 2017 12:50
-
-
Save Yoxem/f2949ebab19aa67d528ea0a42669b761 to your computer and use it in GitHub Desktop.
a test for output text by pycairo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| #under MIT license | |
| #by yoxem 2017-04-02 | |
| import cairo | |
| #設定寬高 | |
| #setting the width and height in inches. | |
| #width of a4 = 8.3 inch = 8.3*72px | |
| #height of a4 = 8.3 inch = 8.3*72px | |
| WIDTH, HEIGHT = (8.3 * 72), (11.7 * 72) | |
| #設定畫布 | |
| surface = cairo.PDFSurface("example.pdf", #匯出檔名 | |
| WIDTH, #寬度 | |
| HEIGHT) #高度 | |
| context = cairo.Context(surface) | |
| context.scale(WIDTH, HEIGHT) #正規化帆布 | |
| #設定漸層(x0,y0,x1,y1) | |
| #x,y從左上起算,以帆布比例為基準。x1 = 0.5 ,帆布一半寬 | |
| pat = cairo.LinearGradient (0.0, 0.0, 0.5,0.5) | |
| #(offset, r, g, b, a) | |
| #offset = 0 => x0,x0 | |
| pat.add_color_stop_rgba (0, 78/256, 226/256, 55/256, 0.4) | |
| #offset = 1 => x1,y1 | |
| pat.add_color_stop_rgba(1, 0, 0.7, 0, 1.0) | |
| # Rectangle(x0, y0, x1, y1) | |
| #x,y從左上起算,以帆布比例為基準。x1 = 0.5 ,帆布一半寬 | |
| context.rectangle (0, 0, 0.5, 0.5) | |
| context.set_source(pat) #設定式樣 | |
| context.fill() #填色 | |
| #移動座標(x,y) | |
| context.move_to (0.1, 0.1) | |
| '''輸入文字''' | |
| #定義字形(字族,字斜,字重) | |
| #設定文字(Font) | |
| context.select_font_face("Freeserif", | |
| cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_BOLD) | |
| #設置文字顏色(rgb) | |
| context.set_source_rgb(0.2, 0.2, 0.2) | |
| #設置文字大小 | |
| context.set_font_size(12/HEIGHT) | |
| text = "hello world" | |
| x_bearing, y_bearing, t_width, t_height = context.text_extents(text)[:4] | |
| context.move_to(0.5 - t_width / 2 - x_bearing, 0.5 - t_height / 2 - y_bearing) | |
| context.show_text(text) | |
| context.select_font_face("Noto Sans CJK TC", | |
| cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) | |
| context.set_font_size(30/HEIGHT) | |
| text="汝好 Lí hó!" | |
| x_bearing, y_bearing, t_width, t_height = context.text_extents(text)[:4] | |
| context.move_to(0.08, 0.1 - t_height / 2 - y_bearing) | |
| context.show_text(text) | |
| context.select_font_face("AR PL UMing TW", | |
| cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) | |
| context.set_font_size(12/HEIGHT) | |
| text='''臣亮言:先帝創業未半,而中道崩殂。''' | |
| x_bearing, y_bearing, t_width, t_height = context.text_extents(text)[:4] | |
| context.move_to(0.08, 0.15 - t_height / 2 - y_bearing) | |
| context.show_text(text) | |
| context.select_font_face("Freeserif", | |
| cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) | |
| text='''Sîn Liōng giân: Sian-tē chhòng-gia̍p bī-poàⁿ, jî tiong-tō peng-cho͘. Ipsum lorem too long words that is very long to say it''' | |
| context.move_to(0.08 + t_width + x_bearing+ \ | |
| 0.25 * context.text_extents(" ")[2] # 1/4 空白寬 | |
| , 0.15 - t_height / 2 - y_bearing) | |
| context.show_text(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment