EDIT in bold
I have a table where several cells contain Paragraph
s with some arbitrarily long text. The table width is determined via useAllAvailableWidth
, and I am also invoking setAutoLayout
.
I am using the Renderer
framework to set the Paragraph
's font size to the largest possible value without clipping off any content.
In particular I wish to achieve similar results to iText Maximum Font Size, however that questions was written for itext5. I am using itext7.
I have read this sample, and I have come up with the following (partial) solution thanks to a previous answer:
class FontSizeRenderer(val content: Paragraph) : ParagraphRenderer(content) {
override fun getNextRenderer() = FontSizeRenderer(content)
override fun layout(layoutContext: LayoutContext?): LayoutResult {
val currentFontSize = content.getProperty<UnitValue>(Property.FONT_SIZE).value
return layoutBinarySearch(layoutContext, 1f, currentFontSize, 20)
}
private tailrec fun layoutBinarySearch(layoutContext: LayoutContext?, minFontSize: Float, maxFontSize: Float, iterationThreshold: Int): LayoutResult {
val currentLayout = super.layout(layoutContext)
if (iterationThreshold <= 0) {
return currentLayout
}
val currentFontSize = content.getProperty<UnitValue>(Property.FONT_SIZE).value
return if (currentLayout.status == LayoutResult.FULL) {
val increment = (currentFontSize + maxFontSize) / 2
content.setFontSize(increment)
layoutBinarySearch(layoutContext, currentFontSize, maxFontSize, iterationThreshold - 1)
} else {
val decrement = (minFontSize + currentFontSize) / 2
content.setFontSize(decrement)
layoutBinarySearch(layoutContext, minFontSize, currentFontSize, iterationThreshold - 1)
}
}
}
When using this renderer in a fully-fledged table, it "kinda works" in a sense that it starts to recurse but it stops too early.
The expected output string in the bottom cell on the first page is Scramble: R' U' F R F2 D2 R' B2 U2 R F2 R' B2 R' B F U' L2 B' R' B' U' R F2 R' U' F
. The full code sample can be inspected (and downloaded) at this repository, under webscrambles/src/main/kotlin/org/worldcubeassociation/tnoodle/server/webscrambles/pdf
, files FmcSolutionSheet.kt
and util/FooRenderer.kt
.
How can I adjust my Renderer
to prevent the Cell from overflowing?