Class: PDF::Writer::TagAlink

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/writer.rb

Overview

A callback to support the formation of clickable links to external locations.

Constant Summary collapse

DEFAULT_STYLE =

The default anchored link style.

{
  :color      => Color::RGB::Blue,
  :text_color => Color::RGB::Blue,
  :draw_line  => true,
  :line_style => { :dash => PDF::Writer::StrokeStyle::SOLID_LINE },
  :factor     => 0.05
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.styleObject

Sets the style for <c:alink> callback underlines that follow. This is expected to be a hash with the following keys:

:color

The colour to be applied to the link underline. Default is Color::RGB::Blue.

:text_color

The colour to be applied to the link text. Default is Color::RGB::Blue.

:factor

The size of the line, as a multiple of the text height. Default is 0.05.

:draw_line

Whether to draw the underline as part of the link or not. Default is true.

:line_style

The style modification hash supplied to PDF::Writer::StrokeStyle.new. The default is a solid line with normal cap, join, and miter limit values.

Set this to nil to get the default style.



2527
2528
2529
# File 'lib/pdf/writer.rb', line 2527

def style
  @style
end

Class Method Details

.[](pdf, info) ⇒ Object



2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
# File 'lib/pdf/writer.rb', line 2529

def [](pdf, info)
  @style ||= DEFAULT_STYLE.dup

  case info[:status]
  when :start, :start_line
      # The beginning of the link. This should contain the URI for the
      # link as the :params entry, and will also contain the value of
      # :cbid.
    @links ||= {}

    @links[info[:cbid]] = {
      :x         => info[:x],
      :y         => info[:y],
      :angle     => info[:angle],
      :descender => info[:descender],
      :height    => info[:height],
      :uri       => info[:params]["uri"]
    }

    pdf.save_state
    pdf.fill_color @style[:text_color] if @style[:text_color]
    if @style[:draw_line]
      pdf.stroke_color  @style[:color] if @style[:color]
      sz = info[:height] * @style[:factor]
      pdf.stroke_style! StrokeStyle.new(sz, @style[:line_style])
    end
  when :end, :end_line
      # The end of the link. Assume that it is the most recent opening
      # which has closed.
    start = @links[info[:cbid]]
      # Add underlining.
    theta = PDF::Math.deg2rad(start[:angle] - 90.0)
    if @style[:draw_line]
      drop  = start[:height] * @style[:factor] * 1.5
      drop_x = Math.cos(theta) * drop
      drop_y = -Math.sin(theta) * drop
      pdf.move_to(start[:x] - drop_x, start[:y] - drop_y)
      pdf.line_to(info[:x] - drop_x, info[:y] - drop_y).stroke
    end
    pdf.add_link(start[:uri], start[:x], start[:y] +
                 start[:descender], info[:x], start[:y] +
                 start[:descender] + start[:height])
    pdf.restore_state
  end
end