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

FastR Grid: implement L_stringMetric

parent ef25c411
No related branches found
No related tags found
No related merge requests found
......@@ -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":
......
......@@ -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.
......
/*
* 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);
}
}
......@@ -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));
......
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