LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Width of text in a ring

Hello all,
I've followed this
http://digital.ni.com/public.nsf/allkb/3333698A4A073FD786256DD60077693D
to obtain the width of the text contained in a ring but two things go wrong:

GetCtrlAttribute (panelHandle, PANEL_STRING, ATTR_TEXT_FONT, font);
At this stage (1st line of the sequence), font contains "ControlFont"
I don't understand how that's possible as it's not even be defined yet.
It should be NIDialogMetaFont or the corresponding 'real' font, no ?

Or does CreateMetaFont somehow add it to the system fonts at the 1st call?!?


Then at the end of the sequence, the width obtained by GetTextDisplaySize is
about half the real width...

--
Guillaume Dargaud
http://www.gdargaud.net/
0 Kudos
Message 1 of 7
(2,745 Views)

I agree that that does not make a whole lot of sense. No, the font added to the system fonts. The font exists in that process only.

 

I took the code from that example just as it is and font contained NIMetaDialogFont. Could you post the code you are using, or is it also copy pasted from the example?

National Instruments
0 Kudos
Message 2 of 7
(2,728 Views)
> I agree that that does not make a whole lot of sense. No, the font added
> to the system fonts. The font exists in that process only.
>  
> I took the code from that example just as it is and font contained
> NIMetaDialogFont. Could you post the code you are using, or is it also
> copy pasted from the example?

I basically copy-pasted from the example.
I just closed the CVI IDE and started again, even rebooted, on the first and
subsequent run 'font' contains "ControlFont" which seems only possible if
it's been memorized by the system. There's something that escapes me here.

Here's the code:
char font[255]="";
int fontSize, bold, italics, underline, strikeout;

GetCtrlAttribute (p, P_RING, ATTR_TEXT_FONT, font);
GetCtrlAttribute (p, P_RING, ATTR_TEXT_POINT_SIZE, &fontSize);
GetCtrlAttribute (p, P_RING, ATTR_TEXT_BOLD, &bold);
GetCtrlAttribute (p, P_RING, ATTR_TEXT_ITALIC, &italics);
GetCtrlAttribute (p, P_RING, ATTR_TEXT_UNDERLINE, &underline);
GetCtrlAttribute (p, P_RING, ATTR_TEXT_STRIKEOUT, &strikeout);

CreateMetaFont ("Whatever", font, fontSize, bold, italics, underline,
strikeout);

--
Guillaume Dargaud
http://www.gdargaud.net/
0 Kudos
Message 3 of 7
(2,721 Views)
Leaving aside the fact that one of my program always has Font as
"ControlFont" on the 1st call of GetCtrlAttribute/ATTR_TEXT_FONT,
consider the complete code below. Why does the value of font changes ?

When I create a meta font, I'm not asking it to assign it to some control!

#include <userint.h>

int main (int argc, char *argv[]) {
char font[255]="", Typeface[255];
int fontSize, bold, italics, underline, strikeout;
char Str[]="Some long string of text";
int Pnl, Ctrl, TextWidth=0, r;

Pnl = NewPanel (0, "Test", 20, 20, 200, 200);
Ctrl = NewCtrl (Pnl, CTRL_RING_LS, "Test", 0, 0);
InsertListItem (Pnl, Ctrl, -1, Str, 0);

GetCtrlAttribute (Pnl, Ctrl, ATTR_TEXT_POINT_SIZE, &fontSize);
GetCtrlAttribute (Pnl, Ctrl, ATTR_TEXT_BOLD, &bold);
GetCtrlAttribute (Pnl, Ctrl, ATTR_TEXT_ITALIC, &italics);
GetCtrlAttribute (Pnl, Ctrl, ATTR_TEXT_UNDERLINE, &underline);
GetCtrlAttribute (Pnl, Ctrl, ATTR_TEXT_STRIKEOUT, &strikeout);
GetFontTypefaceName (font, Typeface);

GetCtrlAttribute (Pnl, Ctrl, ATTR_TEXT_FONT, font);
r=CreateMetaFont ("Something", font, fontSize, bold, italics,
underline, strikeout);
r=GetTextDisplaySize(Str, "Something", 0, &TextWidth);
// font is now NIDialogMetaFont

GetCtrlAttribute (Pnl, Ctrl, ATTR_TEXT_FONT, font);
r=CreateMetaFont ("SomethingElse", font, fontSize, bold,
italics, underline, strikeout);
r=GetTextDisplaySize(Str, "SomethingElse", 0, &TextWidth);
// font is now Something

GetCtrlAttribute (Pnl, Ctrl, ATTR_TEXT_FONT, font);
r=CreateMetaFont ("YetAnotherThing", font, fontSize, bold,
italics, underline, strikeout);
r=GetTextDisplaySize(Str, "YetAnotherThing", 0, &TextWidth);
// font is now SomethingElse

return 0;
}



--
Guillaume Dargaud
http://www.gdargaud.net/
0 Kudos
Message 4 of 7
(2,718 Views)

I was a little confused by this behavior too. I think it is most clear when you distringuish typeface from font.

 

For the sake of explanation, let's assume you are using 20 point bold Courier as your font. The reason GetCtrlAttribute returns your new MetaFont "Something" after creating it is because "Something" is functionally equivalent to "Courier" with the same attributes. By this I mean they both 20 point bold with Courier typeface. When getting ATTR_TEXT_FONT from the control it finds the "Something" MetaFont before the "Courier" font.

 

You can see this is action if you change the size of the control font and then get the ATTR_TEXT_FONT again. It will then return "Courier" as you originally expected. This behavior may be confusing if you are actually looking at the names of the fonts, but it is functionally the same. If you need to retrieve the typeface from the font, use the GetFontTypefaceName function.

National Instruments
0 Kudos
Message 5 of 7
(2,712 Views)
D Biel wrote:

> [...] The reason GetCtrlAttribute returns your new
> MetaFont "Something" after creating it is because "Something" is
> functionally equivalent to "Courier" with the same attributes.

OK, that makes it a little more clear.

What I still don't get is why one program still returns "ControlFont" even
though I changed the name of the metafont in the code. And another program
which is a simplified copy of the 1st one starts with NIDialogMetaFont. Both
reading the same control from the same .uir.

Not sure if I still have the size problem on the ring control, I need to
check that but my prog is currently broken while I replace realloc() with
Lists...
--
Guillaume Dargaud
http://www.gdargaud.net/
0 Kudos
Message 6 of 7
(2,707 Views)
> What I still don't get is why one program still returns "ControlFont" even

I got it: the functionality was duplicated in the program, with ControlFont
hardcoded in the 2nd place... Sorry.
--
Guillaume Dargaud
http://www.gdargaud.net/
0 Kudos
Message 7 of 7
(2,706 Views)