Commit 3970bcd1 authored by Nico Mack's avatar Nico Mack

Added doesFit method to TextStroke class.

Sector Corona omits unit if value doesn't fit Shape.
parent 013aa9ca
......@@ -103,6 +103,26 @@ public class TextStroke implements Stroke {
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* Checks whether this text stroke fits into the designated shape.
*
* @param shape
* specifies the shape to fit this text stroke into.
* @return <code>true</code> if this text stroke fits into the specified shape, <code>false</code>
* otherwise
*/
// ---------------------------------------------------------------------------
public boolean doesFit(Shape shape) {
FontRenderContext frc = new FontRenderContext(null, true, true);
GlyphVector glyphVector = font.createGlyphVector(frc, text);
double lengthOfPath = measurePathLength(shape);
double lengthOfText = glyphVector.getLogicalBounds().getWidth();
return (lengthOfText <= lengthOfPath);
}
// ---------------------------------------------------------------------------
/**
* Controls whether the text is to be flipped, i.e. rendered upside down.
......@@ -150,7 +170,7 @@ public class TextStroke implements Stroke {
if (numberOfCharacters == 0)
return result;
float lengthOfPath = measurePathLength(shape);
float lengthOfPath = (float) measurePathLength(shape);
float lengthOfText = (float) glyphVector.getLogicalBounds().getWidth();
float factor = stretchToFit ? lengthOfPath / lengthOfText : 1.0f;
......@@ -256,14 +276,14 @@ public class TextStroke implements Stroke {
* @return the length of the specified path
*/
public float measurePathLength(Shape shape) {
public double measurePathLength(Shape shape) {
PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS);
float points[] = new float[6];
float moveX = 0, moveY = 0;
float lastX = 0, lastY = 0;
float thisX = 0, thisY = 0;
int type = 0;
float total = 0;
double total = 0;
while (!it.isDone()) {
type = it.currentSegment(points);
......@@ -286,7 +306,7 @@ public class TextStroke implements Stroke {
thisY = points[1];
float dx = thisX - lastX;
float dy = thisY - lastY;
total += (float) Math.sqrt(dx * dx + dy * dy);
total += Math.sqrt(dx * dx + dy * dy);
lastX = thisX;
lastY = thisY;
break;
......
......@@ -179,7 +179,8 @@ public class Sector extends SelectableCorona {
// ---------------------------------------------------------------------------
protected void renderLabel(String newLabel) {
protected boolean renderLabel(String newLabel) {
boolean doesFit = false;
if ((newLabel != null) && (newLabel.length() > 0)) {
boolean flipped = false;
double angle = this.getAngle();
......@@ -221,10 +222,12 @@ public class Sector extends SelectableCorona {
TextStroke stroke = new TextStroke(newLabel, textFont, false, centered, false);
stroke.setFlipped(flipped);
doesFit = stroke.doesFit(labelPath);
curvedText = stroke.createStrokedShape(labelPath);
this.label = newLabel;
}
return doesFit;
}
// ---------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment