Skip to content

Commit

Permalink
refactored MediaSizeSupported to support checking sizes specified by …
Browse files Browse the repository at this point in the history
…rangeOfInteger values (see PWG 5100.7)
  • Loading branch information
gmuth committed Sep 10, 2024
1 parent d6acb64 commit 73c2e80
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/de/gmuth/ipp/attributes/MediaSize.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.gmuth.ipp.attributes

/**
* Copyright (c) 2020-2023 Gerhard Muth
* Copyright (c) 2020-2024 Gerhard Muth
*/

import de.gmuth.ipp.core.*
Expand All @@ -22,7 +22,7 @@ data class MediaSize(val xDimension: Int, val yDimension: Int, val name: String?

override fun toString() = StringBuilder().run {
name?.let { append("$it ") }
append("${xDimension} x ${yDimension}")
append("${xDimension}x${yDimension}")
toString()
}

Expand Down
66 changes: 45 additions & 21 deletions src/main/kotlin/de/gmuth/ipp/attributes/MediaSizeSupported.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,67 @@
package de.gmuth.ipp.attributes

/**
* Copyright (c) 2020-2023 Gerhard Muth
* Copyright (c) 2020-2024 Gerhard Muth
*/

import de.gmuth.ipp.core.IppAttributesGroup
import de.gmuth.ipp.core.IppCollection

/**
* This attribute lists the supported values of the "media-size" member attribute (section 5.3.1.15).
* Unlike the "media-size" member attribute, the "x-dimension" and "y- dimension" member attributes of
* "media-size-supported" have a syntax of "integer(1:MAX) | rangeOfInteger(1:MAX)" to allow for arbitrary
* https://ftp.pwg.org/pub/pwg/candidates/cs-ippjobext21-20230210-5100.7.pdf
*
* 6.9.50 media-size-supported (1setOf collection)
* This REQUIRED attribute lists the supported values of the "media-size" member attribute (section 6.3.1.15).
* Unlike the "media-size" member attribute, the "x-dimension" and "y-dimension" member attributes of
* "media-size-supported" have a syntax of "integer(1:MAX) | rangeOflnteger(1:MAX)" to allow for arbitrary
* ranges of sizes for custom and roll-fed media.
*
* Unit: 1/100 mm, e.g. 2540 = 1 inch
*/
data class MediaSizeSupported(val xDimension: Any, val yDimension: Any) {

init {
require(dimensionsAreInt() || dimensionsAreIntRange())
}
class MediaSizeSupported(val supportedSizes: Collection<SupportedSize>) {

override fun toString() = "$xDimension x $yDimension"
companion object {
fun fromAttributes(attributes: IppAttributesGroup) = MediaSizeSupported(
attributes
.getValues<List<IppCollection>>("media-size-supported")
.map { SupportedSize.fromIppCollection(it) }
)
}

fun dimensionsAreInt() = // integer(1:MAX)
xDimension is Int && yDimension is Int
fun supports(mediaSize: MediaSize) =
supportedSizes.any { it.supports(mediaSize) }

fun dimensionsAreIntRange() = // rangeOfInteger(1:MAX)
xDimension is IntRange && yDimension is IntRange
override fun toString() = supportedSizes.toString()

fun toMediaSize() =
if (dimensionsAreInt()) MediaSize(xDimension as Int, yDimension as Int)
else error("dimensions are not Int")
data class SupportedSize(val xDimension: Any, val yDimension: Any) {

companion object {
fun fromIppCollection(ippCollection: IppCollection) = ippCollection.run {
MediaSizeSupported(
getValue("x-dimension"),
getValue("y-dimension")
companion object {
fun fromIppCollection(ippCollection: IppCollection) = SupportedSize(
ippCollection.getValue("x-dimension"),
ippCollection.getValue("y-dimension")
)
}

init {
require(dimensionsAreInt() || dimensionsAreIntRange())
}

override fun toString() = "${xDimension}x${yDimension}"

fun dimensionsAreInt() = // integer(1:MAX)
xDimension is Int && yDimension is Int

fun dimensionsAreIntRange() = // rangeOfInteger(1:MAX)
xDimension is IntRange && yDimension is IntRange

fun supports(mediaSize: MediaSize) =
if (dimensionsAreInt()) {
xDimension as Int == mediaSize.xDimension
&& yDimension as Int == mediaSize.yDimension
} else { // dimensionsAreIntRange()
(xDimension as IntRange).contains(mediaSize.xDimension)
&& (yDimension as IntRange).contains(mediaSize.yDimension)
}
}
}
11 changes: 3 additions & 8 deletions src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,8 @@ class IppPrinter(
val mediaSizeDefault: MediaSize
get() = MediaSize.fromIppCollection(attributes.getValue("media-size-default"))

val mediaSizeSupported: List<MediaSizeSupported>
get() = attributes
.getValues<List<IppCollection>>("media-size-supported")
.map { MediaSizeSupported.fromIppCollection(it) }
val mediaSizeSupported: MediaSizeSupported
get() = MediaSizeSupported.fromAttributes(attributes)

val mediaColDefault: MediaCollection
get() = MediaCollection.fromIppCollection(attributes.getValue("media-col-default"))
Expand Down Expand Up @@ -247,10 +245,7 @@ class IppPrinter(
fun isMediaEmpty() = stateReasons.any { it.contains("media-empty") } // media-empty-report
fun isMediaNeeded() = stateReasons.contains("media-needed")

fun isMediaSizeSupported(size: MediaSize) = mediaSizeSupported
.filter { it.dimensionsAreInt() } // IntRange dimensions are not considered
.map { it.toMediaSize() }
.any { it.equalsByDimensions(size) }
fun isMediaSizeSupported(size: MediaSize) = mediaSizeSupported.supports(size)

fun isMediaSizeReady(size: MediaSize) = mediaColReady
.any { it.size?.equalsByDimensions(size) ?: false }
Expand Down

0 comments on commit 73c2e80

Please sign in to comment.