Skip to content

Commit

Permalink
Get the distance field to turn off for emoji!
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyettinger committed May 19, 2024
1 parent f17aa9c commit 10e992e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
31 changes: 29 additions & 2 deletions src/main/java/com/github/tommyettinger/textra/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
* @see #markup(String, Layout) The markup() method's documentation covers all the markup tags.
*/
public class Font implements Disposable {

/**
* A {@link DistanceFieldType} that should be {@link DistanceFieldType#STANDARD} for most fonts, and can be
* {@link DistanceFieldType#SDF}, {@link DistanceFieldType#MSDF}, or {@link DistanceFieldType#SDF_OUTLINE} if you
Expand Down Expand Up @@ -478,10 +477,24 @@ public enum DistanceFieldType {
* drawn from a TextureAtlas that the font shares with other images.
*/
public Array<TextureRegion> parents;
protected DistanceFieldType distanceField;
protected DistanceFieldType distanceField = DistanceFieldType.STANDARD;

/**
* Effectively used to attach a Float value to each Batch that might be used to draw a Font, where the Float is the
* current {@code u_smoothing} uniform value used by that Batch.
*/
private static final IdentityHashMap<Batch, Float> smoothingValues = new IdentityHashMap<>(8);

/**
* The last Texture drawn, which may be null if nothing has drawn before. This is used to tell when to enable or
* disable distance field shaders because some other Texture is being drawn. It's pretty much a hack.
* <br>
* It is a bad practice, but this is a static mutable field. This should be OK in this case because this only has
* any meaning when the Font is being drawn by OpenGL, which is a single-threaded API. All Font instances share this
* because that is necessary to allow switching between fonts to make sense.
*/
private static Texture latestTexture = null;

/**
* If true, this is a fixed-width (monospace) font; if false, this is probably a variable-width font. This affects
* some rendering decisions Font makes, such as whether subscript chars should take up half-width (for variable
Expand Down Expand Up @@ -4151,6 +4164,20 @@ public float drawGlyph(Batch batch, long glyph, float x, float y, float rotation
GlyphRegion tr = font.mapping.get(c);
if (tr == null) return 0f;


if(font.distanceField != DistanceFieldType.STANDARD && latestTexture != (latestTexture = tr.getTexture())) {
boolean located = false;
for (int p = 0; p < font.parents.size; p++) {
if (font.parents.get(p).getTexture() == latestTexture) {
font.enableDistanceFieldShader(batch);
located = true;
break;
}
}
if (!located)
font.disableDistanceFieldShader(batch);
}

if(squashed) {
sizingY *= 0.7f;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public TypingLabel createTypingLabel() {
// font.addImage("😀", atlas.findRegion("love box")).addImage("💀", atlas.findRegion("hate box"));
// font.scale(2, 2);
// Font font = KnownFonts.addEmoji(new Font("experimental/GentiumUnItalic-sdf.json", true)).scale(0.5f, 0.5f);
Font font = KnownFonts.addEmoji(new Font("fontwriter/Gentium-sdf.json", true)).scale(0.6f, 0.6f);
Font font = KnownFonts.addEmoji(new Font("fontwriter/Gentium-msdf.json", true)).scale(0.6f, 0.6f);
// Font font = KnownFonts.addEmoji(KnownFonts.getGentiumSDF());

// Font font = new Font(KnownFonts.getOpenSans().scale(0.5f, 0.5f).setTextureFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.scenes.scene2d.utils.UIUtils;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.TimeUtils;
Expand Down

0 comments on commit 10e992e

Please sign in to comment.