Decrease background size around text

Hey all,

I am having some issues with the background around text being too large. I am trying to control the size using SetBackgroundMargins, but no matter the value I pass in, I get the same amount of background.

Here is my code. setBackground is true in my example below.

  HPS::TextKit textKit;
  CT2A convertedText( text, CP_UTF8 );
  textKit.SetText( convertedText );
  textKit.SetSize( fontSize, HPS::Text::SizeUnits::Pixels );
  textKit.SetColor( COLORREF2RGB( color ) );
  textKit.SetLineSpacing( 0.0f );
  textKit.SetBackground( false );
  textKit.SetBackgroundMargins( 0.1f );
  textKit.SetPriority( 2 );
  textKit.SetFont( font );
  textKit.SetAlignment( HPS::Text::Alignment::TopLeft );
  textKit.SetPosition( HPS::Point( textPos.x, textPos.y, textPos.z ) );

  auto key { m_textKey };
  if( setBackground )
  {
    key = m_textWithBkgKey;
    textKit.SetBackground( true, "rounded box" );
    key.GetMaterialMappingControl().SetFaceColor( HPS::RGBAColor( 1.0, 1.0, 1.0, 1.0 ) );
    key.GetVisibilityControl().SetFaces( true ).SetEdges( false );
  }
  key.InsertText( textKit );

Here is an example. This is with SetBackgroundMargins as 0.1f. You can see how the background overlaps the symbol.

image

Any help is much appreciated.

Hello @amaan.makhani,

It’s hard to tell from the pic you attached because it’s so small. It looks like the text is actually the number 2 with a pinkish background.

Can you check if the variable convertedText is “2” and not " 2" (which has a space preceding the number)? Or as another test to confirm, hard-code it into the function:
textKit.SetText("2");

Thanks,
Tino

Hey tino,

The text was L” 2/n”. So after removing the space and the newline the text covers the symbol. Are there any good suggestion on how to insert the text so it doesn’t cover the symbol? Ideallly better ways to add in padding other than spaces.

Thanks

Hello @amaan.makhani ,

After futher research, SetBackground will not work because it will take any spaces into account.

  1. This is more complicated but you can call ComputeTextExtent to get the text size (w/o padding) and insert your own background.

This background will be render in 3D space. If your text is 2D, then you will need to recalculate your background.

Below is a simple sample on ComputeTextExtent

HPS - ComputeTextExtent example

  1. You can insert your text with a offset to your symbol/marker.

    HPS have a option to add a leader line to your text box so your text does not have to be the same position as your symbol.

    Please see documentation below for sample.

Best Regards,
Simon

Thanks that was super helpful.