Page Index Toggle Pages: [1] 2  Send TopicPrint
Hot Topic (More than 10 Replies) Need custom font size when text is trim (Read 9340 times)
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Need custom font size when text is trim
Dec 21st, 2009 at 12:11pm
Print Post  
Hi Stoyan,

For ShapeNodes in our diagram we are giving some textstyle for text to trim it while it is larger than its specified text area.
When text does not fit in the node’s text area and trimming, we want to decrease font size so that it gets fit but only to an extent i.e. say decrease max. up to 3 points from current text font baseline.
Is there any property by which we can achieve this?

Please suggest.

Regards,
Anshul
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Need custom font size when text is trim
Reply #1 - Dec 21st, 2009 at 4:17pm
Print Post  
Hi Anshul,

There isn't any property for automatically decreasing the font size. You might try calling diagram.MeasureString to see if the text fits into the node, and decrease the font size if it doesn't.

Stoyan
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Need custom font size when text is trim
Reply #2 - Dec 22nd, 2009 at 6:02am
Print Post  
Hi Stoyan,
Thanks for the quick reply.

diagram.MeasureString to fits the text into the node but we are using textarea inside the node. What we can do in this case?

Code for our custom node is as follows,

Code
Select All
textOnTheRight = new MindFusion.Diagramming.Wpf.Shape(
		     Shapes.Rectangle.Outline,// reuse the rectangular shape
		     null,// no decorations
		     new ElementTemplate[]// define text region
			  {
				new LineTemplate(32, 19, 90, 19),
				new LineTemplate(90,19, 90, 79),
				new LineTemplate(90, 79, 32, 79),
				new LineTemplate(32, 79, 32, 19)
			  },
			   FillRule.EvenOdd,// doesn't matter here
			   "TextOnTheRight"// to access the shape later using Shape.FromId
		     ); 



Please suggest.

Thanks,
Anshul
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Need custom font size when text is trim
Reply #3 - Dec 22nd, 2009 at 12:48pm
Print Post  
It should look like this, where 58 and 60 are your text area width and height in percents:

Code
Select All
private void btnFitText(object sender, RoutedEventArgs e)
{
	ShapeNode node = diagram.ActiveItem as ShapeNode;
	if (node != null)
	{
		Rect bounds = node.Bounds;
		double textAreaW = bounds.Width * 58 / 100; // the width of the text area
		double textAreaH = bounds.Height * 60 / 100; // the height of the text area

		Size size = Diagram.MeasureString(node.Text, node.Font, (int)textAreaW, node.TextFormat);
		if (size.Height > textAreaH)
		{
			node.Font.Size -= 3;
			node.Repaint(false);
		}
	}
} 

  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Need custom font size when text is trim
Reply #4 - Dec 23rd, 2009 at 7:07am
Print Post  
Hi Stoyan,

This is working but it is working everytime i.e I want to decrease the node's font size by 1. If now text is fit into the text area than no need to decrease font size any more but if it is not than again do the same till fontsize-3.

One more thing, if font size is 12 and text is small enough to fit into the text area than we don't need to decrease the font size.

The code for Shape is as follows
Code
Select All
textOnTheRight = new MindFusion.Diagramming.Wpf.Shape(
     Shapes.Rectangle.Outline,// reuse the rectangular shape
     null,// no decorations
     new ElementTemplate[]// define text region
  {
new LineTemplate(32, 29, 90, 29),
new LineTemplate(90,29, 90, 69),
new LineTemplate(90, 69, 32, 69),
new LineTemplate(32, 69, 32, 29)
  },
   FillRule.EvenOdd,// doesn't matter here
   "TextOnTheRight"// to access the shape later using Shape.FromId
     );
 



The code for which I have tried is as follows
Code
Select All
private void SetFontSize(ShapeNode node)
	  {
		if (node != null)
		{
		    Rect bounds = node.Bounds;
		    double textAreaWidth = bounds.Width * 58 / 100; // Width of the text area
		    double textAreaHeight = bounds.Height * 40 / 100; // Height of the text area

		    Size textSize = diagram.MeasureString(node.Text, node.Font, (int)textAreaWidth, node.TextFormat);
		    while (textSize.Height > textAreaHeight && node.Font.Size > 9)
		    {
			  node.Font.Size -= 1;
			  node.Repaint(false);
			  SetFontSize(node);
		    }
		}
	  }  



Please suggest which check I am missing?

Regards,
Anshul
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Need custom font size when text is trim
Reply #5 - Dec 28th, 2009 at 7:54am
Print Post  
Hi Stoyan,

