Skip to content

Commit 2d5e3c9

Browse files
committed
make images in markdown use linear scaling
1 parent b2f9d98 commit 2d5e3c9

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* This file is part of Resourcify
3+
* Copyright (C) 2025 DeDiamondPro
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License Version 3 as published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package dev.dediamondpro.resourcify.elements.markdown
19+
20+
import dev.dediamondpro.minemark.LayoutStyle
21+
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
22+
import dev.dediamondpro.minemark.elementa.util.EmptyImage
23+
import dev.dediamondpro.minemark.elements.Element
24+
import dev.dediamondpro.minemark.elements.impl.ImageElement
25+
import gg.essential.elementa.components.UIImage
26+
import gg.essential.universal.UMatrixStack
27+
import org.xml.sax.Attributes
28+
import java.awt.Color
29+
import java.awt.image.BufferedImage
30+
import java.util.concurrent.CompletableFuture
31+
32+
33+
// Custom markdown image element so we can draw images using linear interpolation instead of nearest
34+
// TODO: this should be merged into MineMark at some point
35+
class ResourcifyMarkdownImageElement(
36+
style: MarkdownStyle,
37+
layoutStyle: LayoutStyle,
38+
parent: Element<MarkdownStyle, UMatrixStack>?,
39+
qName: String, attributes: Attributes?
40+
) : ImageElement<MarkdownStyle, UMatrixStack, BufferedImage>(style, layoutStyle, parent, qName, attributes) {
41+
private var uiImage: UIImage? = null
42+
43+
override fun drawImage(
44+
image: BufferedImage,
45+
x: Float,
46+
y: Float,
47+
width: Float,
48+
height: Float,
49+
matrixStack: UMatrixStack
50+
) {
51+
if (uiImage == null) {
52+
uiImage = UIImage(CompletableFuture.supplyAsync { image }, EmptyImage)
53+
uiImage?.textureMinFilter = UIImage.TextureScalingMode.LINEAR_MIPMAP_LINEAR
54+
uiImage?.textureMagFilter = UIImage.TextureScalingMode.LINEAR
55+
}
56+
uiImage?.drawImage(
57+
matrixStack,
58+
x.toDouble(),
59+
y.toDouble(),
60+
width.toDouble(),
61+
height.toDouble(),
62+
Color.WHITE
63+
)
64+
}
65+
}

src/main/kotlin/dev/dediamondpro/resourcify/util/ElementaUtils.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ package dev.dediamondpro.resourcify.util
1919

2020
import dev.dediamondpro.minemark.MineMarkCore
2121
import dev.dediamondpro.minemark.elementa.MineMarkComponent
22-
import dev.dediamondpro.minemark.elementa.addElementaExtensions
22+
import dev.dediamondpro.minemark.elementa.elements.MarkdownBlockquoteComponent
23+
import dev.dediamondpro.minemark.elementa.elements.MarkdownCodeBlockComponent
24+
import dev.dediamondpro.minemark.elementa.elements.MarkdownHeadingComponent
25+
import dev.dediamondpro.minemark.elementa.elements.MarkdownHorizontalRuleComponent
26+
import dev.dediamondpro.minemark.elementa.elements.MarkdownListElementComponent
27+
import dev.dediamondpro.minemark.elementa.elements.MarkdownTableCellComponent
28+
import dev.dediamondpro.minemark.elementa.elements.MarkdownTextComponent
2329
import dev.dediamondpro.minemark.elementa.style.MarkdownStyle
24-
import dev.dediamondpro.minemark.style.ImageStyleConfig
25-
import dev.dediamondpro.minemark.style.LinkStyleConfig
30+
import dev.dediamondpro.minemark.elements.Elements
2631
import dev.dediamondpro.resourcify.elements.McImage
2732
import dev.dediamondpro.resourcify.elements.markdown.ExpandableMarkdownElement
33+
import dev.dediamondpro.resourcify.elements.markdown.ResourcifyMarkdownImageElement
2834
import dev.dediamondpro.resourcify.elements.markdown.SummaryElement
2935
import dev.dediamondpro.resourcify.gui.data.Colors
3036
import dev.dediamondpro.resourcify.gui.data.Icons
@@ -126,7 +132,16 @@ object ElementaUtils {
126132
MineMarkCore.builder<MarkdownStyle, UMatrixStack>()
127133
.addExtension(StrikethroughExtension.create())
128134
.addExtension(TablesExtension.create())
129-
.addElementaExtensions()
135+
//.addElementaExtensions()
136+
.setTextElement(::MarkdownTextComponent)
137+
.addElement(Elements.HEADING, ::MarkdownHeadingComponent)
138+
.addElement(Elements.IMAGE, ::ResourcifyMarkdownImageElement) // Custom element
139+
.addElement(Elements.HORIZONTAL_RULE, ::MarkdownHorizontalRuleComponent)
140+
.addElement(Elements.LIST_ELEMENT, ::MarkdownListElementComponent)
141+
.addElement(Elements.BLOCKQUOTE, ::MarkdownBlockquoteComponent)
142+
.addElement(Elements.CODE_BLOCK, ::MarkdownCodeBlockComponent)
143+
.addElement(Elements.TABLE_CELL, ::MarkdownTableCellComponent)
144+
// Other elements
130145
.addElement(ExpandableMarkdownElement.ExpandableElementCreator)
131146
.addElement(listOf("summary"), ::SummaryElement)
132147
.build()

0 commit comments

Comments
 (0)