Skip to content

Commit

Permalink
Add distance field font version of Source Code Pro
Browse files Browse the repository at this point in the history
The font is SIL Open Font License by Adobe, and hasn't been modified.
The tools used to make distance field fonts before weren't adequate in
this case, so I made the repo tommyettinger/glamer to better handle the
unusual use case we have here of distance-field fixed-width fonts. It
works, as you can see in RotationDemo and SquidLayersTest.
  • Loading branch information
tommyettinger committed Nov 16, 2016
1 parent 6c06e6c commit 15928f3
Show file tree
Hide file tree
Showing 8 changed files with 2,761 additions and 22 deletions.
Binary file modified assets.zip
Binary file not shown.
1,346 changes: 1,346 additions & 0 deletions assets/SourceCodePro-Medium-distance.fnt

Large diffs are not rendered by default.

Binary file added assets/SourceCodePro-Medium-distance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class DefaultResources implements LifecycleListener {
unicode1 = null, unicode2 = null, arial15 = null;

private TextCellFactory distanceNarrow = null, distanceSquare = null, typewriterDistanceNarrow = null,
distancePrint = null, distanceClean = null;
distancePrint = null, distanceClean = null, distanceCode = null;
private TextureAtlas iconAtlas = null;
public static final String squareName = "Zodiac-Square-12x12.fnt",
narrowName = "Rogue-Zodiac-6x12.fnt",
Expand All @@ -81,7 +81,9 @@ public class DefaultResources implements LifecycleListener {
distanceFieldClean = "Noto-Sans-distance.fnt",
distanceFieldCleanTexture = "Noto-Sans-distance.png",
distanceFieldTypewriterNarrow = "CM-Custom-distance.fnt",
distanceFieldTypewriterNarrowTexture = "CM-Custom-distance.png";
distanceFieldTypewriterNarrowTexture = "CM-Custom-distance.png",
distanceFieldCode = "SourceCodePro-Medium-distance.fnt",
distanceFieldCodeTexture = "SourceCodePro-Medium-distance.png";
public static String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n"
+ "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n"
+ "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n"
Expand Down Expand Up @@ -512,6 +514,42 @@ public static TextCellFactory getStretchableTypewriterFont()
return instance.typewriterDistanceNarrow.copy();
return null;
}
/**
* Returns a TextCellFactory already configured to use a highly-legible fixed-width font with broad Unicode support
* that should scale cleanly to many sizes. Caches the result for later calls. The font used is Source Code Pro, an
* open-source (SIL Open Font License) typeface by Adobe, and it has the best Unicode support of any fixed-width
* font used by SquidLib. It may be a good choice for science-fiction games because of its modern feel, but the
* legibility enhancements made so the font could be usable in text editors also are nice for all text-based games.
* The high glyph count means the part of the image for each glyph is smaller, though, so this may look slightly
* pixelated if it starts small and is resized to much larger. A cell width of 15 and cell height of 27 is ideal;
* this allows the font to resize fairly well to larger sizes using Viewports.
* <br>
* This creates a TextCellFactory instead of a BitmapFont because it needs to set some extra information so the
* distance field font technique this uses can work.
* <br>
* Needs files:
* <ul>
* <li>https://github.com/SquidPony/SquidLib/blob/master/assets/SourceCodePro-Medium-distance.fnt</li>
* <li>https://github.com/SquidPony/SquidLib/blob/master/assets/SourceCodePro-Medium-distance.png</li>
* </ul>
* @return the TextCellFactory object that can represent many sizes of the font SourceCodePro-Medium.otf
*/
public static TextCellFactory getStretchableCodeFont()
{
initialize();
if(instance.distanceCode == null)
{
try {
instance.distanceCode = new TextCellFactory()
.fontDistanceField(distanceFieldCode, distanceFieldCodeTexture);
} catch (Exception e) {
e.printStackTrace();
}
}
if(instance.distanceCode != null)
return instance.distanceCode.copy();
return null;
}

/**
* Returns a TextCellFactory already configured to use a variable-width serif font that should look like the serif
Expand Down
10 changes: 5 additions & 5 deletions squidlib/src/test/java/squidpony/gdx/tests/RotationDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public Creature move(int pos) {
/**
* The pixel width of a cell
*/
private static final int cellWidth = 13 * INTERNAL_ZOOM;
private static final int cellWidth = 15 * INTERNAL_ZOOM;
/**
* The pixel height of a cell
*/
private static final int cellHeight = 25 * INTERNAL_ZOOM;
private static final int cellHeight = 27 * INTERNAL_ZOOM;
private VisualInput input;
private double counter;
private boolean[][] seen;
Expand Down Expand Up @@ -133,7 +133,7 @@ public void create() {
// manually if you use a constant internal zoom; here we use 1f for internal zoom 1, about 2/3f for zoom 2, and
// about 1/2f for zoom 3. If you have more zooms as options for some reason, this formula should hold for many
// cases but probably not all.
textFactory = DefaultResources.getStretchableFont().setSmoothingMultiplier(2f / (INTERNAL_ZOOM + 1f))
textFactory = DefaultResources.getStretchableCodeFont().setSmoothingMultiplier(2f / (INTERNAL_ZOOM + 1f))
.width(cellWidth).height(cellHeight).initBySize(); //.setDirectionGlyph('ˆ')
// Creates a layered series of text grids in a SquidLayers object, using the previously set-up textFactory and
// SquidColorCenters.
Expand All @@ -147,8 +147,8 @@ public void create() {
// a bit of a hack to increase the text height slightly without changing the size of the cells they're in.
// this causes a tiny bit of overlap between cells, which gets rid of an annoying gap between vertical lines.
// if you use '#' for walls instead of box drawing chars, you don't need this.
messages.setTextSize(cellWidth, cellHeight + INTERNAL_ZOOM * 2);
display.setTextSize(cellWidth, cellHeight + INTERNAL_ZOOM * 2);
messages.setTextSize(cellWidth, cellHeight + INTERNAL_ZOOM);
display.setTextSize(cellWidth, cellHeight + INTERNAL_ZOOM);
//The subCell SquidPanel uses a smaller size here; the numbers 8 and 16 should change if cellWidth or cellHeight
//change, and the INTERNAL_ZOOM multiplier keeps things sharp, the same as it does all over here.
viewport = new StretchViewport(width * cellWidth, (height + 4) * cellHeight);
Expand Down
39 changes: 24 additions & 15 deletions squidlib/src/test/java/squidpony/gdx/tests/SquidLayersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import squidpony.squidgrid.FOV;
import squidpony.squidgrid.Radius;
import squidpony.squidgrid.gui.gdx.*;
import squidpony.squidgrid.mapping.DungeonGenerator;
import squidpony.squidgrid.mapping.DungeonUtility;
import squidpony.squidgrid.mapping.OrganicMapGenerator;
import squidpony.squidgrid.mapping.*;
import squidpony.squidmath.Coord;
import squidpony.squidmath.GreasedRegion;
import squidpony.squidmath.StatefulRNG;
Expand Down Expand Up @@ -46,24 +44,33 @@ public class SquidLayersTest extends ApplicationAdapter{
public void create() {
super.create();
rng = new StatefulRNG(0x9876543210L);
gridWidth = 120;
gridHeight = 50;
cellWidth = 8;
cellHeight = 17;
gridWidth = 80;
gridHeight = 30;
cellWidth = 15;
cellHeight = 27;

layers = new SquidLayers(gridWidth, gridHeight, cellWidth, cellHeight,
DefaultResources.getStretchableFont());
layers.setTextSize(cellWidth, cellHeight+1);
DefaultResources.getStretchableCodeFont());
//new TextCellFactory().fontDistanceField("SourceCodePro-Medium-distance.fnt", "SourceCodePro-Medium-distance.png"));
//DefaultResources.getStretchableFont());
//colors = DefaultResources.getSCC().rainbow(0.2f, 1.0f, 144);
colors = DefaultResources.getSCC().loopingGradient(SColor.ATOMIC_TANGERINE, SColor.CRIMSON, 100);
mColors = DefaultResources.getSCC().loopingGradient(SColor.ALICE_BLUE, SColor.MAGIC_MINT, 123);
//colors.addAll(DefaultResources.getSCC().zigzagGradient(Color.MAGENTA, Color.RED, 200));
layers.setLightingColor(colors.get(colorIndex));
fov = new FOV(FOV.SHADOW);
//PacMazeGenerator maze = new PacMazeGenerator(gridWidth, gridHeight, rng);
OrganicMapGenerator org = new OrganicMapGenerator(gridWidth, gridHeight, rng);
DungeonGenerator gen = new DungeonGenerator(gridWidth, gridHeight, rng);
map = gen.generate(org.generate());
displayedMap = DungeonUtility.hashesToLines(map);
//OrganicMapGenerator org = new OrganicMapGenerator(gridWidth, gridHeight, rng);
SerpentMapGenerator org = new SerpentMapGenerator(gridWidth, gridHeight, rng, 0.1);
org.putBoxRoomCarvers(3);
org.putRoundRoomCarvers(1);
org.putCaveCarvers(1);
SectionDungeonGenerator gen = new SectionDungeonGenerator(gridWidth, gridHeight, rng);
gen.addMaze(10);
gen.addBoulders(0, 8);
map = org.generate();
map = gen.generate(map, org.getEnvironment());
displayedMap = DungeonUtility.hashesToLines(map, true);
indicesBG = DungeonUtility.generateBGPaletteIndices(map);
indicesFG = DungeonUtility.generatePaletteIndices(map);
resMap = DungeonUtility.generateResistances(map);
Expand Down Expand Up @@ -98,6 +105,7 @@ public void create() {
@Override
public void resize(int width, int height) {
super.resize(width, height);
stage.getViewport().update(width, height, false);
}

@Override
Expand Down Expand Up @@ -143,6 +151,7 @@ public void render() {
}

layers.put(0, 0, displayedMap, indicesFG, indicesBG, lightness);
stage.getViewport().apply(false);
stage.draw();
stage.act();
int aeLen = layers.getForegroundLayer().animatedEntities.size();
Expand All @@ -156,8 +165,8 @@ public void render() {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "SquidLib Test: SquidLayers";
config.width = 120 * 8;
config.height = 50 * 17;
config.width = 80 * 15;
config.height = 30 * 27;
config.addIcon("Tentacle-16.png", Files.FileType.Internal);
config.addIcon("Tentacle-32.png", Files.FileType.Internal);
config.addIcon("Tentacle-128.png", Files.FileType.Internal);
Expand Down
Loading

0 comments on commit 15928f3

Please sign in to comment.