Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Text in a Rectangle

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Text in a Rectangle

    Hello,

    i am trying to draw a number into a rectangle in onRender so that there is no space on top and bottom of the text.



    I tried to set the LineSpacing and Baseline Properties but that didn't work.

    Is there a sample or an idea how this could be done?

    Thank you,
    Mike

    #2
    If I understand what you're after, this is indeed a pretty common/frustrating problem in text rendering due to the hidden glyphs and leading associated with various fonts.

    Most approaches I've seen to this problem suffer some sort of limitations, which this Windows GDI resource does a pretty good job of explaining all the options (the same base principles would apply to Direct2D/SharpDX)



    However, I believe in your case you can just offset the top of the rectangle by retrieving current line spacings baseline from the current layout height, and then apply the same offset to the current baseline for the height of the rectangle

    Code:
    SharpDX.DirectWrite.LineMetrics lineMetrics = textLayout.GetLineMetrics().FirstOrDefault();
    float rectYOffset = textLayout.Metrics.Height -  lineMetrics.Baseline;
    float rectHeightOffset = lineMetrics.Baseline - rectYOffset;  
    
    SharpDX.RectangleF rectangleF = new RectangleF(centerPoint.X, centerPoint.Y + [B]rectYOffset[/B], textLayout.Metrics.Width, [B]rectHeightOffset[/B]);
    Which should result in something similar as the attached image:



    Although I'm not convinced this approach works on all font family's and various scaling of fonts. If other users have a better approach, we'd love to hear them

    Attached is a working example.
    Attached Files
    MatthewNinjaTrader Product Management

    Comment


      #3
      Hey Matthew,

      thank you very much, thats pretty much what i looked for

      Cheers,
      Mike

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by kevinenergy, 02-17-2023, 12:42 PM
      115 responses
      2,699 views
      1 like
      Last Post kevinenergy  
      Started by prdecast, Today, 06:07 AM
      1 response
      4 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by Christopher_R, Today, 12:29 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by chartchart, 05-19-2021, 04:14 PM
      3 responses
      577 views
      1 like
      Last Post NinjaTrader_Gaby  
      Started by bsbisme, Yesterday, 02:08 PM
      1 response
      15 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X