docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
84 lines (83 loc) • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convert = exports.inch = exports.cm = exports.twip = exports.opt = exports.hpt = exports.emu = exports.pt = void 0;
const parameter_checking_js_1 = require("./parameter-checking.js");
function _convert(points) {
// Ensure points is not NaN
(0, parameter_checking_js_1.checkForForbiddenParameters)(points, parameter_checking_js_1.isValidNumber, true);
return {
pt: points,
emu: points * 12700,
hpt: points * 2,
opt: points * 8,
twip: points * 20,
inch: points * (1 / 72),
cm: points * (2.54 / 72),
};
}
/**
* Converts points to any of the other units of length.
*/
function pt(amount) {
return _convert(amount);
}
exports.pt = pt;
/**
* Converts English metric units to any of the other units of length.
*/
function emu(amount) {
return _convert(amount / 12700);
}
exports.emu = emu;
/**
* Converts half-points to any of the other units of length.
*/
function hpt(amount) {
return _convert(amount / 2);
}
exports.hpt = hpt;
/**
* Converts 8th-points to any of the other units of length.
*/
function opt(amount) {
return _convert(amount / 8);
}
exports.opt = opt;
/**
* Converts twentieth-points to any of the other units of length.
*/
function twip(amount) {
return _convert(amount / 20);
}
exports.twip = twip;
/**
* Converts centimeters to any of the other units of length.
*/
function cm(amount) {
return _convert(amount / (2.54 / 72));
}
exports.cm = cm;
/**
* Converts inches to any of the other units of length.
*/
function inch(amount) {
return _convert(amount / (1 / 72));
}
exports.inch = inch;
const ingestors = {
cm,
pt,
hpt,
opt,
inch,
twip,
emu,
};
function convert(value, unit) {
const ingestor = ingestors[unit];
if (!ingestor) {
throw new Error(`Unknown unit "${unit}"`);
}
return ingestor(value);
}
exports.convert = convert;