Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown: make images able to draw side by side with other images or text #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,9 @@ class ParagraphDrawable(
val currentLine = mutableListOf<Drawable>()
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)
Expand All @@ -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)
Expand Down Expand Up @@ -156,9 +152,8 @@ class ParagraphDrawable(
}

if (text is ImageDrawable) {
gotoNextLine()
if (widthRemaining - text.getImageWidth() <= 0) gotoNextLine()
layout(text, width)
gotoNextLine()
continue
}

Expand Down Expand Up @@ -238,8 +233,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
Expand All @@ -265,8 +265,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,
Expand Down Expand Up @@ -387,7 +386,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
Expand Down