Skip to main content

Module function

Module function 

Source
Expand description

§function — PDF function objects (ISO 32000-1 §7.10)

A PDF function is a static, self-contained numerical transformation: m input numbers in, n output numbers out, no side effects, no state. §7.10’s opening paragraph is emphatic that this is not a programming facility — “PDF is not a programming language, and a PDF file is not a program” — and that framing is load-bearing for this module. Everything here is a pure function of its inputs; nothing reads the document, the graphics state, or the clock.

§Why this module exists at all

Nothing in pdfcer could evaluate a function before this module, and that single gap blocked four unrelated features at once. A function is the representation the standard reaches for whenever a value has to be computed rather than looked up:

ConsumerClauseWhat the function does there
Separation colour space§8.6.6.4tint → alternate-space components
DeviceN colour space§8.6.6.5k tints → alternate-space components
Shading dictionaries§8.7.4parametric coordinate → colour
Soft-mask transfer function (/TR)§11.6.5.2mask value → mask value
ExtGState /TR, /TR2, /BG, /UCR§8.4.5tone/black-generation curves
Halftone spot functions, /DotGain§10.5.3, §8.6.6.5screening

One evaluator serves all of them, which is the reason this lives in pdfcer-core rather than beside any one consumer. A second, specialised copy would drift, and a tint transform that disagrees with a shading’s transform over the same function object is a defect nobody would think to look for.

§The four types, and the missing one

/FunctionTypeNamePDFCarried as/Range
0Sampled1.2streamRequired
2Exponential interpolation1.3dictionaryOptional
3Stitching1.3dictionaryOptional
4PostScript calculator1.3streamRequired

There is no type 1. The gap is real, not a transcription slip, and FunctionError::UnknownFunctionType reports 1 exactly like it reports 7 — this module never invents a meaning for it.

/Range is required for types 0 and 4 because n cannot be recovered any other way (§7.10.1: “The number of output values can usually be inferred from other attributes of the function; if not (as is always the case for type 0 and type 4 functions), the Range entry is required”). For types 2 and 3 it is optional, and its absence means no output clipping at all — see the clamping section below, because getting that backwards is the single most damaging mistake available in this file.

§Clamping is specified, not optional

Table 38, on /Domain: “Input values outside the declared domain shall be clipped to the nearest boundary value.”

Table 38, on /Range: “Output values outside the declared range shall be clipped to the nearest boundary value. If this entry is absent, no clipping shall be done.”

Both are shalls, and both are implemented once, centrally, in PdfFunction::eval_into — not per type — so no type can be written that forgets one. The asymmetry in the second rule is why PdfFunction::range returns Option rather than a defaulted [0, 1] per output: a shading function whose outputs were silently squeezed into the unit interval produces colour that is wrong in a way that still looks like colour.

A tint of 1.4 is therefore evaluated as 1.0, and a CMYK component of -0.2 coming out of a transform with a /Range of [0 1 0 1 0 1 0 1] is clamped to 0.0. Neither is a tolerance; both are the standard.

§Refusal posture: a wrong colour is worse than no colour

§7.10.5.2 is the only place in §7.10 that addresses failure, it covers type 4 only, and it explicitly declines to say what a reader should do: “This specification does not define a representation for the errors; those details shall be provided by the conforming reader.” For types 0, 2 and 3 the standard says nothing at all about a truncated sample stream, a /Bounds array out of order, or a negative /N with x = 0.

So the failure behaviour here is pdfcer policy, chosen deliberately, and recorded as such (the spec RAG files this as negative result F-N1 in iso32000__s__7.10.md): every malformed function is refused by name. FunctionError never degrades to a plausible substitute — no all-zero output vector, no identity transform, no black. The reason is specific to colour: an empty output vector read as “all components zero” is white in DeviceCMYK and black in DeviceGray, so any silent substitution is guaranteed to be catastrophically wrong for half of its callers while looking entirely reasonable. A named error lets the caller disclose the failure (project rule 4, fuzzy-never-sneaky); a fabricated colour cannot be distinguished from a real one downstream.

§Resource bounds

Functions are attacker-controlled input (ARCHITECTURE.md §10). Every loop in this module is bounded:

