-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored MediaSizeSupported to support checking sizes specified by …
…rangeOfInteger values (see PWG 5100.7)
- Loading branch information
Showing
3 changed files
with
50 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 45 additions & 21 deletions
66
src/main/kotlin/de/gmuth/ipp/attributes/MediaSizeSupported.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters