Anonymous
Не могу представить китайский рисунок с Freetype
Сообщение
Anonymous » 15 сен 2025, 16:59
Я пытаюсь вывести эту строку L "Привет, 你好, 日本". Вот что он показывает:
Я пытаюсь использовать Freetype Do Drait в HDC, в приложении WinApi /at. PrettyPrint-Override ">
Код: Выделить всё
#include
#include
#include
#include
#include FT_FREETYPE_H
using std::cout;
using std::endl;
template class scope_guard
{
T t;
public:
scope_guard(T _t) :t(_t) {}
~scope_guard() { t(); }
};
void drawGlythBitmap(FT_Face ftface, HDC hdc, int& pen_x, int& pen_y, wchar_t c)
{
FT_Load_Char(ftface, c, FT_LOAD_RENDER);
FT_GlyphSlot& glyph = ftface->glyph;
FT_Bitmap& btm = ftface->glyph->bitmap;
FT_Bitmap& pbtm = glyph->bitmap;
int heigthAdvance = -glyph->bitmap_top + ftface->size->metrics.y_ppem; //stays inside the rectangle, but diacritics go under the bottom
//int heigthAdvance = -glyph->bitmap_top; //stays on top of rectangle and intersects it
int ytop = pen_y + heigthAdvance;
for (int i = 0, xi = pen_x; i < pbtm.width; i++, xi++)
{
for (int j = 0; j < pbtm.rows; j++)
{
char pixel_val = pbtm.buffer[j * pbtm.width + i] ^ -1;
SetPixel(hdc, xi, ytop + j, RGB(pixel_val, pixel_val, pixel_val)); //almost ok
}
}
pen_x += glyph->advance.x >> 6;
}
void drawStringFT_IntoDC(FT_Face ftface, HDC hdc, const wchar_t* str, int& pen_x, int& pen_y, SIZE* sz = 0)
{
for (int i = 0; str[i]; i++)
drawGlythBitmap(ftface, hdc, pen_x, pen_y, str[i]);
}
void drawStringFT_IntoDC(FT_Face ftface, HDC hdc, const wchar_t* str, unsigned int fontSizeFT, int& pen_x, int& pen_y, SIZE* sz = 0)
{
FT_Set_Pixel_Sizes(ftface, 0, fontSizeFT);
drawStringFT_IntoDC(ftface, hdc, str, pen_x, pen_y, sz);
}
class CDemoWindow : public CWindowImpl
{
FT_Library ftlib;
FT_Face ftface;
public:
CDemoWindow()
{
FT_Init_FreeType(&ftlib);
FT_New_Face(ftlib, "C:/Windows/Fonts/arial.ttf", 0, &ftface);
}
virtual ~CDemoWindow() {
FT_Done_Face(ftface);
FT_Done_FreeType(ftlib);
}
/////////////////////////// Message Handlers /////////////////////////////////
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 1;
}
LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(&ps);
scope_guard sg_ps([this, &ps]() { EndPaint(&ps); });
int xstart = 50, pen_x = 50, pen_y = 200, ystart = 200, step = 30;
std::wstring str = L"Hello, 你好, 日本";
unsigned int fontSizeFT = 48;
drawStringFT_IntoDC(ftface, hdc, str.c_str(), fontSizeFT, pen_x, pen_y);
bHandled = TRUE;
return 0;
}
LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
Invalidate(); //repaint request with a click
return 0;
}
LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
bHandled = TRUE;
break;
}
return 0;
}
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PostQuitMessage(0);
return 1;
}
BEGIN_MSG_MAP(CDemoWindow)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
END_MSG_MAP()
};
int main()
{
CDemoWindow cwnd;
HWND hWnd = cwnd.Create(NULL, CWindow::rcDefault, L"Hello", WS_OVERLAPPEDWINDOW);
cwnd.ShowWindow(SW_SHOWNORMAL);
cwnd.UpdateWindow();
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
Замечание 1: Чтобы предвидеть возможные вопросы, все чары установлены и на месте. Если какое -либо подозрение в файле CPP, содержащее китайские ие символы, это также
не < /strong> проблема, это в UTF8, кодируясь с подписью. src = "https://i.sstatic.net/qvowxrnz.png"/ >
Код: Выделить всё
#include
#include
#include
using std::cout;
using std::endl;
template class scope_guard
{
T t;
public:
scope_guard(T _t) :t(_t) {}
~scope_guard() { t(); }
};
SIZE paintWithFont(HDC hDC, int x, int y, const wchar_t* str, wchar_t* font, int height = 0, int width = 0, DWORD iCharset = DEFAULT_CHARSET)
{
HFONT hFont = ::CreateFontW(height, width, 0, 0, FW_DONTCARE,
0, 0, 0, iCharset,
0, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, font);
HGDIOBJ hOldFont = ::SelectObject(hDC, hFont);
scope_guard fnt_guard([&hFont, &hOldFont]() {::DeleteObject(hFont); ::DeleteObject(hOldFont); });
SIZE sz;
::GetTextExtentPoint32W(hDC, str, wcslen(str), &sz);
::TextOutW(hDC, x, y, str, wcslen(str));
::SelectObject(hDC, hOldFont);
return sz;
}
class CDemoWindow : public CWindowImpl
{
public:
CDemoWindow()
{
}
virtual ~CDemoWindow() {
}
/////////////////////////// Message Handlers /////////////////////////////////
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 1;
}
LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(&ps);
scope_guard sg_ps([this, &ps]() { EndPaint(&ps); });
int xstart = 50, pen_x = 50, pen_y = 200, ystart = 200, step = 30;
std::wstring str = L"Hello, 你好, 日本";
SIZE sz;
unsigned int fontSize = 48;
paintWithFont(hdc, 50, ystart, str.c_str(), L"Arial", fontSize); ystart += step;
bHandled = TRUE;
return 0;
}
LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
Invalidate(); //repaint request with a click
return 0;
}
LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
bHandled = TRUE;
break;
}
return 0;
}
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PostQuitMessage(0);
return 1;
}
BEGIN_MSG_MAP(CDemoWindow)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
END_MSG_MAP()
};
int main()
{
CDemoWindow cwnd;
HWND hWnd = cwnd.Create(NULL, CWindow::rcDefault, L"Hello", WS_OVERLAPPEDWINDOW);
cwnd.ShowWindow(SW_SHOWNORMAL);
cwnd.UpdateWindow();
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
замечание 3 [/b] коды Unicode для Chars You 4F60, OK 597D, 2F47, книга 672C
Подробнее здесь:
https://stackoverflow.com/questions/797 ... h-freetype
1757944746
Anonymous
Я пытаюсь вывести эту строку L "Привет, 你好, 日本". Вот что он показывает: Я пытаюсь использовать Freetype Do Drait в HDC, в приложении WinApi /at. PrettyPrint-Override ">[code]#include #include #include #include #include FT_FREETYPE_H using std::cout; using std::endl; template class scope_guard { T t; public: scope_guard(T _t) :t(_t) {} ~scope_guard() { t(); } }; void drawGlythBitmap(FT_Face ftface, HDC hdc, int& pen_x, int& pen_y, wchar_t c) { FT_Load_Char(ftface, c, FT_LOAD_RENDER); FT_GlyphSlot& glyph = ftface->glyph; FT_Bitmap& btm = ftface->glyph->bitmap; FT_Bitmap& pbtm = glyph->bitmap; int heigthAdvance = -glyph->bitmap_top + ftface->size->metrics.y_ppem; //stays inside the rectangle, but diacritics go under the bottom //int heigthAdvance = -glyph->bitmap_top; //stays on top of rectangle and intersects it int ytop = pen_y + heigthAdvance; for (int i = 0, xi = pen_x; i < pbtm.width; i++, xi++) { for (int j = 0; j < pbtm.rows; j++) { char pixel_val = pbtm.buffer[j * pbtm.width + i] ^ -1; SetPixel(hdc, xi, ytop + j, RGB(pixel_val, pixel_val, pixel_val)); //almost ok } } pen_x += glyph->advance.x >> 6; } void drawStringFT_IntoDC(FT_Face ftface, HDC hdc, const wchar_t* str, int& pen_x, int& pen_y, SIZE* sz = 0) { for (int i = 0; str[i]; i++) drawGlythBitmap(ftface, hdc, pen_x, pen_y, str[i]); } void drawStringFT_IntoDC(FT_Face ftface, HDC hdc, const wchar_t* str, unsigned int fontSizeFT, int& pen_x, int& pen_y, SIZE* sz = 0) { FT_Set_Pixel_Sizes(ftface, 0, fontSizeFT); drawStringFT_IntoDC(ftface, hdc, str, pen_x, pen_y, sz); } class CDemoWindow : public CWindowImpl { FT_Library ftlib; FT_Face ftface; public: CDemoWindow() { FT_Init_FreeType(&ftlib); FT_New_Face(ftlib, "C:/Windows/Fonts/arial.ttf", 0, &ftface); } virtual ~CDemoWindow() { FT_Done_Face(ftface); FT_Done_FreeType(ftlib); } /////////////////////////// Message Handlers ///////////////////////////////// LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { return 1; } LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { PAINTSTRUCT ps; HDC hdc = BeginPaint(&ps); scope_guard sg_ps([this, &ps]() { EndPaint(&ps); }); int xstart = 50, pen_x = 50, pen_y = 200, ystart = 200, step = 30; std::wstring str = L"Hello, 你好, 日本"; unsigned int fontSizeFT = 48; drawStringFT_IntoDC(ftface, hdc, str.c_str(), fontSizeFT, pen_x, pen_y); bHandled = TRUE; return 0; } LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { Invalidate(); //repaint request with a click return 0; } LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { switch (wParam) { case VK_ESCAPE: PostQuitMessage(0); bHandled = TRUE; break; } return 0; } LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { PostQuitMessage(0); return 1; } BEGIN_MSG_MAP(CDemoWindow) MESSAGE_HANDLER(WM_CREATE, OnCreate) MESSAGE_HANDLER(WM_PAINT, OnPaint) MESSAGE_HANDLER(WM_DESTROY, OnDestroy) MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp) MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown) END_MSG_MAP() }; int main() { CDemoWindow cwnd; HWND hWnd = cwnd.Create(NULL, CWindow::rcDefault, L"Hello", WS_OVERLAPPEDWINDOW); cwnd.ShowWindow(SW_SHOWNORMAL); cwnd.UpdateWindow(); MSG msg; while (GetMessage(&msg, 0, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } [/code] [b] Замечание 1: [/b] Чтобы предвидеть возможные вопросы, все чары установлены и на месте. Если какое -либо подозрение в файле CPP, содержащее китайские ие символы, это также [b] не < /strong> проблема, это в UTF8, кодируясь с подписью. src = "https://i.sstatic.net/qvowxrnz.png"/> [code] #include #include #include using std::cout; using std::endl; template class scope_guard { T t; public: scope_guard(T _t) :t(_t) {} ~scope_guard() { t(); } }; SIZE paintWithFont(HDC hDC, int x, int y, const wchar_t* str, wchar_t* font, int height = 0, int width = 0, DWORD iCharset = DEFAULT_CHARSET) { HFONT hFont = ::CreateFontW(height, width, 0, 0, FW_DONTCARE, 0, 0, 0, iCharset, 0, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, font); HGDIOBJ hOldFont = ::SelectObject(hDC, hFont); scope_guard fnt_guard([&hFont, &hOldFont]() {::DeleteObject(hFont); ::DeleteObject(hOldFont); }); SIZE sz; ::GetTextExtentPoint32W(hDC, str, wcslen(str), &sz); ::TextOutW(hDC, x, y, str, wcslen(str)); ::SelectObject(hDC, hOldFont); return sz; } class CDemoWindow : public CWindowImpl { public: CDemoWindow() { } virtual ~CDemoWindow() { } /////////////////////////// Message Handlers ///////////////////////////////// LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { return 1; } LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { PAINTSTRUCT ps; HDC hdc = BeginPaint(&ps); scope_guard sg_ps([this, &ps]() { EndPaint(&ps); }); int xstart = 50, pen_x = 50, pen_y = 200, ystart = 200, step = 30; std::wstring str = L"Hello, 你好, 日本"; SIZE sz; unsigned int fontSize = 48; paintWithFont(hdc, 50, ystart, str.c_str(), L"Arial", fontSize); ystart += step; bHandled = TRUE; return 0; } LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { Invalidate(); //repaint request with a click return 0; } LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { switch (wParam) { case VK_ESCAPE: PostQuitMessage(0); bHandled = TRUE; break; } return 0; } LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { PostQuitMessage(0); return 1; } BEGIN_MSG_MAP(CDemoWindow) MESSAGE_HANDLER(WM_CREATE, OnCreate) MESSAGE_HANDLER(WM_PAINT, OnPaint) MESSAGE_HANDLER(WM_DESTROY, OnDestroy) MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp) MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown) END_MSG_MAP() }; int main() { CDemoWindow cwnd; HWND hWnd = cwnd.Create(NULL, CWindow::rcDefault, L"Hello", WS_OVERLAPPEDWINDOW); cwnd.ShowWindow(SW_SHOWNORMAL); cwnd.UpdateWindow(); MSG msg; while (GetMessage(&msg, 0, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } [/code] замечание 3 [/b] коды Unicode для Chars You 4F60, OK 597D, 2F47, книга 672C Подробнее здесь: [url]https://stackoverflow.com/questions/79765185/cant-render-chinese-chars-with-freetype[/url]