From 4f953e8a31a0e630123ef49765c6284a3e199ebf Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Tue, 2 May 2023 17:49:18 +0200 Subject: [PATCH 1/2] Fix images not moving text after it down appropriately --- .../markdown/drawables/ParagraphDrawable.kt | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/gg/essential/elementa/markdown/drawables/ParagraphDrawable.kt b/src/main/kotlin/gg/essential/elementa/markdown/drawables/ParagraphDrawable.kt index fe40b5f2..3463bc1d 100644 --- a/src/main/kotlin/gg/essential/elementa/markdown/drawables/ParagraphDrawable.kt +++ b/src/main/kotlin/gg/essential/elementa/markdown/drawables/ParagraphDrawable.kt @@ -80,13 +80,9 @@ class ParagraphDrawable( val currentLine = mutableListOf() var maxLineHeight = Float.MIN_VALUE - var prevY = y - fun gotoNextLine() { - prevY = currY - currX = x - currY += maxLineHeight * scaleModifier + config.paragraphConfig.spaceBetweenLines + currY += maxLineHeight + config.paragraphConfig.spaceBetweenLines if (maxLineHeight > 9f) { for (drawable in currentLine) @@ -112,9 +108,9 @@ class ParagraphDrawable( drawable.layout(currX, currY, newWidth).also { if (it.height > maxLineHeight) maxLineHeight = it.height + widthRemaining -= it.width + currX += it.width } - widthRemaining -= newWidth - currX += newWidth trimNextText = false currentLine.add(drawable) newDrawables.add(drawable) @@ -156,7 +152,7 @@ class ParagraphDrawable( } if (text is ImageDrawable) { - gotoNextLine() + if (currentLine.isNotEmpty()) gotoNextLine() layout(text, width) gotoNextLine() continue @@ -238,8 +234,13 @@ class ParagraphDrawable( // We can have extra drawables in the current line that didn't get handled // by the last iteration of the loop - if (currentLine.isNotEmpty()) + if (currentLine.isNotEmpty()) { lines.add(currentLine.toList()) + currY += maxLineHeight + } else { + // There isn't a next line, so this space shouldn't be there + currY -= config.paragraphConfig.spaceBetweenLines + } if (centered) { // Offset each text component by half of the space at the end of each line @@ -265,8 +266,7 @@ class ParagraphDrawable( drawables.setDrawables(newDrawables) - val height = (if (currentLine.isNotEmpty()) currY else prevY) - y + 9f * scaleModifier + - if (insertSpaceAfter) config.paragraphConfig.spaceAfter else 0f + val height = currY - y + if (insertSpaceAfter) config.paragraphConfig.spaceAfter else 0f return Layout( x, @@ -387,7 +387,8 @@ class ParagraphDrawable( // Step 5: Get the string offset position in the current text - fun textWidth(offset: Int) = currentDrawable.formattedText.substring(0, offset).width(currentDrawable.scaleModifier) + fun textWidth(offset: Int) = + currentDrawable.formattedText.substring(0, offset).width(currentDrawable.scaleModifier) var offset = currentDrawable.style.numFormattingChars var cachedWidth = 0f From 2a7cfafbb4ca602e9daed6923184a044b351e1a0 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Tue, 2 May 2023 18:11:42 +0200 Subject: [PATCH 2/2] Make images able to draw side by side with other images or text --- .../gg/essential/elementa/markdown/drawables/ImageDrawable.kt | 2 ++ .../essential/elementa/markdown/drawables/ParagraphDrawable.kt | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/gg/essential/elementa/markdown/drawables/ImageDrawable.kt b/src/main/kotlin/gg/essential/elementa/markdown/drawables/ImageDrawable.kt index dcc4167c..b21e0b0b 100644 --- a/src/main/kotlin/gg/essential/elementa/markdown/drawables/ImageDrawable.kt +++ b/src/main/kotlin/gg/essential/elementa/markdown/drawables/ImageDrawable.kt @@ -83,6 +83,8 @@ class ImageDrawable(md: MarkdownComponent, val url: URL, private val fallback: D // TODO: Rename this function? override fun hasSelectedText() = selected + fun getImageWidth(): Float = image.imageWidth + private inner class ShiftableMDPixelConstraint(val base: Float, var shift: Float) : XConstraint, YConstraint { override var cachedValue = 0f override var recalculate = true diff --git a/src/main/kotlin/gg/essential/elementa/markdown/drawables/ParagraphDrawable.kt b/src/main/kotlin/gg/essential/elementa/markdown/drawables/ParagraphDrawable.kt index 3463bc1d..21f0ec32 100644 --- a/src/main/kotlin/gg/essential/elementa/markdown/drawables/ParagraphDrawable.kt +++ b/src/main/kotlin/gg/essential/elementa/markdown/drawables/ParagraphDrawable.kt @@ -152,9 +152,8 @@ class ParagraphDrawable( } if (text is ImageDrawable) { - if (currentLine.isNotEmpty()) gotoNextLine() + if (widthRemaining - text.getImageWidth() <= 0) gotoNextLine() layout(text, width) - gotoNextLine() continue }