File: image_handler.rb

package info (click to toggle)
ruby-prawn 2.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,528 kB
  • sloc: ruby: 17,688; sh: 43; makefile: 20
file content (61 lines) | stat: -rw-r--r-- 1,414 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
# frozen_string_literal: true

module Prawn # rubocop: disable Style/Documentation
  # @group Extension API

  # Image handler.
  #
  # @return [ImageHandler]
  def self.image_handler
    @image_handler ||= ImageHandler.new
  end

  # ImageHandler provides a way to register image processors with Prawn.
  class ImageHandler
    # @private
    def initialize
      @handlers = []
    end

    # Register an image handler.
    #
    # @param handler [Object]
    # @return [void]
    def register(handler)
      @handlers.delete(handler)
      @handlers.push(handler)
    end

    # Register an image handler with the highest priority.
    #
    # @param handler [Object]
    # @return [void]
    def register!(handler)
      @handlers.delete(handler)
      @handlers.unshift(handler)
    end

    # Unregister an image handler.
    #
    # @param handler [Object]
    # @return [void]
    def unregister(handler)
      @handlers.reject! { |h| h == handler }
    end

    # Find an image handler for an image.
    #
    # @param image_blob [String]
    # @return [Object]
    # @raise [Prawn::Errors::UnsupportedImageType] If no image handler were
    #   found for the image.
    def find(image_blob)
      handler = @handlers.find { |h| h.can_render?(image_blob) }

      return handler if handler

      raise Prawn::Errors::UnsupportedImageType,
        'image file is an unrecognised format'
    end
  end
end