Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SharpDX.DirectWrite.TextAlignment.Trailing is not rendered

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

    SharpDX.DirectWrite.TextAlignment.Trailing is not rendered

    version 8.0.22.2 64-bit
    I am having a problem with OnRender DrawTextLayout, when trying to set the TextFormat object property "TextAlignment" equal to the enum choice "Trailing"

    the text does not render, and no error msg.

    If the enum is changed to "Center" or "Leading", the text renders as expected.

    Code:
    // inside OnRender...
    
    // try making a format object directly
    SharpDX.DirectWrite.TextFormat textFormat2 = new SharpDX.DirectWrite.TextFormat(
    NinjaTrader.Core.Globals.DirectWriteFactory,
    "Century Gothic", SharpDX.DirectWrite.FontWeight.Bold, SharpDX.DirectWrite.FontStyle.Italic, 32f
    );
    // now try to set the TextAlignment
    textFormat2.TextAlignment = SharpDX.DirectWrite.TextAlignment.Trailing; // Center, Leading works ok, Trailing fails
    
    // Colors
    // Bid
    SharpDX.Direct2D1.SolidColorBrush ProfileSizeMax_Bid_Color = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Red);
    
    // position of text box, X with minus moves left, Y with plus moves down
    SharpDX.Vector2 ProfileSizeMax_Bid_XYVector = new SharpDX.Vector2(PriceBar_xpos -10, PriceBar_LowPricePixel + 10);
    
    // use new textFormat2 object
    SharpDX.DirectWrite.TextLayout ProfileSizeMax_Bid_TextLayout = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Gl obals.DirectWriteFactory,
    ProfileSizeMax_Bid_value.ToString(),
    textFormat2, ChartPanel.X + ChartPanel.W,
    textFormat2.FontSize
    );
    
    // do the rendering, XY pixels, TextBox, BrushColor, option nosnap
    RenderTarget.DrawTextLayout(ProfileSizeMax_Bid_XYVector, ProfileSizeMax_Bid_TextLayout, ProfileSizeMax_Bid_Color, SharpDX.Direct2D1.DrawTextOptions.NoSnap);

    I searched the forums, but did not find any bugs reports about this. Can anyone test this code to see if they can render some text ( less than 3 digits) to see if it will work using Trailing?

    #2
    Hi balltrader, thanks for your post.

    I do not have PriceBar_xpos PriceBar_LowPricePixel defined in your snippet.

    I will need you to Export a full test script and post it here so I can test on my end.

    Kind regards,
    -ChrisL

    Comment


      #3
      I figured it out.
      The container box for the text, it's property MaxWidth was being set as the full width of the chart panel, and it's upper left corner X origin coor was in the middle of the chart, so the right edge of the container was off the chart, and the text was rendered, but not visible.

      BUT, when I adjusted the MaxWidth to a smaller value, to make the box visible, the right alignment was at the right edge of the container, but it depended on the correct placement of the starting X origin coord.


      Based on an idea in a post by Jim from 2017 (https://ninjatrader.com/support/foru...261#post805261), I used his concept to write a helper function to calculate the actual width of the text string in a temp TextLayout container and returned that float to the real TextLayout method, and adjusted the starting X origin coord, to get teh "right aligned" text to be exactly where I wanted it.

      Note this is not needed if you are only using Left-Aligned "Leading" text in it's container.

      Code:
      /// Helper function to get actual width of text, so maxwidth can be properly set, for proper right alignment
      private float OnRenderHelper_TextLayout_Width_get(string _text, SharpDX.DirectWrite.TextFormat _textFormat)
      { // create layout using input text string, and textFormat object
      SharpDX.DirectWrite.TextLayout _textLayout =
      new SharpDX.DirectWrite.TextLayout(
      NinjaTrader.Core.Globals.DirectWriteFactory,
      _text, _textFormat,
      ChartPanel.X + ChartPanel.W, _textFormat.FontSize
      );
      // get width actual
      float _WidthActual = _textLayout.Metrics.Width;
      // dispose temp textlayout object
      _textLayout.Dispose();
      // return width
      return _WidthActual;
      }

      Code:
      ///@TASK get actual width of rendered text, using helper func
      // OnRenderHelper_TextLayout_Width_get(string _text, SharpDX.DirectWrite.TextFormat _textFormat)
      float _text_width_actual = OnRenderHelper_TextLayout_Width_get(ProfileSizeMax _Bid_value.ToString(), ProfileSizeMax_textFormat);
      Print("text width actual="+ _text_width_actual);
      
      
      // create container (TextLayout) to hold text, and set text value inside here as 2nd parameter, and set width of container to float value returned from helper function
      
      SharpDX.DirectWrite.TextLayout ProfileSizeMax_Bid_TextLayout = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Gl obals.DirectWriteFactory,
      ProfileSizeMax_Bid_value.ToString(),
      ProfileSizeMax_textFormat,
      _text_width_actual, ProfileSizeMax_textFormat.FontSize
      );
      
      // try alignment here on TextLayout, but these should be inherited from TextFormat
      // ProfileSizeMax_Bid_TextLayout.TextAlignment = SharpDX.DirectWrite.TextAlignment.Trailing;
      // ProfileSizeMax_Bid_TextLayout.ParagraphAlignment = SharpDX.DirectWrite.TextAlignment.Trailing; // this is the height position,
      
      ///@TODO - can you adjust the TextLayout Starting X, before calling DrawTextLayout ?
      ProfileSizeMax_Bid_XYVector.X -= _text_width_actual;
      
      // do the rendering, XY pixels, TextBox, BrushColor, option none
      RenderTarget.DrawTextLayout(ProfileSizeMax_Bid_XYVector, ProfileSizeMax_Bid_TextLayout, ProfileSizeMax_Bid_Color, SharpDX.Direct2D1.DrawTextOptions.None);

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      606 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      353 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      560 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      561 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X