Almost rendering glyphs

This commit is contained in:
Willem Cazander 2022-03-16 17:47:42 +01:00
parent ad8102be3a
commit df5efd4dab
19 changed files with 629 additions and 625 deletions

View file

@ -19,5 +19,6 @@ public enum CodePointCommandᶻᴰ {
XY_OFF_CURVE_START,
XY_ON_CURVE,
XY_OFF_CURVE,
XY_SPACE
XY_SPACE,
;
}

View file

@ -0,0 +1,198 @@
package love.distributedrebirth.unicode4d.draw;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import love.distributedrebirth.numberxd.base2t.part.T02PartBinary;
import love.distributedrebirth.numberxd.base2t.type.V036Teger;
import love.distributedrebirth.numberxd.base2t.type.V072Tong;
import love.distributedrebirth.unicode4d.CodePointCommandᶻᴰ;
import love.distributedrebirth.unicode4d.CodePointᶻᴰ;
import love.distributedrebirth.unicode4d.atlas.FontAtlasStoreGlyph;
import love.distributedrebirth.unicode4d.draw.DrawGlyphContour.ImGlyphPoint;
public class DrawCharacter {
private final FontAtlasStoreGlyph glyph;
private final List<DrawGlyphContour> contours = new ArrayList<>();
private final DrawGlyphPath glyphPath;
private DrawGlyphContour currentContour;
private int unicode;
private int xMax;
private int yMax;
private int xMin;
private int yMin;
private int advanceWidth;
private int leftSideBearing;
private boolean leftToRight;
public DrawCharacter(FontAtlasStoreGlyph glyph) {
this.glyph = glyph;
for (V072Tong tong: glyph.getTongs()) {
processCodePoint(tong.getValue(T02PartBinary.PART_1));
processCodePoint(tong.getValue(T02PartBinary.PART_2));
}
if (currentContour != null) {
contours.add(currentContour);
}
this.glyphPath = createGlyphPath();
}
private void processCodePoint(V036Teger codePoint) {
CodePointCommandᶻᴰ cmd = CodePointᶻᴰ.INSTANCE.getCommand(codePoint);
if (CodePointCommandᶻᴰ.NOP.equals(cmd)) {
return;
}
if (CodePointCommandᶻᴰ.START_LR.equals(cmd)) {
leftToRight = true;
return;
}
if (CodePointCommandᶻᴰ.START_RL.equals(cmd)) {
leftToRight = false;
return;
}
if (CodePointCommandᶻᴰ.UNICODE.equals(cmd)) {
unicode = CodePointᶻᴰ.INSTANCE.getArgumentUnicode(codePoint);
return;
}
if (CodePointCommandᶻᴰ.XY_MAX.equals(cmd)) {
xMax = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_1);
yMax = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_2);
return;
}
if (CodePointCommandᶻᴰ.XY_MIN.equals(cmd)) {
xMin = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_1);
yMin = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_2);
return;
}
if (CodePointCommandᶻᴰ.ADVANCE.equals(cmd)) {
advanceWidth = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_1);
leftSideBearing = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_2);
return;
}
if (CodePointCommandᶻᴰ.XY_ON_CURVE_START.equals(cmd)) {
if (currentContour != null) {
contours.add(currentContour);
}
int x = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_1);
int y = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_2);
currentContour = new DrawGlyphContour();
currentContour.point(x, y, true);
return;
}
if (CodePointCommandᶻᴰ.XY_OFF_CURVE_START.equals(cmd)) {
if (currentContour != null) {
contours.add(currentContour);
}
int x = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_1);
int y = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_2);
currentContour = new DrawGlyphContour();
currentContour.point(x, y, true);
return;
}
if (CodePointCommandᶻᴰ.XY_ON_CURVE.equals(cmd)) {
int x = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_1);
int y = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_2);
currentContour.point(x, y, true);
return;
}
if (CodePointCommandᶻᴰ.XY_OFF_CURVE.equals(cmd)) {
int x = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_1);
int y = CodePointᶻᴰ.INSTANCE.getArgument(codePoint, T02PartBinary.PART_2);
currentContour.point(x, y, false);
return;
}
}
private DrawGlyphPath createGlyphPath() {
// source function getPath(points): opentype.js/src/tables/glyf.js
DrawGlyphPath p = new DrawGlyphPath();
for (DrawGlyphContour contour: contours) {
//ImGlyphPoint prev = null;
ImGlyphPoint curr = contour.getPoints().get(contour.getPoints().size() - 1);
ImGlyphPoint next = contour.getPoints().get(0);
if (curr.onCurve) {
p.moveTo(curr.x, curr.y);
} else {
if (next.onCurve) {
p.moveTo(next.x, next.y);
} else {
int x = (curr.x + next.x) / 2;
int y = (curr.y + next.y) / 2;
p.moveTo(x, y);
}
}
Iterator<ImGlyphPoint> pointIt = contour.getPoints().iterator();
pointIt.next(); // Remove first as that is 'next'
while (pointIt.hasNext()) {
//prev = curr;
curr = next;
next = pointIt.next();
if (curr.onCurve) {
p.lineTo(curr.x, curr.y);
} else {
int x = next.x;
int y = next.y;
if (next.onCurve) {
x = (curr.x + next.x) / 2;
y = (curr.y + next.y) / 2;
}
p.quadCurveTo(curr.x, curr.y, x, y);
}
}
p.closePath();
}
return p;
}
public FontAtlasStoreGlyph getGlyph() {
return glyph;
}
public List<DrawGlyphContour> getContours() {
return contours;
}
public DrawGlyphPath getGlyphPath() {
return glyphPath;
}
public int getUnicode() {
return unicode;
}
public int getxMax() {
return xMax;
}
public int getyMax() {
return yMax;
}
public int getxMin() {
return xMin;
}
public int getyMin() {
return yMin;
}
public int getAdvanceWidth() {
return advanceWidth;
}
public int getLeftSideBearing() {
return leftSideBearing;
}
public boolean isLeftToRight() {
return leftToRight;
}
}

