File: font_features.rb

package info (click to toggle)
ruby-gnome2 3.1.0-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,072 kB
  • ctags: 17,433
  • sloc: ansic: 93,621; ruby: 62,273; xml: 335; sh: 246; makefile: 25
file content (148 lines) | stat: -rw-r--r-- 3,527 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Copyright (c) 2016 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
=begin
=  Pango/Font Features

 This demonstrates support for OpenType font features with
 Pango attributes. The attributes can be used manually or
 via Pango markup.
=end
class FontFeaturesDemo
  def initialize(_main_window)
    initialize_builder
    initialize_widgets
    builder_add_callback_symbols

    update(@toggle, @entry, @font, @settings, @label)
  end

  def run
    if !@window.visible?
      @window.show_all
    else
      @window.destroy
    end
    @window
  end

  private

  def initialize_builder
    @builder = Gtk::Builder.new(:resource => "/font_features/font-features.ui")
  end

  def initialize_widgets
    @window = @builder["window"]
    @label = @builder["label"]
    @settings = @builder["settings"]
    @resetbutton = @builder["reset"]
    @font = @builder["font"]
    @numcasedefault = @builder["numcasedefault"]
    @numspacedefault = @builder["numspacedefault"]
    @fractiondefault = @builder["fractiondefault"]
    @stack = @builder["stack"]
    @entry = @builder["entry"]
    @toggle = []

    %w(kern liga dlig hlig clig smcp c2sc lnum onum pnum tnum frac afrc zero
       nalt swsh calt hist salt ss01 ss02 ss03 ss04 ss05).each do |name|
      @toggle << @builder[name]
    end
    @text = nil
  end

  def builder_add_callback_symbols
    @builder.connect_signals do |name|
      case name
      when "reset"
        reset_callback
      when "update"
        update_callback
      when "switch_to_entry"
        switch_to_entry_callback
      when "switch_to_label"
        switch_to_label_callback
      when "entry_key_press"
        entry_key_press_callback
      end
    end
  end

  def reset_callback
    proc do
      @numcasedefault.active = true
      @numspacedefault.active = true
      @fractiondefault.active = true
      @toggle.each do |widget|
        if widget.class != Gtk::RadioButton
          widget.active = false
          widget.sensitive = false
        end
      end
    end
  end

  def update_callback
    proc do
      update(@toggle, @entry, @font, @settings, @label)
    end
  end

  def switch_to_entry_callback
    proc do
      @text = @entry.text
      @stack.visible_child_name = "entry"
    end
  end

  def switch_to_label_callback
    proc do
      @text = nil
      @stack.set_visible_child_name("label")
      update(@toggle, @entry, @font, @settings, @label)
    end
  end

  def entry_key_press_callback
    proc do |_entry, event|
      if event.keyval == Gdk::Keyval::KEY_Escape
        @entry.text = @text
        @text = nil
        @stack.set_visible_child_name("label")
        update(@toggle, @entry, @font, @settings, @label)
      end
      Gdk::Event::PROPAGATE
    end
  end

  def update(toggle, entry, font, settings, label)
    text = entry.text
    font_desc = font.font
    s = ""
    has_feature = false

    toggle.each do |widget|
      next unless widget.sensitive?

      if widget.class == Gtk::RadioButton
        if widget.active?
          s += ", " if has_feature
          s += widget.builder_name
          s += " 1"
          has_feature = true
        end
      else
        s += ", " if has_feature
        s += widget.builder_name
        s += widget.active? ? " 1" : " 0"
        has_feature = true
      end
    end
    font_settings = s
    settings.text = font_settings

    s = "<span font_desc='#{font_desc}' font_features='#{font_settings}'>#{text}</span>"
    label.markup = s
  end
end