Standard formats: Letter dimensions should be respected

This commit is contained in:
Pierre-Yves Nicolas
2026-05-24 11:27:23 +02:00
parent c3bd144681
commit 0a448c24c9
2 changed files with 26 additions and 19 deletions

View File

@@ -46,12 +46,12 @@ class AndroidPdfWriter : PdfWriter {
val dimensions = page.estimatedDimensions() val dimensions = page.estimatedDimensions()
val (widthMm, heightMm) = when (dimensions) { val (widthMm, heightMm) = when (dimensions) {
is EstimatedDimensions.Physical -> is EstimatedDimensions.Physical ->
clipToMaxFormat(dimensions.widthMm, dimensions.heightMm) constrainToMaxFormat(dimensions.widthMm, dimensions.heightMm)
else -> { else -> {
// No physical dimensions available // No physical dimensions available
val maxDimMm = PaperFormats.A4.heightMm val maxDimMm = PaperFormats.A4.heightMm
val scalePxToMm = maxDimMm / maxOf(widthPx, heightPx) val scalePxToMm = maxDimMm / maxOf(widthPx, heightPx)
clipToMaxFormat(widthPx * scalePxToMm, heightPx * scalePxToMm) constrainToMaxFormat(widthPx * scalePxToMm, heightPx * scalePxToMm)
} }
} }
val widthPoints = widthMm.toFloat() * pointsPerMm val widthPoints = widthMm.toFloat() * pointsPerMm
@@ -71,13 +71,14 @@ class AndroidPdfWriter : PdfWriter {
} }
} }
fun clipToMaxFormat(widthMm: Double, heightMm: Double): Pair<Double, Double> { fun constrainToMaxFormat(widthMm: Double, heightMm: Double): Pair<Double, Double> {
// Normalize to portrait for comparison val maxDim = 297.0 // A4 height
val (w, h) = if (widthMm <= heightMm) widthMm to heightMm else heightMm to widthMm val minDim = 215.9 // Letter width
val portrait = widthMm <= heightMm
val maxFormat = PaperFormats.A4 val scale = minOf(
val scale = minOf(maxFormat.widthMm / w, maxFormat.heightMm / h, 1.0) maxDim / maxOf(widthMm, heightMm),
val clipped = w * scale to h * scale minDim / minOf(widthMm, heightMm),
return if (portrait) clipped else clipped.second to clipped.first 1.0
)
return widthMm * scale to heightMm * scale
} }

View File

@@ -21,36 +21,42 @@ import org.junit.Test
class AndroidPdfWriterTest { class AndroidPdfWriterTest {
@Test fun `portrait smaller than A4 is unchanged`() { @Test fun `portrait smaller than A4 is unchanged`() {
val (w, h) = clipToMaxFormat(100.0, 150.0) val (w, h) = constrainToMaxFormat(100.0, 150.0)
assertThat(w).isEqualTo(100.0) assertThat(w).isEqualTo(100.0)
assertThat(h).isEqualTo(150.0) assertThat(h).isEqualTo(150.0)
} }
@Test fun `portrait taller than A4 is clipped preserving ratio`() { @Test fun `portrait taller than A4 is constrained preserving ratio`() {
val (w, h) = clipToMaxFormat(210.0, 400.0) val (w, h) = constrainToMaxFormat(210.0, 400.0)
assertThat(h).isCloseTo(297.0, offset(0.1)) assertThat(h).isCloseTo(297.0, offset(0.1))
assertThat(w / h).isCloseTo(210.0 / 400.0, offset(0.001)) assertThat(w / h).isCloseTo(210.0 / 400.0, offset(0.001))
} }
@Test fun `landscape wider than A4 is clipped preserving ratio`() { @Test fun `landscape wider than A4 is constrained preserving ratio`() {
val (w, h) = clipToMaxFormat(300.0, 200.0) val (w, h) = constrainToMaxFormat(300.0, 200.0)
assertThat(w).isCloseTo(297.0, offset(0.1)) assertThat(w).isCloseTo(297.0, offset(0.1))
assertThat(w / h).isCloseTo(300.0 / 200.0, offset(0.001)) assertThat(w / h).isCloseTo(300.0 / 200.0, offset(0.001))
} }
@Test fun `exactly A4 is unchanged`() { @Test fun `exactly A4 is unchanged`() {
val (w, h) = clipToMaxFormat(210.0, 297.0) val (w, h) = constrainToMaxFormat(210.0, 297.0)
assertThat(w).isCloseTo(210.0, offset(0.001)) assertThat(w).isCloseTo(210.0, offset(0.001))
assertThat(h).isCloseTo(297.0, offset(0.001)) assertThat(h).isCloseTo(297.0, offset(0.001))
} }
@Test fun `landscape orientation is preserved after clip`() { @Test fun `exactly Letter is unchanged`() {
val (w, h) = clipToMaxFormat(400.0, 250.0) val (w, h) = constrainToMaxFormat(215.9, 279.4)
assertThat(w).isCloseTo(215.9, offset(0.001))
assertThat(h).isCloseTo(279.4, offset(0.001))
}
@Test fun `landscape orientation is preserved`() {
val (w, h) = constrainToMaxFormat(400.0, 250.0)
assertThat(w).isGreaterThan(h) assertThat(w).isGreaterThan(h)
} }
@Test fun `landscape smaller than A4 is unchanged`() { @Test fun `landscape smaller than A4 is unchanged`() {
val (w, h) = clipToMaxFormat(297.0, 150.0) val (w, h) = constrainToMaxFormat(297.0, 150.0)
assertThat(w).isCloseTo(297.0, offset(0.001)) assertThat(w).isCloseTo(297.0, offset(0.001))
assertThat(h).isCloseTo(150.0, offset(0.001)) assertThat(h).isCloseTo(150.0, offset(0.001))
} }