correct font positioning to work with any font

This commit is contained in:
l2-
2018-10-22 17:20:51 +02:00
parent 77e8933dbc
commit df7cf901e6
11 changed files with 31 additions and 20 deletions

View File

@@ -373,7 +373,7 @@ class DevToolsOverlay extends Overlay
Rectangle2D textBounds = fm.getStringBounds(idText, graphics);
int textX = (int) (slotBounds.getX() + (slotBounds.getWidth() / 2) - (textBounds.getWidth() / 2));
int textY = (int) (slotBounds.getY() + (slotBounds.getHeight() / 2) + (textBounds.getHeight() / 2));
int textY = (int) (slotBounds.getY() + (slotBounds.getHeight() / 2) + (fm.getAscent() / 2));
graphics.setColor(new Color(255, 255, 255, 65));
graphics.fill(slotBounds);
@@ -483,7 +483,7 @@ class DevToolsOverlay extends Overlay
Rectangle2D textBounds = fm.getStringBounds(text, graphics);
int textX = (int) (bounds.getX() + (bounds.getWidth() / 2) - (textBounds.getWidth() / 2));
int textY = (int) (bounds.getY() + (bounds.getHeight() / 2) + (textBounds.getHeight() / 2));
int textY = (int) (bounds.getY() + (bounds.getHeight() / 2) + (fm.getMaxAscent() / 2));
graphics.setColor(Color.BLACK);
graphics.drawString(text, textX + 1, textY + 1);

View File

@@ -284,14 +284,14 @@ public class GroundItemsOverlay extends Overlay
// Item bounds
int x = textX - 2;
int y = textY - stringHeight - 2;
int y = textY - stringHeight - 2 + fm.getMaxDescent();
int width = stringWidth + 4;
int height = stringHeight + 4;
final Rectangle itemBounds = new Rectangle(x, y, width, height);
// Hidden box
x += width + 2;
y = textY - (RECTANGLE_SIZE + stringHeight) / 2;
y = textY - (fm.getMaxAscent() + RECTANGLE_SIZE) / 2;
width = height = RECTANGLE_SIZE;
final Rectangle itemHiddenBox = new Rectangle(x, y, width, height);

View File

@@ -109,7 +109,7 @@ class ItemChargeOverlay extends Overlay
final Rectangle bounds = item.getCanvasBounds();
final TextComponent textComponent = new TextComponent();
textComponent.setPosition(new Point(bounds.x, bounds.y + 16));
textComponent.setPosition(new Point(bounds.x, bounds.y + 1 + graphics.getFontMetrics().getMaxAscent() - graphics.getFontMetrics().getMaxDescent()));
textComponent.setText(charges < 0 ? "?" : String.valueOf(charges));
textComponent.setColor(itemChargePlugin.getColor(charges));
textComponent.render(graphics);

View File

@@ -79,7 +79,7 @@ public class BindNeckOverlay extends Overlay
final String text = bindingCharges <= 0 ? "?" : bindingCharges + "";
final TextComponent textComponent = new TextComponent();
textComponent.setPosition(new Point(bounds.x, bounds.y + 16));
textComponent.setPosition(new Point(bounds.x, bounds.y + 1 + graphics.getFontMetrics().getMaxAscent() - graphics.getFontMetrics().getMaxDescent()));
textComponent.setText(text);
textComponent.setColor(color);
textComponent.render(graphics);

View File

@@ -104,6 +104,10 @@ public class RunepouchOverlay extends Overlay
StringBuilder tooltipBuilder = new StringBuilder();
// location.getY() + graphics.getFontMetrics().getMaxAscent() - graphics.getFontMetrics().getMaxDescent()
// this will draw the character exactly on the border
int yLocation = location.getY() + 1 +
graphics.getFontMetrics().getMaxAscent() - graphics.getFontMetrics().getMaxDescent();
for (int i = 0; i < AMOUNT_VARBITS.length; i++)
{
Varbits amountVarbit = AMOUNT_VARBITS[i];
@@ -133,13 +137,18 @@ public class RunepouchOverlay extends Overlay
continue;
}
// the reason this is not split up in maxascent and maxdescent to equal the height of the text like it should
// be is because numbers (afaik) dont use font descent so a 1 pixel seperator should be good and give
// consistent results across fonts
int yOffset = (1 + (graphics.getFontMetrics().getMaxAscent()) * i);
graphics.setColor(Color.black);
graphics.drawString("" + formatNumber(amount), location.getX() + (config.showIcons() ? 13 : 6),
location.getY() + 14 + (graphics.getFontMetrics().getHeight() - 1) * i);
yLocation + yOffset);
graphics.setColor(config.fontColor());
graphics.drawString("" + formatNumber(amount), location.getX() + (config.showIcons() ? 12 : 5),
location.getY() + 13 + (graphics.getFontMetrics().getHeight() - 1) * i);
yLocation + yOffset);
if (!config.showIcons())
{
@@ -150,7 +159,7 @@ public class RunepouchOverlay extends Overlay
if (image != null)
{
OverlayUtil.renderImageLocation(graphics,
new Point(location.getX(), location.getY() + graphics.getFontMetrics().getHeight() * i),
new Point(location.getX(), location.getY() + (1 + graphics.getFontMetrics().getMaxAscent()) * i),
image);
}
}

View File

