iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IGraphicsWinFonts.h
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
13#include <windows.h>
14
15#include "IGraphicsPrivate.h"
16
17BEGIN_IPLUG_NAMESPACE
18BEGIN_IGRAPHICS_NAMESPACE
19
20// Fonts
21
23{
24public:
25 InstalledWinFont(void* data, int resSize)
26 : mFontHandle(nullptr)
27 {
28 if (data)
29 {
30 DWORD numFonts = 0;
31 mFontHandle = AddFontMemResourceEx(data, resSize, NULL, &numFonts);
32 }
33 }
34
36 {
37 if (IsValid())
38 RemoveFontMemResourceEx(mFontHandle);
39 }
40
41 InstalledWinFont(const InstalledWinFont&) = delete;
42 InstalledWinFont& operator=(const InstalledWinFont&) = delete;
43
44 bool IsValid() const { return mFontHandle; }
45
46private:
47 HANDLE mFontHandle;
48};
49
51{
52 HFontHolder(HFONT hfont) : mHFont(nullptr)
53 {
54 LOGFONTW lFont = { 0 };
55 GetObjectW(hfont, sizeof(LOGFONTW), &lFont);
56 mHFont = CreateFontIndirectW(&lFont);
57 }
58
59 HFONT mHFont;
60};
61
62class WinFont : public PlatformFont
63{
64public:
65 WinFont(HFONT font, const char* styleName, bool system)
66 : PlatformFont(system), mFont(font), mStyleName(styleName) {}
67 ~WinFont()
68 {
69 DeleteObject(mFont);
70 }
71
72 FontDescriptor GetDescriptor() override { return mFont; }
73
74 IFontDataPtr GetFontData() override
75 {
76 HDC hdc = CreateCompatibleDC(NULL);
77 IFontDataPtr fontData(new IFontData());
78
79 if (hdc != NULL)
80 {
81 SelectObject(hdc, mFont);
82 const size_t size = ::GetFontData(hdc, 0, 0, NULL, 0);
83
84 if (size != GDI_ERROR)
85 {
86 fontData = std::make_unique<IFontData>(size);
87
88 if (fontData->GetSize() == size)
89 {
90 size_t result = ::GetFontData(hdc, 0x66637474, 0, fontData->Get(), size);
91 if (result == GDI_ERROR)
92 result = ::GetFontData(hdc, 0, 0, fontData->Get(), size);
93 if (result == size)
94 fontData->SetFaceIdx(GetFaceIdx(fontData->Get(), fontData->GetSize(), mStyleName.Get()));
95 }
96 }
97
98 DeleteDC(hdc);
99 }
100
101 return fontData;
102 }
103
104private:
105 HFONT mFont;
106 WDL_String mStyleName;
107};
108
109END_IGRAPHICS_NAMESPACE
110END_IPLUG_NAMESPACE