2010-03-15 02:34:25 -07: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/. */
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
#include "prtypes.h"
|
|
|
|
#include "gfxTypes.h"
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxUniscribeShaper.h"
|
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
|
|
|
|
#include "gfxFontTest.h"
|
|
|
|
|
|
|
|
#include "cairo.h"
|
|
|
|
#include "cairo-win32.h"
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#include "prinit.h"
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* class gfxUniscribeShaper
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#define ESTIMATE_MAX_GLYPHS(L) (((3 * (L)) >> 1) + 16)
|
|
|
|
|
|
|
|
class UniscribeItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UniscribeItem(gfxContext *aContext, HDC aDC,
|
|
|
|
gfxUniscribeShaper *aShaper,
|
|
|
|
const PRUnichar *aString, PRUint32 aLength,
|
2010-06-01 06:42:37 -07:00
|
|
|
SCRIPT_ITEM *aItem, PRUint32 aIVS) :
|
2010-03-15 02:34:25 -07:00
|
|
|
mContext(aContext), mDC(aDC),
|
|
|
|
mShaper(aShaper),
|
|
|
|
mItemString(aString), mItemLength(aLength),
|
2012-07-30 07:20:58 -07:00
|
|
|
mAlternativeString(nullptr), mScriptItem(aItem),
|
2010-03-15 02:34:25 -07:00
|
|
|
mScript(aItem->a.eScript),
|
|
|
|
mNumGlyphs(0), mMaxGlyphs(ESTIMATE_MAX_GLYPHS(aLength)),
|
2011-10-17 07:59:28 -07:00
|
|
|
mFontSelected(false), mIVS(aIVS)
|
2010-03-15 02:34:25 -07:00
|
|
|
{
|
2011-01-07 04:38:31 -08:00
|
|
|
// See bug 394751 for details.
|
|
|
|
NS_ASSERTION(mMaxGlyphs < 65535,
|
|
|
|
"UniscribeItem is too big, ScriptShape() will fail!");
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
~UniscribeItem() {
|
|
|
|
free(mAlternativeString);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool AllocateBuffers() {
|
2010-03-15 02:34:25 -07:00
|
|
|
return (mGlyphs.SetLength(mMaxGlyphs) &&
|
|
|
|
mClusters.SetLength(mItemLength + 1) &&
|
|
|
|
mAttr.SetLength(mMaxGlyphs));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* possible return values:
|
|
|
|
* S_OK - things succeeded
|
|
|
|
* GDI_ERROR - things failed to shape. Might want to try again after calling DisableShaping()
|
|
|
|
*/
|
|
|
|
|
|
|
|
HRESULT Shape() {
|
|
|
|
HRESULT rv;
|
2012-07-30 07:20:58 -07:00
|
|
|
HDC shapeDC = nullptr;
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
const PRUnichar *str = mAlternativeString ? mAlternativeString : mItemString;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mScriptItem->a.fLogicalOrder = true;
|
2010-03-15 02:34:25 -07:00
|
|
|
SCRIPT_ANALYSIS sa = mScriptItem->a;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
while (true) {
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
rv = ScriptShape(shapeDC, mShaper->ScriptCache(),
|
|
|
|
str, mItemLength,
|
|
|
|
mMaxGlyphs, &sa,
|
|
|
|
mGlyphs.Elements(), mClusters.Elements(),
|
|
|
|
mAttr.Elements(), &mNumGlyphs);
|
|
|
|
|
|
|
|
if (rv == E_OUTOFMEMORY) {
|
|
|
|
mMaxGlyphs *= 2;
|
|
|
|
if (!mGlyphs.SetLength(mMaxGlyphs) ||
|
|
|
|
!mAttr.SetLength(mMaxGlyphs)) {
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uniscribe can't do shaping with some fonts, so it sets the
|
|
|
|
// fNoGlyphIndex flag in the SCRIPT_ANALYSIS structure to indicate
|
|
|
|
// this. This occurs with CFF fonts loaded with
|
|
|
|
// AddFontMemResourceEx but it's not clear what the other cases
|
|
|
|
// are. We return an error so our caller can try fallback shaping.
|
|
|
|
// see http://msdn.microsoft.com/en-us/library/ms776520(VS.85).aspx
|
|
|
|
|
|
|
|
if (sa.fNoGlyphIndex) {
|
|
|
|
return GDI_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == E_PENDING) {
|
|
|
|
if (shapeDC == mDC) {
|
|
|
|
// we already tried this once, something failed, give up
|
|
|
|
return E_PENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectFont();
|
|
|
|
|
|
|
|
shapeDC = mDC;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// http://msdn.microsoft.com/en-us/library/dd368564(VS.85).aspx:
|
|
|
|
// Uniscribe will return this if "the font corresponding to the
|
|
|
|
// DC does not support the script required by the run...".
|
|
|
|
// In this case, we'll set the script code to SCRIPT_UNDEFINED
|
|
|
|
// and try again, so that we'll at least get glyphs even though
|
|
|
|
// they won't necessarily have proper shaping.
|
|
|
|
// (We probably shouldn't have selected this font at all,
|
|
|
|
// but it's too late to fix that here.)
|
|
|
|
if (rv == USP_E_SCRIPT_NOT_IN_FONT) {
|
|
|
|
sa.eScript = SCRIPT_UNDEFINED;
|
|
|
|
NS_WARNING("Uniscribe says font does not support script needed");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-06-01 06:42:37 -07:00
|
|
|
// Prior to Windows 7, Uniscribe didn't support Ideographic Variation
|
|
|
|
// Selectors. Replace the UVS glyph manually.
|
|
|
|
if (mIVS) {
|
|
|
|
PRUint32 lastChar = str[mItemLength - 1];
|
|
|
|
if (NS_IS_LOW_SURROGATE(lastChar)
|
|
|
|
&& NS_IS_HIGH_SURROGATE(str[mItemLength - 2])) {
|
|
|
|
lastChar = SURROGATE_TO_UCS4(str[mItemLength - 2], lastChar);
|
|
|
|
}
|
|
|
|
PRUint16 glyphId = mShaper->GetFont()->GetUVSGlyph(lastChar, mIVS);
|
|
|
|
if (glyphId) {
|
|
|
|
mGlyphs[mNumGlyphs - 1] = glyphId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-15 02:34:25 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ShapingEnabled() {
|
2010-03-15 02:34:25 -07:00
|
|
|
return (mScriptItem->a.eScript != SCRIPT_UNDEFINED);
|
|
|
|
}
|
|
|
|
void DisableShaping() {
|
|
|
|
mScriptItem->a.eScript = SCRIPT_UNDEFINED;
|
|
|
|
// Note: If we disable the shaping by using SCRIPT_UNDEFINED and
|
|
|
|
// the string has the surrogate pair, ScriptShape API is
|
|
|
|
// *sometimes* crashed. Therefore, we should replace the surrogate
|
|
|
|
// pair to U+FFFD. See bug 341500.
|
|
|
|
GenerateAlternativeString();
|
|
|
|
}
|
|
|
|
void EnableShaping() {
|
|
|
|
mScriptItem->a.eScript = mScript;
|
|
|
|
if (mAlternativeString) {
|
|
|
|
free(mAlternativeString);
|
2012-07-30 07:20:58 -07:00
|
|
|
mAlternativeString = nullptr;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsGlyphMissing(SCRIPT_FONTPROPERTIES *aSFP, PRUint32 aGlyphIndex) {
|
2010-03-15 02:34:25 -07:00
|
|
|
return (mGlyphs[aGlyphIndex] == aSFP->wgDefault);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HRESULT Place() {
|
|
|
|
HRESULT rv;
|
2012-07-30 07:20:58 -07:00
|
|
|
HDC placeDC = nullptr;
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
if (!mOffsets.SetLength(mNumGlyphs) ||
|
|
|
|
!mAdvances.SetLength(mNumGlyphs)) {
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCRIPT_ANALYSIS sa = mScriptItem->a;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
while (true) {
|
2010-03-15 02:34:25 -07:00
|
|
|
rv = ScriptPlace(placeDC, mShaper->ScriptCache(),
|
|
|
|
mGlyphs.Elements(), mNumGlyphs,
|
|
|
|
mAttr.Elements(), &sa,
|
|
|
|
mAdvances.Elements(), mOffsets.Elements(), NULL);
|
|
|
|
|
|
|
|
if (rv == E_PENDING) {
|
|
|
|
SelectFont();
|
|
|
|
placeDC = mDC;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == USP_E_SCRIPT_NOT_IN_FONT) {
|
|
|
|
sa.eScript = SCRIPT_UNDEFINED;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptFontProperties(SCRIPT_FONTPROPERTIES *sfp) {
|
|
|
|
HRESULT rv;
|
|
|
|
|
|
|
|
memset(sfp, 0, sizeof(SCRIPT_FONTPROPERTIES));
|
|
|
|
sfp->cBytes = sizeof(SCRIPT_FONTPROPERTIES);
|
|
|
|
rv = ScriptGetFontProperties(NULL, mShaper->ScriptCache(),
|
|
|
|
sfp);
|
|
|
|
if (rv == E_PENDING) {
|
|
|
|
SelectFont();
|
|
|
|
rv = ScriptGetFontProperties(mDC, mShaper->ScriptCache(),
|
|
|
|
sfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
void SaveGlyphs(gfxShapedWord *aShapedWord) {
|
|
|
|
PRUint32 offsetInRun = mScriptItem->iCharPos;
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
// XXX We should store this in the item and only fetch it once
|
|
|
|
SCRIPT_FONTPROPERTIES sfp;
|
|
|
|
ScriptFontProperties(&sfp);
|
|
|
|
|
|
|
|
PRUint32 offset = 0;
|
2011-12-06 04:39:19 -08:00
|
|
|
nsAutoTArray<gfxShapedWord::DetailedGlyph,1> detailedGlyphs;
|
|
|
|
gfxShapedWord::CompressedGlyph g;
|
|
|
|
const PRUint32 appUnitsPerDevUnit = aShapedWord->AppUnitsPerDevUnit();
|
2010-03-15 02:34:25 -07:00
|
|
|
while (offset < mItemLength) {
|
|
|
|
PRUint32 runOffset = offsetInRun + offset;
|
2011-12-06 04:39:19 -08:00
|
|
|
bool atClusterStart = aShapedWord->IsClusterStart(runOffset);
|
2010-03-15 02:34:25 -07:00
|
|
|
if (offset > 0 && mClusters[offset] == mClusters[offset - 1]) {
|
2011-10-17 07:59:28 -07:00
|
|
|
g.SetComplex(atClusterStart, false, 0);
|
2012-07-30 07:20:58 -07:00
|
|
|
aShapedWord->SetGlyphs(runOffset, g, nullptr);
|
2010-03-15 02:34:25 -07:00
|
|
|
} else {
|
|
|
|
// Count glyphs for this character
|
|
|
|
PRUint32 k = mClusters[offset];
|
|
|
|
PRUint32 glyphCount = mNumGlyphs - k;
|
|
|
|
PRUint32 nextClusterOffset;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool missing = IsGlyphMissing(&sfp, k);
|
2010-03-15 02:34:25 -07:00
|
|
|
for (nextClusterOffset = offset + 1; nextClusterOffset < mItemLength; ++nextClusterOffset) {
|
|
|
|
if (mClusters[nextClusterOffset] > k) {
|
|
|
|
glyphCount = mClusters[nextClusterOffset] - k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PRUint32 j;
|
|
|
|
for (j = 1; j < glyphCount; ++j) {
|
|
|
|
if (IsGlyphMissing(&sfp, k + j)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
missing = true;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PRInt32 advance = mAdvances[k]*appUnitsPerDevUnit;
|
|
|
|
WORD glyph = mGlyphs[k];
|
|
|
|
NS_ASSERTION(!gfxFontGroup::IsInvalidChar(mItemString[offset]),
|
|
|
|
"invalid character detected");
|
|
|
|
if (missing) {
|
|
|
|
if (NS_IS_HIGH_SURROGATE(mItemString[offset]) &&
|
|
|
|
offset + 1 < mItemLength &&
|
|
|
|
NS_IS_LOW_SURROGATE(mItemString[offset + 1])) {
|
2011-12-06 04:39:19 -08:00
|
|
|
aShapedWord->SetMissingGlyph(runOffset,
|
|
|
|
SURROGATE_TO_UCS4(mItemString[offset],
|
|
|
|
mItemString[offset + 1]),
|
|
|
|
mShaper->GetFont());
|
2010-03-15 02:34:25 -07:00
|
|
|
} else {
|
2011-12-06 04:39:19 -08:00
|
|
|
aShapedWord->SetMissingGlyph(runOffset, mItemString[offset],
|
|
|
|
mShaper->GetFont());
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
} else if (glyphCount == 1 && advance >= 0 &&
|
|
|
|
mOffsets[k].dv == 0 && mOffsets[k].du == 0 &&
|
2011-12-06 04:39:19 -08:00
|
|
|
gfxShapedWord::CompressedGlyph::IsSimpleAdvance(advance) &&
|
|
|
|
gfxShapedWord::CompressedGlyph::IsSimpleGlyphID(glyph) &&
|
2010-12-22 06:27:43 -08:00
|
|
|
atClusterStart)
|
|
|
|
{
|
2011-12-06 04:39:19 -08:00
|
|
|
aShapedWord->SetSimpleGlyph(runOffset, g.SetSimpleGlyph(advance, glyph));
|
2010-03-15 02:34:25 -07:00
|
|
|
} else {
|
|
|
|
if (detailedGlyphs.Length() < glyphCount) {
|
|
|
|
if (!detailedGlyphs.AppendElements(glyphCount - detailedGlyphs.Length()))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < glyphCount; ++i) {
|
|
|
|
gfxTextRun::DetailedGlyph *details = &detailedGlyphs[i];
|
|
|
|
details->mGlyphID = mGlyphs[k + i];
|
2011-12-06 04:39:19 -08:00
|
|
|
details->mAdvance = mAdvances[k + i] * appUnitsPerDevUnit;
|
|
|
|
details->mXOffset = float(mOffsets[k + i].du) * appUnitsPerDevUnit *
|
|
|
|
aShapedWord->GetDirection();
|
|
|
|
details->mYOffset = - float(mOffsets[k + i].dv) * appUnitsPerDevUnit;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
2011-12-06 04:39:19 -08:00
|
|
|
aShapedWord->SetGlyphs(runOffset,
|
|
|
|
g.SetComplex(atClusterStart, true,
|
|
|
|
glyphCount),
|
|
|
|
detailedGlyphs.Elements());
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectFont() {
|
|
|
|
if (mFontSelected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cairo_t *cr = mContext->GetCairo();
|
|
|
|
|
|
|
|
cairo_set_font_face(cr, mShaper->GetFont()->CairoFontFace());
|
|
|
|
cairo_set_font_size(cr, mShaper->GetFont()->GetAdjustedSize());
|
|
|
|
cairo_scaled_font_t *scaledFont = mShaper->GetFont()->CairoScaledFont();
|
|
|
|
cairo_win32_scaled_font_select_font(scaledFont, mDC);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mFontSelected = true;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void GenerateAlternativeString() {
|
|
|
|
if (mAlternativeString)
|
|
|
|
free(mAlternativeString);
|
|
|
|
mAlternativeString = (PRUnichar *)malloc(mItemLength * sizeof(PRUnichar));
|
|
|
|
if (!mAlternativeString)
|
|
|
|
return;
|
|
|
|
memcpy((void *)mAlternativeString, (const void *)mItemString,
|
|
|
|
mItemLength * sizeof(PRUnichar));
|
|
|
|
for (PRUint32 i = 0; i < mItemLength; i++) {
|
|
|
|
if (NS_IS_HIGH_SURROGATE(mItemString[i]) || NS_IS_LOW_SURROGATE(mItemString[i]))
|
|
|
|
mAlternativeString[i] = PRUnichar(0xFFFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<gfxContext> mContext;
|
|
|
|
HDC mDC;
|
|
|
|
gfxUniscribeShaper *mShaper;
|
|
|
|
|
|
|
|
SCRIPT_ITEM *mScriptItem;
|
|
|
|
WORD mScript;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// these point to the full string/length of the item
|
|
|
|
const PRUnichar *mItemString;
|
|
|
|
const PRUint32 mItemLength;
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRUnichar *mAlternativeString;
|
|
|
|
|
|
|
|
#define AVERAGE_ITEM_LENGTH 40
|
|
|
|
|
|
|
|
nsAutoTArray<WORD, PRUint32(ESTIMATE_MAX_GLYPHS(AVERAGE_ITEM_LENGTH))> mGlyphs;
|
|
|
|
nsAutoTArray<WORD, AVERAGE_ITEM_LENGTH + 1> mClusters;
|
|
|
|
nsAutoTArray<SCRIPT_VISATTR, PRUint32(ESTIMATE_MAX_GLYPHS(AVERAGE_ITEM_LENGTH))> mAttr;
|
|
|
|
|
|
|
|
nsAutoTArray<GOFFSET, 2 * AVERAGE_ITEM_LENGTH> mOffsets;
|
|
|
|
nsAutoTArray<int, 2 * AVERAGE_ITEM_LENGTH> mAdvances;
|
|
|
|
|
|
|
|
#undef AVERAGE_ITEM_LENGTH
|
|
|
|
|
|
|
|
int mMaxGlyphs;
|
|
|
|
int mNumGlyphs;
|
2010-06-01 06:42:37 -07:00
|
|
|
PRUint32 mIVS;
|
2010-03-15 02:34:25 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mFontSelected;
|
2010-03-15 02:34:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class Uniscribe
|
|
|
|
{
|
|
|
|
public:
|
2010-08-11 09:52:21 -07:00
|
|
|
Uniscribe(const PRUnichar *aString,
|
2011-12-06 04:39:19 -08:00
|
|
|
gfxShapedWord *aShapedWord):
|
|
|
|
mString(aString), mShapedWord(aShapedWord)
|
2010-03-15 02:34:25 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init() {
|
|
|
|
memset(&mControl, 0, sizeof(SCRIPT_CONTROL));
|
|
|
|
memset(&mState, 0, sizeof(SCRIPT_STATE));
|
|
|
|
// Lock the direction. Don't allow the itemizer to change directions
|
|
|
|
// based on character type.
|
2011-12-06 04:39:19 -08:00
|
|
|
mState.uBidiLevel = mShapedWord->IsRightToLeft() ? 1 : 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
mState.fOverrideDirection = true;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
int Itemize() {
|
|
|
|
HRESULT rv;
|
|
|
|
|
|
|
|
int maxItems = 5;
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
|
|
|
// Allocate space for one more item than expected, to handle a rare
|
|
|
|
// overflow in ScriptItemize (pre XP SP2). See bug 366643.
|
|
|
|
if (!mItems.SetLength(maxItems + 1)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-06 04:39:19 -08:00
|
|
|
while ((rv = ScriptItemize(mString, mShapedWord->Length(),
|
|
|
|
maxItems, &mControl, &mState,
|
2010-03-15 02:34:25 -07:00
|
|
|
mItems.Elements(), &mNumItems)) == E_OUTOFMEMORY) {
|
|
|
|
maxItems *= 2;
|
|
|
|
if (!mItems.SetLength(maxItems + 1)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return mNumItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCRIPT_ITEM *ScriptItem(PRUint32 i) {
|
|
|
|
NS_ASSERTION(i <= (PRUint32)mNumItems, "Trying to get out of bounds item");
|
|
|
|
return &mItems[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const PRUnichar *mString;
|
2011-12-06 04:39:19 -08:00
|
|
|
gfxShapedWord *mShapedWord;
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
SCRIPT_CONTROL mControl;
|
|
|
|
SCRIPT_STATE mState;
|
|
|
|
nsTArray<SCRIPT_ITEM> mItems;
|
|
|
|
int mNumItems;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-12-06 04:39:19 -08:00
|
|
|
gfxUniscribeShaper::ShapeWord(gfxContext *aContext,
|
|
|
|
gfxShapedWord *aShapedWord,
|
|
|
|
const PRUnichar *aString)
|
2010-03-15 02:34:25 -07:00
|
|
|
{
|
|
|
|
DCFromContext aDC(aContext);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result = true;
|
2010-03-15 02:34:25 -07:00
|
|
|
HRESULT rv;
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
Uniscribe us(aString, aShapedWord);
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
/* itemize the string */
|
|
|
|
int numItems = us.Itemize();
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
PRUint32 length = aShapedWord->Length();
|
2010-03-15 02:34:25 -07:00
|
|
|
SaveDC(aDC);
|
2010-06-01 06:42:37 -07:00
|
|
|
PRUint32 ivs = 0;
|
2010-03-15 02:34:25 -07:00
|
|
|
for (int i = 0; i < numItems; ++i) {
|
2010-06-01 06:42:37 -07:00
|
|
|
int iCharPos = us.ScriptItem(i)->iCharPos;
|
|
|
|
int iCharPosNext = us.ScriptItem(i+1)->iCharPos;
|
|
|
|
|
|
|
|
if (ivs) {
|
|
|
|
iCharPos += 2;
|
|
|
|
if (iCharPos >= iCharPosNext) {
|
|
|
|
ivs = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
if (i+1 < numItems && iCharPosNext <= length - 2
|
|
|
|
&& aString[iCharPosNext] == H_SURROGATE(kUnicodeVS17)
|
|
|
|
&& PRUint32(aString[iCharPosNext + 1]) - L_SURROGATE(kUnicodeVS17)
|
2010-06-01 06:42:37 -07:00
|
|
|
<= L_SURROGATE(kUnicodeVS256) - L_SURROGATE(kUnicodeVS17)) {
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
ivs = SURROGATE_TO_UCS4(aString[iCharPosNext],
|
|
|
|
aString[iCharPosNext + 1]);
|
2010-06-01 06:42:37 -07:00
|
|
|
} else {
|
|
|
|
ivs = 0;
|
|
|
|
}
|
|
|
|
|
2010-03-15 02:34:25 -07:00
|
|
|
UniscribeItem item(aContext, aDC, this,
|
2011-12-06 04:39:19 -08:00
|
|
|
aString + iCharPos,
|
2010-06-01 06:42:37 -07:00
|
|
|
iCharPosNext - iCharPos,
|
|
|
|
us.ScriptItem(i), ivs);
|
2010-03-15 02:34:25 -07:00
|
|
|
if (!item.AllocateBuffers()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
result = false;
|
2010-03-15 02:34:25 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!item.ShapingEnabled()) {
|
|
|
|
item.EnableShaping();
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = item.Shape();
|
|
|
|
if (FAILED(rv)) {
|
|
|
|
// we know we have the glyphs to display this font already
|
|
|
|
// so Uniscribe just doesn't know how to shape the script.
|
|
|
|
// Render the glyphs without shaping.
|
|
|
|
item.DisableShaping();
|
|
|
|
rv = item.Shape();
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (FAILED(rv)) {
|
|
|
|
NS_WARNING("Uniscribe failed to shape with font");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (SUCCEEDED(rv)) {
|
|
|
|
rv = item.Place();
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (FAILED(rv)) {
|
|
|
|
// crap fonts may fail when placing (e.g. funky free fonts)
|
|
|
|
NS_WARNING("Uniscribe failed to place with font");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(rv)) {
|
|
|
|
// Uniscribe doesn't like this font for some reason.
|
2010-08-11 09:52:26 -07:00
|
|
|
// Returning FALSE will make the gfxGDIFont retry with the
|
2011-01-07 04:38:31 -08:00
|
|
|
// "dumb" GDI shaper, unless useUniscribeOnly was set.
|
2011-10-17 07:59:28 -07:00
|
|
|
result = false;
|
2010-03-15 02:34:25 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-06 04:39:19 -08:00
|
|
|
item.SaveGlyphs(aShapedWord);
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
RestoreDC(aDC, -1);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|