Module: HexaPDF::Type::Annotations::LineEndingStyling

Included in:
Line, PolygonPolyline
Defined in:
lib/hexapdf/type/annotations/line_ending_styling.rb

Overview

This module provides a convenience method for getting and setting the line ending style for line and polyline annotations.

See: PDF2.0 s12.5.6.7

Defined Under Namespace

Classes: LineEndingStyle

Constant Summary collapse

LINE_ENDING_STYLE_MAP =

Maps HexaPDF names to PDF names.

{ # :nodoc:
  Square: :Square, square: :Square,
  Circle: :Circle, circle: :Circle,
  Diamond: :Diamond, diamond: :Diamond,
  OpenArrow: :OpenArrow, open_arrow: :OpenArrow,
  ClosedArrow: :ClosedArrow, closed_arrow: :ClosedArrow,
  None: :None, none: :None,
  Butt: :Butt, butt: :Butt,
  ROpenArrow: :ROpenArrow, ropen_arrow: :ROpenArrow,
  RClosedArrow: :RClosedArrow, rclosed_arrow: :RClosedArrow,
  Slash: :Slash, slash: :Slash,
}.freeze
LINE_ENDING_STYLE_REVERSE_MAP =

:nodoc:

LINE_ENDING_STYLE_MAP.invert

Instance Method Summary collapse

Instance Method Details

#line_ending_style(start_style: :UNSET, end_style: :UNSET) ⇒ Object

:call-seq:

annot.line_ending_style                                         => style
annot.line_ending_style(start_style: :none, end_style: :none)   => line

Returns a LineEndingStyle instance holding the current line ending styles when no argument is given. Otherwise sets the line ending style of the annotation and returns self.

When returning the styles, unknown line ending styles are mapped to :none.

When setting the line ending style, arguments that are not provided will use the currently defined value or fall back to the default of :none.

Possible line ending styles (the first one is the HexaPDF name, the second the PDF name):

:square or :Square:: A square filled with the annotation's interior colour, if any.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :square).
    regenerate_appearance

:circle or :Circle:: A circle filled with the annotation’s interior colour, if any.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :circle).
    regenerate_appearance

:diamond or :Diamond:: A diamond shape filled with the annotation’s interior colour, if any.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :diamond).
    regenerate_appearance

:open_arrow or :OpenArrow:: Two short lines meeting in an acute angle to form an open arrowhead.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :open_arrow).
    regenerate_appearance

:closed_arrow or :ClosedArrow:: Two short lines meeting in an acute angle as in the :open_arrow style and connected by a third line to form a triangular closed arrowhead filled with the annotation’s interior colour, if any.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :closed_arrow).
    regenerate_appearance

:none or :None:: No line ending.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :none).
    regenerate_appearance

:butt or :Butt:: A short line at the endpoint perpendicular to the line itself.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :butt).
    regenerate_appearance

:ropen_arrow or :ROpenArrow:: Two short lines in the reverse direction from :open_arrow.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :ropen_arrow).
    regenerate_appearance

:rclosed_arrow or :RClosedArrow:: A triangular closed arrowhead in the reverse direction from :closed_arrow.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :rclosed_arrow).
    regenerate_appearance

:slash or :Slash:: A short line at the endpoint approximately 30 degrees clockwise from perpendicular to the line itself.

  #>pdf-small-hide
  doc.annotations.
    create_line(doc.pages[0], start_point: [20, 20], end_point: [80, 60]).
    interior_color("hp-orange").
    line_ending_style(end_style: :slash).
    regenerate_appearance


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/hexapdf/type/annotations/line_ending_styling.rb', line 185

def line_ending_style(start_style: :UNSET, end_style: :UNSET)
  if start_style == :UNSET && end_style == :UNSET
    le = self[:LE]
    LineEndingStyle.new(LINE_ENDING_STYLE_REVERSE_MAP.fetch(le[0], :none),
                        LINE_ENDING_STYLE_REVERSE_MAP.fetch(le[1], :none))
  else
    start_style = self[:LE][0] if start_style == :UNSET
    end_style = self[:LE][1] if end_style == :UNSET
    start_style = LINE_ENDING_STYLE_MAP.fetch(start_style) do
      raise ArgumentError, "Invalid line ending style: #{start_style.inspect}"
    end
    end_style = LINE_ENDING_STYLE_MAP.fetch(end_style) do
      raise ArgumentError, "Invalid line ending style: #{end_style.inspect}"
    end
    self[:LE] = [start_style, end_style]
    self
  end
end