import pretty from 'pretty'; import { createQRCode } from '../create-qrcode'; import { getSymbols } from './symbols'; const symbols = getSymbols(); const qrCodeOptions = { showLogo: false, width: 100, padding: 0, radius: 0 }; export interface QuestLayoutOptions { width?: number; assetUrl: string; serviceRequestUrl: string; manufacturer: string; model: string; serialNumber: string; coastId: string; yearAcquired?: string | number; } export function createQuestLayout({ width, assetUrl, serviceRequestUrl, manufacturer, model, serialNumber, coastId, yearAcquired, }: QuestLayoutOptions): string { const assetQRCode = createQRCode({ url: assetUrl, ...qrCodeOptions }); const serviceRequestQRCode = createQRCode({ url: serviceRequestUrl, ...qrCodeOptions }); return pretty(` ${symbols} ${getYearAcquiredText(yearAcquired?.toString())} ${assetQRCode} ${serviceRequestQRCode} ${getSvgText(manufacturer)} ${getSvgText(model)} ${getSvgText(serialNumber)} ${getSvgText(coastId)} `); } // this controls letter spacing for the monospace text elements const glyphWidth = 5.5; type SvgTextJustify = 'left' | 'center' | 'right'; function getSvgText(text?: string, justify: SvgTextJustify = 'center'): string { if (!text) { return ''; } const characters = text.trim().replace(/ +/g, ' ').slice(0, 20).toUpperCase().split(''); const symbols = characters.map(getSymbolForChar).join(''); // offset by half the text width to center the text const offset = getOffset(characters, justify); return `${symbols}`; } function getOffset(characters: string[], justify: SvgTextJustify): number { switch (justify) { case 'left': return 0; case 'right': return characters.length * glyphWidth; case 'center': return (characters.length * glyphWidth) / 2; } } function getSymbolForChar(char: string, index: number): string { if (char === ' ') { return ''; } else { return ``; } } function getEncodedChar(char: string): string { // something about the '%2C' encoding causes commas not the show up in the // PDF, even though they show up in the SVG if (char === ',') { return 'comma'; } else if (char === '/') { return 'slash'; } else { return encodeURIComponent(char); } } function getYearAcquiredText(year?: string): string { if (!year) { return ''; } return ` ${getSvgText(year, 'right')} `; }