• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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)

    https://support.microsoft.com/en-us/kb/200262

    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 + rectYOffset, textLayout.Metrics.Width, rectHeightOffset);
    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 simjs, Yesterday, 08:11 PM
      0 responses
      3 views
      0 likes
      Last Post simjs
      by simjs
       
      Started by mrlucky1x, Yesterday, 06:01 PM
      0 responses
      3 views
      0 likes
      Last Post mrlucky1x  
      Started by dedouglas, Yesterday, 03:17 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by Jugado, Yesterday, 02:22 PM
      2 responses
      30 views
      0 likes
      Last Post Jugado
      by Jugado
       
      Started by login_dejavu, 04-21-2019, 04:58 AM
      2 responses
      24 views
      0 likes
      Last Post login_dejavu  
      Working...
      X