ducjs
Version:
The duc 2D CAD file format is a cornerstone of our advanced design system, conceived to cater to professionals seeking precision and efficiency in their design work.
50 lines (49 loc) • 1.68 kB
JavaScript
import { MAX_ZOOM, MIN_ZOOM } from "./constants";
import { clamp } from "./math";
export const getNormalizedZoom = (zoom) => {
return clamp(zoom, MIN_ZOOM, MAX_ZOOM);
};
export const getNormalizedGridSize = (gridStep) => {
return clamp(Math.round(gridStep), 1, 100);
};
export const getNormalizedGridStep = (gridStep) => {
return clamp(Math.round(gridStep), 1, 100);
};
export const normalizeFixedPoint = (fixedPoint) => {
// Do not allow a precise 0.5 for fixed point ratio
// to avoid jumping arrow heading due to floating point imprecision
if (fixedPoint && (fixedPoint.x === 0.5 || fixedPoint.y === 0.5)) {
return {
x: fixedPoint.x === 0.5 ? 0.5001 : fixedPoint.x,
y: fixedPoint.y === 0.5 ? 0.5001 : fixedPoint.y,
};
}
return fixedPoint;
};
export const getNormalizedDimensions = (element) => {
const ret = {
width: element.width.scoped,
height: element.height.scoped,
x: element.x.scoped,
y: element.y.scoped,
};
if (element.width.scoped < 0) {
const nextWidth = Math.abs(element.width.scoped);
ret.width = nextWidth;
ret.x = element.x.scoped - nextWidth;
}
if (element.height.scoped < 0) {
const nextHeight = Math.abs(element.height.scoped);
ret.height = nextHeight;
ret.y = element.y.scoped - nextHeight;
}
return ret;
};
export const normalizeEOL = (str) => {
return str.replace(/\r?\n|\r/g, "\n");
};
export const normalizeText = (text) => {
return (normalizeEOL(text)
// replace tabs with spaces so they render and measure correctly
.replace(/\t/g, " "));
};