File: annotations.rb

package info (click to toggle)
ruby-prawn 1.0.0~rc1%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: ruby: 17,499; sh: 44; makefile: 17
file content (61 lines) | stat: -rw-r--r-- 2,044 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# encoding: utf-8

# annotations.rb : Implements low-level annotation support for PDF
#
# Copyright November 2008, Jamis Buck. All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
#
module Prawn
  module Core 
    
    # Provides very low-level support for annotations. 
    #
    module Annotations #:nodoc:
      
      # Adds a new annotation (section 8.4 in PDF spec) to the current page.
      # +options+ must be a Hash describing the annotation.
      #
      def annotate(options)
        state.page.dictionary.data[:Annots] ||= []
        options = sanitize_annotation_hash(options)
        state.page.dictionary.data[:Annots] << ref!(options)
        return options
      end

      # A convenience method for creating Text annotations. +rect+ must be an array
      # of four numbers, describing the bounds of the annotation. +contents+ should
      # be a string, to be shown when the annotation is activated.
      #
      def text_annotation(rect, contents, options={})
        options = options.merge(:Subtype => :Text, :Rect => rect, :Contents => contents)
        annotate(options)
      end

      # A convenience method for creating Link annotations. +rect+ must be an array
      # of four numbers, describing the bounds of the annotation. The +options+ hash
      # should include either :Dest (describing the target destination, usually as a
      # string that has been recorded in the document's Dests tree), or :A (describing
      # an action to perform on clicking the link), or :PA (for describing a URL to
      # link to).
      #
      def link_annotation(rect, options={})
        options = options.merge(:Subtype => :Link, :Rect => rect)
        annotate(options)
      end

      private

      def sanitize_annotation_hash(options)
        options = options.merge(:Type => :Annot)

        if options[:Dest].is_a?(String)
          options[:Dest] = Prawn::Core::LiteralString.new(options[:Dest])
        end

        options
      end
      
    end
  end
end