Calculation of italic width and mixed Layout

Source: Internet
Author: User
Tags italic font textout

/*
Question 1:
I want to calculate the width of a string and draw it again.
First SelectObject (HDC, font );
Then, use gettextextentpoint32 to calculate the width.

It is found that when the same font and size are italics, the calculated width is the same as that of the non-italics. Italics
The rightmost word is missing.

Solution 1:

When the text is followed by a common text, if no processing is performed, the final character of the Oblique text and the beginning of the normal text
The characters will overlap. (This is the problem in question 1)
To solve this problem, you must compensate for the multi-margin trailing characters of the first string when plotting normal text.
The TrueType font uses getcharabcwidths to retrieve these values.

The following code example outputs the character string "fff" in the skewed text and immediately uses ggg regular (non-skewed) after it)
The string in the text. If you do not perform compensation, the two substrings will usually overlap.

// Sample call to the contrived example below:
Italicandregular (HDC, 0, 0, 12 );

// Outputs the Italic string "fff" immediately followed by the non-italic
// String "ggg" at position X, Y. The fonts are created at the specified
// Point size and using the Times New Roman font for this contrived
// Example.
*/
Void coptfonttest: drawtest4 (CDC * PDC)
// Void italicandregular (HDC, int X, int y, int ipointsize)
{
HDC = PDC-> getsafehdc ();
Int x = 100;
Int y= 200;
Int ipointsize = 20;
Logfont lf;
Hfont holdfont;
Hfont hfontregular = NULL;
Hfont hfontitalic = NULL;
Size size;
Int lpy;
ABC ABC1;
ABC abc2;
Cstring strfff = _ T ("fff ");
// For Chinese Characters
Strfff = _ T ("if it is a Chinese character ");
Cstring strggg = _ T ("ggg ");
Strggg = _ T ("no problem ");

// Get the vertical DPI of the device.
Lpy = getdevicecaps (HDC, logpixelsy );

// Create a regular (non-italic) font.
Zeromemory (& lf, sizeof (LF ));
Lf. lfheight =-muldiv (ipointsize, lpy, 72 );
Lf. lfitalic = false;
Lf. lfcharset = default_charset;
Lstrcpy (LF. lffacename, text ("Times New Roman "));
Hfontregular = createfontindirect (& lf );

// Create an italic font.
Zeromemory (& lf, sizeof (LF ));
Lf. lfheight =-muldiv (ipointsize, lpy, 72 );
Lf. lfitalic = true;
Lf. lfcharset = default_charset;
Lstrcpy (LF. lffacename, text ("Times New Roman "));
Hfontitalic = createfontindirect (& lf );

// Output an italic string.
Holdfont = (hfont) SelectObject (HDC, hfontitalic );
Textout (HDC, X, Y, strfff, strfff. getlength ());

// Get the base width of the Italic string * and * the overhang
// The Last italic character if any (abc1.abcc ).
Gettextextentpoint32 (HDC, strfff, strfff. getlength (), & size );
Getcharabcwidths (HDC, 'F', 'F', & ABC1 );

// Immediately follow the Italic string with a non-italic string,
// Taking into account the base width of the Italic string,
// Overhang of the trailing italic character if any (abc1.abcc ),
// And the underhang of the leading non-italic character if any
// (Abc2.abca ).
SelectObject (HDC, hfontregular );
Getcharabcwidths (HDC, 'G', 'G', & abc2 );
Textout (HDC, x + size. CX-abc1.abcc-abc2.abca, Y, strggg,
Strggg. getlength ());

// Clean up.
SelectObject (HDC, holdfont );
Deleteobject (hfontitalic );
Deleteobject (hfontregular );

Return;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.