-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.txt
More file actions
158 lines (145 loc) · 4.97 KB
/
Copy pathreader.txt
File metadata and controls
158 lines (145 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import fs from 'node:fs'
import path from 'node:path'
import { initEpubFile } from '@svg-ebook-reader/epub-parser'
import type { Page, SvgRenderOptions } from '@svg-ebook-reader/svg-render'
import { SvgRender } from '@svg-ebook-reader/svg-render'
export async function initAllPage(
epubPath: string,
svgRenderOptions: SvgRenderOptions = {},
) {
const pages: Page[] = []
const epub = await initEpubFile(epubPath, svgRenderOptions.imageRoot)
const tableOfContents = epub.getToc()
for (let i = 0; i < tableOfContents.length; i++) {
const id = tableOfContents[i].id
const chapter = await epub.getChapter(id)
const renderer = new SvgRender(id, svgRenderOptions)
await renderer.addContents(chapter.contents)
const pageOfChapter = renderer.getPages()
pages.push(...pageOfChapter)
}
// save svg result to disk if savePath.length > 0
const savePath = svgRenderOptions.saveDir
if (savePath && savePath.length > 0) {
const fileName = epub.getFileName()
pages.forEach((page) => {
const dir = path.resolve(
savePath,
`${fileName}-${page.chapterId}-${page.pageIndex}-${page.lastContentIndexOfPage}.svg`,
)
fs.writeFileSync(dir, page.svg)
})
}
return pages
}
// /**
// * Combine epub-parser and svg-render to reader,
// * expose nextPage, prevPage and init methods to get page string
// */
// export function Reader(
// epubPath: string,
// svgRenderOptions: SvgRenderOptions = {},
// ) {
// // chapter > pages > page(string)
// let epub: EpubFile
// let tableOfContents: (TOCOutput | ManifestItem)[] = []
// let pageIndex: number = 0
// let chapterIndex: number = 0
// let currChapterPages: string[] | undefined = []
// let nextChapterPages: string[] | undefined
// let prevChapterPages: string[] | undefined
// const loadChapter = async (chapterIndex: number) => {
// const id = tableOfContents[chapterIndex].id
// const chapter = await epub.getChapter(id)
// const renderer = new SvgRender(id, svgRenderOptions)
// await renderer.addContents(chapter.contents)
// return renderer.pages
// }
// return {
// async init() {
// epub = await initEpubFile(epubPath, svgRenderOptions.imageRoot)
// tableOfContents = epub.getToc()
// currChapterPages = await loadChapter(chapterIndex)
// nextChapterPages = chapterIndex + 1 < tableOfContents.length
// ? await loadChapter(chapterIndex + 1)
// : undefined
// prevChapterPages = chapterIndex - 1 >= 0
// ? await loadChapter(chapterIndex - 1)
// : undefined
// // for (let i = 0; i < currChapterPages.length; i++) {
// // if (!fs.existsSync(`./example/${chapterIndex}-${i}.svg`)) {
// // fs.writeFileSync(`./example/${chapterIndex}-${0}.svg`, currChapterPages[0])
// // fs.writeFileSync(`./example/${chapterIndex}-${1}.svg`, currChapterPages[1])
// // fs.writeFileSync(`./example/${chapterIndex}-${2}.svg`, currChapterPages[2])
// // }
// // }
// return currChapterPages[pageIndex]
// },
// async toNextPage() {
// const nextPageIndex = pageIndex + 1
// if (nextPageIndex < currChapterPages!.length) {
// pageIndex = nextPageIndex
// return currChapterPages![pageIndex]
// }
// else {
// if (!nextChapterPages) {
// return undefined
// }
// else {
// pageIndex = 0
// prevChapterPages = currChapterPages
// currChapterPages = nextChapterPages
// chapterIndex++
// if (chapterIndex + 1 >= tableOfContents.length) {
// nextChapterPages = undefined
// return undefined
// }
// else {
// nextChapterPages = undefined
// nextChapterPages = await loadChapter(chapterIndex + 1)
// }
// return currChapterPages[pageIndex]
// }
// }
// },
// toPrevPage() {
// const prevPageIndex = pageIndex - 1
// if (prevPageIndex >= 0) {
// pageIndex = prevPageIndex
// return currChapterPages![pageIndex]
// }
// else {
// if (!prevChapterPages) {
// return undefined
// }
// else {
// pageIndex = 0
// nextChapterPages = currChapterPages
// currChapterPages = prevChapterPages
// chapterIndex--
// if (chapterIndex - 1 < 0) {
// prevChapterPages = undefined
// return undefined
// }
// else {
// prevChapterPages = undefined
// loadChapter(chapterIndex - 1)
// .then((pages) => {
// prevChapterPages = pages
// })
// }
// return currChapterPages[pageIndex]
// }
// }
// },
// getPageIndex() {
// return pageIndex
// },
// getChapterIndex() {
// return chapterIndex
// },
// getCurrChapterPageCount() {
// return currChapterPages!.length
// },
// }
// }