-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmisc.cpp
More file actions
3633 lines (3086 loc) · 112 KB
/
misc.cpp
File metadata and controls
3633 lines (3086 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* \file misc.cpp
* \brief Misc Library File
*
* This file contains the main DCX dll routines.
*
* \author David Legault ( clickhere at scriptsdb dot org )
* \version 1.0
*
* \b Revisions
*
* © ScriptsDB.org - 2006-2008
*/
#include "defines.h"
//#include "Dcx.h"
#include <shlwapi.h>
// DCX Stuff
/*!
* \brief Rounding function
*/
int dcx_round(const float x) noexcept
{
const auto t = gsl::narrow_cast<int>(x);
if ((x - gsl::narrow_cast<float>(t)) > 0.5f)
return t + 1;
return t;
}
namespace
{
// taken from: http://www.codeproject.com/Tips/317642/Handling-simple-text-files-in-C-Cplusplus
//PTSTR Normalise(PBYTE pBuffer)
//{
// PTSTR ptText; // pointer to the text char* or wchar_t* depending on UNICODE setting
// PWSTR pwStr; // pointer to a wchar_t buffer
// int nLength; // a useful integer variable
//
// //IsTextUnicode(pBuffer, iSize, iRes);
//
// // obtain a wide character pointer to check BOMs
// pwStr = reinterpret_cast<PWSTR>(pBuffer);
//
// // check if the first word is a Unicode Byte Order Mark
// if (*pwStr == 0xFFFE || *pwStr == 0xFEFF)
// {
// // Yes, this is Unicode data
// if (*pwStr++ == 0xFFFE)
// {
// // BOM says this is Big Endian so we need
// // to swap bytes in each word of the text
// while (*pwStr)
// {
// // swap bytes in each word of the buffer
// WCHAR wcTemp = *pwStr >> 8;
// wcTemp |= *pwStr << 8;
// *pwStr = wcTemp;
// ++pwStr;
// }
// // point back to the start of the text
// pwStr = reinterpret_cast<PWSTR>(pBuffer + 2);
// }
//#if !defined(UNICODE)
// // This is a non-Unicode project so we need
// // to convert wide characters to multi-byte
//
// // get calculated buffer size
// nLength = WideCharToMultiByte(CP_UTF8, 0, pwStr, -1, nullptr, 0, nullptr, nullptr);
// // obtain a new buffer for the converted characters
// ptText = new TCHAR[nLength];
// // convert to multi-byte characters
// nLength = WideCharToMultiByte(CP_UTF8, 0, pwStr, -1, ptText, nLength, nullptr, nullptr);
//#else
// nLength = wcslen(pwStr) + 1; // if Unicode, then copy the input text
// ptText = new WCHAR[nLength]; // to a new output buffer
// nLength *= sizeof(WCHAR); // adjust to size in bytes
// memcpy_s(ptText, nLength, pwStr, nLength);
//#endif
// }
// else
// {
// // The text data is UTF-8 or Ansi
//#if defined(UNICODE)
// // This is a Unicode project so we need to convert
// // multi-byte or Ansi characters to Unicode.
//
// // get calculated buffer size
// nLength = MultiByteToWideChar(CP_UTF8, 0, reinterpret_cast<PCSTR>(pBuffer), -1, nullptr, 0);
// // obtain a new buffer for the converted characters
// ptText = new TCHAR[nLength];
// // convert to Unicode characters
// nLength = MultiByteToWideChar(CP_UTF8, 0, reinterpret_cast<PCSTR>(pBuffer), -1, ptText, nLength);
//#else
// // This is a non-Unicode project so we just need
// // to skip the UTF-8 BOM, if present
// if (memcmp(pBuffer, "\xEF\xBB\xBF", 3) == 0)
// {
// // UTF-8
// pBuffer += 3;
// }
// nLength = strlen(reinterpret_cast<PSTR>(pBuffer)) + 1; // if UTF-8/ANSI, then copy the input text
// ptText = new char[nLength]; // to a new output buffer
// memcpy_s(ptText, nLength, pBuffer, nLength);
//#endif
// }
//
// // return pointer to the (possibly converted) text buffer.
// return ptText;
//}
TString Normalise(PBYTE pBuffer)
{
// pointer to a wchar_t buffer
// obtain a wide character pointer to check BOMs
PWSTR pwStr = reinterpret_cast<PWSTR>(pBuffer);
if (!pwStr)
return TString();
// check if the first word is a Unicode Byte Order Mark
if (*pwStr == 0xFFFE || *pwStr == 0xFEFF)
{
// Yes, this is Unicode data
if (*pwStr++ == 0xFFFE)
{
// BOM says this is Big Endian so we need
// to swap bytes in each word of the text
while (*pwStr)
{
// swap bytes in each word of the buffer
WCHAR wcTemp = static_cast<WCHAR>((*pwStr >> 8));
wcTemp |= (*pwStr << 8);
*pwStr = wcTemp;
++pwStr;
}
// point back to the start of the text
pwStr = reinterpret_cast<PWSTR>(pBuffer + 2);
}
// creation of TString object handles any conversions & memory allocations etc..
//return pwStr;
return TString(pwStr);
}
else
{
// The text data is UTF-8 or Ansi
// skip the UTF-8 BOM, if present
if (memcmp(pBuffer, "\xEF\xBB\xBF", 3) == 0)
{
// UTF-8
pBuffer += 3;
}
}
// return pointer to the (possibly converted) text buffer.
// creation of TString object handles any conversions & memory allocations etc..
return TString(reinterpret_cast<PCSTR>(pBuffer));
}
// Taken from msft examples.
PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp) noexcept
{
if (!hBmp)
return nullptr;
BITMAP bmp{};
PBITMAPINFO pbmi{};
WORD cClrBits{};
// Retrieve the bitmap color format, width, and height.
if (!GetObject(hBmp, sizeof(BITMAP), &bmp))
return nullptr;
// Convert the color format to a count of bits.
cClrBits = gsl::narrow_cast<WORD>(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1)
cClrBits = 1;
else if (cClrBits <= 4)
cClrBits = 4;
else if (cClrBits <= 8)
cClrBits = 8;
else if (cClrBits <= 16)
cClrBits = 16;
else if (cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;
// Allocate memory for the BITMAPINFO structure. (This structure
// contains a BITMAPINFOHEADER structure and an array of RGBQUAD
// data structures.)
if (cClrBits < 24)
pbmi = static_cast<PBITMAPINFO>(LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD) * (static_cast<size_t>(1U) << cClrBits))));
// There is no RGBQUAD array for these formats: 24-bit-per-pixel or 32-bit-per-pixel
else
pbmi = static_cast<PBITMAPINFO>(LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER)));
if (!pbmi)
return nullptr;
// Initialize the fields in the BITMAPINFO structure.
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (cClrBits < 24)
pbmi->bmiHeader.biClrUsed = (1 << cClrBits);
// If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB;
// Compute the number of bytes in the array of color
// indices and store the result in biSizeImage.
// The width must be DWORD aligned unless the bitmap is RLE
// compressed.
pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits + 31) & ~31) / 8 * pbmi->bmiHeader.biHeight;
// Set biClrImportant to 0, indicating that all of the
// device colors are important.
pbmi->bmiHeader.biClrImportant = 0;
return pbmi;
}
// Taken from msft examples.
bool CreateBMPFile(LPCTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC) noexcept
{
if ((!pszFile) || (!pbi) || (!hBMP) || (!hDC))
return false;
PBITMAPINFOHEADER pbih = reinterpret_cast<PBITMAPINFOHEADER>(pbi); // bitmap info-header
LPBYTE lpBits = static_cast<LPBYTE>(GlobalAlloc(GMEM_FIXED, pbih->biSizeImage)); // memory pointer
if (!lpBits)
return false;
Auto(GlobalFree(lpBits));
// Retrieve the color table (RGBQUAD array) and the bits
// (array of palette indices) from the DIB.
if (!GetDIBits(hDC, hBMP, 0, pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS))
return false;
//Dcx::dcxFileHandleResource hf(pszFile, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
// Create the .BMP file.
auto hf = CreateFile(pszFile,
GENERIC_READ | GENERIC_WRITE,
0,
nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
nullptr);
if (hf == INVALID_HANDLE_VALUE)
return false;
Auto(CloseHandle(hf));
BITMAPFILEHEADER hdr{}; // bitmap file-header
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file.
hdr.bfSize = (sizeof(BITMAPFILEHEADER) + pbih->biSize + (pbih->biClrUsed * sizeof(RGBQUAD)) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
// Compute the offset to the array of color indices.
hdr.bfOffBits = sizeof(BITMAPFILEHEADER) + pbih->biSize + (pbih->biClrUsed * sizeof(RGBQUAD));
DWORD dwTmp{};
// Copy the BITMAPFILEHEADER into the .BMP file.
if (!WriteFile(hf, &hdr, sizeof(BITMAPFILEHEADER), &dwTmp, nullptr))
return false;
// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
if (!WriteFile(hf, pbih, sizeof(BITMAPINFOHEADER) + (pbih->biClrUsed * sizeof(RGBQUAD)), &dwTmp, nullptr))
return false;
// Copy the array of color indices into the .BMP file.
if (!WriteFile(hf, lpBits, pbih->biSizeImage, &dwTmp, nullptr))
return false;
return true;
}
/*!
* \brief Save a HBITMAP object to file
*
* TODO: ...
*/
bool SaveDataToFile(const TString& tsFile, const HBITMAP hBm) noexcept
{
if ((!hBm) || (tsFile.empty()))
return false;
auto pbis = CreateBitmapInfoStruct(hBm);
if (!pbis)
return false;
Auto(LocalFree(pbis));
auto hDC = CreateCompatibleDC(nullptr);
if (!hDC)
return false;
Auto(DeleteDC(hDC));
auto oldBm = SelectObject(hDC, hBm);
Auto(SelectObject(hDC, oldBm));
return CreateBMPFile(tsFile.to_chr(), pbis, hBm, hDC);
}
bool SaveClipboardAsText(const TString& tsFile)
{
if (auto hData = GetClipboardData(CF_UNICODETEXT); hData)
{
// clipboard has a string
auto szClip = static_cast<LPCWSTR>(GlobalLock(hData));
Auto(GlobalUnlock(hData));
return SaveDataToFile(tsFile, szClip);
}
return false;
}
bool SaveClipboardAsBitmap(const TString& tsFile) noexcept
{
if (auto hBm = static_cast<HBITMAP>(GetClipboardData(CF_BITMAP)); hBm)
{
// clipboard has a bitmap
return SaveDataToFile(tsFile, hBm);
}
return false;
}
HICON CreateGrayscaleIcon(HICON hIcon, const COLORREF* const pPalette) noexcept
{
if ((!hIcon) || (!pPalette))
return nullptr;
auto hdc = ::GetDC(nullptr);
Auto(::ReleaseDC(nullptr, hdc));
HICON hGrayIcon{ nullptr };
ICONINFO icInfo{};
ICONINFO icGrayInfo{};
BITMAPINFO bmpInfo = { 0 };
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
if (::GetIconInfo(hIcon, &icInfo))
{
if (icInfo.hbmColor)
{
Auto(::DeleteObject(icInfo.hbmColor));
if (::GetDIBits(hdc, icInfo.hbmColor, 0, 0, nullptr, &bmpInfo, DIB_RGB_COLORS) != 0)
{
const SIZE sz{ bmpInfo.bmiHeader.biWidth, bmpInfo.bmiHeader.biHeight };
bmpInfo.bmiHeader.biCompression = BI_RGB;
const auto c1 = gsl::narrow_cast<DWORD>(sz.cx * sz.cy);
const auto lpBits = static_cast<LPDWORD>(::GlobalAlloc(GMEM_FIXED, (gsl::narrow_cast<SIZE_T>(c1)) * 4));
if (!lpBits)
return nullptr;
Auto(::GlobalFree(lpBits));
if (::GetDIBits(hdc, icInfo.hbmColor, 0, gsl::narrow_cast<UINT>(sz.cy), lpBits, &bmpInfo, DIB_RGB_COLORS) != 0)
{
auto lpBitsPtr = reinterpret_cast<LPBYTE>(lpBits);
for (auto i = decltype(c1){0}; i < c1; ++i)
{
auto off = gsl::narrow_cast<UINT>((255 - ((lpBitsPtr[0] + lpBitsPtr[1] + lpBitsPtr[2]) / 3)));
if (lpBitsPtr[3] != 0 || off != 255)
{
if (off == 0)
{
off = 1;
}
lpBits[i] = pPalette[off] | (lpBitsPtr[3] << 24);
}
lpBitsPtr += 4;
}
icGrayInfo.hbmColor = ::CreateCompatibleBitmap(hdc, sz.cx, sz.cy);
if (icGrayInfo.hbmColor)
{
Auto(::DeleteObject(icGrayInfo.hbmColor));
::SetDIBits(hdc, icGrayInfo.hbmColor, 0, gsl::narrow_cast<UINT>(sz.cy), lpBits, &bmpInfo, DIB_RGB_COLORS);
icGrayInfo.hbmMask = icInfo.hbmMask;
icGrayInfo.fIcon = TRUE;
hGrayIcon = ::CreateIconIndirect(&icGrayInfo);
}
}
}
if (icInfo.hbmMask)
::DeleteObject(icInfo.hbmMask);
}
}
return hGrayIcon;
}
constexpr int unfoldColor(int nColor) noexcept
{
while (nColor > 99)
nColor -= 100;
return nColor;
}
void mIRC_OutText(HDC hdc, LPCRECT rcBounds, TString& txt, LPRECT rcOut, const LPLOGFONT lf, const UINT iStyle, const COLORREF clrFG, const bool shadow) noexcept
{
//if (txt.empty() || !rcOut || !lf || !hdc)
// return;
//
//const auto len = txt.len();
//auto hNewFont = CreateFontIndirectW(lf);
//if (!hNewFont)
// return;
//
//const auto hOldFont = SelectObject(hdc, hNewFont);
//auto rcTmp = *rcOut;
//
//TEXTMETRICW tm{};
//GetTextMetricsW(hdc, std::addressof(tm));
//
//if (!dcx_testflag(iStyle, DT_CALCRECT))
//{ // if DT_CALCRECT flag NOT given then do calcrect here.
// //if (shadow)
// // dcxDrawShadowText(hdc, txt.to_wchr(), gsl::narrow_cast<UINT>(len), std::addressof(rcTmp), iStyle | DT_CALCRECT, clrFG, 0, 5, 5);
// //else
// // DrawTextW(hdc, txt.to_wchr(), gsl::narrow_cast<int>(len), std::addressof(rcTmp), iStyle | DT_CALCRECT);
// DRAWTEXTPARAMS params{};
// params.cbSize = sizeof(DRAWTEXTPARAMS);
// params.iTabLength = 4;
//
// DrawTextExW(hdc, txt.to_wchr(), gsl::narrow_cast<int>(len), std::addressof(rcTmp), iStyle | DT_CALCRECT, ¶ms);
//
// if (params.uiLengthDrawn < len)
// {
// if (rcTmp.left != rcBounds->left)
// {
// rcTmp.left = rcBounds->left;
// rcTmp.top += tm.tmHeight;
// rcOut->left = rcBounds->left;
// rcOut->top = rcTmp.top;
//
// DrawTextExW(hdc, txt.to_wchr(), gsl::narrow_cast<int>(len), std::addressof(rcTmp), iStyle | DT_CALCRECT, ¶ms);
// }
// }
//}
//if (shadow)
// dcxDrawShadowText(hdc, txt.to_wchr(), gsl::narrow_cast<UINT>(len), std::addressof(rcTmp), iStyle, clrFG, 0, 5, 5);
//else
// DrawTextW(hdc, txt.to_wchr(), gsl::narrow_cast<int>(len), std::addressof(rcTmp), iStyle);
//
//if (tm.tmHeight != 0)
//{
// if (!dcx_testflag(iStyle, DT_SINGLELINE))
// {
// if ((rcTmp.bottom - rcTmp.top) >= (tm.tmHeight * 2))
// {
// //rcOut->top += tm.tmHeight;
// rcOut->top += (rcTmp.bottom - rcTmp.top);
// rcOut->left = rcBounds->left;
// }
// else
// rcOut->left += (rcTmp.right - rcTmp.left) - tm.tmOverhang;
// }
// else
// rcOut->left += (rcTmp.right - rcTmp.left) - tm.tmOverhang;
//}
//else
// rcOut->left += (rcTmp.right - rcTmp.left);
//
//DeleteObject(SelectObject(hdc, hOldFont));
//txt.clear();
if (txt.empty() || !rcOut || !lf || !hdc)
return;
const auto len = txt.len();
auto hNewFont = CreateFontIndirectW(lf);
if (!hNewFont)
return;
const auto hOldFont = SelectObject(hdc, hNewFont);
auto rcTmp = *rcOut;
TEXTMETRICW tm{};
GetTextMetricsW(hdc, std::addressof(tm));
DRAWTEXTPARAMS params{};
params.cbSize = sizeof(DRAWTEXTPARAMS);
params.iTabLength = 4;
WCHAR* pText = txt.to_wchr();
for (UINT uCharsDrawn{}; uCharsDrawn < len; uCharsDrawn += params.uiLengthDrawn)
{
params.uiLengthDrawn = 0;
// see if text fits...
DrawTextExW(hdc, pText, gsl::narrow_cast<int>(len - uCharsDrawn), std::addressof(rcTmp), iStyle | DT_CALCRECT, ¶ms);
// doesnt all fit on this line
if (params.uiLengthDrawn < (len - uCharsDrawn))
{
// if not at the start of the line, dropdown to the next & try again.
if (rcTmp.left != rcBounds->left)
{
rcTmp = *rcOut;
rcTmp.left = rcBounds->left;
rcTmp.top += tm.tmHeight;
rcOut->left = rcBounds->left;
rcOut->top = rcTmp.top;
DrawTextExW(hdc, pText, gsl::narrow_cast<int>(len - uCharsDrawn), std::addressof(rcTmp), iStyle | DT_CALCRECT, ¶ms);
}
}
if (shadow)
dcxDrawShadowText(hdc, pText, gsl::narrow_cast<UINT>(params.uiLengthDrawn), std::addressof(rcTmp), iStyle, clrFG, 0, 5, 5);
else
DrawTextW(hdc, pText, gsl::narrow_cast<int>(params.uiLengthDrawn), std::addressof(rcTmp), iStyle);
// check if all characters were drawn
if ((params.uiLengthDrawn + uCharsDrawn) < len)
{
//if (rcTmp.left != rcBounds->left)
{
rcTmp.left = rcBounds->left;
rcTmp.top += tm.tmHeight;
rcOut->left = rcBounds->left;
rcOut->top = rcTmp.top;
}
pText += params.uiLengthDrawn;
}
if (rcTmp.bottom > rcBounds->bottom)
break;
}
if (tm.tmHeight != 0)
{
if (!dcx_testflag(iStyle, DT_SINGLELINE))
{
if ((rcTmp.bottom - rcTmp.top) >= (tm.tmHeight * 2))
{
//rcOut->top += tm.tmHeight;
rcOut->top += (rcTmp.bottom - rcTmp.top);
rcOut->left = rcBounds->left;
}
else
rcOut->left += (rcTmp.right - rcTmp.left) - tm.tmOverhang;
}
else
rcOut->left += (rcTmp.right - rcTmp.left) - tm.tmOverhang;
}
else
rcOut->left += (rcTmp.right - rcTmp.left);
DeleteObject(SelectObject(hdc, hOldFont));
txt.clear();
}
double sRGBtoLin(double colorChannel) noexcept
{
// Send this function a decimal sRGB gamma encoded color value
// between 0.0 and 1.0, and it returns a linearized value.
if (colorChannel <= 0.04045)
return colorChannel / 12.92;
return pow(((colorChannel + 0.055) / 1.055), 2.4);
}
double YtoLstar(double Y) noexcept
{
// Send this function a luminance value between 0.0 and 1.0,
// and it returns L* which is "perceptual lightness"
if (Y <= (216.0 / 24389.0)) // The CIE standard states 0.008856 but 216/24389 is the intent for 0.008856451679036
return Y * (24389.0 / 27.0); // The CIE standard states 903.3, but 24389/27 is the intent, making 903.296296296296296
return pow(Y, (1.0 / 3.0)) * 116.0 - 16.0;
}
#ifdef DCX_USE_GDIPLUS
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
//UINT num = 0; // number of image encoders
//UINT size = 0; // size of the image encoder array in bytes
//
//Gdiplus::GetImageEncodersSize(&num, &size);
//if (size == 0)
// return -1; // Failure
//
//Gdiplus::ImageCodecInfo* pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
//if (pImageCodecInfo == nullptr)
// return -1; // Failure
//
//GetImageEncoders(num, size, pImageCodecInfo);
//
//for (UINT j = 0; j < num; ++j)
//{
// if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
// {
// *pClsid = pImageCodecInfo[j].Clsid;
// free(pImageCodecInfo);
// return j; // Success
// }
//}
//
//free(pImageCodecInfo);
//return -1; // Failure
if (!format || !pClsid)
return -1;
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
Gdiplus::GetImageEncodersSize(&num, &size);
if (size == 0)
return -1; // Failure
std::vector<BYTE> codecdata;
codecdata.reserve(size);
Gdiplus::ImageCodecInfo* pImageCodecInfo = reinterpret_cast<Gdiplus::ImageCodecInfo*>(codecdata.data());
if (pImageCodecInfo == nullptr)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for (UINT j = 0; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
return j; // Success
}
}
return -1; // Failure
}
bool CreatePNGFile(const WCHAR* pszFile, Gdiplus::Image& img)
{
if (!pszFile)
return false;
CLSID pngClsid;
if (GetEncoderClsid(L"image/png", &pngClsid) != -1)
{
if (img.Save(pszFile, &pngClsid) == Gdiplus::Status::Ok)
return true;
}
return false;
}
#endif
}
/*!
* \brief Read File Contents
*
*/
auto readFile(const TString& filename)
-> std::unique_ptr<BYTE[]>
{
if (filename.empty())
return nullptr;
#if DCX_USE_WRAPPERS
const Dcx::dcxFileResource file(filename, TEXT("rb"));
const auto size = file.Size();
// make data container for file contents
auto fileContents = std::make_unique<BYTE[]>(gsl::narrow_cast<size_t>(size) + 2);
// Null terminate the string (use double zero)
gsl::at(fileContents, size) = 0;
gsl::at(fileContents, gsl::narrow_cast<gsl::index>(size) + 1U) = 0;
// read the file
fread(fileContents.get(), 1U, size, file.get());
// return memory block containing file data
return fileContents;
#else
auto file = dcx_fopen(filename, TEXT("rb"));
// Open file in binary mode and read
if (!file)
return nullptr;
Auto(fclose(file));
// Seek End of file
if (fseek(file, 0, SEEK_END))
return nullptr;
// Read pointer location, because pointer is at the end, results into file size
const auto size = gsl::narrow_cast<size_t>(ftell(file));
// Get back to file beginning
if (fseek(file, 0, SEEK_SET))
return nullptr;
// make data container for file contents
auto fileContents = std::make_unique<BYTE[]>(size + 2);
// Null terminate the string (use double zero)
fileContents[size] = 0;
fileContents[size + 1U] = 0;
// read the file
fread(fileContents.get(), 1U, size, file);
// return memory block containing file data
return fileContents;
#endif
}
/*!
* \brief Read Text File Contents
*
* This function correctly handles BOM types for ascii,utf8,utf16BE,utf16LE & returns text in a TString object.
*/
TString readTextFile(const TString& tFile)
{
const auto data(readFile(tFile));
return Normalise(data.get());
}
/*!
* \brief Save a TString object to file as text
*
* TODO: ...
*/
bool SaveDataToFile(const TString& tsFile, const TString& tsData)
{
#if DCX_USE_WRAPPERS
const Dcx::dcxFileResource file(tsFile, TEXT("wb"));
constexpr WCHAR tBOM = 0xFEFF; // unicode BOM
fwrite(&tBOM, sizeof(WCHAR), 1, file.get());
// if not in unicode mode then save without BOM as ascii/utf8
fwrite(tsData.to_chr(), sizeof(TString::value_type), tsData.len(), file.get());
fflush(file.get());
return true;
#else
auto file = dcx_fopen(tsFile.to_chr(), TEXT("wb"));
if (!file)
return false;
Auto(fclose(file));
constexpr WCHAR tBOM = 0xFEFF; // unicode BOM
fwrite(&tBOM, sizeof(WCHAR), 1, file);
// if not in unicode mode then save without BOM as ascii/utf8
fwrite(tsData.to_chr(), sizeof(TString::value_type), tsData.len(), file);
fflush(file);
return true;
#endif
}
/*!
* \brief Copies string to the clipboard
*
* Returns TRUE if successful
*
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/dataexchange/clipboard/usingtheclipboard.asp
*/
bool CopyToClipboard(const HWND owner, const TString& str) noexcept
{
if (!OpenClipboard(owner))
{
Dcx::error(TEXT("CopyToClipboard"), TEXT("Couldn't open clipboard"));
return false;
}
Auto(CloseClipboard());
const size_t cbsize = (str.len() + 1) * sizeof(TCHAR);
EmptyClipboard();
auto hglbCopy = GlobalAlloc(GMEM_MOVEABLE, cbsize);
if (!hglbCopy)
{
Dcx::error(TEXT("CopyToClipboard"), TEXT("Couldn't open global memory"));
return false;
}
{ // simply sets scope for vars
const auto strCopy = static_cast<TCHAR*>(GlobalLock(hglbCopy));
if (!strCopy)
{
GlobalFree(hglbCopy);
Dcx::error(TEXT("CopyToClipboard"), TEXT("Couldn't lock global memory"));
return false;
}
dcx_strcpyn(strCopy, str.to_chr(), str.len() + 1);
GlobalUnlock(hglbCopy);
}
SetClipboardData(CF_UNICODETEXT, hglbCopy);
return true;
}
std::pair<bool, int> SaveClipboardToFile(const XSwitchFlags& xFlags, const TString& tsFile)
{
if (!OpenClipboard(nullptr))
throw Dcx::dcxException(TEXT("SaveClipboardToFile: %"), TEXT("Couldn't open clipboard"));
Auto(CloseClipboard());
if (!xFlags[TEXT('+')])
throw DcxExceptions::dcxInvalidFlag();
// text save only
if (xFlags[TEXT('t')])
return { SaveClipboardAsText(tsFile), CF_UNICODETEXT };
// bitmap save only
if (xFlags[TEXT('b')])
return { SaveClipboardAsBitmap(tsFile), CF_BITMAP };
// otherwise try both...
if (SaveClipboardAsText(tsFile))
return { true, CF_UNICODETEXT };
return { SaveClipboardAsBitmap(tsFile), CF_BITMAP };
}
//Dcx::BoolValue<int> SaveClipboardToFile(const XSwitchFlags& xFlags, const TString& tsFile)
//{
// if (!OpenClipboard(nullptr))
// throw Dcx::dcxException(TEXT("SaveClipboardToFile: %"), TEXT("Couldn't open clipboard"));
//
// Auto(CloseClipboard());
//
// if (!xFlags[TEXT('+')])
// throw DcxExceptions::dcxInvalidFlag();
//
// // text save only
// if (xFlags[TEXT('t')])
// return { SaveClipboardAsText(tsFile), CF_UNICODETEXT };
//
// // bitmap save only
// if (xFlags[TEXT('b')])
// return { SaveClipboardAsBitmap(tsFile), CF_BITMAP };
//
// // otherwise try both...
// if (SaveClipboardAsText(tsFile))
// return { true, CF_UNICODETEXT };
//
// return { SaveClipboardAsBitmap(tsFile), CF_BITMAP };
//}
// Turns a command (+flags CHARSET SIZE FONTNAME) into a LOGFONT struct
GSL_SUPPRESS(bounds.3) bool ParseCommandToLogfont(const TString& cmd, LPLOGFONT lf)
{
if (!lf || cmd.numtok() < 4)
return false;
ZeroMemory(lf, sizeof(LOGFONT));
const auto flags = parseFontFlags(cmd.getfirsttok(1));
if (dcx_testflag(flags, dcxFontFlags::DCF_DEFAULT))
return (GetObject(Dcx::dcxGetStockObject<HFONT>(DEFAULT_GUI_FONT), sizeof(LOGFONT), lf) != 0);
lf->lfCharSet = gsl::narrow_cast<BYTE>(parseFontCharSet(cmd.getnexttok()) & 0xFF); // tok 2
const auto fSize = cmd.getnexttokas<int>(); // tok 3
const auto fName(cmd.getlasttoks().trim()); // tok 4, -1
if (!fSize)
return false;
{
const auto hdc = GetDC(nullptr);
if (!hdc)
return false;
Auto(ReleaseDC(nullptr, hdc));
lf->lfHeight = -MulDiv(fSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
}
if (dcx_testflag(flags, dcxFontFlags::DCF_ANTIALIASE))
lf->lfQuality = ANTIALIASED_QUALITY;
if (dcx_testflag(flags, dcxFontFlags::DCF_BOLD))
lf->lfWeight = FW_BOLD;
else if (dcx_testflag(flags, dcxFontFlags::DCF_LIGHT))
lf->lfWeight = FW_LIGHT;
else
lf->lfWeight = FW_NORMAL;
if (dcx_testflag(flags, dcxFontFlags::DCF_ITALIC))
lf->lfItalic = TRUE;
if (dcx_testflag(flags, dcxFontFlags::DCF_STRIKEOUT))
lf->lfStrikeOut = TRUE;
if (dcx_testflag(flags, dcxFontFlags::DCF_UNDERLINE))
lf->lfUnderline = TRUE;
dcx_strcpyn(&lf->lfFaceName[0], fName.to_chr(), std::size(lf->lfFaceName));
gsl::at(lf->lfFaceName, std::size(lf->lfFaceName) - 1) = 0;
return true;
}
dcxFontFlags parseFontFlags(const TString& flags) noexcept
{
dcxFontFlags iFlags{};
const XSwitchFlags xflags(flags);
if (!xflags[TEXT('+')])
return iFlags;
if (xflags[TEXT('a')])
iFlags |= dcxFontFlags::DCF_ANTIALIASE;
if (xflags[TEXT('b')])
iFlags |= dcxFontFlags::DCF_BOLD;
if (xflags[TEXT('d')])
iFlags |= dcxFontFlags::DCF_DEFAULT;
if (xflags[TEXT('i')])
iFlags |= dcxFontFlags::DCF_ITALIC;
if (xflags[TEXT('l')])
iFlags |= dcxFontFlags::DCF_LIGHT;
if (xflags[TEXT('s')])
iFlags |= dcxFontFlags::DCF_STRIKEOUT;
if (xflags[TEXT('u')])
iFlags |= dcxFontFlags::DCF_UNDERLINE;
return iFlags;
}
UINT parseFontCharSet(const TString& charset)
{
const static std::map<std::hash<TString>::result_type, UINT> charset_map{
{TEXT("ansi"_hash), ANSI_CHARSET},
{ TEXT("baltic"_hash), BALTIC_CHARSET },
{ TEXT("chinesebig"_hash), CHINESEBIG5_CHARSET },
{ TEXT("default"_hash), DEFAULT_CHARSET },
{ TEXT("easteurope"_hash), EASTEUROPE_CHARSET },
{ TEXT("gb2312"_hash), GB2312_CHARSET },
{ TEXT("greek"_hash), GREEK_CHARSET },
{ TEXT("hangul"_hash), HANGUL_CHARSET },
{ TEXT("mac"_hash), MAC_CHARSET },
{ TEXT("oem"_hash), OEM_CHARSET },
{ TEXT("russian"_hash), RUSSIAN_CHARSET },
{ TEXT("shiftjis"_hash), SHIFTJIS_CHARSET },
{ TEXT("symbol"_hash), SYMBOL_CHARSET },
{ TEXT("turkish"_hash), TURKISH_CHARSET },
{ TEXT("vietnamese"_hash), VIETNAMESE_CHARSET },
{ TEXT("johab"_hash), JOHAB_CHARSET },
{ TEXT("hebrew"_hash), HEBREW_CHARSET },
{ TEXT("arabic"_hash), ARABIC_CHARSET },
{ TEXT("thai"_hash), THAI_CHARSET }
};
if (const auto it = charset_map.find(std::hash<TString>{}(charset)); it != charset_map.end())
return it->second;
return DEFAULT_CHARSET;
}
TString ParseLogfontToCommand(const LPLOGFONT lf)
{
TString flags(TEXT('+')), charset(TEXT("default"_ts)), tmp;