fixed font rendering
This commit is contained in:
parent
1d3daf5c8c
commit
9400ff5535
@ -88,35 +88,55 @@ class BookStore(private val context: Context) {
|
||||
|
||||
private fun extractEpub(book: EpubBook, baseDirectory: File) {
|
||||
try {
|
||||
// First extract CSS files
|
||||
// First extract fonts and CSS files
|
||||
book.resources.all
|
||||
.filter { resource ->
|
||||
resource.mediaType?.toString()?.contains("css") ?: false
|
||||
val mediaType = resource.mediaType?.toString() ?: ""
|
||||
mediaType.contains("css") ||
|
||||
mediaType.contains("font") ||
|
||||
resource.href.endsWith(".otf") ||
|
||||
resource.href.endsWith(".ttf") ||
|
||||
resource.href.endsWith(".woff") ||
|
||||
resource.href.endsWith(".woff2")
|
||||
}
|
||||
.forEach { resource ->
|
||||
val cssPath = resource.href
|
||||
.replace("../", "")
|
||||
.replace("./", "")
|
||||
// Extract to both original location and root fonts directory
|
||||
val paths = listOf(
|
||||
resource.href.replace("../", "").replace("./", ""),
|
||||
"Fonts/${resource.href.substringAfterLast("/")}",
|
||||
"Text/Fonts/${resource.href.substringAfterLast("/")}"
|
||||
).distinct()
|
||||
|
||||
val resourceFile = File(baseDirectory, cssPath)
|
||||
resourceFile.parentFile?.mkdirs()
|
||||
resourceFile.writeBytes(resource.data)
|
||||
|
||||
Timber.d("""
|
||||
CSS File Extracted:
|
||||
- Original href: ${resource.href}
|
||||
- Final path: $cssPath
|
||||
- Full path: ${resourceFile.absolutePath}
|
||||
- Size: ${resourceFile.length()}
|
||||
- Exists: ${resourceFile.exists()}
|
||||
- Content sample: ${String(resource.data.take(100).toByteArray())}
|
||||
""".trimIndent())
|
||||
paths.forEach { path ->
|
||||
val resourceFile = File(baseDirectory, path)
|
||||
resourceFile.parentFile?.mkdirs()
|
||||
resourceFile.writeBytes(resource.data)
|
||||
|
||||
// Log font file extraction
|
||||
if (resource.href.matches(Regex(".+\\.(otf|ttf|woff|woff2)$"))) {
|
||||
Timber.d("""
|
||||
Font File Extracted:
|
||||
- Original href: ${resource.href}
|
||||
- Path: $path
|
||||
- Full path: ${resourceFile.absolutePath}
|
||||
- Size: ${resourceFile.length()}
|
||||
- Exists: ${resourceFile.exists()}
|
||||
- Media Type: ${resource.mediaType}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Then extract HTML files
|
||||
// Then extract remaining files
|
||||
book.resources.all
|
||||
.filter { resource ->
|
||||
!(resource.mediaType?.toString()?.contains("css") ?: false)
|
||||
val mediaType = resource.mediaType?.toString() ?: ""
|
||||
!(mediaType.contains("css") ||
|
||||
mediaType.contains("font") ||
|
||||
resource.href.endsWith(".otf") ||
|
||||
resource.href.endsWith(".ttf") ||
|
||||
resource.href.endsWith(".woff") ||
|
||||
resource.href.endsWith(".woff2"))
|
||||
}
|
||||
.forEach { resource ->
|
||||
val resourcePath = resource.href.replace("../", "").replace("./", "")
|
||||
@ -133,26 +153,34 @@ class BookStore(private val context: Context) {
|
||||
.escapeMode(org.jsoup.nodes.Entities.EscapeMode.xhtml)
|
||||
.prettyPrint(false)
|
||||
|
||||
// Ensure head section exists
|
||||
var head = doc.head()
|
||||
if (head == null) {
|
||||
head = doc.createElement("head")
|
||||
doc.prependChild(head)
|
||||
}
|
||||
|
||||
// Process CSS links
|
||||
doc.select("link[rel=stylesheet]").forEach { link ->
|
||||
val cssHref = link.attr("href")
|
||||
.replace("../", "")
|
||||
.replace("./", "")
|
||||
|
||||
// Create a properly formatted self-closing link tag
|
||||
link.tagName("link")
|
||||
.attr("rel", "stylesheet")
|
||||
.attr("type", "text/css")
|
||||
.attr("href", cssHref)
|
||||
}
|
||||
|
||||
// Update font references in CSS and style tags
|
||||
doc.select("style").forEach { style ->
|
||||
val cssContent = style.html()
|
||||
val updatedCss = cssContent.replace(
|
||||
Regex("url\\(['\"]?(.*?)['\"]?\\)"),
|
||||
{ matchResult ->
|
||||
val fontPath = matchResult.groupValues[1]
|
||||
.replace("../", "")
|
||||
.replace("./", "")
|
||||
.substringAfterLast("/")
|
||||
"url('Fonts/$fontPath')"
|
||||
}
|
||||
)
|
||||
style.html(updatedCss)
|
||||
}
|
||||
|
||||
// Write properly formatted XHTML
|
||||
val xhtml = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
||||
@ -170,6 +198,9 @@ class BookStore(private val context: Context) {
|
||||
// Set permissions
|
||||
baseDirectory.walk().forEach { file ->
|
||||
file.setReadable(true, false)
|
||||
if (file.isDirectory) {
|
||||
file.setExecutable(true, false)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Error extracting EPUB: ${e.message}")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user