怎么判断,一个字在不在某个字库里?

C++语言 码拜 9年前 (2015-05-11) 1118次浏览 0个评论
 

例如,我想知道,某些越南文字字符,在不在“宋体”字体库中存在,有什么API函数可以判断?

3分
仅供参考:
EnumFontFamiliesEx
The EnumFontFamiliesEx function enumerates all fonts in the system that match the font characteristics specified by the LOGFONT structure. EnumFontFamiliesEx enumerates fonts based on typeface name, character set, or both. 

int EnumFontFamiliesEx(
  HDC hdc,              // handle to device context
  LPLOGFONT lpLogfont,  // pointer to logical font information
  FONTENUMPROC lpEnumFontFamExProc,
                        // pointer to callback function
  LPARAM lParam,        // application-supplied data
  DWORD dwFlags         // reserved; must be zero
);
 
Parameters
hdc 
Handle to the device context. 
lpLogfont 
Pointer to a LOGFONT structure that contains information about the fonts to enumerate. The function examines these members: Member Description 
lfCharset If set to DEFAULT_CHARSET, the function enumerates all fonts in all character sets. If set to a valid character set value, the function enumerates only fonts in the specified character set. 
lfFaceName If set to an empty string, the function enumerates one font in each available typeface name. If set to a valid typeface name, the function enumerates all fonts with the specified name. 
lfPitchAndFamily Must be set to zero for all language versions of the operating system except Hebrew and Arabic. For these languages, set IfPitchAndFamily to MONO_FONT to enumerate only fonts that provide all codepage characters within the font. 

lpEnumFontFamExProc 
Pointer to the application-defined callback function. For more information about the callback function, see the EnumFontFamExProc function. 
lParam 
Specifies a 32-bit application-defined value. The function passes this value to the callback function along with font information. 
dwFlags 
Reserved; must be zero. 
Return Values
The return value is the last value returned by the callback function. This value depends on which font families are available for the specified device. 

Remarks
EnumFontFamiliesEx does not use “tagged” typeface names to identify character sets. Instead, it always passes the correct typeface name and a separate character set value to the callback function. The function enumerates fonts based on the the values of the lfCharset and lfFacename members in the LOGFONT structure. 

If lfCharset is DEFAULT_CHARSET and lfFaceName is an empty string, the function enumerates one font in every face in every character set. If lfFaceName is not empty, the function enumerates every font in the specified typeface regardless of character set.

If lfCharset is a valid character set value and lfFaceName is an empty string, the function enumerates every font in the specified character set. If lfFaceName is not empty, the function enumerates every font having the specified typeface and character set.

QuickInfo
  Windows NT: Requires version 4.0 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Unsupported.
  Header: Declared in wingdi.h.
  Import Library: Use gdi32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT.

See Also
Fonts and Text Overview, Font and Text Functions, EnumFontFamExProc, LOGFONT 

 

3分
再供参考

#pragma comment(lib,"gdi32")
#include <windows.h>
#include <stdio.h>

