docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
73 lines (72 loc) • 1.65 kB
JavaScript
import { checkForForbiddenParameters, isValidNumber } from './parameter-checking.js';
function _convert(points) {
// Ensure points is not NaN
checkForForbiddenParameters(points, 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.
*/
export function pt(amount) {
return _convert(amount);
}
/**
* Converts English metric units to any of the other units of length.
*/
export function emu(amount) {
return _convert(amount / 12700);
}
/**
* Converts half-points to any of the other units of length.
*/
export function hpt(amount) {
return _convert(amount / 2);
}
/**
* Converts 8th-points to any of the other units of length.
*/
export function opt(amount) {
return _convert(amount / 8);
}
/**
* Converts twentieth-points to any of the other units of length.
*/
export function twip(amount) {
return _convert(amount / 20);
}
/**
* Converts centimeters to any of the other units of length.
*/
export function cm(amount) {
return _convert(amount / (2.54 / 72));
}
/**
* Converts inches to any of the other units of length.
*/
export function inch(amount) {
return _convert(amount / (1 / 72));
}
const ingestors = {
cm,
pt,
hpt,
opt,
inch,
twip,
emu,
};
export function convert(value, unit) {
const ingestor = ingestors[unit];
if (!ingestor) {
throw new Error(`Unknown unit "${unit}"`);
}
return ingestor(value);
}