Author: rwhitcomb Date: Mon Feb 26 21:45:27 2018 New Revision: 1825418 URL: http://svn.apache.org/viewvc?rev=1825418&view=rev Log: PIVOT-1031: Tweak the font sizing calculations: * Do everything at drawing time, first of all, to get proper sizing based on the text, the font, and the font rendering hints. * Then do the tick mark coordinate conversion from double to int doing rounding up to get better consistency as we go around the circle. * Use only the ascent + descent as the text height instead of the full height (which also includes leading which we don't need since it is only one line of text).
Then tweak the gauge text in the demo/test program to see the font sizing better. Also, tweak the gauge label text in the KitchenSink demo and make the gauges smaller and put into two columns to fit better in the window space. Modified: pivot/trunk/tests/src/org/apache/pivot/tests/GaugeTest.java pivot/trunk/tutorials/src/org/apache/pivot/tutorials/meters.bxml pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraGaugeSkin.java Modified: pivot/trunk/tests/src/org/apache/pivot/tests/GaugeTest.java URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/GaugeTest.java?rev=1825418&r1=1825417&r2=1825418&view=diff ============================================================================== --- pivot/trunk/tests/src/org/apache/pivot/tests/GaugeTest.java (original) +++ pivot/trunk/tests/src/org/apache/pivot/tests/GaugeTest.java Mon Feb 26 21:45:27 2018 @@ -62,7 +62,7 @@ public class GaugeTest implements Applic color = warningColor; } speedGauge.getStyles().put("textColor", color); - speedGauge.setText(Integer.toString(speed) + " mph"); + speedGauge.setText(Integer.toString(speed) + " MpH"); } private void hitTheGas() { Modified: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/meters.bxml URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/meters.bxml?rev=1825418&r1=1825417&r2=1825418&view=diff ============================================================================== --- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/meters.bxml (original) +++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/meters.bxml Mon Feb 26 21:45:27 2018 @@ -46,26 +46,32 @@ limitations under the License. <BoxPane orientation="vertical" styles="{padding:6, spacing:8, horizontalAlignment:'center'}"> <Label text="Gauges" styles="{font:{bold:true}}"/> - <BoxPane orientation="vertical" styles="{spacing:6, horizontalAlignment:'center'}"> - <Gauge origin="NORTH" type="Integer" minValue="0" maxValue="100" value="50" warningLevel="70" criticalLevel="90" text="50%" - styles="{backgroundColor:10, criticalColor:23, font:{size:20}, thickness:10.0, padding:4, showTickMarks:true, tickFrequency:10}"/> - <Label text="North origin: ticks at 10"/> - </BoxPane> - <BoxPane orientation="vertical" styles="{spacing:6, horizontalAlignment:'center'}"> - <Gauge origin="EAST" type="Integer" minValue="0" maxValue="100" value="40" warningLevel="70" criticalLevel="90" text="40%" - styles="{backgroundColor:10, criticalColor:23, font:{size:20}, thickness:10.0, padding:4, showTickMarks:true, tickFrequency:25}"/> - <Label text="East origin: ticks at 25"/> - </BoxPane> - <BoxPane orientation="vertical" styles="{spacing:6, horizontalAlignment:'center'}"> - <Gauge origin="SOUTH" type="Integer" minValue="0" maxValue="100" value="75" warningLevel="70" criticalLevel="90" text="75%" - styles="{backgroundColor:10, criticalColor:23, font:{size:20}, thickness:10.0, padding:4}"/> - <Label text="South origin: no ticks, warning at 70%"/> - </BoxPane> - <BoxPane orientation="vertical" styles="{spacing:6, horizontalAlignment:'center'}"> - <Gauge origin="WEST" type="Integer" minValue="0" maxValue="100" value="95" warningLevel="70" criticalLevel="90" text="95%" - styles="{backgroundColor:10, criticalColor:23, font:{size:20}, thickness:10.0, padding:4}"/> - <Label text="West origin: no ticks, critical at 90%"/> - </BoxPane> + <GridPane columnCount="2" styles="{horizontalSpacing:4,verticalSpacing:2,showVerticalGridLines:true,showHorizontalGridLines:true}"> + <GridPane.Row> + <BoxPane orientation="vertical" styles="{spacing:6, horizontalAlignment:'center'}"> + <Gauge origin="NORTH" preferredWidth="80" preferredHeight="80" type="Integer" minValue="0" maxValue="100" value="50" warningLevel="70" criticalLevel="90" text="50%" + styles="{backgroundColor:null, criticalColor:23, font:{size:20}, thickness:10.0, padding:4, showTickMarks:true, tickFrequency:10}"/> + <Label text="North origin: ticks at 10"/> + </BoxPane> + <BoxPane orientation="vertical" styles="{spacing:6, horizontalAlignment:'center'}"> + <Gauge origin="SOUTH" preferredWidth="80" preferredHeight="80" type="Integer" minValue="0" maxValue="100" value="75" warningLevel="70" criticalLevel="90" text="75%" + styles="{backgroundColor:null, criticalColor:23, font:{size:20}, thickness:10.0, padding:4}"/> + <Label text="South origin: warning at 70%"/> + </BoxPane> + </GridPane.Row> + <GridPane.Row> + <BoxPane orientation="vertical" styles="{spacing:6, horizontalAlignment:'center'}"> + <Gauge origin="EAST" preferredWidth="80" preferredHeight="80" type="Integer" minValue="0" maxValue="100" value="40" warningLevel="70" criticalLevel="90" text="40%" + styles="{backgroundColor:null, criticalColor:23, font:{size:20}, thickness:10.0, padding:4, showTickMarks:true, tickFrequency:25}"/> + <Label text="East origin: ticks at 25"/> + </BoxPane> + <BoxPane orientation="vertical" styles="{spacing:6, horizontalAlignment:'center'}"> + <Gauge origin="WEST" preferredWidth="80" preferredHeight="80" type="Integer" minValue="0" maxValue="100" value="95" warningLevel="70" criticalLevel="90" text="95%" + styles="{backgroundColor:null, criticalColor:23, font:{size:20}, thickness:10.0, padding:4}"/> + <Label text="West origin: critical at 90%"/> + </BoxPane> + </GridPane.Row> + </GridPane> </BoxPane> <BoxPane orientation="vertical" styles="{padding:6, spacing:8}"> Modified: pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraGaugeSkin.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraGaugeSkin.java?rev=1825418&r1=1825417&r2=1825418&view=diff ============================================================================== --- pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraGaugeSkin.java (original) +++ pivot/trunk/wtk-terra/src/org/apache/pivot/wtk/skin/terra/TerraGaugeSkin.java Mon Feb 26 21:45:27 2018 @@ -65,7 +65,6 @@ public class TerraGaugeSkin<T extends Nu private Font font; private float thickness = STROKE_WIDTH; private T tickFrequency; - private float textAscent; private static final RenderingHints renderingHints = new RenderingHints(null); @@ -257,11 +256,11 @@ public class TerraGaugeSkin<T extends Nu for (int i = 0; i < numMarks; i++) { float cosAngle = (float)Math.cos(angleRadians); float sinAngle = (float)Math.sin(angleRadians); - float xInner = xCenter + (innerRadius * cosAngle); - float yInner = yCenter + (innerRadius * sinAngle); - float xOuter = xCenter + (outerRadius * cosAngle); - float yOuter = yCenter + (outerRadius * sinAngle); - graphics.drawLine((int)xInner, (int)yInner, (int)xOuter, (int)yOuter); + int xInner = (int)(xCenter + (innerRadius * cosAngle) + 0.5f); + int yInner = (int)(yCenter + (innerRadius * sinAngle) + 0.5f); + int xOuter = (int)(xCenter + (outerRadius * cosAngle) + 0.5f); + int yOuter = (int)(yCenter + (outerRadius * sinAngle) + 0.5f); + graphics.drawLine(xInner, yInner, xOuter, yOuter); angleRadians -= frequencyAngleRadians; } } @@ -269,10 +268,13 @@ public class TerraGaugeSkin<T extends Nu // Draw the text in the middle (if any) if (!Utils.isNullOrEmpty(text)) { FontRenderContext fontRenderContext = GraphicsUtilities.prepareForText(graphics, font, textColor); - + LineMetrics lm = font.getLineMetrics(text, fontRenderContext); Rectangle2D textBounds = font.getStringBounds(text, fontRenderContext); + + // Since this is only a single line, ignore the text leading in the height + double textHeight = lm.getAscent() + lm.getDescent(); double textX = x + (diameter - textBounds.getWidth()) / 2.0; - double textY = y + (diameter - textBounds.getHeight()) / 2.0 + textAscent; + double textY = y + (diameter - textHeight) / 2.0 + lm.getAscent(); graphics.drawString(text, (int) textX, (int) textY); } @@ -286,7 +288,7 @@ public class TerraGaugeSkin<T extends Nu Utils.checkNull(font, "font"); this.font = font; - invalidateComponent(); + repaintComponent(); } public final void setFont(String font) { @@ -571,12 +573,6 @@ public class TerraGaugeSkin<T extends Nu @Override public void textChanged(Gauge<T> gauge, String previousText) { - String text = gauge.getText(); - if (!Utils.isNullOrEmpty(text)) { - FontRenderContext fontRenderContext = Platform.getFontRenderContext(); - LineMetrics lm = font.getLineMetrics(text, fontRenderContext); - textAscent = lm.getAscent(); - } repaintComponent(); }