PaperForge.Coordinates (PaperForge v0.2.0)

Copy Markdown View Source

Helpers for converting between PDF coordinate systems.

PDF uses the bottom-left corner as its native origin. PaperForge can also expose a top-left origin, which is often easier for document layout.

Supported origins:

  • :bottom_left
  • :top_left

Summary

Functions

Converts the Y coordinate of a rectangular element into PDF coordinates.

Converts two Y coordinates used by a line.

Converts a point Y coordinate into PDF coordinates.

Returns whether the provided origin is supported.

Validates and returns an origin.

Types

origin()

@type origin() :: :bottom_left | :top_left

Functions

box_y(page_height, y, height, origin)

@spec box_y(
  number(),
  number(),
  number(),
  origin()
) :: number()

Converts the Y coordinate of a rectangular element into PDF coordinates.

This should be used for elements whose Y coordinate represents the top or bottom edge of a box, such as:

  • rectangles;
  • images;
  • text boxes;
  • other elements with a known height.

When the origin is :top_left, the element height is subtracted so the box appears below the provided Y coordinate.

Examples

PaperForge.Coordinates.box_y(
  842,
  72,
  100,
  :top_left
)
#=> 670

PaperForge.Coordinates.box_y(
  842,
  72,
  100,
  :bottom_left
)
#=> 72

line_y(page_height, y1, y2, origin)

@spec line_y(
  number(),
  number(),
  number(),
  origin()
) :: {number(), number()}

Converts two Y coordinates used by a line.

Example

PaperForge.Coordinates.line_y(
  842,
  72,
  120,
  :top_left
)
#=> {770, 722}

point_y(page_height, y, origin)

@spec point_y(
  number(),
  number(),
  origin()
) :: number()

Converts a point Y coordinate into PDF coordinates.

For :bottom_left, the value is returned unchanged.

For :top_left, the value is measured from the top edge of the page.

Examples

PaperForge.Coordinates.point_y(
  842,
  72,
  :top_left
)
#=> 770

PaperForge.Coordinates.point_y(
  842,
  72,
  :bottom_left
)
#=> 72

valid_origin?(origin)

@spec valid_origin?(term()) :: boolean()

Returns whether the provided origin is supported.

validate_origin!(origin)

@spec validate_origin!(term()) :: origin()

Validates and returns an origin.

Raises ArgumentError for unsupported values.