1 package net.mograsim.logic.model.util;
3 import net.haspamelodica.swt.helper.gcs.GeneralGC;
4 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
5 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
8 public class TextRenderingHelper
10 public static void drawTextFitting(GeneralGC gc, String string, Rectangle box, double margin, boolean isTransparent)
12 drawTextFitting(gc, string, box.x, box.y, box.width, box.height, margin, isTransparent);
15 public static void drawTextFitting(GeneralGC gc, String string, double x, double y, double width, double height, double margin,
16 boolean isTransparent)
18 Point textExtentOldFont = gc.textExtent(string);
19 Font oldFont = gc.getFont();
20 double factorX = (width - margin * 2) / textExtentOldFont.x;
21 double factorY = (height - margin * 2) / textExtentOldFont.y;
22 double factor = Math.min(factorX, factorY);
23 gc.setFont(oldFont.scale(factor));
24 Point textExtentCurrentFont = gc.textExtent(string);
25 double xOff = (width - textExtentCurrentFont.x) / 2;
26 double yOff = (height - textExtentCurrentFont.y) / 2;
27 gc.drawText(string, x + xOff, y + yOff, isTransparent);