Windows vector font lattice data extraction (GO)

Source: Internet
Author: User
Tags vector font

Source: Windows vector font lattice data extraction

1. Extraction principle

In the Windows system to extract vector font fonts There are many ways, the following describes a use of getglyphoutline to achieve the extraction of matrix number data.

Getglyphoutline is a Windows system API function, using this function, you can easily and quickly extract vector font character lattice data, and can well support the reading of characters from a text file. In the face of large numbers of character data input, the amount of time required to obtain the lattice data is also very small.

The Getglyphoutline function is declared as follows:

DWORD Getglyphoutline (hdc hdc,             //  handle to DC  UINT UChar,          //  Character to query  UINT uformat,        //  data format  //  Glyph Metrics  DWORD cbbuffer,      //  size of data buffer  lpvoid lpvbuffer,    // Data Buffer  CONST MAT2 *lpmat2   //  transformation matrix);

Getglyphoutline function is the API function of Windows system , when using VC + + development, this function is encapsulated in the DC class, is a member function of the DC class.

2. Realization of character lattice data extraction

When the application calls the Getglyphoutline function, this function can return the rectangular region information that we need for the character lattice data by lpglyphmetric this struct pointer. The lattice data obtained by the function is the lattice data of the smallest rectangular region composed of Gmblackboxx and Gmblackboxy, 1, And the character lattice data that we need to display in the actual application is the lattice data in the large rectangular region composed of gmcellincx and gmcellincy, so when we call the lattice data of the character that Getglyphoutline obtains, It is also necessary to add the bounding area outside the minimum rectangle, which requires the pingsheng of the smallest rectangular lattice data to be moved to the middle position of the large rectangular region composed of gmcellincx and gmcellincy by the corresponding matrix transformation.

In the Windows operating system practice shows that GETGLYPHOUTLINEP this function returns the struct lpglyphmetrics in gmcellincy This value return is 0, this is the operating system version itself, and therefore need to be obtained by another method. We use GetTextExtent (Cstring,int) and GetTextMetrics (Textmetric *tm), through the above two functions we can obtain the width and height of the character information, and then through the corresponding matrix transformation of the transformation, You can get the required word definable lattice data! The width of the character lattice data obtained by the Getglyphoutline function is 4-byte aligned, so 4-byte alignment is done. For character data with a width other than 8-bit alignment, you should do 4-byte alignment after topping up 8 bits. The program to get the character lattice data is as follows:

CString Str ("Wah");//characterCDC DC;//CDC class, this class has getglyphoutline this methoddc. CreateDC (_t ("DISPLAY"), NULL, NULL, NULL); CFont*poldfont=dc. SelectObject (&m_font);//font SettingsTextmetric TM;//it's a struct. This structure contains information about the font,GLYPHMETRICSPGL;//This structure contains information about the location and direction of a basic character cell .MAT2 Mat2 = {{0,1},{0,0},{0,0},{0,1}};//Transformation Matrixdc. GetTextMetrics (&AMP;TM);//gets the current selection font width and height      intBitwidth=tm.tmavecharwidth;//average of character widths      intBitheigh =tm.tmheight;//Character Heightintch = str. GetAt (0); intLen =dc. Getglyphoutline (Ch,ggo_bitmap, &AMP;PGL,0, NULL, &AMP;MAT2);//obtained//to the size of the data bufferCSize cs = pdc->gettextextent (str,1);//regain the width of the character, fix the value      intWidthex =cs.cx; Bitwidth=Widthex; if(Bitwidth%8==0) {Bitwidth= Bitwidth/8;//character width 8-bit aligned, less than 8 bits, 8-bit      }      Else{bitwidth= Bitwidth/8+1; }intBoxxbytewidth = ALIGN (Pgl.gmblackboxx,4);//Minimum Rectangle width, 4-byte alignmentintFontoffy = TM.TMASCENT-PGL.GMPTGLYPHORIGIN.Y;//get the Y-direction offsetintFONTOFFX = Pgl.gmptglyphorigin.x <0?0:p gl.gmptglyphorigin.x;//get the X-direction offsetintBufSize = bitwidth* Bitheigh;//character lattice data sizeUnsignedChar*pbuf =NewUnsignedChar[BufSize];//Databufif(PBuf! =NULL) {memset (PBuf,0, bufSize); if(Len >0) {unsignedChar*PSRC = newunsignedChar[Len]; unsignedChar*pdest = (unsignedChar*) PBuf; dc. Getglyphoutline (CH, ggo_bitmap,&AMP;PGL, Len, PSRC, &AMP;MAT2);//Get Lattice data                     for(inti =0; I < len/boxxbytewidth;i++)                    {    //copy Databuf to PDestmemcpy (PDest + i * (bitwidth/8), PSRC + i *boxxbytewidth, Boxxbytewidth); }                    //transform the matrix to convert the dot matrix obtained by getglyphoutline into a lattice data containing a bounding rectangle.Martixcovert (Pdest,bitwidth,bitheigh, FONTOFFX, Fontoffy); }}DC. SelectObject (Poldfont);d C. DeleteDC ();Delete[]psrc;Delete[]pdest;

The following is the display of the lattice data

Windows vector font lattice data extraction (GO)

Related Article

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.