Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #107 from readium/fix/upgrade-readium-css
Browse files Browse the repository at this point in the history
Simplify Readium CSS injection and remove injection of the default stylesheet
  • Loading branch information
mickael-menu authored Jun 2, 2020
2 parents 84f03de + 131b1bb commit 67139dd
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 520 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.json.JSONArray
import org.json.JSONObject
import org.readium.r2.shared.Injectable
import org.readium.r2.shared.ReadiumCSSName
import org.readium.r2.shared.publication.ContentLayout
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.epub.EpubLayout
import org.readium.r2.shared.publication.epub.layout
Expand Down Expand Up @@ -85,17 +86,18 @@ internal class ContentFiltersEpub(private val userPropertiesPath: String?, priva
if (endHeadIndex == -1)
return stream

val cssStyle = publication.cssStyle
val contentLayout = publication.contentLayout

val endIncludes = mutableListOf<String>()
val beginIncludes = mutableListOf<String>()
beginIncludes.add("<meta name=\"viewport\" content=\"width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\" />")

beginIncludes.add(getHtmlLink("/"+ Injectable.Style.rawValue +"/$cssStyle-before.css"))
beginIncludes.add(getHtmlLink("/"+ Injectable.Style.rawValue +"/$cssStyle-default.css"))
endIncludes.add(getHtmlLink("/"+ Injectable.Style.rawValue +"/$cssStyle-after.css"))
endIncludes.add(getHtmlScript("/"+ Injectable.Script.rawValue +"/touchHandling.js"))
endIncludes.add(getHtmlScript("/"+ Injectable.Script.rawValue +"/utils.js"))
beginIncludes.add(getHtmlLink("/assets/readium-css/${contentLayout.readiumCSSPath}ReadiumCSS-before.css"))
endIncludes.add(getHtmlLink("/assets/readium-css/${contentLayout.readiumCSSPath}ReadiumCSS-after.css"))
endIncludes.add(getHtmlScript("/assets/scripts/touchHandling.js"))
endIncludes.add(getHtmlScript("/assets/scripts/utils.js"))
endIncludes.add(getHtmlScript("/assets/scripts/crypto-sha256.js"))
endIncludes.add(getHtmlScript("/assets/scripts/highlight.js"))

customResources?.let {
// Inject all custom resourses
Expand All @@ -120,7 +122,7 @@ internal class ContentFiltersEpub(private val userPropertiesPath: String?, priva
resourceHtml = StringBuilder(resourceHtml).insert(endHeadIndex, element).toString()
endHeadIndex += element.length
}
resourceHtml = StringBuilder(resourceHtml).insert(endHeadIndex, getHtmlFont("/fonts/OpenDyslexic-Regular.otf")).toString()
resourceHtml = StringBuilder(resourceHtml).insert(endHeadIndex, getHtmlFont(fontFamily = "OpenDyslexic", href = "/assets/fonts/OpenDyslexic-Regular.otf")).toString()
resourceHtml = StringBuilder(resourceHtml).insert(endHeadIndex, "<style>@import url('https://fonts.googleapis.com/css?family=PT+Serif|Roboto|Source+Sans+Pro|Vollkorn');</style>\n").toString()

// Inject userProperties
Expand Down Expand Up @@ -186,10 +188,10 @@ internal class ContentFiltersEpub(private val userPropertiesPath: String?, priva
return resourceHtml.toByteArray().inputStream()
}

private fun getHtmlFont(resourceName: String): String {
val prefix = "<style type=\"text/css\"> @font-face{font-family: \"OpenDyslexic\"; src:url(\""
private fun getHtmlFont(fontFamily: String, href: String): String {
val prefix = "<style type=\"text/css\"> @font-face{font-family: \"$fontFamily\"; src:url(\""
val suffix = "\") format('truetype');}</style>\n"
return prefix + resourceName + suffix
return prefix + href + suffix
}

private fun getHtmlLink(resourceName: String): String {
Expand Down Expand Up @@ -319,10 +321,15 @@ internal class ContentFiltersEpub(private val userPropertiesPath: String?, priva
return string
}

private val ContentLayout.readiumCSSPath: String get() = when(this) {
ContentLayout.LTR -> ""
ContentLayout.RTL -> "rtl/"
ContentLayout.CJK_VERTICAL -> "cjk-vertical/"
ContentLayout.CJK_HORIZONTAL -> "cjk-horizontal/"
}

}


internal class ContentFiltersCbz : ContentFilters

/** Content filter for LCP protected packages (except EPUB). */
Expand All @@ -343,4 +350,4 @@ internal class ContentFiltersLcp : ContentFilters {
return DrmDecoder().decoding(inputStream, resourceLink, container.drm).readBytes()
}

}
}
62 changes: 62 additions & 0 deletions r2-streamer/src/main/java/org/readium/r2/streamer/server/Assets.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Module: r2-streamer-kotlin
* Developers: Mickaël Menu
*
* Copyright (c) 2020. Readium Foundation. All rights reserved.
* Use of this source code is governed by a BSD-style license which is detailed in the
* LICENSE file present in the project repository where this source code is maintained.
*/

package org.readium.r2.streamer.server

import android.content.res.AssetManager
import android.net.Uri
import org.readium.r2.shared.extensions.isParentOf
import org.readium.r2.shared.format.Format
import org.readium.r2.shared.format.MediaType
import java.io.File
import java.io.InputStream


/**
* Files to be served from the application's assets.
*
* @param basePath Base path (ignoring host) from where the files are served.
* @param fallbackMediaType Media type which will be used for responses when it can't be determined
* from the served file.
*/
internal class Assets(
private val assetManager: AssetManager,
private val basePath: String,
private val fallbackMediaType: MediaType = MediaType.BINARY
) {
private val assets: MutableList<Pair<String, File>> = mutableListOf()

fun add(href: String, path: String) {
// Inserts at the beginning to take precedence over already registered assets.
assets.add(0, Pair(href, File("/$path").canonicalFile))
}

fun find(uri: Uri): ServedAsset? {
val path = uri.path?.removePrefix(basePath) ?: return null

for ((href, file) in assets) {
if (path.startsWith(href)) {
val requestedFile = File(file, path.removePrefix(href)).canonicalFile
// Makes sure that the requested file is `file` or one of its descendant.
if (file == requestedFile || file.isParentOf(requestedFile)) {
val mediaType = Format.of(fileExtension = requestedFile.extension)?.mediaType ?: fallbackMediaType
return ServedAsset(assetManager.open(requestedFile.path.removePrefix("/")), mediaType)
}
}
}

return null
}

data class ServedAsset(
val stream: InputStream,
val mediaType: MediaType
)

}
60 changes: 60 additions & 0 deletions r2-streamer/src/main/java/org/readium/r2/streamer/server/Files.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Module: r2-streamer-kotlin
* Developers: Aferdita Muriqi, Clément Baumann
*
* Copyright (c) 2018. Readium Foundation. All rights reserved.
* Use of this source code is governed by a BSD-style license which is detailed in the
* LICENSE file present in the project repository where this source code is maintained.
*/

package org.readium.r2.streamer.server

import android.net.Uri
import org.readium.r2.shared.extensions.isParentOf
import org.readium.r2.shared.format.Format
import org.readium.r2.shared.format.MediaType
import java.io.File

/**
* Files to be served from the file system.
*
* @param basePath Base path (ignoring host) from where the files are served.
* @param fallbackMediaType Media type which will be used for responses when it can't be determined
* from the served file.
*/
internal class Files(
private val basePath: String,
private val fallbackMediaType: MediaType = MediaType.BINARY
) {
private val files: MutableMap<String, File> = mutableMapOf()

operator fun set(href: String, file: File) {
files[href] = file.canonicalFile
}

operator fun get(key: String): File? = files[key]

fun find(uri: Uri): ServedFile? {
val path = uri.path?.removePrefix(basePath) ?: return null

for ((href, file) in files) {
if (path.startsWith(href)) {
val requestedFile = File(file, path.removePrefix(href)).canonicalFile
// Makes sure that the requested file is `file` or one of its descendant.
if (file.isParentOf(requestedFile)) {
return ServedFile(requestedFile, fallbackMediaType)
}
}
}

return null
}

data class ServedFile(
val file: File,
private val fallbackMediaType: MediaType
) {
val mediaType: MediaType get() = Format.of(file)?.mediaType ?: fallbackMediaType
}

}
24 changes: 0 additions & 24 deletions r2-streamer/src/main/java/org/readium/r2/streamer/server/Fonts.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ class Resources {
}
}

fun getPair(key: String): Any {
return resources[key] ?: ""
}

fun get(key: String): String =
fun get(key: String): String? =
when (val resource = resources[key]) {
is Pair<*, *> -> resource.first as? String
else -> resource as? String
} ?: ""
}

}
Loading

0 comments on commit 67139dd

Please sign in to comment.