Hash :
db8d099d
Author :
Date :
2025-03-15T13:53:36
[dwrite] Split shape code into its own file
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
/*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Author(s): Behdad Esfahbod
*/
#ifndef HB_DIRECTWRITE_HH
#define HB_DIRECTWRITE_HH
#include "hb.hh"
#include "hb-directwrite.h"
#include "hb-mutex.hh"
#include "hb-map.hh"
/*
* DirectWrite font stream helpers
*/
// Have a look at to NativeFontResourceDWrite.cpp in Mozilla
/* Declare object creator for dynamic support of DWRITE */
typedef HRESULT (WINAPI *t_DWriteCreateFactory)(
DWRITE_FACTORY_TYPE factoryType,
REFIID iid,
IUnknown **factory
);
class DWriteFontFileLoader : public IDWriteFontFileLoader
{
private:
hb_reference_count_t mRefCount;
hb_mutex_t mutex;
hb_hashmap_t<uint64_t, IDWriteFontFileStream *> mFontStreams;
uint64_t mNextFontFileKey = 0;
public:
DWriteFontFileLoader ()
{
mRefCount.init ();
}
uint64_t RegisterFontFileStream (IDWriteFontFileStream *fontFileStream)
{
fontFileStream->AddRef ();
auto lock = hb_lock_t (mutex);
mFontStreams.set (mNextFontFileKey, fontFileStream);
return mNextFontFileKey++;
}
void UnregisterFontFileStream (uint64_t fontFileKey)
{
auto lock = hb_lock_t (mutex);
IDWriteFontFileStream *stream = mFontStreams.get (fontFileKey);
if (stream)
{
mFontStreams.del (fontFileKey);
stream->Release ();
}
}
// IUnknown interface
IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
{ return S_OK; }
IFACEMETHOD_ (ULONG, AddRef) ()
{
return mRefCount.inc () + 1;
}
IFACEMETHOD_ (ULONG, Release) ()
{
signed refCount = mRefCount.dec () - 1;
assert (refCount >= 0);
if (refCount)
return refCount;
delete this;
return 0;
}
// IDWriteFontFileLoader methods
virtual HRESULT STDMETHODCALLTYPE
CreateStreamFromKey (void const* fontFileReferenceKey,
uint32_t fontFileReferenceKeySize,
OUT IDWriteFontFileStream** fontFileStream)
{
if (fontFileReferenceKeySize != sizeof (uint64_t))
return E_INVALIDARG;
uint64_t fontFileKey = * (uint64_t *) fontFileReferenceKey;
IDWriteFontFileStream *stream = mFontStreams.get (fontFileKey);
if (!stream)
return E_FAIL;
stream->AddRef ();
*fontFileStream = stream;
return S_OK;
}
virtual ~DWriteFontFileLoader()
{
for (auto v : mFontStreams.values ())
v->Release ();
}
};
class DWriteFontFileStream : public IDWriteFontFileStream
{
private:
hb_reference_count_t mRefCount;
hb_blob_t *mBlob;
uint8_t *mData;
unsigned mSize;
DWriteFontFileLoader *mLoader;
public:
uint64_t fontFileKey;
public:
DWriteFontFileStream (hb_blob_t *blob);
// IUnknown interface
IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
{ return S_OK; }
IFACEMETHOD_ (ULONG, AddRef) ()
{
return mRefCount.inc () + 1;
}
IFACEMETHOD_ (ULONG, Release) ()
{
signed refCount = mRefCount.dec () - 1;
assert (refCount >= 0);
if (refCount)
return refCount;
delete this;
return 0;
}
// IDWriteFontFileStream methods
virtual HRESULT STDMETHODCALLTYPE
ReadFileFragment (void const** fragmentStart,
UINT64 fileOffset,
UINT64 fragmentSize,
OUT void** fragmentContext)
{
// We are required to do bounds checking.
if (fileOffset + fragmentSize > mSize) return E_FAIL;
// truncate the 64 bit fileOffset to size_t sized index into mData
size_t index = static_cast<size_t> (fileOffset);
// We should be alive for the duration of this.
*fragmentStart = &mData[index];
*fragmentContext = nullptr;
return S_OK;
}
virtual void STDMETHODCALLTYPE
ReleaseFileFragment (void* fragmentContext) {}
virtual HRESULT STDMETHODCALLTYPE
GetFileSize (OUT UINT64* fileSize)
{
*fileSize = mSize;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE
GetLastWriteTime (OUT UINT64* lastWriteTime) { return E_NOTIMPL; }
virtual ~DWriteFontFileStream();
};
struct hb_directwrite_global_t
{
hb_directwrite_global_t ()
{
dwrite_dll = LoadLibraryW (L"DWrite.dll");
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
t_DWriteCreateFactory p_DWriteCreateFactory = (t_DWriteCreateFactory)
GetProcAddress (dwrite_dll, "DWriteCreateFactory");
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
if (unlikely (!p_DWriteCreateFactory))
return;
HRESULT hr = p_DWriteCreateFactory (DWRITE_FACTORY_TYPE_SHARED, __uuidof (IDWriteFactory),
(IUnknown**) &dwriteFactory);
if (unlikely (hr != S_OK))
return;
fontFileLoader = new DWriteFontFileLoader ();
dwriteFactory->RegisterFontFileLoader (fontFileLoader);
success = true;
}
~hb_directwrite_global_t ()
{
if (fontFileLoader)
fontFileLoader->Release ();
if (dwriteFactory)
dwriteFactory->Release ();
if (dwrite_dll)
FreeLibrary (dwrite_dll);
}
bool success = false;
HMODULE dwrite_dll;
IDWriteFactory *dwriteFactory;
DWriteFontFileLoader *fontFileLoader;
};
HB_INTERNAL hb_directwrite_global_t *
get_directwrite_global ();
HB_INTERNAL IDWriteFontFace *
dw_face_create (hb_blob_t *blob, unsigned index);
#endif /* HB_DIRECTWRITE_HH */