|
| 1 | +/** |
| 2 | + * Core EPUB to XTC/XTCH converter |
| 3 | + * Uses CREngine WASM for EPUB rendering |
| 4 | + */ |
| 5 | + |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | +const { applyDithering, applyNegative } = require('./dither'); |
| 9 | +const { encodeXTG, encodeXTH, buildXTCContainer } = require('./encoder'); |
| 10 | + |
| 11 | +let Module = null; |
| 12 | +let renderer = null; |
| 13 | + |
| 14 | +/** |
| 15 | + * Initialize CREngine WASM module |
| 16 | + */ |
| 17 | +async function initWasm() { |
| 18 | + if (Module) return; |
| 19 | + |
| 20 | + const wasmPath = path.join(__dirname, '..', 'web', 'crengine.js'); |
| 21 | + |
| 22 | + if (!fs.existsSync(wasmPath)) { |
| 23 | + throw new Error(`CREngine WASM not found at: ${wasmPath}`); |
| 24 | + } |
| 25 | + |
| 26 | + // Load CREngine module |
| 27 | + const CREngine = require(wasmPath); |
| 28 | + Module = await CREngine(); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Create renderer with specified dimensions |
| 33 | + */ |
| 34 | +function createRenderer(width, height) { |
| 35 | + if (!Module) { |
| 36 | + throw new Error('WASM module not initialized. Call initWasm() first.'); |
| 37 | + } |
| 38 | + renderer = new Module.EpubRenderer(width, height); |
| 39 | + |
| 40 | + // Disable built-in status bar |
| 41 | + renderer.configureStatusBar(false, false, false, false, false, false, false, false, false); |
| 42 | + |
| 43 | + return renderer; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Register font from file |
| 48 | + */ |
| 49 | +async function registerFont(fontPath) { |
| 50 | + if (!renderer) { |
| 51 | + throw new Error('Renderer not initialized'); |
| 52 | + } |
| 53 | + |
| 54 | + const fontData = fs.readFileSync(fontPath); |
| 55 | + const fontName = path.basename(fontPath); |
| 56 | + |
| 57 | + const ptr = Module.allocateMemory(fontData.length); |
| 58 | + Module.HEAPU8.set(new Uint8Array(fontData), ptr); |
| 59 | + renderer.registerFontFromMemory(ptr, fontData.length, fontName); |
| 60 | + Module.freeMemory(ptr); |
| 61 | + |
| 62 | + return fontName; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Load EPUB file into renderer |
| 67 | + */ |
| 68 | +async function loadEpub(epubPath) { |
| 69 | + if (!renderer) { |
| 70 | + throw new Error('Renderer not initialized'); |
| 71 | + } |
| 72 | + |
| 73 | + const epubData = fs.readFileSync(epubPath); |
| 74 | + |
| 75 | + const ptr = Module.allocateMemory(epubData.length); |
| 76 | + Module.HEAPU8.set(new Uint8Array(epubData), ptr); |
| 77 | + |
| 78 | + try { |
| 79 | + renderer.loadEpubFromMemory(ptr, epubData.length); |
| 80 | + } finally { |
| 81 | + Module.freeMemory(ptr); |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + pageCount: renderer.getPageCount(), |
| 86 | + info: renderer.getDocumentInfo() || {}, |
| 87 | + toc: renderer.getToc() || [] |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Apply rendering settings |
| 93 | + */ |
| 94 | +function applySettings(settings) { |
| 95 | + if (!renderer) { |
| 96 | + throw new Error('Renderer not initialized'); |
| 97 | + } |
| 98 | + |
| 99 | + const { margins, font, lineHeight, textAlignValue, hyphenation } = settings; |
| 100 | + |
| 101 | + renderer.setMargins( |
| 102 | + margins.left, |
| 103 | + margins.top, |
| 104 | + margins.right, |
| 105 | + margins.bottom |
| 106 | + ); |
| 107 | + renderer.setFontSize(font.size); |
| 108 | + renderer.setFontWeight(font.weight); |
| 109 | + renderer.setInterlineSpace(lineHeight); |
| 110 | + renderer.setTextAlign(textAlignValue); |
| 111 | + |
| 112 | + if (hyphenation.enabled) { |
| 113 | + renderer.setHyphenation(2); // Dictionary-based |
| 114 | + if (renderer.setHyphenationLanguage) { |
| 115 | + renderer.setHyphenationLanguage(hyphenation.language); |
| 116 | + } |
| 117 | + } else { |
| 118 | + renderer.setHyphenation(0); // Disabled |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Render a single page |
| 124 | + */ |
| 125 | +function renderPage(pageNum) { |
| 126 | + if (!renderer) { |
| 127 | + throw new Error('Renderer not initialized'); |
| 128 | + } |
| 129 | + |
| 130 | + renderer.goToPage(pageNum); |
| 131 | + renderer.renderCurrentPage(); |
| 132 | + |
| 133 | + const frameBuffer = renderer.getFrameBuffer(); |
| 134 | + if (!frameBuffer || frameBuffer.length === 0) { |
| 135 | + throw new Error(`Empty frame buffer for page ${pageNum}`); |
| 136 | + } |
| 137 | + |
| 138 | + // Copy buffer (frame buffer may be reused by WASM) |
| 139 | + return new Uint8ClampedArray(frameBuffer); |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Convert single EPUB to XTC/XTCH |
| 144 | + */ |
| 145 | +async function convertEpub(epubPath, outputPath, settings, progressCallback) { |
| 146 | + const { width, height, output } = settings; |
| 147 | + const isHQ = output.format === 'xtch'; |
| 148 | + const bits = isHQ ? 2 : 1; |
| 149 | + |
| 150 | + // Initialize and setup |
| 151 | + await initWasm(); |
| 152 | + createRenderer(width, height); |
| 153 | + |
| 154 | + // Register font |
| 155 | + await registerFont(settings.font.path); |
| 156 | + |
| 157 | + // Load EPUB |
| 158 | + const { pageCount, info, toc } = await loadEpub(epubPath); |
| 159 | + |
| 160 | + if (pageCount === 0) { |
| 161 | + throw new Error('EPUB has no pages'); |
| 162 | + } |
| 163 | + |
| 164 | + // Apply settings after loading (affects pagination) |
| 165 | + applySettings(settings); |
| 166 | + |
| 167 | + // Re-get page count after settings (pagination may change) |
| 168 | + const totalPages = renderer.getPageCount(); |
| 169 | + |
| 170 | + // Render all pages |
| 171 | + const pages = []; |
| 172 | + for (let i = 0; i < totalPages; i++) { |
| 173 | + // Render page |
| 174 | + let imageData = renderPage(i); |
| 175 | + |
| 176 | + // Apply dithering if enabled |
| 177 | + if (output.dithering) { |
| 178 | + imageData = applyDithering(imageData, width, height, bits, output.ditherStrength); |
| 179 | + } |
| 180 | + |
| 181 | + // Apply negative if enabled |
| 182 | + if (output.negative) { |
| 183 | + applyNegative(imageData); |
| 184 | + } |
| 185 | + |
| 186 | + // Encode page |
| 187 | + const encoded = isHQ |
| 188 | + ? encodeXTH(imageData, width, height) |
| 189 | + : encodeXTG(imageData, width, height); |
| 190 | + pages.push(encoded); |
| 191 | + |
| 192 | + // Progress callback |
| 193 | + if (progressCallback) { |
| 194 | + progressCallback(i + 1, totalPages); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // Build container |
| 199 | + const metadata = { |
| 200 | + title: info.title || path.basename(epubPath, '.epub'), |
| 201 | + author: info.author || info.authors || '' |
| 202 | + }; |
| 203 | + |
| 204 | + const container = buildXTCContainer(pages, metadata, toc, width, height, isHQ); |
| 205 | + |
| 206 | + // Write output |
| 207 | + fs.writeFileSync(outputPath, container); |
| 208 | + |
| 209 | + return { |
| 210 | + outputPath, |
| 211 | + pageCount: totalPages, |
| 212 | + format: output.format |
| 213 | + }; |
| 214 | +} |
| 215 | + |
| 216 | +/** |
| 217 | + * Get output path for an EPUB file |
| 218 | + */ |
| 219 | +function getOutputPath(inputPath, outputDir, format) { |
| 220 | + const basename = path.basename(inputPath, '.epub'); |
| 221 | + const extension = format === 'xtch' ? '.xtch' : '.xtc'; |
| 222 | + return path.join(outputDir, basename + extension); |
| 223 | +} |
| 224 | + |
| 225 | +/** |
| 226 | + * Cleanup renderer resources |
| 227 | + */ |
| 228 | +function cleanup() { |
| 229 | + if (renderer) { |
| 230 | + renderer = null; |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +module.exports = { |
| 235 | + initWasm, |
| 236 | + createRenderer, |
| 237 | + registerFont, |
| 238 | + loadEpub, |
| 239 | + applySettings, |
| 240 | + renderPage, |
| 241 | + convertEpub, |
| 242 | + getOutputPath, |
| 243 | + cleanup |
| 244 | +}; |
0 commit comments