iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IGraphicsPrivate.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
18#include <string>
19#include <memory>
20
21#include "mutex.h"
22#include "wdlstring.h"
23#include "wdlutf8.h"
24#include "wdlendian.h"
25#include "ptrlist.h"
26#include "heapbuf.h"
27
28#if defined IGRAPHICS_SKIA && !defined IGRAPHICS_NO_SKIA_SVG
29#define SVG_USE_SKIA
30#endif
31
32#ifdef SVG_USE_SKIA
33 #pragma warning( push )
34 #pragma warning( disable : 4244 )
35 #pragma warning( disable : 5030 )
36 #include "modules/svg/include/SkSVGDOM.h"
37 #include "include/core/SkCanvas.h"
38 #include "include/core/SkStream.h"
39 #include "src/xml/SkDOM.h"
40 #pragma warning( pop )
41 #pragma comment(lib, "svg.lib")
42 #pragma comment(lib, "skshaper.lib")
43 #pragma comment(lib, "skunicode_core.lib")
44 #pragma comment(lib, "skunicode_icu.lib")
45#else
46 #include "nanosvg.h"
47#endif
48
49#include "IPlugPlatform.h"
50
51#if defined IGRAPHICS_NANOVG
52 #define BITMAP_DATA_TYPE int;
53#elif defined IGRAPHICS_SKIA
54 #pragma warning( push )
55 #pragma warning( disable : 4244 )
56 #include "include/core/SkImage.h"
57 #include "include/core/SkSurface.h"
58 #pragma warning( pop )
59 struct SkiaDrawable
60 {
61 bool mIsSurface;
62 sk_sp<SkImage> mImage;
63 sk_sp<SkSurface> mSurface;
64 };
65 #define BITMAP_DATA_TYPE SkiaDrawable*
66#else // NO_IGRAPHICS
67 #define BITMAP_DATA_TYPE void*;
68#endif
69
70#if defined OS_MAC || defined OS_IOS
71 #include <CoreText/CoreText.h>
72 #define FONT_DESCRIPTOR_TYPE CTFontDescriptorRef
73#elif defined OS_WIN
74 #include "wingdi.h"
75 #define FONT_DESCRIPTOR_TYPE HFONT
76#elif defined OS_LINUX
77 // Linux uses a path string to locate fonts (resolved via Fontconfig)
78 #define FONT_DESCRIPTOR_TYPE WDL_String*
79#elif defined OS_WEB
80 #define FONT_DESCRIPTOR_TYPE std::pair<WDL_String, WDL_String>*
81#else
82 // NO_IGRAPHICS
83#endif
84
85BEGIN_IPLUG_NAMESPACE
86BEGIN_IGRAPHICS_NAMESPACE
87using BitmapData = BITMAP_DATA_TYPE;
88using FontDescriptor = FONT_DESCRIPTOR_TYPE;
89using RawBitmapData = WDL_TypedBuf<uint8_t>;
90
95{
96public:
97
104 APIBitmap(BitmapData pBitmap, int w, int h, float scale, float drawScale)
105 : mBitmap(pBitmap)
106 , mWidth(w)
107 , mHeight(h)
108 , mScale(scale)
109 , mDrawScale(drawScale)
110 {}
111
112 APIBitmap()
113 : mBitmap(0)
114 , mWidth(0)
115 , mHeight(0)
116 , mScale(0)
117 , mDrawScale(1.f)
118 {}
119
120 virtual ~APIBitmap() {}
121
122 APIBitmap(const APIBitmap&) = delete;
123 APIBitmap& operator=(const APIBitmap&) = delete;
124
131 void SetBitmap(BitmapData pBitmap, int w, int h, float scale, float drawScale)
132 {
133 mBitmap = pBitmap;
134 mWidth = w;
135 mHeight = h;
136 mScale = scale;
137 mDrawScale = drawScale;
138 }
139
141 BitmapData GetBitmap() const { return mBitmap; }
142
144 int GetWidth() const { return mWidth; }
145
147 int GetHeight() const { return mHeight; }
148
150 float GetScale() const { return mScale; }
151
153 float GetDrawScale() const { return mDrawScale; }
154
155private:
156 BitmapData mBitmap; // for most drawing APIs BitmapData is a pointer. For Nanovg it is an integer index
157 int mWidth;
158 int mHeight;
159 float mScale;
160 float mDrawScale;
161};
162
164class IFontInfo
165{
166public:
167 IFontInfo(const void* data, uint32_t dataSize, uint32_t faceIdx)
168 : mData(reinterpret_cast<const unsigned char*>(data))
169 , mDataSize(dataSize)
170 {
171 if (mData)
172 FindFace(faceIdx);
173
174 if (mData)
175 {
176 mHeadLocation = LocateTable("head");
177 mNameLocation = LocateTable("name");
178 mHheaLocation = LocateTable("hhea");
179 mFDscLocation = LocateTable("fdsc");
180
181 if (IsValid())
182 {
183 mUnitsPerEM = GetUInt16(mHeadLocation + 18);
184 mMacStyle = GetUInt16(mHeadLocation + 44);
185 mFamily = SearchFontString(1);
186 mStyle = SearchFontString(2);
187 mAscender = GetSInt16(mHheaLocation + 4);
188 mDescender = GetSInt16(mHheaLocation + 6);
189 mLineGap = GetSInt16(mHheaLocation + 8);
190 }
191 }
192 }
193
194 bool IsValid() const { return mData && mHeadLocation && mNameLocation && mHheaLocation; }
195
196 const WDL_String& GetFamily() const { return mFamily; }
197 const WDL_String& GetStyle() const { return mStyle; }
198
199 bool IsBold() const { return mMacStyle & (1 << 0); }
200 bool IsItalic() const { return mMacStyle & (1 << 1); }
201 bool IsUnderline() const { return mMacStyle & (1 << 2); }
202 bool IsOutline() const { return mMacStyle & (1 << 3); }
203 bool IsShadow() const { return mMacStyle & (1 << 4); }
204 bool IsCondensed() const { return mMacStyle & (1 << 5); }
205 bool IsExpanded() const { return mMacStyle & (1 << 6); }
206
207 double GetHeightEMRatio() const { return mUnitsPerEM / static_cast<double>(mAscender - mDescender); }
208
209 uint16_t GetUnitsPerEM() const { return mUnitsPerEM; }
210 int16_t GetAscender() const { return mAscender; }
211 int16_t GetDescender() const { return mDescender; }
212 int16_t GetLineGap() const { return mLineGap; }
213 int16_t GetLineHeight() const { return (mAscender - mDescender) + mLineGap; }
214
215private:
216
217 enum class EStringID { Mac, Windows };
218
219 bool InBounds(uint32_t loc, uint32_t size) const
220 {
221 return loc <= mDataSize && size <= mDataSize - loc;
222 }
223
224 bool MatchTag(uint32_t loc, const char* tag)
225 {
226 return InBounds(loc, 4) && mData[loc+0] == tag[0] && mData[loc+1] == tag[1] && mData[loc+2] == tag[2] && mData[loc+3] == tag[3];
227 }
228
229 uint32_t LocateTable(const char *tag)
230 {
231 uint16_t numTables = GetUInt16(mFaceLocation + 4);
232
233 for (uint16_t i = 0; i < numTables; ++i)
234 {
235 uint32_t tableLocation = mFaceLocation + 12 + (16 * i);
236 if (MatchTag(tableLocation, tag))
237 {
238 uint32_t location = GetUInt32(tableLocation + 8);
239 return InBounds(location, GetUInt32(tableLocation + 12)) ? location : 0;
240 }
241 }
242
243 return 0;
244 }
245
246 WDL_String SearchFontString(int nameID)
247 {
248 WDL_String str = GetFontString(nameID, EStringID::Windows);
249
250 if (str.GetLength())
251 return str;
252
253 return GetFontString(nameID, EStringID::Mac);
254 }
255
256 WDL_String GetFontString(int nameID, EStringID stringID)
257 {
258 // Default to windows values
259
260 int platformID = 3;
261 int encodingID = 1;
262 int languageID = 0x409;
263
264 switch (stringID)
265 {
266 case EStringID::Mac:
267 platformID = 1;
268 encodingID = 0;
269 languageID = 0;
270 break;
271
272 case EStringID::Windows:
273 break;
274 }
275
276 for (uint16_t i = 0; i < GetUInt16(mNameLocation + 2); ++i)
277 {
278 uint32_t loc = mNameLocation + 6 + (12 * i);
279
280 if (platformID == GetUInt16(loc + 0) && encodingID == GetUInt16(loc + 2)
281 && languageID == GetUInt16(loc + 4) && nameID == GetUInt16(loc + 6))
282 {
283 uint32_t stringLocation = GetUInt16(mNameLocation + 4) + GetUInt16(loc + 10);
284 uint16_t length = GetUInt16(loc + 8);
285
286 switch (stringID)
287 {
288 case EStringID::Windows:
289 {
290 WDL_TypedBuf<char16_t> utf16;
291 utf16.Resize(length / sizeof(char16_t));
292
293 for (int j = 0; j < utf16.GetSize(); j++)
294 utf16.Get()[j] = GetUInt16(mNameLocation + stringLocation + j * 2);
295
296 // Convert UTF-16 to UTF-8 using WDL
297 std::string utf8Result;
298 for (int i = 0; i < utf16.GetSize(); i++)
299 {
300 int ch = utf16.Get()[i];
301 // Handle surrogate pairs
302 if (ch >= 0xD800 && ch <= 0xDBFF && i + 1 < utf16.GetSize())
303 {
304 int low = utf16.Get()[i + 1];
305 if (low >= 0xDC00 && low <= 0xDFFF)
306 {
307 ch = 0x10000 + ((ch - 0xD800) << 10) + (low - 0xDC00);
308 i++;
309 }
310 }
311 char buf[5];
312 int len = WDL_MakeUTFChar(buf, ch, sizeof(buf));
313 utf8Result.append(buf, len);
314 }
315 return WDL_String(utf8Result.c_str());
316 }
317
318 case EStringID::Mac:
319 if (!InBounds(mNameLocation + stringLocation, length))
320 return WDL_String();
321 return WDL_String((const char*)(mData + mNameLocation + stringLocation), length);
322 }
323 }
324 }
325
326 return WDL_String();
327 }
328
329 void FindFace(uint32_t faceIdx)
330 {
331 bool singleFont = IsSingleFont();
332
333 if (singleFont && faceIdx == 0 )
334 return;
335
336 // Check if it's a TTC file
337 if (!singleFont && MatchTag(0, "ttcf"))
338 {
339 // Check version
340 if (GetUInt32(4) == 0x00010000 || GetUInt32(4) == 0x00020000)
341 {
342 if (faceIdx < GetSInt32(8))
343 {
344 uint32_t faceLocation = GetUInt32(12 + faceIdx * 4);
345 if (InBounds(faceLocation, 0))
346 {
347 mFaceLocation = faceLocation;
348 return;
349 }
350 }
351 }
352 }
353 mData = nullptr;
354 }
355
356 bool IsSingleFont()
357 {
358 char TTV1[4] = { '1', 0, 0, 0 };
359 char OTV1[4] = { 0, 1, 0, 0 };
360
361 // Check the version number
362 if (MatchTag(0, TTV1)) return true; // TrueType 1
363 if (MatchTag(0, "typ1")) return true; // TrueType with type 1 font -- we don't support this!
364 if (MatchTag(0, "OTTO")) return true; // OpenType with CFF
365 if (MatchTag(0, OTV1)) return true; // OpenType 1.0
366
367 return false;
368 }
369
370#if defined WDL_LITTLE_ENDIAN
371 uint16_t GetUInt16(uint32_t loc) { return InBounds(loc, 2) ? (((uint16_t)mData[loc + 0]) << 8) | (uint16_t)mData[loc + 1] : 0; }
372 int16_t GetSInt16(uint32_t loc) { return GetUInt16(loc); }
373 uint32_t GetUInt32(uint32_t loc) { return (((uint32_t)GetUInt16(loc + 0)) << 16) | (uint32_t)GetUInt16(loc + 2); }
374 int32_t GetSInt32(uint32_t loc) { return (((uint32_t)GetUInt16(loc + 0)) << 16) | (uint32_t)GetUInt16(loc + 2); }
375#else
376 uint16_t GetUInt16(uint32_t loc) { return InBounds(loc, 2) ? (((uint16_t)mData[loc + 1]) << 8) | (uint16_t)mData[loc + 0] : 0; }
377 int16_t GetSInt16(uint32_t loc) { return GetUInt16(loc); }
378 uint32_t GetUInt32(uint32_t loc) { return (((uint32_t)GetUInt16(loc + 2)) << 16) | (uint32_t)GetUInt16(loc + 0); }
379 int32_t GetSInt32(uint32_t loc) { return (((uint32_t)GetUInt16(loc + 2)) << 16) | (uint32_t)GetUInt16(loc + 0); }
380#endif
381
382private:
383 const unsigned char* mData;
384 uint32_t mDataSize;
385
386 uint32_t mFaceLocation = 0; // the face's table directory: table locations are relative to the start of the data
387 uint32_t mHeadLocation = 0;
388 uint32_t mNameLocation = 0;
389 uint32_t mHheaLocation = 0;
390 uint32_t mFDscLocation = 0;
391
392 // Font Identifiers
393 WDL_String mFamily;
394 WDL_String mStyle;
395 uint16_t mMacStyle = 0;
396
397 // Metrics
398 uint16_t mUnitsPerEM = 0;
399 int16_t mAscender = 0;
400 int16_t mDescender = 0;
401 int16_t mLineGap = 0;
402};
403
405class IFontData : public IFontInfo, private WDL_TypedBuf<unsigned char>
406{
407public:
408 IFontData() : IFontInfo(nullptr, 0, -1), mFaceIdx(-1) {}
409
410 IFontData(const void* data, int size, int faceIdx) : IFontInfo(data, size, faceIdx), mFaceIdx(faceIdx)
411 {
412 const unsigned char* src = reinterpret_cast<const unsigned char*>(data);
413 unsigned char* dest = ResizeOK(size);
414
415 if (dest)
416 std::copy(src, src + size, dest);
417 }
418
419 IFontData(int size) : IFontInfo(nullptr, 0, -1), mFaceIdx(-1)
420 {
421 Resize(size);
422 }
423
424 void SetFaceIdx(int faceIdx)
425 {
426 mFaceIdx = faceIdx;
427 static_cast<IFontData&>(*this) = IFontData(Get(), GetSize(), mFaceIdx);
428 }
429
430 bool IsValid() const { return GetSize() && mFaceIdx >= 0 && IFontInfo::IsValid(); }
431
432 unsigned char* Get() { return WDL_TypedBuf<unsigned char>::Get(); }
433 int GetSize() const { return WDL_TypedBuf<unsigned char>::GetSize(); }
434 int GetFaceIdx() const { return mFaceIdx; }
435
436private:
437 int mFaceIdx;
438};
439
441using IFontDataPtr = std::unique_ptr<IFontData>;
442
444class PlatformFont
445{
446public:
447 PlatformFont(bool system) : mSystem(system) {}
448 virtual ~PlatformFont() {}
449
450 PlatformFont(const PlatformFont&) = delete;
451 PlatformFont& operator=(const PlatformFont&) = delete;
452
453 virtual FontDescriptor GetDescriptor() { return nullptr; }
454 virtual IFontDataPtr GetFontData() { return IFontDataPtr(new IFontData()); }
455 bool IsSystem() { return mSystem; }
456
457protected:
458 int GetFaceIdx(const void* data, int dataSize, const char* styleName)
459 {
460 for (int idx = 0; ; idx++)
461 {
462 IFontInfo fontInfo(data, dataSize, idx);
463
464 if (!fontInfo.IsValid())
465 return -1;
466
467 const WDL_String& style = fontInfo.GetStyle();
468
469 if (style.GetLength() && (!styleName[0] || !strcmp(style.Get(), styleName)))
470 return idx;
471 }
472 }
473
474 bool mSystem;
475};
476
477using PlatformFontPtr = std::unique_ptr<PlatformFont>;
478
479#ifdef SVG_USE_SKIA
480struct SVGHolder
481{
482 SVGHolder(sk_sp<SkSVGDOM> svgDom)
483 : mSVGDom(svgDom)
484 {
485 }
486
487 ~SVGHolder()
488 {
489 mSVGDom = nullptr;
490 }
491
492 SVGHolder(const SVGHolder&) = delete;
493 SVGHolder& operator=(const SVGHolder&) = delete;
494
495 sk_sp<SkSVGDOM> mSVGDom;
496};
497#else
499struct SVGHolder
500{
501 SVGHolder(NSVGimage* pImage)
502 : mImage(pImage)
503 {
504 }
505
506 ~SVGHolder()
507 {
508 if(mImage)
509 nsvgDelete(mImage);
510
511 mImage = nullptr;
512 }
513
514 SVGHolder(const SVGHolder&) = delete;
515 SVGHolder& operator=(const SVGHolder&) = delete;
516
517 NSVGimage* mImage = nullptr;
518};
519#endif
520
522template <class T>
523class StaticStorage
524{
525public:
527 class Accessor : private WDL_MutexLock
528 {
529 public:
530 Accessor(StaticStorage& storage)
531 : WDL_MutexLock(&storage.mMutex)
532 , mStorage(storage)
533 {}
534
535 T* Find(const char* str, double scale = 1.) { return mStorage.Find(str, scale); }
536 void Add(T* pData, const char* str, double scale = 1.) { return mStorage.Add(pData, str, scale); }
537 void Remove(T* pData) { return mStorage.Remove(pData); }
538 void Clear() { return mStorage.Clear(); }
539 void Retain() { return mStorage.Retain(); }
540 void Release() { return mStorage.Release(); }
541
542 private:
543 StaticStorage& mStorage;
544 };
545
546 StaticStorage() {}
547
548 ~StaticStorage()
549 {
550 Clear();
551 }
552
553 StaticStorage(const StaticStorage&) = delete;
554 StaticStorage& operator=(const StaticStorage&) = delete;
555
556private:
558 struct DataKey
559 {
560 // N.B. - hashID is not guaranteed to be unique
561 size_t hashID;
562 WDL_String name;
563 double scale;
564 std::unique_ptr<T> data;
565 };
566
570 size_t Hash(const char* str)
571 {
572 std::string string(str);
573 return std::hash<std::string>()(string);
574 }
575
580 T* Find(const char* str, double scale = 1.)
581 {
582 WDL_String cacheName(str);
583 cacheName.AppendFormatted((int) strlen(str) + 6, "-%.1fx", scale);
584
585 size_t hashID = Hash(cacheName.Get());
586
587 int i, n = mDatas.GetSize();
588 for (i = 0; i < n; ++i)
589 {
590 DataKey* pKey = mDatas.Get(i);
591
592 // Use the hash id for a quick search and then confirm with the scale and identifier to ensure uniqueness
593 if (pKey->hashID == hashID && scale == pKey->scale && !strcmp(str, pKey->name.Get()))
594 return pKey->data.get();
595 }
596 return nullptr;
597 }
598
603 void Add(T* pData, const char* str, double scale = 1.)
604 {
605 DataKey* pKey = mDatas.Add(new DataKey);
606
607 WDL_String cacheName(str);
608 cacheName.AppendFormatted((int) strlen(str) + 6, "-%.1fx", scale);
609
610 pKey->hashID = Hash(cacheName.Get());
611 pKey->data = std::unique_ptr<T>(pData);
612 pKey->scale = scale;
613 pKey->name.Set(str);
614
615 //DBGMSG("adding %s to the static storage at %.1fx the original scale\n", str, scale);
616 }
617
620 void Remove(T* pData)
621 {
622 for (int i = 0; i < mDatas.GetSize(); ++i)
623 {
624 if (mDatas.Get(i)->data.get() == pData)
625 {
626 mDatas.Delete(i, true);
627 break;
628 }
629 }
630 }
631
633 void Clear()
634 {
635 mDatas.Empty(true);
636 };
637
639 void Retain()
640 {
641 mCount++;
642 }
643
645 void Release()
646 {
647 if (--mCount == 0)
648 Clear();
649 }
650
651 int mCount = 0;
652 WDL_Mutex mMutex;
653 WDL_PtrList<DataKey> mDatas;
654};
655
657struct IVec2
658{
659 float x, y;
660 IVec2() = default;
661 IVec2(float x, float y) : x(x), y(y) {}
662
663 IVec2 operator-(const IVec2 b) { return IVec2{x-b.x, y-b.y}; }
664 IVec2 operator+(const IVec2 b) { return IVec2{x+b.x, y+b.y}; }
665};
666
667END_IGRAPHICS_NAMESPACE
668END_IPLUG_NAMESPACE
669
Include to get consistently named preprocessor macros for different platforms and logging functionali...
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
float GetScale() const
APIBitmap(BitmapData pBitmap, int w, int h, float scale, float drawScale)
APIBitmap constructor.
BitmapData GetBitmap() const
void SetBitmap(BitmapData pBitmap, int w, int h, float scale, float drawScale)
Used to initialise the members after construction.
int GetHeight() const
float GetDrawScale() const
int GetWidth() const
Encapsulate an xy point in one struct.