Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Custom text rotation/rendering (Read 4048 times)
stefski
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 31
Location: Norway
Joined: Jul 17th, 2009
Custom text rotation/rendering
Apr 17th, 2015 at 10:08am
Print Post  
Hi Stoyan,

another request here (still working in the past with version 5.7):

We are trying to display the text of diagram nodes in a way so that it is never shown upside down. That is, when rotation angle is between 90° and 270° we want to simply subtract 180° from the rotation angle before rendering the text.

I have been looking for a setting to have the DiagramNode do this for us, but have only found an option for turning text rotation on or off.

Now I am trying to render the text myself as part of our Draw override, but as we are also utilizing the TextArea property of ShapeNode, this seems to become rather complicated - to make this work correctly, I think I need to at least "be inspired" by your text drawing source code in ShapeNode, and I find myself re-implementing a lot of code that you already have in the base class. Unfortunately some of the data I need seems to be private or internal to the ShapeNode or Shape classes, so I cannot get at it.

I am kind of hoping that upgrading to the newest version might solve this issue for us, either by built-in functionality or by more options for overriding rendering in more detail, e.g. let SvgNode render the Svg image, but let me render the text (in an easy way).

Do you have any suggestions? Will a newer version give us more control, or should I continue on my own bumpy customized rendering path?

  

Kind regards

Steffen Skov
OLGA Application Architect
Schlumberger Information Solutions AS
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Custom text rotation/rendering
Reply #1 - Apr 17th, 2015 at 12:00pm
Print Post  
Hi Steffen,

You mentioned you were moving to WPF earlier; in the WPF control there's ShapeNode.TextRotationAngle property you could use for this. We'll try to implement TextRotationAngle in next Windows Forms diagram release as well.

If you don't use polygonal layout but set text area only for positioning the text rectangle, you could override DrawLocal to flip text like this:

Code
Select All
class MyNode : ShapeNode
{
	public override void DrawLocal(IGraphics graphics, RenderOptions options)
	{
		var angle = RotationAngle;
		if (angle < 0)
			angle += 360;
		if (angle > 90 && angle < 270)
		{
			// draw standard graphics except text
			options.EnableText = false;
			base.DrawLocal(graphics, options);
			options.EnableText = true;

			// get text area's bounding rect
			var textShape = new Shape(this.Shape.TextArea, FillMode.Winding);
			var textPath = textShape.GetRenderingPath(GetLocalBounds(), 0, 0, null);
			var textBounds = textPath.GetBounds();

			var centerX = textBounds.Width / 2;
			var centerY = textBounds.Height / 2;

			// flip text vertically
			var state = graphics.Save();
			graphics.TranslateTransform(centerX, centerY);
			graphics.ScaleTransform(-1, -1);
			graphics.TranslateTransform(-centerX, -centerY);

			graphics.DrawString(Text, EffectiveFont, Brushes.Black, textBounds, TextFormat);

			graphics.Restore(state);
		}
		else
		{
			base.DrawLocal(graphics, options);
		}
	}
}

private void test()
{
	MyNode node0 = new MyNode();
	node0.Bounds = new RectangleF(30, 30, 50, 50);
	diagram.Nodes.Add(node0);

	// display text in left half
	node0.Shape = new Shape(Shapes.Rectangle.Outline, null, new[]
	{
		new LineTemplate(0, 0, 50, 0),
		new LineTemplate(50, 0, 50, 100),
		new LineTemplate(50, 100, 0, 100),
		new LineTemplate(0, 100, 0, 0)
	}, FillMode.Winding);

	node0.EnabledHandles = AdjustmentHandles.All;
	node0.RotateText = true;
	node0.Text = "node0.EnabledHandles = AdjustmentHandles.All; node0.RotateText = true;";
	node0.TextFormat.LineAlignment = StringAlignment.Center;
} 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
stefski
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 31
Location: Norway
Joined: Jul 17th, 2009
Re: Custom text rotation/rendering
Reply #2 - Apr 17th, 2015 at 12:29pm
Print Post  
Thanks you very much, Stoyan!

Good to know that we have something to look forward to when migrating to WPF. Smiley

I just took a quick look in the source code, but did not find any DrawLocal() method to override. I suppose this is a function, which has been introduced in a version later than 5.7. But I can probably do something similar with my current Draw() override - I have disabled the base class text rendering by temporarily setting the RenderOptions.EnableText to false before calling base.Draw().

Big thanks for your great help!

  

Kind regards

Steffen Skov
OLGA Application Architect
Schlumberger Information Solutions AS
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Custom text rotation/rendering
Reply #3 - Apr 17th, 2015 at 12:56pm
Print Post  
Hi Steffen,

If you override Draw, you must also call graphics.RotateTransform to draw the text rotated, and use diagram coordinates instead of local ones for the node (e.g. Bounds instead of GetLocalBounds()).

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
stefski
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 31
Location: Norway
Joined: Jul 17th, 2009
Re: Custom text rotation/rendering
Reply #4 - Apr 17th, 2015 at 1:29pm
Print Post  
Hi Stoyan,

yes, that is right - in the code I wrote before receiving your previous response I was actually doing this.

However, I wanted to use the code example you provided, in order to get the text area right for a correct positioning of the text, but I am struggling with the textShape.GetRenderingPath(), which is internal to your assembly.

I am so close to solving this now - I just need to get that text area right...
  

Kind regards

Steffen Skov
OLGA Application Architect
Schlumberger Information Solutions AS
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Custom text rotation/rendering
Reply #5 - Apr 17th, 2015 at 3:30pm
Print Post  
Hi Steffen,

GetRenderingPath is public since the 6.0.1 release. If you have the source code, feel free to change it to public in your custom v5.7 build. Alternatively you could collect Min/Max values from LineTemplate.Coordinates in area definition and call RectangleF.FromLTRB() to get the text rectangle.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
stefski
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 31
Location: Norway
Joined: Jul 17th, 2009
Re: Custom text rotation/rendering
Reply #6 - Apr 21st, 2015 at 1:03pm
Print Post  
Thank you Stoyan,

I got the data via the ElementTemplate collection (the Shape.TextArea property), did the graphics transformation setup and rendered the text inside the now rotated text bounding box. Works like a charm for our limited scenario. There might be scenarios, where one has more complex text areas, but for our application that does not apply.

Would be nice, though, to have a built-in text rotation option, which would do what we just did. Seems reasonable if people wanted their object labels to be within the -90° to +90° range, i.e. not upside down. Smiley

Again, thank you for your help!
  

Kind regards

Steffen Skov
OLGA Application Architect
Schlumberger Information Solutions AS
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint