Nevron Forum

Rotating NPrimitiveText

https://www.nevron.com/Forum/Topic3078.aspx

By Ted O'Grady - Saturday, February 13, 2010

I am having a trouble understanding how to rotate text. Any help would be appreciated.

I have tried using the TextStyle.Orientation and calculating its rotated bounding box, but the stretch function resizes the text and foils my attempts. The text does not rotate without stretch specified. I seem to be missing the trick to rotating text easily.

Here's example code I am using: (uses NTextShape, instead of the primitive to same effect)

private static NShape BuildRotatedStretchTextAndBoundingBox( string str, NDrawingDocument document, NBasicShapesFactory factory)
{
var myStyle = BuildOrientationTextStyle();
// show the box
var originalSize = CalculateRotatedStringSize(document, myStyle, str, 45); // simple trig
var origboundingBox = factory.CreateShape(BasicShapes.Rectangle);
origboundingBox.Bounds = new NRectangleF(new NPointF(0, 500), originalSize);
document.ActiveLayer.AddChild(origboundingBox);

// show the text
var text = new NTextShape(str, origboundingBox.Bounds);
text.Mode = BoxTextMode.Stretch;
text.Style = myStyle;
document.ActiveLayer.AddChild(text);

// show the coordinates
var origcoords = new NTextShape("Rotated and stretched OrigCoords: " + origboundingBox.Bounds, 0, 725, 400, 50);
document.ActiveLayer.AddChild(origcoords);
return origboundingBox;
}

private static NStyle BuildOrientationTextStyle()
{
var myStyle = BuildSimpleTextStyle();
myStyle.TextStyle.Orientation = -45;
return myStyle;
}

public static NSizeF CalculateRotatedStringSize(NDrawingDocument document, NStyle myStyle, string str, float rotation)
{
double radians = DegreeToRadian(rotation);
NSizeF sizeF;
document.MeasureStringInWorld(str, myStyle.TextStyle, out sizeF);
sizeF.Width = (float)((sizeF.Height * Math.Sin(radians)) + (sizeF.Width * Math.Cos(radians)));
sizeF.Height = (float)((sizeF.Height * Math.Cos(radians)) + (sizeF.Width * Math.Sin(radians)));

return sizeF;
}
By Ted O'Grady - Sunday, February 14, 2010

Doh. Must have been 30 seconds after I posted I found NTextShape.Rotate(). Works beautifully. Also exists on primitive text.

This post will at least serve as documentation to others