ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  getHeight of scale label?
Posted by Steve Valliere on May-22-2018 02:58
Attachments:
I'm working with LinearMeters (horizontal at the moment, but vertical soon) and I need to determine the number of vertical pixels used by the scale labels.  I have the scale labels set  above the scale itself, as shown in the example code in the ChartDirector help.

The sample code attached to this message shows a function I wrote to reduce the point size of TextBox objects until the height fits within the number of fontPixels specified as a parameter.  The function returns the point size and the new pixel height is returned in the boxPixels parameter.  The function works perfectly for actual text boxes created with m->addText(), but when it calls getHeight() for the text box returned by m->setLabelStyle(), it gets a value of zero, which is obviously incorrect.

The ChartDirector help says that I can use the TextBox object returned by setLabelStyle to fine tune the labels, but without the height (or width for vertical scales) there's not much I can do.

If there was a ChartDirector function that would return the appropriate font size for a given pixel height and/or that would simplify things, but I can live with my work-around ... if it worked.

Help?

Oh, I'm using 64 bit ChartDirector 6 on 64 bit Windows 10 Pro and working in Visual Studio 2017 in a C project with a CPP file for accessing the ChartDirector library.
transfer.txt
#define MinFontPoints 8

int lm_FitFont( TextBox *t, int fontPoints, int fontPixels, int *boxPixels )
{
   int   p = fontPoints;

   while( (MinFontPoints < p) && (fontPixels < t->getHeight()) )
   {
      p -= 1;
      t->setFontSize( p );
   }

   if( boxPixels ) *boxPixels = t->getHeight();
   return p;
}

   
TextBox *t = m->setLabelStyle( labelFont, fontPoints, color->fgScaleLabels );
fontPoints = lm_FitFont( t, fontPoints, fontPixels, &tboxPixels );
   

  Re: getHeight of scale label?
Posted by Peter Kwan on May-22-2018 16:48
Hi Steve,

The TextBox returned by setLabelStyle is only a "template". Because every label can have a different height, so the "template" does not have a definite height.

Note that the height depends not only on the font and font size in setLabelStyle. It can also depend on the content. For example, some labels can have multiple lines. If the label is rotated (and so flows vertically), the height depends on the number of characters in the label. ChartDirector also allows an image to be embedded in the label, or specifying multiple font sizes in the same label. See:

http://www.advsofteng.com/doc/cdcpp.htm#cdml.htm

For your case, if you know all labels should be of the same height, you may simply use Base.addText and provide some typical text to measure the text height. It is like:

int lm_FitFont(TextBox *t, int fontPoints, int fontPixels, int *boxPixels)
{
   for (; MinFontPoints < fontPoints; --fontPoints)
  {
      t->setFontSize(fontPoints);
      if (fontPixels >= t->getHeight()) break;
  }

   if( boxPixels ) *boxPixels = t->getHeight();
   return fontPoints;
}

// Determine the font size first
fontPoints = lm_FitFont(m->addText(-9999, -9999, "0", labelFont), fontPoints, fontPixels, &tboxPixels );

// Set the label style to the computed font size
TextBox *t = m->setLabelStyle(labelFont, fontPoints, color->fgScaleLabels);

Note that the text box height includes margins inside the text box. The margins are configurable using TextBox.setMargin. During code development, sometimes it is helpful to set a background color for the TextBox so that you can see the actual box (eg. t->setBackground(0xff0000);).

Hope this can help.

Regards
Peter Kwan

  Re: getHeight of scale label?
Posted by Steve Valliere on May-22-2018 19:46
Thanks, that helps.  I hadn't thought of adding a "throw-away" text box way outside the meter -- I thought it would [quite sensibly] fail.

This does bring up a request for a new feature... it would be great if text could be added by pixel height instead of points.  That would make all measurements consistent (as pixels) for library users and simplify things for those of us with flexible sizing requirements.  This is trickier, but still possible even with width limitations -- I know, we wrote our own wrapper for the Windows CreateFont() function that keeps creating, measuring, and tweaking parameters until it finds the best fit for the height/width we require (it is only used for fixed-width fonts, though, but the concept is the same.)

Please consider an option to use pixels instead of points for font height.

Thanks for your consideration and your typical excellent support!

  Re: getHeight of scale label?
Posted by Peter Kwan on May-22-2018 23:20
Hi Steve,

The pixel_height and points are related by the formula pixel_height = points * 4.0 / 3. This formula is chosen by Microsoft in the 1980s and is now the default in most systems, and is the formula ChartDirector used. ChartDirector internally only uses pixels as the font size, as the computer can only draw pixels. Its external API uses points because it is what most people or system use.

Note that a font height of 12 pixels do not mean the actual characters are 12 pixels high. It is decided by the person who designed that font and depends on the character. It is possible for a character to be taller or shorter than 12 pixels, but for most of the commonly used fonts, the characters do fit in 12 pixels.

Hope this can help.

Regards
Peter Kwan