File: image_handler.rb

package info (click to toggle)
ruby-prawn 2.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,380 kB
  • sloc: ruby: 15,820; sh: 43; makefile: 20
file content (44 lines) | stat: -rw-r--r-- 927 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

# ImageHandler provides a way to register image processors with Prawn
#
# Contributed by Evan Sharp in November 2013.
#
# This is free software. Please see the LICENSE and COPYING files for details.

module Prawn
  # @group Extension API

  def self.image_handler
    @image_handler ||= ImageHandler.new
  end

  class ImageHandler
    def initialize
      @handlers = []
    end

    def register(handler)
      @handlers.delete(handler)
      @handlers.push handler
    end

    def register!(handler)
      @handlers.delete(handler)
      @handlers.unshift handler
    end

    def unregister(handler)
      @handlers.reject! { |h| h == handler }
    end

    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