We are using following code now. We are facing only one little issue with this code.

Code
Select All
private void FitText()

  {


bool isGotFit = false;


ShapeNode node = diagram.ActiveItem as ShapeNode;


while(node.Font.Size > 9 && !isGotFit)


{


    Rect bounds = node.Bounds;


    double textAreaW = bounds.Width * 58 / 100; // the width of the text area


    double textAreaH = bounds.Height * 60 / 100; // the height of the text area


    Size size = diagram.MeasureString(node.Text, node.Font, (int)textAreaW, node.TextFormat);


    if ((size.Height > textAreaH))


    {



  node.Font.Size -= 1;



  node.Repaint(false);


    }


    else


    {



  isGotFit = true;


    }


}

  }  




Issue: - When node text is large and continuous (without space), then font size doesn’t decrease.
How to solve this issue? Please suggest.

Regards,
Anshul Jain
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Need custom font size when text is trim
Reply #6 - Dec 28th, 2009 at 11:07am
Print Post  
Everything seems fine in our test. Could you email us a saved diagram xml so we can try this with your text and font settings?

Stoyan
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Need custom font size when text is trim
Reply #7 - Dec 29th, 2009 at 12:42pm
Print Post  
Hi Stoyan,

We have sent a sample to your support ID. Please look into that and suggest us on those issues mentioned in the mail.

Regards,
Anshul
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Need custom font size when text is trim
Reply #8 - Dec 30th, 2009 at 2:32pm
Print Post  
If you want the text to be trimmed, add this to the createNode method:

sf.Trimming = StringTrimming.Character;

but call MeasureString with trimming disabled:

StringFormat stF = node.TextFormat.Clone() as StringFormat;
stF.Trimming = StringTrimming.None;
Size size = diagram.MeasureString(node.Text, node.Font, (int)Math.Ceiling(textAreaW), stF);
if ((size.Height > textAreaH))
...
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Need custom font size when text is trim
Reply #9 - Jan 4th, 2010 at 7:09am
Print Post  
Hi Stoyan,

Thanks for your suggestion.

It has solved 80% of our problem but only last issue (Double Trimming) are still unsolved. We have sent mail for this issue.
Please have a look.

Regards,
Anshul
« Last Edit: Jan 4th, 2010 at 10:19am by Anshul »  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Need custom font size when text is trim
Reply #10 - Jan 4th, 2010 at 2:07pm
Print Post  
Hi Anshul,

The control calls DrawingContext.DrawText(new FormattedText(...)) to render node labels, and FormattedText provides only MaxTextWidth and Trimming properties from what we can see. FormattedText wraps text only at word boundaries, and if a long word does not fit, it's always trimmed instead of wrapped. We could not find any way to force wrapping in this case. Let us know if you know of any way to do that, and we can add a property to enable it.

Stoyan
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Need custom font size when text is trim
Reply #11 - Jan 5th, 2010 at 6:12am
Print Post  
Hi Stoyan,

I think we can check for text height as well. If text area height is enough to take text on next line than do not trim the text on previous line, else trim the text.

It can be one of the way.

Regards,
Anshul
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Need custom font size when text is trim
Reply #12 - Jan 5th, 2010 at 12:10pm
Print Post  
Hi Stoyan,

I found one more issue. Try the string "Copy of Copy(3) of Input" for node's text, you will see trimming text in first line only.

Now remove last 3 character from the string i.e. "Copy of Copy(3) of In" and try again for node's text. You will see text wrapped in the second line in this case.

It should also wrap first string because second line can contain "Input" word (because of short length text).

Please suggest for this.

Regards,
Anshul
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Need custom font size when text is trim
Reply #13 - Jan 6th, 2010 at 3:17pm
Print Post  
Hi Anshul,

This is how the WPF's DrawText(FormattedText) method lays the text out and we can't do anything to change it. The solution is to use multiple DrawText calls - that's what we do in the EnableStyledText case, but we haven't implemented trimming with ellipsis for it yet. TextBlocks provide more control over the text layout than FormattedText, so you could attach a TextBlock to the main node to display text, instead of using the ShapeNodes' text area.

Stoyan
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Need custom font size when text is trim
Reply #14 - Jan 7th, 2010 at 6:13am
Print Post  
Hi Stoyan,

Thanks for the detailed information.

Would you provide support for "multiple DrawText calls" and "EnableStyledText - trimming with ellipsis" in next build?

Regards,
Anshul
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send TopicPrint