Skip to content
Snippets Groups Projects
Commit a0b337e0 authored by stepan's avatar stepan
Browse files

FastR Grid: more precise string height and text drawing

parent 87516f03
No related branches found
No related tags found
No related merge requests found
...@@ -185,6 +185,9 @@ public final class Unit { ...@@ -185,6 +185,9 @@ public final class Unit {
private static double convertToInches(double value, int index, int unitId, RList data, UnitConversionContext ctx, AxisOrDimension axisOrDim) { private static double convertToInches(double value, int index, int unitId, RList data, UnitConversionContext ctx, AxisOrDimension axisOrDim) {
double vpSize = ctx.getViewPortSize(axisOrDim); double vpSize = ctx.getViewPortSize(axisOrDim);
String str;
String[] lines;
double result = 0;
switch (unitId) { switch (unitId) {
case INCHES: case INCHES:
return value; return value;
...@@ -207,10 +210,20 @@ public final class Unit { ...@@ -207,10 +210,20 @@ public final class Unit {
return (value * ctx.gpar.getDrawingContext(index).getFontSize() * ctx.gpar.getDrawingContext(index).getLineHeight()) / INCH_TO_POINTS_FACTOR; return (value * ctx.gpar.getDrawingContext(index).getFontSize() * ctx.gpar.getDrawingContext(index).getLineHeight()) / INCH_TO_POINTS_FACTOR;
case STRINGWIDTH: case STRINGWIDTH:
case MYSTRINGWIDTH: case MYSTRINGWIDTH:
return ctx.device.getStringWidth(ctx.gpar.getDrawingContext(index), RRuntime.asString(data.getDataAt(0))); str = RRuntime.asString(data.getDataAt(0));
lines = str.split("\n");
for (int i = 0; i < lines.length; i++) {
result = Math.max(result, ctx.device.getStringWidth(ctx.gpar.getDrawingContext(index), lines[i]));
}
return result;
case STRINGHEIGHT: case STRINGHEIGHT:
case MYSTRINGHEIGHT: case MYSTRINGHEIGHT:
return ctx.device.getStringHeight(ctx.gpar.getDrawingContext(index), RRuntime.asString(data.getDataAt(0))); str = RRuntime.asString(data.getDataAt(0));
lines = str.split("\n");
for (int i = 0; i < lines.length; i++) {
result += ctx.device.getStringHeight(ctx.gpar.getDrawingContext(index), lines[i]);
}
return result;
case NULL: case NULL:
return evaluateNullUnit(value, vpSize, ctx.nullLayoutMode, ctx.nullArithmeticMode); return evaluateNullUnit(value, vpSize, ctx.nullLayoutMode, ctx.nullArithmeticMode);
default: default:
......
...@@ -106,13 +106,11 @@ public interface GridDevice { ...@@ -106,13 +106,11 @@ public interface GridDevice {
double getStringWidth(DrawingContext ctx, String text); double getStringWidth(DrawingContext ctx, String text);
/** /**
* Gets the height of a line of text in inches, the default implementation uses only the * Gets the height of a line of text in inches. This should include ascent and descent, i.e.
* parameters from the drawing context, but we allow the device to override this calculation * from the very bottom to the very top of the tallest letter(s). The text is guaranteed to not
* with something more precise. * contain any new lines.
*/ */
default double getStringHeight(DrawingContext ctx, String text) { double getStringHeight(DrawingContext ctx, String text);
return (ctx.getLineHeight() * ctx.getFontSize()) / INCH_TO_POINTS_FACTOR;
}
final class DeviceCloseException extends Exception { final class DeviceCloseException extends Exception {
private static final long serialVersionUID = 1182697755931636214L; private static final long serialVersionUID = 1182697755931636214L;
......
...@@ -28,6 +28,7 @@ import static java.awt.geom.Path2D.WIND_EVEN_ODD; ...@@ -28,6 +28,7 @@ import static java.awt.geom.Path2D.WIND_EVEN_ODD;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
import java.awt.Font; import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Paint; import java.awt.Paint;
import java.awt.Shape; import java.awt.Shape;
...@@ -128,7 +129,8 @@ public class Graphics2DDevice implements GridDevice { ...@@ -128,7 +129,8 @@ public class Graphics2DDevice implements GridDevice {
public void drawString(DrawingContext ctx, double leftXIn, double bottomYIn, double rotationAnticlockWise, String text) { public void drawString(DrawingContext ctx, double leftXIn, double bottomYIn, double rotationAnticlockWise, String text) {
setContextAndFont(ctx); setContextAndFont(ctx);
int leftX = transX(leftXIn); int leftX = transX(leftXIn);
int bottomY = transY(bottomYIn); FontMetrics fontMetrics = graphics.getFontMetrics(graphics.getFont());
int bottomY = transY(bottomYIn) - fontMetrics.getDescent();
transformed(leftX, bottomY, rotationAnticlockWise, () -> graphics.drawString(text, 0, 0)); transformed(leftX, bottomY, rotationAnticlockWise, () -> graphics.drawString(text, 0, 0));
} }
...@@ -152,7 +154,8 @@ public class Graphics2DDevice implements GridDevice { ...@@ -152,7 +154,8 @@ public class Graphics2DDevice implements GridDevice {
@Override @Override
public double getStringHeight(DrawingContext ctx, String text) { public double getStringHeight(DrawingContext ctx, String text) {
setContextAndFont(ctx); setContextAndFont(ctx);
int swingUnits = graphics.getFont().getSize(); FontMetrics fontMetrics = graphics.getFontMetrics(graphics.getFont());
double swingUnits = fontMetrics.getAscent() + fontMetrics.getDescent();
return swingUnits / AWT_POINTS_IN_INCH; return swingUnits / AWT_POINTS_IN_INCH;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment