I am reading
Programming.Windows.5th.Edition,Charles Petzold
when I was doing the Figure 4-5. SYSMETS1.C,I met the following codes:
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
and there are explanations in the book,
SYSMETS1 also saves an average width of uppercase letters in the static variable cxCaps. For a fixed-pitch font, cxCaps would equal cxChar. For a variable-width font, cxCaps is set to 150 percent of cxChar. The low bit of the tmPitchAndFamily field in the TEXTMETRIC structure is 1 for a variable-width font and 0 for a fixed-pitch font. SYSMETS1 uses this bit to calculate cxCaps from cxChar.
Now that cxCaps is set to 150 percent of cxChar,I think it should be
cxCaps = (tm.tmPitchAndFamily & 1 ? 3.0 : 2.0) * cxChar / 2 ;
Can you explain it for me?Thanks!
It is trying to calculate an integer result, expressed in pixels. The effective calculation is
(3 * cxChar) / 2, not3 * (cxChar / 2). Presumably the latter one is giving you pause.It’s going to get rounded down for a proportional font but will never be off by more than a single pixel. It doesn’t matter since the value is only a guess anyway, the actual width is different for every glyph in a proportional font.
No points scored for readability, you could perhaps rewrite it like this:
Note how the flag has the wrong name, perhaps the reason Petzold wrote it like that.