Skip to content

Commit

Permalink
java: working on measure function stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
adjabaev committed Sep 3, 2024
1 parent 59a793f commit 765ccd2
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 2 deletions.
75 changes: 75 additions & 0 deletions bindings/java/java/examples/java/com/dioxuslabs/taffy/Measure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.dioxuslabs.taffy;

import com.dioxuslabs.taffy.common.Image.ImageContext;
import com.dioxuslabs.taffy.common.Text;
import com.dioxuslabs.taffy.common.Text.FontMetrics;
import com.dioxuslabs.taffy.common.Text.TextContext;
import com.dioxuslabs.taffy.enums.Display;
import com.dioxuslabs.taffy.enums.FlexDirection;
import com.dioxuslabs.taffy.geom.Size;
import com.dioxuslabs.taffy.geom.measure.AvailableSpace;
import com.dioxuslabs.taffy.geom.measure.Dimension;
import com.dioxuslabs.taffy.style.Style;

import java.util.List;

import static com.dioxuslabs.taffy.common.Image.imageMeasureFunction;
import static com.dioxuslabs.taffy.common.Text.LOREM_IPSUM;
import static com.dioxuslabs.taffy.common.Text.textMeasureFunction;

/**
* Equivalent of taffy/examples/measure.rs
*/
public class Measure {
public static Size<Float> measureFunction(
Size<Float> knownDimensions,
Size<AvailableSpace> availableSpace,
Object nodeContext,
FontMetrics fontMetrics
) {
if (knownDimensions.bothAxisDefined()) {
return new Size<>(knownDimensions.width(), knownDimensions.height());
}

if (nodeContext instanceof TextContext nc) {
return textMeasureFunction(knownDimensions, availableSpace, nc, fontMetrics);
} else if (nodeContext instanceof ImageContext nc) {
return imageMeasureFunction(knownDimensions, nc);
} else {
return Size.zero(Float.class);
}
}

public static void main(String[] args) {
TaffyTree taffy = new TaffyTree();

FontMetrics fontMetrics = new FontMetrics(10f, 10f);

long textNode = taffy.newLeafWithContext(
Style.def(),
new TextContext(LOREM_IPSUM, Text.WritingMode.HORIZONTAL)
);

long imageNode = taffy.newLeafWithContext(
Style.def(),
new ImageContext(400f, 300f)
);

long root = taffy.newWithChildren(
Style.builder()
.display(Display.FLEX)
.flexDirection(FlexDirection.COLUMN)
.size(new Size<>(Dimension.length(200), Dimension.auto())),
List.of(textNode, imageNode)
);

// Compute layout and print result
taffy.computeLayoutWithMeasure(
root,
Size.maxContentAvailableSize()
// todo measure func
);

taffy.printTree(root);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,53 @@ public static Size<Float> textMeasureFunction(
AtomicInteger maxLineLength = new AtomicInteger();
Arrays.stream(words).forEach(line -> maxLineLength.addAndGet(line.length()));

float inlineSize =
knownDimensions.getAbs(inlineAxis)
Float abs = knownDimensions.getAbs(inlineAxis);
float inlineSize;
if (abs == null) {
AvailableSpace as = availableSpace.getAbs(inlineAxis);
if (as.isMinContent()) {
inlineSize = minLineLength * fontMetrics.charWidth;
} else if (as.isMaxContent()) {
inlineSize = maxLineLength.get() * fontMetrics.charWidth;
} else {
inlineSize = Math.max(Math.min(as.val(), maxLineLength.get() * fontMetrics.charWidth), minLineLength * fontMetrics.charWidth);
}
} else {
inlineSize = abs;
}

abs = knownDimensions.getAbs(blockAxis);
float blockSize;
if (abs == null) {
int inlineLineLength = (int) Math.floor(inlineSize / fontMetrics.charWidth);
int lineCount = 1;
int currentLineLength = 0;

for (String word : words) {
if (currentLineLength == 0) {
// first word
currentLineLength = word.length();
} else if (currentLineLength + word.length() + 1 > inlineLineLength) {
// every word past the first needs to check for line length including the space between words
// note: a real implementation of this should handle whitespace characters other than ' '
// and do something more sophisticated for long words
lineCount += 1;
currentLineLength = word.length();
} else {
// add the word and a space
currentLineLength += word.length() + 1;
}
}

blockSize = lineCount * fontMetrics.charHeight;
} else {
blockSize = abs;
}

if (textContext.writingMode == WritingMode.HORIZONTAL) {
return new Size<>(inlineSize, blockSize);
} else {
return new Size<>(blockSize, inlineSize);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dioxuslabs.taffy;

import com.dioxuslabs.taffy.geom.Size;
import com.dioxuslabs.taffy.geom.measure.AvailableSpace;
import com.dioxuslabs.taffy.style.Style;

public interface MeasureFunction {
Size<Float> measure(
Size<Float> knownDimensions,
Size<AvailableSpace> availableSpace,
long nodeId,
Object nodeContext,
Style style
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

public class TaffyTree {
public final long ptr;
public Object context;

public TaffyTree() {
this.ptr = nvNewTaffyTree();
Expand All @@ -31,6 +32,11 @@ public long newLeaf(Style style) {
return nvNewLeaf(this.ptr, style);
}

public long newLeafWithContext(Style style, Object context) {
this.context = context;
return nvNewLeaf(this.ptr, style);
}

public long newWithChildren(Style style, List<Long> children) {
return nvNewWithChildren(this.ptr, style, children);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static <T> Size<T> length(Class<T> clazz, float width, float height) {
return (Size<T>) lengthLengthPercentage(width, height);
} else if (clazz == LengthPercentageAuto.class) {
return (Size<T>) lengthLengthPercentageAuto(width, height);
} else if (clazz == Float.class) {
return (Size<T>) new Size<>(width, height);
}
return null;
}
Expand Down Expand Up @@ -214,4 +216,8 @@ public String toString() {
public T getAbs(AbsoluteAxis axis) {
return axis == AbsoluteAxis.HORIZONTAL ? width : height;
}

public boolean bothAxisDefined() {
return width != null && height != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,20 @@ public boolean equals(Object obj) {
}
return type == as.type && value == as.value;
}

public boolean isDefinite() {
return type == 0;
}

public boolean isMinContent() {
return type == 1;
}

public boolean isMaxContent() {
return type == 2;
}

public float val() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public static Style builder() {
return new Style();
}

public static Style def() {
return new Style();
}

public Style display(Display display) {
this.display = display;
return this;
Expand Down

0 comments on commit 765ccd2

Please sign in to comment.