@pdfking/math

Runtime

spatial

ToMargins

const ToMargins = <T>(value: T | Margins<T>): Margins<T>

Margins as given, or the same margin on every edge.

AddMargins

const AddMargins = <T extends Measure>(
  { x, y, width, height }: SpatialRectangle<T>,
  margins: T | Margins<T> = 0 as T,
  sign: 1 | -1 = 1
): SpatialRectangle<T>

Expand a rect by edge margins; sign=-1 shrinks it instead — see RemoveMargins.

Mirrors Python pyfuncs.objects.measure.functions.add_margins (functions.py:52-57) — but y grows up here, so the edge that moves the origin is bottom, where the y-down original moved it by top.

BoundingBox

const BoundingBox = <T extends number>(...points: Point<T>[]): SpatialRectangle<T>

The smallest box holding every one of points: with two, the box with them at opposite corners, whichever way round.

Center

const Center = <T extends number>({
  x,
  y,
  width,
  height,
}: SpatialRectangle<T>): Point<T>

The middle of a box.

ClampMargins

const ClampMargins = <T extends number>(
  margins: Margins<T>,
  floor: T | Margins<T> = 0 as T,
  ceiling: T | Margins<T> = POS_INFINITY as T
): Margins<T>

Margins with every edge raised to at least its floor edge and cut down to at most its ceiling one — by default nothing below and no cap above: an inner box poking out of the outer one leaves no room on that side.

FlipMargins

const FlipMargins = <T>(
  margins: Margins<T>,
  axis: keyof Point<unknown> | (keyof Point<unknown>)[]
): Margins<T>

Margins turned over on an axis: on x left and right change places, on y top and bottom. Several axes are turned over one after the other, so ["x", "y"] is the full symmetry.

GetXY

const GetXY = <T extends number>({ x, y, width, height }: SpatialRectangle<T>)

A box as its four edges. y1 is the bottom and y2 the top: y grows up, like the plot API and the PDF it becomes.

MarginsBetween

const MarginsBetween = <T extends Measure>(
  outer: SpatialRectangle<T>,
  inner: SpatialRectangle<T>
): Margins<T>

The gap between a rect and one nested inside it, per edge — the inverse of AddMargins, so AddMargins(inner, MarginsBetween(outer, inner)) is outer again. Edges where inner pokes outside outer come back negative; clamp at the call site if that isn't meaningful there.

top is the higher y: y grows up.

Outline

const Outline = <T extends number>(boxes: SpatialRectangle<T>[]): SpatialRectangle<T>

The smallest box holding every one of boxes: the one round their corners.

RemoveMargins

const RemoveMargins = <T extends Measure>(
  rect: SpatialRectangle<T>,
  margins: T | Margins<T> = 0 as T
): SpatialRectangle<T>

Pull each edge of a rect in by its margin — AddMargins reversed.

RotateMarginsLeft

const RotateMarginsLeft = <T>(p: Margins<T>): Margins<T>

Rotate margins 90° counter-clockwise: each edge takes the value of the next one clockwise, so the amount that stood on the right ends up on top.

Mirrors Python MarginMeasureSet.rotate_fields() (no reverse).

RotateMarginsRight

const RotateMarginsRight = <T>(p: Margins<T>): Margins<T>

Rotate margins 90° clockwise — the inverse of RotateMarginsLeft.

Mirrors Python MarginMeasureSet.rotate_fields(reverse=True).

SumMargins

const SumMargins = <T extends Measure>(a: T | Margins<T>, b: T | Margins<T>): Margins<T>

Add two sets of margins edge by edge — what a box reaches when it carries both, such as a page's own padding plus the share of the sheet around it.

Not to be confused with AddMargins, which grows a rect by one set.

ToSpatial

const ToSpatial = (
  { width, height }: Rectangle<Distance>,
  { x, y }: Point<Distance> = ZERO_POINT
): SpatialRectangle<Distance>

