Blame src/hb-directwrite.cc

Packit 874993
/*
Packit 874993
 * Copyright © 2015-2016  Ebrahim Byagowi
Packit 874993
 *
Packit 874993
 *  This is part of HarfBuzz, a text shaping library.
Packit 874993
 *
Packit 874993
 * Permission is hereby granted, without written agreement and without
Packit 874993
 * license or royalty fees, to use, copy, modify, and distribute this
Packit 874993
 * software and its documentation for any purpose, provided that the
Packit 874993
 * above copyright notice and the following two paragraphs appear in
Packit 874993
 * all copies of this software.
Packit 874993
 *
Packit 874993
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
Packit 874993
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
Packit 874993
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
Packit 874993
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
Packit 874993
 * DAMAGE.
Packit 874993
 *
Packit 874993
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
Packit 874993
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit 874993
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
Packit 874993
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
Packit 874993
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Packit 874993
 */
Packit 874993
Packit 874993
#define HB_SHAPER directwrite
Packit 874993
#include "hb-shaper-impl-private.hh"
Packit 874993
Packit 874993
#include <DWrite_1.h>
Packit 874993
Packit 874993
#include "hb-directwrite.h"
Packit 874993
Packit 874993
Packit 874993
#ifndef HB_DEBUG_DIRECTWRITE
Packit 874993
#define HB_DEBUG_DIRECTWRITE (HB_DEBUG+0)
Packit 874993
#endif
Packit 874993
Packit 874993
HB_SHAPER_DATA_ENSURE_DEFINE(directwrite, face)
Packit 874993
HB_SHAPER_DATA_ENSURE_DEFINE(directwrite, font)
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * DirectWrite font stream helpers
Packit 874993
 */
Packit 874993
Packit 874993
// This is a font loader which provides only one font (unlike its original design).
Packit 874993
// For a better implementation which was also source of this
Packit 874993
// and DWriteFontFileStream, have a look at to NativeFontResourceDWrite.cpp in Mozilla
Packit 874993
class DWriteFontFileLoader : public IDWriteFontFileLoader
Packit 874993
{
Packit 874993
private:
Packit 874993
  IDWriteFontFileStream *mFontFileStream;
Packit 874993
public:
Packit 874993
  DWriteFontFileLoader (IDWriteFontFileStream *fontFileStream) {
Packit 874993
    mFontFileStream = fontFileStream;
Packit 874993
  }
Packit 874993
Packit 874993
  // IUnknown interface
Packit 874993
  IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) { return S_OK; }
Packit 874993
  IFACEMETHOD_(ULONG, AddRef)() { return 1; }
Packit 874993
  IFACEMETHOD_(ULONG, Release)() { return 1; }
Packit 874993
Packit 874993
  // IDWriteFontFileLoader methods
Packit 874993
  virtual HRESULT STDMETHODCALLTYPE CreateStreamFromKey(void const* fontFileReferenceKey,
Packit 874993
    UINT32 fontFileReferenceKeySize,
Packit 874993
    OUT IDWriteFontFileStream** fontFileStream)
Packit 874993
  {
Packit 874993
    *fontFileStream = mFontFileStream;
Packit 874993
    return S_OK;
Packit 874993
  }
Packit 874993
};
Packit 874993
Packit 874993
class DWriteFontFileStream : public IDWriteFontFileStream
Packit 874993
{
Packit 874993
private:
Packit 874993
  uint8_t *mData;
Packit 874993
  uint32_t mSize;
Packit 874993
public:
Packit 874993
  DWriteFontFileStream(uint8_t *aData, uint32_t aSize)
Packit 874993
  {
Packit 874993
    mData = aData;
Packit 874993
    mSize = aSize;
Packit 874993
  }
Packit 874993
Packit 874993
  // IUnknown interface
Packit 874993
  IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) { return S_OK; }
Packit 874993
  IFACEMETHOD_(ULONG, AddRef)() { return 1; }
Packit 874993
  IFACEMETHOD_(ULONG, Release)() { return 1; }
Packit 874993
Packit 874993
  // IDWriteFontFileStream methods
Packit 874993
  virtual HRESULT STDMETHODCALLTYPE ReadFileFragment(void const** fragmentStart,
Packit 874993
    UINT64 fileOffset,
Packit 874993
    UINT64 fragmentSize,
Packit 874993
    OUT void** fragmentContext)
Packit 874993
  {
Packit 874993
    // We are required to do bounds checking.
Packit 874993
    if (fileOffset + fragmentSize > mSize) {
Packit 874993
      return E_FAIL;
Packit 874993
    }
Packit 874993
Packit 874993
    // truncate the 64 bit fileOffset to size_t sized index into mData
Packit 874993
    size_t index = static_cast<size_t> (fileOffset);
Packit 874993
Packit 874993
    // We should be alive for the duration of this.
Packit 874993
    *fragmentStart = &mData[index];
Packit 874993
    *fragmentContext = nullptr;
Packit 874993
    return S_OK;
Packit 874993
  }
Packit 874993
Packit 874993
  virtual void STDMETHODCALLTYPE ReleaseFileFragment(void* fragmentContext) { }
Packit 874993
Packit 874993
  virtual HRESULT STDMETHODCALLTYPE GetFileSize(OUT UINT64* fileSize)
Packit 874993
  {
Packit 874993
    *fileSize = mSize;
Packit 874993
    return S_OK;
Packit 874993
  }
Packit 874993
Packit 874993
  virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime(OUT UINT64* lastWriteTime)
