PyCairo 中的文本
PyCairo 教程的這個(gè)部分,我們將與文本打交道。
靈魂伴侶
在第一個(gè)例子中,我們將在窗口中顯示一些歌詞。
def on_draw(self, wid, cr):cr.set_source_rgb(0.1, 0.1, 0.1)cr.select_font_face("Purisa", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)cr.set_font_size(13)cr.move_to(20, 30)cr.show_text("Most relationships seem so transitory")cr.move_to(20, 60)cr.show_text("They're all good but not the permanent one")cr.move_to(20, 120)cr.show_text("Who doesn't long for someone to hold")cr.move_to(20, 150)cr.show_text("Who knows how to love without being told")cr.move_to(20, 180)cr.show_text("Somebody tell me why I'm on my own")cr.move_to(20, 210)cr.show_text("If there's a soulmate for everyone")這段代碼中,我們顯示了來自于 Natasha Bedingfields Soulmate 這首歌的歌詞的一部分。
cr.select_font_face("Purisa", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)此處我們選擇 font face。這個(gè)方法接收三個(gè)參數(shù),font family,font slant 和font weight.
cr.set_font_size(13)此處我們指定字體大小。
cr.move_to(20, 30)cr.show_text("Most relationships seem so transitory")我們通過為文本指定位置并調(diào)用 show_text() 方法將文本顯示在窗口中。
居中的文字
接下來我們將演示如何在窗口中居中顯示文字。
def on_draw(self, wid, cr):w, h = self.get_size()cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_BOLD)cr.set_font_size(60)(x, y, width, height, dx, dy) = cr.text_extents("ZetCode")cr.move_to(w / 2 - width / 2, h / 2)cr.show_text("ZetCode")這段代碼將一段文字放在窗口的中心。即使調(diào)整了窗口的大小,也依然會(huì)位于中心。
w, h = self.get_size()為了使文字位于窗口的中心,需要獲取窗口的客戶區(qū)域的大小。
cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_BOLD)cr.set_font_size(60)我們選擇一個(gè)字體及字體大小用于顯示。
(x, y, width, height, dx, dy) = cr.text_extents("ZetCode")我們獲取文本 extents,這是一些用來描述文字的數(shù)字。我們的例子需要文字的寬度。
cr.move_to(w / 2 - width / 2, h / 2)cr.show_text("ZetCode")我們將文字定位在窗口的中間,并用 show_text() 顯示它。
陰影文字
現(xiàn)在我們將在窗口中創(chuàng)建一段陰影文字。
def on_draw(self, wid, cr):cr.select_font_face("Serif", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_BOLD)cr.set_font_size(50)cr.set_source_rgb(0, 0, 0)cr.move_to(40, 60)cr.show_text("ZetCode")cr.set_source_rgb(0.5, 0.5, 0.5)cr.move_to(43, 63)cr.show_text("ZetCode")為了創(chuàng)建陰影,我們以不同的顏色繪制文字兩次。第二段文字向右下方移動(dòng)了一點(diǎn)。
cr.set_source_rgb(0, 0, 0)cr.move_to(40, 60)cr.show_text("ZetCode")第一段文字用黑色墨水繪制。它做為陰影。
cr.set_source_rgb(0.5, 0.5, 0.5)cr.move_to(43, 63)cr.show_text("ZetCode")第二段文字以一些灰色墨水繪制。它向右下邊移動(dòng)了 3px。
漸變填充文字
下面的例子將創(chuàng)建一個(gè)很棒的效果。我們將用線性漸變來填充文字。
def on_draw(self, wid, cr):cr.set_source_rgb(0.2, 0.2, 0.2)cr.paint()h = 90cr.select_font_face("Serif", cairo.FONT_SLANT_ITALIC,cairo.FONT_WEIGHT_BOLD)cr.set_font_size(h)lg = cairo.LinearGradient(0, 15, 0, h * 0.8)lg.set_extend(cairo.EXTEND_REPEAT)lg.add_color_stop_rgb(0.0, 1, 0.6, 0)lg.add_color_stop_rgb(0.5, 1, 0.3, 0)cr.move_to(15, 80)cr.text_path("ZetCode")cr.set_source(lg)cr.fill()我們?cè)诖翱谥欣L制一段文字,并以線性漸變填充。顏色是一些桔黃色。
cr.set_source_rgb(0.2, 0.2, 0.2)cr.paint()為了使它的視覺效果更吸引人,我們用深灰色來繪制背景。
lg = cairo.LinearGradient(0, 15, 0, h * 0.8)lg.set_extend(cairo.EXTEND_REPEAT)lg.add_color_stop_rgb(0.0, 1, 0.6, 0)lg.add_color_stop_rgb(0.5, 1, 0.3, 0)創(chuàng)建線性漸變。
cr.move_to(15, 80)cr.text_path("ZetCode")cr.set_source(lg)cr.fill()文字被顯示于窗口中。我們用漸變作為 source 來繪制。
逐字符顯示
在這個(gè)效果中,我們將逐字符顯示一段文字。字符的顯示將有一些延時(shí)。
#!/usr/bin/python''' ZetCode PyCairo tutorialThis program shows text letter by letter.author: Jan Bodnar website: zetcode.com last edited: August 2012 '''import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GLib import cairoclass cv(object):SPEED = 800TEXT_SIZE = 35COUNT_MAX = 8class Example(Gtk.Window):def __init__(self):super(Example, self).__init__()self.init_ui()self.init_vars()def init_ui(self):self.darea = Gtk.DrawingArea()self.darea.connect("draw", self.on_draw)self.add(self.darea)GLib.timeout_add(cv.SPEED, self.on_timer)self.set_title("Letter by letter")self.resize(350, 200)self.set_position(Gtk.WindowPosition.CENTER)self.connect("delete-event", Gtk.main_quit)self.show_all()def init_vars(self):self.timer = Trueself.count = 0self.text = ["Z", "e", "t", "C", "o", "d", "e"]def on_timer(self):if not self.timer: return Falseself.darea.queue_draw()return Truedef on_draw(self, wid, cr):cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_BOLD)cr.set_font_size(cv.TEXT_SIZE)dis = 0for i in range(self.count):(x, y, width, height, dx, dy) = cr.text_extents(self.text[i])dis += width + 2cr.move_to(dis + 30, 50)cr.show_text(self.text[i])self.count += 1if self.count == cv.COUNT_MAX:self.timer = Falseself.count = 0def main():app = Example()Gtk.main()if __name__ == "__main__":main()在我們的例子中,我們以一定的延時(shí),逐字符的在 GTK 窗口中顯示 “ZetCode” 字串。
self.text = ["Z", "e", "t", "C", "o", "d", "e"]這是將在窗口中顯示的字符的列表。
cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_BOLD)我們以 bold weight 選擇一個(gè) Courier font face。
for i in range(self.count):(x, y, width, height, dx, dy) = cr.text_extents(self.text[i])dis += width + 2cr.move_to(dis + 30, 50)cr.show_text(self.text[i])此處我們逐字符繪制文本。我們獲取每個(gè)字符的寬度,然后計(jì)算沿 x 軸需要移動(dòng)的距離。
Glyphs
show_text() 方法只適合簡(jiǎn)單的文本渲染。Cairo 開發(fā)者稱它為玩具方法。更專業(yè)的文本渲染通過 glyphs 完成。一個(gè) glyph 是為一個(gè)字符提供一個(gè)外形的圖形符號(hào)。一個(gè)字符提供一種含義。它可以有多個(gè) glyphs。字符沒有內(nèi)在的外觀。glyph 沒有內(nèi)在的含義。
注意,許多需要處理文字的通用編程需要由 Pango 庫來處理。
def on_draw(self, wid, cr):cr.select_font_face("Serif", cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL)cr.set_font_size(13)glyphs = []index = 0for y in range(20):for x in range(35):glyphs.append((index, x * 15 + 20, y * 18 + 20))index += 1cr.show_glyphs(glyphs)這段代碼顯示選中的字體中的 700 個(gè) glyphs。
glyphs = []glyphs 列表將存儲(chǔ)三個(gè)整數(shù)值。第一個(gè)值是 glyph 在選中的字體類型中的索引。第二個(gè)和第三個(gè)是 glyph 的 x,y 位置。
cr.show_glyphs(glyphs)show_glyphs() 方法在窗口中顯示 glyphs。
本章討論了 PyCairo 中的文本。
原文
Done.
總結(jié)
以上是生活随笔為你收集整理的PyCairo 中的文本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PyCairo 中的变换
- 下一篇: PyCairo 中的图片