2010-12-05 10:38:12 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2011-06-02 05:56:50 -07:00
|
|
|
#include "nsAlgorithm.h"
|
2009-10-07 10:16:52 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsBidiUtils.h"
|
|
|
|
#include "nsMathUtils.h"
|
|
|
|
|
|
|
|
#include "gfxTypes.h"
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "gfxHarfBuzzShaper.h"
|
|
|
|
#include "gfxFontUtils.h"
|
2012-02-24 02:15:46 -08:00
|
|
|
#include "nsUnicodeProperties.h"
|
|
|
|
#include "nsUnicodeScriptCodes.h"
|
2012-02-20 12:39:59 -08:00
|
|
|
#include "nsUnicodeNormalizer.h"
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2012-04-21 14:24:39 -07:00
|
|
|
#include "harfbuzz/hb.h"
|
2010-07-13 13:31:31 -07:00
|
|
|
#include "harfbuzz/hb-ot.h"
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2010-11-14 11:21:37 -08:00
|
|
|
#include "cairo.h"
|
|
|
|
|
2010-07-13 13:31:31 -07:00
|
|
|
#include "nsCRT.h"
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2011-04-20 23:29:50 -07:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
#endif
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
#define FloatToFixed(f) (65536 * (f))
|
|
|
|
#define FixedToFloat(f) ((f) * (1.0 / 65536.0))
|
2010-11-14 11:21:37 -08:00
|
|
|
// Right shifts of negative (signed) integers are undefined, as are overflows
|
|
|
|
// when converting unsigned to negative signed integers.
|
|
|
|
// (If speed were an issue we could make some 2's complement assumptions.)
|
|
|
|
#define FixedToIntRound(f) ((f) > 0 ? ((32768 + (f)) >> 16) \
|
|
|
|
: -((32767 - (f)) >> 16))
|
2009-10-07 10:16:52 -07:00
|
|
|
|
|
|
|
using namespace mozilla; // for AutoSwap_* types
|
2012-02-24 02:15:46 -08:00
|
|
|
using namespace mozilla::unicode; // for Unicode property lookup
|
2009-10-07 10:16:52 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Creation and destruction; on deletion, release any font tables we're holding
|
|
|
|
*/
|
|
|
|
|
|
|
|
gfxHarfBuzzShaper::gfxHarfBuzzShaper(gfxFont *aFont)
|
|
|
|
: gfxFontShaper(aFont),
|
2012-07-30 07:20:58 -07:00
|
|
|
mHBFace(nullptr),
|
|
|
|
mKernTable(nullptr),
|
|
|
|
mHmtxTable(nullptr),
|
2009-10-07 10:16:52 -07:00
|
|
|
mNumLongMetrics(0),
|
2012-07-30 07:20:58 -07:00
|
|
|
mCmapTable(nullptr),
|
2009-10-07 10:16:52 -07:00
|
|
|
mCmapFormat(-1),
|
|
|
|
mSubtableOffset(0),
|
|
|
|
mUVSTableOffset(0),
|
2010-12-08 23:28:51 -08:00
|
|
|
mUseFontGetGlyph(aFont->ProvidesGetGlyph()),
|
2011-10-17 07:59:28 -07:00
|
|
|
mUseFontGlyphWidths(false)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxHarfBuzzShaper::~gfxHarfBuzzShaper()
|
|
|
|
{
|
2010-06-15 23:37:37 -07:00
|
|
|
hb_blob_destroy(mCmapTable);
|
|
|
|
hb_blob_destroy(mHmtxTable);
|
2010-09-16 09:41:41 -07:00
|
|
|
hb_blob_destroy(mKernTable);
|
2010-06-15 23:37:37 -07:00
|
|
|
hb_face_destroy(mHBFace);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HarfBuzz callback access to font table data
|
|
|
|
*/
|
|
|
|
|
|
|
|
// callback for HarfBuzz to get a font table (in hb_blob_t form)
|
|
|
|
// from the shaper (passed as aUserData)
|
|
|
|
static hb_blob_t *
|
2012-02-14 00:03:01 -08:00
|
|
|
HBGetTable(hb_face_t *face, hb_tag_t aTag, void *aUserData)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
|
|
|
gfxHarfBuzzShaper *shaper = static_cast<gfxHarfBuzzShaper*>(aUserData);
|
2011-04-19 01:01:21 -07:00
|
|
|
gfxFont *font = shaper->GetFont();
|
|
|
|
|
|
|
|
// bug 589682 - ignore the GDEF table in buggy fonts (applies to
|
|
|
|
// Italic and BoldItalic faces of Times New Roman)
|
|
|
|
if (aTag == TRUETYPE_TAG('G','D','E','F') &&
|
2012-03-17 02:38:42 -07:00
|
|
|
font->GetFontEntry()->IgnoreGDEF()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-17 02:38:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// bug 721719 - ignore the GSUB table in buggy fonts (applies to Roboto,
|
|
|
|
// at least on some Android ICS devices; set in gfxFT2FontList.cpp)
|
|
|
|
if (aTag == TRUETYPE_TAG('G','S','U','B') &&
|
|
|
|
font->GetFontEntry()->IgnoreGSUB()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-04-19 01:01:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return font->GetFontTable(aTag);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-02-14 00:03:01 -08:00
|
|
|
* HarfBuzz font callback functions; font_data is a ptr to a
|
2009-10-07 10:16:52 -07:00
|
|
|
* FontCallbackData struct
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct FontCallbackData {
|
|
|
|
FontCallbackData(gfxHarfBuzzShaper *aShaper, gfxContext *aContext)
|
|
|
|
: mShaper(aShaper), mContext(aContext)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
gfxHarfBuzzShaper *mShaper;
|
|
|
|
gfxContext *mContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define UNICODE_BMP_LIMIT 0x10000
|
|
|
|
|
|
|
|
hb_codepoint_t
|
|
|
|
gfxHarfBuzzShaper::GetGlyph(hb_codepoint_t unicode,
|
|
|
|
hb_codepoint_t variation_selector) const
|
|
|
|
{
|
2010-12-08 23:28:51 -08:00
|
|
|
if (mUseFontGetGlyph) {
|
|
|
|
return mFont->GetGlyph(unicode, variation_selector);
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
// we only instantiate a harfbuzz shaper if there's a cmap available
|
|
|
|
NS_ASSERTION(mFont->GetFontEntry()->HasCmapTable(),
|
|
|
|
"we cannot be using this font!");
|
|
|
|
|
2010-06-15 23:37:37 -07:00
|
|
|
NS_ASSERTION(mCmapTable && (mCmapFormat > 0) && (mSubtableOffset > 0),
|
|
|
|
"cmap data not correctly set up, expect disaster");
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint8_t* data = (const uint8_t*)hb_blob_get_data(mCmapTable, nullptr);
|
2009-10-07 10:16:52 -07:00
|
|
|
|
|
|
|
hb_codepoint_t gid;
|
|
|
|
switch (mCmapFormat) {
|
|
|
|
case 4:
|
|
|
|
gid = unicode < UNICODE_BMP_LIMIT ?
|
|
|
|
gfxFontUtils::MapCharToGlyphFormat4(data + mSubtableOffset, unicode) : 0;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
gid = gfxFontUtils::MapCharToGlyphFormat12(data + mSubtableOffset, unicode);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("unsupported cmap format, glyphs will be missing");
|
|
|
|
gid = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gid && variation_selector && mUVSTableOffset) {
|
|
|
|
hb_codepoint_t varGID =
|
|
|
|
gfxFontUtils::MapUVSToGlyphFormat14(data + mUVSTableOffset,
|
|
|
|
unicode, variation_selector);
|
|
|
|
if (varGID) {
|
|
|
|
gid = varGID;
|
|
|
|
}
|
|
|
|
// else the variation sequence was not supported, use default mapping
|
|
|
|
// of the character code alone
|
|
|
|
}
|
|
|
|
|
|
|
|
return gid;
|
|
|
|
}
|
|
|
|
|
2012-02-14 00:03:01 -08:00
|
|
|
static hb_bool_t
|
|
|
|
HBGetGlyph(hb_font_t *font, void *font_data,
|
|
|
|
hb_codepoint_t unicode, hb_codepoint_t variation_selector,
|
|
|
|
hb_codepoint_t *glyph,
|
|
|
|
void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
|
|
|
const FontCallbackData *fcd =
|
2012-02-14 00:03:01 -08:00
|
|
|
static_cast<const FontCallbackData*>(font_data);
|
|
|
|
*glyph = fcd->mShaper->GetGlyph(unicode, variation_selector);
|
2012-05-21 13:05:36 -07:00
|
|
|
return *glyph != 0;
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct HMetricsHeader {
|
|
|
|
AutoSwap_PRUint32 tableVersionNumber;
|
|
|
|
AutoSwap_PRInt16 ascender;
|
|
|
|
AutoSwap_PRInt16 descender;
|
|
|
|
AutoSwap_PRInt16 lineGap;
|
|
|
|
AutoSwap_PRUint16 advanceWidthMax;
|
|
|
|
AutoSwap_PRInt16 minLeftSideBearing;
|
|
|
|
AutoSwap_PRInt16 minRightSideBearing;
|
|
|
|
AutoSwap_PRInt16 xMaxExtent;
|
|
|
|
AutoSwap_PRInt16 caretSlopeRise;
|
|
|
|
AutoSwap_PRInt16 caretSlopeRun;
|
|
|
|
AutoSwap_PRInt16 caretOffset;
|
|
|
|
AutoSwap_PRInt16 reserved[4];
|
|
|
|
AutoSwap_PRInt16 metricDataFormat;
|
|
|
|
AutoSwap_PRUint16 numberOfHMetrics;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HLongMetric {
|
|
|
|
AutoSwap_PRUint16 advanceWidth;
|
|
|
|
AutoSwap_PRInt16 lsb;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HMetrics {
|
|
|
|
HLongMetric metrics[1]; // actually numberOfHMetrics
|
|
|
|
// the variable-length metrics[] array is immediately followed by:
|
|
|
|
// AutoSwap_PRUint16 leftSideBearing[];
|
|
|
|
};
|
|
|
|
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_position_t
|
|
|
|
gfxHarfBuzzShaper::GetGlyphHAdvance(gfxContext *aContext,
|
|
|
|
hb_codepoint_t glyph) const
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
2011-01-21 02:35:21 -08:00
|
|
|
if (mUseFontGlyphWidths) {
|
2012-02-14 00:03:01 -08:00
|
|
|
return mFont->GetGlyphWidth(aContext, glyph);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// font did not implement GetHintedGlyphWidth, so get an unhinted value
|
|
|
|
// directly from the font tables
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION((mNumLongMetrics > 0) && mHmtxTable != nullptr,
|
2010-06-15 23:37:37 -07:00
|
|
|
"font is lacking metrics, we shouldn't be here");
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (glyph >= uint32_t(mNumLongMetrics)) {
|
2009-10-07 10:16:52 -07:00
|
|
|
glyph = mNumLongMetrics - 1;
|
|
|
|
}
|
|
|
|
|
2010-07-22 02:25:23 -07:00
|
|
|
// glyph must be valid now, because we checked during initialization
|
|
|
|
// that mNumLongMetrics is > 0, and that the hmtx table is large enough
|
|
|
|
// to contain mNumLongMetrics records
|
|
|
|
const HMetrics* hmtx =
|
2012-07-30 07:20:58 -07:00
|
|
|
reinterpret_cast<const HMetrics*>(hb_blob_get_data(mHmtxTable, nullptr));
|
2012-02-14 00:03:01 -08:00
|
|
|
return FloatToFixed(mFont->FUnitsToDevUnitsFactor() *
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t(hmtx->metrics[glyph].advanceWidth));
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 00:03:01 -08:00
|
|
|
static hb_position_t
|
|
|
|
HBGetGlyphHAdvance(hb_font_t *font, void *font_data,
|
|
|
|
hb_codepoint_t glyph, void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
|
|
|
const FontCallbackData *fcd =
|
2012-02-14 00:03:01 -08:00
|
|
|
static_cast<const FontCallbackData*>(font_data);
|
|
|
|
return fcd->mShaper->GetGlyphHAdvance(fcd->mContext, glyph);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static hb_bool_t
|
2012-02-14 00:03:01 -08:00
|
|
|
HBGetContourPoint(hb_font_t *font, void *font_data,
|
2009-10-07 10:16:52 -07:00
|
|
|
unsigned int point_index, hb_codepoint_t glyph,
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_position_t *x, hb_position_t *y,
|
|
|
|
void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
|
|
|
/* not yet implemented - no support for used of hinted contour points
|
|
|
|
to fine-tune anchor positions in GPOS AnchorFormat2 */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-28 06:36:07 -07:00
|
|
|
struct KernHeaderFmt0 {
|
|
|
|
AutoSwap_PRUint16 nPairs;
|
|
|
|
AutoSwap_PRUint16 searchRange;
|
|
|
|
AutoSwap_PRUint16 entrySelector;
|
|
|
|
AutoSwap_PRUint16 rangeShift;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KernPair {
|
|
|
|
AutoSwap_PRUint16 left;
|
|
|
|
AutoSwap_PRUint16 right;
|
|
|
|
AutoSwap_PRInt16 value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find a kern pair in a Format 0 subtable.
|
|
|
|
// The aSubtable parameter points to the subtable itself, NOT its header,
|
|
|
|
// as the header structure differs between Windows and Mac (v0 and v1.0)
|
|
|
|
// versions of the 'kern' table.
|
|
|
|
// aSubtableLen is the length of the subtable EXCLUDING its header.
|
|
|
|
// If the pair <aFirstGlyph,aSecondGlyph> is found, the kerning value is
|
|
|
|
// added to aValue, so that multiple subtables can accumulate a total
|
|
|
|
// kerning value for a given pair.
|
|
|
|
static void
|
|
|
|
GetKernValueFmt0(const void* aSubtable,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aSubtableLen,
|
|
|
|
uint16_t aFirstGlyph,
|
|
|
|
uint16_t aSecondGlyph,
|
|
|
|
int32_t& aValue,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsOverride = false,
|
|
|
|
bool aIsMinimum = false)
|
2010-07-28 06:36:07 -07:00
|
|
|
{
|
|
|
|
const KernHeaderFmt0* hdr =
|
|
|
|
reinterpret_cast<const KernHeaderFmt0*>(aSubtable);
|
|
|
|
|
|
|
|
const KernPair *lo = reinterpret_cast<const KernPair*>(hdr + 1);
|
2012-08-22 08:56:38 -07:00
|
|
|
const KernPair *hi = lo + uint16_t(hdr->nPairs);
|
2010-09-22 02:15:57 -07:00
|
|
|
const KernPair *limit = hi;
|
2010-07-28 06:36:07 -07:00
|
|
|
|
|
|
|
if (reinterpret_cast<const char*>(aSubtable) + aSubtableLen <
|
|
|
|
reinterpret_cast<const char*>(hi)) {
|
|
|
|
// subtable is not large enough to contain the claimed number
|
|
|
|
// of kern pairs, so just ignore it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
#define KERN_PAIR_KEY(l,r) (uint32_t((uint16_t(l) << 16) + uint16_t(r)))
|
2010-07-28 06:36:07 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t key = KERN_PAIR_KEY(aFirstGlyph, aSecondGlyph);
|
2010-07-28 06:36:07 -07:00
|
|
|
while (lo < hi) {
|
|
|
|
const KernPair *mid = lo + (hi - lo) / 2;
|
|
|
|
if (KERN_PAIR_KEY(mid->left, mid->right) < key) {
|
|
|
|
lo = mid + 1;
|
|
|
|
} else {
|
|
|
|
hi = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-22 02:15:57 -07:00
|
|
|
if (lo < limit && KERN_PAIR_KEY(lo->left, lo->right) == key) {
|
2010-07-28 06:36:07 -07:00
|
|
|
if (aIsOverride) {
|
2012-08-22 08:56:38 -07:00
|
|
|
aValue = int16_t(lo->value);
|
2010-07-28 06:36:07 -07:00
|
|
|
} else if (aIsMinimum) {
|
2012-08-22 08:56:38 -07:00
|
|
|
aValue = NS_MAX(aValue, int32_t(lo->value));
|
2010-07-28 06:36:07 -07:00
|
|
|
} else {
|
2012-08-22 08:56:38 -07:00
|
|
|
aValue += int16_t(lo->value);
|
2010-07-28 06:36:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get kerning value from Apple (version 1.0) kern table,
|
|
|
|
// subtable format 2 (simple N x M array of kerning values)
|
|
|
|
|
|
|
|
// See http://developer.apple.com/fonts/TTRefMan/RM06/Chap6kern.html
|
|
|
|
// for details of version 1.0 format 2 subtable.
|
|
|
|
|
|
|
|
struct KernHeaderVersion1Fmt2 {
|
|
|
|
KernTableSubtableHeaderVersion1 header;
|
|
|
|
AutoSwap_PRUint16 rowWidth;
|
|
|
|
AutoSwap_PRUint16 leftOffsetTable;
|
|
|
|
AutoSwap_PRUint16 rightOffsetTable;
|
|
|
|
AutoSwap_PRUint16 array;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KernClassTableHdr {
|
|
|
|
AutoSwap_PRUint16 firstGlyph;
|
|
|
|
AutoSwap_PRUint16 nGlyphs;
|
|
|
|
AutoSwap_PRUint16 offsets[1]; // actually an array of nGlyphs entries
|
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static int16_t
|
2010-07-28 06:36:07 -07:00
|
|
|
GetKernValueVersion1Fmt2(const void* aSubtable,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aSubtableLen,
|
|
|
|
uint16_t aFirstGlyph,
|
|
|
|
uint16_t aSecondGlyph)
|
2010-07-28 06:36:07 -07:00
|
|
|
{
|
|
|
|
if (aSubtableLen < sizeof(KernHeaderVersion1Fmt2)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* base = reinterpret_cast<const char*>(aSubtable);
|
|
|
|
const char* subtableEnd = base + aSubtableLen;
|
|
|
|
|
|
|
|
const KernHeaderVersion1Fmt2* h =
|
|
|
|
reinterpret_cast<const KernHeaderVersion1Fmt2*>(aSubtable);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t offset = h->array;
|
2010-07-28 06:36:07 -07:00
|
|
|
|
|
|
|
const KernClassTableHdr* leftClassTable =
|
|
|
|
reinterpret_cast<const KernClassTableHdr*>(base +
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t(h->leftOffsetTable));
|
2010-07-28 06:36:07 -07:00
|
|
|
if (reinterpret_cast<const char*>(leftClassTable) +
|
|
|
|
sizeof(KernClassTableHdr) > subtableEnd) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
if (aFirstGlyph >= uint16_t(leftClassTable->firstGlyph)) {
|
|
|
|
aFirstGlyph -= uint16_t(leftClassTable->firstGlyph);
|
|
|
|
if (aFirstGlyph < uint16_t(leftClassTable->nGlyphs)) {
|
2010-07-28 06:36:07 -07:00
|
|
|
if (reinterpret_cast<const char*>(leftClassTable) +
|
|
|
|
sizeof(KernClassTableHdr) +
|
2012-08-22 08:56:38 -07:00
|
|
|
aFirstGlyph * sizeof(uint16_t) >= subtableEnd) {
|
2010-07-28 06:36:07 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
offset = uint16_t(leftClassTable->offsets[aFirstGlyph]);
|
2010-07-28 06:36:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const KernClassTableHdr* rightClassTable =
|
|
|
|
reinterpret_cast<const KernClassTableHdr*>(base +
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t(h->rightOffsetTable));
|
2010-07-28 06:36:07 -07:00
|
|
|
if (reinterpret_cast<const char*>(rightClassTable) +
|
|
|
|
sizeof(KernClassTableHdr) > subtableEnd) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
if (aSecondGlyph >= uint16_t(rightClassTable->firstGlyph)) {
|
|
|
|
aSecondGlyph -= uint16_t(rightClassTable->firstGlyph);
|
|
|
|
if (aSecondGlyph < uint16_t(rightClassTable->nGlyphs)) {
|
2010-07-28 06:36:07 -07:00
|
|
|
if (reinterpret_cast<const char*>(rightClassTable) +
|
|
|
|
sizeof(KernClassTableHdr) +
|
2012-08-22 08:56:38 -07:00
|
|
|
aSecondGlyph * sizeof(uint16_t) >= subtableEnd) {
|
2010-07-28 06:36:07 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
offset += uint16_t(rightClassTable->offsets[aSecondGlyph]);
|
2010-07-28 06:36:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const AutoSwap_PRInt16* pval =
|
|
|
|
reinterpret_cast<const AutoSwap_PRInt16*>(base + offset);
|
|
|
|
if (reinterpret_cast<const char*>(pval + 1) >= subtableEnd) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return *pval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get kerning value from Apple (version 1.0) kern table,
|
|
|
|
// subtable format 3 (simple N x M array of kerning values)
|
|
|
|
|
|
|
|
// See http://developer.apple.com/fonts/TTRefMan/RM06/Chap6kern.html
|
|
|
|
// for details of version 1.0 format 3 subtable.
|
|
|
|
|
|
|
|
struct KernHeaderVersion1Fmt3 {
|
|
|
|
KernTableSubtableHeaderVersion1 header;
|
|
|
|
AutoSwap_PRUint16 glyphCount;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t kernValueCount;
|
|
|
|
uint8_t leftClassCount;
|
|
|
|
uint8_t rightClassCount;
|
|
|
|
uint8_t flags;
|
2010-07-28 06:36:07 -07:00
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static int16_t
|
2010-07-28 06:36:07 -07:00
|
|
|
GetKernValueVersion1Fmt3(const void* aSubtable,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aSubtableLen,
|
|
|
|
uint16_t aFirstGlyph,
|
|
|
|
uint16_t aSecondGlyph)
|
2010-07-28 06:36:07 -07:00
|
|
|
{
|
|
|
|
// check that we can safely read the header fields
|
|
|
|
if (aSubtableLen < sizeof(KernHeaderVersion1Fmt3)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const KernHeaderVersion1Fmt3* hdr =
|
|
|
|
reinterpret_cast<const KernHeaderVersion1Fmt3*>(aSubtable);
|
|
|
|
if (hdr->flags != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t glyphCount = hdr->glyphCount;
|
2010-07-28 06:36:07 -07:00
|
|
|
|
|
|
|
// check that table is large enough for the arrays
|
|
|
|
if (sizeof(KernHeaderVersion1Fmt3) +
|
2012-08-22 08:56:38 -07:00
|
|
|
hdr->kernValueCount * sizeof(int16_t) +
|
2010-07-28 06:36:07 -07:00
|
|
|
glyphCount + glyphCount +
|
|
|
|
hdr->leftClassCount * hdr->rightClassCount > aSubtableLen) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFirstGlyph >= glyphCount || aSecondGlyph >= glyphCount) {
|
|
|
|
// glyphs are out of range for the class tables
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get pointers to the four arrays within the subtable
|
|
|
|
const AutoSwap_PRInt16* kernValue =
|
|
|
|
reinterpret_cast<const AutoSwap_PRInt16*>(hdr + 1);
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint8_t* leftClass =
|
|
|
|
reinterpret_cast<const uint8_t*>(kernValue + hdr->kernValueCount);
|
|
|
|
const uint8_t* rightClass = leftClass + glyphCount;
|
|
|
|
const uint8_t* kernIndex = rightClass + glyphCount;
|
2010-07-28 06:36:07 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t lc = leftClass[aFirstGlyph];
|
|
|
|
uint8_t rc = rightClass[aSecondGlyph];
|
2010-07-28 06:36:07 -07:00
|
|
|
if (lc >= hdr->leftClassCount || rc >= hdr->rightClassCount) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t ki = kernIndex[leftClass[aFirstGlyph] * hdr->rightClassCount +
|
2010-07-28 06:36:07 -07:00
|
|
|
rightClass[aSecondGlyph]];
|
|
|
|
if (ki >= hdr->kernValueCount) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kernValue[ki];
|
|
|
|
}
|
|
|
|
|
|
|
|
#define KERN0_COVERAGE_HORIZONTAL 0x0001
|
|
|
|
#define KERN0_COVERAGE_MINIMUM 0x0002
|
|
|
|
#define KERN0_COVERAGE_CROSS_STREAM 0x0004
|
|
|
|
#define KERN0_COVERAGE_OVERRIDE 0x0008
|
|
|
|
#define KERN0_COVERAGE_RESERVED 0x00F0
|
|
|
|
|
|
|
|
#define KERN1_COVERAGE_VERTICAL 0x8000
|
|
|
|
#define KERN1_COVERAGE_CROSS_STREAM 0x4000
|
|
|
|
#define KERN1_COVERAGE_VARIATION 0x2000
|
|
|
|
#define KERN1_COVERAGE_RESERVED 0x1F00
|
|
|
|
|
|
|
|
hb_position_t
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxHarfBuzzShaper::GetHKerning(uint16_t aFirstGlyph,
|
|
|
|
uint16_t aSecondGlyph) const
|
2010-07-28 06:36:07 -07:00
|
|
|
{
|
|
|
|
// We want to ignore any kern pairs involving <space>, because we are
|
|
|
|
// handling words in isolation, the only space characters seen here are
|
|
|
|
// the ones artificially added by the textRun code.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t spaceGlyph = mFont->GetSpaceGlyph();
|
2010-07-28 06:36:07 -07:00
|
|
|
if (aFirstGlyph == spaceGlyph || aSecondGlyph == spaceGlyph) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mKernTable) {
|
|
|
|
mKernTable = mFont->GetFontTable(TRUETYPE_TAG('k','e','r','n'));
|
|
|
|
if (!mKernTable) {
|
2012-02-14 00:03:01 -08:00
|
|
|
mKernTable = hb_blob_get_empty();
|
2010-07-28 06:36:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2012-02-14 00:03:01 -08:00
|
|
|
const char* base = hb_blob_get_data(mKernTable, &len);
|
2010-07-28 06:36:07 -07:00
|
|
|
if (len < sizeof(KernTableVersion0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t value = 0;
|
2010-07-28 06:36:07 -07:00
|
|
|
|
|
|
|
// First try to interpret as "version 0" kern table
|
|
|
|
// (see http://www.microsoft.com/typography/otspec/kern.htm)
|
|
|
|
const KernTableVersion0* kern0 =
|
|
|
|
reinterpret_cast<const KernTableVersion0*>(base);
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint16_t(kern0->version) == 0) {
|
|
|
|
uint16_t nTables = kern0->nTables;
|
|
|
|
uint32_t offs = sizeof(KernTableVersion0);
|
|
|
|
for (uint16_t i = 0; i < nTables; ++i) {
|
2010-07-28 06:36:07 -07:00
|
|
|
if (offs + sizeof(KernTableSubtableHeaderVersion0) > len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const KernTableSubtableHeaderVersion0* st0 =
|
|
|
|
reinterpret_cast<const KernTableSubtableHeaderVersion0*>
|
|
|
|
(base + offs);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t subtableLen = uint16_t(st0->length);
|
2010-07-28 06:36:07 -07:00
|
|
|
if (offs + subtableLen > len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offs += subtableLen;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t coverage = st0->coverage;
|
2010-07-28 06:36:07 -07:00
|
|
|
if (!(coverage & KERN0_COVERAGE_HORIZONTAL)) {
|
|
|
|
// we only care about horizontal kerning (for now)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (coverage &
|
|
|
|
(KERN0_COVERAGE_CROSS_STREAM | KERN0_COVERAGE_RESERVED)) {
|
|
|
|
// we don't support cross-stream kerning, and
|
|
|
|
// reserved bits should be zero;
|
|
|
|
// ignore the subtable if not
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t format = (coverage >> 8);
|
2010-07-28 06:36:07 -07:00
|
|
|
switch (format) {
|
|
|
|
case 0:
|
|
|
|
GetKernValueFmt0(st0 + 1, subtableLen - sizeof(*st0),
|
|
|
|
aFirstGlyph, aSecondGlyph, value,
|
|
|
|
(coverage & KERN0_COVERAGE_OVERRIDE) != 0,
|
|
|
|
(coverage & KERN0_COVERAGE_MINIMUM) != 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO: implement support for other formats,
|
|
|
|
// if they're ever used in practice
|
|
|
|
#if DEBUG
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
sprintf(buf, "unknown kern subtable in %s: "
|
|
|
|
"ver 0 format %d\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mFont->GetName()).get(),
|
|
|
|
format);
|
|
|
|
NS_WARNING(buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// It wasn't a "version 0" table; check if it is Apple version 1.0
|
|
|
|
// (see http://developer.apple.com/fonts/TTRefMan/RM06/Chap6kern.html)
|
|
|
|
const KernTableVersion1* kern1 =
|
|
|
|
reinterpret_cast<const KernTableVersion1*>(base);
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(kern1->version) == 0x00010000) {
|
|
|
|
uint32_t nTables = kern1->nTables;
|
|
|
|
uint32_t offs = sizeof(KernTableVersion1);
|
|
|
|
for (uint32_t i = 0; i < nTables; ++i) {
|
2010-07-28 06:36:07 -07:00
|
|
|
if (offs + sizeof(KernTableSubtableHeaderVersion1) > len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const KernTableSubtableHeaderVersion1* st1 =
|
|
|
|
reinterpret_cast<const KernTableSubtableHeaderVersion1*>
|
|
|
|
(base + offs);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t subtableLen = uint32_t(st1->length);
|
2010-07-28 06:36:07 -07:00
|
|
|
offs += subtableLen;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t coverage = st1->coverage;
|
2010-07-28 06:36:07 -07:00
|
|
|
if (coverage &
|
|
|
|
(KERN1_COVERAGE_VERTICAL |
|
|
|
|
KERN1_COVERAGE_CROSS_STREAM |
|
|
|
|
KERN1_COVERAGE_VARIATION |
|
|
|
|
KERN1_COVERAGE_RESERVED)) {
|
|
|
|
// we only care about horizontal kerning (for now),
|
|
|
|
// we don't support cross-stream kerning,
|
|
|
|
// we don't support variations,
|
|
|
|
// reserved bits should be zero;
|
|
|
|
// ignore the subtable if not
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t format = (coverage & 0xff);
|
2010-07-28 06:36:07 -07:00
|
|
|
switch (format) {
|
|
|
|
case 0:
|
|
|
|
GetKernValueFmt0(st1 + 1, subtableLen - sizeof(*st1),
|
|
|
|
aFirstGlyph, aSecondGlyph, value);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
value = GetKernValueVersion1Fmt2(st1, subtableLen,
|
|
|
|
aFirstGlyph, aSecondGlyph);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
value = GetKernValueVersion1Fmt3(st1, subtableLen,
|
|
|
|
aFirstGlyph, aSecondGlyph);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// TODO: implement support for other formats.
|
|
|
|
// Note that format 1 cannot be supported here,
|
|
|
|
// as it requires the full glyph array to run the FSM,
|
|
|
|
// not just the current glyph pair.
|
|
|
|
#if DEBUG
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
sprintf(buf, "unknown kern subtable in %s: "
|
|
|
|
"ver 0 format %d\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mFont->GetName()).get(),
|
|
|
|
format);
|
|
|
|
NS_WARNING(buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != 0) {
|
|
|
|
return FloatToFixed(mFont->FUnitsToDevUnitsFactor() * value);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
static hb_position_t
|
2012-02-14 00:03:01 -08:00
|
|
|
HBGetHKerning(hb_font_t *font, void *font_data,
|
|
|
|
hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
|
|
|
|
void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
2010-07-28 06:36:07 -07:00
|
|
|
const FontCallbackData *fcd =
|
2012-02-14 00:03:01 -08:00
|
|
|
static_cast<const FontCallbackData*>(font_data);
|
|
|
|
return fcd->mShaper->GetHKerning(first_glyph, second_glyph);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HarfBuzz unicode property callbacks
|
|
|
|
*/
|
|
|
|
|
|
|
|
static hb_codepoint_t
|
2012-08-06 03:42:20 -07:00
|
|
|
HBGetMirroring(hb_unicode_funcs_t *ufuncs, hb_codepoint_t aCh,
|
|
|
|
void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
2012-02-24 02:15:46 -08:00
|
|
|
return GetMirroredChar(aCh);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
2012-02-14 00:03:01 -08:00
|
|
|
static hb_unicode_general_category_t
|
2012-08-06 03:42:20 -07:00
|
|
|
HBGetGeneralCategory(hb_unicode_funcs_t *ufuncs, hb_codepoint_t aCh,
|
|
|
|
void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
2012-02-24 02:15:46 -08:00
|
|
|
return hb_unicode_general_category_t(GetGeneralCategory(aCh));
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static hb_script_t
|
2012-02-14 00:03:01 -08:00
|
|
|
HBGetScript(hb_unicode_funcs_t *ufuncs, hb_codepoint_t aCh, void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
2012-08-06 03:42:20 -07:00
|
|
|
return hb_script_t(GetScriptTagForCode(GetScriptCode(aCh)));
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
2012-08-06 03:42:20 -07:00
|
|
|
static hb_unicode_combining_class_t
|
|
|
|
HBGetCombiningClass(hb_unicode_funcs_t *ufuncs, hb_codepoint_t aCh,
|
|
|
|
void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
2012-08-06 03:42:20 -07:00
|
|
|
return hb_unicode_combining_class_t(GetCombiningClass(aCh));
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
2012-08-06 03:42:20 -07:00
|
|
|
HBGetEastAsianWidth(hb_unicode_funcs_t *ufuncs, hb_codepoint_t aCh,
|
|
|
|
void *user_data)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
2012-02-24 02:15:46 -08:00
|
|
|
return GetEastAsianWidth(aCh);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
2012-02-20 12:39:59 -08:00
|
|
|
// Hebrew presentation forms with dagesh, for characters 0x05D0..0x05EA;
|
|
|
|
// note that some letters do not have a dagesh presForm encoded
|
|
|
|
static const PRUnichar sDageshForms[0x05EA - 0x05D0 + 1] = {
|
|
|
|
0xFB30, // ALEF
|
|
|
|
0xFB31, // BET
|
|
|
|
0xFB32, // GIMEL
|
|
|
|
0xFB33, // DALET
|
|
|
|
0xFB34, // HE
|
|
|
|
0xFB35, // VAV
|
|
|
|
0xFB36, // ZAYIN
|
|
|
|
0, // HET
|
|
|
|
0xFB38, // TET
|
|
|
|
0xFB39, // YOD
|
|
|
|
0xFB3A, // FINAL KAF
|
|
|
|
0xFB3B, // KAF
|
|
|
|
0xFB3C, // LAMED
|
|
|
|
0, // FINAL MEM
|
|
|
|
0xFB3E, // MEM
|
|
|
|
0, // FINAL NUN
|
|
|
|
0xFB40, // NUN
|
|
|
|
0xFB41, // SAMEKH
|
|
|
|
0, // AYIN
|
|
|
|
0xFB43, // FINAL PE
|
|
|
|
0xFB44, // PE
|
|
|
|
0, // FINAL TSADI
|
|
|
|
0xFB46, // TSADI
|
|
|
|
0xFB47, // QOF
|
|
|
|
0xFB48, // RESH
|
|
|
|
0xFB49, // SHIN
|
|
|
|
0xFB4A // TAV
|
|
|
|
};
|
|
|
|
|
2012-02-20 12:39:59 -08:00
|
|
|
static hb_bool_t
|
|
|
|
HBUnicodeCompose(hb_unicode_funcs_t *ufuncs,
|
|
|
|
hb_codepoint_t a,
|
|
|
|
hb_codepoint_t b,
|
|
|
|
hb_codepoint_t *ab,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-02-20 12:39:59 -08:00
|
|
|
hb_bool_t found = nsUnicodeNormalizer::Compose(a, b, ab);
|
|
|
|
|
|
|
|
if (!found && (b & 0x1fff80) == 0x0580) {
|
|
|
|
// special-case Hebrew presentation forms that are excluded from
|
|
|
|
// standard normalization, but wanted for old fonts
|
|
|
|
switch (b) {
|
|
|
|
case 0x05B4: // HIRIQ
|
|
|
|
if (a == 0x05D9) { // YOD
|
|
|
|
*ab = 0xFB1D;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x05B7: // patah
|
|
|
|
if (a == 0x05F2) { // YIDDISH YOD YOD
|
|
|
|
*ab = 0xFB1F;
|
|
|
|
found = true;
|
|
|
|
} else if (a == 0x05D0) { // ALEF
|
|
|
|
*ab = 0xFB2E;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x05B8: // QAMATS
|
|
|
|
if (a == 0x05D0) { // ALEF
|
|
|
|
*ab = 0xFB2F;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x05B9: // HOLAM
|
|
|
|
if (a == 0x05D5) { // VAV
|
|
|
|
*ab = 0xFB4B;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x05BC: // DAGESH
|
|
|
|
if (a >= 0x05D0 && a <= 0x05EA) {
|
|
|
|
*ab = sDageshForms[a - 0x05D0];
|
|
|
|
found = (*ab != 0);
|
|
|
|
} else if (a == 0xFB2A) { // SHIN WITH SHIN DOT
|
|
|
|
*ab = 0xFB2C;
|
|
|
|
found = true;
|
|
|
|
} else if (a == 0xFB2B) { // SHIN WITH SIN DOT
|
|
|
|
*ab = 0xFB2D;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x05BF: // RAFE
|
|
|
|
switch (a) {
|
|
|
|
case 0x05D1: // BET
|
|
|
|
*ab = 0xFB4C;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
case 0x05DB: // KAF
|
|
|
|
*ab = 0xFB4D;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
case 0x05E4: // PE
|
|
|
|
*ab = 0xFB4E;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x05C1: // SHIN DOT
|
|
|
|
if (a == 0x05E9) { // SHIN
|
|
|
|
*ab = 0xFB2A;
|
|
|
|
found = true;
|
|
|
|
} else if (a == 0xFB49) { // SHIN WITH DAGESH
|
|
|
|
*ab = 0xFB2C;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x05C2: // SIN DOT
|
|
|
|
if (a == 0x05E9) { // SHIN
|
|
|
|
*ab = 0xFB2B;
|
|
|
|
found = true;
|
|
|
|
} else if (a == 0xFB49) { // SHIN WITH DAGESH
|
|
|
|
*ab = 0xFB2D;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
2012-02-20 12:39:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static hb_bool_t
|
|
|
|
HBUnicodeDecompose(hb_unicode_funcs_t *ufuncs,
|
|
|
|
hb_codepoint_t ab,
|
|
|
|
hb_codepoint_t *a,
|
|
|
|
hb_codepoint_t *b,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
return nsUnicodeNormalizer::DecomposeNonRecursively(ab, a, b);
|
|
|
|
}
|
|
|
|
|
2012-04-25 23:26:03 -07:00
|
|
|
static PLDHashOperator
|
2012-08-22 08:56:38 -07:00
|
|
|
AddFeature(const uint32_t& aTag, uint32_t& aValue, void *aUserArg)
|
2012-04-25 23:26:03 -07:00
|
|
|
{
|
|
|
|
nsTArray<hb_feature_t>* features = static_cast<nsTArray<hb_feature_t>*> (aUserArg);
|
|
|
|
|
|
|
|
hb_feature_t feat = { 0, 0, 0, UINT_MAX };
|
|
|
|
feat.tag = aTag;
|
|
|
|
feat.value = aValue;
|
|
|
|
features->AppendElement(feat);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
/*
|
|
|
|
* gfxFontShaper override to initialize the text run using HarfBuzz
|
|
|
|
*/
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
static hb_font_funcs_t * sHBFontFuncs = nullptr;
|
|
|
|
static hb_unicode_funcs_t * sHBUnicodeFuncs = nullptr;
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-12-06 04:39:19 -08:00
|
|
|
gfxHarfBuzzShaper::ShapeWord(gfxContext *aContext,
|
|
|
|
gfxShapedWord *aShapedWord,
|
|
|
|
const PRUnichar *aText)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
|
|
|
// some font back-ends require this in order to get proper hinted metrics
|
2012-04-17 01:50:26 -07:00
|
|
|
if (!mFont->SetupCairoFont(aContext)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-07 10:16:52 -07:00
|
|
|
|
|
|
|
if (!mHBFace) {
|
2011-01-21 08:44:33 -08:00
|
|
|
|
|
|
|
mUseFontGlyphWidths = mFont->ProvidesGlyphWidths();
|
|
|
|
|
2010-06-15 23:37:37 -07:00
|
|
|
// set up the harfbuzz face etc the first time we use the font
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
if (!sHBFontFuncs) {
|
|
|
|
// static function callback pointers, initialized by the first
|
|
|
|
// harfbuzz shaper used
|
2010-12-01 08:34:24 -08:00
|
|
|
sHBFontFuncs = hb_font_funcs_create();
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_font_funcs_set_glyph_func(sHBFontFuncs, HBGetGlyph,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_font_funcs_set_glyph_h_advance_func(sHBFontFuncs,
|
|
|
|
HBGetGlyphHAdvance,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_font_funcs_set_glyph_contour_point_func(sHBFontFuncs,
|
|
|
|
HBGetContourPoint,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_font_funcs_set_glyph_h_kerning_func(sHBFontFuncs,
|
|
|
|
HBGetHKerning,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2012-02-14 00:03:01 -08:00
|
|
|
|
|
|
|
sHBUnicodeFuncs =
|
|
|
|
hb_unicode_funcs_create(hb_unicode_funcs_get_empty());
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_unicode_funcs_set_mirroring_func(sHBUnicodeFuncs,
|
2012-02-14 00:03:01 -08:00
|
|
|
HBGetMirroring,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_unicode_funcs_set_script_func(sHBUnicodeFuncs, HBGetScript,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_unicode_funcs_set_general_category_func(sHBUnicodeFuncs,
|
2012-02-14 00:03:01 -08:00
|
|
|
HBGetGeneralCategory,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_unicode_funcs_set_combining_class_func(sHBUnicodeFuncs,
|
2012-02-14 00:03:01 -08:00
|
|
|
HBGetCombiningClass,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_unicode_funcs_set_eastasian_width_func(sHBUnicodeFuncs,
|
2012-02-14 00:03:01 -08:00
|
|
|
HBGetEastAsianWidth,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2012-02-20 12:39:59 -08:00
|
|
|
hb_unicode_funcs_set_compose_func(sHBUnicodeFuncs,
|
|
|
|
HBUnicodeCompose,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2012-02-20 12:39:59 -08:00
|
|
|
hb_unicode_funcs_set_decompose_func(sHBUnicodeFuncs,
|
|
|
|
HBUnicodeDecompose,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mHBFace = hb_face_create_for_tables(HBGetTable, this, nullptr);
|
2010-06-15 23:37:37 -07:00
|
|
|
|
2010-12-08 23:28:51 -08:00
|
|
|
if (!mUseFontGetGlyph) {
|
|
|
|
// get the cmap table and find offset to our subtable
|
|
|
|
mCmapTable = mFont->GetFontTable(TRUETYPE_TAG('c','m','a','p'));
|
|
|
|
if (!mCmapTable) {
|
|
|
|
NS_WARNING("failed to load cmap, glyphs will be missing");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-12-08 23:28:51 -08:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
|
|
|
const uint8_t* data = (const uint8_t*)hb_blob_get_data(mCmapTable, &len);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool symbol;
|
2010-12-08 23:28:51 -08:00
|
|
|
mCmapFormat = gfxFontUtils::
|
2012-02-14 00:03:01 -08:00
|
|
|
FindPreferredSubtable(data, len,
|
2010-12-08 23:28:51 -08:00
|
|
|
&mSubtableOffset, &mUVSTableOffset,
|
|
|
|
&symbol);
|
2010-06-15 23:37:37 -07:00
|
|
|
}
|
|
|
|
|
2011-01-21 02:35:21 -08:00
|
|
|
if (!mUseFontGlyphWidths) {
|
|
|
|
// if font doesn't implement GetGlyphWidth, we will be reading
|
2010-06-15 23:37:37 -07:00
|
|
|
// the hmtx table directly;
|
|
|
|
// read mNumLongMetrics from hhea table without caching its blob,
|
|
|
|
// and preload/cache the hmtx table
|
|
|
|
hb_blob_t *hheaTable =
|
|
|
|
mFont->GetFontTable(TRUETYPE_TAG('h','h','e','a'));
|
2012-02-14 00:03:01 -08:00
|
|
|
if (hheaTable) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2010-06-15 23:37:37 -07:00
|
|
|
const HMetricsHeader* hhea =
|
|
|
|
reinterpret_cast<const HMetricsHeader*>
|
2012-02-14 00:03:01 -08:00
|
|
|
(hb_blob_get_data(hheaTable, &len));
|
|
|
|
if (len >= sizeof(HMetricsHeader)) {
|
|
|
|
mNumLongMetrics = hhea->numberOfHMetrics;
|
|
|
|
if (mNumLongMetrics > 0 &&
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t(hhea->metricDataFormat) == 0) {
|
2012-02-14 00:03:01 -08:00
|
|
|
// no point reading hmtx if number of entries is zero!
|
|
|
|
// in that case, we won't be able to use this font
|
|
|
|
// (this method will return FALSE below if mHmtx is null)
|
|
|
|
mHmtxTable =
|
|
|
|
mFont->GetFontTable(TRUETYPE_TAG('h','m','t','x'));
|
|
|
|
if (hb_blob_get_length(mHmtxTable) <
|
|
|
|
mNumLongMetrics * sizeof(HLongMetric)) {
|
|
|
|
// hmtx table is not large enough for the claimed
|
|
|
|
// number of entries: invalid, do not use.
|
|
|
|
hb_blob_destroy(mHmtxTable);
|
2012-07-30 07:20:58 -07:00
|
|
|
mHmtxTable = nullptr;
|
2012-02-14 00:03:01 -08:00
|
|
|
}
|
2010-07-22 02:25:23 -07:00
|
|
|
}
|
|
|
|
}
|
2010-06-15 23:37:37 -07:00
|
|
|
}
|
|
|
|
hb_blob_destroy(hheaTable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 23:28:51 -08:00
|
|
|
if ((!mUseFontGetGlyph && mCmapFormat <= 0) ||
|
2011-01-21 02:35:21 -08:00
|
|
|
(!mUseFontGlyphWidths && !mHmtxTable)) {
|
2010-06-15 23:37:37 -07:00
|
|
|
// unable to shape with this font
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
FontCallbackData fcd(this, aContext);
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_font_t *font = hb_font_create(mHBFace);
|
2012-07-30 07:20:58 -07:00
|
|
|
hb_font_set_funcs(font, sHBFontFuncs, &fcd, nullptr);
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_font_set_ppem(font, mFont->GetAdjustedSize(), mFont->GetAdjustedSize());
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t scale = FloatToFixed(mFont->GetAdjustedSize()); // 16.16 fixed-point
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_font_set_scale(font, scale, scale);
|
|
|
|
|
|
|
|
nsAutoTArray<hb_feature_t,20> features;
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
gfxFontEntry *entry = mFont->GetFontEntry();
|
|
|
|
const gfxFontStyle *style = mFont->GetStyle();
|
2012-04-25 23:26:03 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDataHashtable<nsUint32HashKey,uint32_t> mergedFeatures;
|
2012-04-25 23:26:03 -07:00
|
|
|
|
|
|
|
if (MergeFontFeatures(style->featureSettings,
|
|
|
|
mFont->GetFontEntry()->mFeatureSettings,
|
|
|
|
aShapedWord->DisableLigatures(), mergedFeatures)) {
|
|
|
|
// enumerate result and insert into hb_feature array
|
|
|
|
mergedFeatures.Enumerate(AddFeature, &features);
|
2010-07-13 13:31:31 -07:00
|
|
|
}
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
bool isRightToLeft = aShapedWord->IsRightToLeft();
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_buffer_t *buffer = hb_buffer_create();
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_buffer_set_unicode_funcs(buffer, sHBUnicodeFuncs);
|
2011-12-06 04:39:19 -08:00
|
|
|
hb_buffer_set_direction(buffer, isRightToLeft ? HB_DIRECTION_RTL :
|
|
|
|
HB_DIRECTION_LTR);
|
2010-11-07 14:44:51 -08:00
|
|
|
// For unresolved "common" or "inherited" runs, default to Latin for now.
|
|
|
|
// (Should we somehow use the language or locale to try and infer
|
|
|
|
// a better default?)
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t scriptCode = aShapedWord->Script();
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_script_t scriptTag = (scriptCode <= MOZ_SCRIPT_INHERITED) ?
|
|
|
|
HB_SCRIPT_LATIN :
|
2012-02-24 02:15:46 -08:00
|
|
|
hb_script_t(GetScriptTagForCode(scriptCode));
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_buffer_set_script(buffer, scriptTag);
|
2010-12-08 23:28:51 -08:00
|
|
|
|
|
|
|
hb_language_t language;
|
|
|
|
if (style->languageOverride) {
|
|
|
|
language = hb_ot_tag_to_language(style->languageOverride);
|
2011-12-06 04:39:19 -08:00
|
|
|
} else if (entry->mLanguageOverride) {
|
|
|
|
language = hb_ot_tag_to_language(entry->mLanguageOverride);
|
2010-12-08 23:28:51 -08:00
|
|
|
} else {
|
|
|
|
nsCString langString;
|
|
|
|
style->language->ToUTF8String(langString);
|
2012-02-14 00:03:01 -08:00
|
|
|
language =
|
|
|
|
hb_language_from_string(langString.get(), langString.Length());
|
2010-12-08 23:28:51 -08:00
|
|
|
}
|
|
|
|
hb_buffer_set_language(buffer, language);
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t length = aShapedWord->Length();
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_buffer_add_utf16(buffer,
|
|
|
|
reinterpret_cast<const uint16_t*>(aText),
|
2011-12-06 04:39:19 -08:00
|
|
|
length, 0, length);
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2012-02-14 00:03:01 -08:00
|
|
|
hb_shape(font, buffer, features.Elements(), features.Length());
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
if (isRightToLeft) {
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_buffer_reverse(buffer);
|
|
|
|
}
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
nsresult rv = SetGlyphsFromRun(aContext, aShapedWord, buffer);
|
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "failed to store glyphs into gfxShapedWord");
|
2009-10-07 10:16:52 -07:00
|
|
|
hb_buffer_destroy(buffer);
|
|
|
|
hb_font_destroy(font);
|
|
|
|
|
2011-10-07 09:25:19 -07:00
|
|
|
return NS_SUCCEEDED(rv);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
2010-11-14 11:21:37 -08:00
|
|
|
/**
|
|
|
|
* Work out whether cairo will snap inter-glyph spacing to pixels.
|
|
|
|
*
|
|
|
|
* Layout does not align text to pixel boundaries, so, with font drawing
|
|
|
|
* backends that snap glyph positions to pixels, it is important that
|
|
|
|
* inter-glyph spacing within words is always an integer number of pixels.
|
|
|
|
* This ensures that the drawing backend snaps all of the word's glyphs in the
|
|
|
|
* same direction and so inter-glyph spacing remains the same.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
GetRoundOffsetsToPixels(gfxContext *aContext,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *aRoundX, bool *aRoundY)
|
2010-11-14 11:21:37 -08:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aRoundX = false;
|
2010-11-14 11:21:37 -08:00
|
|
|
// Could do something fancy here for ScaleFactors of
|
|
|
|
// AxisAlignedTransforms, but we leave things simple.
|
|
|
|
// Not much point rounding if a matrix will mess things up anyway.
|
|
|
|
if (aContext->CurrentMatrix().HasNonTranslation()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aRoundY = false;
|
2010-11-14 11:21:37 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All raster backends snap glyphs to pixels vertically.
|
|
|
|
// Print backends set CAIRO_HINT_METRICS_OFF.
|
2011-10-17 07:59:28 -07:00
|
|
|
*aRoundY = true;
|
2010-11-14 11:21:37 -08:00
|
|
|
|
|
|
|
cairo_t *cr = aContext->GetCairo();
|
|
|
|
cairo_scaled_font_t *scaled_font = cairo_get_scaled_font(cr);
|
|
|
|
// Sometimes hint metrics gets set for us, most notably for printing.
|
|
|
|
cairo_font_options_t *font_options = cairo_font_options_create();
|
|
|
|
cairo_scaled_font_get_font_options(scaled_font, font_options);
|
|
|
|
cairo_hint_metrics_t hint_metrics =
|
|
|
|
cairo_font_options_get_hint_metrics(font_options);
|
|
|
|
cairo_font_options_destroy(font_options);
|
|
|
|
|
|
|
|
switch (hint_metrics) {
|
|
|
|
case CAIRO_HINT_METRICS_OFF:
|
2011-10-17 07:59:28 -07:00
|
|
|
*aRoundY = false;
|
2010-11-14 11:21:37 -08:00
|
|
|
return;
|
|
|
|
case CAIRO_HINT_METRICS_DEFAULT:
|
|
|
|
// Here we mimic what cairo surface/font backends do. Printing
|
|
|
|
// surfaces have already been handled by hint_metrics. The
|
|
|
|
// fallback show_glyphs implementation composites pixel-aligned
|
|
|
|
// glyph surfaces, so we just pick surface/font combinations that
|
|
|
|
// override this.
|
|
|
|
switch (cairo_scaled_font_get_type(scaled_font)) {
|
2010-11-16 00:38:44 -08:00
|
|
|
#if CAIRO_HAS_DWRITE_FONT // dwrite backend is not in std cairo releases yet
|
2010-11-14 11:21:37 -08:00
|
|
|
case CAIRO_FONT_TYPE_DWRITE:
|
|
|
|
// show_glyphs is implemented on the font and so is used for
|
2011-04-20 23:29:50 -07:00
|
|
|
// all surface types; however, it may pixel-snap depending on
|
|
|
|
// the dwrite rendering mode
|
2011-06-02 21:31:07 -07:00
|
|
|
if (!cairo_dwrite_scaled_font_get_force_GDI_classic(scaled_font) &&
|
|
|
|
gfxWindowsPlatform::GetPlatform()->DWriteMeasuringMode() ==
|
|
|
|
DWRITE_MEASURING_MODE_NATURAL) {
|
2011-04-20 23:29:50 -07:00
|
|
|
return;
|
|
|
|
}
|
2010-11-16 00:38:44 -08:00
|
|
|
#endif
|
2010-11-14 11:21:37 -08:00
|
|
|
case CAIRO_FONT_TYPE_QUARTZ:
|
|
|
|
// Quartz surfaces implement show_glyphs for Quartz fonts
|
|
|
|
if (cairo_surface_get_type(cairo_get_target(cr)) ==
|
|
|
|
CAIRO_SURFACE_TYPE_QUARTZ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through:
|
|
|
|
case CAIRO_HINT_METRICS_ON:
|
|
|
|
break;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
*aRoundX = true;
|
2010-11-14 11:21:37 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
#define SMALL_GLYPH_RUN 128 // some testing indicates that 90%+ of text runs
|
|
|
|
// will fit without requiring separate allocation
|
|
|
|
// for charToGlyphArray
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxHarfBuzzShaper::SetGlyphsFromRun(gfxContext *aContext,
|
2011-12-06 04:39:19 -08:00
|
|
|
gfxShapedWord *aShapedWord,
|
|
|
|
hb_buffer_t *aBuffer)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numGlyphs;
|
2012-02-14 00:03:01 -08:00
|
|
|
const hb_glyph_info_t *ginfo = hb_buffer_get_glyph_infos(aBuffer, &numGlyphs);
|
2011-12-06 04:39:19 -08:00
|
|
|
if (numGlyphs == 0) {
|
2009-10-07 10:16:52 -07:00
|
|
|
return NS_OK;
|
2011-12-06 04:39:19 -08:00
|
|
|
}
|
2009-10-07 10:16:52 -07:00
|
|
|
|
|
|
|
nsAutoTArray<gfxTextRun::DetailedGlyph,1> detailedGlyphs;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t wordLength = aShapedWord->Length();
|
|
|
|
static const int32_t NO_GLYPH = -1;
|
|
|
|
nsAutoTArray<int32_t,SMALL_GLYPH_RUN> charToGlyphArray;
|
2011-12-06 04:39:19 -08:00
|
|
|
if (!charToGlyphArray.SetLength(wordLength)) {
|
2009-10-07 10:16:52 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2011-12-06 04:39:19 -08:00
|
|
|
}
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t *charToGlyph = charToGlyphArray.Elements();
|
|
|
|
for (uint32_t offset = 0; offset < wordLength; ++offset) {
|
2009-10-07 10:16:52 -07:00
|
|
|
charToGlyph[offset] = NO_GLYPH;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < numGlyphs; ++i) {
|
|
|
|
uint32_t loc = ginfo[i].cluster;
|
2011-12-06 04:39:19 -08:00
|
|
|
if (loc < wordLength) {
|
2009-10-07 10:16:52 -07:00
|
|
|
charToGlyph[loc] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t glyphStart = 0; // looking for a clump that starts at this glyph
|
|
|
|
int32_t charStart = 0; // and this char index within the range of the run
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool roundX;
|
|
|
|
bool roundY;
|
2010-11-14 11:21:37 -08:00
|
|
|
GetRoundOffsetsToPixels(aContext, &roundX, &roundY);
|
2011-12-06 04:39:19 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t appUnitsPerDevUnit = aShapedWord->AppUnitsPerDevUnit();
|
2011-12-06 04:39:19 -08:00
|
|
|
|
2010-07-28 06:36:07 -07:00
|
|
|
// factor to convert 16.16 fixed-point pixels to app units
|
2010-11-14 11:21:37 -08:00
|
|
|
// (only used if not rounding)
|
2011-12-06 04:39:19 -08:00
|
|
|
double hb2appUnits = FixedToFloat(aShapedWord->AppUnitsPerDevUnit());
|
2010-07-28 06:36:07 -07:00
|
|
|
|
2012-01-20 01:19:10 -08:00
|
|
|
// Residual from rounding of previous advance, for use in rounding the
|
|
|
|
// subsequent offset or advance appropriately. 16.16 fixed-point
|
|
|
|
//
|
|
|
|
// When rounding, the goal is to make the distance between glyphs and
|
|
|
|
// their base glyph equal to the integral number of pixels closest to that
|
|
|
|
// suggested by that shaper.
|
|
|
|
// i.e. posInfo[n].x_advance - posInfo[n].x_offset + posInfo[n+1].x_offset
|
|
|
|
//
|
|
|
|
// The value of the residual is the part of the desired distance that has
|
|
|
|
// not been included in integer offsets.
|
|
|
|
hb_position_t x_residual = 0;
|
|
|
|
|
2010-07-28 06:36:07 -07:00
|
|
|
// keep track of y-position to set glyph offsets if needed
|
|
|
|
nscoord yPos = 0;
|
|
|
|
|
2012-02-14 00:03:01 -08:00
|
|
|
const hb_glyph_position_t *posInfo =
|
2012-07-30 07:20:58 -07:00
|
|
|
hb_buffer_get_glyph_positions(aBuffer, nullptr);
|
2010-07-28 06:36:07 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
while (glyphStart < int32_t(numGlyphs)) {
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t charEnd = ginfo[glyphStart].cluster;
|
|
|
|
int32_t glyphEnd = glyphStart;
|
|
|
|
int32_t charLimit = wordLength;
|
2009-10-07 10:16:52 -07:00
|
|
|
while (charEnd < charLimit) {
|
|
|
|
// This is normally executed once for each iteration of the outer loop,
|
|
|
|
// but in unusual cases where the character/glyph association is complex,
|
|
|
|
// the initial character range might correspond to a non-contiguous
|
|
|
|
// glyph range with "holes" in it. If so, we will repeat this loop to
|
|
|
|
// extend the character range until we have a contiguous glyph sequence.
|
|
|
|
charEnd += 1;
|
|
|
|
while (charEnd != charLimit && charToGlyph[charEnd] == NO_GLYPH) {
|
|
|
|
charEnd += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the maximum glyph index covered by the clump so far
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = charStart; i < charEnd; ++i) {
|
2009-10-07 10:16:52 -07:00
|
|
|
if (charToGlyph[i] != NO_GLYPH) {
|
2011-06-02 05:56:50 -07:00
|
|
|
glyphEnd = NS_MAX(glyphEnd, charToGlyph[i] + 1);
|
2009-10-07 10:16:52 -07:00
|
|
|
// update extent of glyph range
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glyphEnd == glyphStart + 1) {
|
|
|
|
// for the common case of a single-glyph clump,
|
|
|
|
// we can skip the following checks
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glyphEnd == glyphStart) {
|
|
|
|
// no glyphs, try to extend the clump
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether all glyphs in the range are associated with the characters
|
|
|
|
// in our clump; if not, we have a discontinuous range, and should extend it
|
|
|
|
// unless we've reached the end of the text
|
2011-09-28 23:19:26 -07:00
|
|
|
bool allGlyphsAreWithinCluster = true;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = glyphStart; i < glyphEnd; ++i) {
|
|
|
|
int32_t glyphCharIndex = ginfo[i].cluster;
|
2009-10-07 10:16:52 -07:00
|
|
|
if (glyphCharIndex < charStart || glyphCharIndex >= charEnd) {
|
2011-10-17 07:59:28 -07:00
|
|
|
allGlyphsAreWithinCluster = false;
|
2009-10-07 10:16:52 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allGlyphsAreWithinCluster) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(glyphStart < glyphEnd,
|
|
|
|
"character/glyph clump contains no glyphs!");
|
|
|
|
NS_ASSERTION(charStart != charEnd,
|
|
|
|
"character/glyph clump contains no characters!");
|
|
|
|
|
|
|
|
// Now charStart..charEnd is a ligature clump, corresponding to glyphStart..glyphEnd;
|
|
|
|
// Set baseCharIndex to the char we'll actually attach the glyphs to (1st of ligature),
|
|
|
|
// and endCharIndex to the limit (position beyond the last char),
|
|
|
|
// adjusting for the offset of the stringRange relative to the textRun.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t baseCharIndex, endCharIndex;
|
|
|
|
while (charEnd < int32_t(wordLength) && charToGlyph[charEnd] == NO_GLYPH)
|
2009-10-07 10:16:52 -07:00
|
|
|
charEnd++;
|
|
|
|
baseCharIndex = charStart;
|
|
|
|
endCharIndex = charEnd;
|
|
|
|
|
|
|
|
// Then we check if the clump falls outside our actual string range;
|
|
|
|
// if so, just go to the next.
|
2012-08-22 08:56:38 -07:00
|
|
|
if (baseCharIndex >= int32_t(wordLength)) {
|
2009-10-07 10:16:52 -07:00
|
|
|
glyphStart = glyphEnd;
|
|
|
|
charStart = charEnd;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Ensure we won't try to go beyond the valid length of the textRun's text
|
2012-08-22 08:56:38 -07:00
|
|
|
endCharIndex = NS_MIN<int32_t>(endCharIndex, wordLength);
|
2009-10-07 10:16:52 -07:00
|
|
|
|
2010-07-28 06:36:07 -07:00
|
|
|
// Now we're ready to set the glyph info in the textRun
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t glyphsInClump = glyphEnd - glyphStart;
|
2009-10-07 10:16:52 -07:00
|
|
|
|
|
|
|
// Check for default-ignorable char that didn't get filtered, combined,
|
|
|
|
// etc by the shaping process, and remove from the run.
|
|
|
|
// (This may be done within harfbuzz eventually.)
|
|
|
|
if (glyphsInClump == 1 && baseCharIndex + 1 == endCharIndex &&
|
2011-12-06 04:39:19 -08:00
|
|
|
aShapedWord->FilterIfIgnorable(baseCharIndex)) {
|
2009-10-07 10:16:52 -07:00
|
|
|
glyphStart = glyphEnd;
|
|
|
|
charStart = charEnd;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-20 01:19:10 -08:00
|
|
|
hb_position_t x_offset = posInfo[glyphStart].x_offset;
|
2010-11-14 11:21:37 -08:00
|
|
|
hb_position_t x_advance = posInfo[glyphStart].x_advance;
|
2012-01-20 01:19:10 -08:00
|
|
|
nscoord xOffset, advance;
|
|
|
|
if (roundX) {
|
|
|
|
xOffset =
|
|
|
|
appUnitsPerDevUnit * FixedToIntRound(x_offset + x_residual);
|
|
|
|
// Desired distance from the base glyph to the next reference point.
|
|
|
|
hb_position_t width = x_advance - x_offset;
|
|
|
|
int intWidth = FixedToIntRound(width);
|
|
|
|
x_residual = width - FloatToFixed(intWidth);
|
|
|
|
advance = appUnitsPerDevUnit * intWidth + xOffset;
|
|
|
|
} else {
|
|
|
|
xOffset = floor(hb2appUnits * x_offset + 0.5);
|
|
|
|
advance = floor(hb2appUnits * x_advance + 0.5);
|
|
|
|
}
|
|
|
|
// Check if it's a simple one-to-one mapping
|
2009-10-07 10:16:52 -07:00
|
|
|
if (glyphsInClump == 1 &&
|
|
|
|
gfxTextRun::CompressedGlyph::IsSimpleGlyphID(ginfo[glyphStart].codepoint) &&
|
|
|
|
gfxTextRun::CompressedGlyph::IsSimpleAdvance(advance) &&
|
2011-12-06 04:39:19 -08:00
|
|
|
aShapedWord->IsClusterStart(baseCharIndex) &&
|
2012-01-20 01:19:10 -08:00
|
|
|
xOffset == 0 &&
|
2010-07-28 06:36:07 -07:00
|
|
|
posInfo[glyphStart].y_offset == 0 && yPos == 0)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
|
|
|
gfxTextRun::CompressedGlyph g;
|
2011-12-06 04:39:19 -08:00
|
|
|
aShapedWord->SetSimpleGlyph(baseCharIndex,
|
2009-10-07 10:16:52 -07:00
|
|
|
g.SetSimpleGlyph(advance,
|
|
|
|
ginfo[glyphStart].codepoint));
|
|
|
|
} else {
|
|
|
|
// collect all glyphs in a list to be assigned to the first char;
|
|
|
|
// there must be at least one in the clump, and we already measured
|
|
|
|
// its advance, hence the placement of the loop-exit test and the
|
|
|
|
// measurement of the next glyph
|
|
|
|
while (1) {
|
|
|
|
gfxTextRun::DetailedGlyph* details =
|
|
|
|
detailedGlyphs.AppendElement();
|
|
|
|
details->mGlyphID = ginfo[glyphStart].codepoint;
|
2010-11-14 11:21:37 -08:00
|
|
|
|
2012-01-20 01:19:10 -08:00
|
|
|
details->mXOffset = xOffset;
|
|
|
|
details->mAdvance = advance;
|
|
|
|
|
2010-11-14 11:21:37 -08:00
|
|
|
hb_position_t y_offset = posInfo[glyphStart].y_offset;
|
|
|
|
details->mYOffset = yPos -
|
2011-12-06 04:39:19 -08:00
|
|
|
(roundY ? appUnitsPerDevUnit * FixedToIntRound(y_offset)
|
2011-05-30 12:08:55 -07:00
|
|
|
: floor(hb2appUnits * y_offset + 0.5));
|
2010-11-14 11:21:37 -08:00
|
|
|
|
|
|
|
hb_position_t y_advance = posInfo[glyphStart].y_advance;
|
|
|
|
if (y_advance != 0) {
|
|
|
|
yPos -=
|
2011-12-06 04:39:19 -08:00
|
|
|
roundY ? appUnitsPerDevUnit * FixedToIntRound(y_advance)
|
2011-05-30 12:08:55 -07:00
|
|
|
: floor(hb2appUnits * y_advance + 0.5);
|
2010-07-28 06:36:07 -07:00
|
|
|
}
|
|
|
|
if (++glyphStart >= glyphEnd) {
|
2009-10-07 10:16:52 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-01-20 01:19:10 -08:00
|
|
|
|
|
|
|
x_offset = posInfo[glyphStart].x_offset;
|
2010-11-14 11:21:37 -08:00
|
|
|
x_advance = posInfo[glyphStart].x_advance;
|
2012-01-20 01:19:10 -08:00
|
|
|
if (roundX) {
|
|
|
|
xOffset = appUnitsPerDevUnit *
|
|
|
|
FixedToIntRound(x_offset + x_residual);
|
|
|
|
// Desired distance to the next reference point. The
|
|
|
|
// residual is considered here, and includes the residual
|
|
|
|
// from the base glyph offset and subsequent advances, so
|
|
|
|
// that the distance from the base glyph is optimized
|
|
|
|
// rather than the distance from combining marks.
|
|
|
|
x_advance += x_residual;
|
|
|
|
int intAdvance = FixedToIntRound(x_advance);
|
|
|
|
x_residual = x_advance - FloatToFixed(intAdvance);
|
|
|
|
advance = appUnitsPerDevUnit * intAdvance;
|
|
|
|
} else {
|
|
|
|
xOffset = floor(hb2appUnits * x_offset + 0.5);
|
|
|
|
advance = floor(hb2appUnits * x_advance + 0.5);
|
|
|
|
}
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun::CompressedGlyph g;
|
2011-12-06 04:39:19 -08:00
|
|
|
g.SetComplex(aShapedWord->IsClusterStart(baseCharIndex),
|
2011-10-17 07:59:28 -07:00
|
|
|
true, detailedGlyphs.Length());
|
2011-12-06 04:39:19 -08:00
|
|
|
aShapedWord->SetGlyphs(baseCharIndex,
|
2009-10-07 10:16:52 -07:00
|
|
|
g, detailedGlyphs.Elements());
|
|
|
|
|
|
|
|
detailedGlyphs.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// the rest of the chars in the group are ligature continuations,
|
|
|
|
// no associated glyphs
|
2010-12-05 10:38:12 -08:00
|
|
|
while (++baseCharIndex != endCharIndex &&
|
2012-08-22 08:56:38 -07:00
|
|
|
baseCharIndex < int32_t(wordLength)) {
|
2009-10-07 10:16:52 -07:00
|
|
|
gfxTextRun::CompressedGlyph g;
|
2012-05-07 12:18:24 -07:00
|
|
|
g.SetComplex(aShapedWord->IsClusterStart(baseCharIndex),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, 0);
|
2012-07-30 07:20:58 -07:00
|
|
|
aShapedWord->SetGlyphs(baseCharIndex, g, nullptr);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
glyphStart = glyphEnd;
|
|
|
|
charStart = charEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|