BoundValueSource
PS_STACK_LIMIT100spec — §7.10.5.1 shall-minimum
MAX_PS_STEPS1,000,000pdfcer policy — no spec equivalent
MAX_PS_NESTING32pdfcer policy — no spec equivalent
MAX_SAMPLED_INPUTS8pdfcer policy, spec-sanctioned (§7.10.2)
MAX_FUNCTION_DEPTH8pdfcer policy — no spec equivalent

The distinction between the first row and the rest is not pedantry. The 100-entry stack is a conformance fact: §7.10.5.1 says an implementation “shall provide a stack with room for at least 100 entries”, that “no implementation shall be required to provide a larger stack”, and that it “shall be an error to overflow the stack”. A fixed 100-entry stack that errors on overflow is therefore exactly conformant, and growing it dynamically would not be robustness — it would be inventing a dialect that only pdfcer can run.

The step and nesting caps have no such backing. ISO 32000-1 imposes no limit on how many operators a type 4 program may execute, so a program that never terminates is legal and a conforming reader must still not hang. MAX_PS_STEPS is pdfcer’s answer to that, and it is a policy number that may be tuned; it is not a claim about the standard.

MAX_SAMPLED_INPUTS sits between the two. §7.10.2 says there is “no dimensionality limit of a sampled function except for possible implementation limits” — an explicit licence to impose one. Eight is chosen because multilinear interpolation visits 2^m corners per evaluation (256 at m = 8) and a DeviceN image runs the transform once per pixel; the spec’s own worked example of a hard case is a six-component hexachrome.

§Spec sources

  • iso32000__s__7.10.md — §7.10 in full: Tables 38–42, the type 0 evaluation algorithm, the type 3 partition rule, §7.10.5.2’s error list, and the recorded ambiguities F-A1 (cubic spline unspecified), F-A2 (multilinear named only in an informative NOTE) and F-N1/F-N2 (no fallback for an unevaluatable function or an arity mismatch).
  • iso32000__annex__b.md — ISO 32000-1 Annex B, (normative), “Operators in Type 4 Functions”: the stack effect, arity and operand/result typing of all 42 Table 42 entries. §7.10.5 never cross-references it, so it is easy to conclude from the clause alone that the operator semantics are unsourceable inside ISO 32000-1. They are not.
  • PLRM3 §8.2 (_sources/Adobe_PLRM3_1999.pdf) — what Annex B leaves out: rounding directions, sign conventions, integer preservation, atan’s [0, 360) normalisation, the zero-fill of a right shift. §7.10.5.1 points at PLRM3’s “Appendix B” for this, which is a misdirected cross-reference (erratum F-E3) — that appendix is Implementation Limits; the operators are Chapter 8. PLRM3 is a Bibliography reference and therefore informative, so it is cited alongside Annex B, never instead of it.
  • iso32000__s__7.3.8.md — stream extent, for the type 0 “long enough” rule.
  • iso32000__annex__c.md — implementation limits, which type 4 intermediates are explicitly exempt from.

Structs§

PdfFunction
A loaded, evaluatable PDF function (§7.10).

Enums§

FunctionError
Everything that can stop a function from loading or evaluating.
FunctionType
Which of §7.10’s four types a PdfFunction is.

Constants§

MAX_FUNCTION_DEPTH
Maximum nesting depth of type 3 stitching sub-functions — pdfcer policy.
MAX_PS_NESTING
Maximum { } nesting depth inside a type 4 program — pdfcer policy.
MAX_PS_STEPS
Maximum operators a single type 4 evaluation may execute — pdfcer policy.
MAX_SAMPLED_INPUTS
Maximum input dimensionality of a type 0 sampled function — pdfcer policy, explicitly permitted by the spec.
PS_INTEGER_BITS
Integer width for type 4 arithmetic — a resolved spec ambiguity, not a sourced value (recorded as F-A3 in iso32000__s__7.10.md).
PS_STACK_LIMIT
Operand-stack capacity for a type 4 program — a spec value, not policy.
VALID_BITS_PER_SAMPLE
Sample widths /BitsPerSample may take (§7.10.2 Table 39).