File: test_font_face.rb

package info (click to toggle)
libcairo-ruby 1.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,476 kB
  • ctags: 5,116
  • sloc: ruby: 9,621; ansic: 6,413; sh: 19; makefile: 3
file content (244 lines) | stat: -rw-r--r-- 8,642 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require 'cairo'

class FontFaceTest < Test::Unit::TestCase
  include CairoTestUtils

  def test_toy_font_face_new
    only_cairo_version(1, 7, 2)

    face = Cairo::ToyFontFace.new
    default_font_family = ""
    # default_font_family = "Helvetica" if quartz?
    # default_font_family = "Arial" if win32?
    assert_equal([default_font_family,
                  Cairo::FONT_SLANT_NORMAL,
                  Cairo::FONT_WEIGHT_NORMAL],
                 [face.family, face.slant, face.weight])

    face = Cairo::ToyFontFace.new("serif")
    assert_equal(["serif", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL],
                 [face.family, face.slant, face.weight])

    face = Cairo::ToyFontFace.new(:serif)
    assert_equal(["serif", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL],
                 [face.family, face.slant, face.weight])

    face = Cairo::ToyFontFace.new("serif", :oblique)
    assert_equal(["serif", Cairo::FONT_SLANT_OBLIQUE, Cairo::FONT_WEIGHT_NORMAL],
                 [face.family, face.slant, face.weight])

    face = Cairo::ToyFontFace.new("serif", nil, :bold)
    assert_equal(["serif", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD],
                 [face.family, face.slant, face.weight])

    face = Cairo::ToyFontFace.new("serif", :italic, :bold)
    assert_equal(["serif", Cairo::FONT_SLANT_ITALIC, Cairo::FONT_WEIGHT_BOLD],
                 [face.family, face.slant, face.weight])
  end

  def test_toy_font_face_new_with_invalid_family_name
    only_cairo_version(1, 7, 2)

    exception = assert_raise(ArgumentError) do
      Cairo::ToyFontFace.new(999)
    end
    assert_equal("family name should be nil, String or Symbol: 999",
                 exception.message)
  end

  def test_user_font_face_empty
    only_cairo_version(1, 7, 2)

    face = Cairo::UserFontFace.new
    scaled_font = Cairo::ScaledFont.new(face,
                                        Cairo::Matrix.identity,
                                        Cairo::Matrix.identity,
                                        Cairo::FontOptions.new)
    assert_equal([[], [], 0],
                 scaled_font.text_to_glyphs(0, 0, "text"))
  end

  def test_user_font_face_callback
    only_cairo_version(1, 7, 2)

    face = Cairo::UserFontFace.new

    init_args = []
    face.on_init do |*args|
      init_args << args
    end

    render_glyph_args = []
    face.on_render_glyph do |*args|
      render_glyph_args << args
    end

    text_to_glyphs_args = []
    face.on_text_to_glyphs do |*args|
      text_to_glyphs_args << args
      scaled_font, utf8, data = args
      data.cluster_flags = :backward
    end

    unicode_to_glyph_args = []
    face.on_unicode_to_glyph do |*args|
      unicode_to_glyph_args << args
      scaled_font, unicode = args
      unicode
    end

    scaled_font = Cairo::ScaledFont.new(face,
                                        Cairo::Matrix.identity,
                                        Cairo::Matrix.identity,
                                        Cairo::FontOptions.new)
    result = scaled_font.text_to_glyphs(0, 0, "text")
    assert_equal([[[Cairo::ScaledFont, Cairo::Context, Cairo::FontExtents]],
                  [[Cairo::ScaledFont, ?t, Cairo::Context, Cairo::TextExtents],
                   [Cairo::ScaledFont, ?e, Cairo::Context, Cairo::TextExtents],
                   [Cairo::ScaledFont, ?x, Cairo::Context, Cairo::TextExtents]],
                  [[Cairo::ScaledFont, "text",
                    Cairo::UserFontFace::TextToGlyphsData]],
                  [[Cairo::ScaledFont, ?t],
                   [Cairo::ScaledFont, ?e],
                   [Cairo::ScaledFont, ?x],
                   [Cairo::ScaledFont, ?t]],
                  [[], [], Cairo::TextClusterFlag::BACKWARD]],
                 [classify_cairo_object(init_args),
                  classify_cairo_object(render_glyph_args),
                  classify_cairo_object(text_to_glyphs_args),
                  classify_cairo_object(unicode_to_glyph_args),
                  result])
  end

  if Cairo.satisfied_version?(1, 7, 2)
    class CustomUserFontFace < Cairo::UserFontFace
      attr_reader :init_args, :render_glyph_args
      attr_reader :text_to_glyphs_args, :unicode_to_glyph_args
      def initialize
        super
        @init_args = []
        @render_glyph_args = []
        @text_to_glyphs_args = []
        @unicode_to_glyph_args = []
      end

      def init(*args)
        @init_args << args
      end

      def render_glyph(*args)
        @render_glyph_args << args
      end

      def text_to_glyphs(*args)
        @text_to_glyphs_args << args
        scaled_font, utf8, data = args
        data.cluster_flags = :backward
      end

      def unicode_to_glyph(*args)
        @unicode_to_glyph_args << args
        scaled_font, unicode = args
        unicode
      end
    end
  end

  def test_user_font_face_class
    only_cairo_version(1, 7, 2)

    face = CustomUserFontFace.new

    scaled_font = Cairo::ScaledFont.new(face,
                                        Cairo::Matrix.identity,
                                        Cairo::Matrix.identity,
                                        Cairo::FontOptions.new)
    result = scaled_font.text_to_glyphs(0, 0, "text")
    assert_equal([[[Cairo::ScaledFont, Cairo::Context, Cairo::FontExtents]],
                  [[Cairo::ScaledFont, ?t, Cairo::Context, Cairo::TextExtents],
                   [Cairo::ScaledFont, ?e, Cairo::Context, Cairo::TextExtents],
                   [Cairo::ScaledFont, ?x, Cairo::Context, Cairo::TextExtents]],
                  [[Cairo::ScaledFont, "text",
                    Cairo::UserFontFace::TextToGlyphsData]],
                  [[Cairo::ScaledFont, ?t],
                   [Cairo::ScaledFont, ?e],
                   [Cairo::ScaledFont, ?x],
                   [Cairo::ScaledFont, ?t]],
                  [[], [], Cairo::TextClusterFlag::BACKWARD]],
                 [classify_cairo_object(face.init_args),
                  classify_cairo_object(face.render_glyph_args),
                  classify_cairo_object(face.text_to_glyphs_args),
                  classify_cairo_object(face.unicode_to_glyph_args),
                  result])
  end

  def test_user_font_face_class_and_callback
    only_cairo_version(1, 7, 2)

    face = CustomUserFontFace.new

    init_args = []
    face.on_init do |*args|
      init_args << args
    end

    render_glyph_args = []
    face.on_render_glyph do |*args|
      render_glyph_args << args
    end

    text_to_glyphs_args = []
    face.on_text_to_glyphs do |*args|
      text_to_glyphs_args << args
      scaled_font, utf8, data = args
      data.cluster_flags = :backward
    end

    unicode_to_glyph_args = []
    face.on_unicode_to_glyph do |*args|
      unicode_to_glyph_args << args
      scaled_font, unicode = args
      unicode
    end

    scaled_font = Cairo::ScaledFont.new(face,
                                        Cairo::Matrix.identity,
                                        Cairo::Matrix.identity,
                                        Cairo::FontOptions.new)
    result = scaled_font.text_to_glyphs(0, 0, "text")
    assert_equal([[[Cairo::ScaledFont, Cairo::Context, Cairo::FontExtents]],
                  [[Cairo::ScaledFont, ?t, Cairo::Context, Cairo::TextExtents],
                   [Cairo::ScaledFont, ?e, Cairo::Context, Cairo::TextExtents],
                   [Cairo::ScaledFont, ?x, Cairo::Context, Cairo::TextExtents]],
                  [[Cairo::ScaledFont, "text",
                    Cairo::UserFontFace::TextToGlyphsData]],
                  [[Cairo::ScaledFont, ?t],
                   [Cairo::ScaledFont, ?e],
                   [Cairo::ScaledFont, ?x],
                   [Cairo::ScaledFont, ?t]],
                  [],
                  [],
                  [],
                  [],
                  [[], [], Cairo::TextClusterFlag::BACKWARD]],
                 [classify_cairo_object(init_args),
                  classify_cairo_object(render_glyph_args),
                  classify_cairo_object(text_to_glyphs_args),
                  classify_cairo_object(unicode_to_glyph_args),
                  classify_cairo_object(face.init_args),
                  classify_cairo_object(face.render_glyph_args),
                  classify_cairo_object(face.text_to_glyphs_args),
                  classify_cairo_object(face.unicode_to_glyph_args),
                  result])
  end

  def classify_cairo_object(object)
    if object.is_a?(Array)
      object.collect {|obj| classify_cairo_object(obj)}
    elsif /\ACairo::/ =~ object.class.name
      object.class
    else
      object
    end
  end
end