Search in sources :

Example 1 with TextMetrics

use of com.ait.lienzo.client.core.types.TextMetrics in project lienzo-core by ahome-it.

the class Text method measureWithIdentityTransform.

/**
 * Returns TextMetrics, which includes an approximate value for
 * height. As close as we can estimate it at this time.
 *
 * @param context
 * @return TextMetric or null if the text is empty or null
 */
public TextMetrics measureWithIdentityTransform(final Context2D context) {
    context.save();
    context.setToIdentityTransform();
    final TextMetrics size = measure(context);
    context.restore();
    return size;
}
Also used : TextMetrics(com.ait.lienzo.client.core.types.TextMetrics)

Example 2 with TextMetrics

use of com.ait.lienzo.client.core.types.TextMetrics in project drools-wb by kiegroup.

the class BaseGuidedDecisionTreeNodeFactory method getDragProxyHeight.

private double getDragProxyHeight(final Text nodeLabel) {
    final LienzoPanel panel = new LienzoPanel(100, 100);
    final Layer layer = new Layer();
    panel.add(layer);
    final TextMetrics tm = nodeLabel.measure(layer.getContext());
    return Math.max(getHeight(), tm.getHeight());
}
Also used : LienzoPanel(com.ait.lienzo.client.widget.LienzoPanel) TextMetrics(com.ait.lienzo.client.core.types.TextMetrics) Layer(com.ait.lienzo.client.core.shape.Layer)

Example 3 with TextMetrics

use of com.ait.lienzo.client.core.types.TextMetrics in project lienzo-core by ahome-it.

the class Text method measure.

/**
 * Returns TextMetrics, which includes an approximate value for
 * height. As close as we can estimate it at this time.
 *
 * @param context
 * @return TextMetric or null if the text is empty or null
 */
public TextMetrics measure(final Context2D context) {
    final String text = getText();
    final double size = getFontSize();
    if ((null == text) || (text.isEmpty()) || (false == (size > 0))) {
        return TextMetrics.make(0, 0);
    }
    context.save();
    context.setTextAlign(TextAlign.LEFT);
    context.setTextBaseline(TextBaseLine.ALPHABETIC);
    context.setTextFont(TextUtils.getFontString(size, getTextUnit(), getFontStyle(), getFontFamily()));
    double width = getStrokeWidth();
    if (width == 0) {
        width = 1;
    }
    context.setStrokeWidth(width);
    context.transform(getAbsoluteTransform());
    final TextMetrics meas = context.measureText(text);
    final double height = context.measureText("M").getWidth();
    meas.setHeight(height - (height / 6));
    context.restore();
    return meas;
}
Also used : TextMetrics(com.ait.lienzo.client.core.types.TextMetrics)

Example 4 with TextMetrics

use of com.ait.lienzo.client.core.types.TextMetrics in project lienzo-core by ahome-it.

the class Text method fill.

@Override
protected boolean fill(final Context2D context, final Attributes attr, double alpha) {
    final boolean filled = attr.hasFill();
    if ((filled) || (attr.isFillShapeForSelection())) {
        alpha = alpha * attr.getFillAlpha();
        if (alpha <= 0) {
            return false;
        }
        if (context.isSelection()) {
            final String color = getColorKey();
            if (null == color) {
                return false;
            }
            context.save();
            if (GRADFILLS) {
                final TextMetrics size = measureWithIdentityTransform(context);
                if (null != size) {
                    final double wide = size.getWidth();
                    final double high = size.getHeight();
                    drawString(context, attr, new IDrawString() {

                        @Override
                        public void draw(final Context2D context, final String s, final double xOffset, final double lineNum) {
                            context.fillTextWithGradient(s, xOffset, getLineHeight(context) * lineNum, 0, 0, wide + (wide / 6), high + (high / 6), color);
                        }
                    });
                } else {
                    final Layer layer = getLayer();
                    drawString(context, attr, new IDrawString() {

                        @Override
                        public void draw(final Context2D context, final String s, final double xOffset, final double lineNum) {
                            context.fillTextWithGradient(s, xOffset, getLineHeight(context) * lineNum, 0, 0, layer.getWidth(), layer.getHeight(), color);
                        }
                    });
                }
            } else {
                context.setFillColor(color);
                drawString(context, attr, FILL);
            }
            context.restore();
            return true;
        }
        if (false == filled) {
            return false;
        }
        context.save();
        if (attr.hasShadow()) {
            doApplyShadow(context, attr);
        }
        context.setGlobalAlpha(alpha);
        final String fill = attr.getFillColor();
        if (null != fill) {
            context.setFillColor(fill);
            drawString(context, attr, FILL);
            context.restore();
            return true;
        } else {
            final FillGradient grad = attr.getFillGradient();
            if (null != grad) {
                final String type = grad.getType();
                if (LinearGradient.TYPE.equals(type)) {
                    context.setFillGradient(grad.asLinearGradient());
                    drawString(context, attr, FILL);
                    context.restore();
                    return true;
                } else if (RadialGradient.TYPE.equals(type)) {
                    context.setFillGradient(grad.asRadialGradient());
                    drawString(context, attr, FILL);
                    context.restore();
                    return true;
                } else if (PatternGradient.TYPE.equals(type)) {
                    context.setFillGradient(grad.asPatternGradient());
                    drawString(context, attr, FILL);
                    context.restore();
                    return true;
                }
            }
        }
        context.restore();
    }
    return false;
}
Also used : Context2D(com.ait.lienzo.client.core.Context2D) TextMetrics(com.ait.lienzo.client.core.types.TextMetrics) FillGradient(com.ait.lienzo.client.core.types.FillGradient)

Example 5 with TextMetrics

use of com.ait.lienzo.client.core.types.TextMetrics in project lienzo-core by ahome-it.

the class Movie method getTextBestFit.

private final String getTextBestFit(final Context2D context, final String text, final int wide) {
    double pt = LienzoCore.get().getDefaultFontSize();
    final String st = LienzoCore.get().getDefaultFontStyle();
    final String fm = LienzoCore.get().getDefaultFontFamily();
    String tf = TextUtils.getFontString(pt, TextUnit.PT, st, fm);
    context.save();
    context.setToIdentityTransform();
    while (true) {
        context.setTextFont(tf);
        final TextMetrics tm = context.measureText(text);
        if (tm.getWidth() < wide) {
            break;
        }
        pt = pt - 2;
        if (pt < 6) {
            break;
        }
        tf = TextUtils.getFontString(pt, TextUnit.PT, st, fm);
    }
    context.restore();
    return tf;
}
Also used : TextMetrics(com.ait.lienzo.client.core.types.TextMetrics)

Aggregations

TextMetrics (com.ait.lienzo.client.core.types.TextMetrics)7 Layer (com.ait.lienzo.client.core.shape.Layer)3 LienzoPanel (com.ait.lienzo.client.widget.LienzoPanel)3 Context2D (com.ait.lienzo.client.core.Context2D)1 FillGradient (com.ait.lienzo.client.core.types.FillGradient)1 Point2D (com.ait.lienzo.client.core.types.Point2D)1