Sync TortoiseIDiff and TortoiseUDiff from TortoiseSVN
[TortoiseGit.git] / src / Utils / MiscUI / ExtTextOutFL.h
blobdbcfc6b616be475228649b7ae02d719921f6d001
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008, 2010 - 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 ATL::CComPtr<IMLangFontLink2> pfl;
30 HRESULT hr = pfl.CoCreateInstance(CLSID_CMultiLanguage, NULL, CLSCTX_ALL);
31 if (FAILED(hr))
32 return hr;
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);
92 return hr;
95 void TextOutTryFL(HDC hdc, int x, int y, LPCWSTR psz, int cch)
97 if (FAILED(TextOutFL(hdc, x, y, psz, cch)))
99 TextOut(hdc, x, y, psz, cch);