class PDF::Writer::TagAlink

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

Constants

DEFAULT_STYLE

The default anchored link style.

Attributes

style[RW]

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.

Public Class Methods

[](pdf, info) click to toggle source
     # File lib/pdf/writer.rb
2509 def [](pdf, info)
2510   @style ||= DEFAULT_STYLE.dup
2511 
2512   case info[:status]
2513   when :start, :start_line
2514       # The beginning of the link. This should contain the URI for the
2515       # link as the :params entry, and will also contain the value of
2516       # :cbid.
2517     @links ||= {}
2518 
2519     @links[info[:cbid]] = {
2520       :x         => info[:x],
2521       :y         => info[:y],
2522       :angle     => info[:angle],
2523       :descender => info[:descender],
2524       :height    => info[:height],
2525       :uri       => info[:params]["uri"]
2526     }
2527 
2528     pdf.save_state
2529     pdf.fill_color @style[:text_color] if @style[:text_color]
2530     if @style[:draw_line]
2531       pdf.stroke_color  @style[:color] if @style[:color]
2532       sz = info[:height] * @style[:factor]
2533       pdf.stroke_style! StrokeStyle.new(sz, @style[:line_style])
2534     end
2535   when :end, :end_line
2536       # The end of the link. Assume that it is the most recent opening
2537       # which has closed.
2538     start = @links[info[:cbid]]
2539       # Add underlining.
2540     theta = PDF::Math.deg2rad(start[:angle] - 90.0)
2541     if @style[:draw_line]
2542       drop  = start[:height] * @style[:factor] * 1.5
2543       drop_x = Math.cos(theta) * drop
2544       drop_y = -Math.sin(theta) * drop
2545       pdf.move_to(start[:x] - drop_x, start[:y] - drop_y)
2546       pdf.line_to(info[:x] - drop_x, info[:y] - drop_y).stroke
2547     end
2548     pdf.add_link(start[:uri], start[:x], start[:y] +
2549                  start[:descender], info[:x], start[:y] +
2550                  start[:descender] + start[:height])
2551     pdf.restore_state
2552   end
2553 end