List of usage examples for com.google.gwt.canvas.dom.client Context2d measureText
public final native TextMetrics measureText(String text) ;
From source file:com.sencha.gxt.chart.client.draw.engine.Canvas2d.java
License:sencha.com license
@Override protected PreciseRectangle getBBoxText(TextSprite sprite) { Context2d ctx = getContext(); ctx.setFont(sprite.getFontSize() + "px " + sprite.getFont()); TextMetrics text = ctx.measureText(sprite.getText()); //TODO real height return new PreciseRectangle(sprite.getX(), sprite.getY(), text.getWidth(), sprite.getFontSize() * 4 / 3); }
From source file:edu.umb.jsPedigrees.client.Pelican.PelicanLines.java
License:Open Source License
public static Canvas drawLines(AbsolutePanel panel) { Canvas canvas = Canvas.createIfSupported(); Context2d ctx = canvas.getContext2d(); canvas.setCoordinateSpaceHeight(panel.getOffsetHeight()); canvas.setCoordinateSpaceWidth(panel.getOffsetWidth()); ctx.setStrokeStyle(CssColor.make("0,0,0")); ctx.setLineWidth(1.0f);/*from ww w . j a v a 2 s.com*/ ctx.setFont("12px sans-serif"); int fontHeight = 15; int fontAscent = 15; int dropSize = Math.max(2, Math.min(PelicanPerson.symbolSize / 2, 3 * (PelicanPerson.ySpace - PelicanPerson.symbolSize - fontHeight) / 4)); for (int i = 0; i < panel.getWidgetCount(); i++) if (panel.getWidget(i) instanceof PelicanPerson) { // draw a line from this person to its parents PelicanPerson person = (PelicanPerson) panel.getWidget(i); if (person.father != null && person.mother != null) { //System.out.println("HERE "+String.valueOf(i)); // find the mother and father PelicanPerson father = person.father; PelicanPerson mother = person.mother; if (father != null && mother != null) { // line between parents int fatherX = panel.getWidgetLeft(father) + ((panel.getWidgetLeft(father) < panel.getWidgetLeft(mother)) ? PelicanPerson.symbolSize : 0); int motherX = panel.getWidgetLeft(mother) + ((panel.getWidgetLeft(mother) < panel.getWidgetLeft(father)) ? PelicanPerson.symbolSize : 0); int fatherY = panel.getWidgetTop(father) + PelicanPerson.symbolSize / 2; int motherY = panel.getWidgetTop(mother) + PelicanPerson.symbolSize / 2; int leftX = fatherX; int leftY = fatherY; int rightX = motherX; int rightY = motherY; if (motherX < fatherX) { leftX = motherX; leftY = motherY; rightX = fatherX; rightY = fatherY; } int gap = PelicanPerson.xSpace - PelicanPerson.symbolSize; // see if any subjects lie between the father and mother if (!adjacent(panel, father, mother) && father.generation == mother.generation) { // draw lines which avoid other symbols // g2.drawLine(leftX,leftY,leftX+gap/4,leftY); ctx.beginPath(); ctx.moveTo(leftX, leftY); ctx.lineTo(leftX + gap / 4, leftY); ctx.closePath(); ctx.stroke(); // g2.drawLine(rightX,rightY,rightX-gap/2,rightY); ctx.beginPath(); ctx.moveTo(rightX, rightY); ctx.lineTo(rightX - gap / 2, rightY); ctx.closePath(); ctx.stroke(); leftX += gap / 4; rightX -= gap / 2; // g2.drawLine(leftX,leftY,leftX,leftY-(PelicanPerson.symbolSize+dropSize)/2); ctx.beginPath(); ctx.moveTo(leftX, leftY); ctx.lineTo(leftX, leftY - (PelicanPerson.symbolSize + dropSize) / 2); ctx.closePath(); ctx.stroke(); // g2.drawLine(rightX,rightY,rightX,rightY-(PelicanPerson.symbolSize+dropSize)/2); ctx.beginPath(); ctx.moveTo(rightX, rightY); ctx.lineTo(rightX, rightY - (PelicanPerson.symbolSize + dropSize) / 2); ctx.closePath(); ctx.stroke(); leftY -= (PelicanPerson.symbolSize + dropSize) / 2; rightY -= (PelicanPerson.symbolSize + dropSize) / 2; } // g2.drawLine(leftX,leftY,rightX,rightY); ctx.beginPath(); ctx.moveTo(leftX, leftY); ctx.lineTo(rightX, rightY); ctx.closePath(); ctx.stroke(); // line up from child // g2.drawLine(person.getX()+PelicanPerson.symbolSize/2,person.getY(),person.getX()+PelicanPerson.symbolSize/2,person.getY()-dropSize); ctx.beginPath(); ctx.moveTo(panel.getWidgetLeft(person) + PelicanPerson.symbolSize / 2, panel.getWidgetTop(person)); ctx.lineTo(panel.getWidgetLeft(person) + PelicanPerson.symbolSize / 2, panel.getWidgetTop(person) - dropSize); ctx.closePath(); ctx.stroke(); // line across from child // try to attach to an orphan parent int parentX = fatherX; if (father.isOrphan() || mother.isOrphan()) { parentX = Math.max(fatherX, motherX) - gap / 2; } else { // if no orphan parents, go straight up from // middle laid out sib int nsib = 0; for (int j = 0; j < panel.getWidgetCount(); j++) if (panel.getWidget(j) instanceof PelicanPerson) { PelicanPerson sib = (PelicanPerson) panel.getWidget(j); if (areSibs(person, sib)) nsib++; } int sibs = 0; for (int j = 0; j < panel.getWidgetCount() && sibs <= nsib / 2; j++) if (panel.getWidget(j) instanceof PelicanPerson) { PelicanPerson sib = (PelicanPerson) panel.getWidget(j); if (areSibs(person, sib)) sibs++; parentX = panel.getWidgetLeft(sib) + PelicanPerson.symbolSize / 2; } if (nsib > 1 && nsib % 2 == 0) parentX -= PelicanPerson.xSpace / 2; if (parentX <= leftX) parentX = leftX + PelicanPerson.symbolSize / 2; if (parentX >= rightX) parentX = rightX - PelicanPerson.symbolSize / 2; } // g2.drawLine(person.getX()+PelicanPerson.symbolSize/2,person.getY()-dropSize,parentX,person.getY()-dropSize); ctx.beginPath(); ctx.moveTo(panel.getWidgetLeft(person) + PelicanPerson.symbolSize / 2, panel.getWidgetTop(person) - dropSize); ctx.lineTo(parentX, panel.getWidgetTop(person) - dropSize); ctx.closePath(); ctx.stroke(); // line up to parents // Draw a vertical line up to the line joining the parents // if this happens to be not between the parents, // change it to a line to the midpoint between the parents int parentY = (rightX != leftX) ? leftY + (rightY - leftY) * (parentX - leftX) / (rightX - leftX) : (leftY + rightY) / 2; if (rightX == leftX || parentY > Math.max(leftY, rightY) || parentY < Math.min(leftY, rightY)) { // g2.drawLine(parentX,person.getY()-dropSize, (leftX+rightX)/2,(leftY+rightY)/2); ctx.beginPath(); ctx.moveTo(parentX, panel.getWidgetTop(person) - dropSize); ctx.lineTo((leftX + rightX) / 2, (leftY + rightY) / 2); ctx.closePath(); ctx.stroke(); } else { // g2.drawLine(parentX,person.getY()-dropSize,parentX,parentY); ctx.beginPath(); ctx.moveTo(parentX, panel.getWidgetTop(person) - dropSize); ctx.lineTo(parentX, parentY); ctx.closePath(); ctx.stroke(); } } } // write out id int verticalPosn = panel.getWidgetTop(person) + PelicanPerson.symbolSize + fontAscent; String idString = String.valueOf(person.id); int fontWidth = (int) ctx.measureText(idString).getWidth(); // g2.drawString(idString, // person.getX() + PelicanPerson.symbolSize/2 - fontWidth/2, // verticalPosn); ctx.fillText(idString, panel.getWidgetLeft(person) + PelicanPerson.symbolSize / 2 - fontWidth / 2, verticalPosn); verticalPosn += fontAscent; } return canvas; }
From source file:jetbrains.jetpad.projectional.domUtil.TextMetricsCalculator.java
License:Apache License
static double calculateWidth(Font font, String text) { Canvas canvas = canvas();// w w w. j a v a2 s. c o m Context2d ctx = canvas.getContext2d(); ctx.setFont(getFontString(font)); return ctx.measureText(normalize(text)).getWidth(); }
From source file:jetbrains.jetpad.projectional.domUtil.TextMetricsCalculator.java
License:Apache License
private static int measureHeight(Font font, String text) { Canvas canvas = canvas();/* w w w .j a v a 2 s . c o m*/ Context2d ctx = canvas.getContext2d(); ctx.setFont(getFontString(font)); ctx.setFillStyle("rgb(255, 0, 0)"); int width = (int) ctx.measureText(text).getWidth(); int canvasHeight = font.getSize() * 2; canvas.setHeight(canvasHeight + "px"); canvas.setHeight(font.getSize() * 2 + "px"); canvas.setWidth(width + "px"); ctx.fillText(text, 0, font.getSize()); ImageData data = ctx.getImageData(0, 0, width, canvasHeight); int firstY = canvasHeight - 1; int lastY = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < canvasHeight; y++) { int red = data.getRedAt(x, y); if (red != 0) { if (firstY > y) { firstY = y; } if (lastY < y) { lastY = y; } } } } return lastY - firstY; }
From source file:org.primaresearch.web.gwt.client.ui.page.renderer.PageRenderer.java
License:Apache License
public int measureTextWidth(String text, String font) { Context2d context = canvas.getContext2d(); context.setFont(font);/* w w w .j av a2 s . c o m*/ return (int) context.measureText(text).getWidth(); }
From source file:playn.html.HtmlTextLayout.java
License:Apache License
HtmlTextLayout(Context2d ctx, String text, TextFormat format) { Font font = getFont(format);// ww w .j av a 2 s .co m this.format = format; this.metrics = ((HtmlGraphics) graphics()).getFontMetrics(font); configContext(ctx); // normalize newlines in the text (Windows: CRLF -> LF, Mac OS pre-X: CR -> LF) text = text.replace("\r\n", "\n").replace('\r', '\n'); if (format.shouldWrap() || text.indexOf('\n') != -1) { for (String line : text.split("\\n")) { String[] words = line.split("\\s"); // TODO: preserve intra-line whitespace for (int idx = 0; idx < words.length;) { // note: measureLine has the side effect of adding the measured line to this.lines and // setting this.width to the maximum of the current width and the measured line width idx = measureLine(ctx, words, idx); } } height = metrics.height * lines.size(); } else { width = (float) ctx.measureText(text).getWidth(); height = metrics.height; lines.add(new Line(text, width)); } // Canvas.measureText does not account for the extra width consumed by italic characters, so we // fudge in a fraction of an em and hope the font isn't too slanted switch (font.style()) { case ITALIC: width += metrics.emwidth / 8; break; case BOLD_ITALIC: width += metrics.emwidth / 6; break; } }
From source file:playn.html.HtmlTextLayout.java
License:Apache License
int measureLine(Context2d ctx, String[] words, int idx) { // we always put at least one word on a line String line = words[idx++];/* ww w.j a va2 s. c o m*/ int startIdx = idx; // build a rough estimate line based on character count and emwidth for (; idx < words.length; idx++) { String nline = line + " " + words[idx]; if (nline.length() * metrics.emwidth > format.wrapWidth) break; line = nline; } // now, based on exact measurements, either add more words... double lineWidth = ctx.measureText(line).getWidth(); if (lineWidth < format.wrapWidth) { for (; idx < words.length; idx++) { String nline = line + " " + words[idx]; double nlineWidth = ctx.measureText(nline).getWidth(); if (nlineWidth > format.wrapWidth) break; line = nline; lineWidth = nlineWidth; } } // or pop words off... while (lineWidth > format.wrapWidth && idx > (startIdx + 1)) { // don't pop off the last word line = line.substring(0, line.length() - words[--idx].length() - 1); lineWidth = ctx.measureText(line).getWidth(); } // finally, if we're still over the limit (we have a single looong word), hard break if (lineWidth > format.wrapWidth) { StringBuilder remainder = new StringBuilder(); while (lineWidth > format.wrapWidth && line.length() > 1) { // this could be more efficient, but this edge case should be rare enough not to matter int lastIdx = line.length() - 1; remainder.insert(0, line.charAt(lastIdx)); line = line.substring(0, lastIdx); lineWidth = ctx.measureText(line).getWidth(); } words[--idx] = remainder.toString(); } lines.add(new Line(line, (float) lineWidth)); width = (float) Math.max(width, lineWidth); return idx; }
From source file:playn.html.OldHtmlTextLayout.java
License:Apache License
OldHtmlTextLayout(Context2d ctx, String text, TextFormat format) { super(text, format); HtmlFont font = getFont(format);// w w w. j a v a 2 s.c om this.metrics = ((HtmlGraphics) graphics()).getFontMetrics(font); configContext(ctx); // normalize newlines in the text (Windows: CRLF -> LF, Mac OS pre-X: CR -> LF) text = text.replace("\r\n", "\n").replace('\r', '\n'); if (format.shouldWrap() || text.indexOf('\n') != -1) { for (String line : text.split("\\n")) { String[] words = line.split("\\s"); // TODO: preserve intra-line whitespace for (int idx = 0; idx < words.length;) { // note: measureLine has the side effect of adding the measured line to this.lines and // setting this.width to the maximum of the current width and the measured line width idx = measureLine(ctx, words, idx); } } height = metrics.height * lines.size(); } else { width = (float) ctx.measureText(text).getWidth(); height = metrics.height; lines.add(new Line(text, width)); } // Canvas.measureText does not account for the extra width consumed by italic characters, so we // fudge in a fraction of an em and hope the font isn't too slanted switch (font.style()) { case ITALIC: width += metrics.emwidth / 8; break; case BOLD_ITALIC: width += metrics.emwidth / 6; break; default: break; // nada } }