Skip to content

Commit 42eafdd

Browse files
committed
better font support for maps
1 parent 59a2d2f commit 42eafdd

File tree

6 files changed

+329
-61
lines changed

6 files changed

+329
-61
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.broken.arrow.library.itemcreator.meta.map;
2+
3+
public interface CharShape {
4+
5+
/**
6+
* Set the transparency for your letter.
7+
*
8+
* @param width the width currently processing
9+
* @param height the height currently processing
10+
* @return boolean for given width and height for the letter set.
11+
*/
12+
boolean getPixel(int width, int height);
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.broken.arrow.library.itemcreator.meta.map;
2+
3+
public class Characters {
4+
5+
private static final String fontChars =
6+
" !\"#$%&'()*+,-./0123456789:;<=>?" +
7+
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" +
8+
"'abcdefghijklmnopqrstuvwxyz{|}~\u007F" +
9+
"\u00C7\u00FC\u00E9\u00E2\u00E4\u00E0\u00E5\u00E7" + // Çüéâäàåç
10+
"\u00EA\u00EB\u00E8\u00EF\u00EE\u00EC\u00C4\u00C5" + // êëèïîìÄÅ
11+
"\u00C9\u00E6\u00C6\u00F4\u00F6\u00F2\u00FB\u00F9" + // ÉæÆôöòûù
12+
"\u00FF\u00D6\u00DC\u00F8\u00A3\u00D8\u00D7\u0191" + // ÿÖÜø£Ø×ƒ
13+
"\u00E1\u00ED\u00F3\u00FA\u00F1\u00D1\u00AA\u00BA" + // áíóúñѪº
14+
"\u00BF\u00AE\u00AC\u00BD\u00BC\u00A1\u00AB\u00BB"; // ¿®¬½¼¡«»
15+
16+
private static final char[] fontCharsArray = fontChars.toCharArray();
17+
18+
/**
19+
*
20+
* @return get the String of characters set.
21+
*/
22+
public static String getFontChars() {
23+
return fontChars;
24+
}
25+
26+
/**
27+
*
28+
* @return get the array of char set.
29+
*/
30+
public static char[] getFontCharsArray() {
31+
return fontCharsArray;
32+
}
33+
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/MapRendererData.java

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.broken.arrow.library.itemcreator.meta.map.cursor.MapCursorAdapter;
55
import org.broken.arrow.library.itemcreator.meta.map.cursor.MapCursorWrapper;
66
import org.broken.arrow.library.itemcreator.meta.map.font.CharacterSprite;
7+
import org.broken.arrow.library.itemcreator.meta.map.font.MapFontWrapper;
78
import org.broken.arrow.library.itemcreator.meta.map.pixel.ImageOverlay;
89
import org.broken.arrow.library.itemcreator.meta.map.pixel.MapPixel;
910
import org.broken.arrow.library.itemcreator.meta.map.pixel.MapColoredPixel;
@@ -23,6 +24,7 @@
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Objects;
27+
import java.util.function.Consumer;
2628
import java.util.stream.Collectors;
2729

2830
/**
@@ -45,6 +47,7 @@ public class MapRendererData {
4547
private MapCursorAdapter mapCursors = new MapCursorAdapter();
4648
private final List<MapPixel> pixels = new ArrayList<>();
4749
private MapRenderHandler dynamicRenderer;
50+
private char[] fontChars = Characters.getFontCharsArray();
4851

4952
/**
5053
* Constructs a new MapRendererData instance with no associated {@link MapRenderer}.
@@ -97,29 +100,45 @@ public void addPixel(@Nonnull final MapColoredPixel mapColoredPixel) {
97100
/**
98101
* Adds a text overlay to the map without a custom font character sprite.
99102
*
100-
* @param x The x-coordinate of the text.
101-
* @param y The y-coordinate of the text.
102-
* @param text The text to display.
103+
* @param x The x-coordinate of the text.
104+
* @param y The y-coordinate of the text.
105+
* @param text The text to display.
103106
*/
104107
public void addText(final int x, int y, final String text) {
105-
TextOverlay textOverlay = new TextOverlay(x, y, text);
106-
pixels.add(textOverlay);
108+
this.addText(x, y, text, null, null);
109+
}
110+
111+
/**
112+
* Adds a text overlay to the map with a custom font character sprite. Will use the default set
113+
* of characters instead.
114+
*
115+
* @param x The x-coordinate of the text.
116+
* @param y The y-coordinate of the text.
117+
* @param text The text to display.
118+
* @param font The font for the character.
119+
*/
120+
public void addText(final int x, int y, final String text, @Nullable final Font font) {
121+
this.addText(x, y, text, null, font);
107122
}
108123

109124
/**
110125
* Adds a text overlay to the map with a custom font character sprite.
111126
*
112-
* @param x The x-coordinate of the text.
113-
* @param y The y-coordinate of the text.
114-
* @param text The text to display.
115-
* @param ch The character associated with the custom font.
116-
* @param sprite The {@link CharacterSprite} for the font character.
127+
* @param x The x-coordinate of the text.
128+
* @param y The y-coordinate of the text.
129+
* @param text The text to display.
130+
* @param fontChars Set the characters you want to replace in your text with the font.
131+
* @param font The font for the character
117132
*/
118-
public void addText(final int x, int y, final String text, final char ch, @Nullable CharacterSprite sprite) {
133+
public void addText(final int x, int y,@Nonnull final String text, @Nullable final char[] fontChars, @Nullable final Font font) {
119134
TextOverlay textOverlay = new TextOverlay(x, y, text);
120-
if (sprite != null)
121-
textOverlay.setMapFont(ch, sprite);
122-
pixels.add(textOverlay);
135+
if (font != null) {
136+
if (fontChars != null && fontChars.length > 0) {
137+
this.fontChars = fontChars;
138+
}
139+
textOverlay.setMapFont(this.fontChars, font);
140+
}
141+
this.addText(textOverlay);
123142
}
124143

125144
/**
@@ -138,7 +157,7 @@ public void addText(@Nonnull final TextOverlay textOverlay) {
138157
* @param y The y-coordinate of the image.
139158
* @param image The image to display.
140159
*/
141-
public void addImage(final int x,final int y,@Nonnull final Image image) {
160+
public void addImage(final int x, final int y, @Nonnull final Image image) {
142161
pixels.add(new ImageOverlay(x, y, image));
143162
}
144163

@@ -211,9 +230,9 @@ public MapCursorAdapter getMapCursors() {
211230
}
212231

213232
/**
214-
* Returns the current collection of map cursors.
233+
* Returns the current collection of map pixels.
215234
*
216-
* @return The {@link MapCursorAdapter} managing cursors.
235+
* @return The {@link MapCursorAdapter} managing pixels.
217236
*/
218237
public List<MapPixel> getPixels() {
219238
return pixels;
@@ -226,18 +245,14 @@ public List<MapPixel> getPixels() {
226245
* @return The {@link MapRenderer} instance.
227246
*/
228247
public MapRenderer getMapRenderer() {
229-
/* if (this.mapRenderer != null) {
230-
mapRenderer.render();
248+
if (this.mapRenderer != null) {
231249
return mapRenderer;
232-
}*/
250+
}
233251
return new MapRenderer() {
234252
@Override
235253
public void render(@Nonnull MapView map, @Nonnull MapCanvas canvas, @Nonnull Player player) {
236254
if (dynamicRenderer != null && dynamicRenderer.render(map, canvas, player))
237255
return;
238-
if (mapRenderer != null) {
239-
mapRenderer.render(map, canvas, player);
240-
}
241256
canvas.setCursors(mapCursors.getMapCursorCollection());
242257
if (!getPixels().isEmpty()) {
243258
setPixels(canvas);
@@ -246,26 +261,6 @@ public void render(@Nonnull MapView map, @Nonnull MapCanvas canvas, @Nonnull Pla
246261
};
247262
}
248263

249-
private void setPixels(final MapCanvas canvas) {
250-
getPixels().forEach(mapPixel -> {
251-
if (mapPixel instanceof MapColoredPixel) {
252-
if (ItemCreator.getServerVersion() < 20.0F)
253-
canvas.setPixel(mapPixel.getX(), mapPixel.getY(), MapPalette.matchColor(((MapColoredPixel) mapPixel).getColor()));
254-
else
255-
canvas.setPixelColor(mapPixel.getX(), mapPixel.getY(), ((MapColoredPixel) mapPixel).getColor());
256-
}
257-
if (mapPixel instanceof TextOverlay) {
258-
TextOverlay textOverlay = (TextOverlay) mapPixel;
259-
canvas.drawText(mapPixel.getX(), mapPixel.getY(), textOverlay.getMapFont(), textOverlay.getText());
260-
}
261-
if (mapPixel instanceof ImageOverlay) {
262-
ImageOverlay textOverlay = (ImageOverlay) mapPixel;
263-
final Image image = textOverlay.getImage();
264-
if (image != null)
265-
canvas.drawImage(mapPixel.getX(), mapPixel.getY(), image);
266-
}
267-
});
268-
}
269264

270265
@Override
271266
public boolean equals(Object o) {
@@ -336,4 +331,26 @@ public String toString() {
336331
", dynamicRenderer=" + dynamicRenderer +
337332
'}';
338333
}
334+
335+
private void setPixels(@Nonnull final MapCanvas canvas) {
336+
getPixels().forEach(mapPixel -> {
337+
if (mapPixel instanceof MapColoredPixel) {
338+
final Color color = ((MapColoredPixel) mapPixel).getColor();
339+
if (ItemCreator.getServerVersion() < 20.0F)
340+
canvas.setPixel(mapPixel.getX(), mapPixel.getY(), MapPalette.matchColor(color));
341+
else
342+
canvas.setPixelColor(mapPixel.getX(), mapPixel.getY(), color);
343+
}
344+
if (mapPixel instanceof TextOverlay) {
345+
TextOverlay textOverlay = (TextOverlay) mapPixel;
346+
canvas.drawText(mapPixel.getX(), mapPixel.getY(), textOverlay.getMapFont(), textOverlay.getText());
347+
}
348+
if (mapPixel instanceof ImageOverlay) {
349+
ImageOverlay textOverlay = (ImageOverlay) mapPixel;
350+
final Image image = textOverlay.getImage();
351+
if (image != null)
352+
canvas.drawImage(mapPixel.getX(), mapPixel.getY(), image);
353+
}
354+
});
355+
}
339356
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/font/CharacterSprite.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class CharacterSprite {
2828
private final int width;
2929
private final int height;
3030
private final boolean[] data;
31+
3132
/**
3233
* Constructs a CharacterSprite with specified dimensions and pixel data.
3334
*
@@ -36,7 +37,7 @@ public class CharacterSprite {
3637
* @param data A boolean array representing pixel solidity; length must be width * height.
3738
* @throws IllegalArgumentException if data length does not match width * height.
3839
*/
39-
public CharacterSprite(int width, int height, @Nonnull boolean[] data) {
40+
public CharacterSprite(final int width, final int height, @Nonnull final boolean[] data) {
4041
this.width = width;
4142
this.height = height;
4243
this.data = data;
@@ -53,7 +54,7 @@ public CharacterSprite(int width, int height, @Nonnull boolean[] data) {
5354
* @param col The column, in the range [0,8).
5455
* @return True if the pixel is solid, false if transparent.
5556
*/
56-
public boolean get(int row, int col) {
57+
public boolean get(final int row, final int col) {
5758
if (row < 0 || col < 0 || row >= height || col >= width) return false;
5859
return data[row * width + col];
5960
}
@@ -77,10 +78,10 @@ public int getHeight() {
7778
}
7879

7980
/**
80-
* Returns the raw pixel data array for this character.
81-
* Each element corresponds to a pixel, ordered row-major.
81+
* Returns the array if what pixel is solid or transparent data for this character.
82+
* Each element corresponds to a pixel is solid or transparent.
8283
*
83-
* @return The pixel data array.
84+
* @return The solid or transparent pixel data array.
8485
*/
8586
public boolean[] getData() {
8687
return data;
@@ -106,7 +107,6 @@ public Map<String, Object> serialize() {
106107
Map<String, Object> map = new HashMap<>();
107108
map.put("height", height);
108109
map.put("width", width);
109-
// Boxed Boolean[] instead of primitive boolean[]
110110
List<Boolean> boolList = new ArrayList<>(data.length);
111111
for (boolean b : data) {
112112
boolList.add(b);

0 commit comments

Comments
 (0)