Resources Numerical accuracy Calculation policy

Vector PDF output

Writes drawings to a PDF file: the page size, the millimeter scale, lines, circles and text positions.

How the precision is kept

Coordinates arrive in millimeters as 64-bit numbers and are written as decimal text with at most 4 decimals. The page content is scaled from millimeters to points by one matrix whose factor 72 ÷ 25.4 is also written with 4 decimals, as 2.8346. Circles are drawn as four cubic Bézier curves.

Open item. Write the millimeter-to-point scale with full precision instead of the 4-decimal value 2.8346.

Calculations under this policy

6 calculations in 1 source file. The code is read from the released app when this page loads. It is the calculation people run, not a copy kept for this page.

pdf.js BimCraft 0.8.1 64-bit 6 calculations Reviewed

Tests None found by the review

Number format in the file

64-bit

Turns a number into the text written in the PDF.

toFixed(4), trailing zeros removed; |v| < 5e-5 → 0

Every coordinate, line width and matrix entry is rounded to 4 decimals when it is written. A value that is not finite is written as 0.

  • pdf.js
  • lines 19 to 23
  • BimCraft 0.8.1
export function num(v) {
  if (!Number.isFinite(v)) return '0';
  const s = (Math.abs(v) < 5e-5 ? 0 : v).toFixed(4);
  return s.includes('.') ? s.replace(/0+$/, '').replace(/\.$/, '') : s;
}

Millimeters to points

Exact

The factor between millimeters and PDF points.

K = 72 ÷ 25.4

Both numbers are exact by definition and the quotient is the nearest 64-bit number to 2.834645669…

  • pdf.js
  • line 17
  • BimCraft 0.8.1
const K = 72 / 25.4;

Page scale matrix

64-bit

The matrix that lets the rest of the page be written in millimeters.

[K 0 0 K 0 0] with K written as num(K) = 2.8346

K passes through the 4-decimal number format, so the file holds 2.8346 instead of 2.834645669. That is a relative error of 1.6e-5 applied to every length on the page.

  • pdf.js
  • line 46
  • BimCraft 0.8.1
out.push(num(K) + ' 0 0 ' + num(K) + ' 0 0 cm', '1 J 1 j 0 0 0 RG 0 0 0 rg');

Page size in points

64-bit

The page box from the sheet size in millimeters.

points = num(mm × K)

The sheet size is multiplied by the unrounded factor and then written with 4 decimals, so the page box does not carry the scale matrix's error.

  • pdf.js
  • line 109
  • BimCraft 0.8.1
const pt = (mm) => num(mm * K);

Circle as four curves

64-bit

A circle written as four cubic Bézier curves.

control distance k = 0.5522847498 × r

The constant is 4 ÷ 3 × (√2 − 1) written to 10 digits. The curves pass through the four quarter points exactly and bulge slightly outside the circle between them.

  • pdf.js
  • lines 62 to 67
  • BimCraft 0.8.1
const [cx, cy] = it.c, r = it.r, k = 0.5522847498 * r;
out.push(num(cx + r) + ' ' + num(cy) + ' m',
  [cx + r, cy + k, cx + k, cy + r, cx, cy + r].map(num).join(' ') + ' c',
  [cx - k, cy + r, cx - r, cy + k, cx - r, cy].map(num).join(' ') + ' c',
  [cx - r, cy - k, cx - k, cy - r, cx, cy - r].map(num).join(' ') + ' c',
  [cx + k, cy - r, cx + r, cy - k, cx + r, cy].map(num).join(' ') + ' c', it.fill ? 'h f' : 'h S');

Text position and matrix

64-bit

Where a text starts and how it is turned and sized.

x = x₀ + dx·cos θ, y = y₀ + dx·sin θ; matrix = [h·cos θ, h·sin θ, −h·sin θ, h·cos θ, x, y]

Centered and right-aligned text is moved back along its own direction by half or all of its width. The width comes from the Helvetica advance table in font.js, with bold taken as 5 % wider rather than measured.

  • pdf.js
  • lines 71 to 74
  • BimCraft 0.8.1
const h = it.h, wdt = textWidth(it.s, h, it.bold);
const dx = it.align === 'center' ? -wdt / 2 : it.align === 'right' ? -wdt : 0;
const c = Math.cos(it.rot || 0), s = Math.sin(it.rot || 0);
const x = it.x + dx * c, y = it.y + dx * s;

In detail

Method
PDF 1.4 content stream: a scale matrix, then path and text operators with numbers printed by toFixed(4) with trailing zeros removed.
Number types
64-bit numbers formatted to 4 decimals; values smaller than 5e-5 in size are written as 0.
Tolerances
No comparisons. Number format step 0.0001 (millimeters inside the page, points for the page box).
Limits
The scale written is 2.8346, while 72 ÷ 25.4 is 2.83464567; every length on the page is therefore about 0.0016 % short (0.016 mm per meter of paper). The page box is computed with the unrounded factor. A four-curve circle differs from a true circle by up to about 0.03 % of its radius.

Approach: 64-bit, stated limit

The arithmetic is 64-bit, but the method or the data sets the accuracy: a solver tolerance, a mesh, a step size, a datum shift, a table. The limit is stated where the result is shown.

Every policy that takes this approach