Cleanup
[TortoiseGit.git] / src / Utils / MiscUI / ExtTextOutFL.h
blobc7c7cde7effa9c52140387b9aa9f11bf6e866718
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
20 #include <mlang.h>
23 // these functions are taken from an article of the 'old new thing' blog:
24 // http://blogs.msdn.com/oldnewthing/archive/2004/07/16/185261.aspx
27 HRESULT TextOutFL(HDC hdc, int x, int y, LPCWSTR psz, int cch)
29 HRESULT hr;
30 IMLangFontLink2 *pfl;
31 if (SUCCEEDED(hr = CoCreateInstance(CLSID_CMultiLanguage, NULL,
32 CLSCTX_ALL, IID_IMLangFontLink2, (void**)&pfl)))
34 HFONT hfOrig = (HFONT)GetCurrentObject(hdc, OBJ_FONT);
35 POINT ptOrig;
36 DWORD dwAlignOrig = GetTextAlign(hdc);
37 if (!(dwAlignOrig & TA_UPDATECP))
39 SetTextAlign(hdc, dwAlignOrig | TA_UPDATECP);
41 MoveToEx(hdc, x, y, &ptOrig);
42 DWORD dwFontCodePages = 0;
43 hr = pfl->GetFontCodePages(hdc, hfOrig, &dwFontCodePages);
44 if (SUCCEEDED(hr))
46 while (cch > 0)
48 DWORD dwActualCodePages;
49 long cchActual;
50 hr = pfl->GetStrCodePages(psz, cch, dwFontCodePages, &dwActualCodePages, &cchActual);
51 if (FAILED(hr))
53 break;
56 if (dwActualCodePages & dwFontCodePages)
58 TextOut(hdc, 0, 0, psz, cchActual);
60 else
62 HFONT hfLinked;
63 if (FAILED(hr = pfl->MapFont(hdc, dwActualCodePages, 0, &hfLinked)))
65 break;
67 SelectObject(hdc, (HGDIOBJ)(HFONT)hfLinked);
68 TextOut(hdc, 0, 0, psz, cchActual);
69 SelectObject(hdc, (HGDIOBJ)(HFONT)hfOrig);
70 pfl->ReleaseFont(hfLinked);
72 psz += cchActual;
73 cch -= cchActual;
75 if (FAILED(hr))
77 // We started outputting characters so we have to finish.
78 // Do the rest without font linking since we have no choice.
79 TextOut(hdc, 0, 0, psz, cch);
80 hr = S_FALSE;
84 pfl->Release();
86 if (!(dwAlignOrig & TA_UPDATECP))
88 SetTextAlign(hdc, dwAlignOrig);
89 MoveToEx(hdc, ptOrig.x, ptOrig.y, NULL);
93 return hr;
96 void TextOutTryFL(HDC hdc, int x, int y, LPCWSTR psz, int cch)
98 if (FAILED(TextOutFL(hdc, x, y, psz, cch)))
100 TextOut(hdc, x, y, psz, cch);