PaperForge.ImageRegistry (PaperForge v0.2.0)

Copy Markdown View Source

Stores images registered inside a PDF document.

Images are indexed by their content hash so the same image can be reused across multiple pages without being embedded more than once.

Resource names are generated sequentially:

Im1
Im2
Im3

Summary

Functions

Returns all registered images sorted by their resource number.

Returns the number of registered images.

Fetches a registered image by hash.

Fetches a registered image and raises when it does not exist.

Returns every registered image hash.

Creates an empty image registry.

Returns the resource name that will be assigned to the next new image.

Adds an image directly to the registry.

Registers an image.

Returns whether an image hash is already registered.

Creates an /XObject resource dictionary for selected image hashes.

Types

t()

@type t() :: %PaperForge.ImageRegistry{
  images: %{optional(binary()) => PaperForge.Image.t()},
  next_resource_id: pos_integer()
}

Functions

all(registry)

@spec all(t()) :: [PaperForge.Image.t()]

Returns all registered images sorted by their resource number.

count(registry)

@spec count(t()) :: non_neg_integer()

Returns the number of registered images.

fetch(registry, hash)

@spec fetch(t(), binary()) :: {:ok, PaperForge.Image.t()} | :error

Fetches a registered image by hash.

Returns {:ok, image} or :error.

fetch!(registry, hash)

@spec fetch!(t(), binary()) :: PaperForge.Image.t()

Fetches a registered image and raises when it does not exist.

hashes(registry)

@spec hashes(t()) :: [binary()]

Returns every registered image hash.

new()

@spec new() :: t()

Creates an empty image registry.

next_resource_name(registry)

@spec next_resource_name(t()) :: binary()

Returns the resource name that will be assigned to the next new image.

Example

PaperForge.ImageRegistry.next_resource_name(registry)
#=> "Im1"

put(registry, image)

@spec put(t(), PaperForge.Image.t()) :: t()

Adds an image directly to the registry.

This is similar to register/2, but always replaces an existing image with the same hash.

register(registry, image)

@spec register(t(), PaperForge.Image.t()) :: {t(), PaperForge.Image.t()}

Registers an image.

When an image with the same hash already exists, the existing image and unchanged registry are returned.

Example

{registry, image} =
  PaperForge.ImageRegistry.register(
    registry,
    image
  )

registered?(registry, hash)

@spec registered?(t(), binary()) :: boolean()

Returns whether an image hash is already registered.

resource_dictionary(registry, hashes)

@spec resource_dictionary(
  t(),
  [binary()]
) :: map()

Creates an /XObject resource dictionary for selected image hashes.

Duplicate hashes are ignored.

Example result

%{
  "Im1" => %PaperForge.Reference{
    object_id: 8,
    generation: 0
  }
}