colors

CmykToRgb

const CmykToRgb = ({ alpha, cyan, black, magenta, yellow }: CMYKColor): RGBColor

Converts CMYK color to RGB color Assumes CMYK values are in range 0-1

CreateColorConvert

const CreateColorConvert =
  <T>(rgb: (color: RGBColor) => T, cmyk: (color: CMYKColor) => T): ((color: Color) => T)

Creates a color conversion function that converts any Color to type T Uses type-safe discrimination based on color properties

HslToRgb

const HslToRgb = ({ alpha, hue, lightness, saturation }: HSLColor): RGBColor

Converts HSL color to RGB color Assumes HSL values are in range 0-1

RgbToCmyk

const RgbToCmyk = ({ alpha, blue, green, red }: RGBColor): CMYKColor

Converts RGB color to CMYK color Assumes RGB values are in range 0-1

ToCmyk

const ToCmyk = CreateColorConvert(RgbToCmyk, c => c)

Converts a Color to a CMYKColor If the input is already CMYK, it will be returned as is If the input is RGB, it will be converted to CMYK

ToRgb

const ToRgb = CreateColorConvert(c => c, CmykToRgb)

Converts a Color to an RGBColor If the input is already RGB, it will be returned as is If the input is CMYK, it will be converted to RGB

math

Abs

const Abs: <T extends number>(x: T) => T

Ceil

const Ceil: <T extends number>(x: T) => T

Floor

const Floor: <T extends number>(x: T) => T

Fround

const Fround: <T extends number>(x: T) => T

Round

const Round: <T extends number>(x: T) => T

Trunc

const Trunc: <T extends number>(x: T) => T

Max

const Max: <T extends number>(...values: T[]) => T

Min

const Min: <T extends number>(...values: T[]) => T

Cos

const Cos: (x: Radian) => number

Sin

const Sin: (x: Radian) => number

Tan

const Tan: (x: Radian) => number

Acos

const Acos: (x: number) => Radian

Asin

const Asin: (x: number) => Radian

Atan

const Atan: (x: number) => Radian

Atan2

const Atan2: (y: number, x: number) => Radian

Exp

const Exp: (x: number) => number

Hypot

const Hypot: (...values: number[]) => number

Log

const Log: (x: number) => number

Log2

const Log2: (x: number) => number

Pow

const Pow: (x: number, y: number) => number

Random

const Random: () => number

Sign

const Sign: (x: number) => number

Sqrt

const Sqrt: (x: number) => number

PI

const PI: Radian

E

const E: number

POS_INFINITY

const POS_INFINITY: number

NEG_INFINITY

const NEG_INFINITY: number

MAX_INTEGER

const MAX_INTEGER: number

MIN_INTEGER

const MIN_INTEGER: number

MAX_REAL

const MAX_REAL: number

MIN_REAL

const MIN_REAL: number

ToNumber

const ToNumber: (value: string | number | bigint | boolean) => number

InfinityQ

const InfinityQ = (x: number): boolean

IntegerQ

const IntegerQ: (x: number) => boolean

NanQ

const NanQ: (x: number) => boolean

Total

const Total = <T extends number>(...values: T[]): T

Mean

const Mean = <T extends number>(...values: T[]): T

Mult

const Mult = <T extends number>(...values: T[]): T

Subtract

const Subtract = <T extends number>(a: T, b: T): T

Divide

const Divide = <T extends number>(a: T, b: T): T

Scale

const Scale = <T extends number>(value: T, factor: number): T

value times a plain factor, of the same kind: a length a share of another.

Rotate

const Rotate = <T extends Measure>(cx: T, cy: T, x: T, y: T, angle: Radian): Point<T>

Rotates a point around a center point by the specified angle

RoundTo

const RoundTo = <T extends number>(val: T, decimals = 0): T

ToRadians

const ToRadians = (degrees: Degree): Radian

Converts degrees to radians