28#if defined IGRAPHICS_SKIA && !defined IGRAPHICS_NO_SKIA_SVG
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")
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 )
62 sk_sp<SkImage> mImage;
63 sk_sp<SkSurface> mSurface;
65 #define BITMAP_DATA_TYPE SkiaDrawable*
67 #define BITMAP_DATA_TYPE void*;
70#if defined OS_MAC || defined OS_IOS
71 #include <CoreText/CoreText.h>
72 #define FONT_DESCRIPTOR_TYPE CTFontDescriptorRef
75 #define FONT_DESCRIPTOR_TYPE HFONT
78 #define FONT_DESCRIPTOR_TYPE WDL_String*
80 #define FONT_DESCRIPTOR_TYPE std::pair<WDL_String, WDL_String>*
86BEGIN_IGRAPHICS_NAMESPACE
87using BitmapData = BITMAP_DATA_TYPE;
88using FontDescriptor = FONT_DESCRIPTOR_TYPE;
89using RawBitmapData = WDL_TypedBuf<uint8_t>;
104 APIBitmap(BitmapData pBitmap,
int w,
int h,
float scale,
float drawScale)
109 , mDrawScale(drawScale)
131 void SetBitmap(BitmapData pBitmap,
int w,
int h,
float scale,
float drawScale)
137 mDrawScale = drawScale;
167 IFontInfo(
const void* data, uint32_t dataSize, uint32_t faceIdx)
168 : mData(reinterpret_cast<const unsigned char*>(data))
169 , mDataSize(dataSize)
176 mHeadLocation = LocateTable(
"head");
177 mNameLocation = LocateTable(
"name");
178 mHheaLocation = LocateTable(
"hhea");
179 mFDscLocation = LocateTable(
"fdsc");
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);
194 bool IsValid()
const {
return mData && mHeadLocation && mNameLocation && mHheaLocation; }
196 const WDL_String& GetFamily()
const {
return mFamily; }
197 const WDL_String& GetStyle()
const {
return mStyle; }
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); }
207 double GetHeightEMRatio()
const {
return mUnitsPerEM /
static_cast<double>(mAscender - mDescender); }
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; }
217 enum class EStringID { Mac, Windows };
219 bool InBounds(uint32_t loc, uint32_t size)
const
221 return loc <= mDataSize && size <= mDataSize - loc;
224 bool MatchTag(uint32_t loc,
const char* tag)
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];
229 uint32_t LocateTable(
const char *tag)
231 uint16_t numTables = GetUInt16(mFaceLocation + 4);
233 for (uint16_t i = 0; i < numTables; ++i)
235 uint32_t tableLocation = mFaceLocation + 12 + (16 * i);
236 if (MatchTag(tableLocation, tag))
238 uint32_t location = GetUInt32(tableLocation + 8);
239 return InBounds(location, GetUInt32(tableLocation + 12)) ? location : 0;
246 WDL_String SearchFontString(
int nameID)
248 WDL_String str = GetFontString(nameID, EStringID::Windows);
253 return GetFontString(nameID, EStringID::Mac);
256 WDL_String GetFontString(
int nameID, EStringID stringID)
262 int languageID = 0x409;
272 case EStringID::Windows:
276 for (uint16_t i = 0; i < GetUInt16(mNameLocation + 2); ++i)
278 uint32_t loc = mNameLocation + 6 + (12 * i);
280 if (platformID == GetUInt16(loc + 0) && encodingID == GetUInt16(loc + 2)
281 && languageID == GetUInt16(loc + 4) && nameID == GetUInt16(loc + 6))
283 uint32_t stringLocation = GetUInt16(mNameLocation + 4) + GetUInt16(loc + 10);
284 uint16_t length = GetUInt16(loc + 8);
288 case EStringID::Windows:
290 WDL_TypedBuf<char16_t> utf16;
291 utf16.Resize(length /
sizeof(
char16_t));
293 for (
int j = 0; j < utf16.GetSize(); j++)
294 utf16.Get()[j] = GetUInt16(mNameLocation + stringLocation + j * 2);
297 std::string utf8Result;
298 for (
int i = 0; i < utf16.GetSize(); i++)
300 int ch = utf16.Get()[i];
302 if (ch >= 0xD800 && ch <= 0xDBFF && i + 1 < utf16.GetSize())
304 int low = utf16.Get()[i + 1];
305 if (low >= 0xDC00 && low <= 0xDFFF)
307 ch = 0x10000 + ((ch - 0xD800) << 10) + (low - 0xDC00);
312 int len = WDL_MakeUTFChar(buf, ch,
sizeof(buf));
313 utf8Result.append(buf, len);
315 return WDL_String(utf8Result.c_str());
319 if (!InBounds(mNameLocation + stringLocation, length))
321 return WDL_String((
const char*)(mData + mNameLocation + stringLocation), length);
329 void FindFace(uint32_t faceIdx)
331 bool singleFont = IsSingleFont();
333 if (singleFont && faceIdx == 0 )
337 if (!singleFont && MatchTag(0,
"ttcf"))
340 if (GetUInt32(4) == 0x00010000 || GetUInt32(4) == 0x00020000)
342 if (faceIdx < GetSInt32(8))
344 uint32_t faceLocation = GetUInt32(12 + faceIdx * 4);
345 if (InBounds(faceLocation, 0))
347 mFaceLocation = faceLocation;
358 char TTV1[4] = {
'1', 0, 0, 0 };
359 char OTV1[4] = { 0, 1, 0, 0 };
362 if (MatchTag(0, TTV1))
return true;
363 if (MatchTag(0,
"typ1"))
return true;
364 if (MatchTag(0,
"OTTO"))
return true;
365 if (MatchTag(0, OTV1))
return true;
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); }
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); }
383 const unsigned char* mData;
386 uint32_t mFaceLocation = 0;
387 uint32_t mHeadLocation = 0;
388 uint32_t mNameLocation = 0;
389 uint32_t mHheaLocation = 0;
390 uint32_t mFDscLocation = 0;
395 uint16_t mMacStyle = 0;
398 uint16_t mUnitsPerEM = 0;
399 int16_t mAscender = 0;
400 int16_t mDescender = 0;
401 int16_t mLineGap = 0;
405class IFontData :
public IFontInfo,
private WDL_TypedBuf<unsigned char>
408 IFontData() : IFontInfo(nullptr, 0, -1), mFaceIdx(-1) {}
410 IFontData(
const void* data,
int size,
int faceIdx) : IFontInfo(data, size, faceIdx), mFaceIdx(faceIdx)
412 const unsigned char* src =
reinterpret_cast<const unsigned char*
>(data);
413 unsigned char* dest = ResizeOK(size);
416 std::copy(src, src + size, dest);
419 IFontData(
int size) : IFontInfo(nullptr, 0, -1), mFaceIdx(-1)
424 void SetFaceIdx(
int faceIdx)
427 static_cast<IFontData&
>(*this) = IFontData(Get(), GetSize(), mFaceIdx);
430 bool IsValid()
const {
return GetSize() && mFaceIdx >= 0 && IFontInfo::IsValid(); }
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; }
441using IFontDataPtr = std::unique_ptr<IFontData>;
447 PlatformFont(
bool system) : mSystem(system) {}
448 virtual ~PlatformFont() {}
450 PlatformFont(
const PlatformFont&) =
delete;
451 PlatformFont& operator=(
const PlatformFont&) =
delete;
453 virtual FontDescriptor GetDescriptor() {
return nullptr; }
454 virtual IFontDataPtr GetFontData() {
return IFontDataPtr(
new IFontData()); }
455 bool IsSystem() {
return mSystem; }
458 int GetFaceIdx(
const void* data,
int dataSize,
const char* styleName)
460 for (
int idx = 0; ; idx++)
462 IFontInfo fontInfo(data, dataSize, idx);
464 if (!fontInfo.IsValid())
467 const WDL_String& style = fontInfo.GetStyle();
469 if (style.GetLength() && (!styleName[0] || !strcmp(style.Get(), styleName)))
477using PlatformFontPtr = std::unique_ptr<PlatformFont>;
482 SVGHolder(sk_sp<SkSVGDOM> svgDom)
492 SVGHolder(
const SVGHolder&) =
delete;
493 SVGHolder& operator=(
const SVGHolder&) =
delete;
495 sk_sp<SkSVGDOM> mSVGDom;
501 SVGHolder(NSVGimage* pImage)
514 SVGHolder(
const SVGHolder&) =
delete;
515 SVGHolder& operator=(
const SVGHolder&) =
delete;
517 NSVGimage* mImage =
nullptr;
527 class Accessor :
private WDL_MutexLock
530 Accessor(StaticStorage& storage)
531 : WDL_MutexLock(&storage.mMutex)
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(); }
543 StaticStorage& mStorage;
553 StaticStorage(
const StaticStorage&) =
delete;
554 StaticStorage& operator=(
const StaticStorage&) =
delete;
564 std::unique_ptr<T> data;
570 size_t Hash(
const char* str)
572 std::string string(str);
573 return std::hash<std::string>()(string);
580 T* Find(
const char* str,
double scale = 1.)
582 WDL_String cacheName(str);
583 cacheName.AppendFormatted((
int) strlen(str) + 6,
"-%.1fx", scale);
585 size_t hashID = Hash(cacheName.Get());
587 int i, n = mDatas.GetSize();
588 for (i = 0; i < n; ++i)
590 DataKey* pKey = mDatas.Get(i);
593 if (pKey->hashID == hashID && scale == pKey->scale && !strcmp(str, pKey->name.Get()))
594 return pKey->data.get();
603 void Add(T* pData,
const char* str,
double scale = 1.)
605 DataKey* pKey = mDatas.Add(
new DataKey);
607 WDL_String cacheName(str);
608 cacheName.AppendFormatted((
int) strlen(str) + 6,
"-%.1fx", scale);
610 pKey->hashID = Hash(cacheName.Get());
611 pKey->data = std::unique_ptr<T>(pData);
620 void Remove(T* pData)
622 for (
int i = 0; i < mDatas.GetSize(); ++i)
624 if (mDatas.Get(i)->data.get() == pData)
626 mDatas.Delete(i,
true);
653 WDL_PtrList<DataKey> mDatas;
661 IVec2(
float x,
float y) : x(x), y(y) {}
667END_IGRAPHICS_NAMESPACE
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
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.
float GetDrawScale() const
Encapsulate an xy point in one struct.