diff --git a/build.gradle.kts b/build.gradle.kts index 82c6d75..5f89a3f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "dev.dediamondpro" -version = "1.0-SNAPSHOT76" +version = "1.0-SNAPSHOT77" repositories { mavenCentral() diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/LayoutConfigImpl.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/LayoutConfigImpl.kt deleted file mode 100644 index 2abecaa..0000000 --- a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/LayoutConfigImpl.kt +++ /dev/null @@ -1,60 +0,0 @@ -package dev.dediamondpro.minemark.elementa - -import dev.dediamondpro.minemark.LayoutConfig -import dev.dediamondpro.minemark.elementa.util.ElementaBrowserProvider -import dev.dediamondpro.minemark.providers.BrowserProvider -import dev.dediamondpro.minemark.providers.DefaultImageProvider -import dev.dediamondpro.minemark.providers.ImageProvider -import gg.essential.elementa.font.DefaultFonts -import gg.essential.elementa.font.FontProvider -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, - preFormatted: Boolean = false, - spacingConfig: SpacingConfig = SpacingConfig(2f, 6f, 16f), - headingConfig: HeadingConfig = HeadingConfig(2f, 1.66f, 1.33f, 1f, 1.2f, 1f), - imageProvider: ImageProvider = DefaultImageProvider.INSTANCE, - browserProvider: BrowserProvider = ElementaBrowserProvider -) : LayoutConfig( - alignment, - fontSize, - textColor, - bold, - italic, - underlined, - strikethrough, - partOfLink, - preFormatted, - spacingConfig, - headingConfig, - imageProvider, - browserProvider -) { - override fun clone(): LayoutConfigImpl { - return LayoutConfigImpl( - fontProvider, - alignment, - fontSize, - Color(textColor.rgb), - isBold, - isItalic, - isUnderlined, - isStrikethrough, - isPartOfLink, - isPreFormatted, - spacingConfig, - headingConfig, - imageProvider, - browserProvider - ) - } -} diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/MineMarkComponent.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/MineMarkComponent.kt index dcb3b6b..eaeb6c3 100644 --- a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/MineMarkComponent.kt +++ b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/MineMarkComponent.kt @@ -3,6 +3,7 @@ package dev.dediamondpro.minemark.elementa import dev.dediamondpro.minemark.MineMarkCore import dev.dediamondpro.minemark.MineMarkCoreBuilder import dev.dediamondpro.minemark.elementa.elements.* +import dev.dediamondpro.minemark.elementa.style.MarkdownStyle import dev.dediamondpro.minemark.elements.Elements import dev.dediamondpro.minemark.utils.MouseButton import gg.essential.elementa.UIComponent @@ -12,11 +13,11 @@ import gg.essential.universal.UMatrixStack class MineMarkComponent( markdown: String, - layoutConfig: LayoutConfigImpl = LayoutConfigImpl(), - core: MineMarkCore = defaultCore + style: MarkdownStyle = MarkdownStyle(), + core: MineMarkCore = defaultCore ) : UIComponent() { - private val parsedMarkdown = core.parse(layoutConfig, markdown).apply { + private val parsedMarkdown = core.parse(style, markdown).apply { addLayoutCallback(this@MineMarkComponent::layoutCallback) } @@ -70,11 +71,11 @@ class MineMarkComponent( companion object { private val defaultCore = MineMarkCore - .builder() + .builder() .addElementaExtensions() .build() - fun MineMarkCoreBuilder.addElementaExtensions(): MineMarkCoreBuilder { + fun MineMarkCoreBuilder.addElementaExtensions(): MineMarkCoreBuilder { return this.addElement(Elements.TEXT, ::MarkdownTextComponent) .addElement(Elements.HEADING, ::MarkdownHeadingComponent) .addElement(Elements.IMAGE, ::MarkdownImageComponent) diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownBlockquoteComponent.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownBlockquoteComponent.kt index d5aa2f7..54b77f7 100644 --- a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownBlockquoteComponent.kt +++ b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownBlockquoteComponent.kt @@ -1,8 +1,9 @@ package dev.dediamondpro.minemark.elementa.elements import dev.dediamondpro.minemark.LayoutData -import dev.dediamondpro.minemark.elementa.LayoutConfigImpl +import dev.dediamondpro.minemark.LayoutStyle import dev.dediamondpro.minemark.elementa.RenderData +import dev.dediamondpro.minemark.elementa.style.MarkdownStyle import dev.dediamondpro.minemark.elements.ChildMovingElement import dev.dediamondpro.minemark.elements.Element import gg.essential.elementa.components.UIBlock @@ -10,10 +11,11 @@ import org.xml.sax.Attributes import java.awt.Color class MarkdownBlockquoteComponent( - layoutConfig: LayoutConfigImpl, - parent: Element?, + style: MarkdownStyle, + layoutStyle: LayoutStyle, + parent: Element?, qName: String, attributes: Attributes? -) : ChildMovingElement(layoutConfig, parent, qName, attributes) { +) : ChildMovingElement(style, layoutStyle, parent, qName, attributes) { override fun drawMarker(x: Float, y: Float, height: Float, renderData: RenderData) { UIBlock.drawBlockSized( renderData.matrixStack, @@ -28,6 +30,7 @@ class MarkdownBlockquoteComponent( } override fun getPadding(layoutData: LayoutData?): Float { - return layoutConfig.spacingConfig.blockQuotePadding + // TODO: give blockquote own styling + return style.getParagraphStyle().padding } } \ No newline at end of file diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownHeadingComponent.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownHeadingComponent.kt index dc57166..2a8014b 100644 --- a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownHeadingComponent.kt +++ b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownHeadingComponent.kt @@ -1,8 +1,9 @@ package dev.dediamondpro.minemark.elementa.elements import dev.dediamondpro.minemark.LayoutData -import dev.dediamondpro.minemark.elementa.LayoutConfigImpl +import dev.dediamondpro.minemark.LayoutStyle import dev.dediamondpro.minemark.elementa.RenderData +import dev.dediamondpro.minemark.elementa.style.MarkdownStyle import dev.dediamondpro.minemark.elements.Element import dev.dediamondpro.minemark.elements.impl.formatting.HeadingElement import gg.essential.elementa.components.UIBlock @@ -10,10 +11,11 @@ import org.xml.sax.Attributes import java.awt.Color class MarkdownHeadingComponent( - layoutConfig: LayoutConfigImpl, - parent: Element?, + style: MarkdownStyle, + layoutStyle: LayoutStyle, + parent: Element?, qName: String, attributes: Attributes? -) : HeadingElement(layoutConfig, parent, qName, attributes) { +) : HeadingElement(style, layoutStyle, parent, qName, attributes) { private var position: LayoutData.MarkDownLine? = null override fun generateLayout(layoutData: LayoutData) { diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownHorizontalLineComponent.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownHorizontalLineComponent.kt index bd3acf1..4f93657 100644 --- a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownHorizontalLineComponent.kt +++ b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownHorizontalLineComponent.kt @@ -1,8 +1,9 @@ package dev.dediamondpro.minemark.elementa.elements import dev.dediamondpro.minemark.LayoutData -import dev.dediamondpro.minemark.elementa.LayoutConfigImpl +import dev.dediamondpro.minemark.LayoutStyle import dev.dediamondpro.minemark.elementa.RenderData +import dev.dediamondpro.minemark.elementa.style.MarkdownStyle import dev.dediamondpro.minemark.elements.BasicElement import dev.dediamondpro.minemark.elements.Element import gg.essential.elementa.components.UIBlock @@ -10,10 +11,11 @@ import org.xml.sax.Attributes import java.awt.Color class MarkdownHorizontalLineComponent( - layoutConfig: LayoutConfigImpl, - parent: Element?, + style: MarkdownStyle, + layoutStyle: LayoutStyle, + parent: Element?, qName: String, attributes: Attributes? -) : BasicElement(layoutConfig, parent, qName, attributes) { +) : BasicElement(style, layoutStyle, parent, qName, attributes) { override fun drawElement(x: Float, y: Float, renderData: RenderData) { UIBlock.drawBlockSized( renderData.matrixStack, diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownImageComponent.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownImageComponent.kt index 1c19167..ca9be7c 100644 --- a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownImageComponent.kt +++ b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownImageComponent.kt @@ -1,7 +1,8 @@ package dev.dediamondpro.minemark.elementa.elements -import dev.dediamondpro.minemark.elementa.LayoutConfigImpl +import dev.dediamondpro.minemark.LayoutStyle import dev.dediamondpro.minemark.elementa.RenderData +import dev.dediamondpro.minemark.elementa.style.MarkdownStyle import dev.dediamondpro.minemark.elementa.util.EmptyImage import dev.dediamondpro.minemark.elements.Element import dev.dediamondpro.minemark.elements.impl.ImageElement @@ -12,10 +13,11 @@ import java.awt.image.BufferedImage import java.util.concurrent.CompletableFuture class MarkdownImageComponent( - layoutConfig: LayoutConfigImpl, - parent: Element?, + style: MarkdownStyle, + layoutStyle: LayoutStyle, + parent: Element?, qName: String, attributes: Attributes? -) : ImageElement(layoutConfig, parent, qName, attributes) { +) : ImageElement(style, layoutStyle, parent, qName, attributes) { private var uiImage: UIImage? = null override fun drawImage( diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownListElementComponent.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownListElementComponent.kt index e4bf572..9e96920 100644 --- a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownListElementComponent.kt +++ b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownListElementComponent.kt @@ -1,19 +1,21 @@ package dev.dediamondpro.minemark.elementa.elements import dev.dediamondpro.minemark.LayoutData -import dev.dediamondpro.minemark.elementa.LayoutConfigImpl +import dev.dediamondpro.minemark.LayoutStyle import dev.dediamondpro.minemark.elementa.RenderData +import dev.dediamondpro.minemark.elementa.style.MarkdownStyle import dev.dediamondpro.minemark.elements.Element import dev.dediamondpro.minemark.elements.impl.list.ListElement import dev.dediamondpro.minemark.elements.impl.list.ListHolderElement import org.xml.sax.Attributes class MarkdownListElementComponent( - layoutConfig: LayoutConfigImpl, - parent: Element?, + style: MarkdownStyle, + layoutStyle: LayoutStyle, + parent: Element?, qName: String, attributes: Attributes? -) : ListElement(layoutConfig, parent, qName, attributes) { - private val fontProvider = layoutConfig.fontProvider +) : ListElement(style, layoutStyle, parent, qName, attributes) { + private val fontProvider = style.textStyle.font private var markerStr: String = when (listType) { ListHolderElement.ListType.ORDERED -> "${elementIndex + 1}. " ListHolderElement.ListType.UNORDERED -> "● " @@ -21,13 +23,13 @@ class MarkdownListElementComponent( } override fun drawMarker(x: Float, y: Float, renderData: RenderData) { - val scale = layoutConfig.fontSize + val scale = layoutStyle.fontSize renderData.matrixStack.push() renderData.matrixStack.scale(scale, scale, 1f) fontProvider.drawString( renderData.matrixStack, markerStr, - layoutConfig.textColor, + layoutStyle.textColor, x / scale, y / scale, 1f, 1f ) @@ -35,10 +37,10 @@ class MarkdownListElementComponent( } override fun getMarkerWidth(): Float { - return fontProvider.getStringWidth(markerStr, 1f) * layoutConfig.fontSize + return fontProvider.getStringWidth(markerStr, 1f) * layoutStyle.fontSize } override fun getMarkerHeight(layoutData: LayoutData?): Float { - return fontProvider.getStringHeight(markerStr, 1f) * layoutConfig.fontSize + return fontProvider.getStringHeight(markerStr, 1f) * layoutStyle.fontSize } } \ No newline at end of file diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownTextComponent.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownTextComponent.kt index b4f7ad3..00b5313 100644 --- a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownTextComponent.kt +++ b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/elements/MarkdownTextComponent.kt @@ -1,8 +1,9 @@ package dev.dediamondpro.minemark.elementa.elements import dev.dediamondpro.minemark.LayoutData -import dev.dediamondpro.minemark.elementa.LayoutConfigImpl +import dev.dediamondpro.minemark.LayoutStyle import dev.dediamondpro.minemark.elementa.RenderData +import dev.dediamondpro.minemark.elementa.style.MarkdownStyle import dev.dediamondpro.minemark.elements.Element import dev.dediamondpro.minemark.elements.impl.TextElement import gg.essential.universal.UResolution @@ -10,31 +11,32 @@ import org.xml.sax.Attributes import kotlin.math.round class MarkdownTextComponent( - layoutConfig: LayoutConfigImpl, - parent: Element?, + style: MarkdownStyle, + layoutStyle: LayoutStyle, + parent: Element?, qName: String, attributes: Attributes? -) : TextElement(layoutConfig, parent, qName, attributes) { - private val font = layoutConfig.fontProvider - private var scale = layoutConfig.fontSize +) : TextElement(style, layoutStyle, parent, qName, attributes) { + private val font = style.textStyle.font + private var scale = layoutStyle.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") + if (layoutStyle.isBold) append("§l") + if (layoutStyle.isItalic) append("§o") + if (layoutStyle.isUnderlined) append("§n") + if (layoutStyle.isStrikethrough) append("§m") } override fun generateLayout(layoutData: LayoutData?) { val mcScale = UResolution.scaleFactor.toFloat() - scale = round(layoutConfig.fontSize * mcScale) / mcScale + scale = round(layoutStyle.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") - if (layoutConfig.isItalic) append("§o") - if (layoutConfig.isStrikethrough) append("§m") - if (layoutConfig.isUnderlined || layoutConfig.isPartOfLink && hovered) append("§n") + if (layoutStyle.isBold) append("§l") + if (layoutStyle.isItalic) append("§o") + if (layoutStyle.isStrikethrough) append("§m") + if (layoutStyle.isUnderlined || layoutStyle.isPartOfLink && hovered) append("§n") } val y = bottomY - getTextHeight(text) @@ -43,7 +45,7 @@ class MarkdownTextComponent( font.drawString( renderData.matrixStack, prefix + text, - layoutConfig.textColor, + layoutStyle.textColor, x / scale, y / scale, 1f, 1f ) diff --git a/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/style/MarkdownStyle.kt b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/style/MarkdownStyle.kt new file mode 100644 index 0000000..0e7215b --- /dev/null +++ b/elementa/src/main/kotlin/dev/dediamondpro/minemark/elementa/style/MarkdownStyle.kt @@ -0,0 +1,40 @@ +package dev.dediamondpro.minemark.elementa.style + +import dev.dediamondpro.minemark.elementa.util.ElementaBrowserProvider +import dev.dediamondpro.minemark.providers.DefaultImageProvider +import dev.dediamondpro.minemark.style.* +import gg.essential.elementa.font.DefaultFonts +import gg.essential.elementa.font.FontProvider +import java.awt.Color + +data class MarkdownStyle @JvmOverloads constructor( + private val textStyle: MarkdownTextStyle = MarkdownTextStyle( + 1f, Color.WHITE, 2f, DefaultFonts.VANILLA_FONT_RENDERER + ), + private val paragraphStyle: ParagraphStyleConfig = ParagraphStyleConfig(6f), + private val linkStyle: LinkStyleConfig = LinkStyleConfig(Color(65, 105, 225), true, ElementaBrowserProvider), + private val headerStyle: HeaderStyleConfig = HeaderStyleConfig( + HeaderLevelStyleConfig(2f, 12f, true, Color(80, 80, 80), 2f, 5f), + HeaderLevelStyleConfig(1.66f, 10f, true, Color(80, 80, 80), 2f, 5f), + HeaderLevelStyleConfig(1.33f, 8f), + HeaderLevelStyleConfig(1f, 6f), + HeaderLevelStyleConfig(0.7f, 4f), + HeaderLevelStyleConfig(0.7f, 4f) + ), + private val imageStyle: ImageStyleConfig = ImageStyleConfig(DefaultImageProvider.INSTANCE), + private val listStyle: ListStyleConfig = ListStyleConfig(16f, 6f) +) : Style { + override fun getTextStyle(): MarkdownTextStyle = textStyle + override fun getParagraphStyle(): ParagraphStyleConfig = paragraphStyle + override fun getLinkStyle(): LinkStyleConfig = linkStyle + override fun getHeaderStyle(): HeaderStyleConfig = headerStyle + override fun getImageStyle(): ImageStyleConfig = imageStyle + override fun getListStyle(): ListStyleConfig = listStyle +} + +class MarkdownTextStyle( + defaultFontSize: Float, + defaultTextColor: Color?, + padding: Float, + val font: FontProvider +) : TextStyleConfig(defaultFontSize, defaultTextColor, padding) \ No newline at end of file diff --git a/src/main/java/dev/dediamondpro/minemark/LayoutConfig.java b/src/main/java/dev/dediamondpro/minemark/LayoutConfig.java deleted file mode 100644 index b5855a6..0000000 --- a/src/main/java/dev/dediamondpro/minemark/LayoutConfig.java +++ /dev/null @@ -1,230 +0,0 @@ -package dev.dediamondpro.minemark; - -import dev.dediamondpro.minemark.providers.BrowserProvider; -import dev.dediamondpro.minemark.providers.DefaultBrowserProvider; -import dev.dediamondpro.minemark.providers.DefaultImageProvider; -import dev.dediamondpro.minemark.providers.ImageProvider; - -import java.awt.*; - -/** - * Class that will be given to an element at parsing time - * Default variables used to control the layout - */ -public class LayoutConfig { - private Alignment alignment; - private float fontSize; - private Color textColor; - private boolean bold; - private boolean italic; - private boolean underlined; - private boolean strikethrough; - private boolean partOfLink; - private boolean preFormatted; - 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, boolean preFormatted, 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.partOfLink = partOfLink; - this.preFormatted = preFormatted; - this.strikethrough = strikethrough; - this.spacingConfig = spacingConfig; - this.headingConfig = headingConfig; - this.imageProvider = imageProvider; - this.browserProvider = browserProvider; - } - - public LayoutConfig(float defaultFontSize, SpacingConfig spacingConfig, HeadingConfig headingConfig, ImageProvider imageProvider, BrowserProvider browserProvider) { - this(Alignment.LEFT, defaultFontSize, Color.WHITE, false, false, false, false, false, false, spacingConfig, headingConfig, imageProvider, browserProvider); - } - - 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, preFormatted, spacingConfig, headingConfig, imageProvider, browserProvider); - } - - public Alignment getAlignment() { - return alignment; - } - - public void setAlignment(Alignment alignment) { - this.alignment = alignment; - } - - public float getFontSize() { - return fontSize; - } - - public void setFontSize(float fontSize) { - this.fontSize = fontSize; - } - - public Color getTextColor() { - return textColor; - } - - public void setTextColor(Color textColor) { - this.textColor = textColor; - } - - public boolean isBold() { - return bold; - } - - public void setBold(boolean bold) { - this.bold = bold; - } - - public boolean isItalic() { - return italic; - } - - public void setItalic(boolean italic) { - this.italic = italic; - } - - public boolean isUnderlined() { - return underlined; - } - - public void setUnderlined(boolean underlined) { - this.underlined = underlined; - } - - public boolean isStrikethrough() { - return strikethrough; - } - - public void setStrikethrough(boolean strikethrough) { - this.strikethrough = strikethrough; - } - - public boolean isPartOfLink() { - return partOfLink; - } - - public void setPartOfLink(boolean partOfLink) { - this.partOfLink = partOfLink; - } - - public boolean isPreFormatted() { - return preFormatted; - } - - public void setPreFormatted(boolean preFormatted) { - this.preFormatted = preFormatted; - } - - public SpacingConfig getSpacingConfig() { - return spacingConfig; - } - - public HeadingConfig getHeadingConfig() { - return headingConfig; - } - - public ImageProvider getImageProvider() { - return imageProvider; - } - - public BrowserProvider getBrowserProvider() { - return browserProvider; - } - - public enum Alignment { - CENTER, LEFT, RIGHT - } - - public static class SpacingConfig { - private final float textPadding; - private final float paragraphPadding; - private final float listPadding; - private final float listIndentSpacing; - private final float blockQuotePadding; - - public SpacingConfig(float textPadding, float paragraphPadding, float listIndentSpacing, float listPadding, float blockQuotePadding) { - this.textPadding = textPadding; - this.paragraphPadding = paragraphPadding; - this.listIndentSpacing = listIndentSpacing; - this.listPadding = listPadding; - this.blockQuotePadding = blockQuotePadding; - } - - public SpacingConfig(float textPadding, float paragraphPadding, float listIndentSpacing) { - this(textPadding, paragraphPadding, listIndentSpacing, paragraphPadding, paragraphPadding); - } - - public float getTextPadding() { - return textPadding; - } - - public float getParagraphPadding() { - return paragraphPadding; - } - - public float getListPadding() { - return listPadding; - } - - public float getListIndentSpacing() { - return listIndentSpacing; - } - - public float getBlockQuotePadding() { - return blockQuotePadding; - } - } - - public static class HeadingConfig { - private final float h1FontSize; - private final float h2FontSize; - private final float h3FontSize; - private final float h4FontSize; - private final float h5FontSize; - private final float h6FontSize; - - public HeadingConfig(float h1FontSize, float h2FontSize, float h3FontSize, float h4FontSize, float h5FontSize, float h6FontSize) { - this.h1FontSize = h1FontSize; - this.h2FontSize = h2FontSize; - this.h3FontSize = h3FontSize; - this.h4FontSize = h4FontSize; - this.h5FontSize = h5FontSize; - this.h6FontSize = h6FontSize; - } - - public float getH1FontSize() { - return h1FontSize; - } - - public float getH2FontSize() { - return h2FontSize; - } - - public float getH3FontSize() { - return h3FontSize; - } - - public float getH4FontSize() { - return h4FontSize; - } - - public float getH5FontSize() { - return h5FontSize; - } - - public float getH6FontSize() { - return h6FontSize; - } - } -} diff --git a/src/main/java/dev/dediamondpro/minemark/LayoutData.java b/src/main/java/dev/dediamondpro/minemark/LayoutData.java index 843d232..55a3170 100644 --- a/src/main/java/dev/dediamondpro/minemark/LayoutData.java +++ b/src/main/java/dev/dediamondpro/minemark/LayoutData.java @@ -28,7 +28,7 @@ public void nextLine() { bottomSpacingLocked = false; } - public MarkDownElementPosition addElement(LayoutConfig.Alignment alignment, float width, float height) { + public MarkDownElementPosition addElement(LayoutStyle.Alignment alignment, float width, float height) { MarkDownElementPosition position = new MarkDownElementPosition(currentLine, currentLine.width, width, height, maxWidth, alignment); if (!elementListeners.isEmpty()) { elementListeners.get(0).accept(position); @@ -126,9 +126,9 @@ public static class MarkDownElementPosition { private final float width; private final float height; private final float maxWidth; - private final LayoutConfig.Alignment alignment; + private final LayoutStyle.Alignment alignment; - public MarkDownElementPosition(MarkDownLine line, float x, float width, float height, float maxWidth, LayoutConfig.Alignment alignment) { + public MarkDownElementPosition(MarkDownLine line, float x, float width, float height, float maxWidth, LayoutStyle.Alignment alignment) { this.line = line; this.x = x; this.width = width; diff --git a/src/main/java/dev/dediamondpro/minemark/LayoutStyle.java b/src/main/java/dev/dediamondpro/minemark/LayoutStyle.java new file mode 100644 index 0000000..ef4589a --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/LayoutStyle.java @@ -0,0 +1,122 @@ +package dev.dediamondpro.minemark; + +import dev.dediamondpro.minemark.providers.BrowserProvider; +import dev.dediamondpro.minemark.providers.DefaultBrowserProvider; +import dev.dediamondpro.minemark.providers.DefaultImageProvider; +import dev.dediamondpro.minemark.providers.ImageProvider; +import dev.dediamondpro.minemark.style.Style; + +import java.awt.*; + +/** + * Class that will be given to an element at parsing time + * Default variables used to control the layout + */ +public class LayoutStyle { + private Alignment alignment; + private float fontSize; + private Color textColor; + private boolean bold; + private boolean italic; + private boolean underlined; + private boolean strikethrough; + private boolean partOfLink; + private boolean preFormatted; + + public LayoutStyle(Alignment alignment, float fontSize, Color textColor, boolean bold, boolean italic, boolean underlined, boolean strikethrough, boolean partOfLink, boolean preFormatted) { + this.alignment = alignment; + this.fontSize = fontSize; + this.textColor = textColor; + this.bold = bold; + this.italic = italic; + this.underlined = underlined; + this.partOfLink = partOfLink; + this.preFormatted = preFormatted; + this.strikethrough = strikethrough; + } + + public LayoutStyle(Style style) { + this(Alignment.LEFT, style.getTextStyle().getDefaultFontSize(), Color.WHITE, false, false, false, false, false, false); + } + + public LayoutStyle clone() { + return new LayoutStyle(alignment, fontSize, new Color(textColor.getRGB()), bold, italic, underlined, strikethrough, partOfLink, preFormatted); + } + + public Alignment getAlignment() { + return alignment; + } + + public void setAlignment(Alignment alignment) { + this.alignment = alignment; + } + + public float getFontSize() { + return fontSize; + } + + public void setFontSize(float fontSize) { + this.fontSize = fontSize; + } + + public Color getTextColor() { + return textColor; + } + + public void setTextColor(Color textColor) { + this.textColor = textColor; + } + + public boolean isBold() { + return bold; + } + + public void setBold(boolean bold) { + this.bold = bold; + } + + public boolean isItalic() { + return italic; + } + + public void setItalic(boolean italic) { + this.italic = italic; + } + + public boolean isUnderlined() { + return underlined; + } + + public void setUnderlined(boolean underlined) { + this.underlined = underlined; + } + + public boolean isStrikethrough() { + return strikethrough; + } + + public void setStrikethrough(boolean strikethrough) { + this.strikethrough = strikethrough; + } + + public boolean isPartOfLink() { + return partOfLink; + } + + public void setPartOfLink(boolean partOfLink) { + this.partOfLink = partOfLink; + } + + public boolean isPreFormatted() { + return preFormatted; + } + + public void setPreFormatted(boolean preFormatted) { + this.preFormatted = preFormatted; + } + + + public enum Alignment { + CENTER, LEFT, RIGHT + } +} diff --git a/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java b/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java index 360f134..abe0203 100644 --- a/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java +++ b/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java @@ -2,6 +2,7 @@ import dev.dediamondpro.minemark.elements.ElementLoader; import dev.dediamondpro.minemark.elements.MineMarkElement; +import dev.dediamondpro.minemark.style.Style; import org.commonmark.Extension; import org.commonmark.node.Node; import org.commonmark.parser.Parser; @@ -22,10 +23,10 @@ /** * Class responsible for integrating parsing, layout and rendering */ -public class MineMarkCore { +public class MineMarkCore { private final Parser markdownParser; private final HtmlRenderer htmlRenderer; - private final MineMarkHtmlParser htmlParser; + private final MineMarkHtmlParser htmlParser; private final org.ccil.cowan.tagsoup.Parser xmlParser; private final ReentrantLock parsingLock = new ReentrantLock(); @@ -34,7 +35,7 @@ public class MineMarkCore { * @param extensions Markdown extensions that should be used when parsing * @param urlSanitizer An optional urlSanitizer */ - protected MineMarkCore(Map, ElementLoader> elements, Iterable extensions, @Nullable UrlSanitizer urlSanitizer) { + protected MineMarkCore(Map, ElementLoader> elements, Iterable extensions, @Nullable UrlSanitizer urlSanitizer) { this.markdownParser = Parser.builder().extensions(extensions).build(); HtmlRenderer.Builder htmlRendererBuilder = HtmlRenderer.builder().extensions(extensions); if (urlSanitizer != null) { @@ -49,38 +50,38 @@ protected MineMarkCore(Map, ElementLoader> elements, Iterable /** * Parse markdown to an element used to render it * - * @param markdown The markdown text to parse - * @param layoutConfig The config used at parsing time to create the layout + * @param markdown The markdown text to parse + * @param style The style passed to all elements * @return The parsed markdown element * @throws SAXException An exception during SAX parsing - * @throws IOException An IOException during parsing + * @throws IOException An IOException during parsing */ - public MineMarkElement parse(@NotNull L layoutConfig, @NotNull String markdown) throws SAXException, IOException { + public MineMarkElement parse(@NotNull S style, @NotNull String markdown) throws SAXException, IOException { Node document = markdownParser.parse(markdown); - return parseDocument(layoutConfig, document); + return parseDocument(style, document); } /** * Parse markdown to an element used to render it * - * @param markdown The markdown text to parse - * @param layoutConfig The config used at parsing time to create the layout + * @param markdown The markdown text to parse + * @param style The style passed to all elements * @return The parsed markdown element * @throws SAXException An exception during SAX parsing - * @throws IOException An IOException during parsing + * @throws IOException An IOException during parsing */ - public MineMarkElement parse(@NotNull L layoutConfig, @NotNull Reader markdown) throws SAXException, IOException { + public MineMarkElement parse(@NotNull S style, @NotNull Reader markdown) throws SAXException, IOException { Node document = markdownParser.parseReader(markdown); - return parseDocument(layoutConfig, document); + return parseDocument(style, document); } - private MineMarkElement parseDocument(@NotNull L layoutConfig, Node document) throws SAXException, IOException { + private MineMarkElement parseDocument(@NotNull S style, Node document) throws SAXException, IOException { String html = "\n" + htmlRenderer.render(document) + ""; System.out.println(html); try { // Acquire the lock to make sure this thread is the only one using the parser parsingLock.lock(); - htmlParser.setLayoutConfig(layoutConfig); + htmlParser.setStyle(style, new LayoutStyle(style)); xmlParser.parse(new InputSource(new ByteArrayInputStream(html.getBytes()))); return htmlParser.getParsedResult(); } finally { @@ -90,11 +91,11 @@ private MineMarkElement parseDocument(@NotNull L layoutConfig, Node docume } /** - * @param A class that is given to elements at parse time + * @param The style object passed to each element * @param A class that is given to elements at render time * @return The builder used to create the core */ - public static MineMarkCoreBuilder builder() { + public static MineMarkCoreBuilder builder() { return new MineMarkCoreBuilder<>(); } } diff --git a/src/main/java/dev/dediamondpro/minemark/MineMarkCoreBuilder.java b/src/main/java/dev/dediamondpro/minemark/MineMarkCoreBuilder.java index 68a1e64..ba10ab4 100644 --- a/src/main/java/dev/dediamondpro/minemark/MineMarkCoreBuilder.java +++ b/src/main/java/dev/dediamondpro/minemark/MineMarkCoreBuilder.java @@ -7,6 +7,7 @@ import dev.dediamondpro.minemark.elements.impl.formatting.AlignmentElement; import dev.dediamondpro.minemark.elements.impl.formatting.FormattingElement; import dev.dediamondpro.minemark.elements.impl.list.ListHolderElement; +import dev.dediamondpro.minemark.style.Style; import org.commonmark.Extension; import org.commonmark.renderer.html.UrlSanitizer; import org.jetbrains.annotations.NotNull; @@ -16,11 +17,11 @@ import java.util.List; import java.util.Map; -public class MineMarkCoreBuilder { +public class MineMarkCoreBuilder { protected MineMarkCoreBuilder() { } - private final HashMap, ElementLoader> elements = new HashMap<>(); + private final HashMap, ElementLoader> elements = new HashMap<>(); private final ArrayList extensions = new ArrayList<>(); private boolean withDefaultElements = true; private UrlSanitizer urlSanitizer = null; @@ -31,7 +32,7 @@ protected MineMarkCoreBuilder() { * @param elementName Tags the element should use * @param element An ElementLoader of that element */ - public MineMarkCoreBuilder addElement(@NotNull Elements elementName, @NotNull ElementLoader element) { + public MineMarkCoreBuilder addElement(@NotNull Elements elementName, @NotNull ElementLoader element) { this.elements.put(elementName.tags, element); return this; } @@ -42,7 +43,7 @@ public MineMarkCoreBuilder addElement(@NotNull Elements elementName, @NotN * @param tags Tags the element should use * @param element An ElementLoader of that element */ - public MineMarkCoreBuilder addElement(@NotNull List tags, @NotNull ElementLoader element) { + public MineMarkCoreBuilder addElement(@NotNull List tags, @NotNull ElementLoader element) { this.elements.put(tags, element); return this; } @@ -52,8 +53,8 @@ public MineMarkCoreBuilder addElement(@NotNull List tags, @NotNull * * @param elements A Map with all elements that should be added */ - public MineMarkCoreBuilder addElements(@NotNull Map> elements) { - for (Map.Entry> element : elements.entrySet()) { + public MineMarkCoreBuilder addElements(@NotNull Map> elements) { + for (Map.Entry> element : elements.entrySet()) { addElement(element.getKey(), element.getValue()); } return this; @@ -64,7 +65,7 @@ public MineMarkCoreBuilder addElements(@NotNull Map addElementsString(@NotNull Map, ElementLoader> elements) { + public MineMarkCoreBuilder addElementsString(@NotNull Map, ElementLoader> elements) { this.elements.putAll(elements); return this; } @@ -74,7 +75,7 @@ public MineMarkCoreBuilder addElementsString(@NotNull Map, El * * @param extension The extension */ - public MineMarkCoreBuilder addExtension(Extension extension) { + public MineMarkCoreBuilder addExtension(Extension extension) { extensions.add(extension); return this; } @@ -82,7 +83,7 @@ public MineMarkCoreBuilder addExtension(Extension extension) { /** * Disable default extensions */ - public MineMarkCoreBuilder withoutDefaultElements() { + public MineMarkCoreBuilder withoutDefaultElements() { withDefaultElements = false; return this; } @@ -99,7 +100,7 @@ public void setUrlSanitizer(UrlSanitizer urlSanitizer) { /** * @return a MineMarkCore with the given settings */ - public MineMarkCore build() { + public MineMarkCore build() { if (withDefaultElements) { addElement(Elements.PARAGRAPH, ParagraphElement::new); addElement(Elements.FORMATTING, FormattingElement::new); diff --git a/src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java b/src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java index 6364d35..eb7da8d 100644 --- a/src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java +++ b/src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java @@ -3,21 +3,23 @@ import dev.dediamondpro.minemark.elements.Element; import dev.dediamondpro.minemark.elements.ElementLoader; import dev.dediamondpro.minemark.elements.MineMarkElement; +import dev.dediamondpro.minemark.style.Style; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; import java.util.List; import java.util.Map; -public class MineMarkHtmlParser extends DefaultHandler { - private final Map, ElementLoader> elements; - private MineMarkElement markDown; - private Element currentElement; - private L layoutConfig; +public class MineMarkHtmlParser extends DefaultHandler { + private final Map, ElementLoader> elements; + private MineMarkElement markDown; + private Element currentElement; + private LayoutStyle layoutStyle; + private S style; private StringBuilder textBuilder = new StringBuilder(); private boolean isPreFormatted = false; - protected MineMarkHtmlParser(Map, ElementLoader> elements) { + protected MineMarkHtmlParser(Map, ElementLoader> elements) { this.elements = elements; } @@ -25,7 +27,7 @@ protected MineMarkHtmlParser(Map, ElementLoader> elements) { public void startElement(String uri, String localName, String qName, Attributes attributes) { switch (qName) { case "minemark": - markDown = new MineMarkElement<>(layoutConfig, attributes); + markDown = new MineMarkElement<>(style, layoutStyle, attributes); currentElement = markDown; return; case "br": @@ -36,12 +38,12 @@ public void startElement(String uri, String localName, String qName, Attributes break; } addText(); - ElementLoader elementCreator = findElement(qName); + ElementLoader elementCreator = findElement(qName); if (elementCreator == null) { System.out.println("Unknown element " + qName); return; } - currentElement = elementCreator.get(currentElement.getLayoutConfig(), currentElement, qName, attributes); + currentElement = elementCreator.get(style, currentElement.getLayoutStyle(), currentElement, qName, attributes); } @Override @@ -91,16 +93,16 @@ public void characters(char[] ch, int start, int length) { private void addText() { String text = textBuilder.toString(); if (text.isEmpty()) return; - ElementLoader textLoader = findElement("text"); + ElementLoader textLoader = findElement("text"); if (textLoader == null) { throw new IllegalArgumentException("No text element provided!"); } - textLoader.get(currentElement.getLayoutConfig(), currentElement, "text", null).setText(textBuilder.toString()); + textLoader.get(style, currentElement.getLayoutStyle(), currentElement, "text", null).setText(textBuilder.toString()); textBuilder = new StringBuilder(); } - private ElementLoader findElement(String qName) { - for (Map.Entry, ElementLoader> element : elements.entrySet()) { + private ElementLoader findElement(String qName) { + for (Map.Entry, ElementLoader> element : elements.entrySet()) { if (element.getKey().contains(qName)) { return element.getValue(); } @@ -112,18 +114,20 @@ private boolean hasElement(String qName) { return findElement(qName) != null; } - protected void setLayoutConfig(L layoutConfig) { - this.layoutConfig = layoutConfig; + protected void setStyle(S style, LayoutStyle layoutStyle) { + this.style = style; + this.layoutStyle = layoutStyle; } - protected MineMarkElement getParsedResult() { + protected MineMarkElement getParsedResult() { return markDown; } protected void cleanUp() { markDown = null; currentElement = null; - layoutConfig = null; + layoutStyle = null; + style = null; textBuilder = new StringBuilder(); isPreFormatted = false; } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/BasicElement.java b/src/main/java/dev/dediamondpro/minemark/elements/BasicElement.java index 7d4acaa..2eaba1f 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/BasicElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/BasicElement.java @@ -1,18 +1,19 @@ package dev.dediamondpro.minemark.elements; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public abstract class BasicElement extends Element { +public abstract class BasicElement extends Element { private float width; private float height; protected LayoutData.MarkDownElementPosition position; - public BasicElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); + public BasicElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); } @Override @@ -27,7 +28,7 @@ protected void generateLayout(LayoutData layoutData) { if ((!(this instanceof Inline) && layoutData.isLineOccupied()) || layoutData.getX() + width > layoutData.getMaxWidth()) { layoutData.nextLine(); } - position = layoutData.addElement(layoutConfig.getAlignment(), width, height); + position = layoutData.addElement(layoutStyle.getAlignment(), width, height); if (!(this instanceof Inline) && layoutData.isLineOccupied()) { layoutData.nextLine(); } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/ChildBasedElement.java b/src/main/java/dev/dediamondpro/minemark/elements/ChildBasedElement.java index a86ddd7..c1bb6e0 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/ChildBasedElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/ChildBasedElement.java @@ -1,19 +1,20 @@ package dev.dediamondpro.minemark.elements; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public abstract class ChildBasedElement extends Element { - public ChildBasedElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); +public abstract class ChildBasedElement extends Element { + public ChildBasedElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); } @Override protected void draw(float xOffset, float yOffset, float mouseX, float mouseY, R renderData) { - for (Element child : children) { + for (Element child : children) { child.draw(xOffset, yOffset, mouseX, mouseY, renderData); } } @@ -25,7 +26,7 @@ protected void generateLayout(LayoutData layoutData) { } float padding = getPadding(layoutData); layoutData.updateTopSpacing(padding); - for (Element child : children) { + for (Element child : children) { child.generateLayout(layoutData); } layoutData.updateBottomSpacing(padding); diff --git a/src/main/java/dev/dediamondpro/minemark/elements/ChildMovingElement.java b/src/main/java/dev/dediamondpro/minemark/elements/ChildMovingElement.java index 98f3df4..467b15d 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/ChildMovingElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/ChildMovingElement.java @@ -1,7 +1,8 @@ package dev.dediamondpro.minemark.elements; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; +import dev.dediamondpro.minemark.style.Style; import dev.dediamondpro.minemark.utils.MouseButton; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -10,15 +11,15 @@ /** * An element that moves all child elements to the right, useful for blockquotes, lists, ... */ -public abstract class ChildMovingElement extends Element { +public abstract class ChildMovingElement extends Element { protected final MarkerType markerType = getMarkerType(); protected LayoutData.MarkDownElementPosition marker; protected float totalHeight; protected float extraXOffset; protected float extraYOffset; - public ChildMovingElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); + public ChildMovingElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); } @Override @@ -34,7 +35,7 @@ protected void generateLayout(LayoutData layoutData) { LayoutData newLayoutData = new LayoutData(layoutData.getMaxWidth() - xMovement); if (markerType == MarkerType.ONE_LINE) { - marker = layoutData.addElement(LayoutConfig.Alignment.LEFT, xMovement, getMarkerHeight(layoutData)); + marker = layoutData.addElement(LayoutStyle.Alignment.LEFT, xMovement, getMarkerHeight(layoutData)); newLayoutData.setTopSpacing(layoutData.getCurrentLine().getTopSpacing()); newLayoutData.lockTopSpacing(); newLayoutData.setLineHeight(layoutData.getLineHeight()); @@ -48,13 +49,13 @@ protected void generateLayout(LayoutData layoutData) { if (markerType == MarkerType.ONE_LINE) { layoutData.updateLineHeight(firstLine.getRawHeight()); - newLayoutData.setBottomSpacing(layoutData.getCurrentLine().getBottomSpacing()); + newLayoutData.setBottomSpacing(layoutData.getCurrentLine().getBottomSpacing()); if (newLayoutData.getCurrentLine() != firstLine) { layoutData.nextLine(); layoutData.setLineHeight(newLayoutData.getCurrentLine().getBottomY() - firstLine.getHeight()); } } else { - marker = layoutData.addElement(LayoutConfig.Alignment.LEFT, xMovement, totalHeight); + marker = layoutData.addElement(LayoutStyle.Alignment.LEFT, xMovement, totalHeight); } layoutData.nextLine(); @@ -63,7 +64,7 @@ protected void generateLayout(LayoutData layoutData) { } protected void generateNewLayout(LayoutData layoutData) { - for (Element child : children) { + for (Element child : children) { child.generateLayout(layoutData); } } @@ -71,7 +72,7 @@ protected void generateNewLayout(LayoutData layoutData) { @Override protected void draw(float xOffset, float yOffset, float mouseX, float mouseY, R renderData) { drawMarker(marker.getX() + xOffset, marker.getY() + yOffset, totalHeight, renderData); - for (Element child : children) { + for (Element child : children) { child.draw( xOffset + extraXOffset, yOffset + extraYOffset, mouseX - extraXOffset, mouseY - extraYOffset, renderData diff --git a/src/main/java/dev/dediamondpro/minemark/elements/Element.java b/src/main/java/dev/dediamondpro/minemark/elements/Element.java index 72918c6..cff4a2a 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/Element.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/Element.java @@ -1,8 +1,9 @@ package dev.dediamondpro.minemark.elements; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; import dev.dediamondpro.minemark.elements.impl.TextElement; +import dev.dediamondpro.minemark.style.Style; import dev.dediamondpro.minemark.utils.MouseButton; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -10,24 +11,26 @@ import java.util.ArrayList; -public abstract class Element { - protected final @Nullable Element parent; - protected final ArrayList> children = new ArrayList<>(); +public abstract class Element { + protected final @Nullable Element parent; + protected final ArrayList> children = new ArrayList<>(); protected final String qName; protected final Attributes attributes; - protected L layoutConfig; + protected final S style; + protected LayoutStyle layoutStyle; protected String text; /** * Base Element Constructor used by {@link ElementLoader} * - * @param layoutConfig The configuration used to generate the layout positioning + * @param layoutStyle The configuration used to generate the layout positioning * @param parent Parent element, null in top level element {@link MineMarkElement} * @param qName The name of the HTML tag * @param attributes The attributes of the HTML tag, null for text {@link TextElement} */ - public Element(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - this.layoutConfig = layoutConfig; + public Element(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + this.style = style; + this.layoutStyle = layoutStyle; if (parent != null) parent.getChildren().add(this); this.parent = parent; this.qName = qName; @@ -41,24 +44,16 @@ public void setText(String text) { this.text = text; } - public @Nullable Element getParent() { + public @Nullable Element getParent() { return parent; } - public ArrayList> getChildren() { + public ArrayList> getChildren() { return children; } - protected L cloneLayoutConfig(L layoutConfig) { - try { - return (L) layoutConfig.clone(); - } catch (ClassCastException e) { - throw new IllegalStateException("Could not cast layoutConfig.clone() to custom layoutConfig class, is clone implemented properly?"); - } - } - - public L getLayoutConfig() { - return layoutConfig; + public LayoutStyle getLayoutStyle() { + return layoutStyle; } protected abstract void generateLayout(LayoutData layoutData); @@ -73,13 +68,13 @@ public void regenerateLayout() { protected abstract void draw(float xOffset, float yOffset, float mouseX, float mouseY, R renderData); protected void beforeDraw(float xOffset, float yOffset, float mouseX, float mouseY, R renderData) { - for (Element child : children) { + for (Element child : children) { child.beforeDraw(xOffset, yOffset, mouseX, mouseY, renderData); } } protected void onMouseClicked(MouseButton button, float mouseX, float mouseY) { - for (Element child : children) { + for (Element child : children) { child.onMouseClicked(button, mouseX, mouseY); } } @@ -90,7 +85,7 @@ protected void onMouseClicked(MouseButton button, float mouseX, float mouseY) { public String buildTree(int depth) { StringBuilder builder = new StringBuilder(); builder.append(this); - for (Element child : children) { + for (Element child : children) { builder.append("\n"); for (int i = 0; i < depth; i++) builder.append(" "); builder.append("+ "); diff --git a/src/main/java/dev/dediamondpro/minemark/elements/ElementLoader.java b/src/main/java/dev/dediamondpro/minemark/elements/ElementLoader.java index 379f060..2f00ca2 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/ElementLoader.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/ElementLoader.java @@ -1,11 +1,12 @@ package dev.dediamondpro.minemark.elements; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public interface ElementLoader { - Element get(L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes); +public interface ElementLoader { + Element get(S style, LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes); } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/MineMarkElement.java b/src/main/java/dev/dediamondpro/minemark/elements/MineMarkElement.java index 419e2a6..97894d2 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/MineMarkElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/MineMarkElement.java @@ -1,20 +1,21 @@ package dev.dediamondpro.minemark.elements; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; +import dev.dediamondpro.minemark.style.Style; import dev.dediamondpro.minemark.utils.MouseButton; import org.xml.sax.Attributes; import java.util.ArrayList; import java.util.function.Consumer; -public final class MineMarkElement extends ChildBasedElement { +public final class MineMarkElement extends ChildBasedElement { private final ArrayList> layoutCallbacks = new ArrayList<>(); private float lastWidth = -1; private float height; - public MineMarkElement(L layoutConfig, Attributes attributes) { - super(layoutConfig, null, null, attributes); + public MineMarkElement(S style, LayoutStyle layoutStyle, Attributes attributes) { + super(style, layoutStyle, null, null, attributes); } @Override diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/ImageElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/ImageElement.java index 4784983..d133e50 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/ImageElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/ImageElement.java @@ -1,11 +1,12 @@ package dev.dediamondpro.minemark.elements.impl; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; import dev.dediamondpro.minemark.elements.BasicElement; import dev.dediamondpro.minemark.elements.Element; import dev.dediamondpro.minemark.elements.Inline; import dev.dediamondpro.minemark.providers.ImageProvider; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; @@ -13,7 +14,7 @@ import java.awt.image.BufferedImage; import java.util.regex.Pattern; -public abstract class ImageElement extends BasicElement implements Inline { +public abstract class ImageElement extends BasicElement implements Inline { private static final Pattern pixelPattern = Pattern.compile("^\\d+(px)?$"); protected float imageWidth = -1; protected float imageHeight = -1; @@ -22,11 +23,11 @@ public abstract class ImageElement extends BasicEleme protected final String src; protected BufferedImage image = null; - public ImageElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); + public ImageElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); assert attributes != null; this.src = attributes.getValue("src"); - layoutConfig.getImageProvider().getImage(src, this::onDimensionsReceived, this::onImageReceived); + style.getImageStyle().getImageProvider().getImage(src, this::onDimensionsReceived, this::onImageReceived); } protected void onDimensionsReceived(ImageProvider.Dimension dimension) { diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/LinkElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/LinkElement.java index 4be4e5c..0c618ee 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/LinkElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/LinkElement.java @@ -1,10 +1,11 @@ package dev.dediamondpro.minemark.elements.impl; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; 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.style.Style; import dev.dediamondpro.minemark.utils.MouseButton; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -13,24 +14,24 @@ import java.awt.*; import java.util.ArrayList; -public class LinkElement extends ChildBasedElement implements Inline { +public class LinkElement extends ChildBasedElement implements Inline { protected final ArrayList positions = new ArrayList<>(); protected final String link; - public LinkElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); + public LinkElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); this.link = attributes != null ? attributes.getValue("href") : null; if (link != null) { - this.layoutConfig = cloneLayoutConfig(this.layoutConfig); - this.layoutConfig.setTextColor(new Color(65, 105, 225)); - this.layoutConfig.setPartOfLink(true); + this.layoutStyle = this.layoutStyle.clone(); + this.layoutStyle.setTextColor(style.getLinkStyle().getTextColor()); + this.layoutStyle.setPartOfLink(true); } } @Override protected void onMouseClicked(MouseButton button, float mouseX, float mouseY) { if ((button == MouseButton.LEFT || button == MouseButton.MIDDLE) && isAnyInside(mouseX, mouseY) && link != null) { - layoutConfig.getBrowserProvider().browse(link); + style.getLinkStyle().getBrowserProvider().browse(link); } else { super.onMouseClicked(button, mouseX, mouseY); } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/ParagraphElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/ParagraphElement.java index 63c55f5..dfab3a4 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/ParagraphElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/ParagraphElement.java @@ -1,20 +1,22 @@ package dev.dediamondpro.minemark.elements.impl; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; import dev.dediamondpro.minemark.elements.ChildBasedElement; import dev.dediamondpro.minemark.elements.Element; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public class ParagraphElement extends ChildBasedElement { - public ParagraphElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); +public class ParagraphElement extends ChildBasedElement { + + public ParagraphElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); } @Override protected float getPadding(LayoutData layoutData) { - return layoutConfig.getSpacingConfig().getParagraphPadding(); + return style.getParagraphStyle().getPadding(); } } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/TextElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/TextElement.java index dd4352c..46cb65c 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/TextElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/TextElement.java @@ -1,9 +1,10 @@ package dev.dediamondpro.minemark.elements.impl; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; import dev.dediamondpro.minemark.elements.Element; import dev.dediamondpro.minemark.elements.Inline; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; @@ -13,11 +14,11 @@ import java.util.List; import java.util.Map; -public abstract class TextElement extends Element implements Inline { +public abstract class TextElement extends Element implements Inline { protected final HashMap lines = new HashMap<>(); - public TextElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); + public TextElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); } @Override @@ -27,7 +28,7 @@ public void generateLayout(LayoutData layoutData) { String[] predefinedLines = text.split("\n", -1); for (int i = 0; i < predefinedLines.length; i++) { String line = predefinedLines[i].replace("\n", ""); - if (layoutConfig.isPreFormatted()) { + if (layoutStyle.isPreFormatted()) { allLines.add(line); } else { allLines.addAll(wrapText(line, i == 0 ? layoutData.getX() : 0f, layoutData.getMaxWidth())); @@ -35,8 +36,8 @@ public void generateLayout(LayoutData layoutData) { } for (int i = 0; i < allLines.size(); i++) { String line = allLines.get(i); - layoutData.updatePadding(layoutConfig.getSpacingConfig().getTextPadding() * layoutConfig.getFontSize()); - lines.put(layoutData.addElement(layoutConfig.getAlignment(), getTextWidth(line), getTextHeight(line)), line); + layoutData.updatePadding(style.getTextStyle().getPadding() * layoutStyle.getFontSize()); + lines.put(layoutData.addElement(layoutStyle.getAlignment(), getTextWidth(line), getTextHeight(line)), line); if (i != allLines.size() - 1) { layoutData.nextLine(); } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/AlignmentElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/AlignmentElement.java index 4977f73..9499297 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/AlignmentElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/AlignmentElement.java @@ -1,31 +1,32 @@ package dev.dediamondpro.minemark.elements.impl.formatting; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; 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.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public class AlignmentElement extends ChildBasedElement implements Inline { +public class AlignmentElement extends ChildBasedElement implements Inline { protected String alignment; - public AlignmentElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); - this.layoutConfig = cloneLayoutConfig(layoutConfig); + public AlignmentElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); + this.layoutStyle = this.layoutStyle.clone(); alignment = qName.equals("div") ? (attributes != null ? attributes.getValue("align") : null) : qName; if (alignment == null) return; switch (alignment) { case "left": - this.layoutConfig.setAlignment(LayoutConfig.Alignment.LEFT); + this.layoutStyle.setAlignment(LayoutStyle.Alignment.LEFT); break; case "center": - this.layoutConfig.setAlignment(LayoutConfig.Alignment.CENTER); + this.layoutStyle.setAlignment(LayoutStyle.Alignment.CENTER); break; case "right": - this.layoutConfig.setAlignment(LayoutConfig.Alignment.RIGHT); + this.layoutStyle.setAlignment(LayoutStyle.Alignment.RIGHT); break; } } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/FormattingElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/FormattingElement.java index 2e32990..c894628 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/FormattingElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/FormattingElement.java @@ -1,36 +1,37 @@ package dev.dediamondpro.minemark.elements.impl.formatting; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.elements.ChildBasedElement; import dev.dediamondpro.minemark.elements.Element; import dev.dediamondpro.minemark.elements.Inline; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public class FormattingElement extends ChildBasedElement implements Inline { - public FormattingElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); - this.layoutConfig = cloneLayoutConfig(layoutConfig); +public class FormattingElement extends ChildBasedElement implements Inline { + public FormattingElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); + this.layoutStyle = this.layoutStyle.clone(); switch (qName) { case "strong": case "b": - this.layoutConfig.setBold(true); + this.layoutStyle.setBold(true); break; case "em": case "i": - this.layoutConfig.setItalic(true); + this.layoutStyle.setItalic(true); break; case "ins": case "u": - this.layoutConfig.setUnderlined(true); + this.layoutStyle.setUnderlined(true); break; case "del": case "s": - this.layoutConfig.setStrikethrough(true); + this.layoutStyle.setStrikethrough(true); break; case "pre": - this.layoutConfig.setPreFormatted(true); + this.layoutStyle.setPreFormatted(true); break; } } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/HeadingElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/HeadingElement.java index fab7669..0de7fd3 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/HeadingElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/formatting/HeadingElement.java @@ -1,20 +1,21 @@ package dev.dediamondpro.minemark.elements.impl.formatting; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.elements.ChildBasedElement; import dev.dediamondpro.minemark.elements.Element; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public class HeadingElement extends ChildBasedElement { +public class HeadingElement extends ChildBasedElement { protected final HeadingType headingType; - public HeadingElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); + public HeadingElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); this.headingType = HeadingType.getFromHtmlTag(qName); - this.layoutConfig = cloneLayoutConfig(layoutConfig); - this.layoutConfig.setFontSize(headingType.getFontSize(this.layoutConfig.getHeadingConfig())); + this.layoutStyle = this.layoutStyle.clone(); + this.layoutStyle.setFontSize(headingType.getFontSize(style)); } @Override @@ -25,20 +26,20 @@ public String toString() { public enum HeadingType { H1, H2, H3, H4, H5, H6; - public float getFontSize(LayoutConfig.HeadingConfig config) { + public float getFontSize(Style style) { switch (this) { case H1: - return config.getH1FontSize(); + return style.getHeaderStyle().getH1().getFontSize(); case H2: - return config.getH2FontSize(); + return style.getHeaderStyle().getH2().getFontSize(); case H3: - return config.getH3FontSize(); + return style.getHeaderStyle().getH3().getFontSize(); case H4: - return config.getH4FontSize(); + return style.getHeaderStyle().getH4().getFontSize(); case H5: - return config.getH5FontSize(); + return style.getHeaderStyle().getH5().getFontSize(); case H6: - return config.getH6FontSize(); + return style.getHeaderStyle().getH6().getFontSize(); } throw new IllegalStateException("This should literally never be thrown."); } diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/list/ListElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/list/ListElement.java index 0a08e44..6275207 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/list/ListElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/list/ListElement.java @@ -1,24 +1,25 @@ package dev.dediamondpro.minemark.elements.impl.list; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; import dev.dediamondpro.minemark.elements.ChildMovingElement; import dev.dediamondpro.minemark.elements.Element; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public abstract class ListElement extends ChildMovingElement { +public abstract class ListElement extends ChildMovingElement { protected final ListHolderElement.ListType listType; protected final int elementIndex; protected float markerWidth; - public ListElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); + public ListElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); if (!(parent instanceof ListHolderElement)) { throw new IllegalArgumentException("List element has no surrounding list holder!"); } - ListHolderElement holder = (ListHolderElement) parent; + ListHolderElement holder = (ListHolderElement) parent; listType = holder.getListType(); elementIndex = holder.getChildren().indexOf(this); } @@ -26,7 +27,7 @@ public ListElement(@NotNull L layoutConfig, @Nullable Element parent, @Not @Override protected float getPadding(LayoutData layoutData) { - return layoutConfig.getSpacingConfig().getTextPadding(); + return style.getTextStyle().getPadding(); } @Override @@ -42,7 +43,7 @@ protected final void drawMarker(float x, float y, float totalHeight, R renderDat @Override protected final float getMarkerWidth(LayoutData layoutData) { markerWidth = getMarkerWidth(); - return Math.max(markerWidth, layoutConfig.getSpacingConfig().getListIndentSpacing()); + return Math.max(markerWidth, style.getListStyle().getIndentation()); } protected abstract void drawMarker(float x, float y, R renderData); diff --git a/src/main/java/dev/dediamondpro/minemark/elements/impl/list/ListHolderElement.java b/src/main/java/dev/dediamondpro/minemark/elements/impl/list/ListHolderElement.java index 90c80ff..6f65950 100644 --- a/src/main/java/dev/dediamondpro/minemark/elements/impl/list/ListHolderElement.java +++ b/src/main/java/dev/dediamondpro/minemark/elements/impl/list/ListHolderElement.java @@ -1,17 +1,18 @@ package dev.dediamondpro.minemark.elements.impl.list; -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.LayoutData; import dev.dediamondpro.minemark.elements.ChildBasedElement; import dev.dediamondpro.minemark.elements.Element; +import dev.dediamondpro.minemark.style.Style; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.xml.sax.Attributes; -public class ListHolderElement extends ChildBasedElement { +public class ListHolderElement extends ChildBasedElement { protected ListType listType; - public ListHolderElement(@NotNull L layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); + public ListHolderElement(@NotNull S style, @NotNull LayoutStyle layoutStyle, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { + super(style, layoutStyle, parent, qName, attributes); switch (qName) { default: case "ol": @@ -25,7 +26,7 @@ public ListHolderElement(@NotNull L layoutConfig, @Nullable Element parent @Override protected float getPadding(LayoutData layoutData) { - return layoutConfig.getSpacingConfig().getListPadding(); + return style.getListStyle().getPadding(); } public ListType getListType() { diff --git a/src/main/java/dev/dediamondpro/minemark/style/HeaderLevelStyleConfig.java b/src/main/java/dev/dediamondpro/minemark/style/HeaderLevelStyleConfig.java new file mode 100644 index 0000000..118ef08 --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/style/HeaderLevelStyleConfig.java @@ -0,0 +1,50 @@ +package dev.dediamondpro.minemark.style; + + +import java.awt.*; + +public class HeaderLevelStyleConfig { + private final float fontSize; + private final float padding; + private final boolean hasDivider; + private final Color dividerColor; + private final float dividerHeight; + private final float spaceBeforeDivider; + + public HeaderLevelStyleConfig(float fontSize, float padding, boolean hasDivider, Color dividerColor, float dividerHeight, float spaceBeforeDivider) { + this.fontSize = fontSize; + this.padding = padding; + this.hasDivider = hasDivider; + this.dividerColor = dividerColor; + this.dividerHeight = dividerHeight; + this.spaceBeforeDivider = spaceBeforeDivider; + } + + public HeaderLevelStyleConfig(float fontSize, float padding) { + this(fontSize, padding, false, null, 0f, 0f); + } + + public float getFontSize() { + return fontSize; + } + + public float getPadding() { + return padding; + } + + public boolean isHasDivider() { + return hasDivider; + } + + public Color getDividerColor() { + return dividerColor; + } + + public float getDividerHeight() { + return dividerHeight; + } + + public float getSpaceBeforeDivider() { + return spaceBeforeDivider; + } +} diff --git a/src/main/java/dev/dediamondpro/minemark/style/HeaderStyleConfig.java b/src/main/java/dev/dediamondpro/minemark/style/HeaderStyleConfig.java new file mode 100644 index 0000000..4d7bc1c --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/style/HeaderStyleConfig.java @@ -0,0 +1,43 @@ +package dev.dediamondpro.minemark.style; + +public class HeaderStyleConfig { + private final HeaderLevelStyleConfig h1; + private final HeaderLevelStyleConfig h2; + private final HeaderLevelStyleConfig h3; + private final HeaderLevelStyleConfig h4; + private final HeaderLevelStyleConfig h5; + private final HeaderLevelStyleConfig h6; + + public HeaderStyleConfig(HeaderLevelStyleConfig h1, HeaderLevelStyleConfig h2, HeaderLevelStyleConfig h3, HeaderLevelStyleConfig h4, HeaderLevelStyleConfig h5, HeaderLevelStyleConfig h6) { + this.h1 = h1; + this.h2 = h2; + this.h3 = h3; + this.h4 = h4; + this.h5 = h5; + this.h6 = h6; + } + + public HeaderLevelStyleConfig getH1() { + return h1; + } + + public HeaderLevelStyleConfig getH2() { + return h2; + } + + public HeaderLevelStyleConfig getH3() { + return h3; + } + + public HeaderLevelStyleConfig getH4() { + return h4; + } + + public HeaderLevelStyleConfig getH5() { + return h5; + } + + public HeaderLevelStyleConfig getH6() { + return h6; + } +} diff --git a/src/main/java/dev/dediamondpro/minemark/style/ImageStyleConfig.java b/src/main/java/dev/dediamondpro/minemark/style/ImageStyleConfig.java new file mode 100644 index 0000000..0d0203c --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/style/ImageStyleConfig.java @@ -0,0 +1,15 @@ +package dev.dediamondpro.minemark.style; + +import dev.dediamondpro.minemark.providers.ImageProvider; + +public class ImageStyleConfig { + private final ImageProvider imageProvider; + + public ImageStyleConfig(ImageProvider imageProvider) { + this.imageProvider = imageProvider; + } + + public ImageProvider getImageProvider() { + return imageProvider; + } +} diff --git a/src/main/java/dev/dediamondpro/minemark/style/LinkStyleConfig.java b/src/main/java/dev/dediamondpro/minemark/style/LinkStyleConfig.java new file mode 100644 index 0000000..21a1f4c --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/style/LinkStyleConfig.java @@ -0,0 +1,29 @@ +package dev.dediamondpro.minemark.style; + +import dev.dediamondpro.minemark.providers.BrowserProvider; + +import java.awt.*; + +public class LinkStyleConfig { + private final Color textColor; + private final boolean underlineHovered; + private final BrowserProvider browserProvider; + + public LinkStyleConfig(Color textColor, boolean underlineHovered, BrowserProvider browserProvider) { + this.textColor = textColor; + this.underlineHovered = underlineHovered; + this.browserProvider = browserProvider; + } + + public Color getTextColor() { + return textColor; + } + + public boolean isUnderlineHovered() { + return underlineHovered; + } + + public BrowserProvider getBrowserProvider() { + return browserProvider; + } +} diff --git a/src/main/java/dev/dediamondpro/minemark/style/ListStyleConfig.java b/src/main/java/dev/dediamondpro/minemark/style/ListStyleConfig.java new file mode 100644 index 0000000..a4917fc --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/style/ListStyleConfig.java @@ -0,0 +1,19 @@ +package dev.dediamondpro.minemark.style; + +public class ListStyleConfig { + private final float indentation; + private final float padding; + + public ListStyleConfig(float indentation, float padding) { + this.indentation = indentation; + this.padding = padding; + } + + public float getIndentation() { + return indentation; + } + + public float getPadding() { + return padding; + } +} diff --git a/src/main/java/dev/dediamondpro/minemark/style/ParagraphStyleConfig.java b/src/main/java/dev/dediamondpro/minemark/style/ParagraphStyleConfig.java new file mode 100644 index 0000000..fb48248 --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/style/ParagraphStyleConfig.java @@ -0,0 +1,13 @@ +package dev.dediamondpro.minemark.style; + +public class ParagraphStyleConfig { + private final float padding; + + public ParagraphStyleConfig(float padding) { + this.padding = padding; + } + + public float getPadding() { + return padding; + } +} diff --git a/src/main/java/dev/dediamondpro/minemark/style/Style.java b/src/main/java/dev/dediamondpro/minemark/style/Style.java new file mode 100644 index 0000000..05ea2f4 --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/style/Style.java @@ -0,0 +1,16 @@ +package dev.dediamondpro.minemark.style; + +public interface Style { + + TextStyleConfig getTextStyle(); + + ParagraphStyleConfig getParagraphStyle(); + + LinkStyleConfig getLinkStyle(); + + HeaderStyleConfig getHeaderStyle(); + + ImageStyleConfig getImageStyle(); + + ListStyleConfig getListStyle(); +} diff --git a/src/main/java/dev/dediamondpro/minemark/style/TextStyleConfig.java b/src/main/java/dev/dediamondpro/minemark/style/TextStyleConfig.java new file mode 100644 index 0000000..3f60a3a --- /dev/null +++ b/src/main/java/dev/dediamondpro/minemark/style/TextStyleConfig.java @@ -0,0 +1,27 @@ +package dev.dediamondpro.minemark.style; + +import java.awt.*; + +public class TextStyleConfig { + private final float defaultFontSize; + private final Color defaultTextColor; + private final float padding; + + public TextStyleConfig(float defaultFontSize, Color defaultTextColor, float padding) { + this.defaultFontSize = defaultFontSize; + this.defaultTextColor = defaultTextColor; + this.padding = padding; + } + + public float getDefaultFontSize() { + return defaultFontSize; + } + + public Color getDefaultTextColor() { + return defaultTextColor; + } + + public float getPadding() { + return padding; + } +} diff --git a/src/test/java/BasicTest.java b/src/test/java/BasicTest.java index 82ae529..133d24e 100644 --- a/src/test/java/BasicTest.java +++ b/src/test/java/BasicTest.java @@ -1,4 +1,4 @@ -import dev.dediamondpro.minemark.LayoutConfig; +import dev.dediamondpro.minemark.LayoutStyle; import dev.dediamondpro.minemark.MineMarkCore; import dev.dediamondpro.minemark.elements.Elements; import dev.dediamondpro.minemark.elements.MineMarkElement; @@ -13,25 +13,25 @@ public class BasicTest { - private final MineMarkCore core = MineMarkCore.builder() + /*private final MineMarkCore core = MineMarkCore.builder() .addExtension(StrikethroughExtension.create()) .addElement(Elements.TEXT, DummyTextElement::new) .build(); - private final LayoutConfig config = new LayoutConfig(5, new LayoutConfig.SpacingConfig(1f, 2f, 5f), new LayoutConfig.HeadingConfig(6, 5, 4, 3, 2, 1)); + private final LayoutStyle config = new LayoutStyle(5, new LayoutStyle.SpacingConfig(1f, 2f, 5f), new LayoutStyle.HeadingConfig(6, 5, 4, 3, 2, 1)); @Test public void test() throws IOException, SAXException { - MineMarkElement element = core.parse(config, "> Test"); + MineMarkElement element = core.parse(config, "> Test"); //element.beforeDraw(0f, 0f, 25f, 0f, 0f, new Object()); System.out.println(element.getTree()); } @Test public void testFile() throws IOException, SAXException { - MineMarkElement element = core.parse(config, readFromInputStream(getClass().getResourceAsStream("test.md"))); + MineMarkElement element = core.parse(config, readFromInputStream(getClass().getResourceAsStream("test.md"))); System.out.println(element.getTree()); - } + }*/ private String readFromInputStream(InputStream inputStream) throws IOException { diff --git a/src/test/java/DummyTextElement.java b/src/test/java/DummyTextElement.java deleted file mode 100644 index a7b7f37..0000000 --- a/src/test/java/DummyTextElement.java +++ /dev/null @@ -1,33 +0,0 @@ -import dev.dediamondpro.minemark.LayoutConfig; -import dev.dediamondpro.minemark.LayoutData; -import dev.dediamondpro.minemark.elements.Element; -import dev.dediamondpro.minemark.elements.impl.TextElement; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.xml.sax.Attributes; - -public class DummyTextElement extends TextElement { - public DummyTextElement(@NotNull LayoutConfig layoutConfig, @Nullable Element parent, @NotNull String qName, @Nullable Attributes attributes) { - super(layoutConfig, parent, qName, attributes); - } - - @Override - public void generateLayout(LayoutData layoutData) { - super.generateLayout(layoutData); - } - - @Override - public void drawText(@NotNull String text, float x, float bottomY, boolean hovered, @NotNull Object renderData) { - - } - - @Override - public float getTextWidth(@NotNull String text) { - return text.length(); - } - - @Override - public float getTextHeight(@NotNull String text) { - return 1; - } -}