@reekon-tools/boldr-utils 1.21.0 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ // A LIST group's batch formula, applied to one measurement value — the
2
+ // evaluation every tile that shows a group-formula'd measurement performs.
3
+ //
4
+ // Ported from the two apps' `lib/calculator.ts` (`applyFormulaToMeasurement` +
5
+ // `arrayToExpression`), which are byte-for-byte duplicates of each other, so
6
+ // the server-side PDF export shows the same value the screens do. Kept
7
+ // deliberately narrow: only the frame kinds a GROUP formula can hold. The
8
+ // calculator's inspection-input frames are not group-formula content and are
9
+ // emitted as an empty token here, exactly as the apps' `default:` branch does.
10
+ import { create, all } from 'mathjs';
11
+ import { Units } from '../types/firestore.js';
12
+ const math = create(all);
13
+ const MICROMETERS_PER_IN = 25400;
14
+ const MICROMETERS_PER_FT = 304800;
15
+ const MICROMETERS_PER_MM = 1000;
16
+ const MICROMETERS_PER_CM = 10000;
17
+ const MICROMETERS_PER_M = 1000000;
18
+ /** Frame types of the stack-expression formula the apps persist. */
19
+ export var GroupFormulaFrameType;
20
+ (function (GroupFormulaFrameType) {
21
+ GroupFormulaFrameType["Operator"] = "operator";
22
+ GroupFormulaFrameType["Number"] = "number";
23
+ GroupFormulaFrameType["Paren"] = "paren";
24
+ GroupFormulaFrameType["Fraction"] = "fraction";
25
+ GroupFormulaFrameType["FeetInches"] = "feetInches";
26
+ GroupFormulaFrameType["MeasurementObj"] = "measurement";
27
+ GroupFormulaFrameType["InspectionInput"] = "InspectionInput";
28
+ GroupFormulaFrameType["Func"] = "func";
29
+ GroupFormulaFrameType["Output"] = "output";
30
+ })(GroupFormulaFrameType || (GroupFormulaFrameType = {}));
31
+ const toNumber = (v) => {
32
+ const n = parseInt(v, 10);
33
+ return Number.isNaN(n) ? 0 : n;
34
+ };
35
+ const fractionToDecimal = (f) => {
36
+ const numerator = parseInt(f.numerator, 10);
37
+ const denominator = parseInt(f.denominator, 10);
38
+ let frac = 0;
39
+ if (!(Number.isNaN(numerator) && Number.isNaN(denominator))) {
40
+ const den = Number.isNaN(denominator) ? 0 : denominator;
41
+ frac =
42
+ den === 0
43
+ ? 0
44
+ : math.number(math.fraction(Number.isNaN(numerator) ? 0 : numerator, den));
45
+ }
46
+ return frac + toNumber(f.int);
47
+ };
48
+ const feetInchesToDecimalFeet = (v) => {
49
+ const feet = parseInt(v.feet, 10);
50
+ const inchInt = parseInt(v.int, 10);
51
+ const numerator = parseInt(v.numerator, 10);
52
+ const denominator = parseInt(v.denominator, 10);
53
+ const inchFrac = !Number.isNaN(numerator) && !Number.isNaN(denominator) && denominator > 0
54
+ ? math.number(math.fraction(numerator, denominator))
55
+ : 0;
56
+ const inches = (Number.isNaN(inchInt) ? 0 : inchInt) + inchFrac;
57
+ const sign = v.feet.trim().startsWith('-') ? -1 : 1;
58
+ return (Number.isNaN(feet) ? 0 : feet) + (sign * inches) / 12;
59
+ };
60
+ /** Composite feet-inches units evaluate in decimal feet; everything else as-is. */
61
+ const evaluationUnit = (u) => {
62
+ if (!u)
63
+ return Units.Inches;
64
+ return u === Units.FeetInchesDecimal || u === Units.FeetInchesFractional
65
+ ? Units.Feet
66
+ : u;
67
+ };
68
+ export const micrometersToUnitNumber = (um, unit) => {
69
+ switch (unit) {
70
+ case Units.Millimeters:
71
+ return um / MICROMETERS_PER_MM;
72
+ case Units.Centimeters:
73
+ return um / MICROMETERS_PER_CM;
74
+ case Units.Meters:
75
+ return um / MICROMETERS_PER_M;
76
+ case Units.Feet:
77
+ case Units.FeetInchesDecimal:
78
+ case Units.FeetInchesFractional:
79
+ return um / MICROMETERS_PER_FT;
80
+ case Units.Inches:
81
+ case Units.FractionalInches:
82
+ return um / MICROMETERS_PER_IN;
83
+ default:
84
+ return NaN;
85
+ }
86
+ };
87
+ export const unitNumberToMicrometers = (value, unit) => {
88
+ switch (unit) {
89
+ case Units.Millimeters:
90
+ return value * MICROMETERS_PER_MM;
91
+ case Units.Centimeters:
92
+ return value * MICROMETERS_PER_CM;
93
+ case Units.Meters:
94
+ return value * MICROMETERS_PER_M;
95
+ case Units.Feet:
96
+ case Units.FeetInchesDecimal:
97
+ case Units.FeetInchesFractional:
98
+ return value * MICROMETERS_PER_FT;
99
+ case Units.Inches:
100
+ case Units.FractionalInches:
101
+ return value * MICROMETERS_PER_IN;
102
+ default:
103
+ return NaN;
104
+ }
105
+ };
106
+ const findLastIndex = (arr, pred) => {
107
+ for (let i = arr.length - 1; i >= 0; i--)
108
+ if (pred(arr[i]))
109
+ return i;
110
+ return -1;
111
+ };
112
+ /** The mathjs expression string a stack formula evaluates to, for `valueUm`. */
113
+ export const groupFormulaExpression = (frames, valueUm, unit) => {
114
+ const outputIndex = findLastIndex(frames, (f) => f.type === GroupFormulaFrameType.Output);
115
+ const sliced = outputIndex === -1 ? frames : frames.slice(outputIndex);
116
+ const evalUnit = evaluationUnit(unit);
117
+ return sliced
118
+ .map((item) => {
119
+ switch (item.type) {
120
+ case GroupFormulaFrameType.Fraction:
121
+ return String(fractionToDecimal(item.value));
122
+ case GroupFormulaFrameType.FeetInches:
123
+ return String(feetInchesToDecimalFeet(item.value));
124
+ case GroupFormulaFrameType.Number:
125
+ case GroupFormulaFrameType.Operator:
126
+ case GroupFormulaFrameType.Paren:
127
+ case GroupFormulaFrameType.Output:
128
+ return String(item.value ?? '');
129
+ case GroupFormulaFrameType.MeasurementObj:
130
+ // The measurement frame stands for THE measurement being displayed.
131
+ return String(micrometersToUnitNumber(valueUm, evalUnit));
132
+ case GroupFormulaFrameType.Func: {
133
+ const funcValue = String(item.value ?? '');
134
+ const m = item.measurement;
135
+ if (m && typeof m.value === 'number') {
136
+ const converted = micrometersToUnitNumber(m.value, evalUnit);
137
+ return funcValue.replace(m.value.toString(), converted.toString());
138
+ }
139
+ return funcValue;
140
+ }
141
+ default:
142
+ return '';
143
+ }
144
+ })
145
+ .join(' ');
146
+ };
147
+ /**
148
+ * Apply a group's stored `formula` to a measurement value (micrometers) and
149
+ * return the result in micrometers, or null when the group has no formula or
150
+ * the expression does not evaluate to a finite number (the caller falls back
151
+ * to the raw value, exactly like the tiles do).
152
+ */
153
+ export const applyGroupFormulaUm = (formula, valueUm, unit) => {
154
+ if (!Array.isArray(formula) || formula.length === 0)
155
+ return null;
156
+ const expression = groupFormulaExpression(formula, valueUm, unit);
157
+ let result;
158
+ try {
159
+ result = math.evaluate(expression);
160
+ }
161
+ catch {
162
+ return null;
163
+ }
164
+ if (typeof result !== 'number' || !Number.isFinite(result))
165
+ return null;
166
+ const um = unitNumberToMicrometers(result, evaluationUnit(unit));
167
+ return Number.isFinite(um) ? um : null;
168
+ };
package/dist/server.d.ts CHANGED
@@ -15,3 +15,6 @@ export { desktopBaseUrlForFirebaseProject, desktopFileEditorUrl, type DesktopLin
15
15
  export { numberToLetterIndex, formGroupInputLabel, listGroupMeasurementLabel, } from './utils/indexLabels.js';
16
16
  export { toSelectedValues, formatSelectedValues, toStoredValue, } from './utils/selectValues.js';
17
17
  export * from './calculator/index.js';
18
+ export * from './export/pdf/index.js';
19
+ export { applyGroupFormulaUm, groupFormulaExpression, micrometersToUnitNumber, unitNumberToMicrometers, type GroupFormulaFrame, } from './formulas/groupFormula.js';
20
+ export { MEASUREMENT_LINE_COLOR, MEASUREMENT_LINE_WIDTH, MEASUREMENT_RECT_WIDTH, } from './annotation/canvas/measurementLineStyle.js';
package/dist/server.js CHANGED
@@ -20,3 +20,8 @@ export { desktopBaseUrlForFirebaseProject, desktopFileEditorUrl, } from './expor
20
20
  export { numberToLetterIndex, formGroupInputLabel, listGroupMeasurementLabel, } from './utils/indexLabels.js';
21
21
  export { toSelectedValues, formatSelectedValues, toStoredValue, } from './utils/selectValues.js';
22
22
  export * from './calculator/index.js';
23
+ // Canvas → annotated PDF export (Cloud Function renderer). Pure pdf-lib; no
24
+ // React or Skia anywhere in its import graph.
25
+ export * from './export/pdf/index.js';
26
+ export { applyGroupFormulaUm, groupFormulaExpression, micrometersToUnitNumber, unitNumberToMicrometers, } from './formulas/groupFormula.js';
27
+ export { MEASUREMENT_LINE_COLOR, MEASUREMENT_LINE_WIDTH, MEASUREMENT_RECT_WIDTH, } from './annotation/canvas/measurementLineStyle.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reekon-tools/boldr-utils",
3
- "version": "1.21.0",
3
+ "version": "1.22.0",
4
4
  "description": "Shared utilities for formulas and measurement conversion used in Reekon apps",
5
5
  "author": "REEKON Tools",
6
6
  "license": "MIT",
@@ -47,7 +47,9 @@
47
47
  "keywords": [],
48
48
  "repository": "https://gitlab.com/reekon-tools/software/rock-pro/boldr-utils#",
49
49
  "dependencies": {
50
+ "@pdf-lib/fontkit": "1.1.1",
50
51
  "mathjs": "^11.11.0",
52
+ "pdf-lib": "1.17.1",
51
53
  "zod": "^4.4.3"
52
54
  },
53
55
  "peerDependencies": {
@@ -70,6 +72,7 @@
70
72
  },
71
73
  "devDependencies": {
72
74
  "@arethetypeswrong/cli": "^0.18.1",
75
+ "@embedpdf/pdfium": "2.15.0",
73
76
  "@shopify/react-native-skia": "^2.6.2",
74
77
  "@types/node": "^22.15.18",
75
78
  "@types/react": "^19.0.8",