View file

@ -0,0 +1,28 @@
package love.distributedrebirth.unicode4d.draw;
import java.util.ArrayList;
import java.util.List;
public class DrawGlyphContour {
private final List<ImGlyphPoint> points = new ArrayList<>();
public class ImGlyphPoint {
int x;
int y;
boolean onCurve;
public ImGlyphPoint(int x, int y, boolean onCurve) {
this.x = x;
this.y = y;
this.onCurve = onCurve;
}
}
public void point(int x, int y, boolean onCurve) {
points.add(new ImGlyphPoint(x,y, onCurve));
}
public List<ImGlyphPoint> getPoints() {
return points;
}
}

View file

@ -0,0 +1,196 @@
package love.distributedrebirth.unicode4d.draw;
import java.util.ArrayList;
import java.util.List;
public class DrawGlyphPath {
private final List<ImGlyphPathCommand> commands = new ArrayList<>();
public interface ImGlyphPathCommand {
int getX();
int getY();
default boolean isImGlyphMoveTo() {
return this instanceof ImGlyphMoveTo;
}
default boolean isImGlyphLineTo() {
return this instanceof ImGlyphLineTo;
}
default boolean isImGlyphCurveTo() {
return this instanceof ImGlyphCurveTo;
}
default boolean isImGlyphQuadCurveTo() {
return this instanceof ImGlyphQuadCurveTo;
}
default boolean isImGlyphClosePath() {
return this instanceof ImGlyphClosePath;
}
default ImGlyphMoveTo toImGlyphMoveTo() {
return ImGlyphMoveTo.class.cast(this);
}
default ImGlyphLineTo toImGlyphLineTo() {
return ImGlyphLineTo.class.cast(this);
}
default ImGlyphCurveTo toImGlyphCurveTo() {
return ImGlyphCurveTo.class.cast(this);
}
default ImGlyphQuadCurveTo toImGlyphQuadCurveTo() {
return ImGlyphQuadCurveTo.class.cast(this);
}
default ImGlyphClosePath toImGlyphClosePath() {
return ImGlyphClosePath.class.cast(this);
}
}
public final class ImGlyphMoveTo implements ImGlyphPathCommand {
private final int x;
private final int y;
public ImGlyphMoveTo(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
}
public final class ImGlyphLineTo implements ImGlyphPathCommand {
private final int x;
private final int y;
public ImGlyphLineTo(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
}
public final class ImGlyphCurveTo implements ImGlyphPathCommand {
private final int x1;
private final int y1;
private final int x2;
private final int y2;
private final int x;
private final int y;
public ImGlyphCurveTo(int x1, int y1,int x2, int y2,int x, int y) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x = x;
this.y = y;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
public int getX1() {
return x1;
}
public int getY1() {
return y1;
}
public int getX2() {
return x2;
}
public int getY2() {
return y2;
}
}
public final class ImGlyphQuadCurveTo implements ImGlyphPathCommand {
private final int x1;
private final int y1;
private final int x;
private final int y;
public ImGlyphQuadCurveTo(int x1, int y1,int x, int y) {
this.x1 = x1;
this.y1 = y1;
this.x = x;
this.y = y;
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
public int getX1() {
return x1;
}
public int getY1() {
return y1;
}
}
public class ImGlyphClosePath implements ImGlyphPathCommand {
@Override
public int getX() {
return -1;
}
@Override
public int getY() {
return -1;
}
}
public void moveTo(int x, int y) {
commands.add(new ImGlyphMoveTo(x,y));
}
public void lineTo(int x, int y) {
commands.add(new ImGlyphLineTo(x,y));
}
public void curveTo(int x1, int y1,int x2, int y2,int x, int y) {
commands.add(new ImGlyphCurveTo(x1,y1,x2,y2,x,y));
}
public void quadCurveTo(int x1, int y1,int x, int y) {
commands.add(new ImGlyphQuadCurveTo(x1,y1,x,y));
}
public void closePath() {
commands.add(new ImGlyphClosePath());
}
public List<ImGlyphPathCommand> getPath() {
return commands;
}
}