Skip to content

Commit

Permalink
Rewrite shadow and gradient drawing for Unicode font
Browse files Browse the repository at this point in the history
The shadows should work for Mac now.
The gradients should always match up with the base font's gradient
  • Loading branch information
danielduner committed Feb 27, 2012
1 parent fda8cf1 commit d6dc4e6
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions src/com/mojang/mojam/gui/FontCharacterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,36 @@ public Bitmap getFontCharacter(char character) {
int fontSize = systemFont.getSize();
int width = 3*fontSize;
int height = 3*fontSize;

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
graphics.setFont(systemFont);

int positionX = fontSize;
int positionY = 2*fontSize;
if(shadowColor != null) {
graphics.setColor(shadowColor);
graphics.drawString(Character.toString(character), positionX+1, positionY+1);
}

Color mainLetterColor;
if(shadowColor!=Color.MAGENTA) { // Any color will do, as long as it's different from the shadow color
mainLetterColor = Color.MAGENTA;
} else {
mainLetterColor = Color.YELLOW;
BufferedImage mainImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D mainGraphics = mainImage.createGraphics();
mainGraphics.setFont(systemFont);
Color mainLetterColor = Color.MAGENTA;
mainGraphics.setColor(mainLetterColor);
mainGraphics.drawString(Character.toString(character), positionX, positionY);

BufferedImage shadowImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
if (shadowColor != null) {
Graphics2D shadowGraphics = shadowImage.createGraphics();
shadowGraphics.setFont(systemFont);
shadowGraphics.setColor(shadowColor);
shadowGraphics.drawString(Character.toString(character), positionX+1, positionY+1);
}
graphics.setColor(mainLetterColor);
graphics.drawString(Character.toString(character), positionX, positionY);


int[][] pixels = new int[width][height];
for (int y = 0; y < height; y++) {
int gradientRow = gradient.length - 1;
for (int y = height-1; y >= 0; y--) {
for (int x = 0; x < width; x++) {
pixels[x][y] = image.getRGB(x, y);
if (mainImage.getRGB(x, y) == 0) {
pixels[x][y] = shadowImage.getRGB(x, y);
} else {
pixels[x][y] = gradient[gradientRow].getRGB();
}
}
if (y < positionY) {
gradientRow = Math.max(gradientRow - 1, 0);
}
}

Expand All @@ -78,19 +83,9 @@ public Bitmap getFontCharacter(char character) {
}
height = pixels[0].length;

int row = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (pixels[x][y] != 0 && (shadowColor==null || pixels[x][y] != shadowColor.getRGB())) {
pixels[x][y] = gradient[row].getRGB();
}
}
row = Math.min(row + 1, gradient.length - 1);
}

Bitmap characterBitmap = new Bitmap(pixels);
characterCache.put(character, characterBitmap);

return characterBitmap;
}

Expand Down

0 comments on commit d6dc4e6

Please sign in to comment.