2018-04-11 15:30:13 +02:00
|
|
|
/*
|
2019-02-22 10:43:42 +01:00
|
|
|
* Copyright © 2015-2019 Ebrahim Byagowi
|
2015-09-11 07:18:12 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb.hh"
|
2019-06-18 07:41:49 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_DIRECTWRITE
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb-shaper-impl.hh"
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2019-07-01 13:31:26 +02:00
|
|
|
#include <dwrite_1.h>
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
#include "hb-directwrite.h"
|
|
|
|
|
2021-07-27 19:51:23 +02:00
|
|
|
#include "hb-ms-feature-ranges.hh"
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2020-12-22 23:45:39 +01:00
|
|
|
/**
|
|
|
|
* SECTION:hb-directwrite
|
|
|
|
* @title: hb-directwrite
|
|
|
|
* @short_description: DirectWrite integration
|
|
|
|
* @include: hb-directwrite.h
|
|
|
|
*
|
|
|
|
* Functions for using HarfBuzz with DirectWrite fonts.
|
|
|
|
**/
|
|
|
|
|
2022-06-27 14:34:18 +02:00
|
|
|
/* Declare object creator for dynamic support of DWRITE */
|
2022-07-06 13:50:47 +02:00
|
|
|
typedef HRESULT (WINAPI *t_DWriteCreateFactory)(
|
2022-06-27 14:34:18 +02:00
|
|
|
DWRITE_FACTORY_TYPE factoryType,
|
|
|
|
REFIID iid,
|
|
|
|
IUnknown **factory
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
/*
|
|
|
|
* DirectWrite font stream helpers
|
|
|
|
*/
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// This is a font loader which provides only one font (unlike its original design).
|
|
|
|
// For a better implementation which was also source of this
|
|
|
|
// and DWriteFontFileStream, have a look at to NativeFontResourceDWrite.cpp in Mozilla
|
|
|
|
class DWriteFontFileLoader : public IDWriteFontFileLoader
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2016-06-21 11:27:26 +02:00
|
|
|
private:
|
|
|
|
IDWriteFontFileStream *mFontFileStream;
|
|
|
|
public:
|
2018-03-19 09:49:42 +01:00
|
|
|
DWriteFontFileLoader (IDWriteFontFileStream *fontFileStream)
|
2018-12-12 12:44:37 +01:00
|
|
|
{ mFontFileStream = fontFileStream; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// IUnknown interface
|
2018-12-12 12:44:37 +01:00
|
|
|
IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
|
|
|
|
{ return S_OK; }
|
2018-12-17 19:01:01 +01:00
|
|
|
IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
|
|
|
|
IFACEMETHOD_ (ULONG, Release) () { return 1; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// IDWriteFontFileLoader methods
|
2018-12-12 12:44:37 +01:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE
|
|
|
|
CreateStreamFromKey (void const* fontFileReferenceKey,
|
|
|
|
uint32_t fontFileReferenceKeySize,
|
|
|
|
OUT IDWriteFontFileStream** fontFileStream)
|
2016-06-21 11:27:26 +02:00
|
|
|
{
|
|
|
|
*fontFileStream = mFontFileStream;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2019-01-19 14:00:07 +01:00
|
|
|
|
|
|
|
virtual ~DWriteFontFileLoader() {}
|
2016-06-21 11:27:26 +02:00
|
|
|
};
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
class DWriteFontFileStream : public IDWriteFontFileStream
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
uint8_t *mData;
|
|
|
|
uint32_t mSize;
|
|
|
|
public:
|
2018-03-19 09:49:42 +01:00
|
|
|
DWriteFontFileStream (uint8_t *aData, uint32_t aSize)
|
2016-06-21 11:27:26 +02:00
|
|
|
{
|
|
|
|
mData = aData;
|
|
|
|
mSize = aSize;
|
|
|
|
}
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// IUnknown interface
|
2018-12-12 12:44:37 +01:00
|
|
|
IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
|
|
|
|
{ return S_OK; }
|
2018-12-17 19:01:01 +01:00
|
|
|
IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
|
|
|
|
IFACEMETHOD_ (ULONG, Release) () { return 1; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// IDWriteFontFileStream methods
|
2018-12-12 12:44:37 +01:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE
|
|
|
|
ReadFileFragment (void const** fragmentStart,
|
|
|
|
UINT64 fileOffset,
|
|
|
|
UINT64 fragmentSize,
|
|
|
|
OUT void** fragmentContext)
|
2016-06-21 11:27:26 +02:00
|
|
|
{
|
|
|
|
// We are required to do bounds checking.
|
2018-12-12 12:44:37 +01:00
|
|
|
if (fileOffset + fragmentSize > mSize) return E_FAIL;
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// truncate the 64 bit fileOffset to size_t sized index into mData
|
|
|
|
size_t index = static_cast<size_t> (fileOffset);
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// We should be alive for the duration of this.
|
|
|
|
*fragmentStart = &mData[index];
|
|
|
|
*fragmentContext = nullptr;
|
|
|
|
return S_OK;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
virtual void STDMETHODCALLTYPE
|
|
|
|
ReleaseFileFragment (void* fragmentContext) {}
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE
|
|
|
|
GetFileSize (OUT UINT64* fileSize)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2016-06-21 11:27:26 +02:00
|
|
|
*fileSize = mSize;
|
|
|
|
return S_OK;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
virtual HRESULT STDMETHODCALLTYPE
|
|
|
|
GetLastWriteTime (OUT UINT64* lastWriteTime) { return E_NOTIMPL; }
|
2019-01-19 14:00:07 +01:00
|
|
|
|
|
|
|
virtual ~DWriteFontFileStream() {}
|
2016-06-21 11:27:26 +02:00
|
|
|
};
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
/*
|
|
|
|
* shaper face data
|
|
|
|
*/
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-08-02 03:03:32 +02:00
|
|
|
struct hb_directwrite_face_data_t
|
2018-03-25 15:49:23 +02:00
|
|
|
{
|
2022-06-27 14:34:18 +02:00
|
|
|
HMODULE dwrite_dll;
|
2016-06-24 09:53:25 +02:00
|
|
|
IDWriteFactory *dwriteFactory;
|
|
|
|
IDWriteFontFile *fontFile;
|
2019-01-18 19:25:21 +01:00
|
|
|
DWriteFontFileStream *fontFileStream;
|
|
|
|
DWriteFontFileLoader *fontFileLoader;
|
2016-06-24 09:53:25 +02:00
|
|
|
IDWriteFontFace *fontFace;
|
|
|
|
hb_blob_t *faceBlob;
|
2016-06-21 11:27:26 +02:00
|
|
|
};
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-08-02 03:03:32 +02:00
|
|
|
hb_directwrite_face_data_t *
|
2018-03-22 12:34:38 +01:00
|
|
|
_hb_directwrite_shaper_face_data_create (hb_face_t *face)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2018-08-02 03:03:32 +02:00
|
|
|
hb_directwrite_face_data_t *data = new hb_directwrite_face_data_t;
|
2015-09-11 07:18:12 +02:00
|
|
|
if (unlikely (!data))
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2019-06-30 15:03:44 +02:00
|
|
|
#define FAIL(...) \
|
|
|
|
HB_STMT_START { \
|
|
|
|
DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
|
|
|
|
return nullptr; \
|
|
|
|
} HB_STMT_END
|
|
|
|
|
2022-06-27 14:34:18 +02:00
|
|
|
data->dwrite_dll = LoadLibrary (TEXT ("DWRITE"));
|
|
|
|
if (unlikely (!data->dwrite_dll))
|
|
|
|
FAIL ("Cannot find DWrite.DLL");
|
|
|
|
|
|
|
|
t_DWriteCreateFactory p_DWriteCreateFactory;
|
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p_DWriteCreateFactory = (t_DWriteCreateFactory)
|
|
|
|
GetProcAddress (data->dwrite_dll, "DWriteCreateFactory");
|
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (unlikely (!p_DWriteCreateFactory))
|
|
|
|
FAIL ("Cannot find DWriteCreateFactory().");
|
|
|
|
|
2019-06-30 15:03:44 +02:00
|
|
|
HRESULT hr;
|
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// TODO: factory and fontFileLoader should be cached separately
|
|
|
|
IDWriteFactory* dwriteFactory;
|
2022-06-27 14:34:18 +02:00
|
|
|
hr = p_DWriteCreateFactory (DWRITE_FACTORY_TYPE_SHARED, __uuidof (IDWriteFactory),
|
|
|
|
(IUnknown**) &dwriteFactory);
|
2016-06-21 11:27:26 +02:00
|
|
|
|
2019-07-01 17:00:21 +02:00
|
|
|
if (unlikely (hr != S_OK))
|
2019-06-30 15:03:44 +02:00
|
|
|
FAIL ("Failed to run DWriteCreateFactory().");
|
|
|
|
|
2016-06-24 09:12:01 +02:00
|
|
|
hb_blob_t *blob = hb_face_reference_blob (face);
|
2018-12-12 12:44:37 +01:00
|
|
|
DWriteFontFileStream *fontFileStream;
|
|
|
|
fontFileStream = new DWriteFontFileStream ((uint8_t *) hb_blob_get_data (blob, nullptr),
|
|
|
|
hb_blob_get_length (blob));
|
2018-03-14 08:34:28 +01:00
|
|
|
|
2018-03-19 09:49:42 +01:00
|
|
|
DWriteFontFileLoader *fontFileLoader = new DWriteFontFileLoader (fontFileStream);
|
2016-06-21 11:27:26 +02:00
|
|
|
dwriteFactory->RegisterFontFileLoader (fontFileLoader);
|
|
|
|
|
|
|
|
IDWriteFontFile *fontFile;
|
|
|
|
uint64_t fontFileKey = 0;
|
|
|
|
hr = dwriteFactory->CreateCustomFontFileReference (&fontFileKey, sizeof (fontFileKey),
|
2018-12-12 12:44:37 +01:00
|
|
|
fontFileLoader, &fontFile);
|
2016-06-21 11:27:26 +02:00
|
|
|
|
2018-03-25 15:49:23 +02:00
|
|
|
if (FAILED (hr))
|
2016-06-21 11:27:26 +02:00
|
|
|
FAIL ("Failed to load font file from data!");
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
BOOL isSupported;
|
|
|
|
DWRITE_FONT_FILE_TYPE fileType;
|
|
|
|
DWRITE_FONT_FACE_TYPE faceType;
|
2018-03-25 15:49:23 +02:00
|
|
|
uint32_t numberOfFaces;
|
2016-06-21 11:27:26 +02:00
|
|
|
hr = fontFile->Analyze (&isSupported, &fileType, &faceType, &numberOfFaces);
|
2018-03-25 15:49:23 +02:00
|
|
|
if (FAILED (hr) || !isSupported)
|
2016-06-21 11:27:26 +02:00
|
|
|
FAIL ("Font file is not supported.");
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
#undef FAIL
|
|
|
|
|
|
|
|
IDWriteFontFace *fontFace;
|
|
|
|
dwriteFactory->CreateFontFace (faceType, 1, &fontFile, 0,
|
2018-12-12 12:44:37 +01:00
|
|
|
DWRITE_FONT_SIMULATIONS_NONE, &fontFace);
|
2016-06-21 11:27:26 +02:00
|
|
|
|
|
|
|
data->dwriteFactory = dwriteFactory;
|
|
|
|
data->fontFile = fontFile;
|
2016-06-24 09:53:25 +02:00
|
|
|
data->fontFileStream = fontFileStream;
|
2016-06-21 11:27:26 +02:00
|
|
|
data->fontFileLoader = fontFileLoader;
|
|
|
|
data->fontFace = fontFace;
|
2016-06-24 09:12:01 +02:00
|
|
|
data->faceBlob = blob;
|
2016-06-21 11:27:26 +02:00
|
|
|
|
2015-09-11 07:18:12 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-08-02 03:03:32 +02:00
|
|
|
_hb_directwrite_shaper_face_data_destroy (hb_directwrite_face_data_t *data)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2016-06-24 09:53:25 +02:00
|
|
|
if (data->fontFace)
|
|
|
|
data->fontFace->Release ();
|
|
|
|
if (data->fontFile)
|
|
|
|
data->fontFile->Release ();
|
2018-03-25 15:49:23 +02:00
|
|
|
if (data->dwriteFactory)
|
|
|
|
{
|
2016-06-24 09:53:25 +02:00
|
|
|
if (data->fontFileLoader)
|
2016-12-17 08:00:40 +01:00
|
|
|
data->dwriteFactory->UnregisterFontFileLoader (data->fontFileLoader);
|
|
|
|
data->dwriteFactory->Release ();
|
2016-06-24 09:53:25 +02:00
|
|
|
}
|
2022-12-27 22:38:17 +01:00
|
|
|
delete data->fontFileLoader;
|
|
|
|
delete data->fontFileStream;
|
|
|
|
hb_blob_destroy (data->faceBlob);
|
2022-06-27 14:34:18 +02:00
|
|
|
if (data->dwrite_dll)
|
|
|
|
FreeLibrary (data->dwrite_dll);
|
2022-12-27 22:38:17 +01:00
|
|
|
delete data;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shaper font data
|
|
|
|
*/
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
struct hb_directwrite_font_data_t {};
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-08-02 03:03:32 +02:00
|
|
|
hb_directwrite_font_data_t *
|
2015-09-11 07:18:12 +02:00
|
|
|
_hb_directwrite_shaper_font_data_create (hb_font_t *font)
|
|
|
|
{
|
2022-06-28 21:47:49 +02:00
|
|
|
return (hb_directwrite_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-08-02 03:03:32 +02:00
|
|
|
_hb_directwrite_shaper_font_data_destroy (hb_directwrite_font_data_t *data)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-21 11:27:26 +02:00
|
|
|
// Most of TextAnalysis is originally written by Bas Schouten for Mozilla project
|
2015-09-11 07:18:12 +02:00
|
|
|
// but now is relicensed to MIT for HarfBuzz use
|
2018-12-12 12:44:37 +01:00
|
|
|
class TextAnalysis : public IDWriteTextAnalysisSource, public IDWriteTextAnalysisSink
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
IFACEMETHOD (QueryInterface) (IID const& iid, OUT void** ppObject)
|
|
|
|
{ return S_OK; }
|
2018-12-17 19:01:01 +01:00
|
|
|
IFACEMETHOD_ (ULONG, AddRef) () { return 1; }
|
|
|
|
IFACEMETHOD_ (ULONG, Release) () { return 1; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-03-14 08:34:28 +01:00
|
|
|
// A single contiguous run of characters containing the same analysis
|
2015-09-11 07:18:12 +02:00
|
|
|
// results.
|
|
|
|
struct Run
|
|
|
|
{
|
2016-04-01 17:47:07 +02:00
|
|
|
uint32_t mTextStart; // starting text position of this run
|
|
|
|
uint32_t mTextLength; // number of contiguous code units covered
|
|
|
|
uint32_t mGlyphStart; // starting glyph in the glyphs array
|
2018-03-14 08:34:28 +01:00
|
|
|
uint32_t mGlyphCount; // number of glyphs associated with this run
|
2015-09-11 07:18:12 +02:00
|
|
|
// text
|
|
|
|
DWRITE_SCRIPT_ANALYSIS mScript;
|
2016-04-01 17:47:07 +02:00
|
|
|
uint8_t mBidiLevel;
|
2015-09-11 07:18:12 +02:00
|
|
|
bool mIsSideways;
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool ContainsTextPosition (uint32_t aTextPosition) const
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2018-04-11 15:30:13 +02:00
|
|
|
return aTextPosition >= mTextStart &&
|
|
|
|
aTextPosition < mTextStart + mTextLength;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Run *nextRun;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2018-12-12 12:44:37 +01:00
|
|
|
TextAnalysis (const wchar_t* text, uint32_t textLength,
|
|
|
|
const wchar_t* localeName, DWRITE_READING_DIRECTION readingDirection)
|
[directwrite] Fix error
hb-directwrite.cc: In constructor 'TextAnalysis::TextAnalysis(const wchar_t*, uint32_t, const wchar_t*, DWRITE_READING_DIRECTION)':
hb-directwrite.cc:489:18: error: 'TextAnalysis::mText' will be initialized after [-Werror=reorder]
const wchar_t* mText;
^~~~~
hb-directwrite.cc:488:12: error: 'uint32_t TextAnalysis::mTextLength' [-Werror=reorder]
uint32_t mTextLength;
^~~~~~~~~~~
hb-directwrite.cc:282:3: error: when initialized here [-Werror=reorder]
TextAnalysis (const wchar_t* text, uint32_t textLength,
^~~~~~~~~~~~
hb-directwrite.cc: In function 'hb_bool_t _hb_directwrite_shape_full(hb_shape_plan_t*, hb_font_t*, hb_buffer_t*, const hb_feature_t*, unsigned int, float)':
2019-01-18 18:22:07 +01:00
|
|
|
: mTextLength (textLength), mText (text), mLocaleName (localeName),
|
2018-12-12 12:44:37 +01:00
|
|
|
mReadingDirection (readingDirection), mCurrentRun (nullptr) {}
|
2018-12-17 19:01:01 +01:00
|
|
|
~TextAnalysis ()
|
2018-03-25 15:49:23 +02:00
|
|
|
{
|
2015-09-11 07:18:12 +02:00
|
|
|
// delete runs, except mRunHead which is part of the TextAnalysis object
|
2018-03-25 15:49:23 +02:00
|
|
|
for (Run *run = mRunHead.nextRun; run;)
|
|
|
|
{
|
2015-09-11 07:18:12 +02:00
|
|
|
Run *origRun = run;
|
|
|
|
run = run->nextRun;
|
2018-03-22 12:34:38 +01:00
|
|
|
delete origRun;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
STDMETHODIMP
|
|
|
|
GenerateResults (IDWriteTextAnalyzer* textAnalyzer, Run **runHead)
|
2018-03-25 15:49:23 +02:00
|
|
|
{
|
2015-09-11 07:18:12 +02:00
|
|
|
// Analyzes the text using the script analyzer and returns
|
|
|
|
// the result as a series of runs.
|
|
|
|
|
|
|
|
HRESULT hr = S_OK;
|
|
|
|
|
|
|
|
// Initially start out with one result that covers the entire range.
|
|
|
|
// This result will be subdivided by the analysis processes.
|
|
|
|
mRunHead.mTextStart = 0;
|
|
|
|
mRunHead.mTextLength = mTextLength;
|
|
|
|
mRunHead.mBidiLevel =
|
|
|
|
(mReadingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
|
2017-10-15 12:11:08 +02:00
|
|
|
mRunHead.nextRun = nullptr;
|
2015-09-11 07:18:12 +02:00
|
|
|
mCurrentRun = &mRunHead;
|
|
|
|
|
|
|
|
// Call each of the analyzers in sequence, recording their results.
|
2018-03-25 15:49:23 +02:00
|
|
|
if (SUCCEEDED (hr = textAnalyzer->AnalyzeScript (this, 0, mTextLength, this)))
|
2015-09-11 07:18:12 +02:00
|
|
|
*runHead = &mRunHead;
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDWriteTextAnalysisSource implementation
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
IFACEMETHODIMP
|
|
|
|
GetTextAtPosition (uint32_t textPosition,
|
|
|
|
OUT wchar_t const** textString,
|
|
|
|
OUT uint32_t* textLength)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2018-03-25 15:49:23 +02:00
|
|
|
if (textPosition >= mTextLength)
|
|
|
|
{
|
2015-09-11 07:18:12 +02:00
|
|
|
// No text at this position, valid query though.
|
2017-10-15 12:11:08 +02:00
|
|
|
*textString = nullptr;
|
2015-09-11 07:18:12 +02:00
|
|
|
*textLength = 0;
|
|
|
|
}
|
2018-03-25 15:49:23 +02:00
|
|
|
else
|
|
|
|
{
|
2015-09-11 07:18:12 +02:00
|
|
|
*textString = mText + textPosition;
|
|
|
|
*textLength = mTextLength - textPosition;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
IFACEMETHODIMP
|
|
|
|
GetTextBeforePosition (uint32_t textPosition,
|
|
|
|
OUT wchar_t const** textString,
|
|
|
|
OUT uint32_t* textLength)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2018-03-25 15:49:23 +02:00
|
|
|
if (textPosition == 0 || textPosition > mTextLength)
|
|
|
|
{
|
2015-09-11 07:18:12 +02:00
|
|
|
// Either there is no text before here (== 0), or this
|
2018-01-31 14:44:45 +01:00
|
|
|
// is an invalid position. The query is considered valid though.
|
2017-10-15 12:11:08 +02:00
|
|
|
*textString = nullptr;
|
2015-09-11 07:18:12 +02:00
|
|
|
*textLength = 0;
|
|
|
|
}
|
2018-03-25 15:49:23 +02:00
|
|
|
else
|
|
|
|
{
|
2015-09-11 07:18:12 +02:00
|
|
|
*textString = mText;
|
|
|
|
*textLength = textPosition;
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
IFACEMETHODIMP_ (DWRITE_READING_DIRECTION)
|
2018-12-17 19:01:01 +01:00
|
|
|
GetParagraphReadingDirection () { return mReadingDirection; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
IFACEMETHODIMP GetLocaleName (uint32_t textPosition, uint32_t* textLength,
|
|
|
|
wchar_t const** localeName)
|
|
|
|
{ return S_OK; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
IFACEMETHODIMP
|
2018-12-12 12:44:37 +01:00
|
|
|
GetNumberSubstitution (uint32_t textPosition,
|
|
|
|
OUT uint32_t* textLength,
|
|
|
|
OUT IDWriteNumberSubstitution** numberSubstitution)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
|
|
|
// We do not support number substitution.
|
2017-10-15 12:11:08 +02:00
|
|
|
*numberSubstitution = nullptr;
|
2015-09-11 07:18:12 +02:00
|
|
|
*textLength = mTextLength - textPosition;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDWriteTextAnalysisSink implementation
|
|
|
|
|
|
|
|
IFACEMETHODIMP
|
2018-12-12 12:44:37 +01:00
|
|
|
SetScriptAnalysis (uint32_t textPosition, uint32_t textLength,
|
|
|
|
DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2018-03-22 12:34:38 +01:00
|
|
|
SetCurrentRun (textPosition);
|
|
|
|
SplitCurrentRun (textPosition);
|
2016-06-23 14:11:37 +02:00
|
|
|
while (textLength > 0)
|
|
|
|
{
|
2018-03-22 12:34:38 +01:00
|
|
|
Run *run = FetchNextRun (&textLength);
|
2015-09-11 07:18:12 +02:00
|
|
|
run->mScript = *scriptAnalysis;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP
|
2018-12-12 12:44:37 +01:00
|
|
|
SetLineBreakpoints (uint32_t textPosition,
|
|
|
|
uint32_t textLength,
|
|
|
|
const DWRITE_LINE_BREAKPOINT* lineBreakpoints)
|
|
|
|
{ return S_OK; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
IFACEMETHODIMP SetBidiLevel (uint32_t textPosition, uint32_t textLength,
|
|
|
|
uint8_t explicitLevel, uint8_t resolvedLevel)
|
|
|
|
{ return S_OK; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
IFACEMETHODIMP
|
2018-12-12 12:44:37 +01:00
|
|
|
SetNumberSubstitution (uint32_t textPosition, uint32_t textLength,
|
|
|
|
IDWriteNumberSubstitution* numberSubstitution)
|
|
|
|
{ return S_OK; }
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
protected:
|
2018-03-22 12:34:38 +01:00
|
|
|
Run *FetchNextRun (IN OUT uint32_t* textLength)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
|
|
|
// Used by the sink setters, this returns a reference to the next run.
|
|
|
|
// Position and length are adjusted to now point after the current run
|
|
|
|
// being returned.
|
|
|
|
|
|
|
|
Run *origRun = mCurrentRun;
|
|
|
|
// Split the tail if needed (the length remaining is less than the
|
|
|
|
// current run's size).
|
2016-06-23 14:11:37 +02:00
|
|
|
if (*textLength < mCurrentRun->mTextLength)
|
|
|
|
SplitCurrentRun (mCurrentRun->mTextStart + *textLength);
|
|
|
|
else
|
2015-09-11 07:18:12 +02:00
|
|
|
// Just advance the current run.
|
|
|
|
mCurrentRun = mCurrentRun->nextRun;
|
|
|
|
*textLength -= origRun->mTextLength;
|
|
|
|
|
|
|
|
// Return a reference to the run that was just current.
|
|
|
|
return origRun;
|
|
|
|
}
|
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
void SetCurrentRun (uint32_t textPosition)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
|
|
|
// Move the current run to the given position.
|
|
|
|
// Since the analyzers generally return results in a forward manner,
|
|
|
|
// this will usually just return early. If not, find the
|
|
|
|
// corresponding run for the text position.
|
|
|
|
|
2016-06-23 14:11:37 +02:00
|
|
|
if (mCurrentRun && mCurrentRun->ContainsTextPosition (textPosition))
|
2015-09-11 07:18:12 +02:00
|
|
|
return;
|
|
|
|
|
2018-03-25 15:49:23 +02:00
|
|
|
for (Run *run = &mRunHead; run; run = run->nextRun)
|
2016-06-23 14:11:37 +02:00
|
|
|
if (run->ContainsTextPosition (textPosition))
|
|
|
|
{
|
2018-04-11 15:30:13 +02:00
|
|
|
mCurrentRun = run;
|
|
|
|
return;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
2018-04-28 11:28:55 +02:00
|
|
|
assert (0); // We should always be able to find the text position in one of our runs
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
void SplitCurrentRun (uint32_t splitPosition)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2016-06-23 14:11:37 +02:00
|
|
|
if (!mCurrentRun)
|
|
|
|
{
|
2018-04-28 11:28:55 +02:00
|
|
|
assert (0); // SplitCurrentRun called without current run
|
2015-09-11 07:18:12 +02:00
|
|
|
// Shouldn't be calling this when no current run is set!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Split the current run.
|
2016-06-23 14:11:37 +02:00
|
|
|
if (splitPosition <= mCurrentRun->mTextStart)
|
|
|
|
{
|
2015-09-11 07:18:12 +02:00
|
|
|
// No need to split, already the start of a run
|
|
|
|
// or before it. Usually the first.
|
|
|
|
return;
|
|
|
|
}
|
2018-03-22 12:34:38 +01:00
|
|
|
Run *newRun = new Run;
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
*newRun = *mCurrentRun;
|
|
|
|
|
|
|
|
// Insert the new run in our linked list.
|
|
|
|
newRun->nextRun = mCurrentRun->nextRun;
|
|
|
|
mCurrentRun->nextRun = newRun;
|
|
|
|
|
|
|
|
// Adjust runs' text positions and lengths.
|
2016-04-01 17:47:07 +02:00
|
|
|
uint32_t splitPoint = splitPosition - mCurrentRun->mTextStart;
|
2015-09-11 07:18:12 +02:00
|
|
|
newRun->mTextStart += splitPoint;
|
|
|
|
newRun->mTextLength -= splitPoint;
|
|
|
|
mCurrentRun->mTextLength = splitPoint;
|
|
|
|
mCurrentRun = newRun;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Input
|
|
|
|
// (weak references are fine here, since this class is a transient
|
|
|
|
// stack-based helper that doesn't need to copy data)
|
2016-04-01 17:47:07 +02:00
|
|
|
uint32_t mTextLength;
|
|
|
|
const wchar_t* mText;
|
|
|
|
const wchar_t* mLocaleName;
|
2015-09-11 07:18:12 +02:00
|
|
|
DWRITE_READING_DIRECTION mReadingDirection;
|
|
|
|
|
|
|
|
// Current processing state.
|
|
|
|
Run *mCurrentRun;
|
|
|
|
|
|
|
|
// Output is a list of runs starting here
|
|
|
|
Run mRunHead;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shaper
|
|
|
|
*/
|
|
|
|
|
2021-06-03 10:45:23 +02:00
|
|
|
hb_bool_t
|
|
|
|
_hb_directwrite_shape (hb_shape_plan_t *shape_plan,
|
|
|
|
hb_font_t *font,
|
|
|
|
hb_buffer_t *buffer,
|
|
|
|
const hb_feature_t *features,
|
|
|
|
unsigned int num_features)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
|
|
|
hb_face_t *face = font->face;
|
2018-11-16 08:29:13 +01:00
|
|
|
const hb_directwrite_face_data_t *face_data = face->data.directwrite;
|
2016-06-21 11:27:26 +02:00
|
|
|
IDWriteFactory *dwriteFactory = face_data->dwriteFactory;
|
|
|
|
IDWriteFontFace *fontFace = face_data->fontFace;
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
IDWriteTextAnalyzer* analyzer;
|
2018-03-22 12:34:38 +01:00
|
|
|
dwriteFactory->CreateTextAnalyzer (&analyzer);
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
unsigned int scratch_size;
|
|
|
|
hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
|
|
|
|
#define ALLOCATE_ARRAY(Type, name, len) \
|
|
|
|
Type *name = (Type *) scratch; \
|
2019-05-14 02:28:59 +02:00
|
|
|
do { \
|
2015-09-11 07:18:12 +02:00
|
|
|
unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
|
|
|
|
assert (_consumed <= scratch_size); \
|
|
|
|
scratch += _consumed; \
|
|
|
|
scratch_size -= _consumed; \
|
2019-05-14 02:28:59 +02:00
|
|
|
} while (0)
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
#define utf16_index() var1.u32
|
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
ALLOCATE_ARRAY (wchar_t, textString, buffer->len * 2);
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
unsigned int chars_len = 0;
|
|
|
|
for (unsigned int i = 0; i < buffer->len; i++)
|
|
|
|
{
|
|
|
|
hb_codepoint_t c = buffer->info[i].codepoint;
|
2018-03-22 12:34:38 +01:00
|
|
|
buffer->info[i].utf16_index () = chars_len;
|
|
|
|
if (likely (c <= 0xFFFFu))
|
2016-04-01 08:39:57 +02:00
|
|
|
textString[chars_len++] = c;
|
2018-03-22 12:34:38 +01:00
|
|
|
else if (unlikely (c > 0x10FFFFu))
|
2016-04-01 08:39:57 +02:00
|
|
|
textString[chars_len++] = 0xFFFDu;
|
2018-03-25 15:49:23 +02:00
|
|
|
else
|
|
|
|
{
|
2016-04-01 08:39:57 +02:00
|
|
|
textString[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
|
2016-08-09 02:24:04 +02:00
|
|
|
textString[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
|
2018-03-25 15:49:23 +02:00
|
|
|
/* Need log_clusters to assign features. */
|
|
|
|
chars_len = 0;
|
|
|
|
for (unsigned int i = 0; i < buffer->len; i++)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
2018-03-25 15:49:23 +02:00
|
|
|
hb_codepoint_t c = buffer->info[i].codepoint;
|
|
|
|
unsigned int cluster = buffer->info[i].cluster;
|
|
|
|
log_clusters[chars_len++] = cluster;
|
|
|
|
if (hb_in_range (c, 0x10000u, 0x10FFFFu))
|
|
|
|
log_clusters[chars_len++] = cluster; /* Surrogates. */
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
DWRITE_READING_DIRECTION readingDirection;
|
|
|
|
readingDirection = buffer->props.direction ?
|
|
|
|
DWRITE_READING_DIRECTION_RIGHT_TO_LEFT :
|
|
|
|
DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2015-12-28 23:23:24 +01:00
|
|
|
/*
|
2015-09-11 07:18:12 +02:00
|
|
|
* There's an internal 16-bit limit on some things inside the analyzer,
|
|
|
|
* but we never attempt to shape a word longer than 64K characters
|
|
|
|
* in a single gfxShapedWord, so we cannot exceed that limit.
|
|
|
|
*/
|
2021-04-15 13:52:19 +02:00
|
|
|
uint32_t textLength = chars_len;
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
TextAnalysis analysis (textString, textLength, nullptr, readingDirection);
|
2015-09-11 07:18:12 +02:00
|
|
|
TextAnalysis::Run *runHead;
|
2016-06-21 11:27:26 +02:00
|
|
|
HRESULT hr;
|
2018-03-22 12:34:38 +01:00
|
|
|
hr = analysis.GenerateResults (analyzer, &runHead);
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-03-31 20:19:44 +02:00
|
|
|
#define FAIL(...) \
|
|
|
|
HB_STMT_START { \
|
2017-10-15 12:11:08 +02:00
|
|
|
DEBUG_MSG (DIRECTWRITE, nullptr, __VA_ARGS__); \
|
2016-03-31 20:19:44 +02:00
|
|
|
return false; \
|
2019-09-26 11:35:27 +02:00
|
|
|
} HB_STMT_END
|
2016-03-31 20:19:44 +02:00
|
|
|
|
2016-04-01 08:39:57 +02:00
|
|
|
if (FAILED (hr))
|
2016-03-31 20:19:44 +02:00
|
|
|
FAIL ("Analyzer failed to generate results.");
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-04-01 17:47:07 +02:00
|
|
|
uint32_t maxGlyphCount = 3 * textLength / 2 + 16;
|
|
|
|
uint32_t glyphCount;
|
2016-04-01 08:39:57 +02:00
|
|
|
bool isRightToLeft = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2016-04-01 08:39:57 +02:00
|
|
|
const wchar_t localeName[20] = {0};
|
2020-04-20 21:18:23 +02:00
|
|
|
if (buffer->props.language)
|
2016-04-01 08:39:57 +02:00
|
|
|
mbstowcs ((wchar_t*) localeName,
|
2018-12-12 12:44:37 +01:00
|
|
|
hb_language_to_string (buffer->props.language), 20);
|
2016-03-30 22:21:40 +02:00
|
|
|
|
2021-06-20 18:59:13 +02:00
|
|
|
/*
|
|
|
|
* Set up features.
|
|
|
|
*/
|
2021-07-27 19:51:23 +02:00
|
|
|
static_assert ((sizeof (DWRITE_TYPOGRAPHIC_FEATURES) == sizeof (hb_ms_features_t)), "");
|
|
|
|
static_assert ((sizeof (DWRITE_FONT_FEATURE) == sizeof (hb_ms_feature_t)), "");
|
|
|
|
hb_vector_t<hb_ms_features_t *> range_features;
|
2021-06-20 18:59:13 +02:00
|
|
|
hb_vector_t<uint32_t> range_char_counts;
|
|
|
|
if (num_features)
|
|
|
|
{
|
2021-07-27 19:51:23 +02:00
|
|
|
hb_vector_t<hb_ms_feature_t> feature_records;
|
2021-07-27 20:43:53 +02:00
|
|
|
hb_vector_t<hb_ms_range_record_t> range_records;
|
2021-07-27 19:51:23 +02:00
|
|
|
if (hb_ms_setup_features (features, num_features, feature_records, range_records))
|
|
|
|
hb_ms_make_feature_ranges (feature_records,
|
|
|
|
range_records,
|
|
|
|
0,
|
|
|
|
chars_len,
|
|
|
|
log_clusters,
|
|
|
|
range_features,
|
|
|
|
range_char_counts);
|
2016-04-01 08:39:57 +02:00
|
|
|
}
|
2021-06-20 18:59:13 +02:00
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
uint16_t* clusterMap;
|
|
|
|
clusterMap = new uint16_t[textLength];
|
|
|
|
DWRITE_SHAPING_TEXT_PROPERTIES* textProperties;
|
|
|
|
textProperties = new DWRITE_SHAPING_TEXT_PROPERTIES[textLength];
|
2021-06-20 18:59:13 +02:00
|
|
|
|
2016-04-01 08:39:57 +02:00
|
|
|
retry_getglyphs:
|
2018-03-22 12:34:38 +01:00
|
|
|
uint16_t* glyphIndices = new uint16_t[maxGlyphCount];
|
2018-12-12 12:44:37 +01:00
|
|
|
DWRITE_SHAPING_GLYPH_PROPERTIES* glyphProperties;
|
|
|
|
glyphProperties = new DWRITE_SHAPING_GLYPH_PROPERTIES[maxGlyphCount];
|
2016-04-01 08:39:57 +02:00
|
|
|
|
2021-07-27 19:51:23 +02:00
|
|
|
hr = analyzer->GetGlyphs (textString,
|
|
|
|
chars_len,
|
|
|
|
fontFace,
|
|
|
|
false,
|
|
|
|
isRightToLeft,
|
|
|
|
&runHead->mScript,
|
|
|
|
localeName,
|
|
|
|
nullptr,
|
|
|
|
(const DWRITE_TYPOGRAPHIC_FEATURES**) range_features.arrayZ,
|
|
|
|
range_char_counts.arrayZ,
|
|
|
|
range_features.length,
|
|
|
|
maxGlyphCount,
|
|
|
|
clusterMap,
|
|
|
|
textProperties,
|
|
|
|
glyphIndices,
|
|
|
|
glyphProperties,
|
|
|
|
&glyphCount);
|
2016-04-01 08:39:57 +02:00
|
|
|
|
|
|
|
if (unlikely (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)))
|
|
|
|
{
|
2018-03-22 12:34:38 +01:00
|
|
|
delete [] glyphIndices;
|
|
|
|
delete [] glyphProperties;
|
2016-03-31 15:45:37 +02:00
|
|
|
|
2016-04-01 08:39:57 +02:00
|
|
|
maxGlyphCount *= 2;
|
2016-03-31 15:45:37 +02:00
|
|
|
|
|
|
|
goto retry_getglyphs;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
2016-04-01 08:39:57 +02:00
|
|
|
if (FAILED (hr))
|
2016-03-31 15:45:37 +02:00
|
|
|
FAIL ("Analyzer failed to get glyphs.");
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
float* glyphAdvances = new float[maxGlyphCount];
|
|
|
|
DWRITE_GLYPH_OFFSET* glyphOffsets = new DWRITE_GLYPH_OFFSET[maxGlyphCount];
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
/* The -2 in the following is to compensate for possible
|
2018-03-22 12:34:38 +01:00
|
|
|
* alignment needed after the WORD array. sizeof (WORD) == 2. */
|
|
|
|
unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
|
2018-12-12 12:44:37 +01:00
|
|
|
/ (sizeof (WORD) +
|
2019-02-22 10:43:42 +01:00
|
|
|
sizeof (DWRITE_SHAPING_GLYPH_PROPERTIES) +
|
|
|
|
sizeof (int) +
|
|
|
|
sizeof (DWRITE_GLYPH_OFFSET) +
|
|
|
|
sizeof (uint32_t));
|
2016-04-01 08:39:57 +02:00
|
|
|
ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
#undef ALLOCATE_ARRAY
|
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
int fontEmSize = font->face->get_upem ();
|
2018-12-12 12:44:37 +01:00
|
|
|
if (fontEmSize < 0) fontEmSize = -fontEmSize;
|
2016-04-01 08:39:57 +02:00
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
if (fontEmSize < 0) fontEmSize = -fontEmSize;
|
2016-04-01 08:39:57 +02:00
|
|
|
double x_mult = (double) font->x_scale / fontEmSize;
|
|
|
|
double y_mult = (double) font->y_scale / fontEmSize;
|
|
|
|
|
2021-07-27 19:51:23 +02:00
|
|
|
hr = analyzer->GetGlyphPlacements (textString,
|
|
|
|
clusterMap,
|
|
|
|
textProperties,
|
|
|
|
chars_len,
|
|
|
|
glyphIndices,
|
|
|
|
glyphProperties,
|
|
|
|
glyphCount,
|
|
|
|
fontFace,
|
|
|
|
fontEmSize,
|
|
|
|
false,
|
|
|
|
isRightToLeft,
|
|
|
|
&runHead->mScript,
|
|
|
|
localeName,
|
|
|
|
(const DWRITE_TYPOGRAPHIC_FEATURES**) range_features.arrayZ,
|
|
|
|
range_char_counts.arrayZ,
|
|
|
|
range_features.length,
|
|
|
|
glyphAdvances,
|
|
|
|
glyphOffsets);
|
2016-04-01 08:39:57 +02:00
|
|
|
|
|
|
|
if (FAILED (hr))
|
2016-03-31 15:45:37 +02:00
|
|
|
FAIL ("Analyzer failed to get glyph placements.");
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
/* Ok, we've got everything we need, now compose output buffer,
|
|
|
|
* very, *very*, carefully! */
|
|
|
|
|
|
|
|
/* Calculate visual-clusters. That's what we ship. */
|
2016-04-01 08:39:57 +02:00
|
|
|
for (unsigned int i = 0; i < glyphCount; i++)
|
2019-01-18 19:45:33 +01:00
|
|
|
vis_clusters[i] = (uint32_t) -1;
|
2016-04-01 08:39:57 +02:00
|
|
|
for (unsigned int i = 0; i < buffer->len; i++)
|
|
|
|
{
|
|
|
|
uint32_t *p =
|
2018-03-22 12:34:38 +01:00
|
|
|
&vis_clusters[log_clusters[buffer->info[i].utf16_index ()]];
|
2019-05-08 05:54:31 +02:00
|
|
|
*p = hb_min (*p, buffer->info[i].cluster);
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
2016-04-01 08:39:57 +02:00
|
|
|
for (unsigned int i = 1; i < glyphCount; i++)
|
2019-01-18 19:45:33 +01:00
|
|
|
if (vis_clusters[i] == (uint32_t) -1)
|
2015-09-11 07:18:12 +02:00
|
|
|
vis_clusters[i] = vis_clusters[i - 1];
|
|
|
|
|
|
|
|
#undef utf16_index
|
|
|
|
|
2016-04-01 08:39:57 +02:00
|
|
|
if (unlikely (!buffer->ensure (glyphCount)))
|
2016-03-31 15:45:37 +02:00
|
|
|
FAIL ("Buffer in error");
|
2015-09-11 07:18:12 +02:00
|
|
|
|
|
|
|
#undef FAIL
|
|
|
|
|
|
|
|
/* Set glyph infos */
|
|
|
|
buffer->len = 0;
|
2016-04-01 08:39:57 +02:00
|
|
|
for (unsigned int i = 0; i < glyphCount; i++)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
|
|
|
hb_glyph_info_t *info = &buffer->info[buffer->len++];
|
|
|
|
|
2016-04-01 08:39:57 +02:00
|
|
|
info->codepoint = glyphIndices[i];
|
2015-09-11 07:18:12 +02:00
|
|
|
info->cluster = vis_clusters[i];
|
|
|
|
|
|
|
|
/* The rest is crap. Let's store position info there for now. */
|
2016-04-01 08:39:57 +02:00
|
|
|
info->mask = glyphAdvances[i];
|
|
|
|
info->var1.i32 = glyphOffsets[i].advanceOffset;
|
|
|
|
info->var2.i32 = glyphOffsets[i].ascenderOffset;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set glyph positions */
|
|
|
|
buffer->clear_positions ();
|
2016-04-01 08:39:57 +02:00
|
|
|
for (unsigned int i = 0; i < glyphCount; i++)
|
2015-09-11 07:18:12 +02:00
|
|
|
{
|
|
|
|
hb_glyph_info_t *info = &buffer->info[i];
|
|
|
|
hb_glyph_position_t *pos = &buffer->pos[i];
|
|
|
|
|
|
|
|
/* TODO vertical */
|
2016-03-31 14:26:16 +02:00
|
|
|
pos->x_advance = x_mult * (int32_t) info->mask;
|
2018-12-12 12:44:37 +01:00
|
|
|
pos->x_offset = x_mult * (isRightToLeft ? -info->var1.i32 : info->var1.i32);
|
2016-03-31 14:26:16 +02:00
|
|
|
pos->y_offset = y_mult * info->var2.i32;
|
2015-09-11 07:18:12 +02:00
|
|
|
}
|
|
|
|
|
2018-12-12 12:44:37 +01:00
|
|
|
if (isRightToLeft) hb_buffer_reverse (buffer);
|
2015-09-11 07:18:12 +02:00
|
|
|
|
2022-01-22 19:41:30 +01:00
|
|
|
buffer->clear_glyph_flags ();
|
|
|
|
buffer->unsafe_to_break ();
|
2021-12-01 17:57:21 +01:00
|
|
|
|
2018-03-22 12:34:38 +01:00
|
|
|
delete [] clusterMap;
|
|
|
|
delete [] glyphIndices;
|
|
|
|
delete [] textProperties;
|
|
|
|
delete [] glyphProperties;
|
|
|
|
delete [] glyphAdvances;
|
|
|
|
delete [] glyphOffsets;
|
2016-04-05 23:01:05 +02:00
|
|
|
|
2015-09-11 07:18:12 +02:00
|
|
|
/* Wow, done! */
|
|
|
|
return true;
|
2015-12-28 23:23:24 +01:00
|
|
|
}
|
2016-12-17 08:00:40 +01:00
|
|
|
|
2019-02-22 10:43:42 +01:00
|
|
|
struct _hb_directwrite_font_table_context {
|
|
|
|
IDWriteFontFace *face;
|
|
|
|
void *table_context;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
_hb_directwrite_table_data_release (void *data)
|
|
|
|
{
|
|
|
|
_hb_directwrite_font_table_context *context = (_hb_directwrite_font_table_context *) data;
|
|
|
|
context->face->ReleaseFontTable (context->table_context);
|
2021-07-08 18:58:50 +02:00
|
|
|
hb_free (context);
|
2019-02-22 10:43:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static hb_blob_t *
|
2019-07-05 16:16:41 +02:00
|
|
|
_hb_directwrite_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
|
2019-02-22 10:43:42 +01:00
|
|
|
{
|
|
|
|
IDWriteFontFace *dw_face = ((IDWriteFontFace *) user_data);
|
|
|
|
const void *data;
|
|
|
|
uint32_t length;
|
|
|
|
void *table_context;
|
|
|
|
BOOL exists;
|
2019-07-16 19:57:01 +02:00
|
|
|
if (!dw_face || FAILED (dw_face->TryGetFontTable (hb_uint32_swap (tag), &data,
|
2019-02-22 10:43:42 +01:00
|
|
|
&length, &table_context, &exists)))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (!data || !exists || !length)
|
|
|
|
{
|
|
|
|
dw_face->ReleaseFontTable (table_context);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-07-08 18:58:50 +02:00
|
|
|
_hb_directwrite_font_table_context *context = (_hb_directwrite_font_table_context *) hb_malloc (sizeof (_hb_directwrite_font_table_context));
|
2019-02-22 10:43:42 +01:00
|
|
|
context->face = dw_face;
|
|
|
|
context->table_context = table_context;
|
|
|
|
|
|
|
|
return hb_blob_create ((const char *) data, length, HB_MEMORY_MODE_READONLY,
|
|
|
|
context, _hb_directwrite_table_data_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_hb_directwrite_font_release (void *data)
|
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
((IDWriteFontFace *) data)->Release ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hb_directwrite_face_create:
|
2019-04-30 22:01:04 +02:00
|
|
|
* @font_face: a DirectWrite IDWriteFontFace object.
|
|
|
|
*
|
2020-12-30 22:58:37 +01:00
|
|
|
* Constructs a new face object from the specified DirectWrite IDWriteFontFace.
|
|
|
|
*
|
2019-04-30 22:01:04 +02:00
|
|
|
* Return value: #hb_face_t object corresponding to the given input
|
2019-03-29 21:00:56 +01:00
|
|
|
*
|
2019-03-29 18:55:12 +01:00
|
|
|
* Since: 2.4.0
|
2019-02-22 10:43:42 +01:00
|
|
|
**/
|
|
|
|
hb_face_t *
|
|
|
|
hb_directwrite_face_create (IDWriteFontFace *font_face)
|
|
|
|
{
|
|
|
|
if (font_face)
|
|
|
|
font_face->AddRef ();
|
2019-07-05 16:16:41 +02:00
|
|
|
return hb_face_create_for_tables (_hb_directwrite_reference_table, font_face,
|
2019-02-22 10:43:42 +01:00
|
|
|
_hb_directwrite_font_release);
|
|
|
|
}
|
2019-03-29 21:00:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* hb_directwrite_face_get_font_face:
|
2019-04-30 22:01:04 +02:00
|
|
|
* @face: a #hb_face_t object
|
|
|
|
*
|
2020-12-30 22:58:37 +01:00
|
|
|
* Gets the DirectWrite IDWriteFontFace associated with @face.
|
|
|
|
*
|
2019-04-30 22:01:04 +02:00
|
|
|
* Return value: DirectWrite IDWriteFontFace object corresponding to the given input
|
2019-03-29 21:00:56 +01:00
|
|
|
*
|
2019-06-01 08:25:37 +02:00
|
|
|
* Since: 2.5.0
|
2019-03-29 21:00:56 +01:00
|
|
|
**/
|
|
|
|
IDWriteFontFace *
|
|
|
|
hb_directwrite_face_get_font_face (hb_face_t *face)
|
|
|
|
{
|
|
|
|
return face->data.directwrite->fontFace;
|
|
|
|
}
|
2019-06-18 07:41:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|