Author: rwhitcomb Date: Thu Mar 18 16:11:51 2021 New Revision: 1887788 URL: http://svn.apache.org/viewvc?rev=1887788&view=rev Log: PIVOT-1054: Fix the drawing problem with border titles at larger font sizes.
Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraBorderSkin.java pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/BorderSkin.java Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraBorderSkin.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraBorderSkin.java?rev=1887788&r1=1887787&r2=1887788&view=diff ============================================================================== --- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraBorderSkin.java (original) +++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraBorderSkin.java Thu Mar 18 16:11:51 2021 @@ -16,7 +16,6 @@ */ package org.apache.pivot.wtk.skin.terra; -import org.apache.pivot.wtk.Theme; import org.apache.pivot.wtk.skin.BorderSkin; /** @@ -31,13 +30,11 @@ public class TerraBorderSkin extends Bor } public final void setColor(int color) { - Theme theme = currentTheme(); - setColor(theme.getColor(color)); + setColor(getColor(color)); } public final void setTitleColor(int titleColor) { - Theme theme = currentTheme(); - setTitleColor(theme.getColor(titleColor)); + setTitleColor(getColor(titleColor)); } } Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/BorderSkin.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/BorderSkin.java?rev=1887788&r1=1887787&r2=1887788&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/BorderSkin.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/BorderSkin.java Thu Mar 18 16:11:51 2021 @@ -55,24 +55,29 @@ public class BorderSkin extends Containe private CornerRadii cornerRadii; public BorderSkin() { - font = currentTheme().getFont().deriveFont(Font.BOLD); + font = getThemeFont().deriveFont(Font.BOLD); setBackgroundColor(defaultBackgroundColor()); color = defaultForegroundColor(); titleColor = defaultForegroundColor(); - thickness = topThickness = 1; - titleAscent = 0.0f; + thickness = 1; padding = Insets.NONE; cornerRadii = CornerRadii.NONE; } + private Border getBorder() { + return (Border) getComponent(); + } + @Override public void install(Component component) { super.install(component); Border border = (Border) component; border.getBorderListeners().add(this); + + calculateTitleSize(); } private int paddingThicknessWidth() { @@ -87,7 +92,7 @@ public class BorderSkin extends Containe public int getPreferredWidth(int height) { int preferredWidth = 0; - Border border = (Border) getComponent(); + Border border = getBorder(); String title = border.getTitle(); if (title != null && title.length() > 0) { @@ -115,7 +120,7 @@ public class BorderSkin extends Containe public int getPreferredHeight(int width) { int preferredHeight = 0; - Border border = (Border) getComponent(); + Border border = getBorder(); Component content = border.getContent(); if (content != null) { @@ -137,7 +142,7 @@ public class BorderSkin extends Containe int preferredWidth = 0; int preferredHeight = 0; - Border border = (Border) getComponent(); + Border border = getBorder(); String title = border.getTitle(); if (title != null && title.length() > 0) { @@ -163,7 +168,7 @@ public class BorderSkin extends Containe public int getBaseline(int width, int height) { int baseline = -1; - Border border = (Border) getComponent(); + Border border = getBorder(); // Delegate baseline calculation to the content component Component content = border.getContent(); @@ -187,7 +192,7 @@ public class BorderSkin extends Containe int width = getWidth(); int height = getHeight(); - Border border = (Border) getComponent(); + Border border = getBorder(); Component content = border.getContent(); if (content != null) { @@ -202,7 +207,7 @@ public class BorderSkin extends Containe @Override public void paint(Graphics2D graphics) { - Border border = (Border) getComponent(); + Border border = getBorder(); String title = border.getTitle(); @@ -245,8 +250,7 @@ public class BorderSkin extends Containe padding.left + thickness, (topThickness - titleBounds.getHeight()) / 2, titleBounds.getWidth() + 1, titleBounds.getHeight()); - graphics.drawString(title, (int) titleBounds.getX(), - (int) (titleBounds.getY() + titleAscent)); + graphics.drawString(title, (int) titleBounds.getX(), (int) (titleBounds.getY() + titleAscent)); Area titleClip = new Area(graphics.getClip()); titleClip.subtract(new Area(titleBounds)); @@ -274,6 +278,24 @@ public class BorderSkin extends Containe } /** + * Redo the top thickness calculation when the title, font, or thickness changes. + * <p> Caches the {@link #topThickness} and {@link #titleAscent} values for painting. + */ + private void calculateTitleSize() { + // Redo the top thickness calculation when the title changes + topThickness = thickness; + titleAscent = 0.0f; + + String title = getBorder().getTitle(); + if (title != null && title.length() > 0) { + FontRenderContext fontRenderContext = Platform.getFontRenderContext(); + LineMetrics lm = font.getLineMetrics(title, fontRenderContext); + titleAscent = lm.getAscent(); + topThickness = Math.max((int) Math.ceil(lm.getHeight()), topThickness); + } + } + + /** * @return The font used in rendering the title. */ public Font getFont() { @@ -289,6 +311,7 @@ public class BorderSkin extends Containe Utils.checkNull(font, "font"); this.font = font; + calculateTitleSize(); invalidateComponent(); } @@ -370,6 +393,9 @@ public class BorderSkin extends Containe Utils.checkNonNegative(thickness, "thickness"); this.thickness = thickness; + + calculateTitleSize(); + invalidateComponent(); } @@ -517,17 +543,7 @@ public class BorderSkin extends Containe // Border events @Override public void titleChanged(Border border, String previousTitle) { - // Redo the top thickness calculation when the title changes - topThickness = thickness; - titleAscent = 0f; - - String title = border.getTitle(); - if (title != null && title.length() > 0) { - FontRenderContext fontRenderContext = Platform.getFontRenderContext(); - LineMetrics lm = font.getLineMetrics(title, fontRenderContext); - titleAscent = lm.getAscent(); - topThickness = Math.max((int) Math.ceil(lm.getHeight()), topThickness); - } + calculateTitleSize(); invalidateComponent(); }