<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # PyCairo 中的文字 > 原文: [https://zetcode.com/gfx/pycairo/text/](https://zetcode.com/gfx/pycairo/text/) 在 PyCairo 教程的這一部分中,我們將處理文本。 ## 靈魂伴侶 在第一個示例中,我們將在窗口上顯示一些歌詞。 ```py 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 歌曲的部分歌詞。 ```py cr.select_font_face("Purisa", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) ``` 在這里,我們選擇字體。 該方法采用三個參數:字體系列,字體傾斜度和字體粗細。 ```py cr.set_font_size(13) ``` 在這里,我們指定字體大小。 ```py cr.move_to(20, 30) cr.show_text("Most relationships seem so transitory") ``` 我們通過指定文本的位置并調用`show_text()`方法在窗口上顯示文本。 ![Soulmate](https://img.kancloud.cn/e7/21/e721c9dd047d7dd49cbccc90517524bd_372x266.jpg) 圖:靈魂伴侶 ## 居中文字 接下來,我們將展示如何在窗口上居中放置文本。 ```py 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") ``` 該代碼將使文本在窗口上居中。 即使我們調整窗口大小,它仍然居中。 ```py w, h = self.get_size() ``` 為了使文本在窗口上居中,有必要獲取窗口工作區的大小。 ```py cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) cr.set_font_size(60) ``` 我們選擇要顯示的字體及其大小。 ```py (x, y, width, height, dx, dy) = cr.text_extents("ZetCode") ``` 我們得到了文本范圍。 這些是描述文字的數字。 我們的示例需要文本的寬度。 ```py cr.move_to(w/2 - width/2, h/2) cr.show_text("ZetCode") ``` 我們將文本放置在窗口的中間,并使用`show_text()`方法顯示它。 ![Centered text](https://img.kancloud.cn/6b/70/6b70acaaaaee9901d7a2ecd46b573e2b_302x226.jpg) 圖:居中文本 ## 帶陰影的文字 現在,我們將在窗口上創建一個陰影文本。 ```py 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") ``` 要創建陰影,我們將文本繪制兩次。 以不同的顏色。 第二個文本向右和向下移動一點。 ```py cr.set_source_rgb(0, 0, 0) cr.move_to(40, 60) cr.show_text("ZetCode") ``` 第一個文本用黑色墨水繪制。 它充當陰影。 ```py cr.set_source_rgb(0.5, 0.5, 0.5) cr.move_to(43, 63) cr.show_text("ZetCode") ``` 第二個文本用灰色墨水繪制。 它向右和向下移動了 3px。 ![Shaded text](https://img.kancloud.cn/06/3a/063a43717535de821fd410a321c96b84_372x266.jpg) 圖:陰影文本 ## 漸變填充文本 以下示例將產生很好的效果。 我們將使用一些線性漸變填充文本。 ```py def on_draw(self, wid, cr): cr.set_source_rgb(0.2, 0.2, 0.2) cr.paint() h = 90 cr.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() ``` 我們在充滿線性漸變的窗口上繪制文本。 顏色是一些橙色。 ```py cr.set_source_rgb(0.2, 0.2, 0.2) cr.paint() ``` 為了使其更具視覺吸引力,我們將背景涂成深灰色。 ```py 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) ``` 將創建線性漸變。 ```py cr.move_to(15, 80) cr.text_path("ZetCode") cr.set_source(lg) cr.fill() ``` 文本顯示在窗口上。 我們使用漸變作為繪畫源。 ![Text filled with gradient](https://img.kancloud.cn/ba/fd/bafdcef4a8628006b1094e2796b42072_402x176.jpg) 圖:用漸變填充的文本 ## 逐個字母 為此,我們將逐個字母顯示一個文本。 這些字母將被繪制得有些延遲。 ```py #!/usr/bin/python ''' ZetCode PyCairo tutorial This program shows text letter by letter. author: Jan Bodnar website: zetcode.com last edited: August 2012 ''' from gi.repository import Gtk, GLib import cairo class cv(object): SPEED = 800 TEXT_SIZE = 35 COUNT_MAX = 8 class 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 = True self.count = 0 self.text = [ "Z", "e", "t", "C", "o", "d", "e" ] def on_timer(self): if not self.timer: return False self.darea.queue_draw() return True def 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 = 0 for i in range(self.count): (x, y, width, height, dx, dy) = cr.text_extents(self.text[i]) dis += width + 2 cr.move_to(dis + 30, 50) cr.show_text(self.text[i]) self.count += 1 if self.count == cv.COUNT_MAX: self.timer = False self.count = 0 def main(): app = Example() Gtk.main() if __name__ == "__main__": main() ``` 在我們的示例中,我們將在 GTK 窗口上逐個字母地繪制`"ZetCode"`字符串,并稍作延遲。 ```py self.text = [ "Z", "e", "t", "C", "o", "d", "e" ] ``` 這是要在窗口上顯示的字母列表。 ```py cr.select_font_face("Courier", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) ``` 我們選擇粗體的 Courier 字體。 ```py for i in range(self.count): (x, y, width, height, dx, dy) = cr.text_extents(self.text[i]) dis += width + 2 cr.move_to(dis + 30, 50) cr.show_text(self.text[i]) ``` 在這里,我們逐個字母地繪制文本。 我們獲得每個字母的寬度并計算 x 軸上的距離。 ## 字形 `show_text()`方法僅適用于簡單的文本呈現。 Cairo 開發者將其稱為玩具方法。 使用字形可以完成更專業的文本渲染。 標志符號是圖形符號,可提供字符形式。 字符提供含義。 它可以有多個字形。 角色沒有內在的外觀。 字形沒有內在的含義。 請注意,Pango 庫解決了許多常見的編程要求,包括文本。 ```py 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 = 0 for y in range(20): for x in range(35): glyphs.append((index, x*15 + 20, y*18 + 20)) index += 1 cr.show_glyphs(glyphs) ``` 該代碼顯示了所選字體的 700 個字形。 ```py glyphs = [] ``` 字形列表將存儲三個整數值。 第一個值是字形到所選字體類型的索引。 第二和第三值是字形的 x,y 位置。 ```py cr.show_glyphs(glyphs) ``` `show_glyphs()`方法在窗口上顯示字形。 ![Glyphs](https://img.kancloud.cn/4f/1a/4f1aeefb12a1be3855f29a9cb683cde0_562x122.jpg) 圖:字形 本章介紹了 PyCairo 中的文本。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看