int main() {
    const DWORD uWidth = 18 + 17 * 256, uHeight = 18 + 17 * 128;

    PBITMAPINFO pbmi = (PBITMAPINFO) LocalAlloc (LPTR, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2);
    pbmi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
    pbmi->bmiHeader.biWidth = uWidth;
    pbmi->bmiHeader.biHeight = uHeight;
    pbmi->bmiHeader.biPlanes = 1;
    pbmi->bmiHeader.biBitCount = 1;
    pbmi->bmiHeader.biSizeImage = ((uWidth + 31) & ~31) / 8 * uHeight;
    pbmi->bmiColors[0].rgbBlue = 0;
    pbmi->bmiColors[0].rgbGreen = 0;
    pbmi->bmiColors[0].rgbRed = 0;
    pbmi->bmiColors[1].rgbBlue = 255;
    pbmi->bmiColors[1].rgbGreen = 255;
    pbmi->bmiColors[1].rgbRed = 255;

    HDC hDC = CreateCompatibleDC (0);
    void * pvBits;
    HBITMAP hBitmap = CreateDIBSection (hDC, pbmi, 0, &pvBits, NULL, 0);
    SelectObject (hDC, hBitmap);
    HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体");
//  HFONT hFont = CreateFont (16, 0, 0, 0, 0, 0, 0, 0, SHIFTJIS_CHARSET, 0, 0, 0, 0, "宋体");
    SelectObject (hDC, hFont);
    BitBlt (hDC, 0, 0, uWidth, uHeight, NULL, 0, 0, WHITENESS);

    char c[4];
    int i, j;
    for (i = 128; i < 256; i++) {
        sprintf (c, "%02X", i);
        TextOut (hDC, 1, (i - 127) * 17 + 1, c, 2);
    }
    for (j = 0; j < 256; j++) {
        sprintf (c, "%02X", j);
        TextOut (hDC, (j + 1)* 17 + 1, 1, c, 2);
    }
    for (i = 128; i < 256; i++) {
        for (j = 0; j < 256; j++) {
            c[0] = (char) i;
            c[1] = (char) j;
            TextOut (hDC, (j + 1) * 17 + 1, (i - 127) * 17 + 1, c, 2);
        }
    }
    for (i = 0; i < 130; i++) {
        MoveToEx (hDC, 0, i * 17, NULL);
        LineTo (hDC, uWidth, i * 17);
    }
    for (j = 0; j < 258; j++) {
        MoveToEx (hDC, j * 17, 0, NULL);
        LineTo (hDC, j * 17, uHeight);
    }

    BITMAPFILEHEADER bmfh;
    bmfh.bfType = *(PWORD) "BM";
    bmfh.bfReserved1 = 0;
    bmfh.bfReserved2 = 0;
    bmfh.bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2;
    bmfh.bfSize = bmfh.bfOffBits + pbmi->bmiHeader.biSizeImage;

    HANDLE hFile = CreateFile ("goal.bmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
    if (hFile != INVALID_HANDLE_VALUE) {
        DWORD dwWritten;
        WriteFile (hFile, &bmfh, sizeof (BITMAPFILEHEADER), &dwWritten, NULL);
        WriteFile (hFile, pbmi, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2, &dwWritten, NULL);
        WriteFile (hFile, pvBits, pbmi->bmiHeader.biSizeImage, &dwWritten, NULL);

        CloseHandle (hFile);
    }

    DeleteObject (hFont);
    DeleteObject (hBitmap);
    DeleteDC (hDC);
    LocalFree (pbmi);

    return 0;
}
楼上的代码,看完了,没用。
3分
引用 3 楼 AxYug 的回复:

楼上的代码,看完了,没用。

你不会参考2楼代码将你要检测的字符显示出来,然后用GetPixel函数逐像素判断显示内容是否空白或或?或??吗?

3分
TextOut
The TextOut function writes a character string at the specified location, using the currently selected font, background color, and text color. 

BOOL TextOut(
  HDC hdc,           // handle to device context
  int nXStart,       // x-coordinate of starting position
  int nYStart,       // y-coordinate of starting position
  LPCTSTR lpString,  // pointer to string
  int cbString       // number of characters in string
);
 
Parameters
hdc 
Handle to the device context. 
nXStart 
Specifies the logical x-coordinate of the reference point that the system uses to align the string. 
nYStart 
Specifies the logical y-coordinate of the reference point that the system uses to align the string. 
lpString 
Pointer to the string to be drawn. The string does not need to be zero-terminated, since cbString specifies the length of the string. 
cbString 
Specifies the number of characters in the string. 
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. 

Windows NT: To get extended error information, callGetLastError. 

3分
引用 4 楼 zhao4zhong1 的回复:
Quote: 引用 3 楼 AxYug 的回复:

楼上的代码,看完了,没用。

你不会参考2楼代码将你要检测的字符显示出来,然后用GetPixel函数逐像素判断显示内容是否空白或或?或??吗?

不得不佩服赵老师

这方法

我只是想判断一下一个字符在不在字体库中存在,感觉你方法绕了过多弯路。

有简单点的么?

引用 4 楼 zhao4zhong1 的回复:
Quote: 引用 3 楼 AxYug 的回复:

楼上的代码,看完了,没用。

你不会参考2楼代码将你要检测的字符显示出来,然后用GetPixel函数逐像素判断显示内容是否空白或或?或??吗?

TextOut返回值失败,有很多情况吧,并不一定就是字符不在字体库中这种情况

引用 5 楼 zhao4zhong1 的回复:

TextOut
The TextOut function writes a character string at the specified location, using the currently selected font, background color, and text color. 

BOOL TextOut(
  HDC hdc,           // handle to device context
  int nXStart,       // x-coordinate of starting position
  int nYStart,       // y-coordinate of starting position
  LPCTSTR lpString,  // pointer to string
  int cbString       // number of characters in string
);
 
Parameters
hdc 
Handle to the device context. 
nXStart 
Specifies the logical x-coordinate of the reference point that the system uses to align the string. 
nYStart 
Specifies the logical y-coordinate of the reference point that the system uses to align the string. 
lpString 
Pointer to the string to be drawn. The string does not need to be zero-terminated, since cbString specifies the length of the string. 
cbString 
Specifies the number of characters in the string. 
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. 

Windows NT: To get extended error information, callGetLastError. 

3分
引用 楼主 AxYug 的回复:

例如,我想知道,某些越南文字字符,在不在“宋体”字体库中存在,有什么API函数可以判断?

去该字符的自摸(GetGlyphOutlineW())看他是不是为空。为空就不存在

1分
所以你要GetLaskError()啊。
当然最稳妥还是4楼的方法了。
提醒:有时字库里面有这个字符,只不过你调用CreateFont时候给的dwCharSet参数不对。
要不参考一下
Microsoft SDK\samples\multimedia\Gdi\Fonts\FontView\*.*

MSDN98\SAMPLES\VC98\SDK\GRAPHICS\GDI\FONTS\FONTVIEW\*.*
http://bbs.csdn.net/topics/390374955
1分
或者参考
windows_2000_source_code\win2k\private\windows\shell\accesory\ucharmap\*.*
http://bbs.csdn.net/topics/390779805
GetTextFace 获取字体的字样名
怎么感觉要用到map来处理呢
GetFontUnicodeRanges函数可以

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么判断,一个字在不在某个字库里?
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!