michael@0: /* michael@0: * Copyright © 2011,2012,2013 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #define _WIN32_WINNT 0x0600 michael@0: #define WIN32_LEAN_AND_MEAN michael@0: michael@0: #define HB_SHAPER uniscribe michael@0: #include "hb-shaper-impl-private.hh" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "hb-uniscribe.h" michael@0: michael@0: #include "hb-open-file-private.hh" michael@0: #include "hb-ot-name-table.hh" michael@0: #include "hb-ot-tag.h" michael@0: michael@0: michael@0: #ifndef HB_DEBUG_UNISCRIBE michael@0: #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0) michael@0: #endif michael@0: michael@0: michael@0: typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/( michael@0: const WCHAR *pwcInChars, michael@0: int cInChars, michael@0: int cMaxItems, michael@0: const SCRIPT_CONTROL *psControl, michael@0: const SCRIPT_STATE *psState, michael@0: SCRIPT_ITEM *pItems, michael@0: OPENTYPE_TAG *pScriptTags, michael@0: int *pcItems michael@0: ); michael@0: michael@0: typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/( michael@0: HDC hdc, michael@0: SCRIPT_CACHE *psc, michael@0: SCRIPT_ANALYSIS *psa, michael@0: OPENTYPE_TAG tagScript, michael@0: OPENTYPE_TAG tagLangSys, michael@0: int *rcRangeChars, michael@0: TEXTRANGE_PROPERTIES **rpRangeProperties, michael@0: int cRanges, michael@0: const WCHAR *pwcChars, michael@0: int cChars, michael@0: int cMaxGlyphs, michael@0: WORD *pwLogClust, michael@0: SCRIPT_CHARPROP *pCharProps, michael@0: WORD *pwOutGlyphs, michael@0: SCRIPT_GLYPHPROP *pOutGlyphProps, michael@0: int *pcGlyphs michael@0: ); michael@0: michael@0: typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/( michael@0: HDC hdc, michael@0: SCRIPT_CACHE *psc, michael@0: SCRIPT_ANALYSIS *psa, michael@0: OPENTYPE_TAG tagScript, michael@0: OPENTYPE_TAG tagLangSys, michael@0: int *rcRangeChars, michael@0: TEXTRANGE_PROPERTIES **rpRangeProperties, michael@0: int cRanges, michael@0: const WCHAR *pwcChars, michael@0: WORD *pwLogClust, michael@0: SCRIPT_CHARPROP *pCharProps, michael@0: int cChars, michael@0: const WORD *pwGlyphs, michael@0: const SCRIPT_GLYPHPROP *pGlyphProps, michael@0: int cGlyphs, michael@0: int *piAdvance, michael@0: GOFFSET *pGoffset, michael@0: ABC *pABC michael@0: ); michael@0: michael@0: michael@0: /* Fallback implementations. */ michael@0: michael@0: static HRESULT WINAPI michael@0: hb_ScriptItemizeOpenType( michael@0: const WCHAR *pwcInChars, michael@0: int cInChars, michael@0: int cMaxItems, michael@0: const SCRIPT_CONTROL *psControl, michael@0: const SCRIPT_STATE *psState, michael@0: SCRIPT_ITEM *pItems, michael@0: OPENTYPE_TAG *pScriptTags, michael@0: int *pcItems michael@0: ) michael@0: { michael@0: { michael@0: return ScriptItemize (pwcInChars, michael@0: cInChars, michael@0: cMaxItems, michael@0: psControl, michael@0: psState, michael@0: pItems, michael@0: pcItems); michael@0: } michael@0: } michael@0: michael@0: static HRESULT WINAPI michael@0: hb_ScriptShapeOpenType( michael@0: HDC hdc, michael@0: SCRIPT_CACHE *psc, michael@0: SCRIPT_ANALYSIS *psa, michael@0: OPENTYPE_TAG tagScript, michael@0: OPENTYPE_TAG tagLangSys, michael@0: int *rcRangeChars, michael@0: TEXTRANGE_PROPERTIES **rpRangeProperties, michael@0: int cRanges, michael@0: const WCHAR *pwcChars, michael@0: int cChars, michael@0: int cMaxGlyphs, michael@0: WORD *pwLogClust, michael@0: SCRIPT_CHARPROP *pCharProps, michael@0: WORD *pwOutGlyphs, michael@0: SCRIPT_GLYPHPROP *pOutGlyphProps, michael@0: int *pcGlyphs michael@0: ) michael@0: { michael@0: SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps; michael@0: return ScriptShape (hdc, michael@0: psc, michael@0: pwcChars, michael@0: cChars, michael@0: cMaxGlyphs, michael@0: psa, michael@0: pwOutGlyphs, michael@0: pwLogClust, michael@0: psva, michael@0: pcGlyphs); michael@0: } michael@0: michael@0: static HRESULT WINAPI michael@0: hb_ScriptPlaceOpenType( michael@0: HDC hdc, michael@0: SCRIPT_CACHE *psc, michael@0: SCRIPT_ANALYSIS *psa, michael@0: OPENTYPE_TAG tagScript, michael@0: OPENTYPE_TAG tagLangSys, michael@0: int *rcRangeChars, michael@0: TEXTRANGE_PROPERTIES **rpRangeProperties, michael@0: int cRanges, michael@0: const WCHAR *pwcChars, michael@0: WORD *pwLogClust, michael@0: SCRIPT_CHARPROP *pCharProps, michael@0: int cChars, michael@0: const WORD *pwGlyphs, michael@0: const SCRIPT_GLYPHPROP *pGlyphProps, michael@0: int cGlyphs, michael@0: int *piAdvance, michael@0: GOFFSET *pGoffset, michael@0: ABC *pABC michael@0: ) michael@0: { michael@0: SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps; michael@0: return ScriptPlace (hdc, michael@0: psc, michael@0: pwGlyphs, michael@0: cGlyphs, michael@0: psva, michael@0: psa, michael@0: piAdvance, michael@0: pGoffset, michael@0: pABC); michael@0: } michael@0: michael@0: michael@0: struct hb_uniscribe_shaper_funcs_t { michael@0: SIOT ScriptItemizeOpenType; michael@0: SSOT ScriptShapeOpenType; michael@0: SPOT ScriptPlaceOpenType; michael@0: michael@0: inline void init (void) michael@0: { michael@0: HMODULE hinstLib; michael@0: this->ScriptItemizeOpenType = NULL; michael@0: this->ScriptShapeOpenType = NULL; michael@0: this->ScriptPlaceOpenType = NULL; michael@0: michael@0: hinstLib = GetModuleHandle (TEXT ("usp10.dll")); michael@0: if (hinstLib) michael@0: { michael@0: this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType"); michael@0: this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType"); michael@0: this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType"); michael@0: } michael@0: if (!this->ScriptItemizeOpenType || michael@0: !this->ScriptShapeOpenType || michael@0: !this->ScriptPlaceOpenType) michael@0: { michael@0: DEBUG_MSG (UNISCRIBE, NULL, "OpenType versions of functions not found; falling back."); michael@0: this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType; michael@0: this->ScriptShapeOpenType = hb_ScriptShapeOpenType; michael@0: this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType; michael@0: } michael@0: } michael@0: }; michael@0: static hb_uniscribe_shaper_funcs_t *uniscribe_funcs; michael@0: michael@0: static inline void michael@0: free_uniscribe_funcs (void) michael@0: { michael@0: free (uniscribe_funcs); michael@0: } michael@0: michael@0: static hb_uniscribe_shaper_funcs_t * michael@0: hb_uniscribe_shaper_get_funcs (void) michael@0: { michael@0: retry: michael@0: hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs); michael@0: michael@0: if (unlikely (!funcs)) michael@0: { michael@0: funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t)); michael@0: if (unlikely (!funcs)) michael@0: return NULL; michael@0: michael@0: funcs->init (); michael@0: michael@0: if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, NULL, funcs)) { michael@0: free (funcs); michael@0: goto retry; michael@0: } michael@0: michael@0: #ifdef HAVE_ATEXIT michael@0: atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */ michael@0: #endif michael@0: } michael@0: michael@0: return funcs; michael@0: } michael@0: michael@0: michael@0: struct active_feature_t { michael@0: OPENTYPE_FEATURE_RECORD rec; michael@0: unsigned int order; michael@0: michael@0: static int cmp (const active_feature_t *a, const active_feature_t *b) { michael@0: return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 : michael@0: a->order < b->order ? -1 : a->order > b->order ? 1 : michael@0: a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 : michael@0: 0; michael@0: } michael@0: bool operator== (const active_feature_t *f) { michael@0: return cmp (this, f) == 0; michael@0: } michael@0: }; michael@0: michael@0: struct feature_event_t { michael@0: unsigned int index; michael@0: bool start; michael@0: active_feature_t feature; michael@0: michael@0: static int cmp (const feature_event_t *a, const feature_event_t *b) { michael@0: return a->index < b->index ? -1 : a->index > b->index ? 1 : michael@0: a->start < b->start ? -1 : a->start > b->start ? 1 : michael@0: active_feature_t::cmp (&a->feature, &b->feature); michael@0: } michael@0: }; michael@0: michael@0: struct range_record_t { michael@0: TEXTRANGE_PROPERTIES props; michael@0: unsigned int index_first; /* == start */ michael@0: unsigned int index_last; /* == end - 1 */ michael@0: }; michael@0: michael@0: HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, face) michael@0: HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, font) michael@0: michael@0: michael@0: /* michael@0: * shaper face data michael@0: */ michael@0: michael@0: struct hb_uniscribe_shaper_face_data_t { michael@0: HANDLE fh; michael@0: hb_uniscribe_shaper_funcs_t *funcs; michael@0: wchar_t face_name[LF_FACESIZE]; michael@0: }; michael@0: michael@0: /* face_name should point to a wchar_t[LF_FACESIZE] object. */ michael@0: static void michael@0: _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen) michael@0: { michael@0: /* We'll create a private name for the font from a UUID using a simple, michael@0: * somewhat base64-like encoding scheme */ michael@0: const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-"; michael@0: UUID id; michael@0: UuidCreate ((UUID*) &id); michael@0: unsigned int name_str_len = 0; michael@0: face_name[name_str_len++] = 'F'; michael@0: face_name[name_str_len++] = '_'; michael@0: unsigned char *p = (unsigned char *) &id; michael@0: for (unsigned int i = 0; i < 16; i += 2) michael@0: { michael@0: /* Spread the 16 bits from two bytes of the UUID across three chars of face_name, michael@0: * using the bits in groups of 5,5,6 to select chars from enc. michael@0: * This will generate 24 characters; with the 'F_' prefix we already provided, michael@0: * the name will be 26 chars (plus the NUL terminator), so will always fit within michael@0: * face_name (LF_FACESIZE = 32). */ michael@0: face_name[name_str_len++] = enc[p[i] >> 3]; michael@0: face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f]; michael@0: face_name[name_str_len++] = enc[p[i + 1] & 0x3f]; michael@0: } michael@0: face_name[name_str_len] = 0; michael@0: if (plen) michael@0: *plen = name_str_len; michael@0: } michael@0: michael@0: /* Destroys blob. */ michael@0: static hb_blob_t * michael@0: _hb_rename_font (hb_blob_t *blob, wchar_t *new_name) michael@0: { michael@0: /* Create a copy of the font data, with the 'name' table replaced by a michael@0: * table that names the font with our private F_* name created above. michael@0: * For simplicity, we just append a new 'name' table and update the michael@0: * sfnt directory; the original table is left in place, but unused. michael@0: * michael@0: * The new table will contain just 5 name IDs: family, style, unique, michael@0: * full, PS. All of them point to the same name data with our unique name. michael@0: */ michael@0: michael@0: blob = OT::Sanitizer::sanitize (blob); michael@0: michael@0: unsigned int length, new_length, name_str_len; michael@0: const char *orig_sfnt_data = hb_blob_get_data (blob, &length); michael@0: michael@0: _hb_generate_unique_face_name (new_name, &name_str_len); michael@0: michael@0: static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 }; michael@0: michael@0: unsigned int name_table_length = OT::name::min_size + michael@0: ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size + michael@0: name_str_len * 2; /* for name data in UTF16BE form */ michael@0: unsigned int name_table_offset = (length + 3) & ~3; michael@0: michael@0: new_length = name_table_offset + ((name_table_length + 3) & ~3); michael@0: void *new_sfnt_data = calloc (1, new_length); michael@0: if (!new_sfnt_data) michael@0: { michael@0: hb_blob_destroy (blob); michael@0: return NULL; michael@0: } michael@0: michael@0: memcpy(new_sfnt_data, orig_sfnt_data, length); michael@0: michael@0: OT::name &name = OT::StructAtOffset (new_sfnt_data, name_table_offset); michael@0: name.format.set (0); michael@0: name.count.set (ARRAY_LENGTH (name_IDs)); michael@0: name.stringOffset.set (name.get_size ()); michael@0: for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++) michael@0: { michael@0: OT::NameRecord &record = name.nameRecord[i]; michael@0: record.platformID.set (3); michael@0: record.encodingID.set (1); michael@0: record.languageID.set (0x0409); /* English */ michael@0: record.nameID.set (name_IDs[i]); michael@0: record.length.set (name_str_len * 2); michael@0: record.offset.set (0); michael@0: } michael@0: michael@0: /* Copy string data from new_name, converting wchar_t to UTF16BE. */ michael@0: unsigned char *p = &OT::StructAfter (name); michael@0: for (unsigned int i = 0; i < name_str_len; i++) michael@0: { michael@0: *p++ = new_name[i] >> 8; michael@0: *p++ = new_name[i] & 0xff; michael@0: } michael@0: michael@0: /* Adjust name table entry to point to new name table */ michael@0: const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data); michael@0: unsigned int face_count = file.get_face_count (); michael@0: for (unsigned int face_index = 0; face_index < face_count; face_index++) michael@0: { michael@0: /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be michael@0: * toe-stepping. But we don't really care. */ michael@0: const OT::OpenTypeFontFace &face = file.get_face (face_index); michael@0: unsigned int index; michael@0: if (face.find_table_index (HB_OT_TAG_name, &index)) michael@0: { michael@0: OT::TableRecord &record = const_cast (face.get_table (index)); michael@0: record.checkSum.set_for_data (&name, name_table_length); michael@0: record.offset.set (name_table_offset); michael@0: record.length.set (name_table_length); michael@0: } michael@0: else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */ michael@0: { michael@0: free (new_sfnt_data); michael@0: hb_blob_destroy (blob); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: /* The checkSumAdjustment field in the 'head' table is now wrong, michael@0: * but that doesn't actually seem to cause any problems so we don't michael@0: * bother. */ michael@0: michael@0: hb_blob_destroy (blob); michael@0: return hb_blob_create ((const char *) new_sfnt_data, new_length, michael@0: HB_MEMORY_MODE_WRITABLE, NULL, free); michael@0: } michael@0: michael@0: hb_uniscribe_shaper_face_data_t * michael@0: _hb_uniscribe_shaper_face_data_create (hb_face_t *face) michael@0: { michael@0: hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t)); michael@0: if (unlikely (!data)) michael@0: return NULL; michael@0: michael@0: data->funcs = hb_uniscribe_shaper_get_funcs (); michael@0: if (unlikely (!data->funcs)) michael@0: { michael@0: free (data); michael@0: return NULL; michael@0: } michael@0: michael@0: hb_blob_t *blob = hb_face_reference_blob (face); michael@0: if (unlikely (!hb_blob_get_length (blob))) michael@0: DEBUG_MSG (UNISCRIBE, face, "Face has empty blob"); michael@0: michael@0: blob = _hb_rename_font (blob, data->face_name); michael@0: if (unlikely (!blob)) michael@0: { michael@0: free (data); michael@0: return NULL; michael@0: } michael@0: michael@0: DWORD num_fonts_installed; michael@0: data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, NULL), michael@0: hb_blob_get_length (blob), michael@0: 0, &num_fonts_installed); michael@0: if (unlikely (!data->fh)) michael@0: { michael@0: DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed"); michael@0: free (data); michael@0: return NULL; michael@0: } michael@0: michael@0: return data; michael@0: } michael@0: michael@0: void michael@0: _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data) michael@0: { michael@0: RemoveFontMemResourceEx (data->fh); michael@0: free (data); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper font data michael@0: */ michael@0: michael@0: struct hb_uniscribe_shaper_font_data_t { michael@0: HDC hdc; michael@0: LOGFONTW log_font; michael@0: HFONT hfont; michael@0: SCRIPT_CACHE script_cache; michael@0: }; michael@0: michael@0: static bool michael@0: populate_log_font (LOGFONTW *lf, michael@0: hb_font_t *font) michael@0: { michael@0: memset (lf, 0, sizeof (*lf)); michael@0: lf->lfHeight = -font->y_scale; michael@0: lf->lfCharSet = DEFAULT_CHARSET; michael@0: michael@0: hb_face_t *face = font->face; michael@0: hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face); michael@0: michael@0: memcpy (lf->lfFaceName, face_data->face_name, sizeof (lf->lfFaceName)); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: hb_uniscribe_shaper_font_data_t * michael@0: _hb_uniscribe_shaper_font_data_create (hb_font_t *font) michael@0: { michael@0: if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return NULL; michael@0: michael@0: hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t)); michael@0: if (unlikely (!data)) michael@0: return NULL; michael@0: michael@0: data->hdc = GetDC (NULL); michael@0: michael@0: if (unlikely (!populate_log_font (&data->log_font, font))) { michael@0: DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed"); michael@0: _hb_uniscribe_shaper_font_data_destroy (data); michael@0: return NULL; michael@0: } michael@0: michael@0: data->hfont = CreateFontIndirectW (&data->log_font); michael@0: if (unlikely (!data->hfont)) { michael@0: DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed"); michael@0: _hb_uniscribe_shaper_font_data_destroy (data); michael@0: return NULL; michael@0: } michael@0: michael@0: if (!SelectObject (data->hdc, data->hfont)) { michael@0: DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed"); michael@0: _hb_uniscribe_shaper_font_data_destroy (data); michael@0: return NULL; michael@0: } michael@0: michael@0: return data; michael@0: } michael@0: michael@0: void michael@0: _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data) michael@0: { michael@0: if (data->hdc) michael@0: ReleaseDC (NULL, data->hdc); michael@0: if (data->hfont) michael@0: DeleteObject (data->hfont); michael@0: if (data->script_cache) michael@0: ScriptFreeCache (&data->script_cache); michael@0: free (data); michael@0: } michael@0: michael@0: LOGFONTW * michael@0: hb_uniscribe_font_get_logfontw (hb_font_t *font) michael@0: { michael@0: if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL; michael@0: hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); michael@0: return &font_data->log_font; michael@0: } michael@0: michael@0: HFONT michael@0: hb_uniscribe_font_get_hfont (hb_font_t *font) michael@0: { michael@0: if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL; michael@0: hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); michael@0: return font_data->hfont; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper shape_plan data michael@0: */ michael@0: michael@0: struct hb_uniscribe_shaper_shape_plan_data_t {}; michael@0: michael@0: hb_uniscribe_shaper_shape_plan_data_t * michael@0: _hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED, michael@0: const hb_feature_t *user_features HB_UNUSED, michael@0: unsigned int num_user_features HB_UNUSED) michael@0: { michael@0: return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; michael@0: } michael@0: michael@0: void michael@0: _hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED) michael@0: { michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper michael@0: */ michael@0: michael@0: michael@0: hb_bool_t michael@0: _hb_uniscribe_shape (hb_shape_plan_t *shape_plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: const hb_feature_t *features, michael@0: unsigned int num_features) michael@0: { michael@0: hb_face_t *face = font->face; michael@0: hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face); michael@0: hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font); michael@0: hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs; michael@0: michael@0: /* michael@0: * Set up features. michael@0: */ michael@0: hb_auto_array_t feature_records; michael@0: hb_auto_array_t range_records; michael@0: if (num_features) michael@0: { michael@0: /* Sort features by start/end events. */ michael@0: hb_auto_array_t feature_events; michael@0: for (unsigned int i = 0; i < num_features; i++) michael@0: { michael@0: active_feature_t feature; michael@0: feature.rec.tagFeature = hb_uint32_swap (features[i].tag); michael@0: feature.rec.lParameter = features[i].value; michael@0: feature.order = i; michael@0: michael@0: feature_event_t *event; michael@0: michael@0: event = feature_events.push (); michael@0: if (unlikely (!event)) michael@0: goto fail_features; michael@0: event->index = features[i].start; michael@0: event->start = true; michael@0: event->feature = feature; michael@0: michael@0: event = feature_events.push (); michael@0: if (unlikely (!event)) michael@0: goto fail_features; michael@0: event->index = features[i].end; michael@0: event->start = false; michael@0: event->feature = feature; michael@0: } michael@0: feature_events.sort (); michael@0: /* Add a strategic final event. */ michael@0: { michael@0: active_feature_t feature; michael@0: feature.rec.tagFeature = 0; michael@0: feature.rec.lParameter = 0; michael@0: feature.order = num_features + 1; michael@0: michael@0: feature_event_t *event = feature_events.push (); michael@0: if (unlikely (!event)) michael@0: goto fail_features; michael@0: event->index = 0; /* This value does magic. */ michael@0: event->start = false; michael@0: event->feature = feature; michael@0: } michael@0: michael@0: /* Scan events and save features for each range. */ michael@0: hb_auto_array_t active_features; michael@0: unsigned int last_index = 0; michael@0: for (unsigned int i = 0; i < feature_events.len; i++) michael@0: { michael@0: feature_event_t *event = &feature_events[i]; michael@0: michael@0: if (event->index != last_index) michael@0: { michael@0: /* Save a snapshot of active features and the range. */ michael@0: range_record_t *range = range_records.push (); michael@0: if (unlikely (!range)) michael@0: goto fail_features; michael@0: michael@0: unsigned int offset = feature_records.len; michael@0: michael@0: active_features.sort (); michael@0: for (unsigned int j = 0; j < active_features.len; j++) michael@0: { michael@0: if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.len - 1].tagFeature) michael@0: { michael@0: OPENTYPE_FEATURE_RECORD *feature = feature_records.push (); michael@0: if (unlikely (!feature)) michael@0: goto fail_features; michael@0: *feature = active_features[j].rec; michael@0: } michael@0: else michael@0: { michael@0: /* Overrides value for existing feature. */ michael@0: feature_records[feature_records.len - 1].lParameter = active_features[j].rec.lParameter; michael@0: } michael@0: } michael@0: michael@0: /* Will convert to pointer after all is ready, since feature_records.array michael@0: * may move as we grow it. */ michael@0: range->props.potfRecords = reinterpret_cast (offset); michael@0: range->props.cotfRecords = feature_records.len - offset; michael@0: range->index_first = last_index; michael@0: range->index_last = event->index - 1; michael@0: michael@0: last_index = event->index; michael@0: } michael@0: michael@0: if (event->start) { michael@0: active_feature_t *feature = active_features.push (); michael@0: if (unlikely (!feature)) michael@0: goto fail_features; michael@0: *feature = event->feature; michael@0: } else { michael@0: active_feature_t *feature = active_features.find (&event->feature); michael@0: if (feature) michael@0: active_features.remove (feature - active_features.array); michael@0: } michael@0: } michael@0: michael@0: if (!range_records.len) /* No active feature found. */ michael@0: goto fail_features; michael@0: michael@0: /* Fixup the pointers. */ michael@0: for (unsigned int i = 0; i < range_records.len; i++) michael@0: { michael@0: range_record_t *range = &range_records[i]; michael@0: range->props.potfRecords = feature_records.array + reinterpret_cast (range->props.potfRecords); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: fail_features: michael@0: num_features = 0; michael@0: } michael@0: michael@0: #define FAIL(...) \ michael@0: HB_STMT_START { \ michael@0: DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \ michael@0: return false; \ michael@0: } HB_STMT_END; michael@0: michael@0: HRESULT hr; michael@0: michael@0: retry: michael@0: michael@0: unsigned int scratch_size; michael@0: hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size); michael@0: michael@0: #define ALLOCATE_ARRAY(Type, name, len) \ michael@0: Type *name = (Type *) scratch; \ michael@0: { \ michael@0: unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ michael@0: assert (_consumed <= scratch_size); \ michael@0: scratch += _consumed; \ michael@0: scratch_size -= _consumed; \ michael@0: } michael@0: michael@0: #define utf16_index() var1.u32 michael@0: michael@0: ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2); michael@0: michael@0: unsigned int chars_len = 0; michael@0: for (unsigned int i = 0; i < buffer->len; i++) michael@0: { michael@0: hb_codepoint_t c = buffer->info[i].codepoint; michael@0: buffer->info[i].utf16_index() = chars_len; michael@0: if (likely (c < 0x10000)) michael@0: pchars[chars_len++] = c; michael@0: else if (unlikely (c >= 0x110000)) michael@0: pchars[chars_len++] = 0xFFFD; michael@0: else { michael@0: pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10); michael@0: pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1)); michael@0: } michael@0: } michael@0: michael@0: ALLOCATE_ARRAY (WORD, log_clusters, chars_len); michael@0: ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len); michael@0: michael@0: if (num_features) michael@0: { michael@0: /* Need log_clusters to assign features. */ michael@0: chars_len = 0; michael@0: for (unsigned int i = 0; i < buffer->len; i++) michael@0: { michael@0: hb_codepoint_t c = buffer->info[i].codepoint; michael@0: unsigned int cluster = buffer->info[i].cluster; michael@0: log_clusters[chars_len++] = cluster; michael@0: if (c >= 0x10000 && c < 0x110000) michael@0: log_clusters[chars_len++] = cluster; /* Surrogates. */ michael@0: } michael@0: } michael@0: michael@0: /* The -2 in the following is to compensate for possible michael@0: * alignment needed after the WORD array. sizeof(WORD) == 2. */ michael@0: unsigned int glyphs_size = (scratch_size * sizeof (int) - 2) michael@0: / (sizeof (WORD) + michael@0: sizeof (SCRIPT_GLYPHPROP) + michael@0: sizeof (int) + michael@0: sizeof (GOFFSET) + michael@0: sizeof (uint32_t)); michael@0: michael@0: ALLOCATE_ARRAY (WORD, glyphs, glyphs_size); michael@0: ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size); michael@0: ALLOCATE_ARRAY (int, advances, glyphs_size); michael@0: ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size); michael@0: ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size); michael@0: michael@0: /* Note: michael@0: * We can't touch the contents of glyph_props. Our fallback michael@0: * implementations of Shape and Place functions use that buffer michael@0: * by casting it to a different type. It works because they michael@0: * both agree about it, but if we want to access it here we michael@0: * need address that issue first. michael@0: */ michael@0: michael@0: #undef ALLOCATE_ARRAY michael@0: michael@0: #define MAX_ITEMS 256 michael@0: michael@0: SCRIPT_ITEM items[MAX_ITEMS + 1]; michael@0: SCRIPT_CONTROL bidi_control = {0}; michael@0: SCRIPT_STATE bidi_state = {0}; michael@0: ULONG script_tags[MAX_ITEMS]; michael@0: int item_count; michael@0: michael@0: /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */ michael@0: //bidi_control.fMergeNeutralItems = true; michael@0: *(uint32_t*)&bidi_control |= 1<<24; michael@0: michael@0: bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; michael@0: bidi_state.fOverrideDirection = 1; michael@0: michael@0: hr = funcs->ScriptItemizeOpenType (pchars, michael@0: chars_len, michael@0: MAX_ITEMS, michael@0: &bidi_control, michael@0: &bidi_state, michael@0: items, michael@0: script_tags, michael@0: &item_count); michael@0: if (unlikely (FAILED (hr))) michael@0: FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr); michael@0: michael@0: #undef MAX_ITEMS michael@0: michael@0: OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language)); michael@0: hb_auto_array_t range_properties; michael@0: hb_auto_array_t range_char_counts; michael@0: michael@0: unsigned int glyphs_offset = 0; michael@0: unsigned int glyphs_len; michael@0: bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); michael@0: for (unsigned int i = 0; i < item_count; i++) michael@0: { michael@0: unsigned int chars_offset = items[i].iCharPos; michael@0: unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset; michael@0: michael@0: if (num_features) michael@0: { michael@0: range_properties.shrink (0); michael@0: range_char_counts.shrink (0); michael@0: michael@0: range_record_t *last_range = &range_records[0]; michael@0: michael@0: for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++) michael@0: { michael@0: range_record_t *range = last_range; michael@0: while (log_clusters[k] < range->index_first) michael@0: range--; michael@0: while (log_clusters[k] > range->index_last) michael@0: range++; michael@0: if (!range_properties.len || michael@0: &range->props != range_properties[range_properties.len - 1]) michael@0: { michael@0: TEXTRANGE_PROPERTIES **props = range_properties.push (); michael@0: int *c = range_char_counts.push (); michael@0: if (unlikely (!props || !c)) michael@0: { michael@0: range_properties.shrink (0); michael@0: range_char_counts.shrink (0); michael@0: break; michael@0: } michael@0: *props = &range->props; michael@0: *c = 1; michael@0: } michael@0: else michael@0: { michael@0: range_char_counts[range_char_counts.len - 1]++; michael@0: } michael@0: michael@0: last_range = range; michael@0: } michael@0: } michael@0: michael@0: /* Asking for glyphs in logical order circumvents at least michael@0: * one bug in Uniscribe. */ michael@0: items[i].a.fLogicalOrder = true; michael@0: michael@0: retry_shape: michael@0: hr = funcs->ScriptShapeOpenType (font_data->hdc, michael@0: &font_data->script_cache, michael@0: &items[i].a, michael@0: script_tags[i], michael@0: language_tag, michael@0: range_char_counts.array, michael@0: range_properties.array, michael@0: range_properties.len, michael@0: pchars + chars_offset, michael@0: item_chars_len, michael@0: glyphs_size - glyphs_offset, michael@0: /* out */ michael@0: log_clusters + chars_offset, michael@0: char_props + chars_offset, michael@0: glyphs + glyphs_offset, michael@0: glyph_props + glyphs_offset, michael@0: (int *) &glyphs_len); michael@0: michael@0: if (unlikely (items[i].a.fNoGlyphIndex)) michael@0: FAIL ("ScriptShapeOpenType() set fNoGlyphIndex"); michael@0: if (unlikely (hr == E_OUTOFMEMORY)) michael@0: { michael@0: buffer->ensure (buffer->allocated * 2); michael@0: if (buffer->in_error) michael@0: FAIL ("Buffer resize failed"); michael@0: goto retry; michael@0: } michael@0: if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT)) michael@0: { michael@0: if (items[i].a.eScript == SCRIPT_UNDEFINED) michael@0: FAIL ("ScriptShapeOpenType() failed: Font doesn't support script"); michael@0: items[i].a.eScript = SCRIPT_UNDEFINED; michael@0: goto retry_shape; michael@0: } michael@0: if (unlikely (FAILED (hr))) michael@0: { michael@0: FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr); michael@0: } michael@0: michael@0: for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++) michael@0: log_clusters[j] += glyphs_offset; michael@0: michael@0: hr = funcs->ScriptPlaceOpenType (font_data->hdc, michael@0: &font_data->script_cache, michael@0: &items[i].a, michael@0: script_tags[i], michael@0: language_tag, michael@0: range_char_counts.array, michael@0: range_properties.array, michael@0: range_properties.len, michael@0: pchars + chars_offset, michael@0: log_clusters + chars_offset, michael@0: char_props + chars_offset, michael@0: item_chars_len, michael@0: glyphs + glyphs_offset, michael@0: glyph_props + glyphs_offset, michael@0: glyphs_len, michael@0: /* out */ michael@0: advances + glyphs_offset, michael@0: offsets + glyphs_offset, michael@0: NULL); michael@0: if (unlikely (FAILED (hr))) michael@0: FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr); michael@0: michael@0: if (DEBUG_ENABLED (UNISCRIBE)) michael@0: fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n", michael@0: i, michael@0: items[i].a.fRTL, michael@0: items[i].a.fLayoutRTL, michael@0: items[i].a.fLogicalOrder, michael@0: HB_UNTAG (hb_uint32_swap (script_tags[i]))); michael@0: michael@0: glyphs_offset += glyphs_len; michael@0: } michael@0: glyphs_len = glyphs_offset; michael@0: michael@0: /* Ok, we've got everything we need, now compose output buffer, michael@0: * very, *very*, carefully! */ michael@0: michael@0: /* Calculate visual-clusters. That's what we ship. */ michael@0: for (unsigned int i = 0; i < glyphs_len; i++) michael@0: vis_clusters[i] = -1; michael@0: for (unsigned int i = 0; i < buffer->len; i++) { michael@0: uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]]; michael@0: *p = MIN (*p, buffer->info[i].cluster); michael@0: } michael@0: for (unsigned int i = 1; i < glyphs_len; i++) michael@0: if (vis_clusters[i] == -1) michael@0: vis_clusters[i] = vis_clusters[i - 1]; michael@0: michael@0: #undef utf16_index michael@0: michael@0: buffer->ensure (glyphs_len); michael@0: if (buffer->in_error) michael@0: FAIL ("Buffer in error"); michael@0: michael@0: #undef FAIL michael@0: michael@0: /* Set glyph infos */ michael@0: buffer->len = 0; michael@0: for (unsigned int i = 0; i < glyphs_len; i++) michael@0: { michael@0: hb_glyph_info_t *info = &buffer->info[buffer->len++]; michael@0: michael@0: info->codepoint = glyphs[i]; michael@0: info->cluster = vis_clusters[i]; michael@0: michael@0: /* The rest is crap. Let's store position info there for now. */ michael@0: info->mask = advances[i]; michael@0: info->var1.u32 = offsets[i].du; michael@0: info->var2.u32 = offsets[i].dv; michael@0: } michael@0: michael@0: /* Set glyph positions */ michael@0: buffer->clear_positions (); michael@0: for (unsigned int i = 0; i < glyphs_len; i++) michael@0: { michael@0: hb_glyph_info_t *info = &buffer->info[i]; michael@0: hb_glyph_position_t *pos = &buffer->pos[i]; michael@0: michael@0: /* TODO vertical */ michael@0: pos->x_advance = info->mask; michael@0: pos->x_offset = backward ? -info->var1.u32 : info->var1.u32; michael@0: pos->y_offset = info->var2.u32; michael@0: } michael@0: michael@0: if (backward) michael@0: hb_buffer_reverse (buffer); michael@0: michael@0: /* Wow, done! */ michael@0: return true; michael@0: } michael@0: michael@0: