Skip to content

Commit

Permalink
fix MineMarkElement's height being too high, add some more javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Jan 11, 2024
1 parent 0ba2b5a commit f125974
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 10 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ dependencies {
implementation("dev.dediamondpro:minemark-elementa:1.0.0")
}
```
It is recommended to **shade and relocate MineMark** if you are using it in a Minecraft mod since no guarantees will be
provided regarding backwards compatibility.

How you render a markdown element will differ from one rendering implementation to another, here is an example of how
you would do it with the elementa rendering implementation:
```kt
MineMarkComponent("This *is* **were** you input your markdown!").constrain {
MineMarkComponent("This *is* **where** you input your markdown!").constrain {
x = 0.pixels()
y = 0.pixels()
width = 600.pixels()
Expand All @@ -38,19 +40,19 @@ MineMarkComponent("This *is* **were** you input your markdown!").constrain {
To create your own rendering implementation, you first have to implement the rendering of the elements you want to
support. You can choose what elements you implement, the only requirement is that **you have to provide a text element**.
To implement an element, you have to extend its abstract form, for an example for each element I would recommend you
look at the elementa implementation as a reference. An element takes 2 type variables, the first one (S) is the style,
so you have to create a class that implements the `Style` interface. The second can be any class, it is given to your
look at the elementa implementation as a reference. An element takes 2 type variables, the first one (`S`) is the style,
so you have to create a class that implements the `Style` interface. The second (`R`) can be any class, it is given to your
elements at render time. If you do not wish to use this you can just set it to be an `Object`.

Once you implemented the elements you want, you have to register them in a core. You can do this as follows:
```java
MineMarkCore<MyStyle, MyRenderObject> core = MineMarkCore.<MyStyle, MyRenderObject>builder()
// Set the text element to your text element
.setTextElement(MyTextElement)
.setTextElement(MyTextElement::new)
// Set the image element to your image element
.addElement(Elements.IMAGE, MyImageElement)
.addElement(Elements.IMAGE, MyImageElement::new)
// Set an element with the html tag "myHtmlTag" to MyElement
.addElement(Arrays.asList("myHtmlTag"), MyElement)
.addElement(Arrays.asList("myHtmlTag"), MyElement::new)
.build();
```

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/dev/dediamondpro/minemark/LayoutData.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public boolean isLineOccupied() {
}

public void nextLine() {
previousLine = currentLine;
// Only set previous line if there was a meaningful change
if (isLineOccupied() || currentLine.bottomSpacing != 0f || currentLine.height != 0f || currentLine.topSpacing != 0f) {
previousLine = currentLine;
}
currentLine = new MarkDownLine(currentLine.getBottomY());
topSpacingLocked = false;
bottomSpacingLocked = false;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/dev/dediamondpro/minemark/MineMarkCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

/**
* Class responsible for integrating parsing, layout and rendering
*
* @param <S> The {@link Style} this core and all associated elements will use
* @param <R> The class passed to the rendering implementation at render time
*/
public class MineMarkCore<S extends Style, R> {
private final Parser markdownParser;
Expand Down Expand Up @@ -109,8 +112,8 @@ private MineMarkElement<S, R> parseDocument(@NotNull S style, Node document) thr
}

/**
* @param <S> The style object passed to each element
* @param <R> A class that is given to elements at render time
* @param <S> The {@link Style} this core and all associated elements will use
* @param <R> The class passed to the rendering implementation at render time
* @return The builder used to create the core
*/
public static <S extends Style, R> MineMarkCoreBuilder<S, R> builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void generateLayout(LayoutData layoutData, R renderData) {
layoutData.lockTopSpacing();
super.generateLayout(layoutData, renderData);
float bottomSpacing = layoutData.getCurrentLine().getBottomSpacing();
if (bottomSpacing == 0f && layoutData.isLineEmpty()) {
if (bottomSpacing == 0f && layoutData.isLineEmpty() && layoutData.getPreviousLine() != null) {
bottomSpacing = layoutData.getPreviousLine().getBottomSpacing();
}
height = layoutData.getY() + layoutData.getLineHeight() - bottomSpacing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@

import java.util.regex.Pattern;

/**
* Base image element
*
* @param <S> The {@link Style} this core and all associated elements will use
* @param <R> The class passed to the rendering implementation at render time
* @param <I> The class of the image, should be the same as your {@link ImageProvider}
*/
public abstract class ImageElement<S extends Style, R, I> extends BasicElement<S, R> implements Inline {
private static final Pattern pixelPattern = Pattern.compile("^\\d+(px)?$");
protected float imageWidth = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@
* Interface that provides a method to open an url in a browser
*/
public interface BrowserProvider {
/**
* Open a url in a browser
*
* @param url The url
*/
void browse(String url);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

import java.util.function.Consumer;

/**
* Interface used to get an image
*
* @param <I> The class of the image
*/
public interface ImageProvider<I> {

/**
Expand Down

0 comments on commit f125974

Please sign in to comment.