2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Foundation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
2009-08-16 06:52:12 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005-2009
|
2007-03-22 10:30:00 -07:00
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Stuart Parmenter <stuart@mozilla.com>
|
|
|
|
* Masayuki Nakano <masayuki@d-toybox.com>
|
2008-08-05 21:34:06 -07:00
|
|
|
* John Daggett <jdaggett@mozilla.com>
|
2009-08-16 06:52:12 -07:00
|
|
|
* Jonathan Kew <jfkthame@gmail.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2009-04-04 02:43:42 -07:00
|
|
|
#include "nsIPrefService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2007-04-03 20:32:43 -07:00
|
|
|
#include "nsExpirationTracker.h"
|
2010-02-24 09:57:44 -08:00
|
|
|
#include "nsILanguageAtomService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "gfxFont.h"
|
|
|
|
#include "gfxPlatform.h"
|
2010-02-24 09:57:57 -08:00
|
|
|
#include "gfxAtoms.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "prtypes.h"
|
|
|
|
#include "gfxTypes.h"
|
|
|
|
#include "gfxContext.h"
|
2007-03-26 20:24:49 -07:00
|
|
|
#include "gfxFontMissingGlyphs.h"
|
2008-09-30 20:01:53 -07:00
|
|
|
#include "gfxUserFontSet.h"
|
2009-08-16 06:52:12 -07:00
|
|
|
#include "gfxPlatformFontList.h"
|
2010-06-11 12:14:38 -07:00
|
|
|
#include "gfxScriptItemizer.h"
|
2009-10-07 10:16:52 -07:00
|
|
|
#include "gfxUnicodeProperties.h"
|
2007-05-08 15:46:14 -07:00
|
|
|
#include "nsMathUtils.h"
|
2008-09-30 20:01:53 -07:00
|
|
|
#include "nsBidiUtils.h"
|
2009-10-07 08:26:58 -07:00
|
|
|
#include "nsUnicodeRange.h"
|
2010-04-28 14:51:16 -07:00
|
|
|
#include "nsCompressedCharMap.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "cairo.h"
|
|
|
|
#include "gfxFontTest.h"
|
|
|
|
|
2010-06-11 12:14:38 -07:00
|
|
|
#include "harfbuzz/hb-blob.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCRT.h"
|
|
|
|
|
2010-07-11 12:33:56 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-04-03 20:32:43 -07:00
|
|
|
gfxFontCache *gfxFontCache::gGlobalCache = nsnull;
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
static PRLogModuleInfo *gFontSelection = PR_NewLogModule("fontSelectionLog");
|
|
|
|
|
2007-07-01 18:24:56 -07:00
|
|
|
#ifdef DEBUG_roc
|
|
|
|
#define DEBUG_TEXT_RUN_STORAGE_METRICS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
2007-07-01 18:12:45 -07:00
|
|
|
static PRUint32 gTextRunStorageHighWaterMark = 0;
|
|
|
|
static PRUint32 gTextRunStorage = 0;
|
2007-10-01 18:47:40 -07:00
|
|
|
static PRUint32 gFontCount = 0;
|
|
|
|
static PRUint32 gGlyphExtentsCount = 0;
|
|
|
|
static PRUint32 gGlyphExtentsWidthsTotalSize = 0;
|
|
|
|
static PRUint32 gGlyphExtentsSetupEagerSimple = 0;
|
|
|
|
static PRUint32 gGlyphExtentsSetupEagerTight = 0;
|
|
|
|
static PRUint32 gGlyphExtentsSetupLazyTight = 0;
|
|
|
|
static PRUint32 gGlyphExtentsSetupFallBackToTight = 0;
|
2007-07-01 18:12:45 -07:00
|
|
|
#endif
|
|
|
|
|
2008-09-30 20:01:53 -07:00
|
|
|
gfxFontEntry::~gfxFontEntry()
|
|
|
|
{
|
2010-06-11 12:14:38 -07:00
|
|
|
if (mUserFontData) {
|
2008-09-30 20:01:53 -07:00
|
|
|
delete mUserFontData;
|
2010-06-11 12:14:38 -07:00
|
|
|
}
|
2010-07-13 13:31:31 -07:00
|
|
|
if (mFeatureSettings) {
|
|
|
|
delete mFeatureSettings;
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
}
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
PRBool gfxFontEntry::TestCharacterMap(PRUint32 aCh)
|
|
|
|
{
|
|
|
|
if (!mCmapInitialized) {
|
|
|
|
ReadCMAP();
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
return mCharacterMap.test(aCh);
|
|
|
|
}
|
|
|
|
|
2010-06-01 06:42:37 -07:00
|
|
|
nsresult gfxFontEntry::InitializeUVSMap()
|
|
|
|
{
|
|
|
|
// mUVSOffset will not be initialized
|
|
|
|
// until cmap is initialized.
|
|
|
|
if (!mCmapInitialized) {
|
|
|
|
ReadCMAP();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mUVSOffset) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mUVSData) {
|
|
|
|
const PRUint32 kCmapTag = TRUETYPE_TAG('c','m','a','p');
|
|
|
|
nsAutoTArray<PRUint8,16384> buffer;
|
|
|
|
if (GetFontTable(kCmapTag, buffer) != NS_OK) {
|
|
|
|
mUVSOffset = 0; // don't bother to read the table again
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint8* uvsData;
|
|
|
|
nsresult rv = gfxFontUtils::ReadCMAPTableFormat14(
|
|
|
|
buffer.Elements() + mUVSOffset,
|
|
|
|
buffer.Length() - mUVSOffset,
|
|
|
|
uvsData);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mUVSOffset = 0; // don't bother to read the table again
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUVSData = uvsData;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint16 gfxFontEntry::GetUVSGlyph(PRUint32 aCh, PRUint32 aVS)
|
|
|
|
{
|
|
|
|
InitializeUVSMap();
|
|
|
|
|
|
|
|
if (mUVSData) {
|
|
|
|
return gfxFontUtils::MapUVSToGlyphFormat14(mUVSData, aCh, aVS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
nsresult gfxFontEntry::ReadCMAP()
|
2008-08-05 21:34:06 -07:00
|
|
|
{
|
2009-08-16 06:52:12 -07:00
|
|
|
mCmapInitialized = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2010-07-16 18:32:18 -07:00
|
|
|
const nsString& gfxFontEntry::FamilyName() const
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mFamily, "gfxFontEntry is not a member of a family");
|
|
|
|
return mFamily->Name();
|
|
|
|
}
|
|
|
|
|
2009-10-07 08:26:58 -07:00
|
|
|
already_AddRefed<gfxFont>
|
2010-02-17 19:31:20 -08:00
|
|
|
gfxFontEntry::FindOrMakeFont(const gfxFontStyle *aStyle, PRBool aNeedsBold)
|
2009-10-07 08:26:58 -07:00
|
|
|
{
|
|
|
|
// the font entry name is the psname, not the family name
|
2010-07-13 13:31:31 -07:00
|
|
|
nsRefPtr<gfxFont> font = gfxFontCache::GetCache()->Lookup(this, aStyle);
|
2010-06-08 17:33:13 -07:00
|
|
|
|
2009-10-07 08:26:58 -07:00
|
|
|
if (!font) {
|
|
|
|
gfxFont *newFont = CreateFontInstance(aStyle, aNeedsBold);
|
|
|
|
if (!newFont)
|
|
|
|
return nsnull;
|
|
|
|
if (!newFont->Valid()) {
|
|
|
|
delete newFont;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
font = newFont;
|
|
|
|
gfxFontCache::GetCache()->AddNew(font);
|
|
|
|
}
|
|
|
|
gfxFont *f = nsnull;
|
|
|
|
font.swap(f);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2010-06-11 12:14:38 -07:00
|
|
|
gfxFontEntry::FontTableCacheEntry::FontTableCacheEntry
|
|
|
|
(nsTArray<PRUint8>& aBuffer,
|
|
|
|
PRUint32 aTag,
|
|
|
|
nsClassHashtable<nsUint32HashKey,FontTableCacheEntry>& aCache)
|
|
|
|
: mTag(aTag), mCache(aCache)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(FontTableCacheEntry);
|
|
|
|
mData.SwapElements(aBuffer);
|
|
|
|
mBlob = hb_blob_create((const char*)mData.Elements(), mData.Length(),
|
|
|
|
HB_MEMORY_MODE_READONLY,
|
|
|
|
gfxFontEntry::FontTableCacheEntry::Destroy,
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
gfxFontEntry::FontTableCacheEntry::Destroy(void *aUserData)
|
|
|
|
{
|
|
|
|
gfxFontEntry::FontTableCacheEntry *ftce =
|
|
|
|
static_cast<gfxFontEntry::FontTableCacheEntry*>(aUserData);
|
|
|
|
ftce->mCache.Remove(ftce->mTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
hb_blob_t *
|
|
|
|
gfxFontEntry::GetFontTable(PRUint32 aTag)
|
|
|
|
{
|
|
|
|
if (!mFontTableCache.IsInitialized()) {
|
|
|
|
// we do this here rather than on fontEntry construction
|
|
|
|
// because not all shapers will access the table cache at all
|
|
|
|
mFontTableCache.Init(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
FontTableCacheEntry *entry = nsnull;
|
|
|
|
if (!mFontTableCache.Get(aTag, &entry)) {
|
|
|
|
nsTArray<PRUint8> buffer;
|
|
|
|
if (NS_SUCCEEDED(GetFontTable(aTag, buffer))) {
|
|
|
|
entry = new FontTableCacheEntry(buffer, // adopts buffer elements
|
|
|
|
aTag, mFontTableCache);
|
|
|
|
if (mFontTableCache.Put(aTag, entry)) {
|
|
|
|
return entry->GetBlob();
|
|
|
|
}
|
|
|
|
hb_blob_destroy(entry->GetBlob());
|
|
|
|
delete entry; // we failed to cache it!
|
2009-10-07 10:16:52 -07:00
|
|
|
return nsnull;
|
2010-06-11 12:14:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
return hb_blob_reference(entry->GetBlob());
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
return nsnull;
|
2010-06-11 12:14:38 -07:00
|
|
|
}
|
|
|
|
|
2010-10-07 00:59:19 -07:00
|
|
|
void
|
|
|
|
gfxFontEntry::PreloadFontTable(PRUint32 aTag, nsTArray<PRUint8>& aTable)
|
|
|
|
{
|
|
|
|
if (!mFontTableCache.IsInitialized()) {
|
|
|
|
// This is intended for use with downloaded fonts, to cache the layout
|
|
|
|
// tables for harfbuzz, so initialize the cache for 3 entries to allow
|
|
|
|
// for GDEF/GSUB/GPOS.
|
|
|
|
mFontTableCache.Init(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
FontTableCacheEntry *entry = nsnull;
|
|
|
|
if (mFontTableCache.Get(aTag, &entry)) {
|
|
|
|
// this should never happen - it's a logic error in the calling code
|
|
|
|
// (so ignore the fact that we'll leak the elements of aTable here)
|
|
|
|
NS_NOTREACHED("can't preload table, already present in cache!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this adopts the buffer elements of aTable
|
|
|
|
entry = new FontTableCacheEntry(aTable, aTag, mFontTableCache);
|
|
|
|
if (!mFontTableCache.Put(aTag, entry)) {
|
|
|
|
NS_WARNING("failed to cache font table!");
|
|
|
|
}
|
|
|
|
}
|
2010-06-11 12:14:38 -07:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// class gfxFontFamily
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// we consider faces with mStandardFace == PR_TRUE to be "greater than" those with PR_FALSE,
|
|
|
|
// because during style matching, later entries will replace earlier ones
|
|
|
|
class FontEntryStandardFaceComparator {
|
|
|
|
public:
|
|
|
|
PRBool Equals(const nsRefPtr<gfxFontEntry>& a, const nsRefPtr<gfxFontEntry>& b) const {
|
|
|
|
return a->mStandardFace == b->mStandardFace;
|
|
|
|
}
|
|
|
|
PRBool LessThan(const nsRefPtr<gfxFontEntry>& a, const nsRefPtr<gfxFontEntry>& b) const {
|
|
|
|
return (a->mStandardFace == PR_FALSE && b->mStandardFace == PR_TRUE);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontFamily::SortAvailableFonts()
|
|
|
|
{
|
|
|
|
mAvailableFonts.Sort(FontEntryStandardFaceComparator());
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontFamily::HasOtherFamilyNames()
|
|
|
|
{
|
|
|
|
// need to read in other family names to determine this
|
|
|
|
if (!mOtherFamilyNamesInitialized) {
|
2010-01-28 17:41:25 -08:00
|
|
|
ReadOtherFamilyNames(gfxPlatformFontList::PlatformFontList()); // sets mHasOtherFamilyNames
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
return mHasOtherFamilyNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontEntry*
|
2010-03-11 20:26:43 -08:00
|
|
|
gfxFontFamily::FindFontForStyle(const gfxFontStyle& aFontStyle,
|
|
|
|
PRBool& aNeedsSyntheticBold)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
|
|
|
if (!mHasStyles)
|
|
|
|
FindStyleVariations(); // collect faces for the family, if not already done
|
|
|
|
|
|
|
|
NS_ASSERTION(mAvailableFonts.Length() > 0, "font family with no faces!");
|
|
|
|
|
2010-03-11 20:26:43 -08:00
|
|
|
aNeedsSyntheticBold = PR_FALSE;
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2010-11-10 07:49:52 -08:00
|
|
|
PRInt8 baseWeight = aFontStyle.ComputeWeight();
|
2009-08-16 06:52:12 -07:00
|
|
|
PRBool wantBold = baseWeight >= 6;
|
|
|
|
|
|
|
|
// If the family has only one face, we simply return it; no further checking needed
|
|
|
|
if (mAvailableFonts.Length() == 1) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[0];
|
2010-03-11 20:26:43 -08:00
|
|
|
aNeedsSyntheticBold = wantBold && !fe->IsBold();
|
2009-08-16 06:52:12 -07:00
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool wantItalic = (aFontStyle.style & (FONT_STYLE_ITALIC | FONT_STYLE_OBLIQUE)) != 0;
|
|
|
|
|
|
|
|
// Most families are "simple", having just Regular/Bold/Italic/BoldItalic,
|
|
|
|
// or some subset of these. In this case, we have exactly 4 entries in mAvailableFonts,
|
|
|
|
// stored in the above order; note that some of the entries may be NULL.
|
|
|
|
// We can then pick the required entry based on whether the request is for
|
|
|
|
// bold or non-bold, italic or non-italic, without running the more complex
|
|
|
|
// matching algorithm used for larger families with many weights and/or widths.
|
|
|
|
|
|
|
|
if (mIsSimpleFamily) {
|
|
|
|
// Family has no more than the "standard" 4 faces, at fixed indexes;
|
|
|
|
// calculate which one we want.
|
|
|
|
// Note that we cannot simply return it as not all 4 faces are necessarily present.
|
|
|
|
PRUint8 faceIndex = (wantItalic ? kItalicMask : 0) |
|
|
|
|
(wantBold ? kBoldMask : 0);
|
|
|
|
|
|
|
|
// if the desired style is available, return it directly
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[faceIndex];
|
|
|
|
if (fe) {
|
2010-03-11 20:26:43 -08:00
|
|
|
// no need to set aNeedsSyntheticBold here as we matched the boldness request
|
2009-08-16 06:52:12 -07:00
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
|
|
|
// order to check fallback faces in a simple family, depending on requested style
|
|
|
|
static const PRUint8 simpleFallbacks[4][3] = {
|
|
|
|
{ kBoldFaceIndex, kItalicFaceIndex, kBoldItalicFaceIndex }, // fallbacks for Regular
|
|
|
|
{ kRegularFaceIndex, kBoldItalicFaceIndex, kItalicFaceIndex },// Bold
|
|
|
|
{ kBoldItalicFaceIndex, kRegularFaceIndex, kBoldFaceIndex }, // Italic
|
|
|
|
{ kItalicFaceIndex, kBoldFaceIndex, kRegularFaceIndex } // BoldItalic
|
|
|
|
};
|
|
|
|
const PRUint8 *order = simpleFallbacks[faceIndex];
|
|
|
|
|
|
|
|
for (PRUint8 trial = 0; trial < 3; ++trial) {
|
|
|
|
// check remaining faces in order of preference to find the first that actually exists
|
|
|
|
fe = mAvailableFonts[order[trial]];
|
|
|
|
if (fe) {
|
|
|
|
PR_LOG(gFontSelection, PR_LOG_DEBUG,
|
|
|
|
("(FindFontForStyle) name: %s, sty: %02x, wt: %d, sz: %.1f -> %s (trial %d)\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mName).get(),
|
|
|
|
aFontStyle.style, aFontStyle.weight, aFontStyle.size,
|
|
|
|
NS_ConvertUTF16toUTF8(fe->Name()).get(), trial));
|
2010-03-11 20:26:43 -08:00
|
|
|
aNeedsSyntheticBold = wantBold && !fe->IsBold();
|
2009-08-16 06:52:12 -07:00
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this can't happen unless we have totally broken the font-list manager!
|
|
|
|
NS_NOTREACHED("no face found in simple font family!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a large/rich font family, so we do full style- and weight-matching:
|
|
|
|
// first collect a list of weights that are the best match for the requested
|
|
|
|
// font-stretch and font-style, then pick the best weight match among those
|
|
|
|
// available.
|
|
|
|
|
|
|
|
gfxFontEntry *weightList[10] = { 0 };
|
|
|
|
PRBool foundWeights = FindWeightsForStyle(weightList, wantItalic, aFontStyle.stretch);
|
|
|
|
if (!foundWeights) {
|
|
|
|
PR_LOG(gFontSelection, PR_LOG_DEBUG,
|
|
|
|
("(FindFontForStyle) name: %s, sty: %02x, wt: %d, sz: %.1f -> null\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mName).get(),
|
|
|
|
aFontStyle.style, aFontStyle.weight, aFontStyle.size));
|
|
|
|
return nsnull;
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2010-07-27 22:30:21 -07:00
|
|
|
// First find a match for the best weight
|
|
|
|
PRInt8 matchBaseWeight = 0;
|
|
|
|
PRInt8 i = baseWeight;
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2010-07-27 22:30:21 -07:00
|
|
|
// Need to special case when normal face doesn't exist but medium does.
|
|
|
|
// In that case, use medium otherwise weights < 400
|
|
|
|
if (baseWeight == 4 && !weightList[4]) {
|
|
|
|
i = 5; // medium
|
2008-08-05 21:34:06 -07:00
|
|
|
}
|
|
|
|
|
2010-07-27 22:30:21 -07:00
|
|
|
// Loop through weights, since one exists loop will terminate
|
2008-08-05 21:34:06 -07:00
|
|
|
PRInt8 direction = (baseWeight > 5) ? 1 : -1;
|
2010-07-27 22:30:21 -07:00
|
|
|
for (; ; i += direction) {
|
2008-08-05 21:34:06 -07:00
|
|
|
if (weightList[i]) {
|
|
|
|
matchBaseWeight = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-27 22:30:21 -07:00
|
|
|
// If we've reached one side without finding a font,
|
|
|
|
// start over and go the other direction until we find a match
|
2009-08-16 06:52:12 -07:00
|
|
|
if (i == 1 || i == 9) {
|
2010-07-27 22:30:21 -07:00
|
|
|
i = baseWeight;
|
2008-08-05 21:34:06 -07:00
|
|
|
direction = -direction;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
}
|
|
|
|
|
2010-07-27 22:30:21 -07:00
|
|
|
NS_ASSERTION(matchBaseWeight != 0,
|
|
|
|
"weight mapping should always find at least one font in a family");
|
|
|
|
|
|
|
|
gfxFontEntry *matchFE = weightList[matchBaseWeight];
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2010-07-27 22:30:21 -07:00
|
|
|
NS_ASSERTION(matchFE,
|
|
|
|
"weight mapping should always find at least one font in a family");
|
2010-03-11 20:26:43 -08:00
|
|
|
|
2010-11-10 07:49:52 -08:00
|
|
|
if (!matchFE->IsBold() && baseWeight >= 6)
|
2010-03-11 20:26:43 -08:00
|
|
|
{
|
|
|
|
aNeedsSyntheticBold = PR_TRUE;
|
2008-08-05 21:34:06 -07:00
|
|
|
}
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
PR_LOG(gFontSelection, PR_LOG_DEBUG,
|
|
|
|
("(FindFontForStyle) name: %s, sty: %02x, wt: %d, sz: %.1f -> %s\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mName).get(),
|
|
|
|
aFontStyle.style, aFontStyle.weight, aFontStyle.size,
|
|
|
|
NS_ConvertUTF16toUTF8(matchFE->Name()).get()));
|
2008-08-05 21:34:06 -07:00
|
|
|
return matchFE;
|
|
|
|
}
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
void
|
|
|
|
gfxFontFamily::CheckForSimpleFamily()
|
|
|
|
{
|
2010-02-11 03:59:47 -08:00
|
|
|
if (mAvailableFonts.Length() > 4 || mAvailableFonts.Length() == 0) {
|
|
|
|
return; // can't be "simple" if there are >4 faces;
|
|
|
|
// if none then the family is unusable anyway
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt16 firstStretch = mAvailableFonts[0]->Stretch();
|
|
|
|
|
|
|
|
gfxFontEntry *faces[4] = { 0 };
|
|
|
|
for (PRUint8 i = 0; i < mAvailableFonts.Length(); ++i) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
if (fe->Stretch() != firstStretch) {
|
|
|
|
return; // font-stretch doesn't match, don't treat as simple family
|
|
|
|
}
|
|
|
|
PRUint8 faceIndex = (fe->IsItalic() ? kItalicMask : 0) |
|
|
|
|
(fe->Weight() >= 600 ? kBoldMask : 0);
|
|
|
|
if (faces[faceIndex]) {
|
|
|
|
return; // two faces resolve to the same slot; family isn't "simple"
|
|
|
|
}
|
|
|
|
faces[faceIndex] = fe;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have successfully slotted the available faces into the standard
|
|
|
|
// 4-face framework
|
|
|
|
mAvailableFonts.SetLength(4);
|
|
|
|
for (PRUint8 i = 0; i < 4; ++i) {
|
|
|
|
if (mAvailableFonts[i].get() != faces[i]) {
|
|
|
|
mAvailableFonts[i].swap(faces[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsSimpleFamily = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline PRUint32
|
|
|
|
StyleDistance(gfxFontEntry *aFontEntry,
|
|
|
|
PRBool anItalic, PRInt16 aStretch)
|
|
|
|
{
|
|
|
|
// Compute a measure of the "distance" between the requested style
|
|
|
|
// and the given fontEntry,
|
|
|
|
// considering italicness and font-stretch but not weight.
|
|
|
|
|
|
|
|
// TODO (refine CSS spec...): discuss priority of italic vs stretch;
|
|
|
|
// whether penalty for stretch mismatch should depend on actual difference in values;
|
|
|
|
// whether a sign mismatch in stretch should increase the effective distance
|
|
|
|
|
|
|
|
return (aFontEntry->IsItalic() != anItalic ? 1 : 0) +
|
|
|
|
(aFontEntry->mStretch != aStretch ? 10 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontFamily::FindWeightsForStyle(gfxFontEntry* aFontsForWeights[],
|
|
|
|
PRBool anItalic, PRInt16 aStretch)
|
|
|
|
{
|
|
|
|
PRUint32 foundWeights = 0;
|
|
|
|
PRUint32 bestMatchDistance = 0xffffffff;
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < mAvailableFonts.Length(); i++) {
|
|
|
|
// this is not called for "simple" families, and therefore it does not
|
|
|
|
// need to check the mAvailableFonts entries for NULL
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
PRUint32 distance = StyleDistance(fe, anItalic, aStretch);
|
|
|
|
if (distance <= bestMatchDistance) {
|
|
|
|
PRInt8 wt = fe->mWeight / 100;
|
|
|
|
NS_ASSERTION(wt >= 1 && wt < 10, "invalid weight in fontEntry");
|
|
|
|
if (!aFontsForWeights[wt]) {
|
|
|
|
// record this as a possible candidate for weight matching
|
|
|
|
aFontsForWeights[wt] = fe;
|
|
|
|
++foundWeights;
|
|
|
|
} else {
|
|
|
|
PRUint32 prevDistance = StyleDistance(aFontsForWeights[wt], anItalic, aStretch);
|
|
|
|
if (prevDistance >= distance) {
|
|
|
|
// replacing a weight we already found, so don't increment foundWeights
|
|
|
|
aFontsForWeights[wt] = fe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bestMatchDistance = distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(foundWeights > 0, "Font family containing no faces?");
|
|
|
|
|
|
|
|
if (foundWeights == 1) {
|
|
|
|
// no need to cull entries if we only found one weight
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we might have recorded some faces that were a partial style match, but later found
|
|
|
|
// others that were closer; in this case, we need to cull the poorer matches from the
|
|
|
|
// weight list we'll return
|
|
|
|
for (PRUint32 i = 0; i < 10; ++i) {
|
|
|
|
if (aFontsForWeights[i] &&
|
|
|
|
StyleDistance(aFontsForWeights[i], anItalic, aStretch) > bestMatchDistance)
|
|
|
|
{
|
|
|
|
aFontsForWeights[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (foundWeights > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void gfxFontFamily::LocalizedName(nsAString& aLocalizedName)
|
|
|
|
{
|
|
|
|
// just return the primary name; subclasses should override
|
|
|
|
aLocalizedName = mName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontFamily::FindFontForChar(FontSearch *aMatchData)
|
|
|
|
{
|
2009-10-12 16:43:43 -07:00
|
|
|
if (!mHasStyles)
|
|
|
|
FindStyleVariations();
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// xxx - optimization point - keep a bit vector with the union of supported unicode ranges
|
|
|
|
// by all fonts for this family and bail immediately if the character is not in any of
|
|
|
|
// this family's cmaps
|
|
|
|
|
|
|
|
// iterate over fonts
|
|
|
|
PRUint32 numFonts = mAvailableFonts.Length();
|
|
|
|
for (PRUint32 i = 0; i < numFonts; i++) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
2010-06-25 05:43:10 -07:00
|
|
|
|
|
|
|
// skip certain fonts during system fallback
|
|
|
|
if (!fe || fe->SkipDuringSystemFallback())
|
2009-08-16 06:52:12 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
PRInt32 rank = 0;
|
|
|
|
|
|
|
|
if (fe->TestCharacterMap(aMatchData->mCh)) {
|
|
|
|
rank += 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we didn't match any characters don't bother wasting more time with this face.
|
|
|
|
if (rank == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// omitting from original windows code -- family name, lang group, pitch
|
|
|
|
// not available in current FontEntry implementation
|
|
|
|
|
|
|
|
if (aMatchData->mFontToMatch) {
|
|
|
|
const gfxFontStyle *style = aMatchData->mFontToMatch->GetStyle();
|
|
|
|
|
|
|
|
// italics
|
|
|
|
if (fe->IsItalic() &&
|
|
|
|
(style->style & (FONT_STYLE_ITALIC | FONT_STYLE_OBLIQUE)) != 0) {
|
|
|
|
rank += 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
// weight
|
2010-11-10 07:49:52 -08:00
|
|
|
PRInt32 targetWeight = style->ComputeWeight() * 100;
|
2009-08-16 06:52:12 -07:00
|
|
|
|
|
|
|
PRInt32 entryWeight = fe->Weight();
|
|
|
|
if (entryWeight == targetWeight) {
|
|
|
|
rank += 5;
|
|
|
|
} else {
|
|
|
|
PRUint32 diffWeight = abs(entryWeight - targetWeight);
|
|
|
|
if (diffWeight <= 100) // favor faces close in weight
|
|
|
|
rank += 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if no font to match, prefer non-bold, non-italic fonts
|
|
|
|
if (!fe->IsItalic() && !fe->IsBold())
|
|
|
|
rank += 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
// xxx - add whether AAT font with morphing info for specific lang groups
|
|
|
|
|
|
|
|
if (rank > aMatchData->mMatchRank
|
|
|
|
|| (rank == aMatchData->mMatchRank &&
|
|
|
|
Compare(fe->Name(), aMatchData->mBestMatch->Name()) > 0))
|
|
|
|
{
|
|
|
|
aMatchData->mBestMatch = fe;
|
|
|
|
aMatchData->mMatchRank = rank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if other names were found, false otherwise
|
|
|
|
PRBool
|
2010-01-28 17:41:25 -08:00
|
|
|
gfxFontFamily::ReadOtherFamilyNamesForFace(gfxPlatformFontList *aPlatformFontList,
|
|
|
|
nsTArray<PRUint8>& aNameTable,
|
2009-09-28 03:25:22 -07:00
|
|
|
PRBool useFullName)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
2010-01-28 17:41:25 -08:00
|
|
|
const PRUint8 *nameData = aNameTable.Elements();
|
|
|
|
PRUint32 dataLength = aNameTable.Length();
|
2009-08-16 06:52:12 -07:00
|
|
|
const gfxFontUtils::NameHeader *nameHeader =
|
|
|
|
reinterpret_cast<const gfxFontUtils::NameHeader*>(nameData);
|
|
|
|
|
|
|
|
PRUint32 nameCount = nameHeader->count;
|
|
|
|
if (nameCount * sizeof(gfxFontUtils::NameRecord) > dataLength) {
|
|
|
|
NS_WARNING("invalid font (name records)");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gfxFontUtils::NameRecord *nameRecord =
|
|
|
|
reinterpret_cast<const gfxFontUtils::NameRecord*>(nameData + sizeof(gfxFontUtils::NameHeader));
|
|
|
|
PRUint32 stringsBase = PRUint32(nameHeader->stringOffset);
|
|
|
|
|
|
|
|
PRBool foundNames = PR_FALSE;
|
|
|
|
for (PRUint32 i = 0; i < nameCount; i++, nameRecord++) {
|
|
|
|
PRUint32 nameLen = nameRecord->length;
|
|
|
|
PRUint32 nameOff = nameRecord->offset; // offset from base of string storage
|
|
|
|
|
|
|
|
if (stringsBase + nameOff + nameLen > dataLength) {
|
|
|
|
NS_WARNING("invalid font (name table strings)");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint16 nameID = nameRecord->nameID;
|
|
|
|
if ((useFullName && nameID == gfxFontUtils::NAME_ID_FULL) ||
|
|
|
|
(!useFullName && (nameID == gfxFontUtils::NAME_ID_FAMILY ||
|
|
|
|
nameID == gfxFontUtils::NAME_ID_PREFERRED_FAMILY))) {
|
|
|
|
nsAutoString otherFamilyName;
|
|
|
|
PRBool ok = gfxFontUtils::DecodeFontName(nameData + stringsBase + nameOff,
|
|
|
|
nameLen,
|
|
|
|
PRUint32(nameRecord->platformID),
|
|
|
|
PRUint32(nameRecord->encodingID),
|
|
|
|
PRUint32(nameRecord->languageID),
|
|
|
|
otherFamilyName);
|
|
|
|
// add if not same as canonical family name
|
|
|
|
if (ok && otherFamilyName != mName) {
|
2010-01-28 17:41:25 -08:00
|
|
|
aPlatformFontList->AddOtherFamilyName(this, otherFamilyName);
|
2009-08-16 06:52:12 -07:00
|
|
|
foundNames = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-01-28 17:41:25 -08:00
|
|
|
gfxFontFamily::ReadOtherFamilyNames(gfxPlatformFontList *aPlatformFontList)
|
2009-08-16 06:52:12 -07:00
|
|
|
{
|
|
|
|
if (mOtherFamilyNamesInitialized)
|
|
|
|
return;
|
|
|
|
mOtherFamilyNamesInitialized = PR_TRUE;
|
|
|
|
|
2009-10-12 16:43:43 -07:00
|
|
|
FindStyleVariations();
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
// read in other family names for the first face in the list
|
2010-01-28 17:41:25 -08:00
|
|
|
PRUint32 i, numFonts = mAvailableFonts.Length();
|
|
|
|
const PRUint32 kNAME = TRUETYPE_TAG('n','a','m','e');
|
|
|
|
nsAutoTArray<PRUint8,8192> buffer;
|
|
|
|
|
2009-08-16 06:52:12 -07:00
|
|
|
for (i = 0; i < numFonts; ++i) {
|
2010-01-28 17:41:25 -08:00
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
if (!fe)
|
2009-08-16 06:52:12 -07:00
|
|
|
continue;
|
2010-01-28 17:41:25 -08:00
|
|
|
|
|
|
|
if (fe->GetFontTable(kNAME, buffer) != NS_OK)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mHasOtherFamilyNames = ReadOtherFamilyNamesForFace(aPlatformFontList,
|
|
|
|
buffer);
|
2009-08-16 06:52:12 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read in other names for the first face in the list with the assumption
|
|
|
|
// that if extra names don't exist in that face then they don't exist in
|
|
|
|
// other faces for the same font
|
2010-01-28 17:41:25 -08:00
|
|
|
if (!mHasOtherFamilyNames)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// read in names for all faces, needed to catch cases where fonts have
|
|
|
|
// family names for individual weights (e.g. Hiragino Kaku Gothic Pro W6)
|
|
|
|
for ( ; i < numFonts; i++) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
if (!fe)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fe->GetFontTable(kNAME, buffer) != NS_OK)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ReadOtherFamilyNamesForFace(aPlatformFontList, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontFamily::ReadFaceNames(gfxPlatformFontList *aPlatformFontList,
|
|
|
|
PRBool aNeedFullnamePostscriptNames)
|
|
|
|
{
|
|
|
|
// if all needed names have already been read, skip
|
|
|
|
if (mOtherFamilyNamesInitialized &&
|
|
|
|
(mFaceNamesInitialized || !aNeedFullnamePostscriptNames))
|
|
|
|
return;
|
|
|
|
|
|
|
|
FindStyleVariations();
|
|
|
|
|
|
|
|
PRUint32 i, numFonts = mAvailableFonts.Length();
|
|
|
|
const PRUint32 kNAME = TRUETYPE_TAG('n','a','m','e');
|
|
|
|
nsAutoTArray<PRUint8,8192> buffer;
|
|
|
|
nsAutoString fullname, psname;
|
|
|
|
|
|
|
|
PRBool firstTime = PR_TRUE, readAllFaces = PR_FALSE;
|
|
|
|
for (i = 0; i < numFonts; ++i) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
if (!fe)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fe->GetFontTable(kNAME, buffer) != NS_OK)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (aNeedFullnamePostscriptNames) {
|
|
|
|
if (gfxFontUtils::ReadCanonicalName(
|
|
|
|
buffer, gfxFontUtils::NAME_ID_FULL, fullname) == NS_OK)
|
|
|
|
{
|
|
|
|
aPlatformFontList->AddFullname(fe, fullname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfxFontUtils::ReadCanonicalName(
|
|
|
|
buffer, gfxFontUtils::NAME_ID_POSTSCRIPT, psname) == NS_OK)
|
|
|
|
{
|
|
|
|
aPlatformFontList->AddPostscriptName(fe, psname);
|
|
|
|
}
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
2010-01-28 17:41:25 -08:00
|
|
|
|
|
|
|
if (!mOtherFamilyNamesInitialized && (firstTime || readAllFaces)) {
|
|
|
|
PRBool foundOtherName = ReadOtherFamilyNamesForFace(aPlatformFontList,
|
|
|
|
buffer);
|
|
|
|
|
|
|
|
// if the first face has a different name, scan all faces, otherwise
|
|
|
|
// assume the family doesn't have other names
|
|
|
|
if (firstTime && foundOtherName) {
|
|
|
|
mHasOtherFamilyNames = PR_TRUE;
|
|
|
|
readAllFaces = PR_TRUE;
|
|
|
|
}
|
|
|
|
firstTime = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not reading in any more names, skip other faces
|
|
|
|
if (!readAllFaces && !aNeedFullnamePostscriptNames)
|
|
|
|
break;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
2010-01-28 17:41:25 -08:00
|
|
|
|
|
|
|
mFaceNamesInitialized = PR_TRUE;
|
|
|
|
mOtherFamilyNamesInitialized = PR_TRUE;
|
2009-08-16 06:52:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gfxFontEntry*
|
|
|
|
gfxFontFamily::FindFont(const nsAString& aPostscriptName)
|
|
|
|
{
|
|
|
|
// find the font using a simple linear search
|
|
|
|
PRUint32 numFonts = mAvailableFonts.Length();
|
|
|
|
for (PRUint32 i = 0; i < numFonts; i++) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i].get();
|
|
|
|
if (fe && fe->Name() == aPostscriptName)
|
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-03 20:32:43 -07:00
|
|
|
nsresult
|
|
|
|
gfxFontCache::Init()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!gGlobalCache, "Where did this come from?");
|
|
|
|
gGlobalCache = new gfxFontCache();
|
|
|
|
return gGlobalCache ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontCache::Shutdown()
|
|
|
|
{
|
|
|
|
delete gGlobalCache;
|
|
|
|
gGlobalCache = nsnull;
|
2007-07-01 18:12:45 -07:00
|
|
|
|
2007-07-01 18:24:56 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
2007-07-01 18:12:45 -07:00
|
|
|
printf("Textrun storage high water mark=%d\n", gTextRunStorageHighWaterMark);
|
2007-10-01 18:47:40 -07:00
|
|
|
printf("Total number of fonts=%d\n", gFontCount);
|
|
|
|
printf("Total glyph extents allocated=%d (size %d)\n", gGlyphExtentsCount,
|
|
|
|
int(gGlyphExtentsCount*sizeof(gfxGlyphExtents)));
|
|
|
|
printf("Total glyph extents width-storage size allocated=%d\n", gGlyphExtentsWidthsTotalSize);
|
|
|
|
printf("Number of simple glyph extents eagerly requested=%d\n", gGlyphExtentsSetupEagerSimple);
|
|
|
|
printf("Number of tight glyph extents eagerly requested=%d\n", gGlyphExtentsSetupEagerTight);
|
|
|
|
printf("Number of tight glyph extents lazily requested=%d\n", gGlyphExtentsSetupLazyTight);
|
|
|
|
printf("Number of simple glyph extent setups that fell back to tight=%d\n", gGlyphExtentsSetupFallBackToTight);
|
2007-07-01 18:12:45 -07:00
|
|
|
#endif
|
2007-04-03 20:32:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontCache::HashEntry::KeyEquals(const KeyTypePointer aKey) const
|
|
|
|
{
|
2010-07-13 13:31:31 -07:00
|
|
|
return aKey->mFontEntry == mFont->GetFontEntry() &&
|
2007-04-03 20:32:43 -07:00
|
|
|
aKey->mStyle->Equals(*mFont->GetStyle());
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxFont>
|
2010-07-13 13:31:31 -07:00
|
|
|
gfxFontCache::Lookup(const gfxFontEntry *aFontEntry,
|
2007-04-03 20:32:43 -07:00
|
|
|
const gfxFontStyle *aStyle)
|
|
|
|
{
|
2010-07-13 13:31:31 -07:00
|
|
|
Key key(aFontEntry, aStyle);
|
2007-04-03 20:32:43 -07:00
|
|
|
HashEntry *entry = mFonts.GetEntry(key);
|
|
|
|
if (!entry)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
gfxFont *font = entry->mFont;
|
|
|
|
NS_ADDREF(font);
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontCache::AddNew(gfxFont *aFont)
|
|
|
|
{
|
2010-07-13 13:31:31 -07:00
|
|
|
Key key(aFont->GetFontEntry(), aFont->GetStyle());
|
2007-04-03 20:32:43 -07:00
|
|
|
HashEntry *entry = mFonts.PutEntry(key);
|
|
|
|
if (!entry)
|
|
|
|
return;
|
2008-01-21 19:11:46 -08:00
|
|
|
gfxFont *oldFont = entry->mFont;
|
2007-04-03 20:32:43 -07:00
|
|
|
entry->mFont = aFont;
|
2008-01-21 19:11:46 -08:00
|
|
|
// If someone's asked us to replace an existing font entry, then that's a
|
|
|
|
// bit weird, but let it happen, and expire the old font if it's not used.
|
|
|
|
if (oldFont && oldFont->GetExpirationState()->IsTracked()) {
|
|
|
|
// if oldFont == aFont, recount should be > 0,
|
|
|
|
// so we shouldn't be here.
|
|
|
|
NS_ASSERTION(aFont != oldFont, "new font is tracked for expiry!");
|
|
|
|
NotifyExpired(oldFont);
|
|
|
|
}
|
2007-04-03 20:32:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontCache::NotifyReleased(gfxFont *aFont)
|
|
|
|
{
|
|
|
|
nsresult rv = AddObject(aFont);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We couldn't track it for some reason. Kill it now.
|
|
|
|
DestroyFont(aFont);
|
|
|
|
}
|
|
|
|
// Note that we might have fonts that aren't in the hashtable, perhaps because
|
|
|
|
// of OOM adding to the hashtable or because someone did an AddNew where
|
|
|
|
// we already had a font. These fonts are added to the expiration tracker
|
|
|
|
// anyway, even though Lookup can't resurrect them. Eventually they will
|
|
|
|
// expire and be deleted.
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontCache::NotifyExpired(gfxFont *aFont)
|
|
|
|
{
|
|
|
|
RemoveObject(aFont);
|
|
|
|
DestroyFont(aFont);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontCache::DestroyFont(gfxFont *aFont)
|
|
|
|
{
|
2010-07-13 13:31:31 -07:00
|
|
|
Key key(aFont->GetFontEntry(), aFont->GetStyle());
|
2008-01-21 19:11:46 -08:00
|
|
|
HashEntry *entry = mFonts.GetEntry(key);
|
|
|
|
if (entry && entry->mFont == aFont)
|
|
|
|
mFonts.RemoveEntry(key);
|
|
|
|
NS_ASSERTION(aFont->GetRefCount() == 0,
|
|
|
|
"Destroying with non-zero ref count!");
|
2007-04-03 20:32:43 -07:00
|
|
|
delete aFont;
|
|
|
|
}
|
|
|
|
|
2008-08-12 02:34:52 -07:00
|
|
|
void
|
|
|
|
gfxFont::RunMetrics::CombineWith(const RunMetrics& aOther, PRBool aOtherIsOnLeft)
|
|
|
|
{
|
|
|
|
mAscent = PR_MAX(mAscent, aOther.mAscent);
|
|
|
|
mDescent = PR_MAX(mDescent, aOther.mDescent);
|
|
|
|
if (aOtherIsOnLeft) {
|
|
|
|
mBoundingBox =
|
|
|
|
(mBoundingBox + gfxPoint(aOther.mAdvanceWidth, 0)).Union(aOther.mBoundingBox);
|
|
|
|
} else {
|
|
|
|
mBoundingBox =
|
|
|
|
mBoundingBox.Union(aOther.mBoundingBox + gfxPoint(mAdvanceWidth, 0));
|
|
|
|
}
|
|
|
|
mAdvanceWidth += aOther.mAdvanceWidth;
|
|
|
|
}
|
|
|
|
|
2010-03-15 02:34:25 -07:00
|
|
|
gfxFont::gfxFont(gfxFontEntry *aFontEntry, const gfxFontStyle *aFontStyle,
|
|
|
|
AntialiasOption anAAOption) :
|
2010-03-10 04:46:41 -08:00
|
|
|
mFontEntry(aFontEntry), mIsValid(PR_TRUE),
|
2009-10-07 10:16:52 -07:00
|
|
|
mStyle(*aFontStyle),
|
|
|
|
mAdjustedSize(0.0),
|
|
|
|
mFUnitsConvFactor(0.0f),
|
|
|
|
mSyntheticBoldOffset(0),
|
2010-03-15 02:34:25 -07:00
|
|
|
mAntialiasOption(anAAOption),
|
2009-10-07 10:16:52 -07:00
|
|
|
mPlatformShaper(nsnull),
|
|
|
|
mHarfBuzzShaper(nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-10-01 18:47:40 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
|
|
|
++gFontCount;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-23 19:19:14 -07:00
|
|
|
gfxFont::~gfxFont()
|
|
|
|
{
|
|
|
|
PRUint32 i;
|
|
|
|
// We destroy the contents of mGlyphExtentsArray explicitly instead of
|
|
|
|
// using nsAutoPtr because VC++ can't deal with nsTArrays of nsAutoPtrs
|
|
|
|
// of classes that lack a proper copy constructor
|
|
|
|
for (i = 0; i < mGlyphExtentsArray.Length(); ++i) {
|
|
|
|
delete mGlyphExtentsArray[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* A helper function in case we need to do any rounding or other
|
|
|
|
* processing here.
|
|
|
|
*/
|
2008-09-03 19:38:32 -07:00
|
|
|
#define ToDeviceUnits(aAppUnits, aDevUnitsPerAppUnit) \
|
|
|
|
(double(aAppUnits)*double(aDevUnitsPerAppUnit))
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-17 16:13:48 -07:00
|
|
|
struct GlyphBuffer {
|
|
|
|
#define GLYPH_BUFFER_SIZE (2048/sizeof(cairo_glyph_t))
|
|
|
|
cairo_glyph_t mGlyphBuffer[GLYPH_BUFFER_SIZE];
|
|
|
|
unsigned int mNumGlyphs;
|
|
|
|
|
|
|
|
GlyphBuffer()
|
|
|
|
: mNumGlyphs(0) { }
|
|
|
|
|
|
|
|
cairo_glyph_t *AppendGlyph() {
|
|
|
|
return &mGlyphBuffer[mNumGlyphs++];
|
|
|
|
}
|
|
|
|
|
2008-09-03 19:38:32 -07:00
|
|
|
void Flush(cairo_t *aCR, PRBool aDrawToPath, PRBool aReverse,
|
|
|
|
PRBool aFinish = PR_FALSE) {
|
|
|
|
// Ensure there's enough room for at least two glyphs in the
|
|
|
|
// buffer (because we may allocate two glyphs between flushes)
|
|
|
|
if (!aFinish && mNumGlyphs + 2 <= GLYPH_BUFFER_SIZE)
|
2007-07-17 16:13:48 -07:00
|
|
|
return;
|
|
|
|
|
2008-09-03 19:38:32 -07:00
|
|
|
if (aReverse) {
|
|
|
|
for (PRUint32 i = 0; i < mNumGlyphs/2; ++i) {
|
|
|
|
cairo_glyph_t tmp = mGlyphBuffer[i];
|
|
|
|
mGlyphBuffer[i] = mGlyphBuffer[mNumGlyphs - 1 - i];
|
|
|
|
mGlyphBuffer[mNumGlyphs - 1 - i] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aDrawToPath)
|
|
|
|
cairo_glyph_path(aCR, mGlyphBuffer, mNumGlyphs);
|
2007-07-17 16:13:48 -07:00
|
|
|
else
|
2008-09-03 19:38:32 -07:00
|
|
|
cairo_show_glyphs(aCR, mGlyphBuffer, mNumGlyphs);
|
2007-07-17 16:13:48 -07:00
|
|
|
|
|
|
|
mNumGlyphs = 0;
|
|
|
|
}
|
|
|
|
#undef GLYPH_BUFFER_SIZE
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
gfxFont::Draw(gfxTextRun *aTextRun, PRUint32 aStart, PRUint32 aEnd,
|
|
|
|
gfxContext *aContext, PRBool aDrawToPath, gfxPoint *aPt,
|
|
|
|
Spacing *aSpacing)
|
|
|
|
{
|
|
|
|
if (aStart >= aEnd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const gfxTextRun::CompressedGlyph *charGlyphs = aTextRun->GetCharacterGlyphs();
|
|
|
|
const PRUint32 appUnitsPerDevUnit = aTextRun->GetAppUnitsPerDevUnit();
|
|
|
|
const double devUnitsPerAppUnit = 1.0/double(appUnitsPerDevUnit);
|
|
|
|
PRBool isRTL = aTextRun->IsRightToLeft();
|
|
|
|
double direction = aTextRun->GetDirection();
|
2008-09-03 19:38:32 -07:00
|
|
|
// double-strike in direction of run
|
|
|
|
double synBoldDevUnitOffsetAppUnits =
|
|
|
|
direction * (double) mSyntheticBoldOffset * appUnitsPerDevUnit;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 i;
|
|
|
|
// Current position in appunits
|
|
|
|
double x = aPt->x;
|
|
|
|
double y = aPt->y;
|
|
|
|
|
2007-09-23 19:19:14 -07:00
|
|
|
PRBool success = SetupCairoFont(aContext);
|
2007-08-06 05:30:13 -07:00
|
|
|
if (NS_UNLIKELY(!success))
|
|
|
|
return;
|
2007-07-17 16:13:48 -07:00
|
|
|
|
|
|
|
GlyphBuffer glyphs;
|
|
|
|
cairo_glyph_t *glyph;
|
2007-09-23 19:19:14 -07:00
|
|
|
cairo_t *cr = aContext->GetCairo();
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aSpacing) {
|
|
|
|
x += direction*aSpacing[0].mBefore;
|
|
|
|
}
|
|
|
|
for (i = aStart; i < aEnd; ++i) {
|
|
|
|
const gfxTextRun::CompressedGlyph *glyphData = &charGlyphs[i];
|
|
|
|
if (glyphData->IsSimpleGlyph()) {
|
2007-07-17 16:13:48 -07:00
|
|
|
glyph = glyphs.AppendGlyph();
|
2007-03-22 10:30:00 -07:00
|
|
|
glyph->index = glyphData->GetSimpleGlyph();
|
|
|
|
double advance = glyphData->GetSimpleAdvance();
|
|
|
|
// Perhaps we should put a scale in the cairo context instead of
|
|
|
|
// doing this scaling here...
|
|
|
|
// Multiplying by the reciprocal may introduce tiny error here,
|
|
|
|
// but we assume cairo is going to round coordinates at some stage
|
|
|
|
// and this is faster
|
2008-09-03 19:38:32 -07:00
|
|
|
double glyphX;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (isRTL) {
|
|
|
|
x -= advance;
|
2008-09-03 19:38:32 -07:00
|
|
|
glyphX = x;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-09-03 19:38:32 -07:00
|
|
|
glyphX = x;
|
2007-03-22 10:30:00 -07:00
|
|
|
x += advance;
|
|
|
|
}
|
2008-09-03 19:38:32 -07:00
|
|
|
glyph->x = ToDeviceUnits(glyphX, devUnitsPerAppUnit);
|
|
|
|
glyph->y = ToDeviceUnits(y, devUnitsPerAppUnit);
|
2008-03-19 17:02:21 -07:00
|
|
|
|
|
|
|
// synthetic bolding by drawing with a one-pixel offset
|
|
|
|
if (mSyntheticBoldOffset) {
|
|
|
|
cairo_glyph_t *doubleglyph;
|
|
|
|
doubleglyph = glyphs.AppendGlyph();
|
|
|
|
doubleglyph->index = glyph->index;
|
2008-09-03 19:38:32 -07:00
|
|
|
doubleglyph->x =
|
|
|
|
ToDeviceUnits(glyphX + synBoldDevUnitOffsetAppUnits,
|
|
|
|
devUnitsPerAppUnit);
|
2008-03-19 17:02:21 -07:00
|
|
|
doubleglyph->y = glyph->y;
|
|
|
|
}
|
|
|
|
|
2008-09-03 19:38:32 -07:00
|
|
|
glyphs.Flush(cr, aDrawToPath, isRTL);
|
2007-11-08 22:27:23 -08:00
|
|
|
} else {
|
|
|
|
PRUint32 j;
|
|
|
|
PRUint32 glyphCount = glyphData->GetGlyphCount();
|
2007-11-07 23:41:06 -08:00
|
|
|
const gfxTextRun::DetailedGlyph *details = aTextRun->GetDetailedGlyphs(i);
|
2007-11-08 22:27:23 -08:00
|
|
|
for (j = 0; j < glyphCount; ++j, ++details) {
|
2007-11-07 23:41:06 -08:00
|
|
|
double advance = details->mAdvance;
|
2007-11-08 22:27:23 -08:00
|
|
|
if (glyphData->IsMissing()) {
|
2010-04-28 14:51:16 -07:00
|
|
|
// default ignorable characters will have zero advance width.
|
|
|
|
// we don't have to draw the hexbox for them
|
|
|
|
if (!aDrawToPath && advance > 0) {
|
2008-09-03 19:38:32 -07:00
|
|
|
double glyphX = x;
|
2007-11-08 22:27:23 -08:00
|
|
|
if (isRTL) {
|
2008-09-03 19:38:32 -07:00
|
|
|
glyphX -= advance;
|
2007-11-08 22:27:23 -08:00
|
|
|
}
|
2008-09-03 19:38:32 -07:00
|
|
|
gfxPoint pt(ToDeviceUnits(glyphX, devUnitsPerAppUnit),
|
|
|
|
ToDeviceUnits(y, devUnitsPerAppUnit));
|
|
|
|
gfxFloat advanceDevUnits = ToDeviceUnits(advance, devUnitsPerAppUnit);
|
2007-11-08 22:27:23 -08:00
|
|
|
gfxFloat height = GetMetrics().maxAscent;
|
|
|
|
gfxRect glyphRect(pt.x, pt.y - height, advanceDevUnits, height);
|
|
|
|
gfxFontMissingGlyphs::DrawMissingGlyph(aContext, glyphRect, details->mGlyphID);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
glyph = glyphs.AppendGlyph();
|
|
|
|
glyph->index = details->mGlyphID;
|
2008-09-03 19:38:32 -07:00
|
|
|
double glyphX = x + details->mXOffset;
|
2007-03-26 20:24:49 -07:00
|
|
|
if (isRTL) {
|
2008-09-03 19:38:32 -07:00
|
|
|
glyphX -= advance;
|
2007-03-26 20:24:49 -07:00
|
|
|
}
|
2008-09-03 19:38:32 -07:00
|
|
|
glyph->x = ToDeviceUnits(glyphX, devUnitsPerAppUnit);
|
|
|
|
glyph->y = ToDeviceUnits(y + details->mYOffset, devUnitsPerAppUnit);
|
2008-03-19 17:02:21 -07:00
|
|
|
|
|
|
|
// synthetic bolding by drawing with a one-pixel offset
|
|
|
|
if (mSyntheticBoldOffset) {
|
|
|
|
cairo_glyph_t *doubleglyph;
|
|
|
|
doubleglyph = glyphs.AppendGlyph();
|
|
|
|
doubleglyph->index = glyph->index;
|
2008-09-03 19:38:32 -07:00
|
|
|
doubleglyph->x =
|
|
|
|
ToDeviceUnits(glyphX + synBoldDevUnitOffsetAppUnits,
|
|
|
|
devUnitsPerAppUnit);
|
2008-03-19 17:02:21 -07:00
|
|
|
doubleglyph->y = glyph->y;
|
|
|
|
}
|
|
|
|
|
2008-09-03 19:38:32 -07:00
|
|
|
glyphs.Flush(cr, aDrawToPath, isRTL);
|
2007-03-26 20:24:49 -07:00
|
|
|
}
|
|
|
|
x += direction*advance;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-08 22:27:23 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aSpacing) {
|
|
|
|
double space = aSpacing[i - aStart].mAfter;
|
|
|
|
if (i + 1 < aEnd) {
|
|
|
|
space += aSpacing[i + 1 - aStart].mBefore;
|
|
|
|
}
|
|
|
|
x += direction*space;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfxFontTestStore::CurrentStore()) {
|
2007-07-17 16:13:48 -07:00
|
|
|
/* This assumes that the tests won't have anything that results
|
|
|
|
* in more than GLYPH_BUFFER_SIZE glyphs. Do this before we
|
|
|
|
* flush, since that'll blow away the num_glyphs.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxFontTestStore::CurrentStore()->AddItem(GetUniqueName(),
|
2007-07-17 16:13:48 -07:00
|
|
|
glyphs.mGlyphBuffer, glyphs.mNumGlyphs);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-07-17 16:13:48 -07:00
|
|
|
|
|
|
|
// draw any remaining glyphs
|
2008-09-03 19:38:32 -07:00
|
|
|
glyphs.Flush(cr, aDrawToPath, isRTL, PR_TRUE);
|
2007-07-17 16:13:48 -07:00
|
|
|
|
|
|
|
*aPt = gfxPoint(x, y);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-11-08 22:27:23 -08:00
|
|
|
static PRInt32
|
|
|
|
GetAdvanceForGlyphs(gfxTextRun *aTextRun, PRUint32 aStart, PRUint32 aEnd)
|
|
|
|
{
|
|
|
|
const gfxTextRun::CompressedGlyph *glyphData = aTextRun->GetCharacterGlyphs() + aStart;
|
|
|
|
PRInt32 advance = 0;
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = aStart; i < aEnd; ++i, ++glyphData) {
|
|
|
|
if (glyphData->IsSimpleGlyph()) {
|
|
|
|
advance += glyphData->GetSimpleAdvance();
|
|
|
|
} else {
|
|
|
|
PRUint32 glyphCount = glyphData->GetGlyphCount();
|
|
|
|
const gfxTextRun::DetailedGlyph *details = aTextRun->GetDetailedGlyphs(i);
|
|
|
|
PRUint32 j;
|
|
|
|
for (j = 0; j < glyphCount; ++j, ++details) {
|
|
|
|
advance += details->mAdvance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return advance;
|
|
|
|
}
|
|
|
|
|
2007-09-23 19:19:14 -07:00
|
|
|
static void
|
2008-08-08 01:24:22 -07:00
|
|
|
UnionRange(gfxFloat aX, gfxFloat* aDestMin, gfxFloat* aDestMax)
|
2007-09-23 19:19:14 -07:00
|
|
|
{
|
2008-08-08 01:24:22 -07:00
|
|
|
*aDestMin = PR_MIN(*aDestMin, aX);
|
|
|
|
*aDestMax = PR_MAX(*aDestMax, aX);
|
2007-09-23 19:19:14 -07:00
|
|
|
}
|
|
|
|
|
2009-06-24 02:04:20 -07:00
|
|
|
// We get precise glyph extents if the textrun creator requested them, or
|
|
|
|
// if the font is a user font --- in which case the author may be relying
|
|
|
|
// on overflowing glyphs.
|
|
|
|
static PRBool
|
|
|
|
NeedsGlyphExtents(gfxFont *aFont, gfxTextRun *aTextRun)
|
|
|
|
{
|
|
|
|
return (aTextRun->GetFlags() & gfxTextRunFactory::TEXT_NEED_BOUNDING_BOX) ||
|
|
|
|
aFont->GetFontEntry()->IsUserFont();
|
|
|
|
}
|
|
|
|
|
2007-09-23 19:19:14 -07:00
|
|
|
static PRBool
|
|
|
|
NeedsGlyphExtents(gfxTextRun *aTextRun)
|
|
|
|
{
|
2009-06-24 02:04:20 -07:00
|
|
|
if (aTextRun->GetFlags() & gfxTextRunFactory::TEXT_NEED_BOUNDING_BOX)
|
|
|
|
return PR_TRUE;
|
|
|
|
PRUint32 numRuns;
|
|
|
|
const gfxTextRun::GlyphRun *glyphRuns = aTextRun->GetGlyphRuns(&numRuns);
|
|
|
|
for (PRUint32 i = 0; i < numRuns; ++i) {
|
|
|
|
if (glyphRuns[i].mFont->GetFontEntry()->IsUserFont())
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
2007-09-23 19:19:14 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxFont::RunMetrics
|
|
|
|
gfxFont::Measure(gfxTextRun *aTextRun,
|
|
|
|
PRUint32 aStart, PRUint32 aEnd,
|
2010-03-15 02:34:25 -07:00
|
|
|
BoundingBoxType aBoundingBoxType,
|
|
|
|
gfxContext *aRefContext,
|
2007-03-22 10:30:00 -07:00
|
|
|
Spacing *aSpacing)
|
|
|
|
{
|
2010-03-15 02:34:25 -07:00
|
|
|
// If aBoundingBoxType is TIGHT_HINTED_OUTLINE_EXTENTS
|
|
|
|
// and the underlying cairo font may be antialiased,
|
|
|
|
// we need to create a copy in order to avoid getting cached extents.
|
2010-05-12 05:17:55 -07:00
|
|
|
// This is only used by MathML layout at present.
|
2010-03-15 02:34:25 -07:00
|
|
|
if (aBoundingBoxType == TIGHT_HINTED_OUTLINE_EXTENTS &&
|
|
|
|
mAntialiasOption != kAntialiasNone) {
|
2010-05-12 05:17:55 -07:00
|
|
|
if (!mNonAAFont) {
|
|
|
|
mNonAAFont = CopyWithAntialiasOption(kAntialiasNone);
|
|
|
|
}
|
2010-03-15 02:34:25 -07:00
|
|
|
// if font subclass doesn't implement CopyWithAntialiasOption(),
|
|
|
|
// it will return null and we'll proceed to use the existing font
|
2010-05-12 05:17:55 -07:00
|
|
|
if (mNonAAFont) {
|
|
|
|
return mNonAAFont->Measure(aTextRun, aStart, aEnd,
|
|
|
|
TIGHT_HINTED_OUTLINE_EXTENTS,
|
|
|
|
aRefContext, aSpacing);
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-23 19:19:14 -07:00
|
|
|
const PRUint32 appUnitsPerDevUnit = aTextRun->GetAppUnitsPerDevUnit();
|
|
|
|
// Current position in appunits
|
|
|
|
const gfxFont::Metrics& fontMetrics = GetMetrics();
|
|
|
|
|
|
|
|
RunMetrics metrics;
|
|
|
|
metrics.mAscent = fontMetrics.maxAscent*appUnitsPerDevUnit;
|
|
|
|
metrics.mDescent = fontMetrics.maxDescent*appUnitsPerDevUnit;
|
2007-11-30 22:52:47 -08:00
|
|
|
if (aStart == aEnd) {
|
2008-08-08 01:24:22 -07:00
|
|
|
// exit now before we look at aSpacing[0], which is undefined
|
|
|
|
metrics.mBoundingBox = gfxRect(0, -metrics.mAscent, 0, metrics.mAscent + metrics.mDescent);
|
|
|
|
return metrics;
|
2007-11-30 22:52:47 -08:00
|
|
|
}
|
2007-09-23 19:19:14 -07:00
|
|
|
|
2008-08-08 01:24:22 -07:00
|
|
|
gfxFloat advanceMin = 0, advanceMax = 0;
|
2007-11-30 22:52:47 -08:00
|
|
|
const gfxTextRun::CompressedGlyph *charGlyphs = aTextRun->GetCharacterGlyphs();
|
|
|
|
PRBool isRTL = aTextRun->IsRightToLeft();
|
|
|
|
double direction = aTextRun->GetDirection();
|
2009-06-24 02:04:20 -07:00
|
|
|
PRBool needsGlyphExtents = NeedsGlyphExtents(this, aTextRun);
|
2007-09-23 19:19:14 -07:00
|
|
|
gfxGlyphExtents *extents =
|
2009-02-24 00:32:58 -08:00
|
|
|
(aBoundingBoxType == LOOSE_INK_EXTENTS &&
|
2009-06-24 02:04:20 -07:00
|
|
|
!needsGlyphExtents &&
|
2009-02-24 00:32:58 -08:00
|
|
|
!aTextRun->HasDetailedGlyphs()) ? nsnull
|
2007-09-23 19:19:14 -07:00
|
|
|
: GetOrCreateGlyphExtents(aTextRun->GetAppUnitsPerDevUnit());
|
|
|
|
double x = 0;
|
|
|
|
if (aSpacing) {
|
|
|
|
x += direction*aSpacing[0].mBefore;
|
|
|
|
}
|
|
|
|
PRUint32 i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = aStart; i < aEnd; ++i) {
|
2007-09-23 19:19:14 -07:00
|
|
|
const gfxTextRun::CompressedGlyph *glyphData = &charGlyphs[i];
|
|
|
|
if (glyphData->IsSimpleGlyph()) {
|
|
|
|
double advance = glyphData->GetSimpleAdvance();
|
|
|
|
// Only get the real glyph horizontal extent if we were asked
|
|
|
|
// for the tight bounding box or we're in quality mode
|
2009-06-24 02:04:20 -07:00
|
|
|
if ((aBoundingBoxType != LOOSE_INK_EXTENTS || needsGlyphExtents) &&
|
|
|
|
extents) {
|
2007-09-23 19:19:14 -07:00
|
|
|
PRUint32 glyphIndex = glyphData->GetSimpleGlyph();
|
|
|
|
PRUint16 extentsWidth = extents->GetContainedGlyphWidthAppUnits(glyphIndex);
|
2009-02-24 00:32:58 -08:00
|
|
|
if (extentsWidth != gfxGlyphExtents::INVALID_WIDTH &&
|
|
|
|
aBoundingBoxType == LOOSE_INK_EXTENTS) {
|
2008-08-08 01:24:22 -07:00
|
|
|
UnionRange(x, &advanceMin, &advanceMax);
|
|
|
|
UnionRange(x + direction*extentsWidth, &advanceMin, &advanceMax);
|
2007-09-23 19:19:14 -07:00
|
|
|
} else {
|
2007-12-20 23:09:50 -08:00
|
|
|
gfxRect glyphRect;
|
|
|
|
if (!extents->GetTightGlyphExtentsAppUnits(this,
|
|
|
|
aRefContext, glyphIndex, &glyphRect)) {
|
|
|
|
glyphRect = gfxRect(0, metrics.mBoundingBox.Y(),
|
|
|
|
advance, metrics.mBoundingBox.Height());
|
|
|
|
}
|
2007-09-23 19:19:14 -07:00
|
|
|
if (isRTL) {
|
|
|
|
glyphRect.pos.x -= advance;
|
|
|
|
}
|
|
|
|
glyphRect.pos.x += x;
|
|
|
|
metrics.mBoundingBox = metrics.mBoundingBox.Union(glyphRect);
|
2007-09-22 05:43:52 -07:00
|
|
|
}
|
|
|
|
}
|
2007-09-23 19:19:14 -07:00
|
|
|
x += direction*advance;
|
2007-11-08 22:27:23 -08:00
|
|
|
} else {
|
|
|
|
PRUint32 glyphCount = glyphData->GetGlyphCount();
|
2007-09-23 19:19:14 -07:00
|
|
|
const gfxTextRun::DetailedGlyph *details = aTextRun->GetDetailedGlyphs(i);
|
2007-11-08 22:27:23 -08:00
|
|
|
PRUint32 j;
|
|
|
|
for (j = 0; j < glyphCount; ++j, ++details) {
|
2007-09-23 19:19:14 -07:00
|
|
|
PRUint32 glyphIndex = details->mGlyphID;
|
|
|
|
gfxPoint glyphPt(x + details->mXOffset, details->mYOffset);
|
|
|
|
double advance = details->mAdvance;
|
2007-12-20 23:09:50 -08:00
|
|
|
gfxRect glyphRect;
|
|
|
|
if (glyphData->IsMissing() || !extents ||
|
|
|
|
!extents->GetTightGlyphExtentsAppUnits(this,
|
|
|
|
aRefContext, glyphIndex, &glyphRect)) {
|
|
|
|
// We might have failed to get glyph extents due to
|
|
|
|
// OOM or something
|
2008-08-08 01:24:22 -07:00
|
|
|
glyphRect = gfxRect(0, -metrics.mAscent,
|
|
|
|
advance, metrics.mAscent + metrics.mDescent);
|
2007-12-20 23:09:50 -08:00
|
|
|
}
|
2007-09-23 19:19:14 -07:00
|
|
|
if (isRTL) {
|
|
|
|
glyphRect.pos.x -= advance;
|
|
|
|
}
|
|
|
|
glyphRect.pos.x += x;
|
|
|
|
metrics.mBoundingBox = metrics.mBoundingBox.Union(glyphRect);
|
|
|
|
x += direction*advance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Every other glyph type is ignored
|
|
|
|
if (aSpacing) {
|
|
|
|
double space = aSpacing[i - aStart].mAfter;
|
|
|
|
if (i + 1 < aEnd) {
|
|
|
|
space += aSpacing[i + 1 - aStart].mBefore;
|
|
|
|
}
|
|
|
|
x += direction*space;
|
2007-09-22 05:43:52 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-02-24 00:32:58 -08:00
|
|
|
if (aBoundingBoxType == LOOSE_INK_EXTENTS) {
|
2008-08-08 01:24:22 -07:00
|
|
|
UnionRange(x, &advanceMin, &advanceMax);
|
|
|
|
gfxRect fontBox(advanceMin, -metrics.mAscent,
|
|
|
|
advanceMax - advanceMin, metrics.mAscent + metrics.mDescent);
|
|
|
|
metrics.mBoundingBox = metrics.mBoundingBox.Union(fontBox);
|
2007-09-22 05:43:52 -07:00
|
|
|
}
|
2007-09-23 19:19:14 -07:00
|
|
|
if (isRTL) {
|
|
|
|
metrics.mBoundingBox.pos.x -= x;
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.mAdvanceWidth = x*direction;
|
2007-09-22 06:28:16 -07:00
|
|
|
return metrics;
|
2007-09-22 05:43:52 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
PRBool
|
2010-03-15 02:34:25 -07:00
|
|
|
gfxFont::InitTextRun(gfxContext *aContext,
|
|
|
|
gfxTextRun *aTextRun,
|
|
|
|
const PRUnichar *aString,
|
|
|
|
PRUint32 aRunStart,
|
2010-06-11 12:14:38 -07:00
|
|
|
PRUint32 aRunLength,
|
|
|
|
PRInt32 aRunScript)
|
2010-03-15 02:34:25 -07:00
|
|
|
{
|
2009-10-07 10:16:52 -07:00
|
|
|
PRBool ok = PR_FALSE;
|
|
|
|
|
|
|
|
if (mHarfBuzzShaper) {
|
|
|
|
if (gfxPlatform::GetPlatform()->UseHarfBuzzLevel() >=
|
|
|
|
gfxUnicodeProperties::ScriptShapingLevel(aRunScript)) {
|
|
|
|
ok = mHarfBuzzShaper->InitTextRun(aContext, aTextRun, aString,
|
|
|
|
aRunStart, aRunLength, aRunScript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
if (!mPlatformShaper) {
|
|
|
|
CreatePlatformShaper();
|
|
|
|
NS_ASSERTION(mPlatformShaper, "no platform shaper available!");
|
|
|
|
}
|
|
|
|
if (mPlatformShaper) {
|
|
|
|
ok = mPlatformShaper->InitTextRun(aContext, aTextRun, aString,
|
|
|
|
aRunStart, aRunLength, aRunScript);
|
|
|
|
}
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(ok, "shaper failed, expect scrambled or missing text");
|
2009-10-07 10:16:52 -07:00
|
|
|
return ok;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
2007-09-23 19:19:14 -07:00
|
|
|
gfxGlyphExtents *
|
|
|
|
gfxFont::GetOrCreateGlyphExtents(PRUint32 aAppUnitsPerDevUnit) {
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < mGlyphExtentsArray.Length(); ++i) {
|
|
|
|
if (mGlyphExtentsArray[i]->GetAppUnitsPerDevUnit() == aAppUnitsPerDevUnit)
|
|
|
|
return mGlyphExtentsArray[i];
|
|
|
|
}
|
|
|
|
gfxGlyphExtents *glyphExtents = new gfxGlyphExtents(aAppUnitsPerDevUnit);
|
|
|
|
if (glyphExtents) {
|
|
|
|
mGlyphExtentsArray.AppendElement(glyphExtents);
|
|
|
|
// Initialize the extents of a space glyph, assuming that spaces don't
|
|
|
|
// render anything!
|
|
|
|
glyphExtents->SetContainedGlyphWidthAppUnits(GetSpaceGlyph(), 0);
|
|
|
|
}
|
|
|
|
return glyphExtents;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFont::SetupGlyphExtents(gfxContext *aContext, PRUint32 aGlyphID, PRBool aNeedTight,
|
|
|
|
gfxGlyphExtents *aExtents)
|
|
|
|
{
|
2008-04-08 06:12:04 -07:00
|
|
|
gfxMatrix matrix = aContext->CurrentMatrix();
|
|
|
|
aContext->IdentityMatrix();
|
2007-09-23 19:19:14 -07:00
|
|
|
cairo_glyph_t glyph;
|
|
|
|
glyph.index = aGlyphID;
|
|
|
|
glyph.x = 0;
|
|
|
|
glyph.y = 0;
|
|
|
|
cairo_text_extents_t extents;
|
|
|
|
cairo_glyph_extents(aContext->GetCairo(), &glyph, 1, &extents);
|
2008-04-08 06:12:04 -07:00
|
|
|
aContext->SetMatrix(matrix);
|
2007-09-23 19:19:14 -07:00
|
|
|
|
|
|
|
const Metrics& fontMetrics = GetMetrics();
|
|
|
|
PRUint32 appUnitsPerDevUnit = aExtents->GetAppUnitsPerDevUnit();
|
|
|
|
if (!aNeedTight && extents.x_bearing >= 0 &&
|
|
|
|
extents.y_bearing >= -fontMetrics.maxAscent &&
|
|
|
|
extents.height + extents.y_bearing <= fontMetrics.maxDescent) {
|
|
|
|
PRUint32 appUnitsWidth =
|
|
|
|
PRUint32(NS_ceil((extents.x_bearing + extents.width)*appUnitsPerDevUnit));
|
|
|
|
if (appUnitsWidth < gfxGlyphExtents::INVALID_WIDTH) {
|
|
|
|
aExtents->SetContainedGlyphWidthAppUnits(aGlyphID, PRUint16(appUnitsWidth));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-10-01 18:47:40 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
|
|
|
if (!aNeedTight) {
|
|
|
|
++gGlyphExtentsSetupFallBackToTight;
|
|
|
|
}
|
|
|
|
#endif
|
2007-09-23 19:19:14 -07:00
|
|
|
|
|
|
|
double d2a = appUnitsPerDevUnit;
|
|
|
|
gfxRect bounds(extents.x_bearing*d2a, extents.y_bearing*d2a,
|
|
|
|
extents.width*d2a, extents.height*d2a);
|
|
|
|
aExtents->SetTightGlyphExtents(aGlyphID, bounds);
|
|
|
|
}
|
|
|
|
|
2010-07-11 12:33:56 -07:00
|
|
|
// Try to initialize font metrics by reading sfnt tables directly;
|
|
|
|
// set mIsValid=TRUE and return TRUE on success.
|
|
|
|
// Return FALSE if the gfxFontEntry subclass does not
|
|
|
|
// implement GetFontTable(), or for non-sfnt fonts where tables are
|
|
|
|
// not available.
|
|
|
|
PRBool
|
|
|
|
gfxFont::InitMetricsFromSfntTables(Metrics& aMetrics)
|
|
|
|
{
|
|
|
|
mIsValid = PR_FALSE; // font is NOT valid in case of early return
|
|
|
|
|
|
|
|
const PRUint32 kHeadTableTag = TRUETYPE_TAG('h','e','a','d');
|
|
|
|
const PRUint32 kHheaTableTag = TRUETYPE_TAG('h','h','e','a');
|
|
|
|
const PRUint32 kPostTableTag = TRUETYPE_TAG('p','o','s','t');
|
|
|
|
const PRUint32 kOS_2TableTag = TRUETYPE_TAG('O','S','/','2');
|
|
|
|
|
|
|
|
if (mFUnitsConvFactor == 0.0) {
|
|
|
|
// If the conversion factor from FUnits is not yet set,
|
|
|
|
// 'head' table is required; otherwise we cannot read any metrics
|
|
|
|
// because we don't know unitsPerEm
|
|
|
|
nsAutoTArray<PRUint8,sizeof(HeadTable)> headData;
|
|
|
|
if (NS_FAILED(mFontEntry->GetFontTable(kHeadTableTag, headData)) ||
|
|
|
|
headData.Length() < sizeof(HeadTable)) {
|
|
|
|
return PR_FALSE; // no 'head' table -> not an sfnt
|
|
|
|
}
|
|
|
|
HeadTable *head = reinterpret_cast<HeadTable*>(headData.Elements());
|
|
|
|
PRUint32 unitsPerEm = head->unitsPerEm;
|
|
|
|
if (!unitsPerEm) {
|
|
|
|
return PR_TRUE; // is an sfnt, but not valid
|
|
|
|
}
|
|
|
|
mFUnitsConvFactor = mAdjustedSize / unitsPerEm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'hhea' table is required to get vertical extents
|
|
|
|
nsAutoTArray<PRUint8,sizeof(HheaTable)> hheaData;
|
|
|
|
if (NS_FAILED(mFontEntry->GetFontTable(kHheaTableTag, hheaData)) ||
|
|
|
|
hheaData.Length() < sizeof(HheaTable)) {
|
|
|
|
return PR_FALSE; // no 'hhea' table -> not an sfnt
|
|
|
|
}
|
|
|
|
HheaTable *hhea = reinterpret_cast<HheaTable*>(hheaData.Elements());
|
|
|
|
|
|
|
|
#define SET_UNSIGNED(field,src) aMetrics.field = PRUint16(src) * mFUnitsConvFactor
|
|
|
|
#define SET_SIGNED(field,src) aMetrics.field = PRInt16(src) * mFUnitsConvFactor
|
|
|
|
|
|
|
|
SET_UNSIGNED(maxAdvance, hhea->advanceWidthMax);
|
|
|
|
SET_SIGNED(maxAscent, hhea->ascender);
|
|
|
|
SET_SIGNED(maxDescent, -PRInt16(hhea->descender));
|
|
|
|
SET_SIGNED(externalLeading, hhea->lineGap);
|
|
|
|
|
|
|
|
// 'post' table is required for underline metrics
|
|
|
|
nsAutoTArray<PRUint8,sizeof(PostTable)> postData;
|
|
|
|
if (NS_FAILED(mFontEntry->GetFontTable(kPostTableTag, postData))) {
|
|
|
|
return PR_TRUE; // no 'post' table -> sfnt is not valid
|
|
|
|
}
|
|
|
|
if (postData.Length() <
|
|
|
|
offsetof(PostTable, underlineThickness) + sizeof(PRUint16)) {
|
|
|
|
return PR_TRUE; // bad post table -> sfnt is not valid
|
|
|
|
}
|
|
|
|
PostTable *post = reinterpret_cast<PostTable*>(postData.Elements());
|
|
|
|
|
|
|
|
SET_SIGNED(underlineOffset, post->underlinePosition);
|
|
|
|
SET_UNSIGNED(underlineSize, post->underlineThickness);
|
|
|
|
|
|
|
|
// 'OS/2' table is optional, if not found we'll estimate xHeight
|
|
|
|
// and aveCharWidth by measuring glyphs
|
|
|
|
nsAutoTArray<PRUint8,sizeof(OS2Table)> os2data;
|
|
|
|
if (NS_SUCCEEDED(mFontEntry->GetFontTable(kOS_2TableTag, os2data))) {
|
|
|
|
OS2Table *os2 = reinterpret_cast<OS2Table*>(os2data.Elements());
|
|
|
|
|
|
|
|
if (os2data.Length() >= offsetof(OS2Table, sxHeight) +
|
|
|
|
sizeof(PRInt16) &&
|
|
|
|
PRUint16(os2->version) >= 2) {
|
|
|
|
// version 2 and later includes the x-height field
|
|
|
|
SET_SIGNED(xHeight, os2->sxHeight);
|
|
|
|
// PR_ABS because of negative xHeight seen in Kokonor (Tibetan) font
|
|
|
|
aMetrics.xHeight = PR_ABS(aMetrics.xHeight);
|
|
|
|
}
|
|
|
|
// this should always be present
|
|
|
|
if (os2data.Length() >= offsetof(OS2Table, yStrikeoutPosition) +
|
|
|
|
sizeof(PRInt16)) {
|
|
|
|
SET_SIGNED(aveCharWidth, os2->xAvgCharWidth);
|
|
|
|
SET_SIGNED(subscriptOffset, os2->ySubscriptYOffset);
|
|
|
|
SET_SIGNED(superscriptOffset, os2->ySuperscriptYOffset);
|
|
|
|
SET_SIGNED(strikeoutSize, os2->yStrikeoutSize);
|
|
|
|
SET_SIGNED(strikeoutOffset, os2->yStrikeoutPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsValid = PR_TRUE;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
RoundToNearestMultiple(double aValue, double aFraction)
|
|
|
|
{
|
|
|
|
return floor(aValue/aFraction + 0.5) * aFraction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxFont::CalculateDerivedMetrics(Metrics& aMetrics)
|
|
|
|
{
|
|
|
|
aMetrics.maxAscent =
|
|
|
|
NS_ceil(RoundToNearestMultiple(aMetrics.maxAscent, 1/1024.0));
|
|
|
|
aMetrics.maxDescent =
|
|
|
|
NS_ceil(RoundToNearestMultiple(aMetrics.maxDescent, 1/1024.0));
|
|
|
|
|
|
|
|
if (aMetrics.xHeight <= 0) {
|
|
|
|
// only happens if we couldn't find either font metrics
|
|
|
|
// or a char to measure;
|
|
|
|
// pick an arbitrary value that's better than zero
|
|
|
|
aMetrics.xHeight = aMetrics.maxAscent * DEFAULT_XHEIGHT_FACTOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMetrics.maxHeight = aMetrics.maxAscent + aMetrics.maxDescent;
|
|
|
|
|
|
|
|
if (aMetrics.maxHeight - aMetrics.emHeight > 0.0) {
|
|
|
|
aMetrics.internalLeading = aMetrics.maxHeight - aMetrics.emHeight;
|
|
|
|
} else {
|
|
|
|
aMetrics.internalLeading = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMetrics.emAscent = aMetrics.maxAscent * aMetrics.emHeight
|
|
|
|
/ aMetrics.maxHeight;
|
|
|
|
aMetrics.emDescent = aMetrics.emHeight - aMetrics.emAscent;
|
|
|
|
|
|
|
|
if (GetFontEntry()->IsFixedPitch()) {
|
|
|
|
// Some Quartz fonts are fixed pitch, but there's some glyph with a bigger
|
|
|
|
// advance than the average character width... this forces
|
|
|
|
// those fonts to be recognized like fixed pitch fonts by layout.
|
|
|
|
aMetrics.maxAdvance = aMetrics.aveCharWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aMetrics.subscriptOffset) {
|
|
|
|
aMetrics.subscriptOffset = aMetrics.xHeight;
|
|
|
|
}
|
|
|
|
if (!aMetrics.superscriptOffset) {
|
|
|
|
aMetrics.superscriptOffset = aMetrics.xHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aMetrics.strikeoutOffset) {
|
|
|
|
aMetrics.strikeoutOffset = aMetrics.xHeight * 0.5;
|
|
|
|
}
|
|
|
|
if (!aMetrics.strikeoutSize) {
|
|
|
|
aMetrics.strikeoutSize = aMetrics.underlineSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-15 13:31:38 -08:00
|
|
|
void
|
2008-03-12 19:36:58 -07:00
|
|
|
gfxFont::SanitizeMetrics(gfxFont::Metrics *aMetrics, PRBool aIsBadUnderlineFont)
|
2008-02-15 13:31:38 -08:00
|
|
|
{
|
2008-03-29 14:25:15 -07:00
|
|
|
// Even if this font size is zero, this font is created with non-zero size.
|
|
|
|
// However, for layout and others, we should return the metrics of zero size font.
|
|
|
|
if (mStyle.size == 0) {
|
|
|
|
memset(aMetrics, 0, sizeof(gfxFont::Metrics));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-15 13:31:38 -08:00
|
|
|
// MS (P)Gothic and MS (P)Mincho are not having suitable values in their super script offset.
|
|
|
|
// If the values are not suitable, we should use x-height instead of them.
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=353632
|
2010-03-31 05:46:18 -07:00
|
|
|
if (aMetrics->superscriptOffset <= 0 ||
|
2008-02-15 13:31:38 -08:00
|
|
|
aMetrics->superscriptOffset >= aMetrics->maxAscent) {
|
|
|
|
aMetrics->superscriptOffset = aMetrics->xHeight;
|
|
|
|
}
|
|
|
|
// And also checking the case of sub script offset. The old gfx for win has checked this too.
|
2010-03-31 05:46:18 -07:00
|
|
|
if (aMetrics->subscriptOffset <= 0 ||
|
2008-02-15 13:31:38 -08:00
|
|
|
aMetrics->subscriptOffset >= aMetrics->maxAscent) {
|
|
|
|
aMetrics->subscriptOffset = aMetrics->xHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMetrics->underlineSize = PR_MAX(1.0, aMetrics->underlineSize);
|
|
|
|
aMetrics->strikeoutSize = PR_MAX(1.0, aMetrics->strikeoutSize);
|
|
|
|
|
|
|
|
aMetrics->underlineOffset = PR_MIN(aMetrics->underlineOffset, -1.0);
|
|
|
|
|
2008-04-12 01:54:18 -07:00
|
|
|
if (aMetrics->maxAscent < 1.0) {
|
|
|
|
// We cannot draw strikeout line and overline in the ascent...
|
|
|
|
aMetrics->underlineSize = 0;
|
|
|
|
aMetrics->underlineOffset = 0;
|
|
|
|
aMetrics->strikeoutSize = 0;
|
|
|
|
aMetrics->strikeoutOffset = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-12 19:36:58 -07:00
|
|
|
/**
|
|
|
|
* Some CJK fonts have bad underline offset. Therefore, if this is such font,
|
|
|
|
* we need to lower the underline offset to bottom of *em* descent.
|
|
|
|
* However, if this is system font, we should not do this for the rendering compatibility with
|
|
|
|
* another application's UI on the platform.
|
2008-04-12 01:54:18 -07:00
|
|
|
* XXX Should not use this hack if the font size is too small?
|
|
|
|
* Such text cannot be read, this might be used for tight CSS rendering? (E.g., Acid2)
|
2008-03-12 19:36:58 -07:00
|
|
|
*/
|
|
|
|
if (!mStyle.systemFont && aIsBadUnderlineFont) {
|
|
|
|
// First, we need 2 pixels between baseline and underline at least. Because many CJK characters
|
|
|
|
// put their glyphs on the baseline, so, 1 pixel is too close for CJK characters.
|
|
|
|
aMetrics->underlineOffset = PR_MIN(aMetrics->underlineOffset, -2.0);
|
|
|
|
|
|
|
|
// Next, we put the underline to bottom of below of the descent space.
|
|
|
|
if (aMetrics->internalLeading + aMetrics->externalLeading > aMetrics->underlineSize) {
|
|
|
|
aMetrics->underlineOffset = PR_MIN(aMetrics->underlineOffset, -aMetrics->emDescent);
|
|
|
|
} else {
|
|
|
|
aMetrics->underlineOffset = PR_MIN(aMetrics->underlineOffset,
|
|
|
|
aMetrics->underlineSize - aMetrics->emDescent);
|
|
|
|
}
|
|
|
|
}
|
2008-02-15 13:31:38 -08:00
|
|
|
// If underline positioned is too far from the text, descent position is preferred so that underline
|
|
|
|
// will stay within the boundary.
|
2008-03-12 19:36:58 -07:00
|
|
|
else if (aMetrics->underlineSize - aMetrics->underlineOffset > aMetrics->maxDescent) {
|
2008-04-12 01:54:18 -07:00
|
|
|
if (aMetrics->underlineSize > aMetrics->maxDescent)
|
|
|
|
aMetrics->underlineSize = PR_MAX(aMetrics->maxDescent, 1.0);
|
|
|
|
// The max underlineOffset is 1px (the min underlineSize is 1px, and min maxDescent is 0px.)
|
2008-02-15 13:31:38 -08:00
|
|
|
aMetrics->underlineOffset = aMetrics->underlineSize - aMetrics->maxDescent;
|
2008-04-12 01:54:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If strikeout line is overflowed from the ascent, the line should be resized and moved for
|
|
|
|
// that being in the ascent space.
|
|
|
|
// Note that the strikeoutOffset is *middle* of the strikeout line position.
|
|
|
|
gfxFloat halfOfStrikeoutSize = NS_floor(aMetrics->strikeoutSize / 2.0 + 0.5);
|
|
|
|
if (halfOfStrikeoutSize + aMetrics->strikeoutOffset > aMetrics->maxAscent) {
|
|
|
|
if (aMetrics->strikeoutSize > aMetrics->maxAscent) {
|
|
|
|
aMetrics->strikeoutSize = PR_MAX(aMetrics->maxAscent, 1.0);
|
|
|
|
halfOfStrikeoutSize = NS_floor(aMetrics->strikeoutSize / 2.0 + 0.5);
|
|
|
|
}
|
|
|
|
gfxFloat ascent = NS_floor(aMetrics->maxAscent + 0.5);
|
|
|
|
aMetrics->strikeoutOffset = PR_MAX(halfOfStrikeoutSize, ascent / 2.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If overline is larger than the ascent, the line should be resized.
|
|
|
|
if (aMetrics->underlineSize > aMetrics->maxAscent) {
|
|
|
|
aMetrics->underlineSize = aMetrics->maxAscent;
|
2008-03-12 19:36:58 -07:00
|
|
|
}
|
2008-02-15 13:31:38 -08:00
|
|
|
}
|
|
|
|
|
2007-10-01 18:47:40 -07:00
|
|
|
gfxGlyphExtents::~gfxGlyphExtents()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
|
|
|
gGlyphExtentsWidthsTotalSize += mContainedGlyphWidths.ComputeSize();
|
|
|
|
gGlyphExtentsCount++;
|
|
|
|
#endif
|
|
|
|
MOZ_COUNT_DTOR(gfxGlyphExtents);
|
|
|
|
}
|
|
|
|
|
2007-12-20 23:09:50 -08:00
|
|
|
PRBool
|
2007-09-23 19:19:14 -07:00
|
|
|
gfxGlyphExtents::GetTightGlyphExtentsAppUnits(gfxFont *aFont,
|
2007-12-20 23:09:50 -08:00
|
|
|
gfxContext *aContext, PRUint32 aGlyphID, gfxRect *aExtents)
|
2007-09-23 19:19:14 -07:00
|
|
|
{
|
|
|
|
HashEntry *entry = mTightGlyphExtents.GetEntry(aGlyphID);
|
|
|
|
if (!entry) {
|
2007-12-20 23:09:50 -08:00
|
|
|
if (!aContext) {
|
|
|
|
NS_WARNING("Could not get glyph extents (no aContext)");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2007-09-23 19:19:14 -07:00
|
|
|
aFont->SetupCairoFont(aContext);
|
2007-10-01 18:47:40 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
|
|
|
++gGlyphExtentsSetupLazyTight;
|
|
|
|
#endif
|
2007-09-23 19:19:14 -07:00
|
|
|
aFont->SetupGlyphExtents(aContext, aGlyphID, PR_TRUE, this);
|
|
|
|
entry = mTightGlyphExtents.GetEntry(aGlyphID);
|
|
|
|
if (!entry) {
|
|
|
|
NS_WARNING("Could not get glyph extents");
|
2007-12-20 23:09:50 -08:00
|
|
|
return PR_FALSE;
|
2007-09-23 19:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2007-12-20 23:09:50 -08:00
|
|
|
*aExtents = gfxRect(entry->x, entry->y, entry->width, entry->height);
|
|
|
|
return PR_TRUE;
|
2007-09-23 19:19:14 -07:00
|
|
|
}
|
|
|
|
|
2007-10-01 18:47:40 -07:00
|
|
|
gfxGlyphExtents::GlyphWidths::~GlyphWidths()
|
|
|
|
{
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < mBlocks.Length(); ++i) {
|
|
|
|
PtrBits bits = mBlocks[i];
|
|
|
|
if (bits && !(bits & 0x1)) {
|
|
|
|
delete[] reinterpret_cast<PRUint16 *>(bits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRUint32
|
|
|
|
gfxGlyphExtents::GlyphWidths::ComputeSize()
|
|
|
|
{
|
|
|
|
PRUint32 i;
|
|
|
|
PRUint32 size = mBlocks.Capacity()*sizeof(PtrBits);
|
|
|
|
for (i = 0; i < mBlocks.Length(); ++i) {
|
|
|
|
PtrBits bits = mBlocks[i];
|
|
|
|
if (bits && !(bits & 0x1)) {
|
|
|
|
size += BLOCK_SIZE*sizeof(PRUint16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-23 19:19:14 -07:00
|
|
|
void
|
2007-10-01 18:47:40 -07:00
|
|
|
gfxGlyphExtents::GlyphWidths::Set(PRUint32 aGlyphID, PRUint16 aWidth)
|
2007-09-23 19:19:14 -07:00
|
|
|
{
|
2007-10-01 18:47:40 -07:00
|
|
|
PRUint32 block = aGlyphID >> BLOCK_SIZE_BITS;
|
|
|
|
PRUint32 len = mBlocks.Length();
|
|
|
|
if (block >= len) {
|
|
|
|
PtrBits *elems = mBlocks.AppendElements(block + 1 - len);
|
2007-09-23 19:19:14 -07:00
|
|
|
if (!elems)
|
|
|
|
return;
|
2007-10-01 18:47:40 -07:00
|
|
|
memset(elems, 0, sizeof(PtrBits)*(block + 1 - len));
|
|
|
|
}
|
|
|
|
|
|
|
|
PtrBits bits = mBlocks[block];
|
|
|
|
PRUint32 glyphOffset = aGlyphID & (BLOCK_SIZE - 1);
|
|
|
|
if (!bits) {
|
|
|
|
mBlocks[block] = MakeSingle(glyphOffset, aWidth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint16 *newBlock;
|
|
|
|
if (bits & 0x1) {
|
|
|
|
// Expand the block to a real block. We could avoid this by checking
|
|
|
|
// glyphOffset == GetGlyphOffset(bits), but that never happens so don't bother
|
|
|
|
newBlock = new PRUint16[BLOCK_SIZE];
|
|
|
|
if (!newBlock)
|
|
|
|
return;
|
2007-10-01 15:11:23 -07:00
|
|
|
PRUint32 i;
|
2007-10-01 18:47:40 -07:00
|
|
|
for (i = 0; i < BLOCK_SIZE; ++i) {
|
|
|
|
newBlock[i] = INVALID_WIDTH;
|
2007-10-01 15:11:23 -07:00
|
|
|
}
|
2007-10-01 18:47:40 -07:00
|
|
|
newBlock[GetGlyphOffset(bits)] = GetWidth(bits);
|
|
|
|
mBlocks[block] = reinterpret_cast<PtrBits>(newBlock);
|
|
|
|
} else {
|
|
|
|
newBlock = reinterpret_cast<PRUint16 *>(bits);
|
2007-09-23 19:19:14 -07:00
|
|
|
}
|
2007-10-01 18:47:40 -07:00
|
|
|
newBlock[glyphOffset] = aWidth;
|
2007-09-23 19:19:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxGlyphExtents::SetTightGlyphExtents(PRUint32 aGlyphID, const gfxRect& aExtentsAppUnits)
|
|
|
|
{
|
|
|
|
HashEntry *entry = mTightGlyphExtents.PutEntry(aGlyphID);
|
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
entry->x = aExtentsAppUnits.pos.x;
|
|
|
|
entry->y = aExtentsAppUnits.pos.y;
|
|
|
|
entry->width = aExtentsAppUnits.size.width;
|
|
|
|
entry->height = aExtentsAppUnits.size.height;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:01:53 -07:00
|
|
|
gfxFontGroup::gfxFontGroup(const nsAString& aFamilies, const gfxFontStyle *aStyle, gfxUserFontSet *aUserFontSet)
|
2008-04-12 01:54:18 -07:00
|
|
|
: mFamilies(aFamilies), mStyle(*aStyle), mUnderlineOffset(UNDERLINE_OFFSET_NOT_SET)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-09-30 20:01:53 -07:00
|
|
|
mUserFontSet = nsnull;
|
|
|
|
SetUserFontSet(aUserFontSet);
|
2009-10-07 08:26:58 -07:00
|
|
|
|
2010-02-24 09:57:57 -08:00
|
|
|
mPageLang = gfxPlatform::GetFontPrefLangFor(mStyle.language);
|
2010-02-23 23:18:49 -08:00
|
|
|
BuildFontList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontGroup::BuildFontList()
|
|
|
|
{
|
2009-10-07 08:26:58 -07:00
|
|
|
// "#if" to be removed once all platforms are moved to gfxPlatformFontList interface
|
|
|
|
// and subclasses of gfxFontGroup eliminated
|
2010-07-15 23:03:45 -07:00
|
|
|
#if defined(XP_MACOSX) || (defined(XP_WIN) && !defined(WINCE)) || defined(ANDROID)
|
2009-10-07 08:26:58 -07:00
|
|
|
ForEachFont(FindPlatformFont, this);
|
2010-02-23 23:18:49 -08:00
|
|
|
|
2009-10-07 08:26:58 -07:00
|
|
|
if (mFonts.Length() == 0) {
|
|
|
|
PRBool needsBold;
|
2010-04-11 12:36:17 -07:00
|
|
|
gfxPlatformFontList *pfl = gfxPlatformFontList::PlatformFontList();
|
|
|
|
gfxFontEntry *defaultFont = pfl->GetDefaultFont(&mStyle, needsBold);
|
2009-10-07 08:26:58 -07:00
|
|
|
NS_ASSERTION(defaultFont, "invalid default font returned by GetDefaultFont");
|
|
|
|
|
2010-04-11 12:36:17 -07:00
|
|
|
if (defaultFont) {
|
|
|
|
nsRefPtr<gfxFont> font = defaultFont->FindOrMakeFont(&mStyle,
|
|
|
|
needsBold);
|
|
|
|
if (font) {
|
|
|
|
mFonts.AppendElement(font);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFonts.Length() == 0) {
|
|
|
|
// Try for a "font of last resort...."
|
|
|
|
// Because an empty font list would be Really Bad for later code
|
|
|
|
// that assumes it will be able to get valid metrics for layout,
|
|
|
|
// just look for the first usable font and put in the list.
|
|
|
|
// (see bug 554544)
|
|
|
|
nsAutoTArray<nsRefPtr<gfxFontFamily>,200> families;
|
|
|
|
pfl->GetFontFamilyList(families);
|
|
|
|
for (PRUint32 i = 0; i < families.Length(); ++i) {
|
|
|
|
gfxFontEntry *fe = families[i]->FindFontForStyle(mStyle,
|
|
|
|
needsBold);
|
|
|
|
if (fe) {
|
|
|
|
nsRefPtr<gfxFont> font = fe->FindOrMakeFont(&mStyle,
|
|
|
|
needsBold);
|
|
|
|
if (font) {
|
|
|
|
mFonts.AppendElement(font);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFonts.Length() == 0) {
|
|
|
|
// an empty font list at this point is fatal; we're not going to
|
|
|
|
// be able to do even the most basic layout operations
|
|
|
|
char msg[256]; // CHECK buffer length if revising message below
|
|
|
|
sprintf(msg, "unable to find a usable font (%.220s)",
|
|
|
|
NS_ConvertUTF16toUTF8(mFamilies).get());
|
|
|
|
NS_RUNTIMEABORT(msg);
|
2009-10-07 08:26:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mStyle.systemFont) {
|
|
|
|
for (PRUint32 i = 0; i < mFonts.Length(); ++i) {
|
|
|
|
gfxFont* font = mFonts[i];
|
|
|
|
if (font->GetFontEntry()->mIsBadUnderlineFont) {
|
|
|
|
gfxFloat first = mFonts[0]->GetMetrics().underlineOffset;
|
|
|
|
gfxFloat bad = font->GetMetrics().underlineOffset;
|
|
|
|
mUnderlineOffset = PR_MIN(first, bad);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::FindPlatformFont(const nsAString& aName,
|
|
|
|
const nsACString& aGenericName,
|
|
|
|
void *aClosure)
|
|
|
|
{
|
|
|
|
gfxFontGroup *fontGroup = static_cast<gfxFontGroup*>(aClosure);
|
|
|
|
const gfxFontStyle *fontStyle = fontGroup->GetStyle();
|
|
|
|
|
|
|
|
|
|
|
|
PRBool needsBold;
|
|
|
|
gfxFontEntry *fe = nsnull;
|
|
|
|
|
|
|
|
// first, look up in the user font set
|
|
|
|
gfxUserFontSet *fs = fontGroup->GetUserFontSet();
|
|
|
|
if (fs) {
|
|
|
|
fe = fs->FindFontEntry(aName, *fontStyle, needsBold);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing in the user font set ==> check system fonts
|
|
|
|
if (!fe) {
|
|
|
|
fe = gfxPlatformFontList::PlatformFontList()->
|
|
|
|
FindFontForFamily(aName, fontStyle, needsBold);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to the font group, unless it's already there
|
|
|
|
if (fe && !fontGroup->HasFont(fe)) {
|
2010-02-17 19:31:20 -08:00
|
|
|
nsRefPtr<gfxFont> font = fe->FindOrMakeFont(fontStyle, needsBold);
|
2009-10-07 08:26:58 -07:00
|
|
|
if (font) {
|
|
|
|
fontGroup->mFonts.AppendElement(font);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::HasFont(const gfxFontEntry *aFontEntry)
|
|
|
|
{
|
|
|
|
for (PRUint32 i = 0; i < mFonts.Length(); ++i) {
|
|
|
|
if (mFonts.ElementAt(i)->GetFontEntry() == aFontEntry)
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
2008-09-30 20:01:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontGroup::~gfxFontGroup() {
|
|
|
|
mFonts.Clear();
|
|
|
|
SetUserFontSet(nsnull);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-07 08:26:58 -07:00
|
|
|
gfxFontGroup *
|
|
|
|
gfxFontGroup::Copy(const gfxFontStyle *aStyle)
|
|
|
|
{
|
|
|
|
return new gfxFontGroup(mFamilies, aStyle, mUserFontSet);
|
|
|
|
}
|
2008-09-30 20:01:53 -07:00
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::IsInvalidChar(PRUnichar ch) {
|
|
|
|
if (ch >= 32) {
|
|
|
|
return ch == 0x0085/*NEL*/ ||
|
|
|
|
((ch & 0xFF00) == 0x2000 /* Unicode control character */ &&
|
|
|
|
(ch == 0x200B/*ZWSP*/ || ch == 0x2028/*LSEP*/ || ch == 0x2029/*PSEP*/ ||
|
|
|
|
IS_BIDI_CONTROL_CHAR(ch)));
|
|
|
|
}
|
|
|
|
// We could just blacklist all control characters, but it seems better
|
|
|
|
// to only blacklist the ones we know cause problems for native font
|
|
|
|
// engines.
|
|
|
|
return ch == 0x0B || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\f' ||
|
|
|
|
(ch >= 0x1c && ch <= 0x1f);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::ForEachFont(FontCreationCallback fc,
|
|
|
|
void *closure)
|
|
|
|
{
|
2010-02-24 09:57:44 -08:00
|
|
|
return ForEachFontInternal(mFamilies, mStyle.language,
|
2008-01-26 23:37:29 -08:00
|
|
|
PR_TRUE, PR_TRUE, fc, closure);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::ForEachFont(const nsAString& aFamilies,
|
2010-02-24 09:57:57 -08:00
|
|
|
nsIAtom *aLanguage,
|
2007-03-22 10:30:00 -07:00
|
|
|
FontCreationCallback fc,
|
|
|
|
void *closure)
|
|
|
|
{
|
2010-02-24 09:57:44 -08:00
|
|
|
return ForEachFontInternal(aFamilies, aLanguage,
|
2008-01-26 23:37:29 -08:00
|
|
|
PR_FALSE, PR_TRUE, fc, closure);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ResolveData {
|
|
|
|
ResolveData(gfxFontGroup::FontCreationCallback aCallback,
|
|
|
|
nsACString& aGenericFamily,
|
|
|
|
void *aClosure) :
|
|
|
|
mCallback(aCallback),
|
|
|
|
mGenericFamily(aGenericFamily),
|
|
|
|
mClosure(aClosure) {
|
2007-04-23 07:21:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxFontGroup::FontCreationCallback mCallback;
|
|
|
|
nsCString mGenericFamily;
|
|
|
|
void *mClosure;
|
|
|
|
};
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies,
|
2010-02-24 09:57:57 -08:00
|
|
|
nsIAtom *aLanguage,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool aResolveGeneric,
|
2008-01-26 23:37:29 -08:00
|
|
|
PRBool aResolveFontName,
|
2007-03-22 10:30:00 -07:00
|
|
|
FontCreationCallback fc,
|
|
|
|
void *closure)
|
|
|
|
{
|
|
|
|
const PRUnichar kSingleQuote = PRUnichar('\'');
|
|
|
|
const PRUnichar kDoubleQuote = PRUnichar('\"');
|
|
|
|
const PRUnichar kComma = PRUnichar(',');
|
|
|
|
|
2010-02-24 09:57:57 -08:00
|
|
|
nsIAtom *groupAtom = nsnull;
|
|
|
|
nsCAutoString groupString;
|
|
|
|
if (aLanguage) {
|
|
|
|
if (!gLangService) {
|
|
|
|
CallGetService(NS_LANGUAGEATOMSERVICE_CONTRACTID, &gLangService);
|
|
|
|
}
|
|
|
|
if (gLangService) {
|
|
|
|
nsresult rv;
|
|
|
|
groupAtom = gLangService->GetLanguageGroup(aLanguage, &rv);
|
2010-02-24 09:57:44 -08:00
|
|
|
}
|
|
|
|
}
|
2010-02-24 09:57:57 -08:00
|
|
|
if (!groupAtom) {
|
|
|
|
groupAtom = gfxAtoms::x_unicode;
|
2010-02-24 09:57:44 -08:00
|
|
|
}
|
2010-02-24 09:57:57 -08:00
|
|
|
groupAtom->ToUTF8String(groupString);
|
2010-02-24 09:57:44 -08:00
|
|
|
|
2009-04-04 02:43:42 -07:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsPromiseFlatString families(aFamilies);
|
|
|
|
const PRUnichar *p, *p_end;
|
|
|
|
families.BeginReading(p);
|
|
|
|
families.EndReading(p_end);
|
|
|
|
nsAutoString family;
|
|
|
|
nsCAutoString lcFamily;
|
|
|
|
nsAutoString genericFamily;
|
2009-04-04 02:43:42 -07:00
|
|
|
nsXPIDLCString value;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (p < p_end) {
|
2010-04-15 01:31:15 -07:00
|
|
|
while (nsCRT::IsAsciiSpace(*p) || *p == kComma)
|
2007-03-22 10:30:00 -07:00
|
|
|
if (++p == p_end)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
PRBool generic;
|
|
|
|
if (*p == kSingleQuote || *p == kDoubleQuote) {
|
|
|
|
// quoted font family
|
|
|
|
PRUnichar quoteMark = *p;
|
|
|
|
if (++p == p_end)
|
|
|
|
return PR_TRUE;
|
|
|
|
const PRUnichar *nameStart = p;
|
|
|
|
|
|
|
|
// XXX What about CSS character escapes?
|
|
|
|
while (*p != quoteMark)
|
|
|
|
if (++p == p_end)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
family = Substring(nameStart, p);
|
|
|
|
generic = PR_FALSE;
|
|
|
|
genericFamily.SetIsVoid(PR_TRUE);
|
|
|
|
|
|
|
|
while (++p != p_end && *p != kComma)
|
|
|
|
/* nothing */ ;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// unquoted font family
|
|
|
|
const PRUnichar *nameStart = p;
|
|
|
|
while (++p != p_end && *p != kComma)
|
|
|
|
/* nothing */ ;
|
|
|
|
|
|
|
|
family = Substring(nameStart, p);
|
|
|
|
family.CompressWhitespace(PR_FALSE, PR_TRUE);
|
|
|
|
|
|
|
|
if (aResolveGeneric &&
|
|
|
|
(family.LowerCaseEqualsLiteral("serif") ||
|
|
|
|
family.LowerCaseEqualsLiteral("sans-serif") ||
|
|
|
|
family.LowerCaseEqualsLiteral("monospace") ||
|
|
|
|
family.LowerCaseEqualsLiteral("cursive") ||
|
|
|
|
family.LowerCaseEqualsLiteral("fantasy")))
|
|
|
|
{
|
|
|
|
generic = PR_TRUE;
|
|
|
|
|
|
|
|
ToLowerCase(NS_LossyConvertUTF16toASCII(family), lcFamily);
|
|
|
|
|
|
|
|
nsCAutoString prefName("font.name.");
|
|
|
|
prefName.Append(lcFamily);
|
|
|
|
prefName.AppendLiteral(".");
|
2010-02-24 09:57:57 -08:00
|
|
|
prefName.Append(groupString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// prefs file always uses (must use) UTF-8 so that we can use
|
|
|
|
// |GetCharPref| and treat the result as a UTF-8 string.
|
2009-04-04 02:43:42 -07:00
|
|
|
nsresult rv = prefs->GetCharPref(prefName.get(), getter_Copies(value));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CopyASCIItoUTF16(lcFamily, genericFamily);
|
2009-04-04 02:43:42 -07:00
|
|
|
CopyUTF8toUTF16(value, family);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
generic = PR_FALSE;
|
|
|
|
genericFamily.SetIsVoid(PR_TRUE);
|
|
|
|
}
|
|
|
|
}
|
2008-04-20 04:34:04 -07:00
|
|
|
|
|
|
|
if (generic) {
|
2010-02-24 09:57:57 -08:00
|
|
|
ForEachFontInternal(family, groupAtom, PR_FALSE,
|
2010-02-24 09:57:44 -08:00
|
|
|
aResolveFontName, fc, closure);
|
2008-04-20 04:34:04 -07:00
|
|
|
} else if (!family.IsEmpty()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LossyConvertUTF16toASCII gf(genericFamily);
|
2008-01-26 23:37:29 -08:00
|
|
|
if (aResolveFontName) {
|
|
|
|
ResolveData data(fc, gf, closure);
|
2008-11-26 10:13:02 -08:00
|
|
|
PRBool aborted = PR_FALSE, needsBold;
|
2008-09-30 20:01:53 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mUserFontSet && mUserFontSet->FindFontEntry(family, mStyle, needsBold)) {
|
|
|
|
gfxFontGroup::FontResolverProc(family, &data);
|
|
|
|
rv = NS_OK;
|
|
|
|
} else {
|
|
|
|
gfxPlatform *pf = gfxPlatform::GetPlatform();
|
|
|
|
rv = pf->ResolveFontName(family,
|
2008-01-26 23:37:29 -08:00
|
|
|
gfxFontGroup::FontResolverProc,
|
|
|
|
&data, aborted);
|
2008-09-30 20:01:53 -07:00
|
|
|
}
|
2008-01-26 23:37:29 -08:00
|
|
|
if (NS_FAILED(rv) || aborted)
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!fc(family, gf, closure))
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (generic && aResolveGeneric) {
|
|
|
|
nsCAutoString prefName("font.name-list.");
|
|
|
|
prefName.Append(lcFamily);
|
|
|
|
prefName.AppendLiteral(".");
|
2010-02-24 09:57:57 -08:00
|
|
|
prefName.Append(groupString);
|
2009-04-04 02:43:42 -07:00
|
|
|
nsresult rv = prefs->GetCharPref(prefName.get(), getter_Copies(value));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-04-04 02:43:42 -07:00
|
|
|
ForEachFontInternal(NS_ConvertUTF8toUTF16(value),
|
2010-02-24 09:57:57 -08:00
|
|
|
groupAtom, PR_FALSE, aResolveFontName,
|
2008-01-26 23:37:29 -08:00
|
|
|
fc, closure);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++p; // may advance past p_end
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::FontResolverProc(const nsAString& aName, void *aClosure)
|
|
|
|
{
|
|
|
|
ResolveData *data = reinterpret_cast<ResolveData*>(aClosure);
|
|
|
|
return (data->mCallback)(aName, data->mGenericFamily, data->mClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun *
|
2007-05-08 15:46:14 -07:00
|
|
|
gfxFontGroup::MakeEmptyTextRun(const Parameters *aParams, PRUint32 aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-05-08 15:46:14 -07:00
|
|
|
aFlags |= TEXT_IS_8BIT | TEXT_IS_ASCII | TEXT_IS_PERSISTENT;
|
2007-11-15 17:43:47 -08:00
|
|
|
return gfxTextRun::Create(aParams, nsnull, 0, this, aFlags);
|
2007-05-08 15:46:14 -07:00
|
|
|
}
|
2007-05-04 21:36:13 -07:00
|
|
|
|
2007-05-08 15:46:14 -07:00
|
|
|
gfxTextRun *
|
|
|
|
gfxFontGroup::MakeSpaceTextRun(const Parameters *aParams, PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
aFlags |= TEXT_IS_8BIT | TEXT_IS_ASCII | TEXT_IS_PERSISTENT;
|
2007-03-22 10:30:00 -07:00
|
|
|
static const PRUint8 space = ' ';
|
|
|
|
|
2007-05-08 15:46:14 -07:00
|
|
|
nsAutoPtr<gfxTextRun> textRun;
|
2007-11-15 17:43:47 -08:00
|
|
|
textRun = gfxTextRun::Create(aParams, &space, 1, this, aFlags);
|
|
|
|
if (!textRun)
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsnull;
|
2007-06-12 13:56:04 -07:00
|
|
|
|
|
|
|
gfxFont *font = GetFontAt(0);
|
2008-01-13 23:05:19 -08:00
|
|
|
if (NS_UNLIKELY(GetStyle()->size == 0)) {
|
|
|
|
// Short-circuit for size-0 fonts, as Windows and ATSUI can't handle
|
|
|
|
// them, and always create at least size 1 fonts, i.e. they still
|
|
|
|
// render something for size 0 fonts.
|
|
|
|
textRun->AddGlyphRun(font, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
textRun->SetSpaceGlyph(font, aParams->mContext, 0);
|
|
|
|
}
|
2007-09-23 19:19:14 -07:00
|
|
|
// Note that the gfxGlyphExtents glyph bounds storage for the font will
|
|
|
|
// always contain an entry for the font's space glyph, so we don't have
|
|
|
|
// to call FetchGlyphExtents here.
|
2007-05-08 15:46:14 -07:00
|
|
|
return textRun.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 08:26:58 -07:00
|
|
|
#define UNICODE_LRO 0x202d
|
|
|
|
#define UNICODE_RLO 0x202e
|
|
|
|
#define UNICODE_PDF 0x202c
|
|
|
|
|
|
|
|
inline void
|
|
|
|
AppendDirectionalIndicatorStart(PRUint32 aFlags, nsAString& aString)
|
|
|
|
{
|
|
|
|
static const PRUnichar overrides[2] = { UNICODE_LRO, UNICODE_RLO };
|
|
|
|
aString.Append(overrides[(aFlags & gfxTextRunFactory::TEXT_IS_RTL) != 0]);
|
|
|
|
aString.Append(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
AppendDirectionalIndicatorEnd(PRBool aNeedDirection, nsAString& aString)
|
|
|
|
{
|
|
|
|
// append a space (always, for consistent treatment of last char,
|
|
|
|
// and a direction control if required (we skip this for 8-bit text,
|
|
|
|
// which is known to be unidirectional LTR, unless the direction was
|
|
|
|
// forced RTL via overrides)
|
|
|
|
aString.Append(' ');
|
|
|
|
if (!aNeedDirection)
|
|
|
|
return;
|
|
|
|
|
|
|
|
aString.Append('.');
|
|
|
|
aString.Append(UNICODE_PDF);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun *
|
|
|
|
gfxFontGroup::MakeTextRun(const PRUint8 *aString, PRUint32 aLength,
|
|
|
|
const Parameters *aParams, PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aLength > 0, "should use MakeEmptyTextRun for zero-length text");
|
|
|
|
NS_ASSERTION(aFlags & TEXT_IS_8BIT, "should be marked 8bit");
|
|
|
|
gfxTextRun *textRun = gfxTextRun::Create(aParams, aString, aLength, this, aFlags);
|
|
|
|
if (!textRun)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
nsDependentCSubstring cString(reinterpret_cast<const char*>(aString),
|
|
|
|
reinterpret_cast<const char*>(aString) + aLength);
|
|
|
|
|
|
|
|
nsAutoString utf16;
|
|
|
|
AppendASCIItoUTF16(cString, utf16);
|
|
|
|
|
2010-03-10 04:46:41 -08:00
|
|
|
InitTextRun(aParams->mContext, textRun, utf16.get(), utf16.Length());
|
2009-10-07 08:26:58 -07:00
|
|
|
|
|
|
|
textRun->FetchGlyphExtents(aParams->mContext);
|
|
|
|
|
|
|
|
return textRun;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun *
|
|
|
|
gfxFontGroup::MakeTextRun(const PRUnichar *aString, PRUint32 aLength,
|
|
|
|
const Parameters *aParams, PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aLength > 0, "should use MakeEmptyTextRun for zero-length text");
|
|
|
|
gfxTextRun *textRun = gfxTextRun::Create(aParams, aString, aLength, this, aFlags);
|
|
|
|
if (!textRun)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
gfxPlatform::GetPlatform()->SetupClusterBoundaries(textRun, aString);
|
|
|
|
|
2010-03-10 04:46:41 -08:00
|
|
|
InitTextRun(aParams->mContext, textRun, aString, aLength);
|
2009-10-07 08:26:58 -07:00
|
|
|
|
|
|
|
textRun->FetchGlyphExtents(aParams->mContext);
|
|
|
|
|
|
|
|
return textRun;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SMALL_GLYPH_RUN 128 // preallocated size of our auto arrays for per-glyph data;
|
|
|
|
// some testing indicates that 90%+ of glyph runs will fit
|
|
|
|
// without requiring a separate allocation
|
|
|
|
|
|
|
|
void
|
2010-03-10 04:46:41 -08:00
|
|
|
gfxFontGroup::InitTextRun(gfxContext *aContext,
|
|
|
|
gfxTextRun *aTextRun,
|
2009-10-07 08:26:58 -07:00
|
|
|
const PRUnichar *aString,
|
|
|
|
PRUint32 aLength)
|
2010-06-11 12:14:38 -07:00
|
|
|
{
|
|
|
|
// split into script runs so that script can potentially influence
|
|
|
|
// the font matching process below
|
|
|
|
gfxScriptItemizer scriptRuns(aString, aLength);
|
|
|
|
|
|
|
|
PRUint32 runStart = 0, runLimit = aLength;
|
|
|
|
PRInt32 runScript = HB_SCRIPT_LATIN;
|
|
|
|
while (scriptRuns.Next(runStart, runLimit, runScript)) {
|
|
|
|
InitTextRun(aContext, aTextRun, aString, aLength,
|
|
|
|
runStart, runLimit, runScript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontGroup::InitTextRun(gfxContext *aContext,
|
|
|
|
gfxTextRun *aTextRun,
|
|
|
|
const PRUnichar *aString,
|
|
|
|
PRUint32 aTotalLength,
|
|
|
|
PRUint32 aScriptRunStart,
|
|
|
|
PRUint32 aScriptRunEnd,
|
|
|
|
PRInt32 aRunScript)
|
2009-10-07 08:26:58 -07:00
|
|
|
{
|
|
|
|
gfxFont *mainFont = mFonts[0].get();
|
|
|
|
|
2010-06-11 12:14:38 -07:00
|
|
|
PRUint32 runStart = aScriptRunStart;
|
2009-10-07 08:26:58 -07:00
|
|
|
nsAutoTArray<gfxTextRange,3> fontRanges;
|
2010-11-07 14:44:51 -08:00
|
|
|
ComputeRanges(fontRanges, aString,
|
|
|
|
aScriptRunStart, aScriptRunEnd, aRunScript);
|
2009-10-07 08:26:58 -07:00
|
|
|
PRUint32 numRanges = fontRanges.Length();
|
|
|
|
|
|
|
|
for (PRUint32 r = 0; r < numRanges; r++) {
|
|
|
|
const gfxTextRange& range = fontRanges[r];
|
|
|
|
PRUint32 matchedLength = range.Length();
|
|
|
|
gfxFont *matchedFont = (range.font ? range.font.get() : nsnull);
|
|
|
|
|
2010-08-11 09:52:23 -07:00
|
|
|
// create the glyph run for this range
|
|
|
|
aTextRun->AddGlyphRun(matchedFont ? matchedFont : mainFont,
|
|
|
|
runStart, (matchedLength > 0));
|
2009-10-07 08:26:58 -07:00
|
|
|
if (matchedFont) {
|
2010-03-10 04:46:41 -08:00
|
|
|
// do glyph layout and record the resulting positioned glyphs
|
2010-08-11 09:52:23 -07:00
|
|
|
if (!matchedFont->InitTextRun(aContext, aTextRun, aString,
|
|
|
|
runStart, matchedLength,
|
|
|
|
aRunScript)) {
|
|
|
|
// glyph layout failed! treat as missing glyphs
|
|
|
|
matchedFont = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!matchedFont) {
|
2009-10-07 08:26:58 -07:00
|
|
|
for (PRUint32 index = runStart; index < runStart + matchedLength; index++) {
|
|
|
|
// Record the char code so we can draw a box with the Unicode value
|
|
|
|
if (NS_IS_HIGH_SURROGATE(aString[index]) &&
|
2010-06-11 12:14:38 -07:00
|
|
|
index + 1 < aScriptRunEnd &&
|
2009-10-07 08:26:58 -07:00
|
|
|
NS_IS_LOW_SURROGATE(aString[index+1])) {
|
|
|
|
aTextRun->SetMissingGlyph(index,
|
|
|
|
SURROGATE_TO_UCS4(aString[index],
|
|
|
|
aString[index+1]));
|
|
|
|
index++;
|
|
|
|
} else {
|
|
|
|
aTextRun->SetMissingGlyph(index, aString[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runStart += matchedLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's possible for CoreText to omit glyph runs if it decides they contain
|
|
|
|
// only invisibles (e.g., U+FEFF, see reftest 474417-1). In this case, we
|
|
|
|
// need to eliminate them from the glyph run array to avoid drawing "partial
|
|
|
|
// ligatures" with the wrong font.
|
|
|
|
aTextRun->SanitizeGlyphRuns();
|
|
|
|
|
|
|
|
// Is this actually necessary? Without it, gfxTextRun::CopyGlyphDataFrom may assert
|
|
|
|
// "Glyphruns not coalesced", but does that matter?
|
|
|
|
aTextRun->SortGlyphRuns();
|
|
|
|
|
2010-07-13 13:31:31 -07:00
|
|
|
#ifdef DUMP_TEXT_RUNS
|
|
|
|
nsCAutoString lang;
|
|
|
|
style->language->ToUTF8String(lang);
|
|
|
|
PR_LOG(gFontSelection, PR_LOG_DEBUG,\
|
|
|
|
("InitTextRun %p fontgroup %p (%s) lang: %s len %d features: %s "
|
|
|
|
"TEXTRUN \"%s\" ENDTEXTRUN\n",
|
|
|
|
aTextRun, this,
|
|
|
|
NS_ConvertUTF16toUTF8(mFamilies).get(),
|
|
|
|
lang.get(), aLength,
|
|
|
|
NS_ConvertUTF16toUTF8(mStyle.featureSettings).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aString, aLength).get()) );
|
|
|
|
#endif
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
|
|
|
|
|
|
|
already_AddRefed<gfxFont>
|
2010-11-07 14:44:51 -08:00
|
|
|
gfxFontGroup::FindFontForChar(PRUint32 aCh, PRUint32 aPrevCh,
|
|
|
|
PRInt32 aRunScript, gfxFont *aPrevMatchedFont)
|
2008-08-05 21:34:06 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<gfxFont> selectedFont;
|
|
|
|
|
2009-12-16 07:47:12 -08:00
|
|
|
// if this character or the previous one is a join-causer,
|
|
|
|
// use the same font as the previous range if we can
|
|
|
|
if (gfxFontUtils::IsJoinCauser(aCh) || gfxFontUtils::IsJoinCauser(aPrevCh)) {
|
2008-08-05 21:34:06 -07:00
|
|
|
if (aPrevMatchedFont && aPrevMatchedFont->HasCharacter(aCh)) {
|
|
|
|
selectedFont = aPrevMatchedFont;
|
|
|
|
return selectedFont.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-01 06:42:37 -07:00
|
|
|
// if this character is a variation selector,
|
|
|
|
// use the previous font regardless of whether it supports VS or not.
|
|
|
|
// otherwise the text run will be divided.
|
|
|
|
if (gfxFontUtils::IsVarSelector(aCh)) {
|
|
|
|
if (aPrevMatchedFont) {
|
|
|
|
selectedFont = aPrevMatchedFont;
|
|
|
|
return selectedFont.forget();
|
|
|
|
}
|
|
|
|
// VS alone. it's meaningless to search different fonts
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2008-08-05 21:34:06 -07:00
|
|
|
// 1. check fonts in the font group
|
|
|
|
for (PRUint32 i = 0; i < FontListLength(); i++) {
|
|
|
|
nsRefPtr<gfxFont> font = GetFontAt(i);
|
|
|
|
if (font->HasCharacter(aCh))
|
|
|
|
return font.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if character is in Private Use Area, don't do matching against pref or system fonts
|
|
|
|
if ((aCh >= 0xE000 && aCh <= 0xF8FF) || (aCh >= 0xF0000 && aCh <= 0x10FFFD))
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
// 2. search pref fonts
|
|
|
|
if ((selectedFont = WhichPrefFontSupportsChar(aCh))) {
|
|
|
|
return selectedFont.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. use fallback fonts
|
|
|
|
// -- before searching for something else check the font used for the previous character
|
|
|
|
if (!selectedFont && aPrevMatchedFont && aPrevMatchedFont->HasCharacter(aCh)) {
|
|
|
|
selectedFont = aPrevMatchedFont;
|
|
|
|
return selectedFont.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- otherwise look for other stuff
|
|
|
|
if (!selectedFont) {
|
|
|
|
selectedFont = WhichSystemFontSupportsChar(aCh);
|
|
|
|
return selectedFont.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-07 14:44:51 -08:00
|
|
|
void gfxFontGroup::ComputeRanges(nsTArray<gfxTextRange>& aRanges,
|
|
|
|
const PRUnichar *aString,
|
|
|
|
PRUint32 begin, PRUint32 end,
|
|
|
|
PRInt32 aRunScript)
|
2008-08-05 21:34:06 -07:00
|
|
|
{
|
|
|
|
const PRUnichar *str = aString + begin;
|
|
|
|
PRUint32 len = end - begin;
|
|
|
|
|
|
|
|
aRanges.Clear();
|
|
|
|
|
2009-04-09 18:32:24 -07:00
|
|
|
if (len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-05 21:34:06 -07:00
|
|
|
PRUint32 prevCh = 0;
|
|
|
|
for (PRUint32 i = 0; i < len; i++) {
|
|
|
|
|
|
|
|
const PRUint32 origI = i; // save off in case we increase for surrogate
|
|
|
|
|
|
|
|
// set up current ch
|
|
|
|
PRUint32 ch = str[i];
|
|
|
|
if ((i+1 < len) && NS_IS_HIGH_SURROGATE(ch) && NS_IS_LOW_SURROGATE(str[i+1])) {
|
|
|
|
i++;
|
|
|
|
ch = SURROGATE_TO_UCS4(ch, str[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the font for this char
|
2010-11-07 14:44:51 -08:00
|
|
|
nsRefPtr<gfxFont> font =
|
|
|
|
FindFontForChar(ch, prevCh, aRunScript,
|
|
|
|
(aRanges.Length() == 0) ?
|
|
|
|
nsnull : aRanges[aRanges.Length() - 1].font.get());
|
2008-08-05 21:34:06 -07:00
|
|
|
|
|
|
|
prevCh = ch;
|
|
|
|
|
|
|
|
if (aRanges.Length() == 0) {
|
|
|
|
// first char ==> make a new range
|
|
|
|
gfxTextRange r(0,1);
|
|
|
|
r.font = font;
|
|
|
|
aRanges.AppendElement(r);
|
|
|
|
} else {
|
|
|
|
// if font has changed, make a new range
|
|
|
|
gfxTextRange& prevRange = aRanges[aRanges.Length() - 1];
|
|
|
|
if (prevRange.font != font) {
|
|
|
|
// close out the previous range
|
|
|
|
prevRange.end = origI;
|
|
|
|
|
|
|
|
gfxTextRange r(origI, i+1);
|
|
|
|
r.font = font;
|
|
|
|
aRanges.AppendElement(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aRanges[aRanges.Length()-1].end = len;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:01:53 -07:00
|
|
|
gfxUserFontSet*
|
|
|
|
gfxFontGroup::GetUserFontSet()
|
|
|
|
{
|
|
|
|
return mUserFontSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontGroup::SetUserFontSet(gfxUserFontSet *aUserFontSet)
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mUserFontSet);
|
|
|
|
mUserFontSet = aUserFontSet;
|
|
|
|
NS_IF_ADDREF(mUserFontSet);
|
|
|
|
mCurrGeneration = GetGeneration();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint64
|
|
|
|
gfxFontGroup::GetGeneration()
|
|
|
|
{
|
|
|
|
if (!mUserFontSet)
|
|
|
|
return 0;
|
|
|
|
return mUserFontSet->GetGeneration();
|
|
|
|
}
|
|
|
|
|
2009-10-07 08:26:58 -07:00
|
|
|
void
|
|
|
|
gfxFontGroup::UpdateFontList()
|
|
|
|
{
|
|
|
|
// if user font set is set, check to see if font list needs updating
|
|
|
|
if (mUserFontSet && mCurrGeneration != GetGeneration()) {
|
|
|
|
// xxx - can probably improve this to detect when all fonts were found, so no need to update list
|
|
|
|
mFonts.Clear();
|
|
|
|
mUnderlineOffset = UNDERLINE_OFFSET_NOT_SET;
|
2010-02-23 23:18:49 -08:00
|
|
|
|
|
|
|
// bug 548184 - need to clean up FT2, OS/2 platform code to use BuildFontList
|
|
|
|
#if defined(XP_MACOSX) || defined(XP_WIN)
|
|
|
|
BuildFontList();
|
|
|
|
#else
|
2009-10-07 08:26:58 -07:00
|
|
|
ForEachFont(FindPlatformFont, this);
|
2010-02-23 23:18:49 -08:00
|
|
|
#endif
|
2009-10-07 08:26:58 -07:00
|
|
|
mCurrGeneration = GetGeneration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PrefFontCallbackData {
|
|
|
|
PrefFontCallbackData(nsTArray<nsRefPtr<gfxFontFamily> >& aFamiliesArray)
|
|
|
|
: mPrefFamilies(aFamiliesArray)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<gfxFontFamily> >& mPrefFamilies;
|
|
|
|
|
|
|
|
static PRBool AddFontFamilyEntry(eFontPrefLang aLang, const nsAString& aName, void *aClosure)
|
|
|
|
{
|
|
|
|
PrefFontCallbackData *prefFontData = static_cast<PrefFontCallbackData*>(aClosure);
|
|
|
|
|
|
|
|
gfxFontFamily *family = gfxPlatformFontList::PlatformFontList()->FindFamily(aName);
|
|
|
|
if (family) {
|
|
|
|
prefFontData->mPrefFamilies.AppendElement(family);
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<gfxFont>
|
|
|
|
gfxFontGroup::WhichPrefFontSupportsChar(PRUint32 aCh)
|
|
|
|
{
|
|
|
|
gfxFont *font;
|
|
|
|
|
|
|
|
// FindCharUnicodeRange only supports BMP character points and there are no non-BMP fonts in prefs
|
|
|
|
if (aCh > 0xFFFF)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
// get the pref font list if it hasn't been set up already
|
|
|
|
PRUint32 unicodeRange = FindCharUnicodeRange(aCh);
|
|
|
|
eFontPrefLang charLang = gfxPlatform::GetPlatform()->GetFontPrefLangFor(unicodeRange);
|
|
|
|
|
|
|
|
// if the last pref font was the first family in the pref list, no need to recheck through a list of families
|
|
|
|
if (mLastPrefFont && charLang == mLastPrefLang &&
|
|
|
|
mLastPrefFirstFont && mLastPrefFont->HasCharacter(aCh)) {
|
|
|
|
font = mLastPrefFont;
|
|
|
|
NS_ADDREF(font);
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
// based on char lang and page lang, set up list of pref lang fonts to check
|
|
|
|
eFontPrefLang prefLangs[kMaxLenPrefLangList];
|
|
|
|
PRUint32 i, numLangs = 0;
|
|
|
|
|
|
|
|
gfxPlatform::GetPlatform()->GetLangPrefs(prefLangs, numLangs, charLang, mPageLang);
|
|
|
|
|
|
|
|
for (i = 0; i < numLangs; i++) {
|
|
|
|
nsAutoTArray<nsRefPtr<gfxFontFamily>, 5> families;
|
|
|
|
eFontPrefLang currentLang = prefLangs[i];
|
|
|
|
|
|
|
|
gfxPlatformFontList *fontList = gfxPlatformFontList::PlatformFontList();
|
|
|
|
|
|
|
|
// get the pref families for a single pref lang
|
|
|
|
if (!fontList->GetPrefFontFamilyEntries(currentLang, &families)) {
|
|
|
|
eFontPrefLang prefLangsToSearch[1] = { currentLang };
|
|
|
|
PrefFontCallbackData prefFontData(families);
|
|
|
|
gfxPlatform::ForEachPrefFont(prefLangsToSearch, 1, PrefFontCallbackData::AddFontFamilyEntry,
|
|
|
|
&prefFontData);
|
|
|
|
fontList->SetPrefFontFamilyEntries(currentLang, families);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the first pref font that includes the character
|
|
|
|
PRUint32 i, numPrefs;
|
|
|
|
numPrefs = families.Length();
|
|
|
|
for (i = 0; i < numPrefs; i++) {
|
|
|
|
// look up the appropriate face
|
|
|
|
gfxFontFamily *family = families[i];
|
|
|
|
if (!family) continue;
|
|
|
|
|
|
|
|
// if a pref font is used, it's likely to be used again in the same text run.
|
|
|
|
// the style doesn't change so the face lookup can be cached rather than calling
|
2010-02-17 19:31:20 -08:00
|
|
|
// FindOrMakeFont repeatedly. speeds up FindFontForChar lookup times for subsequent
|
2009-10-07 08:26:58 -07:00
|
|
|
// pref font lookups
|
|
|
|
if (family == mLastPrefFamily && mLastPrefFont->HasCharacter(aCh)) {
|
|
|
|
font = mLastPrefFont;
|
|
|
|
NS_ADDREF(font);
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool needsBold;
|
|
|
|
gfxFontEntry *fe = family->FindFontForStyle(mStyle, needsBold);
|
|
|
|
// if ch in cmap, create and return a gfxFont
|
|
|
|
if (fe && fe->TestCharacterMap(aCh)) {
|
2010-02-17 19:31:20 -08:00
|
|
|
nsRefPtr<gfxFont> prefFont = fe->FindOrMakeFont(&mStyle, needsBold);
|
2009-10-07 08:26:58 -07:00
|
|
|
if (!prefFont) continue;
|
|
|
|
mLastPrefFamily = family;
|
|
|
|
mLastPrefFont = prefFont;
|
|
|
|
mLastPrefLang = charLang;
|
|
|
|
mLastPrefFirstFont = (i == 0);
|
|
|
|
return prefFont.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxFont>
|
|
|
|
gfxFontGroup::WhichSystemFontSupportsChar(PRUint32 aCh)
|
|
|
|
{
|
|
|
|
gfxFontEntry *fe =
|
|
|
|
gfxPlatformFontList::PlatformFontList()->FindFontForChar(aCh, GetFontAt(0));
|
|
|
|
if (fe) {
|
2010-02-17 19:31:20 -08:00
|
|
|
nsRefPtr<gfxFont> font = fe->FindOrMakeFont(&mStyle, PR_FALSE); // ignore bolder considerations in system fallback case...
|
2009-10-07 08:26:58 -07:00
|
|
|
return font.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2010-02-24 09:57:44 -08:00
|
|
|
/*static*/ void
|
|
|
|
gfxFontGroup::Shutdown()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(gLangService);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsILanguageAtomService* gfxFontGroup::gLangService = nsnull;
|
|
|
|
|
|
|
|
|
2008-03-12 03:52:58 -07:00
|
|
|
#define DEFAULT_PIXEL_FONT_SIZE 16.0f
|
|
|
|
|
2010-07-13 13:31:31 -07:00
|
|
|
/*static*/ void
|
|
|
|
gfxFontStyle::ParseFontFeatureSettings(const nsString& aFeatureString,
|
|
|
|
nsTArray<gfxFontFeature>& aFeatures)
|
|
|
|
{
|
|
|
|
aFeatures.Clear();
|
|
|
|
PRUint32 offset = 0;
|
|
|
|
while (offset < aFeatureString.Length()) {
|
|
|
|
// skip whitespace
|
|
|
|
while (offset < aFeatureString.Length() &&
|
|
|
|
nsCRT::IsAsciiSpace(aFeatureString[offset])) {
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
PRInt32 limit = aFeatureString.FindChar(',', offset);
|
|
|
|
if (limit < 0) {
|
|
|
|
limit = aFeatureString.Length();
|
|
|
|
}
|
|
|
|
// check that we have enough text for a 4-char tag,
|
|
|
|
// the '=' sign, and at least one digit
|
|
|
|
if (offset + 6 <= PRUint32(limit) &&
|
|
|
|
aFeatureString[offset+4] == '=') {
|
|
|
|
gfxFontFeature setting;
|
|
|
|
setting.mTag =
|
|
|
|
((aFeatureString[offset] & 0xff) << 24) +
|
|
|
|
((aFeatureString[offset+1] & 0xff) << 16) +
|
|
|
|
((aFeatureString[offset+2] & 0xff) << 8) +
|
|
|
|
(aFeatureString[offset+3] & 0xff);
|
|
|
|
nsString valString;
|
|
|
|
aFeatureString.Mid(valString, offset+5, limit-offset-5);
|
|
|
|
PRInt32 rv;
|
|
|
|
setting.mValue = valString.ToInteger(&rv);
|
|
|
|
if (rv == NS_OK) {
|
|
|
|
// we keep the features array sorted so that we can
|
|
|
|
// use nsTArray<>::Equals() to compare feature lists
|
|
|
|
aFeatures.InsertElementSorted(setting);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset = limit + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ PRUint32
|
|
|
|
gfxFontStyle::ParseFontLanguageOverride(const nsString& aLangTag)
|
|
|
|
{
|
|
|
|
if (!aLangTag.Length() || aLangTag.Length() > 4) {
|
|
|
|
return NO_FONT_LANGUAGE_OVERRIDE;
|
|
|
|
}
|
|
|
|
PRUint32 index, result = 0;
|
|
|
|
for (index = 0; index < aLangTag.Length(); ++index) {
|
|
|
|
PRUnichar ch = aLangTag[index];
|
|
|
|
if (!nsCRT::IsAscii(ch)) { // valid tags are pure ASCII
|
|
|
|
return NO_FONT_LANGUAGE_OVERRIDE;
|
|
|
|
}
|
|
|
|
result = (result << 8) + ch;
|
|
|
|
}
|
|
|
|
while (index++ < 4) {
|
|
|
|
result = (result << 8) + 0x20;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-03-12 03:52:58 -07:00
|
|
|
gfxFontStyle::gfxFontStyle() :
|
2008-12-16 19:17:50 -08:00
|
|
|
style(FONT_STYLE_NORMAL), systemFont(PR_TRUE), printerFont(PR_FALSE),
|
2009-01-29 12:39:18 -08:00
|
|
|
familyNameQuirks(PR_FALSE), weight(FONT_WEIGHT_NORMAL),
|
|
|
|
stretch(NS_FONT_STRETCH_NORMAL), size(DEFAULT_PIXEL_FONT_SIZE),
|
2010-07-13 13:31:31 -07:00
|
|
|
sizeAdjust(0.0f),
|
|
|
|
language(gfxAtoms::x_western),
|
|
|
|
languageOverride(NO_FONT_LANGUAGE_OVERRIDE),
|
|
|
|
featureSettings(nsnull)
|
2008-03-12 03:52:58 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-01-29 12:39:18 -08:00
|
|
|
gfxFontStyle::gfxFontStyle(PRUint8 aStyle, PRUint16 aWeight, PRInt16 aStretch,
|
2010-02-24 09:57:57 -08:00
|
|
|
gfxFloat aSize, nsIAtom *aLanguage,
|
2007-03-22 10:30:00 -07:00
|
|
|
float aSizeAdjust, PRPackedBool aSystemFont,
|
2008-12-16 19:17:50 -08:00
|
|
|
PRPackedBool aFamilyNameQuirks,
|
2010-07-13 13:31:31 -07:00
|
|
|
PRPackedBool aPrinterFont,
|
|
|
|
const nsString& aFeatureSettings,
|
|
|
|
const nsString& aLanguageOverride):
|
2008-12-16 19:17:50 -08:00
|
|
|
style(aStyle), systemFont(aSystemFont), printerFont(aPrinterFont),
|
2009-01-29 12:39:18 -08:00
|
|
|
familyNameQuirks(aFamilyNameQuirks), weight(aWeight), stretch(aStretch),
|
2010-07-13 13:31:31 -07:00
|
|
|
size(aSize), sizeAdjust(aSizeAdjust),
|
|
|
|
language(aLanguage),
|
|
|
|
languageOverride(ParseFontLanguageOverride(aLanguageOverride)),
|
|
|
|
featureSettings(nsnull)
|
|
|
|
{
|
|
|
|
if (!aFeatureSettings.IsEmpty()) {
|
|
|
|
featureSettings = new nsTArray<gfxFontFeature>;
|
|
|
|
ParseFontFeatureSettings(aFeatureSettings, *featureSettings);
|
|
|
|
if (featureSettings->Length() == 0) {
|
|
|
|
delete featureSettings;
|
|
|
|
featureSettings = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (weight > 900)
|
|
|
|
weight = 900;
|
|
|
|
if (weight < 100)
|
|
|
|
weight = 100;
|
|
|
|
|
2007-06-08 01:22:03 -07:00
|
|
|
if (size >= FONT_MAX_SIZE) {
|
|
|
|
size = FONT_MAX_SIZE;
|
2007-05-30 01:32:50 -07:00
|
|
|
sizeAdjust = 0.0;
|
|
|
|
} else if (size < 0.0) {
|
|
|
|
NS_WARNING("negative font size");
|
|
|
|
size = 0.0;
|
|
|
|
}
|
|
|
|
|
2010-02-24 09:57:57 -08:00
|
|
|
if (!language) {
|
|
|
|
NS_WARNING("null language");
|
|
|
|
language = gfxAtoms::x_western;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-03 20:32:43 -07:00
|
|
|
gfxFontStyle::gfxFontStyle(const gfxFontStyle& aStyle) :
|
2008-12-16 19:17:50 -08:00
|
|
|
style(aStyle.style), systemFont(aStyle.systemFont), printerFont(aStyle.printerFont),
|
2007-04-03 20:32:43 -07:00
|
|
|
familyNameQuirks(aStyle.familyNameQuirks), weight(aStyle.weight),
|
2010-07-13 13:31:31 -07:00
|
|
|
stretch(aStyle.stretch), size(aStyle.size),
|
|
|
|
sizeAdjust(aStyle.sizeAdjust),
|
|
|
|
language(aStyle.language),
|
|
|
|
languageOverride(aStyle.languageOverride),
|
|
|
|
featureSettings(nsnull)
|
2007-04-03 20:32:43 -07:00
|
|
|
{
|
2010-07-13 13:31:31 -07:00
|
|
|
if (aStyle.featureSettings) {
|
|
|
|
featureSettings = new nsTArray<gfxFontFeature>;
|
|
|
|
featureSettings->AppendElements(*aStyle.featureSettings);
|
|
|
|
}
|
2007-04-03 20:32:43 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 07:49:52 -08:00
|
|
|
PRInt8
|
|
|
|
gfxFontStyle::ComputeWeight() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PRInt8 baseWeight = (weight + 50) / 100;
|
|
|
|
|
|
|
|
if (baseWeight < 0)
|
|
|
|
baseWeight = 0;
|
|
|
|
if (baseWeight > 9)
|
|
|
|
baseWeight = 9;
|
|
|
|
|
2010-11-10 07:49:52 -08:00
|
|
|
return baseWeight;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxTextRun::GlyphRunIterator::NextRun() {
|
|
|
|
if (mNextIndex >= mTextRun->mGlyphRuns.Length())
|
|
|
|
return PR_FALSE;
|
|
|
|
mGlyphRun = &mTextRun->mGlyphRuns[mNextIndex];
|
|
|
|
if (mGlyphRun->mCharacterOffset >= mEndOffset)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
mStringStart = PR_MAX(mStartOffset, mGlyphRun->mCharacterOffset);
|
|
|
|
PRUint32 last = mNextIndex + 1 < mTextRun->mGlyphRuns.Length()
|
|
|
|
? mTextRun->mGlyphRuns[mNextIndex + 1].mCharacterOffset : mTextRun->mCharacterCount;
|
|
|
|
mStringEnd = PR_MIN(mEndOffset, last);
|
|
|
|
|
|
|
|
++mNextIndex;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-07-01 18:24:56 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
2007-07-01 18:12:45 -07:00
|
|
|
static void
|
|
|
|
AccountStorageForTextRun(gfxTextRun *aTextRun, PRInt32 aSign)
|
|
|
|
{
|
|
|
|
// Ignores detailed glyphs... we don't know when those have been constructed
|
|
|
|
// Also ignores gfxSkipChars dynamic storage (which won't be anything
|
|
|
|
// for preformatted text)
|
|
|
|
// Also ignores GlyphRun array, again because it hasn't been constructed
|
|
|
|
// by the time this gets called. If there's only one glyphrun that's stored
|
|
|
|
// directly in the textrun anyway so no additional overhead.
|
2010-02-18 03:52:34 -08:00
|
|
|
PRUint32 length = aTextRun->GetLength();
|
|
|
|
PRInt32 bytes = length * sizeof(gfxTextRun::CompressedGlyph);
|
2007-07-01 18:12:45 -07:00
|
|
|
if (aTextRun->GetFlags() & gfxTextRunFactory::TEXT_IS_PERSISTENT) {
|
2010-02-18 03:52:34 -08:00
|
|
|
bytes += length * ((aTextRun->GetFlags() & gfxTextRunFactory::TEXT_IS_8BIT) ? 1 : 2);
|
|
|
|
bytes += sizeof(gfxTextRun::CompressedGlyph) - 1;
|
|
|
|
bytes &= ~(sizeof(gfxTextRun::CompressedGlyph) - 1);
|
2007-07-01 18:12:45 -07:00
|
|
|
}
|
2010-02-18 03:52:34 -08:00
|
|
|
bytes += sizeof(gfxTextRun);
|
2007-07-01 18:12:45 -07:00
|
|
|
gTextRunStorage += bytes*aSign;
|
|
|
|
gTextRunStorageHighWaterMark = PR_MAX(gTextRunStorageHighWaterMark, gTextRunStorage);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-18 03:52:34 -08:00
|
|
|
// Helper for textRun creation to preallocate storage for glyphs and text;
|
|
|
|
// this function returns a pointer to the newly-allocated glyph storage,
|
|
|
|
// AND modifies the aText parameter if TEXT_IS_PERSISTENT was not set.
|
|
|
|
// In that case, the text is appended to the glyph storage, so a single
|
|
|
|
// delete[] operation in the textRun destructor will free both.
|
|
|
|
// Returns nsnull if allocation fails.
|
|
|
|
gfxTextRun::CompressedGlyph *
|
|
|
|
gfxTextRun::AllocateStorage(const void*& aText, PRUint32 aLength, PRUint32 aFlags)
|
2007-11-15 17:43:47 -08:00
|
|
|
{
|
2010-02-18 03:52:34 -08:00
|
|
|
// Here, we rely on CompressedGlyph being the largest unit we care about for
|
|
|
|
// allocation/alignment of either glyph data or text, so we allocate an array
|
|
|
|
// of CompressedGlyphs, then take the last chunk of that and cast a pointer to
|
|
|
|
// PRUint8* or PRUnichar* for text storage.
|
2007-11-15 17:43:47 -08:00
|
|
|
|
2010-02-18 03:52:34 -08:00
|
|
|
// always need to allocate storage for the glyph data
|
|
|
|
PRUint64 allocCount = aLength;
|
|
|
|
|
|
|
|
// if the text is not persistent, we also need space for a copy
|
2007-11-15 17:43:47 -08:00
|
|
|
if (!(aFlags & gfxTextRunFactory::TEXT_IS_PERSISTENT)) {
|
2010-02-18 03:52:34 -08:00
|
|
|
// figure out number of extra CompressedGlyph elements we need to
|
|
|
|
// get sufficient space for the text
|
|
|
|
if (aFlags & gfxTextRunFactory::TEXT_IS_8BIT) {
|
|
|
|
allocCount += (aLength + sizeof(CompressedGlyph)-1)
|
|
|
|
/ sizeof(CompressedGlyph);
|
|
|
|
} else {
|
|
|
|
allocCount += (aLength*sizeof(PRUnichar) + sizeof(CompressedGlyph)-1)
|
|
|
|
/ sizeof(CompressedGlyph);
|
|
|
|
}
|
2007-11-15 17:43:47 -08:00
|
|
|
}
|
|
|
|
|
2010-02-18 03:52:34 -08:00
|
|
|
// allocate the storage we need, returning nsnull on failure rather than
|
|
|
|
// throwing an exception (because web content can create huge runs)
|
|
|
|
CompressedGlyph *storage = new (std::nothrow) CompressedGlyph[allocCount];
|
|
|
|
if (!storage) {
|
|
|
|
NS_WARNING("failed to allocate glyph/text storage for text run!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the text if we need to keep a copy in the textrun
|
|
|
|
if (!(aFlags & gfxTextRunFactory::TEXT_IS_PERSISTENT)) {
|
|
|
|
if (aFlags & gfxTextRunFactory::TEXT_IS_8BIT) {
|
|
|
|
PRUint8 *newText = reinterpret_cast<PRUint8*>(storage + aLength);
|
|
|
|
memcpy(newText, aText, aLength);
|
|
|
|
aText = newText;
|
|
|
|
} else {
|
|
|
|
PRUnichar *newText = reinterpret_cast<PRUnichar*>(storage + aLength);
|
|
|
|
memcpy(newText, aText, aLength*sizeof(PRUnichar));
|
|
|
|
aText = newText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage;
|
2007-11-15 17:43:47 -08:00
|
|
|
}
|
|
|
|
|
2010-02-18 03:52:34 -08:00
|
|
|
gfxTextRun *
|
|
|
|
gfxTextRun::Create(const gfxTextRunFactory::Parameters *aParams, const void *aText,
|
|
|
|
PRUint32 aLength, gfxFontGroup *aFontGroup, PRUint32 aFlags)
|
2007-11-15 17:43:47 -08:00
|
|
|
{
|
2010-02-18 03:52:34 -08:00
|
|
|
CompressedGlyph *glyphStorage = AllocateStorage(aText, aLength, aFlags);
|
|
|
|
if (!glyphStorage) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new gfxTextRun(aParams, aText, aLength, aFontGroup, aFlags, glyphStorage);
|
2007-11-15 17:43:47 -08:00
|
|
|
}
|
|
|
|
|
2007-05-08 15:46:14 -07:00
|
|
|
gfxTextRun::gfxTextRun(const gfxTextRunFactory::Parameters *aParams, const void *aText,
|
2007-11-15 17:43:47 -08:00
|
|
|
PRUint32 aLength, gfxFontGroup *aFontGroup, PRUint32 aFlags,
|
2010-02-18 03:52:34 -08:00
|
|
|
CompressedGlyph *aGlyphStorage)
|
|
|
|
: mCharacterGlyphs(aGlyphStorage),
|
|
|
|
mUserData(aParams->mUserData),
|
2007-05-08 15:46:14 -07:00
|
|
|
mFontGroup(aFontGroup),
|
2007-03-22 10:30:00 -07:00
|
|
|
mAppUnitsPerDevUnit(aParams->mAppUnitsPerDevUnit),
|
2007-05-11 22:51:15 -07:00
|
|
|
mFlags(aFlags), mCharacterCount(aLength), mHashCode(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-06 05:30:13 -07:00
|
|
|
NS_ASSERTION(mAppUnitsPerDevUnit != 0, "Invalid app unit scale");
|
2007-06-10 16:07:29 -07:00
|
|
|
MOZ_COUNT_CTOR(gfxTextRun);
|
2007-05-08 15:46:14 -07:00
|
|
|
NS_ADDREF(mFontGroup);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aParams->mSkipChars) {
|
|
|
|
mSkipChars.TakeFrom(aParams->mSkipChars);
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2007-05-08 15:46:14 -07:00
|
|
|
if (mFlags & gfxTextRunFactory::TEXT_IS_8BIT) {
|
2007-07-08 00:08:04 -07:00
|
|
|
mText.mSingle = static_cast<const PRUint8 *>(aText);
|
2007-05-08 15:46:14 -07:00
|
|
|
} else {
|
2007-07-08 00:08:04 -07:00
|
|
|
mText.mDouble = static_cast<const PRUnichar *>(aText);
|
2007-05-08 15:46:14 -07:00
|
|
|
}
|
2007-07-01 18:24:56 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
2007-07-01 18:12:45 -07:00
|
|
|
AccountStorageForTextRun(this, 1);
|
|
|
|
#endif
|
2008-09-30 20:01:53 -07:00
|
|
|
|
|
|
|
mUserFontSetGeneration = mFontGroup->GetGeneration();
|
2007-05-08 15:46:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun::~gfxTextRun()
|
|
|
|
{
|
2007-07-01 18:24:56 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
2007-07-01 18:12:45 -07:00
|
|
|
AccountStorageForTextRun(this, -1);
|
2009-01-08 16:23:28 -08:00
|
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Make it easy to detect a dead text run
|
|
|
|
mFlags = 0xFFFFFFFF;
|
2007-07-01 18:12:45 -07:00
|
|
|
#endif
|
2010-02-18 03:52:34 -08:00
|
|
|
|
|
|
|
// this will also delete the text, if it is owned by the run,
|
|
|
|
// because we merge the storage allocations
|
|
|
|
delete [] mCharacterGlyphs;
|
|
|
|
|
2007-05-08 15:46:14 -07:00
|
|
|
NS_RELEASE(mFontGroup);
|
2007-06-10 16:07:29 -07:00
|
|
|
MOZ_COUNT_DTOR(gfxTextRun);
|
2007-05-08 15:46:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun *
|
|
|
|
gfxTextRun::Clone(const gfxTextRunFactory::Parameters *aParams, const void *aText,
|
|
|
|
PRUint32 aLength, gfxFontGroup *aFontGroup, PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
if (!mCharacterGlyphs)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
nsAutoPtr<gfxTextRun> textRun;
|
2007-11-15 17:43:47 -08:00
|
|
|
textRun = gfxTextRun::Create(aParams, aText, aLength, aFontGroup, aFlags);
|
|
|
|
if (!textRun)
|
2007-05-08 15:46:14 -07:00
|
|
|
return nsnull;
|
|
|
|
|
2007-06-12 13:56:04 -07:00
|
|
|
textRun->CopyGlyphDataFrom(this, 0, mCharacterCount, 0, PR_FALSE);
|
2007-05-08 15:46:14 -07:00
|
|
|
return textRun.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxTextRun::SetPotentialLineBreaks(PRUint32 aStart, PRUint32 aLength,
|
2007-06-26 21:22:21 -07:00
|
|
|
PRPackedBool *aBreakBefore,
|
|
|
|
gfxContext *aRefContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart + aLength <= mCharacterCount, "Overflow");
|
|
|
|
|
|
|
|
if (!mCharacterGlyphs)
|
|
|
|
return PR_TRUE;
|
|
|
|
PRUint32 changed = 0;
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < aLength; ++i) {
|
2007-08-09 18:35:32 -07:00
|
|
|
PRBool canBreak = aBreakBefore[i];
|
|
|
|
if (canBreak && !mCharacterGlyphs[aStart + i].IsClusterStart()) {
|
|
|
|
// This can happen ... there is no guarantee that our linebreaking rules
|
|
|
|
// align with the platform's idea of what constitutes a cluster.
|
|
|
|
NS_WARNING("Break suggested inside cluster!");
|
|
|
|
canBreak = PR_FALSE;
|
|
|
|
}
|
|
|
|
changed |= mCharacterGlyphs[aStart + i].SetCanBreakBefore(canBreak);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return changed != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun::LigatureData
|
2007-08-09 18:35:32 -07:00
|
|
|
gfxTextRun::ComputeLigatureData(PRUint32 aPartStart, PRUint32 aPartEnd,
|
|
|
|
PropertyProvider *aProvider)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-09 18:35:32 -07:00
|
|
|
NS_ASSERTION(aPartStart < aPartEnd, "Computing ligature data for empty range");
|
|
|
|
NS_ASSERTION(aPartEnd <= mCharacterCount, "Character length overflow");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
LigatureData result;
|
|
|
|
CompressedGlyph *charGlyphs = mCharacterGlyphs;
|
2007-08-09 18:35:32 -07:00
|
|
|
|
|
|
|
PRUint32 i;
|
2007-11-08 22:27:23 -08:00
|
|
|
for (i = aPartStart; !charGlyphs[i].IsLigatureGroupStart(); --i) {
|
2007-08-09 18:35:32 -07:00
|
|
|
NS_ASSERTION(i > 0, "Ligature at the start of the run??");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-08-09 18:35:32 -07:00
|
|
|
result.mLigatureStart = i;
|
2007-11-08 22:27:23 -08:00
|
|
|
for (i = aPartStart + 1; i < mCharacterCount && !charGlyphs[i].IsLigatureGroupStart(); ++i) {
|
2007-08-09 18:35:32 -07:00
|
|
|
}
|
|
|
|
result.mLigatureEnd = i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-08 22:27:23 -08:00
|
|
|
PRInt32 ligatureWidth =
|
|
|
|
GetAdvanceForGlyphs(this, result.mLigatureStart, result.mLigatureEnd);
|
2007-03-22 10:30:00 -07:00
|
|
|
// Count the number of started clusters we have seen
|
2007-08-09 18:35:32 -07:00
|
|
|
PRUint32 totalClusterCount = 0;
|
|
|
|
PRUint32 partClusterIndex = 0;
|
|
|
|
PRUint32 partClusterCount = 0;
|
|
|
|
for (i = result.mLigatureStart; i < result.mLigatureEnd; ++i) {
|
2008-04-22 19:06:22 -07:00
|
|
|
// Treat the first character of the ligature as the start of a
|
|
|
|
// cluster for our purposes of allocating ligature width to its
|
|
|
|
// characters.
|
|
|
|
if (i == result.mLigatureStart || charGlyphs[i].IsClusterStart()) {
|
2007-08-09 18:35:32 -07:00
|
|
|
++totalClusterCount;
|
|
|
|
if (i < aPartStart) {
|
|
|
|
++partClusterIndex;
|
|
|
|
} else if (i < aPartEnd) {
|
|
|
|
++partClusterCount;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2009-03-29 17:31:51 -07:00
|
|
|
NS_ASSERTION(totalClusterCount > 0, "Ligature involving no clusters??");
|
2007-08-09 18:35:32 -07:00
|
|
|
result.mPartAdvance = ligatureWidth*partClusterIndex/totalClusterCount;
|
|
|
|
result.mPartWidth = ligatureWidth*partClusterCount/totalClusterCount;
|
2008-04-22 19:06:22 -07:00
|
|
|
|
|
|
|
if (partClusterCount == 0) {
|
|
|
|
// nothing to draw
|
|
|
|
result.mClipBeforePart = result.mClipAfterPart = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
// Determine whether we should clip before or after this part when
|
|
|
|
// drawing its slice of the ligature.
|
|
|
|
// We need to clip before the part if any cluster is drawn before
|
|
|
|
// this part.
|
|
|
|
result.mClipBeforePart = partClusterIndex > 0;
|
|
|
|
// We need to clip after the part if any cluster is drawn after
|
|
|
|
// this part.
|
|
|
|
result.mClipAfterPart = partClusterIndex + partClusterCount < totalClusterCount;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aProvider && (mFlags & gfxTextRunFactory::TEXT_ENABLE_SPACING)) {
|
2007-08-09 18:35:32 -07:00
|
|
|
gfxFont::Spacing spacing;
|
|
|
|
if (aPartStart == result.mLigatureStart) {
|
|
|
|
aProvider->GetSpacing(aPartStart, 1, &spacing);
|
|
|
|
result.mPartWidth += spacing.mBefore;
|
|
|
|
}
|
|
|
|
if (aPartEnd == result.mLigatureEnd) {
|
|
|
|
aProvider->GetSpacing(aPartEnd - 1, 1, &spacing);
|
|
|
|
result.mPartWidth += spacing.mAfter;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
gfxFloat
|
|
|
|
gfxTextRun::ComputePartialLigatureWidth(PRUint32 aPartStart, PRUint32 aPartEnd,
|
|
|
|
PropertyProvider *aProvider)
|
|
|
|
{
|
|
|
|
if (aPartStart >= aPartEnd)
|
|
|
|
return 0;
|
|
|
|
LigatureData data = ComputeLigatureData(aPartStart, aPartEnd, aProvider);
|
|
|
|
return data.mPartWidth;
|
|
|
|
}
|
|
|
|
|
2007-11-08 22:27:23 -08:00
|
|
|
static void
|
|
|
|
GetAdjustedSpacing(gfxTextRun *aTextRun, PRUint32 aStart, PRUint32 aEnd,
|
|
|
|
gfxTextRun::PropertyProvider *aProvider,
|
|
|
|
gfxTextRun::PropertyProvider::Spacing *aSpacing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aStart >= aEnd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
aProvider->GetSpacing(aStart, aEnd - aStart, aSpacing);
|
|
|
|
|
2007-11-07 23:41:06 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Check to see if we have spacing inside ligatures
|
2007-11-08 22:27:23 -08:00
|
|
|
|
|
|
|
const gfxTextRun::CompressedGlyph *charGlyphs = aTextRun->GetCharacterGlyphs();
|
|
|
|
PRUint32 i;
|
|
|
|
|
2007-05-30 22:01:56 -07:00
|
|
|
for (i = aStart; i < aEnd; ++i) {
|
2007-11-08 22:27:23 -08:00
|
|
|
if (!charGlyphs[i].IsLigatureGroupStart()) {
|
2007-05-30 22:01:56 -07:00
|
|
|
NS_ASSERTION(i == aStart || aSpacing[i - aStart].mBefore == 0,
|
|
|
|
"Before-spacing inside a ligature!");
|
|
|
|
NS_ASSERTION(i - 1 <= aStart || aSpacing[i - 1 - aStart].mAfter == 0,
|
|
|
|
"After-spacing inside a ligature!");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-05-30 22:01:56 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxTextRun::GetAdjustedSpacingArray(PRUint32 aStart, PRUint32 aEnd,
|
|
|
|
PropertyProvider *aProvider,
|
2007-08-09 18:35:32 -07:00
|
|
|
PRUint32 aSpacingStart, PRUint32 aSpacingEnd,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTArray<PropertyProvider::Spacing> *aSpacing)
|
|
|
|
{
|
2007-05-11 22:53:29 -07:00
|
|
|
if (!aProvider || !(mFlags & gfxTextRunFactory::TEXT_ENABLE_SPACING))
|
2007-03-22 10:30:00 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
if (!aSpacing->AppendElements(aEnd - aStart))
|
|
|
|
return PR_FALSE;
|
2007-08-09 18:35:32 -07:00
|
|
|
memset(aSpacing->Elements(), 0, sizeof(gfxFont::Spacing)*(aSpacingStart - aStart));
|
2007-11-08 22:27:23 -08:00
|
|
|
GetAdjustedSpacing(this, aSpacingStart, aSpacingEnd, aProvider,
|
2007-08-09 18:35:32 -07:00
|
|
|
aSpacing->Elements() + aSpacingStart - aStart);
|
|
|
|
memset(aSpacing->Elements() + aSpacingEnd - aStart, 0, sizeof(gfxFont::Spacing)*(aEnd - aSpacingEnd));
|
2007-03-22 10:30:00 -07:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxTextRun::ShrinkToLigatureBoundaries(PRUint32 *aStart, PRUint32 *aEnd)
|
|
|
|
{
|
|
|
|
if (*aStart >= *aEnd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CompressedGlyph *charGlyphs = mCharacterGlyphs;
|
|
|
|
|
2007-11-08 22:27:23 -08:00
|
|
|
while (*aStart < *aEnd && !charGlyphs[*aStart].IsLigatureGroupStart()) {
|
2007-08-09 18:35:32 -07:00
|
|
|
++(*aStart);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-08-09 18:35:32 -07:00
|
|
|
if (*aEnd < mCharacterCount) {
|
2007-11-08 22:27:23 -08:00
|
|
|
while (*aEnd > *aStart && !charGlyphs[*aEnd].IsLigatureGroupStart()) {
|
2007-08-09 18:35:32 -07:00
|
|
|
--(*aEnd);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxTextRun::DrawGlyphs(gfxFont *aFont, gfxContext *aContext,
|
|
|
|
PRBool aDrawToPath, gfxPoint *aPt,
|
|
|
|
PRUint32 aStart, PRUint32 aEnd,
|
2007-08-09 18:35:32 -07:00
|
|
|
PropertyProvider *aProvider,
|
|
|
|
PRUint32 aSpacingStart, PRUint32 aSpacingEnd)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoTArray<PropertyProvider::Spacing,200> spacingBuffer;
|
2007-08-09 18:35:32 -07:00
|
|
|
PRBool haveSpacing = GetAdjustedSpacingArray(aStart, aEnd, aProvider,
|
|
|
|
aSpacingStart, aSpacingEnd, &spacingBuffer);
|
2007-03-22 10:30:00 -07:00
|
|
|
aFont->Draw(this, aStart, aEnd, aContext, aDrawToPath, aPt,
|
|
|
|
haveSpacing ? spacingBuffer.Elements() : nsnull);
|
|
|
|
}
|
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
static void
|
|
|
|
ClipPartialLigature(gfxTextRun *aTextRun, gfxFloat *aLeft, gfxFloat *aRight,
|
|
|
|
gfxFloat aXOrigin, gfxTextRun::LigatureData *aLigature)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-04-22 19:06:22 -07:00
|
|
|
if (aLigature->mClipBeforePart) {
|
2007-08-09 18:35:32 -07:00
|
|
|
if (aTextRun->IsRightToLeft()) {
|
|
|
|
*aRight = PR_MIN(*aRight, aXOrigin);
|
|
|
|
} else {
|
|
|
|
*aLeft = PR_MAX(*aLeft, aXOrigin);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2008-04-22 19:06:22 -07:00
|
|
|
if (aLigature->mClipAfterPart) {
|
2007-08-09 18:35:32 -07:00
|
|
|
gfxFloat endEdge = aXOrigin + aTextRun->GetDirection()*aLigature->mPartWidth;
|
|
|
|
if (aTextRun->IsRightToLeft()) {
|
|
|
|
*aLeft = PR_MAX(*aLeft, endEdge);
|
|
|
|
} else {
|
|
|
|
*aRight = PR_MIN(*aRight, endEdge);
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-09 18:35:32 -07:00
|
|
|
gfxTextRun::DrawPartialLigature(gfxFont *aFont, gfxContext *aCtx, PRUint32 aStart,
|
|
|
|
PRUint32 aEnd,
|
2007-03-22 10:30:00 -07:00
|
|
|
const gfxRect *aDirtyRect, gfxPoint *aPt,
|
|
|
|
PropertyProvider *aProvider)
|
|
|
|
{
|
2007-08-09 18:35:32 -07:00
|
|
|
if (aStart >= aEnd)
|
|
|
|
return;
|
|
|
|
if (!aDirtyRect) {
|
|
|
|
NS_ERROR("Cannot draw partial ligatures without a dirty rect");
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
2007-08-09 18:35:32 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Draw partial ligature. We hack this by clipping the ligature.
|
2007-08-09 18:35:32 -07:00
|
|
|
LigatureData data = ComputeLigatureData(aStart, aEnd, aProvider);
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxFloat left = aDirtyRect->X();
|
|
|
|
gfxFloat right = aDirtyRect->XMost();
|
2007-08-09 18:35:32 -07:00
|
|
|
ClipPartialLigature(this, &left, &right, aPt->x, &data);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
aCtx->Save();
|
2007-05-29 02:47:54 -07:00
|
|
|
aCtx->NewPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
// use division here to ensure that when the rect is aligned on multiples
|
2007-05-29 02:47:54 -07:00
|
|
|
// of mAppUnitsPerDevUnit, we clip to true device unit boundaries.
|
|
|
|
// Also, make sure we snap the rectangle to device pixels.
|
|
|
|
aCtx->Rectangle(gfxRect(left/mAppUnitsPerDevUnit,
|
|
|
|
aDirtyRect->Y()/mAppUnitsPerDevUnit,
|
|
|
|
(right - left)/mAppUnitsPerDevUnit,
|
|
|
|
aDirtyRect->Height()/mAppUnitsPerDevUnit), PR_TRUE);
|
|
|
|
aCtx->Clip();
|
2007-08-09 18:35:32 -07:00
|
|
|
gfxFloat direction = GetDirection();
|
|
|
|
gfxPoint pt(aPt->x - direction*data.mPartAdvance, aPt->y);
|
|
|
|
DrawGlyphs(aFont, aCtx, PR_FALSE, &pt, data.mLigatureStart,
|
|
|
|
data.mLigatureEnd, aProvider, aStart, aEnd);
|
2007-03-22 10:30:00 -07:00
|
|
|
aCtx->Restore();
|
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
aPt->x += direction*data.mPartWidth;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-19 17:02:21 -07:00
|
|
|
// returns true if a glyph run is using a font with synthetic bolding enabled, false otherwise
|
|
|
|
static PRBool
|
|
|
|
HasSyntheticBold(gfxTextRun *aRun, PRUint32 aStart, PRUint32 aLength)
|
|
|
|
{
|
|
|
|
gfxTextRun::GlyphRunIterator iter(aRun, aStart, aLength);
|
|
|
|
while (iter.NextRun()) {
|
|
|
|
gfxFont *font = iter.GetGlyphRun()->mFont;
|
|
|
|
if (font && font->IsSyntheticBold()) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if color is non-opaque (i.e. alpha != 1.0) or completely transparent, false otherwise
|
|
|
|
// if true, color is set on output
|
|
|
|
static PRBool
|
|
|
|
HasNonOpaqueColor(gfxContext *aContext, gfxRGBA& aCurrentColor)
|
|
|
|
{
|
2008-04-28 14:27:05 -07:00
|
|
|
if (aContext->GetDeviceColor(aCurrentColor)) {
|
2008-03-19 17:02:21 -07:00
|
|
|
if (aCurrentColor.a < 1.0 && aCurrentColor.a > 0.0) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper class for double-buffering drawing with non-opaque color
|
|
|
|
struct BufferAlphaColor {
|
|
|
|
BufferAlphaColor(gfxContext *aContext)
|
|
|
|
: mContext(aContext)
|
|
|
|
{
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2008-03-19 17:02:21 -07:00
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2008-03-19 17:02:21 -07:00
|
|
|
~BufferAlphaColor() {}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2008-03-19 17:02:21 -07:00
|
|
|
void PushSolidColor(const gfxRect& aBounds, const gfxRGBA& aAlphaColor, PRUint32 appsPerDevUnit)
|
|
|
|
{
|
|
|
|
mContext->Save();
|
|
|
|
mContext->NewPath();
|
|
|
|
mContext->Rectangle(gfxRect(aBounds.X() / appsPerDevUnit,
|
|
|
|
aBounds.Y() / appsPerDevUnit,
|
|
|
|
aBounds.Width() / appsPerDevUnit,
|
|
|
|
aBounds.Height() / appsPerDevUnit), PR_TRUE);
|
|
|
|
mContext->Clip();
|
|
|
|
mContext->SetColor(gfxRGBA(aAlphaColor.r, aAlphaColor.g, aAlphaColor.b));
|
|
|
|
mContext->PushGroup(gfxASurface::CONTENT_COLOR_ALPHA);
|
|
|
|
mAlpha = aAlphaColor.a;
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2008-03-19 17:02:21 -07:00
|
|
|
void PopAlpha()
|
|
|
|
{
|
|
|
|
// pop the text, using the color alpha as the opacity
|
|
|
|
mContext->PopGroupToSource();
|
|
|
|
mContext->SetOperator(gfxContext::OPERATOR_OVER);
|
|
|
|
mContext->Paint(mAlpha);
|
|
|
|
mContext->Restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxContext *mContext;
|
|
|
|
gfxFloat mAlpha;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxTextRun::AdjustAdvancesForSyntheticBold(PRUint32 aStart, PRUint32 aLength)
|
|
|
|
{
|
|
|
|
const PRUint32 appUnitsPerDevUnit = GetAppUnitsPerDevUnit();
|
|
|
|
PRBool isRTL = IsRightToLeft();
|
|
|
|
|
|
|
|
GlyphRunIterator iter(this, aStart, aLength);
|
|
|
|
while (iter.NextRun()) {
|
|
|
|
gfxFont *font = iter.GetGlyphRun()->mFont;
|
|
|
|
if (font->IsSyntheticBold()) {
|
|
|
|
PRUint32 synAppUnitOffset = font->GetSyntheticBoldOffset() * appUnitsPerDevUnit;
|
|
|
|
PRUint32 start = iter.GetStringStart();
|
|
|
|
PRUint32 end = iter.GetStringEnd();
|
|
|
|
PRUint32 i;
|
|
|
|
|
|
|
|
// iterate over glyphs, start to end
|
|
|
|
for (i = start; i < end; ++i) {
|
|
|
|
gfxTextRun::CompressedGlyph *glyphData = &mCharacterGlyphs[i];
|
|
|
|
|
|
|
|
if (glyphData->IsSimpleGlyph()) {
|
|
|
|
// simple glyphs ==> just add the advance
|
|
|
|
PRUint32 advance = glyphData->GetSimpleAdvance() + synAppUnitOffset;
|
|
|
|
if (CompressedGlyph::IsSimpleAdvance(advance)) {
|
|
|
|
glyphData->SetSimpleGlyph(advance, glyphData->GetSimpleGlyph());
|
|
|
|
} else {
|
|
|
|
// rare case, tested by making this the default
|
|
|
|
PRUint32 glyphIndex = glyphData->GetSimpleGlyph();
|
|
|
|
glyphData->SetComplex(PR_TRUE, PR_TRUE, 1);
|
|
|
|
DetailedGlyph detail = {glyphIndex, advance, 0, 0};
|
|
|
|
SetGlyphs(i, *glyphData, &detail);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// complex glyphs ==> add offset at cluster/ligature boundaries
|
|
|
|
PRUint32 detailedLength = glyphData->GetGlyphCount();
|
|
|
|
if (detailedLength && mDetailedGlyphs) {
|
|
|
|
gfxTextRun::DetailedGlyph *details = mDetailedGlyphs[i].get();
|
|
|
|
if (!details) continue;
|
|
|
|
if (isRTL)
|
|
|
|
details[0].mAdvance += synAppUnitOffset;
|
|
|
|
else
|
|
|
|
details[detailedLength - 1].mAdvance += synAppUnitOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
gfxTextRun::Draw(gfxContext *aContext, gfxPoint aPt,
|
|
|
|
PRUint32 aStart, PRUint32 aLength, const gfxRect *aDirtyRect,
|
|
|
|
PropertyProvider *aProvider, gfxFloat *aAdvanceWidth)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart + aLength <= mCharacterCount, "Substring out of range");
|
|
|
|
|
|
|
|
gfxFloat direction = GetDirection();
|
|
|
|
gfxPoint pt = aPt;
|
|
|
|
|
2008-03-19 17:02:21 -07:00
|
|
|
// synthetic bolding draws glyphs twice ==> colors with opacity won't draw correctly unless first drawn without alpha
|
|
|
|
BufferAlphaColor syntheticBoldBuffer(aContext);
|
|
|
|
gfxRGBA currentColor;
|
|
|
|
PRBool needToRestore = PR_FALSE;
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2008-03-19 17:02:21 -07:00
|
|
|
if (HasNonOpaqueColor(aContext, currentColor) && HasSyntheticBold(this, aStart, aLength)) {
|
|
|
|
needToRestore = PR_TRUE;
|
|
|
|
// measure text, use the bounding box
|
2009-02-24 00:32:58 -08:00
|
|
|
gfxTextRun::Metrics metrics = MeasureText(aStart, aLength, gfxFont::LOOSE_INK_EXTENTS,
|
|
|
|
aContext, aProvider);
|
2008-03-19 17:02:21 -07:00
|
|
|
metrics.mBoundingBox.MoveBy(aPt);
|
|
|
|
syntheticBoldBuffer.PushSolidColor(metrics.mBoundingBox, currentColor, GetAppUnitsPerDevUnit());
|
|
|
|
}
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
GlyphRunIterator iter(this, aStart, aLength);
|
|
|
|
while (iter.NextRun()) {
|
|
|
|
gfxFont *font = iter.GetGlyphRun()->mFont;
|
|
|
|
PRUint32 start = iter.GetStringStart();
|
|
|
|
PRUint32 end = iter.GetStringEnd();
|
|
|
|
PRUint32 ligatureRunStart = start;
|
|
|
|
PRUint32 ligatureRunEnd = end;
|
|
|
|
ShrinkToLigatureBoundaries(&ligatureRunStart, &ligatureRunEnd);
|
2008-03-19 17:02:21 -07:00
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
DrawPartialLigature(font, aContext, start, ligatureRunStart, aDirtyRect, &pt, aProvider);
|
2007-03-22 10:30:00 -07:00
|
|
|
DrawGlyphs(font, aContext, PR_FALSE, &pt, ligatureRunStart,
|
2007-08-09 18:35:32 -07:00
|
|
|
ligatureRunEnd, aProvider, ligatureRunStart, ligatureRunEnd);
|
|
|
|
DrawPartialLigature(font, aContext, ligatureRunEnd, end, aDirtyRect, &pt, aProvider);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-19 17:02:21 -07:00
|
|
|
// composite result when synthetic bolding used
|
|
|
|
if (needToRestore) {
|
|
|
|
syntheticBoldBuffer.PopAlpha();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aAdvanceWidth) {
|
|
|
|
*aAdvanceWidth = (pt.x - aPt.x)*direction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxTextRun::DrawToPath(gfxContext *aContext, gfxPoint aPt,
|
|
|
|
PRUint32 aStart, PRUint32 aLength,
|
|
|
|
PropertyProvider *aProvider, gfxFloat *aAdvanceWidth)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart + aLength <= mCharacterCount, "Substring out of range");
|
|
|
|
|
|
|
|
gfxFloat direction = GetDirection();
|
|
|
|
gfxPoint pt = aPt;
|
|
|
|
|
|
|
|
GlyphRunIterator iter(this, aStart, aLength);
|
|
|
|
while (iter.NextRun()) {
|
|
|
|
gfxFont *font = iter.GetGlyphRun()->mFont;
|
|
|
|
PRUint32 start = iter.GetStringStart();
|
|
|
|
PRUint32 end = iter.GetStringEnd();
|
2007-08-09 18:35:32 -07:00
|
|
|
PRUint32 ligatureRunStart = start;
|
|
|
|
PRUint32 ligatureRunEnd = end;
|
|
|
|
ShrinkToLigatureBoundaries(&ligatureRunStart, &ligatureRunEnd);
|
|
|
|
NS_ASSERTION(ligatureRunStart == start,
|
2007-03-22 10:30:00 -07:00
|
|
|
"Can't draw path starting inside ligature");
|
2007-08-09 18:35:32 -07:00
|
|
|
NS_ASSERTION(ligatureRunEnd == end,
|
2007-03-22 10:30:00 -07:00
|
|
|
"Can't end drawing path inside ligature");
|
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
DrawGlyphs(font, aContext, PR_TRUE, &pt, ligatureRunStart, ligatureRunEnd, aProvider,
|
|
|
|
ligatureRunStart, ligatureRunEnd);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aAdvanceWidth) {
|
|
|
|
*aAdvanceWidth = (pt.x - aPt.x)*direction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-09 18:35:32 -07:00
|
|
|
gfxTextRun::AccumulateMetricsForRun(gfxFont *aFont,
|
|
|
|
PRUint32 aStart, PRUint32 aEnd,
|
2009-02-24 00:32:58 -08:00
|
|
|
gfxFont::BoundingBoxType aBoundingBoxType,
|
|
|
|
gfxContext *aRefContext,
|
2007-09-23 19:19:14 -07:00
|
|
|
PropertyProvider *aProvider,
|
2007-08-09 18:35:32 -07:00
|
|
|
PRUint32 aSpacingStart, PRUint32 aSpacingEnd,
|
2007-03-22 10:30:00 -07:00
|
|
|
Metrics *aMetrics)
|
|
|
|
{
|
|
|
|
nsAutoTArray<PropertyProvider::Spacing,200> spacingBuffer;
|
2007-08-09 18:35:32 -07:00
|
|
|
PRBool haveSpacing = GetAdjustedSpacingArray(aStart, aEnd, aProvider,
|
|
|
|
aSpacingStart, aSpacingEnd, &spacingBuffer);
|
2009-02-24 00:32:58 -08:00
|
|
|
Metrics metrics = aFont->Measure(this, aStart, aEnd, aBoundingBoxType, aRefContext,
|
2007-03-22 10:30:00 -07:00
|
|
|
haveSpacing ? spacingBuffer.Elements() : nsnull);
|
2008-08-12 02:34:52 -07:00
|
|
|
aMetrics->CombineWith(metrics, IsRightToLeft());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxTextRun::AccumulatePartialLigatureMetrics(gfxFont *aFont,
|
2009-02-24 00:32:58 -08:00
|
|
|
PRUint32 aStart, PRUint32 aEnd,
|
|
|
|
gfxFont::BoundingBoxType aBoundingBoxType, gfxContext *aRefContext,
|
2007-08-09 18:35:32 -07:00
|
|
|
PropertyProvider *aProvider, Metrics *aMetrics)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-09 18:35:32 -07:00
|
|
|
if (aStart >= aEnd)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Measure partial ligature. We hack this by clipping the metrics in the
|
|
|
|
// same way we clip the drawing.
|
2007-08-09 18:35:32 -07:00
|
|
|
LigatureData data = ComputeLigatureData(aStart, aEnd, aProvider);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// First measure the complete ligature
|
|
|
|
Metrics metrics;
|
2007-08-09 18:35:32 -07:00
|
|
|
AccumulateMetricsForRun(aFont, data.mLigatureStart, data.mLigatureEnd,
|
2009-02-24 00:32:58 -08:00
|
|
|
aBoundingBoxType, aRefContext,
|
|
|
|
aProvider, aStart, aEnd, &metrics);
|
2008-08-05 21:34:06 -07:00
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
// Clip the bounding box to the ligature part
|
|
|
|
gfxFloat bboxLeft = metrics.mBoundingBox.X();
|
|
|
|
gfxFloat bboxRight = metrics.mBoundingBox.XMost();
|
|
|
|
// Where we are going to start "drawing" relative to our left baseline origin
|
|
|
|
gfxFloat origin = IsRightToLeft() ? metrics.mAdvanceWidth - data.mPartAdvance : 0;
|
|
|
|
ClipPartialLigature(this, &bboxLeft, &bboxRight, origin, &data);
|
|
|
|
metrics.mBoundingBox.pos.x = bboxLeft;
|
|
|
|
metrics.mBoundingBox.size.width = bboxRight - bboxLeft;
|
|
|
|
|
|
|
|
// mBoundingBox is now relative to the left baseline origin for the entire
|
|
|
|
// ligature. Shift it left.
|
|
|
|
metrics.mBoundingBox.pos.x -=
|
|
|
|
IsRightToLeft() ? metrics.mAdvanceWidth - (data.mPartAdvance + data.mPartWidth)
|
|
|
|
: data.mPartAdvance;
|
|
|
|
metrics.mAdvanceWidth = data.mPartWidth;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-12 02:34:52 -07:00
|
|
|
aMetrics->CombineWith(metrics, IsRightToLeft());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun::Metrics
|
|
|
|
gfxTextRun::MeasureText(PRUint32 aStart, PRUint32 aLength,
|
2009-02-24 00:32:58 -08:00
|
|
|
gfxFont::BoundingBoxType aBoundingBoxType,
|
|
|
|
gfxContext *aRefContext,
|
2007-03-22 10:30:00 -07:00
|
|
|
PropertyProvider *aProvider)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart + aLength <= mCharacterCount, "Substring out of range");
|
|
|
|
|
|
|
|
Metrics accumulatedMetrics;
|
|
|
|
GlyphRunIterator iter(this, aStart, aLength);
|
|
|
|
while (iter.NextRun()) {
|
|
|
|
gfxFont *font = iter.GetGlyphRun()->mFont;
|
2007-08-09 18:35:32 -07:00
|
|
|
PRUint32 start = iter.GetStringStart();
|
|
|
|
PRUint32 end = iter.GetStringEnd();
|
|
|
|
PRUint32 ligatureRunStart = start;
|
|
|
|
PRUint32 ligatureRunEnd = end;
|
2007-03-22 10:30:00 -07:00
|
|
|
ShrinkToLigatureBoundaries(&ligatureRunStart, &ligatureRunEnd);
|
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
AccumulatePartialLigatureMetrics(font, start, ligatureRunStart,
|
2009-02-24 00:32:58 -08:00
|
|
|
aBoundingBoxType, aRefContext, aProvider, &accumulatedMetrics);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX This sucks. We have to get glyph extents just so we can detect
|
2009-02-24 00:32:58 -08:00
|
|
|
// glyphs outside the font box, even when aBoundingBoxType is LOOSE,
|
2007-03-22 10:30:00 -07:00
|
|
|
// even though in almost all cases we could get correct results just
|
|
|
|
// by getting some ascent/descent from the font and using our stored
|
|
|
|
// advance widths.
|
|
|
|
AccumulateMetricsForRun(font,
|
2009-02-24 00:32:58 -08:00
|
|
|
ligatureRunStart, ligatureRunEnd, aBoundingBoxType,
|
2007-09-23 19:19:14 -07:00
|
|
|
aRefContext, aProvider, ligatureRunStart, ligatureRunEnd,
|
2007-03-22 10:30:00 -07:00
|
|
|
&accumulatedMetrics);
|
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
AccumulatePartialLigatureMetrics(font, ligatureRunEnd, end,
|
2009-02-24 00:32:58 -08:00
|
|
|
aBoundingBoxType, aRefContext, aProvider, &accumulatedMetrics);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return accumulatedMetrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MEASUREMENT_BUFFER_SIZE 100
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
gfxTextRun::BreakAndMeasureText(PRUint32 aStart, PRUint32 aMaxLength,
|
|
|
|
PRBool aLineBreakBefore, gfxFloat aWidth,
|
|
|
|
PropertyProvider *aProvider,
|
|
|
|
PRBool aSuppressInitialBreak,
|
2007-05-22 16:40:07 -07:00
|
|
|
gfxFloat *aTrimWhitespace,
|
2009-02-24 00:32:58 -08:00
|
|
|
Metrics *aMetrics,
|
|
|
|
gfxFont::BoundingBoxType aBoundingBoxType,
|
2007-09-23 19:19:14 -07:00
|
|
|
gfxContext *aRefContext,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool *aUsedHyphenation,
|
2008-07-24 00:16:18 -07:00
|
|
|
PRUint32 *aLastBreak,
|
|
|
|
PRBool aCanWordWrap,
|
|
|
|
gfxBreakPriority *aBreakPriority)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aMaxLength = PR_MIN(aMaxLength, mCharacterCount - aStart);
|
|
|
|
|
|
|
|
NS_ASSERTION(aStart + aMaxLength <= mCharacterCount, "Substring out of range");
|
|
|
|
|
|
|
|
PRUint32 bufferStart = aStart;
|
|
|
|
PRUint32 bufferLength = PR_MIN(aMaxLength, MEASUREMENT_BUFFER_SIZE);
|
|
|
|
PropertyProvider::Spacing spacingBuffer[MEASUREMENT_BUFFER_SIZE];
|
2007-05-11 22:53:29 -07:00
|
|
|
PRBool haveSpacing = aProvider && (mFlags & gfxTextRunFactory::TEXT_ENABLE_SPACING) != 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (haveSpacing) {
|
2007-11-08 22:27:23 -08:00
|
|
|
GetAdjustedSpacing(this, bufferStart, bufferStart + bufferLength, aProvider,
|
2007-03-22 10:30:00 -07:00
|
|
|
spacingBuffer);
|
|
|
|
}
|
|
|
|
PRPackedBool hyphenBuffer[MEASUREMENT_BUFFER_SIZE];
|
|
|
|
PRBool haveHyphenation = (mFlags & gfxTextRunFactory::TEXT_ENABLE_HYPHEN_BREAKS) != 0;
|
|
|
|
if (haveHyphenation) {
|
2007-04-03 19:26:02 -07:00
|
|
|
aProvider->GetHyphenationBreaks(bufferStart, bufferLength,
|
2007-03-22 10:30:00 -07:00
|
|
|
hyphenBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFloat width = 0;
|
|
|
|
gfxFloat advance = 0;
|
2007-05-30 22:04:24 -07:00
|
|
|
// The number of space characters that can be trimmed
|
2007-05-22 16:40:07 -07:00
|
|
|
PRUint32 trimmableChars = 0;
|
2007-05-30 22:04:24 -07:00
|
|
|
// The amount of space removed by ignoring trimmableChars
|
|
|
|
gfxFloat trimmableAdvance = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 lastBreak = -1;
|
2007-05-30 22:04:24 -07:00
|
|
|
PRInt32 lastBreakTrimmableChars = -1;
|
|
|
|
gfxFloat lastBreakTrimmableAdvance = -1;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool aborted = PR_FALSE;
|
|
|
|
PRUint32 end = aStart + aMaxLength;
|
|
|
|
PRBool lastBreakUsedHyphenation = PR_FALSE;
|
|
|
|
|
|
|
|
PRUint32 ligatureRunStart = aStart;
|
|
|
|
PRUint32 ligatureRunEnd = end;
|
|
|
|
ShrinkToLigatureBoundaries(&ligatureRunStart, &ligatureRunEnd);
|
|
|
|
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = aStart; i < end; ++i) {
|
|
|
|
if (i >= bufferStart + bufferLength) {
|
|
|
|
// Fetch more spacing and hyphenation data
|
|
|
|
bufferStart = i;
|
|
|
|
bufferLength = PR_MIN(aStart + aMaxLength, i + MEASUREMENT_BUFFER_SIZE) - i;
|
|
|
|
if (haveSpacing) {
|
2007-11-08 22:27:23 -08:00
|
|
|
GetAdjustedSpacing(this, bufferStart, bufferStart + bufferLength, aProvider,
|
2007-03-22 10:30:00 -07:00
|
|
|
spacingBuffer);
|
|
|
|
}
|
|
|
|
if (haveHyphenation) {
|
2007-04-03 19:26:02 -07:00
|
|
|
aProvider->GetHyphenationBreaks(bufferStart, bufferLength,
|
2007-03-22 10:30:00 -07:00
|
|
|
hyphenBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-27 09:43:07 -07:00
|
|
|
// There can't be a word-wrap break opportunity at the beginning of the
|
|
|
|
// line: if the width is too small for even one character to fit, it
|
|
|
|
// could be the first and last break opportunity on the line, and that
|
|
|
|
// would trigger an infinite loop.
|
|
|
|
if (!aSuppressInitialBreak || i > aStart) {
|
|
|
|
PRBool lineBreakHere = mCharacterGlyphs[i].CanBreakBefore();
|
|
|
|
PRBool hyphenation = haveHyphenation && hyphenBuffer[i - bufferStart];
|
|
|
|
PRBool wordWrapping = aCanWordWrap && *aBreakPriority <= eWordWrapBreak;
|
|
|
|
|
|
|
|
if (lineBreakHere || hyphenation || wordWrapping) {
|
|
|
|
gfxFloat hyphenatedAdvance = advance;
|
|
|
|
if (!lineBreakHere && !wordWrapping) {
|
|
|
|
hyphenatedAdvance += aProvider->GetHyphenWidth();
|
|
|
|
}
|
2007-05-22 16:40:07 -07:00
|
|
|
|
2008-07-27 09:43:07 -07:00
|
|
|
if (lastBreak < 0 || width + hyphenatedAdvance - trimmableAdvance <= aWidth) {
|
|
|
|
// We can break here.
|
|
|
|
lastBreak = i;
|
|
|
|
lastBreakTrimmableChars = trimmableChars;
|
|
|
|
lastBreakTrimmableAdvance = trimmableAdvance;
|
|
|
|
lastBreakUsedHyphenation = !lineBreakHere && !wordWrapping;
|
|
|
|
*aBreakPriority = hyphenation || lineBreakHere ?
|
|
|
|
eNormalBreak : eWordWrapBreak;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-27 09:43:07 -07:00
|
|
|
width += advance;
|
|
|
|
advance = 0;
|
|
|
|
if (width - trimmableAdvance > aWidth) {
|
|
|
|
// No more text fits. Abort
|
|
|
|
aborted = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-08 22:27:23 -08:00
|
|
|
gfxFloat charAdvance;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (i >= ligatureRunStart && i < ligatureRunEnd) {
|
2007-11-08 22:27:23 -08:00
|
|
|
charAdvance = GetAdvanceForGlyphs(this, i, i + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (haveSpacing) {
|
|
|
|
PropertyProvider::Spacing *space = &spacingBuffer[i - bufferStart];
|
2007-05-22 16:40:07 -07:00
|
|
|
charAdvance += space->mBefore + space->mAfter;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
2007-11-08 22:27:23 -08:00
|
|
|
charAdvance = ComputePartialLigatureWidth(i, i + 1, aProvider);
|
2007-05-22 16:40:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
advance += charAdvance;
|
|
|
|
if (aTrimWhitespace) {
|
|
|
|
if (GetChar(i) == ' ') {
|
|
|
|
++trimmableChars;
|
|
|
|
trimmableAdvance += charAdvance;
|
|
|
|
} else {
|
|
|
|
trimmableAdvance = 0;
|
|
|
|
trimmableChars = 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aborted) {
|
|
|
|
width += advance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are three possibilities:
|
|
|
|
// 1) all the text fit (width <= aWidth)
|
|
|
|
// 2) some of the text fit up to a break opportunity (width > aWidth && lastBreak >= 0)
|
|
|
|
// 3) none of the text fits before a break opportunity (width > aWidth && lastBreak < 0)
|
|
|
|
PRUint32 charsFit;
|
2007-07-01 18:12:45 -07:00
|
|
|
PRBool usedHyphenation = PR_FALSE;
|
2007-05-22 16:40:07 -07:00
|
|
|
if (width - trimmableAdvance <= aWidth) {
|
2007-03-22 10:30:00 -07:00
|
|
|
charsFit = aMaxLength;
|
|
|
|
} else if (lastBreak >= 0) {
|
|
|
|
charsFit = lastBreak - aStart;
|
2007-05-30 22:04:24 -07:00
|
|
|
trimmableChars = lastBreakTrimmableChars;
|
|
|
|
trimmableAdvance = lastBreakTrimmableAdvance;
|
2007-07-01 18:12:45 -07:00
|
|
|
usedHyphenation = lastBreakUsedHyphenation;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
charsFit = aMaxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMetrics) {
|
2007-09-23 19:19:14 -07:00
|
|
|
*aMetrics = MeasureText(aStart, charsFit - trimmableChars,
|
2009-02-24 00:32:58 -08:00
|
|
|
aBoundingBoxType, aRefContext, aProvider);
|
2007-05-22 16:40:07 -07:00
|
|
|
}
|
|
|
|
if (aTrimWhitespace) {
|
|
|
|
*aTrimWhitespace = trimmableAdvance;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (aUsedHyphenation) {
|
2007-07-01 18:12:45 -07:00
|
|
|
*aUsedHyphenation = usedHyphenation;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (aLastBreak && charsFit == aMaxLength) {
|
|
|
|
if (lastBreak < 0) {
|
|
|
|
*aLastBreak = PR_UINT32_MAX;
|
|
|
|
} else {
|
|
|
|
*aLastBreak = lastBreak - aStart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return charsFit;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFloat
|
|
|
|
gfxTextRun::GetAdvanceWidth(PRUint32 aStart, PRUint32 aLength,
|
|
|
|
PropertyProvider *aProvider)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart + aLength <= mCharacterCount, "Substring out of range");
|
|
|
|
|
2007-05-30 22:01:56 -07:00
|
|
|
PRUint32 ligatureRunStart = aStart;
|
|
|
|
PRUint32 ligatureRunEnd = aStart + aLength;
|
|
|
|
ShrinkToLigatureBoundaries(&ligatureRunStart, &ligatureRunEnd);
|
|
|
|
|
2007-08-09 18:35:32 -07:00
|
|
|
gfxFloat result = ComputePartialLigatureWidth(aStart, ligatureRunStart, aProvider) +
|
|
|
|
ComputePartialLigatureWidth(ligatureRunEnd, aStart + aLength, aProvider);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-30 22:01:56 -07:00
|
|
|
// Account for all remaining spacing here. This is more efficient than
|
|
|
|
// processing it along with the glyphs.
|
2007-05-11 22:53:29 -07:00
|
|
|
if (aProvider && (mFlags & gfxTextRunFactory::TEXT_ENABLE_SPACING)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 i;
|
|
|
|
nsAutoTArray<PropertyProvider::Spacing,200> spacingBuffer;
|
|
|
|
if (spacingBuffer.AppendElements(aLength)) {
|
2007-11-08 22:27:23 -08:00
|
|
|
GetAdjustedSpacing(this, ligatureRunStart, ligatureRunEnd, aProvider,
|
2007-03-22 10:30:00 -07:00
|
|
|
spacingBuffer.Elements());
|
2007-05-30 22:01:56 -07:00
|
|
|
for (i = 0; i < ligatureRunEnd - ligatureRunStart; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PropertyProvider::Spacing *space = &spacingBuffer[i];
|
|
|
|
result += space->mBefore + space->mAfter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-08 22:27:23 -08:00
|
|
|
return result + GetAdvanceForGlyphs(this, ligatureRunStart, ligatureRunEnd);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 16:07:18 -07:00
|
|
|
PRBool
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxTextRun::SetLineBreaks(PRUint32 aStart, PRUint32 aLength,
|
|
|
|
PRBool aLineBreakBefore, PRBool aLineBreakAfter,
|
2007-06-26 21:22:21 -07:00
|
|
|
gfxFloat *aAdvanceWidthDelta,
|
|
|
|
gfxContext *aRefContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Do nothing because our shaping does not currently take linebreaks into
|
|
|
|
// account. There is no change in advance width.
|
|
|
|
if (aAdvanceWidthDelta) {
|
|
|
|
*aAdvanceWidthDelta = 0;
|
|
|
|
}
|
2007-03-22 16:07:18 -07:00
|
|
|
return PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
gfxTextRun::FindFirstGlyphRunContaining(PRUint32 aOffset)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aOffset <= mCharacterCount, "Bad offset looking for glyphrun");
|
2010-08-11 09:52:21 -07:00
|
|
|
NS_ASSERTION(mCharacterCount == 0 || mGlyphRuns.Length() > 0,
|
|
|
|
"non-empty text but no glyph runs present!");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aOffset == mCharacterCount)
|
|
|
|
return mGlyphRuns.Length();
|
|
|
|
PRUint32 start = 0;
|
|
|
|
PRUint32 end = mGlyphRuns.Length();
|
|
|
|
while (end - start > 1) {
|
|
|
|
PRUint32 mid = (start + end)/2;
|
|
|
|
if (mGlyphRuns[mid].mCharacterOffset <= aOffset) {
|
|
|
|
start = mid;
|
|
|
|
} else {
|
|
|
|
end = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ASSERTION(mGlyphRuns[start].mCharacterOffset <= aOffset,
|
|
|
|
"Hmm, something went wrong, aOffset should have been found");
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-07-18 07:22:06 -07:00
|
|
|
gfxTextRun::AddGlyphRun(gfxFont *aFont, PRUint32 aUTF16Offset, PRBool aForceNewRun)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-06-12 13:56:04 -07:00
|
|
|
PRUint32 numGlyphRuns = mGlyphRuns.Length();
|
2007-07-18 07:22:06 -07:00
|
|
|
if (!aForceNewRun &&
|
|
|
|
numGlyphRuns > 0)
|
|
|
|
{
|
2007-06-12 13:56:04 -07:00
|
|
|
GlyphRun *lastGlyphRun = &mGlyphRuns[numGlyphRuns - 1];
|
|
|
|
|
|
|
|
NS_ASSERTION(lastGlyphRun->mCharacterOffset <= aUTF16Offset,
|
2007-07-18 07:22:06 -07:00
|
|
|
"Glyph runs out of order (and run not forced)");
|
2007-06-12 13:56:04 -07:00
|
|
|
|
|
|
|
if (lastGlyphRun->mFont == aFont)
|
|
|
|
return NS_OK;
|
|
|
|
if (lastGlyphRun->mCharacterOffset == aUTF16Offset) {
|
|
|
|
lastGlyphRun->mFont = aFont;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-18 07:22:06 -07:00
|
|
|
NS_ASSERTION(aForceNewRun || numGlyphRuns > 0 || aUTF16Offset == 0,
|
|
|
|
"First run doesn't cover the first character (and run not forced)?");
|
2007-06-12 13:56:04 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
GlyphRun *glyphRun = mGlyphRuns.AppendElement();
|
|
|
|
if (!glyphRun)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
glyphRun->mFont = aFont;
|
|
|
|
glyphRun->mCharacterOffset = aUTF16Offset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-18 07:22:06 -07:00
|
|
|
void
|
|
|
|
gfxTextRun::SortGlyphRuns()
|
|
|
|
{
|
2007-08-26 16:44:00 -07:00
|
|
|
if (mGlyphRuns.Length() <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsTArray<GlyphRun> runs(mGlyphRuns);
|
2007-07-18 07:22:06 -07:00
|
|
|
GlyphRunOffsetComparator comp;
|
2007-08-26 16:44:00 -07:00
|
|
|
runs.Sort(comp);
|
|
|
|
|
|
|
|
// Now copy back, coalescing adjacent glyph runs that have the same font
|
|
|
|
mGlyphRuns.Clear();
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < runs.Length(); ++i) {
|
|
|
|
// a GlyphRun with the same font as the previous GlyphRun can just
|
|
|
|
// be skipped; the last GlyphRun will cover its character range.
|
|
|
|
if (i == 0 || runs[i].mFont != runs[i - 1].mFont) {
|
|
|
|
mGlyphRuns.AppendElement(runs[i]);
|
2008-10-29 10:24:06 -07:00
|
|
|
// If two fonts have the same character offset, Sort() will have
|
|
|
|
// randomized the order.
|
|
|
|
NS_ASSERTION(i == 0 ||
|
|
|
|
runs[i].mCharacterOffset !=
|
|
|
|
runs[i - 1].mCharacterOffset,
|
|
|
|
"Two fonts for the same run, glyph indices may not match the font");
|
2007-08-26 16:44:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-07-18 07:22:06 -07:00
|
|
|
}
|
|
|
|
|
2009-03-29 17:31:51 -07:00
|
|
|
void
|
|
|
|
gfxTextRun::SanitizeGlyphRuns()
|
|
|
|
{
|
|
|
|
if (mGlyphRuns.Length() <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If any glyph run starts with ligature-continuation characters, we need to advance it
|
|
|
|
// to the first "real" character to avoid drawing partial ligature glyphs from wrong font
|
|
|
|
// (seen with U+FEFF in reftest 474417-1, as Core Text eliminates the glyph, which makes
|
|
|
|
// it appear as if a ligature has been formed)
|
2010-01-07 05:53:25 -08:00
|
|
|
PRInt32 i, lastRunIndex = mGlyphRuns.Length() - 1;
|
|
|
|
for (i = lastRunIndex; i >= 0; --i) {
|
2009-03-29 17:31:51 -07:00
|
|
|
GlyphRun& run = mGlyphRuns[i];
|
|
|
|
while (mCharacterGlyphs[run.mCharacterOffset].IsLigatureContinuation() &&
|
|
|
|
run.mCharacterOffset < mCharacterCount) {
|
|
|
|
run.mCharacterOffset++;
|
|
|
|
}
|
|
|
|
// if the run has become empty, eliminate it
|
2010-01-07 05:53:25 -08:00
|
|
|
if ((i < lastRunIndex &&
|
|
|
|
run.mCharacterOffset >= mGlyphRuns[i+1].mCharacterOffset) ||
|
|
|
|
(i == lastRunIndex && run.mCharacterOffset == mCharacterCount)) {
|
2009-03-29 17:31:51 -07:00
|
|
|
mGlyphRuns.RemoveElementAt(i);
|
2010-01-07 05:53:25 -08:00
|
|
|
--lastRunIndex;
|
2009-03-29 17:31:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32
|
|
|
|
gfxTextRun::CountMissingGlyphs()
|
|
|
|
{
|
|
|
|
PRUint32 i;
|
|
|
|
PRUint32 count = 0;
|
|
|
|
for (i = 0; i < mCharacterCount; ++i) {
|
|
|
|
if (mCharacterGlyphs[i].IsMissing()) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-03-26 20:24:49 -07:00
|
|
|
gfxTextRun::DetailedGlyph *
|
|
|
|
gfxTextRun::AllocateDetailedGlyphs(PRUint32 aIndex, PRUint32 aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-09 15:33:34 -08:00
|
|
|
NS_ASSERTION(aIndex < mCharacterCount, "Index out of range");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mCharacterGlyphs)
|
2007-03-26 20:24:49 -07:00
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mDetailedGlyphs) {
|
|
|
|
mDetailedGlyphs = new nsAutoArrayPtr<DetailedGlyph>[mCharacterCount];
|
|
|
|
if (!mDetailedGlyphs) {
|
2007-11-08 22:27:23 -08:00
|
|
|
mCharacterGlyphs[aIndex].SetMissing(0);
|
2007-03-26 20:24:49 -07:00
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DetailedGlyph *details = new DetailedGlyph[aCount];
|
|
|
|
if (!details) {
|
2007-11-08 22:27:23 -08:00
|
|
|
mCharacterGlyphs[aIndex].SetMissing(0);
|
2007-03-26 20:24:49 -07:00
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
mDetailedGlyphs[aIndex] = details;
|
2007-03-26 20:24:49 -07:00
|
|
|
return details;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-08 22:27:23 -08:00
|
|
|
gfxTextRun::SetGlyphs(PRUint32 aIndex, CompressedGlyph aGlyph,
|
|
|
|
const DetailedGlyph *aGlyphs)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!aGlyph.IsSimpleGlyph(), "Simple glyphs not handled here");
|
|
|
|
NS_ASSERTION(aIndex > 0 ||
|
|
|
|
(aGlyph.IsClusterStart() && aGlyph.IsLigatureGroupStart()),
|
|
|
|
"First character must be the start of a cluster and can't be a ligature continuation!");
|
|
|
|
|
|
|
|
PRUint32 glyphCount = aGlyph.GetGlyphCount();
|
|
|
|
if (glyphCount > 0) {
|
|
|
|
DetailedGlyph *details = AllocateDetailedGlyphs(aIndex, glyphCount);
|
|
|
|
if (!details)
|
|
|
|
return;
|
|
|
|
memcpy(details, aGlyphs, sizeof(DetailedGlyph)*glyphCount);
|
|
|
|
}
|
|
|
|
mCharacterGlyphs[aIndex] = aGlyph;
|
2007-11-07 23:41:06 -08:00
|
|
|
}
|
2007-11-08 22:27:23 -08:00
|
|
|
|
2010-04-28 14:51:16 -07:00
|
|
|
#include "ignorable.x-ccmap"
|
|
|
|
DEFINE_X_CCMAP(gIgnorableCCMapExt, const);
|
|
|
|
|
|
|
|
static inline PRBool
|
|
|
|
IsDefaultIgnorable(PRUint32 aChar)
|
|
|
|
{
|
|
|
|
return CCMAP_HAS_CHAR_EXT(gIgnorableCCMapExt, aChar);
|
|
|
|
}
|
|
|
|
|
2007-03-26 20:24:49 -07:00
|
|
|
void
|
2007-09-20 03:16:15 -07:00
|
|
|
gfxTextRun::SetMissingGlyph(PRUint32 aIndex, PRUint32 aChar)
|
2007-03-26 20:24:49 -07:00
|
|
|
{
|
|
|
|
DetailedGlyph *details = AllocateDetailedGlyphs(aIndex, 1);
|
|
|
|
if (!details)
|
|
|
|
return;
|
|
|
|
|
|
|
|
details->mGlyphID = aChar;
|
|
|
|
GlyphRun *glyphRun = &mGlyphRuns[FindFirstGlyphRunContaining(aIndex)];
|
2010-04-28 14:51:16 -07:00
|
|
|
if (IsDefaultIgnorable(aChar)) {
|
|
|
|
// Setting advance width to zero will prevent drawing the hexbox
|
|
|
|
details->mAdvance = 0;
|
|
|
|
} else {
|
|
|
|
gfxFloat width = PR_MAX(glyphRun->mFont->GetMetrics().aveCharWidth,
|
|
|
|
gfxFontMissingGlyphs::GetDesiredMinWidth(aChar));
|
|
|
|
details->mAdvance = PRUint32(width*GetAppUnitsPerDevUnit());
|
|
|
|
}
|
2007-03-26 20:24:49 -07:00
|
|
|
details->mXOffset = 0;
|
|
|
|
details->mYOffset = 0;
|
2007-11-08 22:27:23 -08:00
|
|
|
mCharacterGlyphs[aIndex].SetMissing(1);
|
2007-03-26 20:24:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
PRBool
|
|
|
|
gfxTextRun::FilterIfIgnorable(PRUint32 aIndex)
|
|
|
|
{
|
|
|
|
PRUint32 ch = GetChar(aIndex);
|
|
|
|
if (IsDefaultIgnorable(ch)) {
|
|
|
|
DetailedGlyph *details = AllocateDetailedGlyphs(aIndex, 1);
|
|
|
|
if (details) {
|
|
|
|
details->mGlyphID = ch;
|
|
|
|
details->mAdvance = 0;
|
|
|
|
details->mXOffset = 0;
|
|
|
|
details->mYOffset = 0;
|
|
|
|
mCharacterGlyphs[aIndex].SetMissing(1);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2007-06-12 13:56:04 -07:00
|
|
|
static void
|
|
|
|
ClearCharacters(gfxTextRun::CompressedGlyph *aGlyphs, PRUint32 aLength)
|
|
|
|
{
|
2007-11-08 22:27:23 -08:00
|
|
|
memset(aGlyphs, 0, sizeof(gfxTextRun::CompressedGlyph)*aLength);
|
2007-06-12 13:56:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxTextRun::CopyGlyphDataFrom(gfxTextRun *aSource, PRUint32 aStart,
|
|
|
|
PRUint32 aLength, PRUint32 aDest,
|
|
|
|
PRBool aStealData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart + aLength <= aSource->GetLength(),
|
|
|
|
"Source substring out of range");
|
|
|
|
NS_ASSERTION(aDest + aLength <= GetLength(),
|
|
|
|
"Destination substring out of range");
|
|
|
|
|
|
|
|
PRUint32 i;
|
2007-11-08 22:27:23 -08:00
|
|
|
// Copy base character data
|
2007-06-12 13:56:04 -07:00
|
|
|
for (i = 0; i < aLength; ++i) {
|
|
|
|
CompressedGlyph g = aSource->mCharacterGlyphs[i + aStart];
|
2007-07-16 15:50:35 -07:00
|
|
|
g.SetCanBreakBefore(mCharacterGlyphs[i + aDest].CanBreakBefore());
|
2007-06-12 13:56:04 -07:00
|
|
|
mCharacterGlyphs[i + aDest] = g;
|
|
|
|
if (aStealData) {
|
2007-11-08 22:27:23 -08:00
|
|
|
aSource->mCharacterGlyphs[i + aStart].SetMissing(0);
|
2007-06-12 13:56:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-08 22:27:23 -08:00
|
|
|
// Copy detailed glyphs
|
2007-06-12 13:56:04 -07:00
|
|
|
if (aSource->mDetailedGlyphs) {
|
|
|
|
for (i = 0; i < aLength; ++i) {
|
|
|
|
DetailedGlyph *details = aSource->mDetailedGlyphs[i + aStart];
|
|
|
|
if (details) {
|
|
|
|
if (aStealData) {
|
|
|
|
if (!mDetailedGlyphs) {
|
|
|
|
mDetailedGlyphs = new nsAutoArrayPtr<DetailedGlyph>[mCharacterCount];
|
|
|
|
if (!mDetailedGlyphs) {
|
|
|
|
ClearCharacters(&mCharacterGlyphs[aDest], aLength);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mDetailedGlyphs[i + aDest] = details;
|
|
|
|
aSource->mDetailedGlyphs[i + aStart].forget();
|
|
|
|
} else {
|
2007-11-08 22:27:23 -08:00
|
|
|
PRUint32 glyphCount = mCharacterGlyphs[i + aDest].GetGlyphCount();
|
2007-06-12 13:56:04 -07:00
|
|
|
DetailedGlyph *dest = AllocateDetailedGlyphs(i + aDest, glyphCount);
|
|
|
|
if (!dest) {
|
|
|
|
ClearCharacters(&mCharacterGlyphs[aDest], aLength);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(dest, details, sizeof(DetailedGlyph)*glyphCount);
|
|
|
|
}
|
|
|
|
} else if (mDetailedGlyphs) {
|
|
|
|
mDetailedGlyphs[i + aDest] = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (mDetailedGlyphs) {
|
|
|
|
for (i = 0; i < aLength; ++i) {
|
|
|
|
mDetailedGlyphs[i + aDest] = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-08 22:27:23 -08:00
|
|
|
// Copy glyph runs
|
2007-06-12 13:56:04 -07:00
|
|
|
GlyphRunIterator iter(aSource, aStart, aLength);
|
2007-08-26 16:44:00 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
gfxFont *lastFont = nsnull;
|
|
|
|
#endif
|
2007-06-12 13:56:04 -07:00
|
|
|
while (iter.NextRun()) {
|
|
|
|
gfxFont *font = iter.GetGlyphRun()->mFont;
|
2007-08-26 16:44:00 -07:00
|
|
|
NS_ASSERTION(font != lastFont, "Glyphruns not coalesced?");
|
|
|
|
#ifdef DEBUG
|
|
|
|
lastFont = font;
|
2008-09-15 06:46:07 -07:00
|
|
|
PRUint32 end = iter.GetStringEnd();
|
2007-08-26 16:44:00 -07:00
|
|
|
#endif
|
2007-06-12 13:56:04 -07:00
|
|
|
PRUint32 start = iter.GetStringStart();
|
2010-01-30 05:16:07 -08:00
|
|
|
|
|
|
|
// These used to be NS_ASSERTION()s, but WARNING is more appropriate.
|
|
|
|
// Although it's unusual (and not desirable), it's possible for us to assign
|
2009-03-29 17:31:51 -07:00
|
|
|
// different fonts to a base character and a following diacritic.
|
2010-01-30 05:16:07 -08:00
|
|
|
// Example on OSX 10.5/10.6 with default fonts installed:
|
|
|
|
// data:text/html,<p style="font-family:helvetica, arial, sans-serif;">
|
|
|
|
// о҆ о҆
|
|
|
|
// This means the rendering of the cluster will probably not be very good,
|
|
|
|
// but it's the best we can do for now if the specified font only covered the
|
|
|
|
// initial base character and not its applied marks.
|
|
|
|
NS_WARN_IF_FALSE(aSource->IsClusterStart(start),
|
|
|
|
"Started font run in the middle of a cluster");
|
|
|
|
NS_WARN_IF_FALSE(end == aSource->GetLength() || aSource->IsClusterStart(end),
|
|
|
|
"Ended font run in the middle of a cluster");
|
2007-06-12 13:56:04 -07:00
|
|
|
|
|
|
|
nsresult rv = AddGlyphRun(font, start - aStart + aDest);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxTextRun::SetSpaceGlyph(gfxFont *aFont, gfxContext *aContext, PRUint32 aCharIndex)
|
|
|
|
{
|
|
|
|
PRUint32 spaceGlyph = aFont->GetSpaceGlyph();
|
|
|
|
float spaceWidth = aFont->GetMetrics().spaceWidth;
|
|
|
|
PRUint32 spaceWidthAppUnits = NS_lroundf(spaceWidth*mAppUnitsPerDevUnit);
|
|
|
|
if (!spaceGlyph ||
|
|
|
|
!CompressedGlyph::IsSimpleGlyphID(spaceGlyph) ||
|
|
|
|
!CompressedGlyph::IsSimpleAdvance(spaceWidthAppUnits)) {
|
|
|
|
gfxTextRunFactory::Parameters params = {
|
|
|
|
aContext, nsnull, nsnull, nsnull, 0, mAppUnitsPerDevUnit
|
|
|
|
};
|
|
|
|
static const PRUint8 space = ' ';
|
|
|
|
nsAutoPtr<gfxTextRun> textRun;
|
|
|
|
textRun = mFontGroup->MakeTextRun(&space, 1, ¶ms,
|
|
|
|
gfxTextRunFactory::TEXT_IS_8BIT | gfxTextRunFactory::TEXT_IS_ASCII |
|
|
|
|
gfxTextRunFactory::TEXT_IS_PERSISTENT);
|
|
|
|
if (!textRun || !textRun->mCharacterGlyphs)
|
|
|
|
return;
|
|
|
|
CopyGlyphDataFrom(textRun, 0, 1, aCharIndex, PR_TRUE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddGlyphRun(aFont, aCharIndex);
|
|
|
|
CompressedGlyph g;
|
|
|
|
g.SetSimpleGlyph(spaceWidthAppUnits, spaceGlyph);
|
2007-11-08 22:27:23 -08:00
|
|
|
SetSimpleGlyph(aCharIndex, g);
|
2007-06-12 13:56:04 -07:00
|
|
|
}
|
2007-09-23 19:19:14 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
gfxTextRun::FetchGlyphExtents(gfxContext *aRefContext)
|
|
|
|
{
|
2009-06-24 02:04:20 -07:00
|
|
|
PRBool needsGlyphExtents = NeedsGlyphExtents(this);
|
|
|
|
if (!needsGlyphExtents && !mDetailedGlyphs)
|
2007-09-23 19:19:14 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
PRUint32 i;
|
|
|
|
CompressedGlyph *charGlyphs = mCharacterGlyphs;
|
|
|
|
for (i = 0; i < mGlyphRuns.Length(); ++i) {
|
|
|
|
gfxFont *font = mGlyphRuns[i].mFont;
|
|
|
|
PRUint32 start = mGlyphRuns[i].mCharacterOffset;
|
|
|
|
PRUint32 end = i + 1 < mGlyphRuns.Length()
|
|
|
|
? mGlyphRuns[i + 1].mCharacterOffset : GetLength();
|
|
|
|
PRBool fontIsSetup = PR_FALSE;
|
|
|
|
PRUint32 j;
|
|
|
|
gfxGlyphExtents *extents = font->GetOrCreateGlyphExtents(mAppUnitsPerDevUnit);
|
|
|
|
|
|
|
|
for (j = start; j < end; ++j) {
|
|
|
|
const gfxTextRun::CompressedGlyph *glyphData = &charGlyphs[j];
|
|
|
|
if (glyphData->IsSimpleGlyph()) {
|
|
|
|
// If we're in speed mode, don't set up glyph extents here; we'll
|
|
|
|
// just return "optimistic" glyph bounds later
|
2009-06-24 02:04:20 -07:00
|
|
|
if (needsGlyphExtents) {
|
2007-09-23 19:19:14 -07:00
|
|
|
PRUint32 glyphIndex = glyphData->GetSimpleGlyph();
|
|
|
|
if (!extents->IsGlyphKnown(glyphIndex)) {
|
|
|
|
if (!fontIsSetup) {
|
|
|
|
font->SetupCairoFont(aRefContext);
|
2007-11-08 22:27:23 -08:00
|
|
|
fontIsSetup = PR_TRUE;
|
2007-09-23 19:19:14 -07:00
|
|
|
}
|
2007-10-01 18:47:40 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
|
|
|
++gGlyphExtentsSetupEagerSimple;
|
|
|
|
#endif
|
2007-09-23 19:19:14 -07:00
|
|
|
font->SetupGlyphExtents(aRefContext, glyphIndex, PR_FALSE, extents);
|
|
|
|
}
|
|
|
|
}
|
2007-12-20 23:09:50 -08:00
|
|
|
} else if (!glyphData->IsMissing()) {
|
2007-11-08 22:27:23 -08:00
|
|
|
PRUint32 k;
|
|
|
|
PRUint32 glyphCount = glyphData->GetGlyphCount();
|
2007-11-07 23:41:06 -08:00
|
|
|
const gfxTextRun::DetailedGlyph *details = GetDetailedGlyphs(j);
|
2007-11-08 22:27:23 -08:00
|
|
|
for (k = 0; k < glyphCount; ++k, ++details) {
|
2007-09-23 19:19:14 -07:00
|
|
|
PRUint32 glyphIndex = details->mGlyphID;
|
|
|
|
if (!extents->IsGlyphKnownWithTightExtents(glyphIndex)) {
|
|
|
|
if (!fontIsSetup) {
|
|
|
|
font->SetupCairoFont(aRefContext);
|
|
|
|
fontIsSetup = PR_TRUE;
|
|
|
|
}
|
2007-10-01 18:47:40 -07:00
|
|
|
#ifdef DEBUG_TEXT_RUN_STORAGE_METRICS
|
|
|
|
++gGlyphExtentsSetupEagerTight;
|
|
|
|
#endif
|
2007-09-23 19:19:14 -07:00
|
|
|
font->SetupGlyphExtents(aRefContext, glyphIndex, PR_TRUE, extents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-20 17:23:50 -08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
gfxTextRun::Dump(FILE* aOutput) {
|
|
|
|
if (!aOutput) {
|
|
|
|
aOutput = stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 i;
|
|
|
|
fputc('"', aOutput);
|
|
|
|
for (i = 0; i < mCharacterCount; ++i) {
|
|
|
|
PRUnichar ch = GetChar(i);
|
|
|
|
if (ch >= 32 && ch < 128) {
|
|
|
|
fputc(ch, aOutput);
|
|
|
|
} else {
|
|
|
|
fprintf(aOutput, "\\u%4x", ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fputs("\" [", aOutput);
|
|
|
|
for (i = 0; i < mGlyphRuns.Length(); ++i) {
|
|
|
|
if (i > 0) {
|
|
|
|
fputc(',', aOutput);
|
|
|
|
}
|
|
|
|
gfxFont* font = mGlyphRuns[i].mFont;
|
|
|
|
const gfxFontStyle* style = font->GetStyle();
|
|
|
|
NS_ConvertUTF16toUTF8 fontName(font->GetName());
|
2010-02-24 09:57:57 -08:00
|
|
|
nsCAutoString lang;
|
|
|
|
style->language->ToUTF8String(lang);
|
2008-01-20 17:23:50 -08:00
|
|
|
fprintf(aOutput, "%d: %s %f/%d/%d/%s", mGlyphRuns[i].mCharacterOffset,
|
|
|
|
fontName.get(), style->size,
|
2010-02-24 09:57:57 -08:00
|
|
|
style->weight, style->style, lang.get());
|
2008-01-20 17:23:50 -08:00
|
|
|
}
|
|
|
|
fputc(']', aOutput);
|
|
|
|
}
|
|
|
|
#endif
|