Class: Emfsvg::ClipRegion

Inherits:
Object
  • Object
show all
Defined in:
lib/emfsvg/clip_region.rb

Overview

Immutable clip region value object. Holds either a rectangular bounds (most common case) or a complex path (for SelectClipPath with arbitrary paths).

Combine modes per MS-WMF 2.1.5:

RGN_AND=1, RGN_OR=2, RGN_XOR=3, RGN_DIFF=4, RGN_COPY=5

Constant Summary collapse

RGN_AND =
1
RGN_OR =
2
RGN_XOR =
3
RGN_DIFF =
4
RGN_COPY =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bounds: nil, path_d: nil) ⇒ ClipRegion

Returns a new instance of ClipRegion.



17
18
19
20
# File 'lib/emfsvg/clip_region.rb', line 17

def initialize(bounds: nil, path_d: nil)
  @bounds = bounds
  @path_d = path_d
end

Instance Attribute Details

#boundsObject (readonly)

Returns the value of attribute bounds.



22
23
24
# File 'lib/emfsvg/clip_region.rb', line 22

def bounds
  @bounds
end

#path_dObject (readonly)

Returns the value of attribute path_d.



22
23
24
# File 'lib/emfsvg/clip_region.rb', line 22

def path_d
  @path_d
end

Instance Method Details

#combine(other, mode) ⇒ Object

Combine with another region per the given mode. For MVP we approximate: only AND and COPY are handled precisely; OR/XOR/DIFF fall back to the new region's bounds.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/emfsvg/clip_region.rb', line 40

def combine(other, mode)
  case mode
  when RGN_AND
    return other if empty?
    return self if other.empty?

    ClipRegion.new(bounds: intersect_rects(bounds, other.bounds))
  else # RGN_COPY, RGN_OR, RGN_XOR, RGN_DIFF — MVP: use new region
    other
  end
end

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/emfsvg/clip_region.rb', line 24

def empty?
  bounds.nil? && path_d.nil?
end

#to_svg_element(builder) ⇒ Object

SVG element body for the clip shape: <rect .../> or <path .../>.



29
30
31
32
33
34
35
# File 'lib/emfsvg/clip_region.rb', line 29

def to_svg_element(builder)
  if path_d
    builder.tag("path", { "d" => path_d }, single: true)
  elsif bounds
    builder.tag("rect", rect_attrs(bounds), single: true)
  end
end