iPlug2 - C++ Audio Plug-in Framework
IGraphicsCoreText.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 <CoreText/CoreText.h>
14 #include "IGraphicsStructs.h"
15 
16 BEGIN_IPLUG_NAMESPACE
17 BEGIN_IGRAPHICS_NAMESPACE
18 
19 class CoreTextFont : public PlatformFont
20 {
21 public:
22  CoreTextFont(CTFontDescriptorRef descriptor, CGDataProviderRef provider, const char * styleString, bool system)
23  : PlatformFont(system)
24  , mDescriptor(descriptor)
25  , mProvider(provider)
26  , mStyleString(styleString)
27  {}
28 
29  ~CoreTextFont();
30 
31  FontDescriptor GetDescriptor() override { return mDescriptor; }
32  IFontDataPtr GetFontData() override;
33 
34 private:
35  CTFontDescriptorRef mDescriptor;
36  CGDataProviderRef mProvider;
37  WDL_String mStyleString;
38 };
39 
40 template <class T>
41 class CFLocal
42 {
43 public:
44  CFLocal(T obj)
45  : mObject(obj)
46  {}
47 
48  ~CFLocal()
49  {
50  if (mObject)
51  CFRelease(mObject);
52  }
53 
54  CFLocal(const CFLocal&) = delete;
55  CFLocal& operator=(const CFLocal&) = delete;
56 
57  T Get() { return mObject; }
58 
59  T Release()
60  {
61  T prev = mObject;
62  mObject = nullptr;
63  return prev;
64  }
65 
66 private:
67  T mObject;
68 };
69 
70 class CoreTextFontDescriptor
71 {
72 public:
73  CoreTextFontDescriptor(CTFontDescriptorRef descriptor, double EMRatio)
74  : mDescriptor(descriptor)
75  , mEMRatio(EMRatio)
76  {
77  CFRetain(mDescriptor);
78  }
79 
80  ~CoreTextFontDescriptor()
81  {
82  CFRelease(mDescriptor);
83  }
84 
85  CoreTextFontDescriptor(const CoreTextFontDescriptor&) = delete;
86  CoreTextFontDescriptor& operator=(const CoreTextFontDescriptor&) = delete;
87 
88  CTFontDescriptorRef GetDescriptor() const { return mDescriptor; }
89  double GetEMRatio() const { return mEMRatio; }
90 
91 private:
92  CTFontDescriptorRef mDescriptor;
93  double mEMRatio;
94 };
95 
96 namespace CoreTextHelpers
97 {
98  extern PlatformFontPtr LoadPlatformFont(const char* fontID, const char* fileNameOrResID, const char* bundleID, const char* sharedResourceSubPath = nullptr);
99 
100  extern PlatformFontPtr LoadPlatformFont(const char* fontID, const char* fontName, ETextStyle style);
101 
102  extern PlatformFontPtr LoadPlatformFont(const char* fontID, void* pData, int dataSize);
103 
104  extern void CachePlatformFont(const char* fontID, const PlatformFontPtr& font, StaticStorage<CoreTextFontDescriptor>& cache);
105 
106  CoreTextFontDescriptor* GetCTFontDescriptor(const IText& text, StaticStorage<CoreTextFontDescriptor>& cache);
107 }
108 
109 END_IGRAPHICS_NAMESPACE
110 END_IPLUG_NAMESPACE
IText is used to manage font and text/text entry style for a piece of text on the UI...