Class: Skia::RRect

Inherits:
Base
  • Object
show all
Defined in:
lib/skia/rrect.rb

Constant Summary collapse

CORNERS =
i[upper_left upper_right lower_right lower_left].freeze
TYPES =
i[empty rect oval simple nine_patch complex].freeze

Instance Attribute Summary

Attributes inherited from Base

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#null?, release_callback, #to_ptr

Constructor Details

#initialize(ptr = nil) ⇒ RRect

Returns a new instance of RRect.

Raises:



8
9
10
11
12
13
14
15
# File 'lib/skia/rrect.rb', line 8

def initialize(ptr = nil)
  created = ptr.nil?
  ptr ||= Native.sk_rrect_new
  raise Error, 'Failed to create rounded rectangle' if ptr.nil? || ptr.null?

  super(ptr, :sk_rrect_delete)
  Native.sk_rrect_set_empty(@ptr) if created
end

Class Method Details

.from_rect(rect) ⇒ Object



17
18
19
20
21
# File 'lib/skia/rrect.rb', line 17

def self.from_rect(rect)
  rrect = new
  rrect.set_rect(rect)
  rrect
end

.from_rect_radii(rect, radii) ⇒ Object



29
30
31
32
33
# File 'lib/skia/rrect.rb', line 29

def self.from_rect_radii(rect, radii)
  rrect = new
  rrect.set_rect_radii(rect, radii)
  rrect
end

.from_rect_xy(rect, x_radius, y_radius = x_radius) ⇒ Object



23
24
25
26
27
# File 'lib/skia/rrect.rb', line 23

def self.from_rect_xy(rect, x_radius, y_radius = x_radius)
  rrect = new
  rrect.set_rect_xy(rect, x_radius, y_radius)
  rrect
end

Instance Method Details

#cloneObject

Raises:



35
36
37
38
39
40
# File 'lib/skia/rrect.rb', line 35

def clone
  ptr = Native.sk_rrect_new_copy(@ptr)
  raise Error, 'Failed to clone rounded rectangle' if ptr.nil? || ptr.null?

  self.class.new(ptr)
end

#contains_rect?(rect) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/skia/rrect.rb', line 119

def contains_rect?(rect)
  rect_struct = rect.to_struct
  Native.sk_rrect_contains(@ptr, rect_struct)
end

#corner_radius(corner) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/skia/rrect.rb', line 93

def corner_radius(corner)
  unless CORNERS.include?(corner)
    raise ArgumentError, "Invalid corner: #{corner.inspect}. Expected one of: #{CORNERS.join(', ')}"
  end

  point_struct = Native::SKPoint.new
  Native.sk_rrect_get_radii(@ptr, corner, point_struct)
  Point.from_struct(point_struct)
end

#heightObject



111
112
113
# File 'lib/skia/rrect.rb', line 111

def height
  Native.sk_rrect_get_height(@ptr)
end

#inset(dx, dy) ⇒ Object



124
125
126
127
# File 'lib/skia/rrect.rb', line 124

def inset(dx, dy)
  Native.sk_rrect_inset(@ptr, dx.to_f, dy.to_f)
  self
end

#offset(dx, dy) ⇒ Object



134
135
136
137
# File 'lib/skia/rrect.rb', line 134

def offset(dx, dy)
  Native.sk_rrect_offset(@ptr, dx.to_f, dy.to_f)
  self
end

#outset(dx, dy) ⇒ Object



129
130
131
132
# File 'lib/skia/rrect.rb', line 129

def outset(dx, dy)
  Native.sk_rrect_outset(@ptr, dx.to_f, dy.to_f)
  self
end

#rectObject



42
43
44
45
46
# File 'lib/skia/rrect.rb', line 42

def rect
  rect_struct = Native::SKRect.new
  Native.sk_rrect_get_rect(@ptr, rect_struct)
  Rect.from_struct(rect_struct)
end

#set_emptyObject



48
49
50
51
# File 'lib/skia/rrect.rb', line 48

def set_empty
  Native.sk_rrect_set_empty(@ptr)
  self
end

#set_rect(rect) ⇒ Object



53
54
55
56
57
# File 'lib/skia/rrect.rb', line 53

def set_rect(rect)
  rect_struct = rect.to_struct
  Native.sk_rrect_set_rect(@ptr, rect_struct)
  self
end

#set_rect_radii(rect, radii) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/skia/rrect.rb', line 65

def set_rect_radii(rect, radii)
  raise ArgumentError, 'radii must have 4 entries (ul, ur, lr, ll)' unless radii.is_a?(Array) && radii.length == 4

  rect_struct = rect.to_struct
  radii_ptr = FFI::MemoryPointer.new(Native::SKPoint, 4)

  radii.each_with_index do |radius, index|
    point = case radius
            when Point
              radius
            when Array
              raise ArgumentError, 'radius pair must have 2 elements' unless radius.length == 2

              Point.new(radius[0], radius[1])
            else
              raise ArgumentError, 'radius must be Point or [x, y]'
            end

    radius_struct = point.to_struct
    destination = Native::SKPoint.new(radii_ptr + (index * Native::SKPoint.size))
    destination[:x] = radius_struct[:x]
    destination[:y] = radius_struct[:y]
  end

  Native.sk_rrect_set_rect_radii(@ptr, rect_struct, radii_ptr)
  self
end

#set_rect_xy(rect, x_radius, y_radius = x_radius) ⇒ Object



59
60
61
62
63
# File 'lib/skia/rrect.rb', line 59

def set_rect_xy(rect, x_radius, y_radius = x_radius)
  rect_struct = rect.to_struct
  Native.sk_rrect_set_rect_xy(@ptr, rect_struct, x_radius.to_f, y_radius.to_f)
  self
end

#typeObject



103
104
105
# File 'lib/skia/rrect.rb', line 103

def type
  Native.sk_rrect_get_type(@ptr)
end

#valid?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/skia/rrect.rb', line 115

def valid?
  Native.sk_rrect_is_valid(@ptr)
end

#widthObject



107
108
109
# File 'lib/skia/rrect.rb', line 107

def width
  Native.sk_rrect_get_width(@ptr)
end