Packit 874993
  {
Packit 874993
    return E_NOTIMPL;
Packit 874993
  }
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
* shaper face data
Packit 874993
*/
Packit 874993
Packit 874993
struct hb_directwrite_shaper_face_data_t {
Packit 874993
  IDWriteFactory *dwriteFactory;
Packit 874993
  IDWriteFontFile *fontFile;
Packit 874993
  IDWriteFontFileStream *fontFileStream;
Packit 874993
  IDWriteFontFileLoader *fontFileLoader;
Packit 874993
  IDWriteFontFace *fontFace;
Packit 874993
  hb_blob_t *faceBlob;
Packit 874993
};
Packit 874993
Packit 874993
hb_directwrite_shaper_face_data_t *
Packit 874993
_hb_directwrite_shaper_face_data_create(hb_face_t *face)
Packit 874993
{
Packit 874993
  hb_directwrite_shaper_face_data_t *data =
Packit 874993
    (hb_directwrite_shaper_face_data_t *) malloc (sizeof (hb_directwrite_shaper_face_data_t));
Packit 874993
  if (unlikely (!data))
Packit 874993
    return NULL;
Packit 874993
Packit 874993
  // TODO: factory and fontFileLoader should be cached separately
Packit 874993
  IDWriteFactory* dwriteFactory;
Packit 874993
  DWriteCreateFactory (
Packit 874993
    DWRITE_FACTORY_TYPE_SHARED,
Packit 874993
    __uuidof (IDWriteFactory),
Packit 874993
    (IUnknown**) &dwriteFactory
Packit 874993
  );
Packit 874993
Packit 874993
  HRESULT hr;
Packit 874993
  hb_blob_t *blob = hb_face_reference_blob (face);
Packit 874993
  IDWriteFontFileStream *fontFileStream = new DWriteFontFileStream (
Packit 874993
    (uint8_t*) hb_blob_get_data (blob, NULL), hb_blob_get_length (blob));
Packit 874993
Packit 874993
  IDWriteFontFileLoader *fontFileLoader = new DWriteFontFileLoader (fontFileStream);
Packit 874993
  dwriteFactory->RegisterFontFileLoader (fontFileLoader);
Packit 874993
Packit 874993
  IDWriteFontFile *fontFile;
Packit 874993
  uint64_t fontFileKey = 0;
Packit 874993
  hr = dwriteFactory->CreateCustomFontFileReference (&fontFileKey, sizeof (fontFileKey),
Packit 874993
      fontFileLoader, &fontFile);
Packit 874993
Packit 874993
#define FAIL(...) \
Packit 874993
  HB_STMT_START { \
Packit 874993
    DEBUG_MSG (DIRECTWRITE, NULL, __VA_ARGS__); \
Packit 874993
    return false; \
Packit 874993
  } HB_STMT_END;
Packit 874993
Packit 874993
  if (FAILED (hr)) {
Packit 874993
    FAIL ("Failed to load font file from data!");
Packit 874993
    return false;
Packit 874993
  }
Packit 874993
Packit 874993
  BOOL isSupported;
Packit 874993
  DWRITE_FONT_FILE_TYPE fileType;
Packit 874993
  DWRITE_FONT_FACE_TYPE faceType;
Packit 874993
  UINT32 numberOfFaces;
Packit 874993
  hr = fontFile->Analyze (&isSupported, &fileType, &faceType, &numberOfFaces);
Packit 874993
  if (FAILED (hr) || !isSupported) {
Packit 874993
    FAIL ("Font file is not supported.");
Packit 874993
    return false;
Packit 874993
  }
Packit 874993
Packit 874993
#undef FAIL
Packit 874993
Packit 874993
  IDWriteFontFace *fontFace;
Packit 874993
  dwriteFactory->CreateFontFace (faceType, 1, &fontFile, 0,
Packit 874993
    DWRITE_FONT_SIMULATIONS_NONE, &fontFace);
Packit 874993
Packit 874993
  data->dwriteFactory = dwriteFactory;
Packit 874993
  data->fontFile = fontFile;
Packit 874993
  data->fontFileStream = fontFileStream;
Packit 874993
  data->fontFileLoader = fontFileLoader;
Packit 874993
  data->fontFace = fontFace;
Packit 874993
  data->faceBlob = blob;
Packit 874993
Packit 874993
  return data;
Packit 874993
}
Packit 874993
Packit 874993
void
Packit 874993
_hb_directwrite_shaper_face_data_destroy(hb_directwrite_shaper_face_data_t *data)
Packit 874993
{
Packit 874993
  if (data->fontFace)
Packit 874993
    data->fontFace->Release ();
Packit 874993
  if (data->fontFile)
Packit 874993
    data->fontFile->Release ();
Packit 874993
  if (data->dwriteFactory) {
Packit 874993
    if (data->fontFileLoader)
Packit 874993
      data->dwriteFactory->UnregisterFontFileLoader (data->fontFileLoader);
Packit 874993
    data->dwriteFactory->Release ();
Packit 874993
  }
Packit 874993
  if (data->fontFileLoader)
Packit 874993
    delete data->fontFileLoader;
Packit 874993
  if (data->fontFileStream)
Packit 874993
    delete data->fontFileStream;
Packit 874993
  if (data->faceBlob)
Packit 874993
    hb_blob_destroy (data->faceBlob);
Packit 874993
  if (data)
Packit 874993
    free (data);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * shaper font data
Packit 874993
 */
Packit 874993
Packit 874993
struct hb_directwrite_shaper_font_data_t {
Packit 874993
};
Packit 874993
Packit 874993
hb_directwrite_shaper_font_data_t *
Packit 874993
_hb_directwrite_shaper_font_data_create (hb_font_t *font)
Packit 874993
{
Packit 874993
  if (unlikely (!hb_directwrite_shaper_face_data_ensure (font->face))) return NULL;
Packit 874993
Packit 874993
  hb_directwrite_shaper_font_data_t *data =
Packit 874993
    (hb_directwrite_shaper_font_data_t *) malloc (sizeof (hb_directwrite_shaper_font_data_t));
Packit 874993
  if (unlikely (!data))
Packit 874993
    return NULL;
Packit 874993
Packit 874993
  return data;
Packit 874993
}
Packit 874993
Packit 874993
void
Packit 874993
_hb_directwrite_shaper_font_data_destroy (hb_directwrite_shaper_font_data_t *data)
Packit 874993
{
Packit 874993
  free (data);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
/*
Packit 874993
 * shaper shape_plan data
Packit 874993
 */
Packit 874993
Packit 874993
struct hb_directwrite_shaper_shape_plan_data_t {};
Packit 874993
Packit 874993
hb_directwrite_shaper_shape_plan_data_t *
Packit 874993
_hb_directwrite_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan HB_UNUSED,
Packit 874993
					       const hb_feature_t *user_features HB_UNUSED,
Packit 874993
					       unsigned int        num_user_features HB_UNUSED,
Packit 874993
					       const int          *coords HB_UNUSED,
Packit 874993
					       unsigned int        num_coords HB_UNUSED)
Packit 874993
{
Packit 874993
  return (hb_directwrite_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
Packit 874993
}
Packit 874993
Packit 874993
void
Packit 874993
_hb_directwrite_shaper_shape_plan_data_destroy (hb_directwrite_shaper_shape_plan_data_t *data HB_UNUSED)
Packit 874993
{
Packit 874993
}
Packit 874993
Packit 874993
// Most of TextAnalysis is originally written by Bas Schouten for Mozilla project
Packit 874993
// but now is relicensed to MIT for HarfBuzz use
Packit 874993
class TextAnalysis
Packit 874993
  : public IDWriteTextAnalysisSource, public IDWriteTextAnalysisSink
Packit 874993
{
Packit 874993
public:
Packit 874993
Packit 874993
  IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) { return S_OK; }
Packit 874993
  IFACEMETHOD_(ULONG, AddRef)() { return 1; }
Packit 874993
  IFACEMETHOD_(ULONG, Release)() { return 1; }
Packit 874993
Packit 874993
  // A single contiguous run of characters containing the same analysis 
Packit 874993
  // results.
Packit 874993
  struct Run
Packit 874993
  {
Packit 874993
    uint32_t mTextStart;   // starting text position of this run
Packit 874993
    uint32_t mTextLength;  // number of contiguous code units covered
Packit 874993
    uint32_t mGlyphStart;  // starting glyph in the glyphs array
Packit 874993
    uint32_t mGlyphCount;  // number of glyphs associated with this run of 
Packit 874993
    // text
Packit 874993
    DWRITE_SCRIPT_ANALYSIS mScript;
Packit 874993
    uint8_t mBidiLevel;
Packit 874993
    bool mIsSideways;
Packit 874993
Packit 874993
    inline bool ContainsTextPosition(uint32_t aTextPosition) const
Packit 874993
    {
Packit 874993
      return aTextPosition >= mTextStart
Packit 874993
        && aTextPosition <  mTextStart + mTextLength;
Packit 874993
    }
Packit 874993
Packit 874993
    Run *nextRun;
Packit 874993
  };
Packit 874993
Packit 874993
public:
Packit 874993
  TextAnalysis(const wchar_t* text,
Packit 874993
    uint32_t textLength,
Packit 874993
    const wchar_t* localeName,
Packit 874993
    DWRITE_READING_DIRECTION readingDirection)
Packit 874993
    : mText(text)
Packit 874993
    , mTextLength(textLength)
Packit 874993
    , mLocaleName(localeName)
Packit 874993
    , mReadingDirection(readingDirection)
Packit 874993
    , mCurrentRun(NULL) { };
Packit 874993
Packit 874993
  ~TextAnalysis() {
Packit 874993
    // delete runs, except mRunHead which is part of the TextAnalysis object
Packit 874993
    for (Run *run = mRunHead.nextRun; run;) {
Packit 874993
      Run *origRun = run;
Packit 874993
      run = run->nextRun;
Packit 874993
      free (origRun);
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  STDMETHODIMP GenerateResults(IDWriteTextAnalyzer* textAnalyzer,
Packit 874993
    Run **runHead) {
Packit 874993
    // Analyzes the text using the script analyzer and returns
Packit 874993
    // the result as a series of runs.
Packit 874993
Packit 874993
    HRESULT hr = S_OK;
Packit 874993
Packit 874993
    // Initially start out with one result that covers the entire range.
Packit 874993
    // This result will be subdivided by the analysis processes.
Packit 874993
    mRunHead.mTextStart = 0;
Packit 874993
    mRunHead.mTextLength = mTextLength;
Packit 874993
    mRunHead.mBidiLevel =
Packit 874993
      (mReadingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
Packit 874993
    mRunHead.nextRun = NULL;
Packit 874993
    mCurrentRun = &mRunHead;
Packit 874993
Packit 874993
    // Call each of the analyzers in sequence, recording their results.
Packit 874993
    if (SUCCEEDED (hr = textAnalyzer->AnalyzeScript (this, 0, mTextLength, this))) {
Packit 874993
      *runHead = &mRunHead;
Packit 874993
    }
Packit 874993
Packit 874993
    return hr;
Packit 874993
  }
Packit 874993
Packit 874993
  // IDWriteTextAnalysisSource implementation
Packit 874993
Packit 874993
  IFACEMETHODIMP GetTextAtPosition(uint32_t textPosition,
Packit 874993
    OUT wchar_t const** textString,
Packit 874993
    OUT uint32_t* textLength)
Packit 874993
  {
Packit 874993
    if (textPosition >= mTextLength) {
Packit 874993
      // No text at this position, valid query though.
Packit 874993
      *textString = NULL;
Packit 874993
      *textLength = 0;
Packit 874993
    }
Packit 874993
    else {
Packit 874993
      *textString = mText + textPosition;
Packit 874993
      *textLength = mTextLength - textPosition;
Packit 874993
    }
Packit 874993
    return S_OK;
Packit 874993
  }
Packit 874993
Packit 874993
  IFACEMETHODIMP GetTextBeforePosition(uint32_t textPosition,
Packit 874993
    OUT wchar_t const** textString,
Packit 874993
    OUT uint32_t* textLength)
Packit 874993
  {
Packit 874993
    if (textPosition == 0 || textPosition > mTextLength) {
Packit 874993
      // Either there is no text before here (== 0), or this
Packit 874993
      // is an invalid position. The query is considered valid thouh.
Packit 874993
      *textString = NULL;
Packit 874993
      *textLength = 0;
Packit 874993
    }
Packit 874993
    else {
Packit 874993
      *textString = mText;
Packit 874993
      *textLength = textPosition;
Packit 874993
    }
Packit 874993
    return S_OK;
Packit 874993
  }
Packit 874993
Packit 874993
  IFACEMETHODIMP_(DWRITE_READING_DIRECTION)
Packit 874993
    GetParagraphReadingDirection() { return mReadingDirection; }
Packit 874993
Packit 874993
  IFACEMETHODIMP GetLocaleName(uint32_t textPosition,
Packit 874993
    uint32_t* textLength,
Packit 874993
    wchar_t const** localeName)
Packit 874993
  {
Packit 874993
    return S_OK;
Packit 874993
  }
Packit 874993
Packit 874993
  IFACEMETHODIMP
Packit 874993
    GetNumberSubstitution(uint32_t textPosition,
Packit 874993
    OUT uint32_t* textLength,
Packit 874993
    OUT IDWriteNumberSubstitution** numberSubstitution)
Packit 874993
  {
Packit 874993
    // We do not support number substitution.
Packit 874993
    *numberSubstitution = NULL;
Packit 874993
    *textLength = mTextLength - textPosition;
Packit 874993
Packit 874993
    return S_OK;
Packit 874993
  }
Packit 874993
Packit 874993
  // IDWriteTextAnalysisSink implementation
Packit 874993
Packit 874993
  IFACEMETHODIMP
Packit 874993
    SetScriptAnalysis(uint32_t textPosition,
Packit 874993
    uint32_t textLength,
Packit 874993
    DWRITE_SCRIPT_ANALYSIS const* scriptAnalysis)
Packit 874993
  {
Packit 874993
    SetCurrentRun(textPosition);
Packit 874993
    SplitCurrentRun(textPosition);
Packit 874993
    while (textLength > 0)
Packit 874993
    {
Packit 874993
      Run *run = FetchNextRun(&textLength);
Packit 874993
      run->mScript = *scriptAnalysis;
Packit 874993
    }
Packit 874993
Packit 874993
    return S_OK;
Packit 874993
  }
Packit 874993
Packit 874993
  IFACEMETHODIMP
Packit 874993
    SetLineBreakpoints(uint32_t textPosition,
Packit 874993
    uint32_t textLength,
Packit 874993
    const DWRITE_LINE_BREAKPOINT* lineBreakpoints) { return S_OK; }
Packit 874993
Packit 874993
  IFACEMETHODIMP SetBidiLevel(uint32_t textPosition,
Packit 874993
    uint32_t textLength,
Packit 874993
    uint8_t explicitLevel,
Packit 874993
    uint8_t resolvedLevel) { return S_OK; }
Packit 874993
Packit 874993
  IFACEMETHODIMP
Packit 874993
    SetNumberSubstitution(uint32_t textPosition,
Packit 874993
    uint32_t textLength,
Packit 874993
    IDWriteNumberSubstitution* numberSubstitution) { return S_OK; }
Packit 874993
Packit 874993
protected:
Packit 874993
  Run *FetchNextRun(IN OUT uint32_t* textLength)
Packit 874993
  {
Packit 874993
    // Used by the sink setters, this returns a reference to the next run.
Packit 874993
    // Position and length are adjusted to now point after the current run
Packit 874993
    // being returned.
Packit 874993
Packit 874993
    Run *origRun = mCurrentRun;
Packit 874993
    // Split the tail if needed (the length remaining is less than the
Packit 874993
    // current run's size).
Packit 874993
    if (*textLength < mCurrentRun->mTextLength)
Packit 874993
    {
Packit 874993
      SplitCurrentRun (mCurrentRun->mTextStart + *textLength);
Packit 874993
    }
Packit 874993
    else
Packit 874993
    {
Packit 874993
      // Just advance the current run.
Packit 874993
      mCurrentRun = mCurrentRun->nextRun;
Packit 874993
    }
Packit 874993
    *textLength -= origRun->mTextLength;
Packit 874993
Packit 874993
    // Return a reference to the run that was just current.
Packit 874993
    return origRun;
Packit 874993
  }
Packit 874993
Packit 874993
  void SetCurrentRun(uint32_t textPosition)
Packit 874993
  {
Packit 874993
    // Move the current run to the given position.
Packit 874993
    // Since the analyzers generally return results in a forward manner,
Packit 874993
    // this will usually just return early. If not, find the
Packit 874993
    // corresponding run for the text position.
Packit 874993
Packit 874993
    if (mCurrentRun && mCurrentRun->ContainsTextPosition (textPosition))
Packit 874993
    {
Packit 874993
      return;
Packit 874993
    }
Packit 874993
Packit 874993
    for (Run *run = &mRunHead; run; run = run->nextRun) {
Packit 874993
      if (run->ContainsTextPosition (textPosition))
Packit 874993
      {
Packit 874993
        mCurrentRun = run;
Packit 874993
        return;
Packit 874993
      }
Packit 874993
    }
Packit 874993
    //NS_NOTREACHED("We should always be able to find the text position in one \
Packit 874993
            //                of our runs");
Packit 874993
  }
Packit 874993
Packit 874993
  void SplitCurrentRun(uint32_t splitPosition)
Packit 874993
  {
Packit 874993
    if (!mCurrentRun)
Packit 874993
    {
Packit 874993
      //NS_ASSERTION(false, "SplitCurrentRun called without current run.");
Packit 874993
      // Shouldn't be calling this when no current run is set!
Packit 874993
      return;
Packit 874993
    }
Packit 874993
    // Split the current run.
Packit 874993
    if (splitPosition <= mCurrentRun->mTextStart)
Packit 874993
    {
Packit 874993
      // No need to split, already the start of a run
Packit 874993
      // or before it. Usually the first.
Packit 874993
      return;
Packit 874993
    }
Packit 874993
    Run *newRun = (Run*) malloc (sizeof (Run));
Packit 874993
Packit 874993
    *newRun = *mCurrentRun;
Packit 874993
Packit 874993
    // Insert the new run in our linked list.
Packit 874993
    newRun->nextRun = mCurrentRun->nextRun;
Packit 874993
    mCurrentRun->nextRun = newRun;
Packit 874993
Packit 874993
    // Adjust runs' text positions and lengths.
Packit 874993
    uint32_t splitPoint = splitPosition - mCurrentRun->mTextStart;
Packit 874993
    newRun->mTextStart += splitPoint;
Packit 874993
    newRun->mTextLength -= splitPoint;
Packit 874993
    mCurrentRun->mTextLength = splitPoint;
Packit 874993
    mCurrentRun = newRun;
Packit 874993
  }
Packit 874993
Packit 874993
protected:
Packit 874993
  // Input
Packit 874993
  // (weak references are fine here, since this class is a transient
Packit 874993
  //  stack-based helper that doesn't need to copy data)
Packit 874993
  uint32_t mTextLength;
Packit 874993
  const wchar_t* mText;
Packit 874993
  const wchar_t* mLocaleName;
Packit 874993
  DWRITE_READING_DIRECTION mReadingDirection;
Packit 874993
Packit 874993
  // Current processing state.
Packit 874993
  Run *mCurrentRun;
Packit 874993
Packit 874993
  // Output is a list of runs starting here
Packit 874993
  Run  mRunHead;
Packit 874993
};
Packit 874993
Packit 874993
static inline uint16_t hb_uint16_swap (const uint16_t v)
Packit 874993
{ return (v >> 8) | (v << 8); }
Packit 874993
static inline uint32_t hb_uint32_swap (const uint32_t v)
Packit 874993
{ return (hb_uint16_swap(v) << 16) | hb_uint16_swap(v >> 16); }
Packit 874993
Packit 874993
/*
Packit 874993
 * shaper
Packit 874993
 */
Packit 874993
Packit 874993
static hb_bool_t
Packit 874993
_hb_directwrite_shape_full(hb_shape_plan_t    *shape_plan,
Packit 874993
  hb_font_t          *font,
Packit 874993
  hb_buffer_t        *buffer,
Packit 874993
  const hb_feature_t *features,
Packit 874993
  unsigned int        num_features,
Packit 874993
  float               lineWidth)
Packit 874993
{
Packit 874993
  hb_face_t *face = font->face;
Packit 874993
  hb_directwrite_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
Packit 874993
  hb_directwrite_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
Packit 874993
  IDWriteFactory *dwriteFactory = face_data->dwriteFactory;
Packit 874993
  IDWriteFontFace *fontFace = face_data->fontFace;
Packit 874993
Packit 874993
  IDWriteTextAnalyzer* analyzer;
Packit 874993
  dwriteFactory->CreateTextAnalyzer(&analyzer);
Packit 874993
Packit 874993
  unsigned int scratch_size;
Packit 874993
  hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
Packit 874993
#define ALLOCATE_ARRAY(Type, name, len) \
Packit 874993
  Type *name = (Type *) scratch; \
Packit 874993
  { \
Packit 874993
    unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
Packit 874993
    assert (_consumed <= scratch_size); \
Packit 874993
    scratch += _consumed; \
Packit 874993
    scratch_size -= _consumed; \
Packit 874993
  }
Packit 874993
Packit 874993
#define utf16_index() var1.u32
Packit 874993
Packit 874993
  ALLOCATE_ARRAY(wchar_t, textString, buffer->len * 2);
Packit 874993
Packit 874993
  unsigned int chars_len = 0;
Packit 874993
  for (unsigned int i = 0; i < buffer->len; i++)
Packit 874993
  {
Packit 874993
    hb_codepoint_t c = buffer->info[i].codepoint;
Packit 874993
    buffer->info[i].utf16_index() = chars_len;
Packit 874993
    if (likely(c <= 0xFFFFu))
Packit 874993
      textString[chars_len++] = c;
Packit 874993
    else if (unlikely(c > 0x10FFFFu))
Packit 874993
      textString[chars_len++] = 0xFFFDu;
Packit 874993
    else {
Packit 874993
      textString[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
Packit 874993
      textString[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  ALLOCATE_ARRAY(WORD, log_clusters, chars_len);
Packit 874993
  // if (num_features)
Packit 874993
  {
Packit 874993
    /* Need log_clusters to assign features. */
Packit 874993
    chars_len = 0;
Packit 874993
    for (unsigned int i = 0; i < buffer->len; i++)
Packit 874993
    {
Packit 874993
      hb_codepoint_t c = buffer->info[i].codepoint;
Packit 874993
      unsigned int cluster = buffer->info[i].cluster;
Packit 874993
      log_clusters[chars_len++] = cluster;
Packit 874993
      if (hb_in_range(c, 0x10000u, 0x10FFFFu))
Packit 874993
        log_clusters[chars_len++] = cluster; /* Surrogates. */
Packit 874993
    }
Packit 874993
  }
Packit 874993
Packit 874993
  // TODO: Handle TEST_DISABLE_OPTIONAL_LIGATURES
Packit 874993
Packit 874993
  DWRITE_READING_DIRECTION readingDirection = buffer->props.direction ? 
Packit 874993
    DWRITE_READING_DIRECTION_RIGHT_TO_LEFT :
Packit 874993
    DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
Packit 874993
Packit 874993
  /*
Packit 874993
  * There's an internal 16-bit limit on some things inside the analyzer,
Packit 874993
  * but we never attempt to shape a word longer than 64K characters
Packit 874993
  * in a single gfxShapedWord, so we cannot exceed that limit.
Packit 874993
  */
Packit 874993
  uint32_t textLength = buffer->len;
Packit 874993
Packit 874993
  TextAnalysis analysis(textString, textLength, NULL, readingDirection);
Packit 874993
  TextAnalysis::Run *runHead;
Packit 874993
  HRESULT hr;
Packit 874993
  hr = analysis.GenerateResults(analyzer, &runHead);
Packit 874993
Packit 874993
#define FAIL(...) \
Packit 874993
  HB_STMT_START { \
Packit 874993
    DEBUG_MSG (DIRECTWRITE, NULL, __VA_ARGS__); \
Packit 874993
    return false; \
Packit 874993
  } HB_STMT_END;
Packit 874993
Packit 874993
  if (FAILED (hr))
Packit 874993
  {
Packit 874993
    FAIL ("Analyzer failed to generate results.");
Packit 874993
    return false;
Packit 874993
  }
Packit 874993
Packit 874993
  uint32_t maxGlyphCount = 3 * textLength / 2 + 16;
Packit 874993
  uint32_t glyphCount;
Packit 874993
  bool isRightToLeft = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
Packit 874993
Packit 874993
  const wchar_t localeName[20] = {0};
Packit 874993
  if (buffer->props.language != NULL)
Packit 874993
  {
Packit 874993
    mbstowcs ((wchar_t*) localeName,
Packit 874993
      hb_language_to_string (buffer->props.language), 20);
Packit 874993
  }
Packit 874993
Packit 874993
  DWRITE_TYPOGRAPHIC_FEATURES singleFeatures;
Packit 874993
  singleFeatures.featureCount = num_features;
Packit 874993
  if (num_features)
Packit 874993
  {
Packit 874993
    DWRITE_FONT_FEATURE* dwfeatureArray = (DWRITE_FONT_FEATURE*)
Packit 874993
      malloc (sizeof (DWRITE_FONT_FEATURE) * num_features);
Packit 874993
    for (unsigned int i = 0; i < num_features; ++i)
Packit 874993
    {
Packit 874993
      dwfeatureArray[i].nameTag = (DWRITE_FONT_FEATURE_TAG)
Packit 874993
        hb_uint32_swap (features[i].tag);
Packit 874993
      dwfeatureArray[i].parameter = features[i].value;
Packit 874993
    }
Packit 874993
    singleFeatures.features = dwfeatureArray;
Packit 874993
  }
Packit 874993
  const DWRITE_TYPOGRAPHIC_FEATURES* dwFeatures =
Packit 874993
    (const DWRITE_TYPOGRAPHIC_FEATURES*) &singleFeatures;
Packit 874993
  const uint32_t featureRangeLengths[] = { textLength };
Packit 874993
Packit 874993
  uint16_t* clusterMap = (uint16_t*) malloc (textLength * sizeof (uint16_t));
Packit 874993
  DWRITE_SHAPING_TEXT_PROPERTIES* textProperties = (DWRITE_SHAPING_TEXT_PROPERTIES*)
Packit 874993
    malloc (textLength * sizeof (DWRITE_SHAPING_TEXT_PROPERTIES));
Packit 874993
retry_getglyphs:
Packit 874993
  uint16_t* glyphIndices = (uint16_t*) malloc (maxGlyphCount * sizeof (uint16_t));
Packit 874993
  DWRITE_SHAPING_GLYPH_PROPERTIES* glyphProperties = (DWRITE_SHAPING_GLYPH_PROPERTIES*)
Packit 874993
    malloc (maxGlyphCount * sizeof (DWRITE_SHAPING_GLYPH_PROPERTIES));
Packit 874993
Packit 874993
  hr = analyzer->GetGlyphs (textString, textLength, fontFace, false,
Packit 874993
    isRightToLeft, &runHead->mScript, localeName, NULL, &dwFeatures,
Packit 874993
    featureRangeLengths, 1, maxGlyphCount, clusterMap, textProperties, glyphIndices,
Packit 874993
    glyphProperties, &glyphCount);
Packit 874993
Packit 874993
  if (unlikely (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)))
Packit 874993
  {
Packit 874993
    free (glyphIndices);
Packit 874993
    free (glyphProperties);
Packit 874993
Packit 874993
    maxGlyphCount *= 2;
Packit 874993
Packit 874993
    goto retry_getglyphs;
Packit 874993
  }
Packit 874993
  if (FAILED (hr))
Packit 874993
  {
Packit 874993
    FAIL ("Analyzer failed to get glyphs.");
Packit 874993
    return false;
Packit 874993
  }
Packit 874993
Packit 874993
  float* glyphAdvances = (float*) malloc (maxGlyphCount * sizeof (float));
Packit 874993
  DWRITE_GLYPH_OFFSET* glyphOffsets = (DWRITE_GLYPH_OFFSET*)
Packit 874993
    malloc(maxGlyphCount * sizeof (DWRITE_GLYPH_OFFSET));
Packit 874993
Packit 874993
  /* The -2 in the following is to compensate for possible
Packit 874993
   * alignment needed after the WORD array.  sizeof(WORD) == 2. */
Packit 874993
  unsigned int glyphs_size = (scratch_size * sizeof(int) - 2)
Packit 874993
         / (sizeof(WORD) +
Packit 874993
            sizeof(DWRITE_SHAPING_GLYPH_PROPERTIES) +
Packit 874993
            sizeof(int) +
Packit 874993
            sizeof(DWRITE_GLYPH_OFFSET) +
Packit 874993
            sizeof(uint32_t));
Packit 874993
  ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
Packit 874993
Packit 874993
#undef ALLOCATE_ARRAY
Packit 874993
Packit 874993
  int fontEmSize = font->face->get_upem();
Packit 874993
  if (fontEmSize < 0)
Packit 874993
    fontEmSize = -fontEmSize;
Packit 874993
Packit 874993
  if (fontEmSize < 0)
Packit 874993
    fontEmSize = -fontEmSize;
Packit 874993
  double x_mult = (double) font->x_scale / fontEmSize;
Packit 874993
  double y_mult = (double) font->y_scale / fontEmSize;
Packit 874993
Packit 874993
  hr = analyzer->GetGlyphPlacements (textString,
Packit 874993
    clusterMap, textProperties, textLength, glyphIndices,
Packit 874993
    glyphProperties, glyphCount, fontFace, fontEmSize,
Packit 874993
    false, isRightToLeft, &runHead->mScript, localeName,
Packit 874993
    &dwFeatures, featureRangeLengths, 1,
Packit 874993
    glyphAdvances, glyphOffsets);
Packit 874993
Packit 874993
  if (FAILED (hr))
Packit 874993
  {
Packit 874993
    FAIL ("Analyzer failed to get glyph placements.");
Packit 874993
    return false;
Packit 874993
  }
Packit 874993
Packit 874993
  IDWriteTextAnalyzer1* analyzer1;
Packit 874993
  analyzer->QueryInterface (&analyzer1);
Packit 874993
Packit 874993
  if (analyzer1 && lineWidth)
Packit 874993
  {
Packit 874993
Packit 874993
    DWRITE_JUSTIFICATION_OPPORTUNITY* justificationOpportunities =
Packit 874993
      (DWRITE_JUSTIFICATION_OPPORTUNITY*)
Packit 874993
      malloc (maxGlyphCount * sizeof (DWRITE_JUSTIFICATION_OPPORTUNITY));
Packit 874993
    hr = analyzer1->GetJustificationOpportunities (fontFace, fontEmSize,
Packit 874993
      runHead->mScript, textLength, glyphCount, textString, clusterMap,
Packit 874993
      glyphProperties, justificationOpportunities);
Packit 874993
Packit 874993
    if (FAILED (hr))
Packit 874993
    {
Packit 874993
      FAIL ("Analyzer failed to get justification opportunities.");
Packit 874993
      return false;
Packit 874993
    }
Packit 874993
Packit 874993
    float* justifiedGlyphAdvances =
Packit 874993
      (float*) malloc (maxGlyphCount * sizeof (float));
Packit 874993
    DWRITE_GLYPH_OFFSET* justifiedGlyphOffsets = (DWRITE_GLYPH_OFFSET*)
Packit 874993
      malloc (glyphCount * sizeof (DWRITE_GLYPH_OFFSET));
Packit 874993
    hr = analyzer1->JustifyGlyphAdvances (lineWidth, glyphCount, justificationOpportunities,
Packit 874993
      glyphAdvances, glyphOffsets, justifiedGlyphAdvances, justifiedGlyphOffsets);
Packit 874993
Packit 874993
    if (FAILED (hr))
Packit 874993
    {
Packit 874993
      FAIL("Analyzer failed to get justified glyph advances.");
Packit 874993
      return false;
Packit 874993
    }
Packit 874993
Packit 874993
    DWRITE_SCRIPT_PROPERTIES scriptProperties;
Packit 874993
    hr = analyzer1->GetScriptProperties (runHead->mScript, &scriptProperties);
Packit 874993
    if (FAILED (hr))
Packit 874993
    {
Packit 874993
      FAIL("Analyzer failed to get script properties.");
Packit 874993
      return false;
Packit 874993
    }
Packit 874993
    uint32_t justificationCharacter = scriptProperties.justificationCharacter;
Packit 874993
Packit 874993
    // if a script justificationCharacter is not space, it can have GetJustifiedGlyphs
Packit 874993
    if (justificationCharacter != 32)
Packit 874993
    {
Packit 874993
      uint16_t* modifiedClusterMap = (uint16_t*) malloc (textLength * sizeof (uint16_t));
Packit 874993
    retry_getjustifiedglyphs:
Packit 874993
      uint16_t* modifiedGlyphIndices = (uint16_t*) malloc (maxGlyphCount * sizeof (uint16_t));
Packit 874993
      float* modifiedGlyphAdvances = (float*) malloc (maxGlyphCount * sizeof (float));
Packit 874993
      DWRITE_GLYPH_OFFSET* modifiedGlyphOffsets = (DWRITE_GLYPH_OFFSET*)
Packit 874993
        malloc (maxGlyphCount * sizeof (DWRITE_GLYPH_OFFSET));
Packit 874993
      uint32_t actualGlyphsCount;
Packit 874993
      hr = analyzer1->GetJustifiedGlyphs (fontFace, fontEmSize, runHead->mScript,
Packit 874993
        textLength, glyphCount, maxGlyphCount, clusterMap, glyphIndices,
Packit 874993
        glyphAdvances, justifiedGlyphAdvances, justifiedGlyphOffsets,
Packit 874993
        glyphProperties, &actualGlyphsCount, modifiedClusterMap, modifiedGlyphIndices,
Packit 874993
        modifiedGlyphAdvances, modifiedGlyphOffsets);
Packit 874993
Packit 874993
      if (hr == HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER))
Packit 874993
      {
Packit 874993
        maxGlyphCount = actualGlyphsCount;
Packit 874993
        free (modifiedGlyphIndices);
Packit 874993
        free (modifiedGlyphAdvances);
Packit 874993
        free (modifiedGlyphOffsets);
Packit 874993
Packit 874993
        maxGlyphCount = actualGlyphsCount;
Packit 874993
Packit 874993
        goto retry_getjustifiedglyphs;
Packit 874993
      }
Packit 874993
      if (FAILED (hr))
Packit 874993
      {
Packit 874993
        FAIL ("Analyzer failed to get justified glyphs.");
Packit 874993
        return false;
Packit 874993
      }
Packit 874993
Packit 874993
      free (clusterMap);
Packit 874993
      free (glyphIndices);
Packit 874993
      free (glyphAdvances);
Packit 874993
      free (glyphOffsets);
Packit 874993
Packit 874993
      glyphCount = actualGlyphsCount;
Packit 874993
      clusterMap = modifiedClusterMap;
Packit 874993
      glyphIndices = modifiedGlyphIndices;
Packit 874993
      glyphAdvances = modifiedGlyphAdvances;
Packit 874993
      glyphOffsets = modifiedGlyphOffsets;
Packit 874993
Packit 874993
      free (justifiedGlyphAdvances);
Packit 874993
      free (justifiedGlyphOffsets);
Packit 874993
    }
Packit 874993
    else
Packit 874993
    {
Packit 874993
      free (glyphAdvances);
Packit 874993
      free (glyphOffsets);
Packit 874993
Packit 874993
      glyphAdvances = justifiedGlyphAdvances;
Packit 874993
      glyphOffsets = justifiedGlyphOffsets;
Packit 874993
    }
Packit 874993
Packit 874993
    free (justificationOpportunities);
Packit 874993
Packit 874993
  }
Packit 874993
Packit 874993
  /* Ok, we've got everything we need, now compose output buffer,
Packit 874993
   * very, *very*, carefully! */
Packit 874993
Packit 874993
  /* Calculate visual-clusters.  That's what we ship. */
Packit 874993
  for (unsigned int i = 0; i < glyphCount; i++)
Packit 874993
    vis_clusters[i] = -1;
Packit 874993
  for (unsigned int i = 0; i < buffer->len; i++)
Packit 874993
  {
Packit 874993
    uint32_t *p =
Packit 874993
      &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
Packit 874993
    *p = MIN (*p, buffer->info[i].cluster);
Packit 874993
  }
Packit 874993
  for (unsigned int i = 1; i < glyphCount; i++)
Packit 874993
    if (vis_clusters[i] == -1)
Packit 874993
      vis_clusters[i] = vis_clusters[i - 1];
Packit 874993
Packit 874993
#undef utf16_index
Packit 874993
Packit 874993
  if (unlikely (!buffer->ensure (glyphCount)))
Packit 874993
    FAIL ("Buffer in error");
Packit 874993
Packit 874993
#undef FAIL
Packit 874993
Packit 874993
  /* Set glyph infos */
Packit 874993
  buffer->len = 0;
Packit 874993
  for (unsigned int i = 0; i < glyphCount; i++)
Packit 874993
  {
Packit 874993
    hb_glyph_info_t *info = &buffer->info[buffer->len++];
Packit 874993
Packit 874993
    info->codepoint = glyphIndices[i];
Packit 874993
    info->cluster = vis_clusters[i];
Packit 874993
Packit 874993
    /* The rest is crap.  Let's store position info there for now. */
Packit 874993
    info->mask = glyphAdvances[i];
Packit 874993
    info->var1.i32 = glyphOffsets[i].advanceOffset;
Packit 874993
    info->var2.i32 = glyphOffsets[i].ascenderOffset;
Packit 874993
  }
Packit 874993
Packit 874993
  /* Set glyph positions */
Packit 874993
  buffer->clear_positions ();
Packit 874993
  for (unsigned int i = 0; i < glyphCount; i++)
Packit 874993
  {
Packit 874993
    hb_glyph_info_t *info = &buffer->info[i];
Packit 874993
    hb_glyph_position_t *pos = &buffer->pos[i];
Packit 874993
Packit 874993
    /* TODO vertical */
Packit 874993
    pos->x_advance = x_mult * (int32_t) info->mask;
Packit 874993
    pos->x_offset =
Packit 874993
      x_mult * (isRightToLeft ? -info->var1.i32 : info->var1.i32);
Packit 874993
    pos->y_offset = y_mult * info->var2.i32;
Packit 874993
  }
Packit 874993
Packit 874993
  if (isRightToLeft)
Packit 874993
    hb_buffer_reverse (buffer);
Packit 874993
Packit 874993
  free (clusterMap);
Packit 874993
  free (glyphIndices);
Packit 874993
  free (textProperties);
Packit 874993
  free (glyphProperties);
Packit 874993
  free (glyphAdvances);
Packit 874993
  free (glyphOffsets);
Packit 874993
Packit 874993
  if (num_features)
Packit 874993
    free (singleFeatures.features);
Packit 874993
Packit 874993
  /* Wow, done! */
Packit 874993
  return true;
Packit 874993
}
Packit 874993
Packit 874993
hb_bool_t
Packit 874993
_hb_directwrite_shape(hb_shape_plan_t    *shape_plan,
Packit 874993
  hb_font_t          *font,
Packit 874993
  hb_buffer_t        *buffer,
Packit 874993
  const hb_feature_t *features,
Packit 874993
  unsigned int        num_features)
Packit 874993
{
Packit 874993
  return _hb_directwrite_shape_full(shape_plan, font, buffer,
Packit 874993
    features, num_features, 0);
Packit 874993
}
Packit 874993
Packit 874993
/*
Packit 874993
 * Public [experimental] API
Packit 874993
 */
Packit 874993
Packit 874993
hb_bool_t
Packit 874993
hb_directwrite_shape_experimental_width(hb_font_t          *font,
Packit 874993
  hb_buffer_t        *buffer,
Packit 874993
  const hb_feature_t *features,
Packit 874993
  unsigned int        num_features,
Packit 874993
  float               width)
Packit 874993
{
Packit 874993
  static char *shapers = "directwrite";
Packit 874993
  hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face,
Packit 874993
    &buffer->props, features, num_features, &shapers);
Packit 874993
  hb_bool_t res = _hb_directwrite_shape_full (shape_plan, font, buffer,
Packit 874993
    features, num_features, width);
Packit 874993
Packit 874993
  if (res)
Packit 874993
    buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
Packit 874993
Packit 874993
  return res;
Packit 874993
}