2011-08-04 01:55:04 +02:00
|
|
|
/*
|
2013-08-12 06:33:28 +02:00
|
|
|
* Copyright © 2011,2012,2013 Google, Inc.
|
2011-08-04 01:55:04 +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.
|
|
|
|
*
|
|
|
|
* Google Author(s): Behdad Esfahbod
|
|
|
|
*/
|
|
|
|
|
2017-11-03 21:57:30 +01:00
|
|
|
#include "hb-private.hh"
|
|
|
|
#include "hb-debug.hh"
|
2012-07-26 23:34:25 +02:00
|
|
|
#define HB_SHAPER uniscribe
|
|
|
|
#include "hb-shaper-impl-private.hh"
|
2011-08-04 01:55:04 +02:00
|
|
|
|
2011-08-06 02:34:50 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <usp10.h>
|
2013-07-21 22:06:49 +02:00
|
|
|
#include <rpc.h>
|
2011-08-06 02:34:50 +02:00
|
|
|
|
2011-08-04 01:55:04 +02:00
|
|
|
#include "hb-uniscribe.h"
|
|
|
|
|
2013-07-21 22:06:49 +02:00
|
|
|
#include "hb-open-file-private.hh"
|
2011-08-17 14:19:59 +02:00
|
|
|
#include "hb-ot-name-table.hh"
|
2011-08-04 01:55:04 +02:00
|
|
|
#include "hb-ot-tag.h"
|
|
|
|
|
|
|
|
|
2014-10-15 06:24:59 +02:00
|
|
|
static inline uint16_t hb_uint16_swap (const uint16_t v)
|
|
|
|
{ return (v >> 8) | (v << 8); }
|
|
|
|
static inline uint32_t hb_uint32_swap (const uint32_t v)
|
|
|
|
{ return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
|
|
|
|
|
|
|
|
|
2013-06-03 11:49:37 +02:00
|
|
|
typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
|
2013-05-28 19:10:51 +02:00
|
|
|
const WCHAR *pwcInChars,
|
|
|
|
int cInChars,
|
|
|
|
int cMaxItems,
|
|
|
|
const SCRIPT_CONTROL *psControl,
|
|
|
|
const SCRIPT_STATE *psState,
|
|
|
|
SCRIPT_ITEM *pItems,
|
|
|
|
OPENTYPE_TAG *pScriptTags,
|
|
|
|
int *pcItems
|
|
|
|
);
|
|
|
|
|
2013-06-03 11:49:37 +02:00
|
|
|
typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
|
2013-05-28 19:10:51 +02:00
|
|
|
HDC hdc,
|
|
|
|
SCRIPT_CACHE *psc,
|
|
|
|
SCRIPT_ANALYSIS *psa,
|
|
|
|
OPENTYPE_TAG tagScript,
|
|
|
|
OPENTYPE_TAG tagLangSys,
|
|
|
|
int *rcRangeChars,
|
|
|
|
TEXTRANGE_PROPERTIES **rpRangeProperties,
|
|
|
|
int cRanges,
|
|
|
|
const WCHAR *pwcChars,
|
|
|
|
int cChars,
|
|
|
|
int cMaxGlyphs,
|
|
|
|
WORD *pwLogClust,
|
|
|
|
SCRIPT_CHARPROP *pCharProps,
|
|
|
|
WORD *pwOutGlyphs,
|
|
|
|
SCRIPT_GLYPHPROP *pOutGlyphProps,
|
|
|
|
int *pcGlyphs
|
2011-08-04 01:55:04 +02:00
|
|
|
);
|
2013-05-28 19:10:51 +02:00
|
|
|
|
2013-06-03 11:49:37 +02:00
|
|
|
typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
|
2013-05-28 19:10:51 +02:00
|
|
|
HDC hdc,
|
|
|
|
SCRIPT_CACHE *psc,
|
|
|
|
SCRIPT_ANALYSIS *psa,
|
|
|
|
OPENTYPE_TAG tagScript,
|
|
|
|
OPENTYPE_TAG tagLangSys,
|
|
|
|
int *rcRangeChars,
|
|
|
|
TEXTRANGE_PROPERTIES **rpRangeProperties,
|
|
|
|
int cRanges,
|
|
|
|
const WCHAR *pwcChars,
|
|
|
|
WORD *pwLogClust,
|
|
|
|
SCRIPT_CHARPROP *pCharProps,
|
|
|
|
int cChars,
|
|
|
|
const WORD *pwGlyphs,
|
|
|
|
const SCRIPT_GLYPHPROP *pGlyphProps,
|
|
|
|
int cGlyphs,
|
|
|
|
int *piAdvance,
|
|
|
|
GOFFSET *pGoffset,
|
|
|
|
ABC *pABC
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/* Fallback implementations. */
|
|
|
|
|
|
|
|
static HRESULT WINAPI
|
|
|
|
hb_ScriptItemizeOpenType(
|
|
|
|
const WCHAR *pwcInChars,
|
|
|
|
int cInChars,
|
|
|
|
int cMaxItems,
|
|
|
|
const SCRIPT_CONTROL *psControl,
|
|
|
|
const SCRIPT_STATE *psState,
|
|
|
|
SCRIPT_ITEM *pItems,
|
|
|
|
OPENTYPE_TAG *pScriptTags,
|
|
|
|
int *pcItems
|
|
|
|
)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return ScriptItemize (pwcInChars,
|
|
|
|
cInChars,
|
|
|
|
cMaxItems,
|
|
|
|
psControl,
|
|
|
|
psState,
|
|
|
|
pItems,
|
|
|
|
pcItems);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI
|
|
|
|
hb_ScriptShapeOpenType(
|
|
|
|
HDC hdc,
|
|
|
|
SCRIPT_CACHE *psc,
|
|
|
|
SCRIPT_ANALYSIS *psa,
|
|
|
|
OPENTYPE_TAG tagScript,
|
|
|
|
OPENTYPE_TAG tagLangSys,
|
|
|
|
int *rcRangeChars,
|
|
|
|
TEXTRANGE_PROPERTIES **rpRangeProperties,
|
|
|
|
int cRanges,
|
|
|
|
const WCHAR *pwcChars,
|
|
|
|
int cChars,
|
|
|
|
int cMaxGlyphs,
|
|
|
|
WORD *pwLogClust,
|
|
|
|
SCRIPT_CHARPROP *pCharProps,
|
|
|
|
WORD *pwOutGlyphs,
|
|
|
|
SCRIPT_GLYPHPROP *pOutGlyphProps,
|
|
|
|
int *pcGlyphs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
|
|
|
|
return ScriptShape (hdc,
|
|
|
|
psc,
|
|
|
|
pwcChars,
|
|
|
|
cChars,
|
|
|
|
cMaxGlyphs,
|
|
|
|
psa,
|
|
|
|
pwOutGlyphs,
|
|
|
|
pwLogClust,
|
|
|
|
psva,
|
|
|
|
pcGlyphs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI
|
|
|
|
hb_ScriptPlaceOpenType(
|
|
|
|
HDC hdc,
|
|
|
|
SCRIPT_CACHE *psc,
|
|
|
|
SCRIPT_ANALYSIS *psa,
|
|
|
|
OPENTYPE_TAG tagScript,
|
|
|
|
OPENTYPE_TAG tagLangSys,
|
|
|
|
int *rcRangeChars,
|
|
|
|
TEXTRANGE_PROPERTIES **rpRangeProperties,
|
|
|
|
int cRanges,
|
|
|
|
const WCHAR *pwcChars,
|
|
|
|
WORD *pwLogClust,
|
|
|
|
SCRIPT_CHARPROP *pCharProps,
|
|
|
|
int cChars,
|
|
|
|
const WORD *pwGlyphs,
|
|
|
|
const SCRIPT_GLYPHPROP *pGlyphProps,
|
|
|
|
int cGlyphs,
|
|
|
|
int *piAdvance,
|
|
|
|
GOFFSET *pGoffset,
|
|
|
|
ABC *pABC
|
|
|
|
)
|
|
|
|
{
|
|
|
|
SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
|
|
|
|
return ScriptPlace (hdc,
|
|
|
|
psc,
|
|
|
|
pwGlyphs,
|
|
|
|
cGlyphs,
|
|
|
|
psva,
|
|
|
|
psa,
|
|
|
|
piAdvance,
|
|
|
|
pGoffset,
|
|
|
|
pABC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct hb_uniscribe_shaper_funcs_t {
|
|
|
|
SIOT ScriptItemizeOpenType;
|
|
|
|
SSOT ScriptShapeOpenType;
|
|
|
|
SPOT ScriptPlaceOpenType;
|
|
|
|
|
|
|
|
inline void init (void)
|
|
|
|
{
|
2013-06-03 11:49:37 +02:00
|
|
|
HMODULE hinstLib;
|
2017-10-15 12:11:08 +02:00
|
|
|
this->ScriptItemizeOpenType = nullptr;
|
|
|
|
this->ScriptShapeOpenType = nullptr;
|
|
|
|
this->ScriptPlaceOpenType = nullptr;
|
2013-05-28 19:10:51 +02:00
|
|
|
|
2013-08-28 18:35:13 +02:00
|
|
|
hinstLib = GetModuleHandle (TEXT ("usp10.dll"));
|
2013-05-28 19:10:51 +02:00
|
|
|
if (hinstLib)
|
|
|
|
{
|
|
|
|
this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType");
|
|
|
|
this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType");
|
|
|
|
this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType");
|
|
|
|
}
|
|
|
|
if (!this->ScriptItemizeOpenType ||
|
|
|
|
!this->ScriptShapeOpenType ||
|
|
|
|
!this->ScriptPlaceOpenType)
|
|
|
|
{
|
2017-10-15 12:11:08 +02:00
|
|
|
DEBUG_MSG (UNISCRIBE, nullptr, "OpenType versions of functions not found; falling back.");
|
2013-05-28 19:10:51 +02:00
|
|
|
this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType;
|
|
|
|
this->ScriptShapeOpenType = hb_ScriptShapeOpenType;
|
|
|
|
this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static hb_uniscribe_shaper_funcs_t *uniscribe_funcs;
|
|
|
|
|
2018-03-30 02:30:28 +02:00
|
|
|
#ifdef HB_USE_ATEXIT
|
2013-05-28 19:10:51 +02:00
|
|
|
static inline void
|
|
|
|
free_uniscribe_funcs (void)
|
|
|
|
{
|
2018-03-30 02:30:28 +02:00
|
|
|
retry:
|
|
|
|
hb_uniscribe_shaper_funcs_t *local_uniscribe_funcs =
|
|
|
|
(hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs);
|
|
|
|
if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, local_uniscribe_funcs, nullptr))
|
|
|
|
goto retry;
|
|
|
|
|
2013-05-28 19:10:51 +02:00
|
|
|
free (uniscribe_funcs);
|
|
|
|
}
|
2018-03-30 02:30:28 +02:00
|
|
|
#endif
|
2013-05-28 19:10:51 +02:00
|
|
|
|
|
|
|
static hb_uniscribe_shaper_funcs_t *
|
|
|
|
hb_uniscribe_shaper_get_funcs (void)
|
|
|
|
{
|
|
|
|
retry:
|
|
|
|
hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs);
|
|
|
|
|
|
|
|
if (unlikely (!funcs))
|
|
|
|
{
|
|
|
|
funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
|
|
|
|
if (unlikely (!funcs))
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2013-05-28 19:10:51 +02:00
|
|
|
|
|
|
|
funcs->init ();
|
|
|
|
|
2017-10-15 12:11:08 +02:00
|
|
|
if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, nullptr, funcs)) {
|
2013-05-28 19:10:51 +02:00
|
|
|
free (funcs);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
2014-08-06 19:34:49 +02:00
|
|
|
#ifdef HB_USE_ATEXIT
|
2013-05-28 19:10:51 +02:00
|
|
|
atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return funcs;
|
|
|
|
}
|
2011-08-04 01:55:04 +02:00
|
|
|
|
2011-08-09 00:13:24 +02:00
|
|
|
|
2013-08-07 23:26:13 +02:00
|
|
|
struct active_feature_t {
|
|
|
|
OPENTYPE_FEATURE_RECORD rec;
|
|
|
|
unsigned int order;
|
|
|
|
|
2017-10-31 18:17:43 +01:00
|
|
|
static int cmp (const void *pa, const void *pb) {
|
|
|
|
const active_feature_t *a = (const active_feature_t *) pa;
|
|
|
|
const active_feature_t *b = (const active_feature_t *) pb;
|
2013-08-07 23:26:13 +02:00
|
|
|
return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 :
|
|
|
|
a->order < b->order ? -1 : a->order > b->order ? 1 :
|
|
|
|
a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 :
|
|
|
|
0;
|
|
|
|
}
|
|
|
|
bool operator== (const active_feature_t *f) {
|
|
|
|
return cmp (this, f) == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct feature_event_t {
|
|
|
|
unsigned int index;
|
|
|
|
bool start;
|
|
|
|
active_feature_t feature;
|
|
|
|
|
2017-10-31 18:17:43 +01:00
|
|
|
static int cmp (const void *pa, const void *pb) {
|
|
|
|
const feature_event_t *a = (const feature_event_t *) pa;
|
|
|
|
const feature_event_t *b = (const feature_event_t *) pb;
|
2013-08-07 23:26:13 +02:00
|
|
|
return a->index < b->index ? -1 : a->index > b->index ? 1 :
|
|
|
|
a->start < b->start ? -1 : a->start > b->start ? 1 :
|
|
|
|
active_feature_t::cmp (&a->feature, &b->feature);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct range_record_t {
|
|
|
|
TEXTRANGE_PROPERTIES props;
|
|
|
|
unsigned int index_first; /* == start */
|
|
|
|
unsigned int index_last; /* == end - 1 */
|
|
|
|
};
|
|
|
|
|
2017-02-04 01:43:25 +01:00
|
|
|
HB_SHAPER_DATA_ENSURE_DEFINE(uniscribe, face)
|
|
|
|
HB_SHAPER_DATA_ENSURE_DEFINE(uniscribe, font)
|
2012-07-27 09:06:30 +02:00
|
|
|
|
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
/*
|
|
|
|
* shaper face data
|
|
|
|
*/
|
2011-08-24 02:09:04 +02:00
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
struct hb_uniscribe_shaper_face_data_t {
|
2011-08-09 00:13:24 +02:00
|
|
|
HANDLE fh;
|
2013-05-28 19:10:51 +02:00
|
|
|
hb_uniscribe_shaper_funcs_t *funcs;
|
2013-07-21 22:06:49 +02:00
|
|
|
wchar_t face_name[LF_FACESIZE];
|
2012-07-26 23:34:25 +02:00
|
|
|
};
|
2011-08-09 00:13:24 +02:00
|
|
|
|
2013-07-21 22:22:10 +02:00
|
|
|
/* face_name should point to a wchar_t[LF_FACESIZE] object. */
|
|
|
|
static void
|
|
|
|
_hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen)
|
2013-07-21 22:06:49 +02:00
|
|
|
{
|
|
|
|
/* We'll create a private name for the font from a UUID using a simple,
|
|
|
|
* somewhat base64-like encoding scheme */
|
|
|
|
const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
|
|
|
|
UUID id;
|
|
|
|
UuidCreate ((UUID*) &id);
|
2017-10-15 12:02:00 +02:00
|
|
|
static_assert ((2 + 3 * (16/2) < LF_FACESIZE), "");
|
2013-07-21 22:06:49 +02:00
|
|
|
unsigned int name_str_len = 0;
|
2013-07-21 22:22:10 +02:00
|
|
|
face_name[name_str_len++] = 'F';
|
|
|
|
face_name[name_str_len++] = '_';
|
2013-07-21 22:06:49 +02:00
|
|
|
unsigned char *p = (unsigned char *) &id;
|
|
|
|
for (unsigned int i = 0; i < 16; i += 2)
|
|
|
|
{
|
|
|
|
/* Spread the 16 bits from two bytes of the UUID across three chars of face_name,
|
|
|
|
* using the bits in groups of 5,5,6 to select chars from enc.
|
|
|
|
* This will generate 24 characters; with the 'F_' prefix we already provided,
|
|
|
|
* the name will be 26 chars (plus the NUL terminator), so will always fit within
|
|
|
|
* face_name (LF_FACESIZE = 32). */
|
2013-07-21 22:22:10 +02:00
|
|
|
face_name[name_str_len++] = enc[p[i] >> 3];
|
|
|
|
face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f];
|
|
|
|
face_name[name_str_len++] = enc[p[i + 1] & 0x3f];
|
2013-07-21 22:06:49 +02:00
|
|
|
}
|
2013-07-21 22:22:10 +02:00
|
|
|
face_name[name_str_len] = 0;
|
|
|
|
if (plen)
|
|
|
|
*plen = name_str_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroys blob. */
|
|
|
|
static hb_blob_t *
|
|
|
|
_hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
|
|
|
|
{
|
2013-07-21 23:05:02 +02:00
|
|
|
/* Create a copy of the font data, with the 'name' table replaced by a
|
|
|
|
* table that names the font with our private F_* name created above.
|
|
|
|
* For simplicity, we just append a new 'name' table and update the
|
|
|
|
* sfnt directory; the original table is left in place, but unused.
|
|
|
|
*
|
|
|
|
* The new table will contain just 5 name IDs: family, style, unique,
|
|
|
|
* full, PS. All of them point to the same name data with our unique name.
|
|
|
|
*/
|
|
|
|
|
2018-01-09 15:48:51 +01:00
|
|
|
blob = OT::Sanitizer<OT::OpenTypeFontFile>().sanitize (blob);
|
2013-07-22 16:57:05 +02:00
|
|
|
|
2013-07-21 22:22:10 +02:00
|
|
|
unsigned int length, new_length, name_str_len;
|
|
|
|
const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
|
|
|
|
|
|
|
|
_hb_generate_unique_face_name (new_name, &name_str_len);
|
2013-07-21 22:06:49 +02:00
|
|
|
|
|
|
|
static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
|
|
|
|
|
2013-07-21 23:05:02 +02:00
|
|
|
unsigned int name_table_length = OT::name::min_size +
|
|
|
|
ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size +
|
2013-07-21 22:06:49 +02:00
|
|
|
name_str_len * 2; /* for name data in UTF16BE form */
|
2018-03-28 22:38:51 +02:00
|
|
|
unsigned int padded_name_table_length = ((name_table_length + 3) & ~3);
|
2013-07-21 22:06:49 +02:00
|
|
|
unsigned int name_table_offset = (length + 3) & ~3;
|
|
|
|
|
2018-03-28 22:38:51 +02:00
|
|
|
new_length = name_table_offset + padded_name_table_length;
|
2013-07-21 23:05:02 +02:00
|
|
|
void *new_sfnt_data = calloc (1, new_length);
|
2013-07-21 22:06:49 +02:00
|
|
|
if (!new_sfnt_data)
|
2013-07-21 23:05:02 +02:00
|
|
|
{
|
|
|
|
hb_blob_destroy (blob);
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2013-07-21 23:05:02 +02:00
|
|
|
}
|
2013-07-21 22:06:49 +02:00
|
|
|
|
|
|
|
memcpy(new_sfnt_data, orig_sfnt_data, length);
|
|
|
|
|
2013-07-21 23:05:02 +02:00
|
|
|
OT::name &name = OT::StructAtOffset<OT::name> (new_sfnt_data, name_table_offset);
|
|
|
|
name.format.set (0);
|
|
|
|
name.count.set (ARRAY_LENGTH (name_IDs));
|
|
|
|
name.stringOffset.set (name.get_size ());
|
|
|
|
for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++)
|
|
|
|
{
|
|
|
|
OT::NameRecord &record = name.nameRecord[i];
|
|
|
|
record.platformID.set (3);
|
|
|
|
record.encodingID.set (1);
|
2014-07-11 20:54:42 +02:00
|
|
|
record.languageID.set (0x0409u); /* English */
|
2013-07-21 23:05:02 +02:00
|
|
|
record.nameID.set (name_IDs[i]);
|
|
|
|
record.length.set (name_str_len * 2);
|
|
|
|
record.offset.set (0);
|
2013-07-21 22:06:49 +02:00
|
|
|
}
|
|
|
|
|
2013-07-21 23:05:02 +02:00
|
|
|
/* Copy string data from new_name, converting wchar_t to UTF16BE. */
|
|
|
|
unsigned char *p = &OT::StructAfter<unsigned char> (name);
|
|
|
|
for (unsigned int i = 0; i < name_str_len; i++)
|
|
|
|
{
|
2013-07-21 22:06:49 +02:00
|
|
|
*p++ = new_name[i] >> 8;
|
|
|
|
*p++ = new_name[i] & 0xff;
|
|
|
|
}
|
|
|
|
|
2013-07-21 23:05:02 +02:00
|
|
|
/* Adjust name table entry to point to new name table */
|
2013-07-22 16:52:12 +02:00
|
|
|
const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data);
|
|
|
|
unsigned int face_count = file.get_face_count ();
|
|
|
|
for (unsigned int face_index = 0; face_index < face_count; face_index++)
|
2013-07-21 22:06:49 +02:00
|
|
|
{
|
2013-07-22 16:57:05 +02:00
|
|
|
/* Note: doing multiple edits (ie. TTC) can be unsafe. There may be
|
|
|
|
* toe-stepping. But we don't really care. */
|
2013-07-22 16:52:12 +02:00
|
|
|
const OT::OpenTypeFontFace &face = file.get_face (face_index);
|
|
|
|
unsigned int index;
|
|
|
|
if (face.find_table_index (HB_OT_TAG_name, &index))
|
|
|
|
{
|
|
|
|
OT::TableRecord &record = const_cast<OT::TableRecord &> (face.get_table (index));
|
2018-03-28 22:38:51 +02:00
|
|
|
record.checkSum.set_for_data (&name, padded_name_table_length);
|
2013-07-22 16:52:12 +02:00
|
|
|
record.offset.set (name_table_offset);
|
|
|
|
record.length.set (name_table_length);
|
|
|
|
}
|
|
|
|
else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */
|
|
|
|
{
|
|
|
|
free (new_sfnt_data);
|
|
|
|
hb_blob_destroy (blob);
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2013-07-22 16:52:12 +02:00
|
|
|
}
|
2013-07-21 22:06:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The checkSumAdjustment field in the 'head' table is now wrong,
|
2013-07-21 23:05:02 +02:00
|
|
|
* but that doesn't actually seem to cause any problems so we don't
|
|
|
|
* bother. */
|
2013-07-21 22:06:49 +02:00
|
|
|
|
2013-07-21 22:16:35 +02:00
|
|
|
hb_blob_destroy (blob);
|
2013-07-21 23:05:02 +02:00
|
|
|
return hb_blob_create ((const char *) new_sfnt_data, new_length,
|
2017-10-15 12:11:08 +02:00
|
|
|
HB_MEMORY_MODE_WRITABLE, nullptr, free);
|
2013-07-21 22:06:49 +02:00
|
|
|
}
|
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
hb_uniscribe_shaper_face_data_t *
|
|
|
|
_hb_uniscribe_shaper_face_data_create (hb_face_t *face)
|
2011-08-09 00:13:24 +02:00
|
|
|
{
|
2012-07-26 23:34:25 +02:00
|
|
|
hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t));
|
2011-08-09 00:13:24 +02:00
|
|
|
if (unlikely (!data))
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2011-08-24 03:22:49 +02:00
|
|
|
|
2013-05-28 19:10:51 +02:00
|
|
|
data->funcs = hb_uniscribe_shaper_get_funcs ();
|
|
|
|
if (unlikely (!data->funcs))
|
|
|
|
{
|
|
|
|
free (data);
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2013-05-28 19:10:51 +02:00
|
|
|
}
|
|
|
|
|
2011-08-09 00:13:24 +02:00
|
|
|
hb_blob_t *blob = hb_face_reference_blob (face);
|
2013-07-21 22:16:35 +02:00
|
|
|
if (unlikely (!hb_blob_get_length (blob)))
|
2011-08-09 00:13:24 +02:00
|
|
|
DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
|
|
|
|
|
2013-07-21 22:16:35 +02:00
|
|
|
blob = _hb_rename_font (blob, data->face_name);
|
2013-07-21 23:05:02 +02:00
|
|
|
if (unlikely (!blob))
|
|
|
|
{
|
|
|
|
free (data);
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2013-07-21 23:05:02 +02:00
|
|
|
}
|
2013-07-21 22:06:49 +02:00
|
|
|
|
|
|
|
DWORD num_fonts_installed;
|
2017-10-15 12:11:08 +02:00
|
|
|
data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, nullptr),
|
2013-07-21 22:16:35 +02:00
|
|
|
hb_blob_get_length (blob),
|
|
|
|
0, &num_fonts_installed);
|
2013-07-21 23:05:02 +02:00
|
|
|
if (unlikely (!data->fh))
|
|
|
|
{
|
2011-08-09 00:13:24 +02:00
|
|
|
DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
|
2012-07-26 23:34:25 +02:00
|
|
|
free (data);
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2011-08-09 00:13:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
void
|
|
|
|
_hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data)
|
|
|
|
{
|
2012-07-30 23:54:38 +02:00
|
|
|
RemoveFontMemResourceEx (data->fh);
|
2012-07-26 23:34:25 +02:00
|
|
|
free (data);
|
|
|
|
}
|
2011-08-09 00:13:24 +02:00
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* shaper font data
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct hb_uniscribe_shaper_font_data_t {
|
2011-08-09 00:13:24 +02:00
|
|
|
HDC hdc;
|
2011-08-10 22:08:36 +02:00
|
|
|
LOGFONTW log_font;
|
2011-08-09 00:13:24 +02:00
|
|
|
HFONT hfont;
|
|
|
|
SCRIPT_CACHE script_cache;
|
2015-08-19 14:20:31 +02:00
|
|
|
double x_mult, y_mult; /* From LOGFONT space to HB space. */
|
2012-07-26 23:34:25 +02:00
|
|
|
};
|
2011-08-09 00:13:24 +02:00
|
|
|
|
2012-07-27 07:26:11 +02:00
|
|
|
static bool
|
|
|
|
populate_log_font (LOGFONTW *lf,
|
2015-08-19 14:20:31 +02:00
|
|
|
hb_font_t *font,
|
|
|
|
unsigned int font_size)
|
2012-07-27 07:26:11 +02:00
|
|
|
{
|
|
|
|
memset (lf, 0, sizeof (*lf));
|
2017-11-02 01:13:35 +01:00
|
|
|
lf->lfHeight = - (int) font_size;
|
2012-07-27 07:26:11 +02:00
|
|
|
lf->lfCharSet = DEFAULT_CHARSET;
|
|
|
|
|
2013-07-21 22:06:49 +02:00
|
|
|
hb_face_t *face = font->face;
|
|
|
|
hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
|
2012-07-27 07:26:11 +02:00
|
|
|
|
2013-07-21 22:06:49 +02:00
|
|
|
memcpy (lf->lfFaceName, face_data->face_name, sizeof (lf->lfFaceName));
|
2012-07-27 07:26:11 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
hb_uniscribe_shaper_font_data_t *
|
|
|
|
_hb_uniscribe_shaper_font_data_create (hb_font_t *font)
|
|
|
|
{
|
2017-10-15 12:11:08 +02:00
|
|
|
if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return nullptr;
|
2012-07-30 23:54:38 +02:00
|
|
|
|
2012-07-27 08:29:32 +02:00
|
|
|
hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t));
|
2012-07-26 23:34:25 +02:00
|
|
|
if (unlikely (!data))
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2012-07-26 23:34:25 +02:00
|
|
|
|
2015-08-19 14:20:31 +02:00
|
|
|
int font_size = font->face->get_upem (); /* Default... */
|
|
|
|
/* No idea if the following is even a good idea. */
|
|
|
|
if (font->y_ppem)
|
|
|
|
font_size = font->y_ppem;
|
|
|
|
|
|
|
|
if (font_size < 0)
|
|
|
|
font_size = -font_size;
|
|
|
|
data->x_mult = (double) font->x_scale / font_size;
|
|
|
|
data->y_mult = (double) font->y_scale / font_size;
|
|
|
|
|
2017-10-15 12:11:08 +02:00
|
|
|
data->hdc = GetDC (nullptr);
|
2012-07-26 23:34:25 +02:00
|
|
|
|
2015-08-19 14:20:31 +02:00
|
|
|
if (unlikely (!populate_log_font (&data->log_font, font, font_size))) {
|
2012-07-26 23:34:25 +02:00
|
|
|
DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
|
|
|
|
_hb_uniscribe_shaper_font_data_destroy (data);
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2012-07-26 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data->hfont = CreateFontIndirectW (&data->log_font);
|
|
|
|
if (unlikely (!data->hfont)) {
|
|
|
|
DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
|
|
|
|
_hb_uniscribe_shaper_font_data_destroy (data);
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2012-07-26 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!SelectObject (data->hdc, data->hfont)) {
|
|
|
|
DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
|
|
|
|
_hb_uniscribe_shaper_font_data_destroy (data);
|
2017-10-15 12:11:08 +02:00
|
|
|
return nullptr;
|
2012-07-26 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data)
|
2011-08-09 00:13:24 +02:00
|
|
|
{
|
|
|
|
if (data->hdc)
|
2017-10-15 12:11:08 +02:00
|
|
|
ReleaseDC (nullptr, data->hdc);
|
2011-08-09 00:13:24 +02:00
|
|
|
if (data->hfont)
|
|
|
|
DeleteObject (data->hfont);
|
|
|
|
if (data->script_cache)
|
|
|
|
ScriptFreeCache (&data->script_cache);
|
|
|
|
free (data);
|
|
|
|
}
|
|
|
|
|
2012-12-10 00:37:38 +01:00
|
|
|
LOGFONTW *
|
|
|
|
hb_uniscribe_font_get_logfontw (hb_font_t *font)
|
|
|
|
{
|
2017-10-15 12:11:08 +02:00
|
|
|
if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return nullptr;
|
2012-12-10 00:37:38 +01:00
|
|
|
hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
|
|
|
|
return &font_data->log_font;
|
|
|
|
}
|
|
|
|
|
|
|
|
HFONT
|
|
|
|
hb_uniscribe_font_get_hfont (hb_font_t *font)
|
|
|
|
{
|
2017-10-15 12:11:08 +02:00
|
|
|
if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return nullptr;
|
2012-12-10 00:37:38 +01:00
|
|
|
hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
|
|
|
|
return font_data->hfont;
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* shaper shape_plan data
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct hb_uniscribe_shaper_shape_plan_data_t {};
|
|
|
|
|
|
|
|
hb_uniscribe_shaper_shape_plan_data_t *
|
2012-08-14 15:33:18 +02:00
|
|
|
_hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
|
|
|
|
const hb_feature_t *user_features HB_UNUSED,
|
2016-09-10 12:57:24 +02:00
|
|
|
unsigned int num_user_features HB_UNUSED,
|
|
|
|
const int *coords HB_UNUSED,
|
|
|
|
unsigned int num_coords HB_UNUSED)
|
2011-08-09 00:13:24 +02:00
|
|
|
{
|
2012-07-26 23:34:25 +02:00
|
|
|
return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
|
|
|
|
}
|
2011-08-09 00:13:24 +02:00
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
void
|
2012-08-14 15:33:18 +02:00
|
|
|
_hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED)
|
2012-07-26 23:34:25 +02:00
|
|
|
{
|
|
|
|
}
|
2011-08-09 00:13:24 +02:00
|
|
|
|
|
|
|
|
2012-07-26 23:34:25 +02:00
|
|
|
/*
|
|
|
|
* shaper
|
|
|
|
*/
|
2011-08-09 00:13:24 +02:00
|
|
|
|
|
|
|
|
2011-08-05 04:31:05 +02:00
|
|
|
hb_bool_t
|
2012-07-27 04:05:39 +02:00
|
|
|
_hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
|
|
|
|
hb_font_t *font,
|
2012-04-12 20:53:53 +02:00
|
|
|
hb_buffer_t *buffer,
|
|
|
|
const hb_feature_t *features,
|
|
|
|
unsigned int num_features)
|
2011-08-04 01:55:04 +02:00
|
|
|
{
|
2012-07-27 07:26:11 +02:00
|
|
|
hb_face_t *face = font->face;
|
|
|
|
hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
|
|
|
|
hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
|
2013-05-28 19:10:51 +02:00
|
|
|
hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
|
2011-08-05 04:31:05 +02:00
|
|
|
|
2013-08-07 23:26:13 +02:00
|
|
|
/*
|
|
|
|
* Set up features.
|
|
|
|
*/
|
|
|
|
hb_auto_array_t<OPENTYPE_FEATURE_RECORD> feature_records;
|
|
|
|
hb_auto_array_t<range_record_t> range_records;
|
|
|
|
if (num_features)
|
|
|
|
{
|
|
|
|
/* Sort features by start/end events. */
|
|
|
|
hb_auto_array_t<feature_event_t> feature_events;
|
|
|
|
for (unsigned int i = 0; i < num_features; i++)
|
|
|
|
{
|
|
|
|
active_feature_t feature;
|
|
|
|
feature.rec.tagFeature = hb_uint32_swap (features[i].tag);
|
|
|
|
feature.rec.lParameter = features[i].value;
|
|
|
|
feature.order = i;
|
|
|
|
|
|
|
|
feature_event_t *event;
|
|
|
|
|
|
|
|
event = feature_events.push ();
|
|
|
|
if (unlikely (!event))
|
|
|
|
goto fail_features;
|
|
|
|
event->index = features[i].start;
|
|
|
|
event->start = true;
|
|
|
|
event->feature = feature;
|
|
|
|
|
|
|
|
event = feature_events.push ();
|
|
|
|
if (unlikely (!event))
|
|
|
|
goto fail_features;
|
|
|
|
event->index = features[i].end;
|
|
|
|
event->start = false;
|
|
|
|
event->feature = feature;
|
|
|
|
}
|
2014-06-19 21:30:18 +02:00
|
|
|
feature_events.qsort ();
|
2013-08-07 23:26:13 +02:00
|
|
|
/* Add a strategic final event. */
|
|
|
|
{
|
|
|
|
active_feature_t feature;
|
|
|
|
feature.rec.tagFeature = 0;
|
|
|
|
feature.rec.lParameter = 0;
|
|
|
|
feature.order = num_features + 1;
|
|
|
|
|
|
|
|
feature_event_t *event = feature_events.push ();
|
|
|
|
if (unlikely (!event))
|
|
|
|
goto fail_features;
|
|
|
|
event->index = 0; /* This value does magic. */
|
|
|
|
event->start = false;
|
|
|
|
event->feature = feature;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scan events and save features for each range. */
|
|
|
|
hb_auto_array_t<active_feature_t> active_features;
|
|
|
|
unsigned int last_index = 0;
|
|
|
|
for (unsigned int i = 0; i < feature_events.len; i++)
|
|
|
|
{
|
|
|
|
feature_event_t *event = &feature_events[i];
|
|
|
|
|
|
|
|
if (event->index != last_index)
|
|
|
|
{
|
|
|
|
/* Save a snapshot of active features and the range. */
|
|
|
|
range_record_t *range = range_records.push ();
|
|
|
|
if (unlikely (!range))
|
|
|
|
goto fail_features;
|
|
|
|
|
|
|
|
unsigned int offset = feature_records.len;
|
|
|
|
|
2014-06-19 21:30:18 +02:00
|
|
|
active_features.qsort ();
|
2013-08-07 23:26:13 +02:00
|
|
|
for (unsigned int j = 0; j < active_features.len; j++)
|
|
|
|
{
|
|
|
|
if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.len - 1].tagFeature)
|
|
|
|
{
|
|
|
|
OPENTYPE_FEATURE_RECORD *feature = feature_records.push ();
|
|
|
|
if (unlikely (!feature))
|
|
|
|
goto fail_features;
|
|
|
|
*feature = active_features[j].rec;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Overrides value for existing feature. */
|
|
|
|
feature_records[feature_records.len - 1].lParameter = active_features[j].rec.lParameter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Will convert to pointer after all is ready, since feature_records.array
|
|
|
|
* may move as we grow it. */
|
|
|
|
range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset);
|
|
|
|
range->props.cotfRecords = feature_records.len - offset;
|
|
|
|
range->index_first = last_index;
|
|
|
|
range->index_last = event->index - 1;
|
|
|
|
|
|
|
|
last_index = event->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->start) {
|
|
|
|
active_feature_t *feature = active_features.push ();
|
|
|
|
if (unlikely (!feature))
|
|
|
|
goto fail_features;
|
|
|
|
*feature = event->feature;
|
|
|
|
} else {
|
|
|
|
active_feature_t *feature = active_features.find (&event->feature);
|
|
|
|
if (feature)
|
|
|
|
active_features.remove (feature - active_features.array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!range_records.len) /* No active feature found. */
|
|
|
|
goto fail_features;
|
|
|
|
|
|
|
|
/* Fixup the pointers. */
|
|
|
|
for (unsigned int i = 0; i < range_records.len; i++)
|
|
|
|
{
|
|
|
|
range_record_t *range = &range_records[i];
|
2014-01-20 15:23:08 +01:00
|
|
|
range->props.potfRecords = feature_records.array + reinterpret_cast<uintptr_t> (range->props.potfRecords);
|
2013-08-07 23:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fail_features:
|
|
|
|
num_features = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-09 00:13:24 +02:00
|
|
|
#define FAIL(...) \
|
|
|
|
HB_STMT_START { \
|
2017-10-15 12:11:08 +02:00
|
|
|
DEBUG_MSG (UNISCRIBE, nullptr, __VA_ARGS__); \
|
2012-06-06 02:35:40 +02:00
|
|
|
return false; \
|
2011-08-09 00:13:24 +02:00
|
|
|
} HB_STMT_END;
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
2011-08-04 01:55:04 +02:00
|
|
|
retry:
|
|
|
|
|
|
|
|
unsigned int scratch_size;
|
2013-11-13 20:44:01 +01:00
|
|
|
hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
|
2011-08-04 01:55:04 +02:00
|
|
|
|
2013-11-13 20:44:01 +01:00
|
|
|
#define ALLOCATE_ARRAY(Type, name, len) \
|
|
|
|
Type *name = (Type *) scratch; \
|
|
|
|
{ \
|
|
|
|
unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
|
|
|
|
assert (_consumed <= scratch_size); \
|
|
|
|
scratch += _consumed; \
|
|
|
|
scratch_size -= _consumed; \
|
|
|
|
}
|
2011-08-04 01:55:04 +02:00
|
|
|
|
|
|
|
#define utf16_index() var1.u32
|
|
|
|
|
2013-11-13 20:44:01 +01:00
|
|
|
ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2);
|
|
|
|
|
2011-08-04 01:55:04 +02:00
|
|
|
unsigned int chars_len = 0;
|
2013-08-07 23:26:13 +02:00
|
|
|
for (unsigned int i = 0; i < buffer->len; i++)
|
|
|
|
{
|
2011-08-04 01:55:04 +02:00
|
|
|
hb_codepoint_t c = buffer->info[i].codepoint;
|
|
|
|
buffer->info[i].utf16_index() = chars_len;
|
2014-07-11 20:54:42 +02:00
|
|
|
if (likely (c <= 0xFFFFu))
|
2011-08-04 01:55:04 +02:00
|
|
|
pchars[chars_len++] = c;
|
2014-07-11 20:54:42 +02:00
|
|
|
else if (unlikely (c > 0x10FFFFu))
|
|
|
|
pchars[chars_len++] = 0xFFFDu;
|
2011-08-04 01:55:04 +02:00
|
|
|
else {
|
2014-07-11 20:54:42 +02:00
|
|
|
pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
|
2016-08-09 02:24:04 +02:00
|
|
|
pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
|
2011-08-04 01:55:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
|
|
|
|
ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
|
|
|
|
|
2013-08-07 23:26:13 +02:00
|
|
|
if (num_features)
|
|
|
|
{
|
|
|
|
/* Need log_clusters to assign features. */
|
|
|
|
chars_len = 0;
|
|
|
|
for (unsigned int i = 0; i < buffer->len; i++)
|
|
|
|
{
|
|
|
|
hb_codepoint_t c = buffer->info[i].codepoint;
|
|
|
|
unsigned int cluster = buffer->info[i].cluster;
|
|
|
|
log_clusters[chars_len++] = cluster;
|
2014-07-11 20:54:42 +02:00
|
|
|
if (hb_in_range (c, 0x10000u, 0x10FFFFu))
|
2013-08-07 23:26:13 +02:00
|
|
|
log_clusters[chars_len++] = cluster; /* Surrogates. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 20:39:23 +01:00
|
|
|
/* The -2 in the following is to compensate for possible
|
|
|
|
* alignment needed after the WORD array. sizeof(WORD) == 2. */
|
|
|
|
unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
|
|
|
|
/ (sizeof (WORD) +
|
|
|
|
sizeof (SCRIPT_GLYPHPROP) +
|
|
|
|
sizeof (int) +
|
|
|
|
sizeof (GOFFSET) +
|
|
|
|
sizeof (uint32_t));
|
2011-08-04 01:55:04 +02:00
|
|
|
|
|
|
|
ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
|
|
|
|
ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
|
|
|
|
ALLOCATE_ARRAY (int, advances, glyphs_size);
|
|
|
|
ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
|
|
|
|
ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
|
|
|
|
|
2013-05-28 19:10:51 +02:00
|
|
|
/* Note:
|
|
|
|
* We can't touch the contents of glyph_props. Our fallback
|
|
|
|
* implementations of Shape and Place functions use that buffer
|
|
|
|
* by casting it to a different type. It works because they
|
|
|
|
* both agree about it, but if we want to access it here we
|
|
|
|
* need address that issue first.
|
|
|
|
*/
|
|
|
|
|
2012-07-26 01:20:34 +02:00
|
|
|
#undef ALLOCATE_ARRAY
|
2011-08-04 01:55:04 +02:00
|
|
|
|
2012-06-08 20:18:30 +02:00
|
|
|
#define MAX_ITEMS 256
|
2011-08-04 01:55:04 +02:00
|
|
|
|
|
|
|
SCRIPT_ITEM items[MAX_ITEMS + 1];
|
2011-09-19 20:53:26 +02:00
|
|
|
SCRIPT_CONTROL bidi_control = {0};
|
2011-08-04 01:55:04 +02:00
|
|
|
SCRIPT_STATE bidi_state = {0};
|
2012-08-28 23:57:49 +02:00
|
|
|
ULONG script_tags[MAX_ITEMS];
|
2011-08-04 01:55:04 +02:00
|
|
|
int item_count;
|
|
|
|
|
2011-09-19 20:53:26 +02:00
|
|
|
/* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
|
2012-06-06 02:35:40 +02:00
|
|
|
//bidi_control.fMergeNeutralItems = true;
|
2016-08-09 02:24:04 +02:00
|
|
|
*(uint32_t*)&bidi_control |= 1u<<24;
|
2011-09-19 20:53:26 +02:00
|
|
|
|
2011-08-04 01:55:04 +02:00
|
|
|
bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
|
2012-06-08 15:26:17 +02:00
|
|
|
bidi_state.fOverrideDirection = 1;
|
2011-08-04 01:55:04 +02:00
|
|
|
|
2013-11-13 20:44:01 +01:00
|
|
|
hr = funcs->ScriptItemizeOpenType (pchars,
|
2013-05-28 19:10:51 +02:00
|
|
|
chars_len,
|
|
|
|
MAX_ITEMS,
|
|
|
|
&bidi_control,
|
|
|
|
&bidi_state,
|
|
|
|
items,
|
|
|
|
script_tags,
|
|
|
|
&item_count);
|
2011-08-04 01:55:04 +02:00
|
|
|
if (unlikely (FAILED (hr)))
|
2011-08-07 06:59:38 +02:00
|
|
|
FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr);
|
2011-08-04 01:55:04 +02:00
|
|
|
|
|
|
|
#undef MAX_ITEMS
|
|
|
|
|
2013-08-07 23:26:13 +02:00
|
|
|
OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language));
|
2013-08-06 20:45:36 +02:00
|
|
|
hb_auto_array_t<TEXTRANGE_PROPERTIES*> range_properties;
|
|
|
|
hb_auto_array_t<int> range_char_counts;
|
2011-08-04 01:55:04 +02:00
|
|
|
|
|
|
|
unsigned int glyphs_offset = 0;
|
|
|
|
unsigned int glyphs_len;
|
2012-06-08 16:10:29 +02:00
|
|
|
bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
|
2017-11-02 01:13:35 +01:00
|
|
|
for (int i = 0; i < item_count; i++)
|
2011-08-04 01:55:04 +02:00
|
|
|
{
|
2012-06-08 16:23:03 +02:00
|
|
|
unsigned int chars_offset = items[i].iCharPos;
|
|
|
|
unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
|
|
|
|
|
2013-08-06 20:45:36 +02:00
|
|
|
if (num_features)
|
|
|
|
{
|
|
|
|
range_properties.shrink (0);
|
2013-08-07 23:26:13 +02:00
|
|
|
range_char_counts.shrink (0);
|
2013-08-06 20:45:36 +02:00
|
|
|
|
2013-08-07 23:26:13 +02:00
|
|
|
range_record_t *last_range = &range_records[0];
|
2013-08-06 20:45:36 +02:00
|
|
|
|
2013-08-07 23:26:13 +02:00
|
|
|
for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++)
|
|
|
|
{
|
|
|
|
range_record_t *range = last_range;
|
|
|
|
while (log_clusters[k] < range->index_first)
|
|
|
|
range--;
|
|
|
|
while (log_clusters[k] > range->index_last)
|
|
|
|
range++;
|
|
|
|
if (!range_properties.len ||
|
|
|
|
&range->props != range_properties[range_properties.len - 1])
|
|
|
|
{
|
|
|
|
TEXTRANGE_PROPERTIES **props = range_properties.push ();
|
|
|
|
int *c = range_char_counts.push ();
|
|
|
|
if (unlikely (!props || !c))
|
|
|
|
{
|
|
|
|
range_properties.shrink (0);
|
|
|
|
range_char_counts.shrink (0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*props = &range->props;
|
|
|
|
*c = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
range_char_counts[range_char_counts.len - 1]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_range = range;
|
|
|
|
}
|
2013-08-06 20:45:36 +02:00
|
|
|
}
|
|
|
|
|
2013-08-21 23:22:21 +02:00
|
|
|
/* Asking for glyphs in logical order circumvents at least
|
|
|
|
* one bug in Uniscribe. */
|
|
|
|
items[i].a.fLogicalOrder = true;
|
|
|
|
|
2012-07-20 23:05:46 +02:00
|
|
|
retry_shape:
|
2013-05-28 19:10:51 +02:00
|
|
|
hr = funcs->ScriptShapeOpenType (font_data->hdc,
|
|
|
|
&font_data->script_cache,
|
|
|
|
&items[i].a,
|
|
|
|
script_tags[i],
|
|
|
|
language_tag,
|
2013-08-06 20:45:36 +02:00
|
|
|
range_char_counts.array,
|
|
|
|
range_properties.array,
|
|
|
|
range_properties.len,
|
2013-11-13 20:44:01 +01:00
|
|
|
pchars + chars_offset,
|
2013-05-28 19:10:51 +02:00
|
|
|
item_chars_len,
|
|
|
|
glyphs_size - glyphs_offset,
|
|
|
|
/* out */
|
|
|
|
log_clusters + chars_offset,
|
|
|
|
char_props + chars_offset,
|
|
|
|
glyphs + glyphs_offset,
|
|
|
|
glyph_props + glyphs_offset,
|
|
|
|
(int *) &glyphs_len);
|
2012-06-08 16:23:03 +02:00
|
|
|
|
|
|
|
if (unlikely (items[i].a.fNoGlyphIndex))
|
|
|
|
FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
|
2015-12-17 17:57:47 +01:00
|
|
|
if (unlikely (hr == E_OUTOFMEMORY || hr == E_NOT_SUFFICIENT_BUFFER))
|
2012-06-08 16:23:03 +02:00
|
|
|
{
|
2014-08-11 19:42:42 +02:00
|
|
|
if (unlikely (!buffer->ensure (buffer->allocated * 2)))
|
2012-06-08 16:23:03 +02:00
|
|
|
FAIL ("Buffer resize failed");
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
|
2012-07-20 23:05:46 +02:00
|
|
|
{
|
|
|
|
if (items[i].a.eScript == SCRIPT_UNDEFINED)
|
|
|
|
FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
|
|
|
|
items[i].a.eScript = SCRIPT_UNDEFINED;
|
|
|
|
goto retry_shape;
|
|
|
|
}
|
2012-06-08 16:23:03 +02:00
|
|
|
if (unlikely (FAILED (hr)))
|
2012-07-20 23:05:46 +02:00
|
|
|
{
|
2012-06-08 16:23:03 +02:00
|
|
|
FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr);
|
2012-07-20 23:05:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
|
|
|
|
log_clusters[j] += glyphs_offset;
|
2012-06-08 16:23:03 +02:00
|
|
|
|
2013-05-28 19:10:51 +02:00
|
|
|
hr = funcs->ScriptPlaceOpenType (font_data->hdc,
|
|
|
|
&font_data->script_cache,
|
|
|
|
&items[i].a,
|
|
|
|
script_tags[i],
|
|
|
|
language_tag,
|
2013-08-06 20:45:36 +02:00
|
|
|
range_char_counts.array,
|
|
|
|
range_properties.array,
|
|
|
|
range_properties.len,
|
2013-11-13 20:44:01 +01:00
|
|
|
pchars + chars_offset,
|
2013-05-28 19:10:51 +02:00
|
|
|
log_clusters + chars_offset,
|
|
|
|
char_props + chars_offset,
|
|
|
|
item_chars_len,
|
|
|
|
glyphs + glyphs_offset,
|
|
|
|
glyph_props + glyphs_offset,
|
|
|
|
glyphs_len,
|
|
|
|
/* out */
|
|
|
|
advances + glyphs_offset,
|
|
|
|
offsets + glyphs_offset,
|
2017-10-15 12:11:08 +02:00
|
|
|
nullptr);
|
2012-06-08 16:23:03 +02:00
|
|
|
if (unlikely (FAILED (hr)))
|
|
|
|
FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr);
|
|
|
|
|
2013-08-20 19:22:21 +02:00
|
|
|
if (DEBUG_ENABLED (UNISCRIBE))
|
|
|
|
fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n",
|
|
|
|
i,
|
|
|
|
items[i].a.fRTL,
|
|
|
|
items[i].a.fLayoutRTL,
|
|
|
|
items[i].a.fLogicalOrder,
|
|
|
|
HB_UNTAG (hb_uint32_swap (script_tags[i])));
|
|
|
|
|
2012-06-08 16:23:03 +02:00
|
|
|
glyphs_offset += glyphs_len;
|
2011-08-04 01:55:04 +02:00
|
|
|
}
|
|
|
|
glyphs_len = glyphs_offset;
|
|
|
|
|
|
|
|
/* Ok, we've got everything we need, now compose output buffer,
|
|
|
|
* very, *very*, carefully! */
|
|
|
|
|
|
|
|
/* Calculate visual-clusters. That's what we ship. */
|
2011-09-19 20:53:26 +02:00
|
|
|
for (unsigned int i = 0; i < glyphs_len; i++)
|
|
|
|
vis_clusters[i] = -1;
|
2011-08-07 07:04:40 +02:00
|
|
|
for (unsigned int i = 0; i < buffer->len; i++) {
|
|
|
|
uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
|
|
|
|
*p = MIN (*p, buffer->info[i].cluster);
|
|
|
|
}
|
2013-08-21 23:22:21 +02:00
|
|
|
for (unsigned int i = 1; i < glyphs_len; i++)
|
|
|
|
if (vis_clusters[i] == -1)
|
|
|
|
vis_clusters[i] = vis_clusters[i - 1];
|
2011-08-04 01:55:04 +02:00
|
|
|
|
|
|
|
#undef utf16_index
|
|
|
|
|
2014-08-11 19:42:42 +02:00
|
|
|
if (unlikely (!buffer->ensure (glyphs_len)))
|
2011-08-05 04:31:05 +02:00
|
|
|
FAIL ("Buffer in error");
|
2011-08-04 01:55:04 +02:00
|
|
|
|
2011-08-05 04:31:05 +02:00
|
|
|
#undef FAIL
|
2011-08-04 01:55:04 +02:00
|
|
|
|
|
|
|
/* Set glyph infos */
|
2011-08-09 14:03:12 +02:00
|
|
|
buffer->len = 0;
|
2011-08-04 01:55:04 +02:00
|
|
|
for (unsigned int i = 0; i < glyphs_len; i++)
|
|
|
|
{
|
2011-08-09 14:03:12 +02:00
|
|
|
hb_glyph_info_t *info = &buffer->info[buffer->len++];
|
2011-08-04 01:55:04 +02:00
|
|
|
|
|
|
|
info->codepoint = glyphs[i];
|
|
|
|
info->cluster = vis_clusters[i];
|
|
|
|
|
|
|
|
/* The rest is crap. Let's store position info there for now. */
|
|
|
|
info->mask = advances[i];
|
2015-08-20 16:39:06 +02:00
|
|
|
info->var1.i32 = offsets[i].du;
|
|
|
|
info->var2.i32 = offsets[i].dv;
|
2011-08-04 01:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set glyph positions */
|
|
|
|
buffer->clear_positions ();
|
2015-08-19 14:20:31 +02:00
|
|
|
double x_mult = font_data->x_mult, y_mult = font_data->y_mult;
|
2011-08-04 01:55:04 +02:00
|
|
|
for (unsigned int i = 0; i < glyphs_len; i++)
|
|
|
|
{
|
|
|
|
hb_glyph_info_t *info = &buffer->info[i];
|
|
|
|
hb_glyph_position_t *pos = &buffer->pos[i];
|
|
|
|
|
|
|
|
/* TODO vertical */
|
2016-01-06 13:29:50 +01:00
|
|
|
pos->x_advance = x_mult * (int32_t) info->mask;
|
2015-08-20 16:39:06 +02:00
|
|
|
pos->x_offset = x_mult * (backward ? -info->var1.i32 : info->var1.i32);
|
|
|
|
pos->y_offset = y_mult * info->var2.i32;
|
2011-08-04 01:55:04 +02:00
|
|
|
}
|
|
|
|
|
2013-08-21 23:22:21 +02:00
|
|
|
if (backward)
|
|
|
|
hb_buffer_reverse (buffer);
|
|
|
|
|
2017-11-11 02:14:27 +01:00
|
|
|
buffer->unsafe_to_break_all ();
|
|
|
|
|
2011-08-04 01:55:04 +02:00
|
|
|
/* Wow, done! */
|
2012-06-06 02:35:40 +02:00
|
|
|
return true;
|
2011-08-04 01:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|