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

Commit

Permalink
Add Presentation.layoutOf(Link)
Browse files Browse the repository at this point in the history
Use Long for file length instead of Int
Remove Metadata.layout (now in Presentation.layout)
  • Loading branch information
mickael-menu committed Feb 10, 2020
1 parent 7a21ab9 commit 362fa97
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 47 deletions.
16 changes: 16 additions & 0 deletions r2-shared/src/main/java/org/readium/r2/shared/extensions/JSON.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ fun JSONObject.optNullableInt(name: String, remove: Boolean = false): Int? {
return value
}

/**
* Returns the value mapped by [name] if it exists, coercing it if necessary, or [null] if no such
* mapping exists.
* If [remove] is true, then the mapping will be removed from the [JSONObject].
*/
fun JSONObject.optNullableLong(name: String, remove: Boolean = false): Long? {
if (!has(name)) {
return null
}
val value = optLong(name)
if (remove) {
this.remove(name)
}
return value
}

/**
* Returns the value mapped by [name] if it exists, coercing it if necessary, or [null] if no such
* mapping exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ data class LocalizedString(val translations: Map<String?, Translation> = emptyMa
* Returns a new [LocalizedString] after adding (or replacing) the translation with the given
* [language].
*/
fun withString(language: String?, string: String): LocalizedString =
fun copyWithString(language: String?, string: String): LocalizedString =
copy(translations = translations + Pair(language, Translation(string = string)))

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.json.JSONObject
import org.readium.r2.shared.JSONable
import org.readium.r2.shared.util.logging.WarningLogger
import org.readium.r2.shared.extensions.optNullableInt
import org.readium.r2.shared.extensions.optNullableLong
import org.readium.r2.shared.extensions.optNullableString
import org.readium.r2.shared.util.logging.JsonWarning
import org.readium.r2.shared.util.logging.log
Expand All @@ -34,7 +35,7 @@ import org.readium.r2.shared.util.logging.log
data class Encryption(
val algorithm: String,
val compression: String? = null,
val originalLength: Int? = null,
val originalLength: Long? = null,
val profile: String? = null,
val scheme: String? = null
) : JSONable, Parcelable {
Expand Down Expand Up @@ -68,8 +69,8 @@ data class Encryption(
compression = json.optNullableString("compression"),
// Fallback on [original-length] for legacy reasons
// See https://github.com/readium/webpub-manifest/pull/43
originalLength = json.optNullableInt("originalLength")
?: json.optNullableInt("original-length"),
originalLength = json.optNullableLong("originalLength")
?: json.optNullableLong("original-length"),
profile = json.optNullableString("profile"),
scheme = json.optNullableString("scheme")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

package org.readium.r2.shared.publication.epub

import org.readium.r2.shared.publication.Metadata

// EPUB extensions for [Metadata].
// https://readium.org/webpub-manifest/schema/extensions/epub/metadata.schema.json
import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.presentation.Presentation

/**
* Hints how the layout of the resource should be presented.
* Get the layout of the given resource in this publication.
* Falls back on REFLOWABLE.
*/
val Metadata.layout: EpubLayout?
get() = EpubLayout(this["layout"] as? String)
fun Presentation.layoutOf(link: Link): EpubLayout {
return link.properties.layout
?: layout
?: EpubLayout.REFLOWABLE
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class LocalizedStringTest {
)),
LocalizedString.fromStrings(mapOf(
"en" to "a string"
)).withString("fr", "une chaîne")
)).copyWithString("fr", "une chaîne")
)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Module: r2-shared-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.shared.publication.epub

import org.junit.Assert.*
import org.junit.Test
import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Properties
import org.readium.r2.shared.publication.presentation.Presentation

class PresentationTest {

@Test
fun `Get the layout of a reflowable resource`() {
assertEquals(
EpubLayout.REFLOWABLE,
Presentation(layout = null).layoutOf(createLink(EpubLayout.REFLOWABLE))
)
}

@Test
fun `Get the layout of a fixed resource`() {
assertEquals(
EpubLayout.FIXED,
Presentation(layout = null).layoutOf(createLink(EpubLayout.FIXED))
)
}

@Test
fun `The layout of a resource takes precedence over the document layout`() {
assertEquals(
EpubLayout.FIXED,
Presentation(layout = EpubLayout.REFLOWABLE).layoutOf(createLink(EpubLayout.FIXED))
)
}

@Test
fun `Get the layout falls back on the document layout`() {
assertEquals(
EpubLayout.FIXED,
Presentation(layout = EpubLayout.FIXED).layoutOf(createLink(null))
)
}

@Test
fun `Get the layout falls back on REFLOWABLE`() {
assertEquals(
EpubLayout.REFLOWABLE,
Presentation(layout = null).layoutOf(createLink(null))
)
}

private fun createLink(layout: EpubLayout?) = Link(
href = "res",
properties = Properties(
otherProperties = layout?.let { mapOf("layout" to layout.value) }
?: emptyMap()
)
)

}

0 comments on commit 362fa97

Please sign in to comment.