TooltipComponent implements LayoutableRenderableEntity { @VisibleForTesting static int calculateTextWidth(FontMetrics metrics, String line) { char[] chars = line.toCharArray(); int textWidth = 0; int begin = 0; boolean inTag = false; for (int j = 0; j < chars.length; j++) { if (chars[j] == '<') { textWidth += metrics.stringWidth(line.substring(begin, j)); begin = j; inTag = true; } else if (chars[j] == '>' && inTag) { String subLine = line.substring(begin + 1, j); if (subLine.startsWith("img=")) { textWidth += MOD_ICON_WIDTH; } else if (!subLine.startsWith("col=") && !subLine.startsWith("/col")) { textWidth += metrics.stringWidth(line.substring(begin, j + 1)); } begin = j + 1; inTag = false; } } textWidth += metrics.stringWidth(line.substring(begin)); return textWidth; }  @Override Dimension render(Graphics2D graphics); @Override Rectangle getBounds(); @Override void setPreferredLocation(Point position); @Override void setPreferredSize(Dimension dimension);  }
@Test public void testCalculateTextWidth() { FontMetrics fontMetics = mock(FontMetrics.class); when(fontMetics.stringWidth(anyString())).thenAnswer((invocation) -> ((String) invocation.getArguments()[0]).length()); assertEquals(11, calculateTextWidth(fontMetics, "line1<col=ff0000>>line2")); }