@@ -39,7 +39,7 @@ import net.runelite.client.ui.FontManager;
@Setter
public class InfoBoxComponent implements LayoutableRenderableEntity
{
private static final int SEPARATOR = 3;
private static final int SEPARATOR = 2;
private static final int DEFAULT_SIZE = 32;
@Getter
@@ -92,7 +92,7 @@ public class InfoBoxComponent implements LayoutableRenderableEntity
final TextComponent textComponent = new TextComponent();
textComponent.setColor(color);
textComponent.setText(text);
textComponent.setPosition(new Point(baseX + ((size - metrics.stringWidth(text)) / 2), baseY + size - SEPARATOR));
textComponent.setPosition(new Point(baseX + ((size - metrics.stringWidth(text)) / 2), baseY + size - metrics.getMaxDescent() - SEPARATOR));
textComponent.render(graphics);
}

View File

@@ -68,7 +68,7 @@ public class LineComponent implements LayoutableRenderableEntity
final FontMetrics metrics = graphics.getFontMetrics();
final int baseX = preferredLocation.x;
final int baseY = preferredLocation.y + metrics.getHeight();
final int baseY = preferredLocation.y;
int x = baseX;
int y = baseY;
final int leftFullWidth = getLineWidth(left, metrics);
@@ -92,6 +92,7 @@ public class LineComponent implements LayoutableRenderableEntity
for (int i = 0; i < lineCount; i++)
{
y += metrics.getMaxAscent();
String leftText = "";
String rightText = "";
@@ -116,7 +117,7 @@ public class LineComponent implements LayoutableRenderableEntity
rightLineComponent.setText(rightText);
rightLineComponent.setColor(rightColor);
rightLineComponent.render(graphics);
y += metrics.getHeight();
y += metrics.getMaxDescent();
}
final Dimension dimension = new Dimension(preferredSize.width, y - baseY);
@@ -124,6 +125,7 @@ public class LineComponent implements LayoutableRenderableEntity
bounds.setSize(dimension);
return dimension;
}
y += metrics.getMaxAscent();
final TextComponent leftLineComponent = new TextComponent();
leftLineComponent.setPosition(new Point(x, y));
@@ -136,7 +138,7 @@ public class LineComponent implements LayoutableRenderableEntity
rightLineComponent.setText(right);
rightLineComponent.setColor(rightColor);
rightLineComponent.render(graphics);
y += metrics.getHeight();
y += metrics.getMaxDescent();
final Dimension dimension = new Dimension(preferredSize.width, y - baseY);
bounds.setLocation(preferredLocation);

View File

@@ -84,7 +84,7 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
final int width = preferredSize.width;
final int height = Math.max(preferredSize.height, 16);
final int progressTextX = barX + (width - metrics.stringWidth(textToWrite)) / 2;
final int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getHeight();
final int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getMaxAscent();
final int progressFill = (int) (width * Math.min(1, pc));
// Draw bar

View File

@@ -64,7 +64,7 @@ public class TitleComponent implements LayoutableRenderableEntity
titleComponent.setColor(color);
titleComponent.setPosition(new Point(
baseX + ((preferredSize.width - metrics.stringWidth(text)) / 2),
baseY + metrics.getHeight()));
baseY + metrics.getMaxAscent()));
final Dimension rendered = titleComponent.render(graphics);
final Dimension dimension = new Dimension(preferredSize.width, rendered.height);
bounds.setLocation(preferredLocation);

View File

@@ -104,7 +104,7 @@ public class TooltipComponent implements RenderableEntity
textComponent.setColor(nextColor);
String text = line.substring(begin, j);
textComponent.setText(text);
textComponent.setPosition(new Point(lineX, textY + (i + 1) * textHeight - textDescent));
textComponent.setPosition(new Point(lineX, textY + (i + 1) * metrics.getMaxAscent() + i * metrics.getMaxDescent()));
textComponent.render(graphics);
lineX += metrics.stringWidth(text);
@@ -141,7 +141,7 @@ public class TooltipComponent implements RenderableEntity
textComponent.setColor(nextColor);
String text = line.substring(begin, j + 1);
textComponent.setText(text);
textComponent.setPosition(new Point(lineX, textY + (i + 1) * textHeight - textDescent));
textComponent.setPosition(new Point(lineX, textY + (i + 1) * metrics.getMaxAscent() + i * metrics.getMaxDescent()));
textComponent.render(graphics);
lineX += metrics.stringWidth(text);
@@ -155,7 +155,7 @@ public class TooltipComponent implements RenderableEntity
final TextComponent textComponent = new TextComponent();
textComponent.setColor(nextColor);
textComponent.setText(line.substring(begin, line.length()));
textComponent.setPosition(new Point(lineX, textY + (i + 1) * textHeight - textDescent));
textComponent.setPosition(new Point(lineX, textY + (i + 1) * metrics.getMaxAscent() + i * metrics.getMaxDescent()));
textComponent.render(graphics);
}

View File

@@ -277,7 +277,7 @@ public class WorldMapOverlay extends Overlay
graphics.setColor(JagexColors.TOOLTIP_BORDER);
graphics.drawRect((int) tooltipRect.getX(), (int) tooltipRect.getY(), (int) tooltipRect.getWidth(), (int) tooltipRect.getHeight());
graphics.setColor(JagexColors.TOOLTIP_TEXT);
graphics.drawString(tooltip, drawPoint.getX(), drawPoint.getY() + height);
graphics.drawString(tooltip, drawPoint.getX(), drawPoint.getY() + fm.getMaxAscent());
}
private Point clipToRectangle(Point drawPoint, Rectangle mapDisplayRectangle)