diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/FastRGridExternalLookup.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/FastRGridExternalLookup.java index 1f297581c409dd3eb1c7e4bf0c1f3cf691fefcd5..d45d0684ae3ff62b17afed69746a9404a1d5757b 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/FastRGridExternalLookup.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/FastRGridExternalLookup.java @@ -94,6 +94,8 @@ public final class FastRGridExternalLookup { return LValidUnit.create(); case "L_pretty": return LPretty.create(); + case "L_stringMetric": + return LStringMetric.create(); // Viewport management case "L_upviewport": diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridUtils.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridUtils.java index 284a98450523ea60c5e2c57de3a91fd81070ab25..e88b9cf5a61311e6d7e7e7d1a013a1a2ff686476 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridUtils.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridUtils.java @@ -15,6 +15,8 @@ import static com.oracle.truffle.r.runtime.nmath.RMath.fmax2; import static com.oracle.truffle.r.runtime.nmath.RMath.fmin2; import com.oracle.truffle.api.nodes.ExplodeLoop; +import com.oracle.truffle.r.library.fastrGrid.device.DrawingContext; +import com.oracle.truffle.r.library.fastrGrid.device.GridDevice; import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RError.Message; import com.oracle.truffle.r.runtime.data.RAttributable; @@ -40,6 +42,16 @@ final class GridUtils { return coord - size * justification; } + static double getStringWidth(DrawingContext drawingContext, GridDevice dev, String str) { + String[] lines; + double result = 0; + lines = str.split("\n"); + for (String line : lines) { + result = Math.max(result, dev.getStringWidth(drawingContext, line)); + } + return result; + } + /** * Returns the amount of justification required. I.e. transforms the justification from value * between 0 and 1 to the value within size. diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LStringMetric.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LStringMetric.java new file mode 100644 index 0000000000000000000000000000000000000000..c4fb977c581e268f818ed9542aa4c051b360b06c --- /dev/null +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LStringMetric.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.r.library.fastrGrid; + +import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.stringValue; + +import java.util.Arrays; + +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; +import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.r.library.fastrGrid.device.GridDevice; +import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode; +import com.oracle.truffle.r.runtime.data.RDataFactory; +import com.oracle.truffle.r.runtime.data.model.RAbstractDoubleVector; +import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector; + +/** + * Returns a list with string's width, ascent and descent all in inches. + */ +public abstract class LStringMetric extends RExternalBuiltinNode.Arg1 { + static { + Casts casts = new Casts(LStringMetric.class); + casts.arg(0).mustBe(stringValue()).asStringVector(); + } + + public static LStringMetric create() { + return LStringMetricNodeGen.create(); + } + + @Specialization + @TruffleBoundary + public Object execute(RAbstractStringVector text) { + int len = text.getLength(); + + // Needs to be determined if ascent/descent are actually used by anyone without knowing the + // actual string height + double[] ascent = new double[len]; + double[] descent = new double[len]; + Arrays.fill(ascent, 0.); + Arrays.fill(descent, 0.); + + GridContext ctx = GridContext.getContext(); + GridDevice dev = ctx.getCurrentDevice(); + GPar gpar = GPar.create(ctx.getGridState().getGpar()); + double[] width = new double[len]; + for (int i = 0; i < text.getLength(); i++) { + width[i] = GridUtils.getStringWidth(gpar.getDrawingContext(i), dev, text.getDataAt(i)); + } + + return RDataFactory.createList(new Object[]{asVec(ascent), asVec(descent), asVec(width)}); + } + + private static RAbstractDoubleVector asVec(double[] data) { + return RDataFactory.createDoubleVector(data, RDataFactory.COMPLETE_VECTOR); + } +} diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/Unit.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/Unit.java index 1d6405efaf78e1942d91018099ea19e6c5df9c42..23a7f92db67eb6c30eff8b842730e90b8bbf7ab7 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/Unit.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/Unit.java @@ -213,11 +213,7 @@ public final class Unit { case STRINGWIDTH: case MYSTRINGWIDTH: str = RRuntime.asString(data.getDataAt(0)); - lines = str.split("\n"); - for (String line1 : lines) { - result = Math.max(result, ctx.device.getStringWidth(ctx.gpar.getDrawingContext(index), line1)); - } - return value * result; + return value * GridUtils.getStringWidth(ctx.gpar.getDrawingContext(index), ctx.device, str); case STRINGHEIGHT: case MYSTRINGHEIGHT: str = RRuntime.asString(data.getDataAt(0));