diff --git a/src/commonMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt b/src/commonMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt index abfb76f..5f18549 100644 --- a/src/commonMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt +++ b/src/commonMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt @@ -28,7 +28,7 @@ expect class PdfDocument private constructor() { suspend fun getTextWidth(text: String, font: PdfFont, size: Float): Float /** Draw and stroke the given [text]. */ - fun drawText(text: String) + suspend fun drawText(text: String) /** Set the line width. */ fun setLineWidth(width: Float) diff --git a/src/commonTest/kotlin/com/jeffpdavidson/kotwords/formats/PdfTest.kt b/src/commonTest/kotlin/com/jeffpdavidson/kotwords/formats/PdfTest.kt index 5c8d167..3e29f24 100644 --- a/src/commonTest/kotlin/com/jeffpdavidson/kotwords/formats/PdfTest.kt +++ b/src/commonTest/kotlin/com/jeffpdavidson/kotwords/formats/PdfTest.kt @@ -34,6 +34,15 @@ class PdfTest { ) } + @Test + fun asPdf_html() = runTest { + ImageComparator.assertPdfEquals( + readBinaryResource(ImageComparator::class, "pdf/test-html.pdf"), + JpzFile(readBinaryResource(PdfTest::class, "jpz/test-html.jpz")) + .asPuzzle().asPdf(blackSquareLightnessAdjustment = 0.75f, fontFamily = getNotoSerifFontFamily()) + ) + } + // Note for splitTextToLines tests: a 100 pt line fits 16 10pt Courier characters. @Test diff --git a/src/commonTest/resources/jpz/test-html.jpz b/src/commonTest/resources/jpz/test-html.jpz new file mode 100644 index 0000000..7928c98 --- /dev/null +++ b/src/commonTest/resources/jpz/test-html.jpz @@ -0,0 +1,119 @@ + + + + + Example Puzzle for Kotwords + Jeff Davidson + © 2018 Jeff Davidson + Notepad text goes here. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <b>Across</b> + First across clue with bold, italic, and bold italic text + Second across clue with subscript and superscript + Third across clue with italic subscript and more + Fourth across clue + Fifth across clue + + + <b>Down</b> + First down clue + Second down clue + Third down clue + Fourth down clue + Fifth down clue + + + + diff --git a/src/jsMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt b/src/jsMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt index 79bdd54..260f6fd 100644 --- a/src/jsMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt +++ b/src/jsMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt @@ -26,12 +26,15 @@ actual class PdfDocument { private lateinit var page: PDFPage private var currentFont: PDFFont? = null + private var fontSize: Float = 0f private val loadedFonts = mutableMapOf() private var lineWidth: Float = 1f private var strokeColor: RGB = rgb(0f, 0f, 0f) private var fillColor: RGB = rgb(1f, 1f, 1f) + private var xOffset: Float = 0f + private suspend fun init() { pdf = PDFDocument.create().await() pdf.registerFontkit(FontkitModule.default) @@ -42,18 +45,21 @@ actual class PdfDocument { actual val height: Float get() = page.getHeight() actual fun beginText() { + xOffset = 0f page.moveTo(0f, 0f) } actual fun endText() {} actual fun newLineAtOffset(offsetX: Float, offsetY: Float) { - page.moveRight(offsetX) + page.moveRight(offsetX - xOffset) + xOffset = 0f page.moveUp(offsetY) } actual suspend fun setFont(font: PdfFont, size: Float) { setFont(font) + fontSize = size page.setFontSize(size) } @@ -91,8 +97,11 @@ actual class PdfDocument { return loadFont(font).widthOfTextAtSize(text, size) } - actual fun drawText(text: String) { + actual suspend fun drawText(text: String) { page.drawText(text) + val width = currentFont!!.widthOfTextAtSize(text, fontSize) + xOffset += width + page.moveRight(width) } actual fun setLineWidth(width: Float) { diff --git a/src/jsTest/resources/pdf/test-html.pdf b/src/jsTest/resources/pdf/test-html.pdf new file mode 100644 index 0000000..6abcc99 Binary files /dev/null and b/src/jsTest/resources/pdf/test-html.pdf differ diff --git a/src/jvmMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt b/src/jvmMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt index c4f04f8..5f091f2 100644 --- a/src/jvmMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt +++ b/src/jvmMain/kotlin/com/jeffpdavidson/kotwords/formats/PdfDocument.kt @@ -48,7 +48,7 @@ actual class PdfDocument { return font.toPdfFont().getStringWidth(text) * size / 1000 } - actual fun drawText(text: String) { + actual suspend fun drawText(text: String) { content.showText(text) } diff --git a/src/jvmTest/resources/pdf/test-html.pdf b/src/jvmTest/resources/pdf/test-html.pdf new file mode 100644 index 0000000..d992e5d Binary files /dev/null and b/src/jvmTest/resources/pdf/test-html.pdf differ