Added String preferences
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / util / TextRenderingHelper.java
1 package net.mograsim.logic.model.util;
2
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;
7
8 public class TextRenderingHelper
9 {
10         public static void drawTextFitting(GeneralGC gc, String string, Rectangle box, double margin, boolean isTransparent)
11         {
12                 drawTextFitting(gc, string, box.x, box.y, box.width, box.height, margin, isTransparent);
13         }
14
15         public static void drawTextFitting(GeneralGC gc, String string, double x, double y, double width, double height, double margin,
16                         boolean isTransparent)
17         {
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);
28                 gc.setFont(oldFont);
29         }
30 }