File: test_context.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 (105 lines) | stat: -rw-r--r-- 3,037 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
require 'cairo'
require 'stringio'

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

  def setup
    @output = StringIO.new
    @surface = Cairo::PDFSurface.new(@output, 10, 10)
  end

  def test_new_and_destroy
    context = Cairo::Context.new(@surface)
    @surface.destroy
    assert_no_match(/%%EOF\s*\z/m, @output.string)
    context.destroy
    assert_match(/%%EOF\s*\z/m, @output.string)
  end

  def test_new_with_block
    Cairo::Context.new(@surface) do |context|
      @surface.destroy
      assert_no_match(/%%EOF\s*\z/m, @output.string)
    end
    assert_match(/%%EOF\s*\z/m, @output.string)
  end

  def test_new_with_block_and_destroy
    assert_nothing_raised do
      Cairo::Context.new(@surface) do |context|
        context.destroy
      end
    end
  end

  def test_font_face
    only_cairo_version(1, 7, 2)

    context = Cairo::Context.new(@surface)

    assert_kind_of(Cairo::FontFace, context.font_face)

    face = Cairo::ToyFontFace.new("serif")
    context.font_face = face
    assert_equal("serif", context.font_face.family)

    face = Cairo::ToyFontFace.new("sans")
    context.font_face = face
    assert_equal("sans", context.font_face.family)
  end

  def test_text_to_glyphs
    only_cairo_version(1, 7, 2)

    surface = Cairo::PDFSurface.new(StringIO.new, 10, 10)
    context = Cairo::Context.new(surface)
    scaled_font = Cairo::ScaledFont.new(context.font_face,
                                        Cairo::Matrix.identity,
                                        Cairo::Matrix.identity,
                                        Cairo::FontOptions.new)

    utf8 = "text"
    glyphs, clusters, backward = scaled_font.text_to_glyphs(0, 0, utf8)
    assert_nothing_raised do
      context.show_text_glyphs(utf8, glyphs, clusters, backward)
    end
  end

  def test_select_font_face
    only_cairo_version(1, 7, 2)

    context = Cairo::Context.new(@surface)

    face = context.font_face
    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])

    context.select_font_face
    face = context.font_face
    assert_equal([default_font_family,
                  Cairo::FONT_SLANT_NORMAL,
                  Cairo::FONT_WEIGHT_NORMAL],
                 [face.family, face.slant, face.weight])

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

  def test_select_font_face_with_invalid_family_name
    context = Cairo::Context.new(@surface)

    exception = assert_raise(ArgumentError) do
      context.select_font_face([999])
    end
    assert_equal("family name should be nil, String or Symbol: [999]",
                 exception.message)
  end
end