Skip to content

Commit

Permalink
lists
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Jan 3, 2024
1 parent 2f2d3fa commit 6f409c4
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 76 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

# MineMark
Java Markdown Rendering library
</div>
</div>

# TODO
- [ ] Code blocks
- [ ] Blockquotes
- [ ] Tables
- [ ] \<summary> tags
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "dev.dediamondpro"
version = "1.0-SNAPSHOT49"
version = "1.0-SNAPSHOT59"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import java.awt.Color

class LayoutConfigImpl(
var fontProvider: FontProvider = DefaultFonts.VANILLA_FONT_RENDERER,
alignment: Alignment = Alignment.LEFT, fontSize: Float = 1f, textColor: Color = Color.WHITE,
bold: Boolean = false, italic: Boolean = false, underlined: Boolean = false, strikethrough: Boolean = false, partOfLink: Boolean = false,
paddingConfig: PaddingConfig = PaddingConfig(4f, 4f),
alignment: Alignment = Alignment.LEFT,
fontSize: Float = 1f,
textColor: Color = Color.WHITE,
bold: Boolean = false,
italic: Boolean = false,
underlined: Boolean = false,
strikethrough: Boolean = false,
partOfLink: Boolean = false,
spacingConfig: SpacingConfig = SpacingConfig(4f, 4f, 4f, 16f),
headingConfig: HeadingConfig = HeadingConfig(2f, 1.66f, 1.33f, 1f, 1.2f, 1f),
imageProvider: ImageProvider = DefaultImageProvider.INSTANCE, browserProvider: BrowserProvider = ElementaBrowserProvider
imageProvider: ImageProvider = DefaultImageProvider.INSTANCE,
browserProvider: BrowserProvider = ElementaBrowserProvider
) : LayoutConfig(
alignment,
fontSize,
Expand All @@ -25,7 +32,7 @@ class LayoutConfigImpl(
underlined,
strikethrough,
partOfLink,
paddingConfig,
spacingConfig,
headingConfig,
imageProvider,
browserProvider
Expand All @@ -41,7 +48,7 @@ class LayoutConfigImpl(
isUnderlined,
isStrikethrough,
isPartOfLink,
paddingConfig,
spacingConfig,
headingConfig,
imageProvider,
browserProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class MineMarkComponent(
.addElement(Elements.HEADING, ::MarkdownHeadingComponent)
.addElement(Elements.IMAGE, ::MarkdownImageComponent)
.addElement(Elements.HORIZONTAL_LINE, ::MarkdownHorizontalLineComponent)
.addElement(Elements.LIST_ELEMENT, ::MarkdownListElementComponent)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MarkdownHorizontalLineComponent(
renderData.matrixStack,
Color.WHITE,
x.toDouble(),
y.toDouble(),
(y + 2f).toDouble(),
position!!.line.width.toDouble(),
2.0
)
Expand All @@ -30,6 +30,6 @@ class MarkdownHorizontalLineComponent(
}

override fun getHeight(layoutData: LayoutData): Float {
return 2f
return 6f
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.LayoutData
import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elements.BasicElement
import dev.dediamondpro.minemark.elements.Element
import dev.dediamondpro.minemark.elements.impl.list.ListElement
import dev.dediamondpro.minemark.elements.impl.list.ListHolderElement
import gg.essential.elementa.components.UIBlock
import org.xml.sax.Attributes
import java.awt.Color

class MarkdownListElementComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
qName: String, attributes: Attributes?
) : ListElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
private val fontProvider = layoutConfig.fontProvider
private var marker: String = when (listType) {
ListHolderElement.ListType.ORDERED -> "${elementIndex + 1}. "
ListHolderElement.ListType.UNORDERED -> ""
else -> ""
}

override fun drawMarker(x: Float, y: Float, renderData: RenderData) {
val scale = layoutConfig.fontSize
renderData.matrixStack.push()
renderData.matrixStack.scale(scale, scale, 1f)
fontProvider.drawString(
renderData.matrixStack,
marker,
layoutConfig.textColor,
x / scale, y / scale,
1f, 1f
)
renderData.matrixStack.pop()
}

override fun getMarkerWidth(): Float {
return fontProvider.getStringWidth(marker, 1f) * layoutConfig.fontSize
}

override fun getMarkerHeight(): Float {
return fontProvider.getStringHeight(marker, 1f) * layoutConfig.fontSize
}

override fun generateLayout(layoutData: LayoutData?) {
super.generateLayout(layoutData)
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package dev.dediamondpro.minemark.elementa.elements

import dev.dediamondpro.minemark.LayoutData
import dev.dediamondpro.minemark.elementa.LayoutConfigImpl
import dev.dediamondpro.minemark.elementa.RenderData
import dev.dediamondpro.minemark.elements.Element
import dev.dediamondpro.minemark.elements.impl.text.TextElement
import gg.essential.universal.UResolution
import org.xml.sax.Attributes
import kotlin.math.floor
import kotlin.math.round

class MarkdownTextComponent(
layoutConfig: LayoutConfigImpl,
parent: Element<LayoutConfigImpl, RenderData>?,
qName: String, attributes: Attributes?
) : TextElement<LayoutConfigImpl, RenderData>(layoutConfig, parent, qName, attributes) {
private val font = layoutConfig.fontProvider
private val scale = layoutConfig.fontSize
private var scale = layoutConfig.fontSize
private var prefix = buildString {
if (layoutConfig.isBold) append("§l")
if (layoutConfig.isItalic) append("§o")
if (layoutConfig.isUnderlined) append("§n")
if (layoutConfig.isStrikethrough) append("§m")
}

override fun generateLayout(layoutData: LayoutData?) {
val mcScale = UResolution.scaleFactor.toFloat()
scale = round(layoutConfig.fontSize * mcScale) / mcScale
super.generateLayout(layoutData)
}

override fun drawText(text: String, x: Float, bottomY: Float, hovered: Boolean, renderData: RenderData) {
prefix = buildString {
if (layoutConfig.isBold) append("§l")
Expand Down
36 changes: 24 additions & 12 deletions src/main/java/dev/dediamondpro/minemark/LayoutConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,35 @@ public class LayoutConfig {
private boolean underlined;
private boolean strikethrough;
private boolean partOfLink;
private final PaddingConfig paddingConfig;
private final SpacingConfig spacingConfig;
private final HeadingConfig headingConfig;
private final ImageProvider imageProvider;
private final BrowserProvider browserProvider;

public LayoutConfig(Alignment alignment, float fontSize, Color textColor, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean partOfLink, PaddingConfig paddingConfig, HeadingConfig headingConfig, ImageProvider imageProvider, BrowserProvider browserProvider) {
public LayoutConfig(Alignment alignment, float fontSize, Color textColor, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean partOfLink, SpacingConfig spacingConfig, HeadingConfig headingConfig, ImageProvider imageProvider, BrowserProvider browserProvider) {
this.alignment = alignment;
this.fontSize = fontSize;
this.textColor = textColor;
this.bold = bold;
this.italic = italic;
this.underlined = underlined;
this.strikethrough = strikethrough;
this.paddingConfig = paddingConfig;
this.spacingConfig = spacingConfig;
this.headingConfig = headingConfig;
this.imageProvider = imageProvider;
this.browserProvider = browserProvider;
}

public LayoutConfig(float defaultFontSize, PaddingConfig paddingConfig, HeadingConfig headingConfig, ImageProvider imageProvider, BrowserProvider browserProvider) {
this(Alignment.LEFT, defaultFontSize, Color.WHITE, false, false, false, false, false, paddingConfig, headingConfig, imageProvider, browserProvider);
public LayoutConfig(float defaultFontSize, SpacingConfig spacingConfig, HeadingConfig headingConfig, ImageProvider imageProvider, BrowserProvider browserProvider) {
this(Alignment.LEFT, defaultFontSize, Color.WHITE, false, false, false, false, false, spacingConfig, headingConfig, imageProvider, browserProvider);
}

public LayoutConfig(float defaultFontSize, PaddingConfig paddingConfig, HeadingConfig headingConfig) {
this(defaultFontSize, paddingConfig, headingConfig, DefaultImageProvider.INSTANCE, DefaultBrowserProvider.INSTANCE);
public LayoutConfig(float defaultFontSize, SpacingConfig spacingConfig, HeadingConfig headingConfig) {
this(defaultFontSize, spacingConfig, headingConfig, DefaultImageProvider.INSTANCE, DefaultBrowserProvider.INSTANCE);
}

public LayoutConfig clone() {
return new LayoutConfig(alignment, fontSize, new Color(textColor.getRGB()), bold, italic, underlined, strikethrough, partOfLink, paddingConfig, headingConfig, imageProvider, browserProvider);
return new LayoutConfig(alignment, fontSize, new Color(textColor.getRGB()), bold, italic, underlined, strikethrough, partOfLink, spacingConfig, headingConfig, imageProvider, browserProvider);
}

public Alignment getAlignment() {
Expand Down Expand Up @@ -115,8 +115,8 @@ public void setPartOfLink(boolean partOfLink) {
this.partOfLink = partOfLink;
}

public PaddingConfig getPaddingConfig() {
return paddingConfig;
public SpacingConfig getSpacingConfig() {
return spacingConfig;
}

public HeadingConfig getHeadingConfig() {
Expand All @@ -135,13 +135,17 @@ public enum Alignment {
CENTER, LEFT, RIGHT
}

public static class PaddingConfig {
public static class SpacingConfig {
private final float textSpacing;
private final float paragraphPadding;
private final float listPadding;
private final float listIndentSpacing;

public PaddingConfig(float textSpacing, float paragraphPadding) {
public SpacingConfig(float textSpacing, float paragraphPadding, float listPadding, float listIndentSpacing) {
this.textSpacing = textSpacing;
this.paragraphPadding = paragraphPadding;
this.listPadding = listPadding;
this.listIndentSpacing = listIndentSpacing;
}

public float getTextSpacing() {
Expand All @@ -151,6 +155,14 @@ public float getTextSpacing() {
public float getParagraphPadding() {
return paragraphPadding;
}

public float getListPadding() {
return listPadding;
}

public float getListIndentSpacing() {
return listIndentSpacing;
}
}

public static class HeadingConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@ protected void generateLayout(LayoutData layoutData) {
if (!(this instanceof Inline) && !layoutData.isLineEmpty()) {
layoutData.nextLine();
}
float padding = getPadding(layoutData);
if (padding != 0 && !(parent instanceof NoPadding)) {
layoutData.setLineHeight(padding);
layoutData.nextLine();
}
for (Element<L, R> child : children) {
child.generateLayout(layoutData);
}
if (!(this instanceof Inline) && !layoutData.isLineEmpty()) {
layoutData.nextLine();
}
if (padding != 0 && !(parent instanceof NoPadding)) {
layoutData.setLineHeight(padding);
layoutData.nextLine();
}
}

protected float getPadding(LayoutData layoutData) {
return 0f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.ArrayList;

public abstract class Element<L extends LayoutConfig, R> {
private final @Nullable Element<L, R> parent;
protected final @Nullable Element<L, R> parent;
protected final ArrayList<Element<L, R>> children = new ArrayList<>();
protected final String qName;
protected final Attributes attributes;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.dediamondpro.minemark.elements;


/**
* Interface to tell elements like {@link dev.dediamondpro.minemark.elements.impl.ParagraphElement} to not add any padding.
* Useful for elements like {@link dev.dediamondpro.minemark.elements.impl.list.ListElement}
*/
public interface NoPadding {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
public class LinkElement<L extends LayoutConfig, R> extends ChildBasedElement<L, R> implements Inline {
protected final ArrayList<LayoutData.MarkDownElementPosition> positions = new ArrayList<>();
protected final String link;
protected boolean wasUnderlined;
protected boolean hovered;

public LinkElement(@NotNull L layoutConfig, @Nullable Element<L, R> parent, @NotNull String qName, @Nullable Attributes attributes) {
super(layoutConfig, parent, qName, attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dev.dediamondpro.minemark.LayoutData;
import dev.dediamondpro.minemark.elements.ChildBasedElement;
import dev.dediamondpro.minemark.elements.Element;
import dev.dediamondpro.minemark.elements.Inline;
import dev.dediamondpro.minemark.elements.NoPadding;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.xml.sax.Attributes;
Expand All @@ -15,14 +15,7 @@ public ParagraphElement(@NotNull L layoutConfig, @Nullable Element<L, R> parent,
}

@Override
protected void generateLayout(LayoutData layoutData) {
if (!layoutData.isLineEmpty()) {
layoutData.nextLine();
}
layoutData.setLineHeight(layoutConfig.getPaddingConfig().getParagraphPadding());
layoutData.nextLine();
super.generateLayout(layoutData);
layoutData.setLineHeight(layoutConfig.getPaddingConfig().getParagraphPadding());
layoutData.nextLine();
protected float getPadding(LayoutData layoutData) {
return layoutConfig.getSpacingConfig().getParagraphPadding();
}
}
Loading

0 comments on commit 6f409c4

Please sign in to comment.