2008-08-06 13:48:55 -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.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
2008-08-28 04:05:53 -07:00
|
|
|
#if defined(MOZ_WIDGET_GTK2)
|
2008-08-06 13:48:55 -07:00
|
|
|
#include "gfxPlatformGtk.h"
|
2008-08-28 04:05:53 -07:00
|
|
|
#define gfxToolkitPlatform gfxPlatformGtk
|
|
|
|
#elif defined(MOZ_WIDGET_QT)
|
|
|
|
#include <qfontinfo.h>
|
2010-03-04 13:51:42 -08:00
|
|
|
#include "gfxQtPlatform.h"
|
2008-08-28 04:05:53 -07:00
|
|
|
#define gfxToolkitPlatform gfxQtPlatform
|
2009-01-22 22:24:29 -08:00
|
|
|
#elif defined(XP_WIN)
|
|
|
|
#ifdef WINCE
|
|
|
|
#define SHGetSpecialFolderPathW SHGetSpecialFolderPath
|
|
|
|
#endif
|
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
#define gfxToolkitPlatform gfxWindowsPlatform
|
2009-10-07 07:13:40 -07:00
|
|
|
#include "gfxFT2FontList.h"
|
2010-05-11 10:27:36 -07:00
|
|
|
#elif defined(ANDROID)
|
|
|
|
#include "gfxAndroidPlatform.h"
|
|
|
|
#define gfxToolkitPlatform gfxAndroidPlatform
|
2008-08-28 04:05:53 -07:00
|
|
|
#endif
|
2010-05-11 10:27:36 -07:00
|
|
|
|
2008-08-06 13:48:55 -07:00
|
|
|
#include "gfxTypes.h"
|
|
|
|
#include "gfxFT2Fonts.h"
|
2009-10-30 16:13:41 -07:00
|
|
|
#include "gfxFT2FontBase.h"
|
|
|
|
#include "gfxFT2Utils.h"
|
2008-08-06 13:48:55 -07:00
|
|
|
#include <locale.h>
|
|
|
|
#include "cairo-ft.h"
|
2009-09-09 08:35:08 -07:00
|
|
|
#include FT_TRUETYPE_TAGS_H
|
|
|
|
#include FT_TRUETYPE_TABLES_H
|
2008-08-06 13:48:55 -07:00
|
|
|
#include "gfxFontUtils.h"
|
2010-12-09 03:58:16 -08:00
|
|
|
#include "gfxHarfBuzzShaper.h"
|
|
|
|
#include "gfxUnicodeProperties.h"
|
2010-02-24 09:57:57 -08:00
|
|
|
#include "gfxAtoms.h"
|
2009-01-18 12:14:14 -08:00
|
|
|
#include "nsTArray.h"
|
2009-09-09 08:35:08 -07:00
|
|
|
#include "nsUnicodeRange.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefLocalizedString.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prinit.h"
|
|
|
|
static PRLogModuleInfo *gFontLog = PR_NewLogModule("ft2fonts");
|
|
|
|
|
|
|
|
static const char *sCJKLangGroup[] = {
|
|
|
|
"ja",
|
|
|
|
"ko",
|
2010-02-24 09:57:57 -08:00
|
|
|
"zh-cn",
|
|
|
|
"zh-hk",
|
|
|
|
"zh-tw"
|
2009-09-09 08:35:08 -07:00
|
|
|
};
|
|
|
|
#define COUNT_OF_CJK_LANG_GROUP 5
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-10 13:52:40 -07:00
|
|
|
// rounding and truncation functions for a Freetype floating point number
|
|
|
|
// (FT26Dot6) stored in a 32bit integer with high 26 bits for the integer
|
|
|
|
// part and low 6 bits for the fractional part.
|
|
|
|
#define MOZ_FT_ROUND(x) (((x) + 32) & ~63) // 63 = 2^6 - 1
|
|
|
|
#define MOZ_FT_TRUNC(x) ((x) >> 6)
|
|
|
|
#define CONVERT_DESIGN_UNITS_TO_PIXELS(v, s) \
|
|
|
|
MOZ_FT_TRUNC(MOZ_FT_ROUND(FT_MulFix((v) , (s))))
|
|
|
|
|
2008-08-06 13:48:55 -07:00
|
|
|
/**
|
|
|
|
* FontEntry
|
|
|
|
*/
|
|
|
|
|
|
|
|
FontEntry::~FontEntry()
|
|
|
|
{
|
2009-09-09 08:35:08 -07:00
|
|
|
// Do nothing for mFTFace here since FTFontDestroyFunc is called by cairo.
|
|
|
|
mFTFace = nsnull;
|
|
|
|
|
2010-05-19 13:11:26 -07:00
|
|
|
#ifndef ANDROID
|
2008-08-06 13:48:55 -07:00
|
|
|
if (mFontFace) {
|
|
|
|
cairo_font_face_destroy(mFontFace);
|
|
|
|
mFontFace = nsnull;
|
|
|
|
}
|
2010-05-19 13:11:26 -07:00
|
|
|
#endif
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
2010-07-15 23:03:45 -07:00
|
|
|
gfxFont*
|
|
|
|
FontEntry::CreateFontInstance(const gfxFontStyle *aFontStyle, PRBool aNeedsBold) {
|
|
|
|
already_AddRefed<gfxFT2Font> font = gfxFT2Font::GetOrMakeFont(this, aFontStyle);
|
|
|
|
return font.get();
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:24:29 -08:00
|
|
|
/* static */
|
2009-09-10 13:52:40 -07:00
|
|
|
FontEntry*
|
|
|
|
FontEntry::CreateFontEntry(const gfxProxyFontEntry &aProxyEntry,
|
2009-08-30 06:55:24 -07:00
|
|
|
const PRUint8 *aFontData,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
// Ownership of aFontData is passed in here; the fontEntry must
|
|
|
|
// retain it as long as the FT_Face needs it, and ensure it is
|
|
|
|
// eventually deleted.
|
2009-01-22 22:24:29 -08:00
|
|
|
FT_Face face;
|
|
|
|
FT_Error error =
|
2009-09-10 13:52:40 -07:00
|
|
|
FT_New_Memory_Face(gfxToolkitPlatform::GetPlatform()->GetFTLibrary(),
|
2009-01-22 22:24:29 -08:00
|
|
|
aFontData, aLength, 0, &face);
|
2009-08-30 06:55:24 -07:00
|
|
|
if (error != FT_Err_Ok) {
|
|
|
|
NS_Free((void*)aFontData);
|
2009-01-22 22:24:29 -08:00
|
|
|
return nsnull;
|
2009-08-30 06:55:24 -07:00
|
|
|
}
|
|
|
|
FontEntry* fe = FontEntry::CreateFontEntryFromFace(face, aFontData);
|
2010-09-24 18:15:02 -07:00
|
|
|
if (fe) {
|
|
|
|
fe->mItalic = aProxyEntry.mItalic;
|
|
|
|
fe->mWeight = aProxyEntry.mWeight;
|
|
|
|
fe->mStretch = aProxyEntry.mStretch;
|
|
|
|
}
|
2009-01-22 22:24:29 -08:00
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
2009-08-30 06:55:24 -07:00
|
|
|
class FTUserFontData {
|
|
|
|
public:
|
|
|
|
FTUserFontData(FT_Face aFace, const PRUint8* aData)
|
|
|
|
: mFace(aFace), mFontData(aData)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~FTUserFontData()
|
|
|
|
{
|
|
|
|
FT_Done_Face(mFace);
|
|
|
|
if (mFontData) {
|
|
|
|
NS_Free((void*)mFontData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FT_Face mFace;
|
|
|
|
const PRUint8 *mFontData;
|
|
|
|
};
|
|
|
|
|
2008-08-06 13:48:55 -07:00
|
|
|
static void
|
|
|
|
FTFontDestroyFunc(void *data)
|
|
|
|
{
|
2009-08-30 06:55:24 -07:00
|
|
|
FTUserFontData *userFontData = static_cast<FTUserFontData*>(data);
|
|
|
|
delete userFontData;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:52:40 -07:00
|
|
|
/* static */ FontEntry*
|
2009-08-30 06:55:24 -07:00
|
|
|
FontEntry::CreateFontEntryFromFace(FT_Face aFace, const PRUint8 *aFontData) {
|
2009-01-22 22:24:29 -08:00
|
|
|
static cairo_user_data_key_t key;
|
|
|
|
|
|
|
|
if (!aFace->family_name) {
|
|
|
|
FT_Done_Face(aFace);
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
// Construct font name from family name and style name, regular fonts
|
|
|
|
// do not have the modifier by convention.
|
|
|
|
NS_ConvertUTF8toUTF16 fontName(aFace->family_name);
|
|
|
|
if (aFace->style_name && strcmp("Regular", aFace->style_name)) {
|
|
|
|
fontName.AppendLiteral(" ");
|
|
|
|
AppendUTF8toUTF16(aFace->style_name, fontName);
|
|
|
|
}
|
|
|
|
FontEntry *fe = new FontEntry(fontName);
|
|
|
|
fe->mItalic = aFace->style_flags & FT_STYLE_FLAG_ITALIC;
|
2009-09-09 08:35:08 -07:00
|
|
|
fe->mFTFace = aFace;
|
2010-10-21 12:02:02 -07:00
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
|
|
fe->mFontFace = cairo_ft_font_face_create_for_ft_face(aFace, FT_LOAD_NO_AUTOHINT | FT_LOAD_NO_HINTING);
|
|
|
|
#else
|
2009-01-22 22:24:29 -08:00
|
|
|
fe->mFontFace = cairo_ft_font_face_create_for_ft_face(aFace, 0);
|
2010-10-21 12:02:02 -07:00
|
|
|
#endif
|
2009-08-30 06:55:24 -07:00
|
|
|
FTUserFontData *userFontData = new FTUserFontData(aFace, aFontData);
|
2009-01-22 22:24:29 -08:00
|
|
|
cairo_font_face_set_user_data(fe->mFontFace, &key,
|
2009-08-30 06:55:24 -07:00
|
|
|
userFontData, FTFontDestroyFunc);
|
2009-07-30 11:19:34 -07:00
|
|
|
|
2009-08-30 06:55:24 -07:00
|
|
|
TT_OS2 *os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(aFace, ft_sfnt_os2));
|
2009-07-30 11:19:34 -07:00
|
|
|
PRUint16 os2weight = 0;
|
|
|
|
if (os2 && os2->version != 0xffff) {
|
|
|
|
// Technically, only 100 to 900 are valid, but some fonts
|
|
|
|
// have this set wrong -- e.g. "Microsoft Logo Bold Italic" has
|
|
|
|
// it set to 6 instead of 600. We try to be nice and handle that
|
|
|
|
// as well.
|
|
|
|
if (os2->usWeightClass >= 100 && os2->usWeightClass <= 900)
|
|
|
|
os2weight = os2->usWeightClass;
|
|
|
|
else if (os2->usWeightClass >= 1 && os2->usWeightClass <= 9)
|
|
|
|
os2weight = os2->usWeightClass * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (os2weight != 0)
|
|
|
|
fe->mWeight = os2weight;
|
2009-01-22 22:24:29 -08:00
|
|
|
else if (aFace->style_flags & FT_STYLE_FLAG_BOLD)
|
|
|
|
fe->mWeight = 700;
|
|
|
|
else
|
|
|
|
fe->mWeight = 400;
|
2009-07-30 11:19:34 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(fe->mWeight >= 100 && fe->mWeight <= 900, "Invalid final weight in font!");
|
|
|
|
|
2009-01-22 22:24:29 -08:00
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
2008-08-28 04:05:53 -07:00
|
|
|
FontEntry*
|
|
|
|
gfxFT2Font::GetFontEntry()
|
|
|
|
{
|
|
|
|
return static_cast<FontEntry*> (mFontEntry.get());
|
|
|
|
}
|
|
|
|
|
2008-08-06 13:48:55 -07:00
|
|
|
cairo_font_face_t *
|
|
|
|
FontEntry::CairoFontFace()
|
|
|
|
{
|
|
|
|
static cairo_user_data_key_t key;
|
|
|
|
|
|
|
|
if (!mFontFace) {
|
|
|
|
FT_Face face;
|
2008-08-28 04:05:53 -07:00
|
|
|
FT_New_Face(gfxToolkitPlatform::GetPlatform()->GetFTLibrary(), mFilename.get(), mFTFontIndex, &face);
|
2009-09-09 08:35:08 -07:00
|
|
|
mFTFace = face;
|
2010-04-22 15:03:02 -07:00
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
|
|
mFontFace = cairo_ft_font_face_create_for_ft_face(face, FT_LOAD_NO_AUTOHINT | FT_LOAD_NO_HINTING);
|
|
|
|
#else
|
2008-08-06 13:48:55 -07:00
|
|
|
mFontFace = cairo_ft_font_face_create_for_ft_face(face, 0);
|
2010-04-22 15:03:02 -07:00
|
|
|
#endif
|
2009-08-30 06:55:24 -07:00
|
|
|
FTUserFontData *userFontData = new FTUserFontData(face, nsnull);
|
|
|
|
cairo_font_face_set_user_data(mFontFace, &key,
|
|
|
|
userFontData, FTFontDestroyFunc);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
return mFontFace;
|
|
|
|
}
|
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
nsresult
|
|
|
|
FontEntry::ReadCMAP()
|
|
|
|
{
|
2009-10-07 10:16:52 -07:00
|
|
|
if (mCmapInitialized) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-09-09 08:35:08 -07:00
|
|
|
|
|
|
|
// attempt this once, if errors occur leave a blank cmap
|
|
|
|
mCmapInitialized = PR_TRUE;
|
|
|
|
|
|
|
|
// Ensure existence of mFTFace
|
|
|
|
CairoFontFace();
|
|
|
|
NS_ENSURE_TRUE(mFTFace, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
FT_Error status;
|
|
|
|
FT_ULong len = 0;
|
|
|
|
status = FT_Load_Sfnt_Table(mFTFace, TTAG_cmap, 0, nsnull, &len);
|
|
|
|
NS_ENSURE_TRUE(status == 0, NS_ERROR_FAILURE);
|
|
|
|
NS_ENSURE_TRUE(len != 0, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsAutoTArray<PRUint8,16384> buffer;
|
2009-10-07 10:16:52 -07:00
|
|
|
if (!buffer.AppendElements(len)) {
|
2009-09-09 08:35:08 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
2009-09-09 08:35:08 -07:00
|
|
|
PRUint8 *buf = buffer.Elements();
|
|
|
|
|
|
|
|
status = FT_Load_Sfnt_Table(mFTFace, TTAG_cmap, 0, buf, &len);
|
|
|
|
NS_ENSURE_TRUE(status == 0, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
PRPackedBool unicodeFont;
|
|
|
|
PRPackedBool symbolFont;
|
2009-10-07 10:16:52 -07:00
|
|
|
nsresult rv = gfxFontUtils::ReadCMAP(buf, len, mCharacterMap, mUVSOffset,
|
|
|
|
unicodeFont, symbolFont);
|
|
|
|
mHasCmapTable = NS_SUCCEEDED(rv);
|
2010-06-13 23:53:53 -07:00
|
|
|
return rv;
|
2009-09-09 08:35:08 -07:00
|
|
|
}
|
|
|
|
|
2008-08-06 13:48:55 -07:00
|
|
|
FontEntry *
|
|
|
|
FontFamily::FindFontEntry(const gfxFontStyle& aFontStyle)
|
2009-01-22 22:24:29 -08:00
|
|
|
{
|
|
|
|
PRBool needsBold = PR_FALSE;
|
|
|
|
return static_cast<FontEntry*>(FindFontForStyle(aFontStyle, needsBold));
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
2009-08-16 06:52:12 -07:00
|
|
|
FontFamily::FindWeightsForStyle(gfxFontEntry* aFontsForWeights[],
|
|
|
|
PRBool anItalic, PRInt16 aStretch)
|
2008-08-06 13:48:55 -07:00
|
|
|
{
|
2009-01-22 22:24:29 -08:00
|
|
|
PRBool matchesSomething = PR_FALSE;
|
2008-08-06 13:48:55 -07:00
|
|
|
|
|
|
|
for (PRUint32 j = 0; j < 2; j++) {
|
|
|
|
// build up an array of weights that match the italicness we're looking for
|
2009-08-16 06:52:12 -07:00
|
|
|
for (PRUint32 i = 0; i < mAvailableFonts.Length(); i++) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
2008-08-06 13:48:55 -07:00
|
|
|
const PRUint8 weight = (fe->mWeight / 100);
|
2009-08-16 06:52:12 -07:00
|
|
|
if (fe->mItalic == anItalic) {
|
2009-01-22 22:24:29 -08:00
|
|
|
aFontsForWeights[weight] = fe;
|
2008-08-06 13:48:55 -07:00
|
|
|
matchesSomething = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (matchesSomething)
|
|
|
|
break;
|
2009-08-16 06:52:12 -07:00
|
|
|
anItalic = !anItalic;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
2009-01-22 22:24:29 -08:00
|
|
|
return matchesSomething;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gfxFT2FontGroup
|
|
|
|
*/
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFT2FontGroup::FontCallback(const nsAString& fontName,
|
|
|
|
const nsACString& genericName,
|
|
|
|
void *closure)
|
|
|
|
{
|
2009-01-18 12:14:14 -08:00
|
|
|
nsTArray<nsString> *sa = static_cast<nsTArray<nsString>*>(closure);
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-01-18 12:14:14 -08:00
|
|
|
if (!fontName.IsEmpty() && !sa->Contains(fontName)) {
|
|
|
|
sa->AppendElement(fontName);
|
2008-08-06 13:48:55 -07:00
|
|
|
#ifdef DEBUG_pavlov
|
|
|
|
printf(" - %s\n", NS_ConvertUTF16toUTF8(fontName).get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFT2FontGroup::gfxFT2FontGroup(const nsAString& families,
|
2010-07-15 23:03:45 -07:00
|
|
|
const gfxFontStyle *aStyle,
|
|
|
|
gfxUserFontSet *aUserFontSet)
|
|
|
|
: gfxFontGroup(families, aStyle, aUserFontSet)
|
2008-08-06 13:48:55 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_pavlov
|
|
|
|
printf("Looking for %s\n", NS_ConvertUTF16toUTF8(families).get());
|
|
|
|
#endif
|
2009-01-18 12:14:14 -08:00
|
|
|
nsTArray<nsString> familyArray;
|
2008-08-06 13:48:55 -07:00
|
|
|
ForEachFont(FontCallback, &familyArray);
|
|
|
|
|
2009-01-18 12:14:14 -08:00
|
|
|
if (familyArray.Length() == 0) {
|
2008-08-06 13:48:55 -07:00
|
|
|
nsAutoString prefFamilies;
|
2010-02-24 09:57:57 -08:00
|
|
|
gfxToolkitPlatform::GetPlatform()->GetPrefFonts(aStyle->language, prefFamilies, nsnull);
|
2008-08-06 13:48:55 -07:00
|
|
|
if (!prefFamilies.IsEmpty()) {
|
2010-02-24 09:57:44 -08:00
|
|
|
ForEachFont(prefFamilies, aStyle->language, FontCallback, &familyArray);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
}
|
2009-01-18 12:14:14 -08:00
|
|
|
if (familyArray.Length() == 0) {
|
2009-01-22 22:24:29 -08:00
|
|
|
#if defined(MOZ_WIDGET_QT) /* FIXME DFB */
|
2008-08-06 13:48:55 -07:00
|
|
|
printf("failde to find a font. sadface\n");
|
|
|
|
// We want to get rid of this entirely at some point, but first we need real lists of fonts.
|
|
|
|
QFont defaultFont;
|
|
|
|
QFontInfo fi (defaultFont);
|
2009-01-18 12:14:14 -08:00
|
|
|
familyArray.AppendElement(nsDependentString(static_cast<const PRUnichar *>(fi.family().utf16())));
|
2009-01-22 22:24:29 -08:00
|
|
|
#elif defined(MOZ_WIDGET_GTK2)
|
|
|
|
FcResult result;
|
|
|
|
FcChar8 *family = nsnull;
|
|
|
|
FcPattern* pat = FcPatternCreate();
|
|
|
|
FcPattern *match = FcFontMatch(nsnull, pat, &result);
|
|
|
|
if (match)
|
|
|
|
FcPatternGetString(match, FC_FAMILY, 0, &family);
|
|
|
|
if (family)
|
2009-01-22 23:59:24 -08:00
|
|
|
familyArray.AppendElement(NS_ConvertUTF8toUTF16((char*)family));
|
2009-01-22 22:24:29 -08:00
|
|
|
#elif defined(XP_WIN)
|
|
|
|
HGDIOBJ hGDI = ::GetStockObject(SYSTEM_FONT);
|
|
|
|
LOGFONTW logFont;
|
2009-09-10 13:52:40 -07:00
|
|
|
if (hGDI && ::GetObjectW(hGDI, sizeof(logFont), &logFont))
|
2009-01-22 22:24:29 -08:00
|
|
|
familyArray.AppendElement(nsDependentString(logFont.lfFaceName));
|
2010-05-11 10:27:36 -07:00
|
|
|
#elif defined(ANDROID)
|
|
|
|
familyArray.AppendElement(NS_LITERAL_STRING("Droid Sans"));
|
2009-01-22 22:24:29 -08:00
|
|
|
#else
|
|
|
|
#error "Platform not supported"
|
2008-08-06 13:48:55 -07:00
|
|
|
#endif
|
2009-01-22 22:24:29 -08:00
|
|
|
}
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-01-18 12:14:14 -08:00
|
|
|
for (PRUint32 i = 0; i < familyArray.Length(); i++) {
|
2009-01-22 22:24:29 -08:00
|
|
|
nsRefPtr<gfxFT2Font> font = gfxFT2Font::GetOrMakeFont(familyArray[i], &mStyle);
|
2008-08-06 13:48:55 -07:00
|
|
|
if (font) {
|
|
|
|
mFonts.AppendElement(font);
|
|
|
|
}
|
|
|
|
}
|
2009-01-22 22:24:29 -08:00
|
|
|
NS_ASSERTION(mFonts.Length() > 0, "We need at least one font in a fontgroup");
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFT2FontGroup::~gfxFT2FontGroup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontGroup *
|
|
|
|
gfxFT2FontGroup::Copy(const gfxFontStyle *aStyle)
|
|
|
|
{
|
2010-07-15 23:03:45 -07:00
|
|
|
return new gfxFT2FontGroup(mFamilies, aStyle, nsnull);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to return the leading UTF-8 character in a char pointer
|
|
|
|
// as 32bit number. Also sets the length of the current character (i.e. the
|
|
|
|
// offset to the next one) in the second argument
|
|
|
|
PRUint32 getUTF8CharAndNext(const PRUint8 *aString, PRUint8 *aLength)
|
|
|
|
{
|
|
|
|
*aLength = 1;
|
|
|
|
if (aString[0] < 0x80) { // normal 7bit ASCII char
|
|
|
|
return aString[0];
|
|
|
|
}
|
|
|
|
if ((aString[0] >> 5) == 6) { // two leading ones -> two bytes
|
|
|
|
*aLength = 2;
|
|
|
|
return ((aString[0] & 0x1F) << 6) + (aString[1] & 0x3F);
|
|
|
|
}
|
|
|
|
if ((aString[0] >> 4) == 14) { // three leading ones -> three bytes
|
|
|
|
*aLength = 3;
|
|
|
|
return ((aString[0] & 0x0F) << 12) + ((aString[1] & 0x3F) << 6) +
|
|
|
|
(aString[2] & 0x3F);
|
|
|
|
}
|
|
|
|
if ((aString[0] >> 4) == 15) { // four leading ones -> four bytes
|
|
|
|
*aLength = 4;
|
|
|
|
return ((aString[0] & 0x07) << 18) + ((aString[1] & 0x3F) << 12) +
|
|
|
|
((aString[2] & 0x3F) << 6) + (aString[3] & 0x3F);
|
|
|
|
}
|
|
|
|
return aString[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
static PRBool
|
|
|
|
AddFontNameToArray(const nsAString& aName,
|
|
|
|
const nsACString& aGenericName,
|
|
|
|
void *aClosure)
|
2008-08-06 13:48:55 -07:00
|
|
|
{
|
2009-09-09 08:35:08 -07:00
|
|
|
if (!aName.IsEmpty()) {
|
|
|
|
nsTArray<nsString> *list = static_cast<nsTArray<nsString> *>(aClosure);
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
if (list->IndexOf(aName) == list->NoIndex)
|
|
|
|
list->AppendElement(aName);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
2009-09-10 13:52:40 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
return PR_TRUE;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
2009-09-10 13:52:40 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
void
|
|
|
|
gfxFT2FontGroup::FamilyListToArrayList(const nsString& aFamilies,
|
2010-02-24 09:57:57 -08:00
|
|
|
nsIAtom *aLangGroup,
|
2009-10-07 07:13:40 -07:00
|
|
|
nsTArray<nsRefPtr<gfxFontEntry> > *aFontEntryList)
|
2008-08-06 13:48:55 -07:00
|
|
|
{
|
2009-09-09 08:35:08 -07:00
|
|
|
nsAutoTArray<nsString, 15> fonts;
|
|
|
|
ForEachFont(aFamilies, aLangGroup, AddFontNameToArray, &fonts);
|
|
|
|
|
|
|
|
PRUint32 len = fonts.Length();
|
|
|
|
for (PRUint32 i = 0; i < len; ++i) {
|
|
|
|
const nsString& str = fonts[i];
|
2009-10-07 07:13:40 -07:00
|
|
|
nsRefPtr<gfxFontEntry> fe = (gfxToolkitPlatform::GetPlatform()->FindFontEntry(str, mStyle));
|
2009-09-09 08:35:08 -07:00
|
|
|
aFontEntryList->AppendElement(fe);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-24 09:57:57 -08:00
|
|
|
void gfxFT2FontGroup::GetPrefFonts(nsIAtom *aLangGroup, nsTArray<nsRefPtr<gfxFontEntry> >& aFontEntryList)
|
|
|
|
{
|
2009-09-09 08:35:08 -07:00
|
|
|
NS_ASSERTION(aLangGroup, "aLangGroup is null");
|
|
|
|
gfxToolkitPlatform *platform = gfxToolkitPlatform::GetPlatform();
|
2009-10-07 07:13:40 -07:00
|
|
|
nsAutoTArray<nsRefPtr<gfxFontEntry>, 5> fonts;
|
2010-02-24 09:57:57 -08:00
|
|
|
nsCAutoString key;
|
|
|
|
aLangGroup->ToUTF8String(key);
|
2009-09-09 08:35:08 -07:00
|
|
|
key.Append("-");
|
|
|
|
key.AppendInt(GetStyle()->style);
|
|
|
|
key.Append("-");
|
|
|
|
key.AppendInt(GetStyle()->weight);
|
|
|
|
if (!platform->GetPrefFontEntries(key, &fonts)) {
|
|
|
|
nsString fontString;
|
|
|
|
platform->GetPrefFonts(aLangGroup, fontString);
|
|
|
|
if (fontString.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2010-02-24 09:57:57 -08:00
|
|
|
FamilyListToArrayList(fontString, aLangGroup, &fonts);
|
2009-09-09 08:35:08 -07:00
|
|
|
|
|
|
|
platform->SetPrefFontEntries(key, fonts);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
2009-09-09 08:35:08 -07:00
|
|
|
aFontEntryList.AppendElements(fonts);
|
|
|
|
}
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
static PRInt32 GetCJKLangGroupIndex(const char *aLangGroup) {
|
|
|
|
PRInt32 i;
|
|
|
|
for (i = 0; i < COUNT_OF_CJK_LANG_GROUP; i++) {
|
|
|
|
if (!PL_strcasecmp(aLangGroup, sCJKLangGroup[i]))
|
|
|
|
return i;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
2009-09-09 08:35:08 -07:00
|
|
|
return -1;
|
|
|
|
}
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
// this function assigns to the array passed in.
|
2009-10-07 07:13:40 -07:00
|
|
|
void gfxFT2FontGroup::GetCJKPrefFonts(nsTArray<nsRefPtr<gfxFontEntry> >& aFontEntryList) {
|
2009-09-09 08:35:08 -07:00
|
|
|
gfxToolkitPlatform *platform = gfxToolkitPlatform::GetPlatform();
|
|
|
|
|
|
|
|
nsCAutoString key("x-internal-cjk-");
|
|
|
|
key.AppendInt(mStyle.style);
|
|
|
|
key.Append("-");
|
|
|
|
key.AppendInt(mStyle.weight);
|
|
|
|
|
|
|
|
if (!platform->GetPrefFontEntries(key, &aFontEntryList)) {
|
|
|
|
nsCOMPtr<nsIPrefService> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (!prefs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch;
|
|
|
|
prefs->GetBranch(0, getter_AddRefs(prefBranch));
|
|
|
|
if (!prefBranch)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add the CJK pref fonts from accept languages, the order should be same order
|
|
|
|
nsCAutoString list;
|
|
|
|
nsCOMPtr<nsIPrefLocalizedString> val;
|
|
|
|
nsresult rv = prefBranch->GetComplexValue("intl.accept_languages", NS_GET_IID(nsIPrefLocalizedString),
|
|
|
|
getter_AddRefs(val));
|
|
|
|
if (NS_SUCCEEDED(rv) && val) {
|
|
|
|
nsAutoString temp;
|
|
|
|
val->ToString(getter_Copies(temp));
|
|
|
|
LossyCopyUTF16toASCII(temp, list);
|
|
|
|
}
|
|
|
|
if (!list.IsEmpty()) {
|
|
|
|
const char kComma = ',';
|
|
|
|
const char *p, *p_end;
|
|
|
|
list.BeginReading(p);
|
|
|
|
list.EndReading(p_end);
|
|
|
|
while (p < p_end) {
|
|
|
|
while (nsCRT::IsAsciiSpace(*p)) {
|
|
|
|
if (++p == p_end)
|
|
|
|
break;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
2009-09-09 08:35:08 -07:00
|
|
|
if (p == p_end)
|
|
|
|
break;
|
|
|
|
const char *start = p;
|
|
|
|
while (++p != p_end && *p != kComma)
|
|
|
|
/* nothing */ ;
|
|
|
|
nsCAutoString lang(Substring(start, p));
|
|
|
|
lang.CompressWhitespace(PR_FALSE, PR_TRUE);
|
|
|
|
PRInt32 index = GetCJKLangGroupIndex(lang.get());
|
2010-02-24 09:57:57 -08:00
|
|
|
if (index >= 0) {
|
|
|
|
nsCOMPtr<nsIAtom> atom = do_GetAtom(sCJKLangGroup[index]);
|
|
|
|
GetPrefFonts(atom, aFontEntryList);
|
|
|
|
}
|
2009-09-09 08:35:08 -07:00
|
|
|
p++;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
// Add the system locale
|
|
|
|
#ifdef XP_WIN
|
|
|
|
switch (::GetACP()) {
|
2010-02-24 09:57:57 -08:00
|
|
|
case 932: GetPrefFonts(gfxAtoms::ja, aFontEntryList); break;
|
|
|
|
case 936: GetPrefFonts(gfxAtoms::zh_cn, aFontEntryList); break;
|
|
|
|
case 949: GetPrefFonts(gfxAtoms::ko, aFontEntryList); break;
|
|
|
|
// XXX Don't we need to append gfxAtoms::zh_hk if the codepage is 950?
|
|
|
|
case 950: GetPrefFonts(gfxAtoms::zh_tw, aFontEntryList); break;
|
2009-09-09 08:35:08 -07:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
const char *ctype = setlocale(LC_CTYPE, NULL);
|
|
|
|
if (ctype) {
|
|
|
|
if (!PL_strncasecmp(ctype, "ja", 2)) {
|
2010-02-24 09:57:57 -08:00
|
|
|
GetPrefFonts(gfxAtoms::ja, aFontEntryList);
|
2009-09-09 08:35:08 -07:00
|
|
|
} else if (!PL_strncasecmp(ctype, "zh_cn", 5)) {
|
2010-02-24 09:57:57 -08:00
|
|
|
GetPrefFonts(gfxAtoms::zh_cn, aFontEntryList);
|
2009-09-09 08:35:08 -07:00
|
|
|
} else if (!PL_strncasecmp(ctype, "zh_hk", 5)) {
|
2010-02-24 09:57:57 -08:00
|
|
|
GetPrefFonts(gfxAtoms::zh_hk, aFontEntryList);
|
2009-09-09 08:35:08 -07:00
|
|
|
} else if (!PL_strncasecmp(ctype, "zh_tw", 5)) {
|
2010-02-24 09:57:57 -08:00
|
|
|
GetPrefFonts(gfxAtoms::zh_tw, aFontEntryList);
|
2009-09-09 08:35:08 -07:00
|
|
|
} else if (!PL_strncasecmp(ctype, "ko", 2)) {
|
2010-02-24 09:57:57 -08:00
|
|
|
GetPrefFonts(gfxAtoms::ko, aFontEntryList);
|
2009-09-09 08:35:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// last resort...
|
2010-02-24 09:57:57 -08:00
|
|
|
GetPrefFonts(gfxAtoms::ja, aFontEntryList);
|
|
|
|
GetPrefFonts(gfxAtoms::ko, aFontEntryList);
|
|
|
|
GetPrefFonts(gfxAtoms::zh_cn, aFontEntryList);
|
|
|
|
GetPrefFonts(gfxAtoms::zh_hk, aFontEntryList);
|
|
|
|
GetPrefFonts(gfxAtoms::zh_tw, aFontEntryList);
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
platform->SetPrefFontEntries(key, aFontEntryList);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
2009-09-09 08:35:08 -07:00
|
|
|
}
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
already_AddRefed<gfxFT2Font>
|
2009-10-07 07:13:40 -07:00
|
|
|
gfxFT2FontGroup::WhichFontSupportsChar(const nsTArray<nsRefPtr<gfxFontEntry> >& aFontEntryList, PRUint32 aCh)
|
2009-09-09 08:35:08 -07:00
|
|
|
{
|
|
|
|
for (PRUint32 i = 0; i < aFontEntryList.Length(); i++) {
|
2009-10-07 07:13:40 -07:00
|
|
|
gfxFontEntry *fe = aFontEntryList[i].get();
|
2009-09-09 08:35:08 -07:00
|
|
|
if (fe->HasCharacter(aCh)) {
|
|
|
|
nsRefPtr<gfxFT2Font> font =
|
2009-10-07 07:13:40 -07:00
|
|
|
gfxFT2Font::GetOrMakeFont(static_cast<FontEntry*>(fe), &mStyle);
|
2009-09-09 08:35:08 -07:00
|
|
|
return font.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsnull;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
2009-09-10 13:52:40 -07:00
|
|
|
already_AddRefed<gfxFont>
|
2009-09-09 08:35:08 -07:00
|
|
|
gfxFT2FontGroup::WhichPrefFontSupportsChar(PRUint32 aCh)
|
2008-08-06 13:48:55 -07:00
|
|
|
{
|
2009-09-09 08:35:08 -07:00
|
|
|
if (aCh > 0xFFFF)
|
|
|
|
return nsnull;
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
nsRefPtr<gfxFT2Font> selectedFont;
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2010-02-24 09:57:44 -08:00
|
|
|
// check out the style's language
|
2009-10-07 07:13:40 -07:00
|
|
|
nsAutoTArray<nsRefPtr<gfxFontEntry>, 5> fonts;
|
2010-02-24 09:57:57 -08:00
|
|
|
GetPrefFonts(mStyle.language, fonts);
|
2009-09-09 08:35:08 -07:00
|
|
|
selectedFont = WhichFontSupportsChar(fonts, aCh);
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
// otherwise search prefs
|
|
|
|
if (!selectedFont) {
|
|
|
|
PRUint32 unicodeRange = FindCharUnicodeRange(aCh);
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
/* special case CJK */
|
|
|
|
if (unicodeRange == kRangeSetCJK) {
|
2009-09-10 13:52:40 -07:00
|
|
|
if (PR_LOG_TEST(gFontLog, PR_LOG_DEBUG)) {
|
2009-09-09 08:35:08 -07:00
|
|
|
PR_LOG(gFontLog, PR_LOG_DEBUG, (" - Trying to find fonts for: CJK"));
|
2009-09-10 13:52:40 -07:00
|
|
|
}
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-10-07 07:13:40 -07:00
|
|
|
nsAutoTArray<nsRefPtr<gfxFontEntry>, 15> fonts;
|
2009-09-09 08:35:08 -07:00
|
|
|
GetCJKPrefFonts(fonts);
|
|
|
|
selectedFont = WhichFontSupportsChar(fonts, aCh);
|
2008-08-06 13:48:55 -07:00
|
|
|
} else {
|
2010-02-24 09:57:57 -08:00
|
|
|
nsIAtom *langGroup = LangGroupFromUnicodeRange(unicodeRange);
|
2009-09-09 08:35:08 -07:00
|
|
|
if (langGroup) {
|
2010-03-10 12:47:45 -08:00
|
|
|
PR_LOG(gFontLog, PR_LOG_DEBUG, (" - Trying to find fonts for: %s", nsAtomCString(langGroup).get()));
|
|
|
|
|
2009-10-07 07:13:40 -07:00
|
|
|
nsAutoTArray<nsRefPtr<gfxFontEntry>, 5> fonts;
|
2009-09-09 08:35:08 -07:00
|
|
|
GetPrefFonts(langGroup, fonts);
|
|
|
|
selectedFont = WhichFontSupportsChar(fonts, aCh);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
if (selectedFont) {
|
|
|
|
nsRefPtr<gfxFont> f = static_cast<gfxFont*>(selectedFont.get());
|
|
|
|
return f.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2009-09-10 13:52:40 -07:00
|
|
|
already_AddRefed<gfxFont>
|
2009-09-09 08:35:08 -07:00
|
|
|
gfxFT2FontGroup::WhichSystemFontSupportsChar(PRUint32 aCh)
|
|
|
|
{
|
2009-10-07 07:13:40 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
FontEntry *fe = static_cast<FontEntry*>
|
|
|
|
(gfxPlatformFontList::PlatformFontList()->FindFontForChar(aCh, GetFontAt(0)));
|
|
|
|
if (fe) {
|
|
|
|
nsRefPtr<gfxFT2Font> f = gfxFT2Font::GetOrMakeFont(fe, &mStyle);
|
|
|
|
nsRefPtr<gfxFont> font = f.get();
|
|
|
|
return font.forget();
|
|
|
|
}
|
|
|
|
#else
|
2009-09-09 08:35:08 -07:00
|
|
|
nsRefPtr<gfxFont> selectedFont;
|
2010-07-15 23:03:45 -07:00
|
|
|
nsRefPtr<gfxFont> refFont = GetFontAt(0);
|
2009-09-09 08:35:08 -07:00
|
|
|
gfxToolkitPlatform *platform = gfxToolkitPlatform::GetPlatform();
|
|
|
|
selectedFont = platform->FindFontForChar(aCh, refFont);
|
|
|
|
if (selectedFont)
|
|
|
|
return selectedFont.forget();
|
2009-10-07 07:13:40 -07:00
|
|
|
#endif
|
2009-09-09 08:35:08 -07:00
|
|
|
return nsnull;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
2010-12-09 03:57:23 -08:00
|
|
|
/**
|
|
|
|
* gfxFT2Font
|
|
|
|
*/
|
2009-09-10 13:52:40 -07:00
|
|
|
|
2010-12-09 03:57:23 -08:00
|
|
|
PRBool
|
|
|
|
gfxFT2Font::InitTextRun(gfxContext *aContext,
|
|
|
|
gfxTextRun *aTextRun,
|
|
|
|
const PRUnichar *aString,
|
|
|
|
PRUint32 aRunStart,
|
|
|
|
PRUint32 aRunLength,
|
|
|
|
PRInt32 aRunScript,
|
|
|
|
PRBool aPreferPlatformShaping)
|
|
|
|
{
|
2010-12-09 03:58:16 -08:00
|
|
|
PRBool ok = PR_FALSE;
|
|
|
|
|
|
|
|
if (gfxPlatform::GetPlatform()->UseHarfBuzzLevel() >=
|
|
|
|
gfxUnicodeProperties::ScriptShapingLevel(aRunScript))
|
|
|
|
{
|
|
|
|
ok = mHarfBuzzShaper->InitTextRun(aContext, aTextRun, aString,
|
|
|
|
aRunStart, aRunLength, aRunScript);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
AddRange(aTextRun, aString, aRunStart, aRunLength);
|
|
|
|
}
|
|
|
|
|
2010-12-09 03:57:23 -08:00
|
|
|
return PR_TRUE;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-09 03:57:23 -08:00
|
|
|
gfxFT2Font::AddRange(gfxTextRun *aTextRun, const PRUnichar *str, PRUint32 offset, PRUint32 len)
|
2008-08-06 13:48:55 -07:00
|
|
|
{
|
|
|
|
const PRUint32 appUnitsPerDevUnit = aTextRun->GetAppUnitsPerDevUnit();
|
|
|
|
// we'll pass this in/figure it out dynamically, but at this point there can be only one face.
|
2010-12-09 03:57:23 -08:00
|
|
|
gfxFT2LockedFace faceLock(this);
|
2009-10-30 16:13:41 -07:00
|
|
|
FT_Face face = faceLock.get();
|
2008-08-06 13:48:55 -07:00
|
|
|
|
|
|
|
gfxTextRun::CompressedGlyph g;
|
|
|
|
|
2009-09-10 13:52:40 -07:00
|
|
|
const gfxFT2Font::CachedGlyphData *cgd = nsnull, *cgdNext = nsnull;
|
|
|
|
|
2010-12-09 03:57:23 -08:00
|
|
|
FT_UInt spaceGlyph = GetSpaceGlyph();
|
2009-09-10 13:52:40 -07:00
|
|
|
|
2010-12-09 03:57:23 -08:00
|
|
|
aTextRun->AddGlyphRun(this, offset);
|
2008-08-06 13:48:55 -07:00
|
|
|
for (PRUint32 i = 0; i < len; i++) {
|
2009-06-24 02:14:10 -07:00
|
|
|
PRUint32 ch = str[offset + i];
|
2008-08-06 13:48:55 -07:00
|
|
|
|
|
|
|
if (ch == 0) {
|
|
|
|
// treat this null byte as a missing glyph, don't create a glyph for it
|
2009-06-24 02:14:10 -07:00
|
|
|
aTextRun->SetMissingGlyph(offset + i, 0);
|
2008-08-06 13:48:55 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-12-09 03:57:23 -08:00
|
|
|
NS_ASSERTION(!gfxFontGroup::IsInvalidChar(ch), "Invalid char detected");
|
2009-09-10 13:52:40 -07:00
|
|
|
|
|
|
|
if (cgdNext) {
|
|
|
|
cgd = cgdNext;
|
|
|
|
cgdNext = nsnull;
|
|
|
|
} else {
|
2010-12-09 03:57:23 -08:00
|
|
|
cgd = GetGlyphDataForChar(ch);
|
2009-09-10 13:52:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
FT_UInt gid = cgd->glyphIndex;
|
2008-08-06 13:48:55 -07:00
|
|
|
PRInt32 advance = 0;
|
|
|
|
|
2009-09-10 13:52:40 -07:00
|
|
|
if (gid == 0) {
|
2008-08-06 13:48:55 -07:00
|
|
|
advance = -1; // trigger the missing glyphs case below
|
|
|
|
} else {
|
|
|
|
// find next character and its glyph -- in case they exist
|
|
|
|
// and exist in the current font face -- to compute kerning
|
|
|
|
PRUint32 chNext = 0;
|
|
|
|
FT_UInt gidNext = 0;
|
|
|
|
FT_Pos lsbDeltaNext = 0;
|
|
|
|
|
|
|
|
if (FT_HAS_KERNING(face) && i + 1 < len) {
|
2009-06-24 02:14:10 -07:00
|
|
|
chNext = str[offset + i + 1];
|
2008-08-06 13:48:55 -07:00
|
|
|
if (chNext != 0) {
|
2010-12-09 03:57:23 -08:00
|
|
|
cgdNext = GetGlyphDataForChar(chNext);
|
2009-09-10 13:52:40 -07:00
|
|
|
gidNext = cgdNext->glyphIndex;
|
|
|
|
if (gidNext && gidNext != spaceGlyph)
|
|
|
|
lsbDeltaNext = cgdNext->lsbDelta;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 13:52:40 -07:00
|
|
|
advance = cgd->xAdvance;
|
2008-08-06 13:48:55 -07:00
|
|
|
|
|
|
|
// now add kerning to the current glyph's advance
|
|
|
|
if (chNext && gidNext) {
|
|
|
|
FT_Vector kerning; kerning.x = 0;
|
|
|
|
FT_Get_Kerning(face, gid, gidNext, FT_KERNING_DEFAULT, &kerning);
|
|
|
|
advance += kerning.x;
|
2009-09-10 13:52:40 -07:00
|
|
|
if (cgd->rsbDelta - lsbDeltaNext >= 32) {
|
2008-08-06 13:48:55 -07:00
|
|
|
advance -= 64;
|
2009-09-10 13:52:40 -07:00
|
|
|
} else if (cgd->rsbDelta - lsbDeltaNext < -32) {
|
2008-08-06 13:48:55 -07:00
|
|
|
advance += 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 12:18:17 -08:00
|
|
|
// convert 26.6 fixed point to app units
|
|
|
|
// round rather than truncate to nearest pixel
|
|
|
|
// because these advances are often scaled
|
|
|
|
advance = ((advance * appUnitsPerDevUnit + 32) >> 6);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf(" gid=%d, advance=%d (%s)\n", gid, advance,
|
|
|
|
NS_LossyConvertUTF16toASCII(font->GetName()).get());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (advance >= 0 &&
|
|
|
|
gfxTextRun::CompressedGlyph::IsSimpleAdvance(advance) &&
|
|
|
|
gfxTextRun::CompressedGlyph::IsSimpleGlyphID(gid)) {
|
2009-06-24 02:14:10 -07:00
|
|
|
aTextRun->SetSimpleGlyph(offset + i, g.SetSimpleGlyph(advance, gid));
|
2008-08-06 13:48:55 -07:00
|
|
|
} else if (gid == 0) {
|
|
|
|
// gid = 0 only happens when the glyph is missing from the font
|
2009-06-24 02:14:10 -07:00
|
|
|
aTextRun->SetMissingGlyph(offset + i, ch);
|
2008-08-06 13:48:55 -07:00
|
|
|
} else {
|
|
|
|
gfxTextRun::DetailedGlyph details;
|
|
|
|
details.mGlyphID = gid;
|
|
|
|
NS_ASSERTION(details.mGlyphID == gid, "Seriously weird glyph ID detected!");
|
|
|
|
details.mAdvance = advance;
|
|
|
|
details.mXOffset = 0;
|
|
|
|
details.mYOffset = 0;
|
2009-06-24 02:14:10 -07:00
|
|
|
g.SetComplex(aTextRun->IsClusterStart(offset + i), PR_TRUE, 1);
|
|
|
|
aTextRun->SetGlyphs(offset + i, g, &details);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
}
|
2009-09-10 13:52:40 -07:00
|
|
|
}
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-10-30 16:13:41 -07:00
|
|
|
gfxFT2Font::gfxFT2Font(cairo_scaled_font_t *aCairoFont,
|
|
|
|
FontEntry *aFontEntry,
|
|
|
|
const gfxFontStyle *aFontStyle)
|
|
|
|
: gfxFT2FontBase(aCairoFont, aFontEntry, aFontStyle)
|
2008-08-06 13:48:55 -07:00
|
|
|
{
|
2008-08-28 04:05:53 -07:00
|
|
|
NS_ASSERTION(mFontEntry, "Unable to find font entry for font. Something is whack.");
|
2009-09-10 13:52:40 -07:00
|
|
|
|
|
|
|
mCharGlyphCache.Init(64);
|
2010-12-09 03:58:16 -08:00
|
|
|
|
|
|
|
mHarfBuzzShaper = new gfxHarfBuzzShaper(this);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFT2Font::~gfxFT2Font()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_font_face_t *
|
|
|
|
gfxFT2Font::CairoFontFace()
|
|
|
|
{
|
|
|
|
// XXX we need to handle fake bold here (or by having a sepaerate font entry)
|
2009-07-30 11:19:34 -07:00
|
|
|
if (mStyle.weight >= 600 && mFontEntry->mWeight < 600) {
|
|
|
|
//printf("** We want fake weight\n");
|
|
|
|
}
|
2008-08-28 04:05:53 -07:00
|
|
|
return GetFontEntry()->CairoFontFace();
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
|
|
|
|
2009-10-30 16:13:41 -07:00
|
|
|
static cairo_scaled_font_t *
|
|
|
|
CreateScaledFont(FontEntry *aFontEntry, const gfxFontStyle *aStyle)
|
2008-08-06 13:48:55 -07:00
|
|
|
{
|
2009-10-30 16:13:41 -07:00
|
|
|
cairo_scaled_font_t *scaledFont = NULL;
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-10-30 16:13:41 -07:00
|
|
|
cairo_matrix_t sizeMatrix;
|
|
|
|
cairo_matrix_t identityMatrix;
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-10-30 16:13:41 -07:00
|
|
|
// XXX deal with adjusted size
|
|
|
|
cairo_matrix_init_scale(&sizeMatrix, aStyle->size, aStyle->size);
|
|
|
|
cairo_matrix_init_identity(&identityMatrix);
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-10-30 16:13:41 -07:00
|
|
|
// synthetic oblique by skewing via the font matrix
|
|
|
|
PRBool needsOblique = (!aFontEntry->mItalic && (aStyle->style & (FONT_STYLE_ITALIC | FONT_STYLE_OBLIQUE)));
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-10-30 16:13:41 -07:00
|
|
|
if (needsOblique) {
|
|
|
|
const double kSkewFactor = 0.25;
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2009-10-30 16:13:41 -07:00
|
|
|
cairo_matrix_t style;
|
|
|
|
cairo_matrix_init(&style,
|
|
|
|
1, //xx
|
|
|
|
0, //yx
|
|
|
|
-1 * kSkewFactor, //xy
|
|
|
|
1, //yy
|
|
|
|
0, //x0
|
|
|
|
0); //y0
|
|
|
|
cairo_matrix_multiply(&sizeMatrix, &sizeMatrix, &style);
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
2009-10-30 16:13:41 -07:00
|
|
|
|
|
|
|
cairo_font_options_t *fontOptions = cairo_font_options_create();
|
2010-04-22 15:03:02 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
|
|
cairo_font_options_set_hint_metrics(fontOptions, CAIRO_HINT_METRICS_OFF);
|
|
|
|
#endif
|
|
|
|
|
2009-10-30 16:13:41 -07:00
|
|
|
scaledFont = cairo_scaled_font_create(aFontEntry->CairoFontFace(),
|
|
|
|
&sizeMatrix,
|
|
|
|
&identityMatrix, fontOptions);
|
|
|
|
cairo_font_options_destroy(fontOptions);
|
|
|
|
|
|
|
|
NS_ASSERTION(cairo_scaled_font_status(scaledFont) == CAIRO_STATUS_SUCCESS,
|
|
|
|
"Failed to make scaled font");
|
|
|
|
|
|
|
|
return scaledFont;
|
2008-08-06 13:48:55 -07:00
|
|
|
}
|
2009-01-22 22:24:29 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up the font in the gfxFont cache. If we don't find it, create one.
|
|
|
|
* In either case, add a ref, append it to the aFonts array, and return it ---
|
|
|
|
* except for OOM in which case we do nothing and return null.
|
|
|
|
*/
|
|
|
|
already_AddRefed<gfxFT2Font>
|
|
|
|
gfxFT2Font::GetOrMakeFont(const nsAString& aName, const gfxFontStyle *aStyle)
|
|
|
|
{
|
2009-10-07 07:13:40 -07:00
|
|
|
FontEntry *fe = static_cast<FontEntry*>
|
|
|
|
(gfxToolkitPlatform::GetPlatform()->FindFontEntry(aName, *aStyle));
|
2009-04-22 22:29:47 -07:00
|
|
|
if (!fe) {
|
2009-09-10 13:52:40 -07:00
|
|
|
NS_WARNING("Failed to find font entry for font!");
|
2009-04-22 22:29:47 -07:00
|
|
|
return nsnull;
|
|
|
|
}
|
2009-01-22 22:24:29 -08:00
|
|
|
|
2009-09-09 08:35:08 -07:00
|
|
|
nsRefPtr<gfxFT2Font> font = GetOrMakeFont(fe, aStyle);
|
|
|
|
return font.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxFT2Font>
|
|
|
|
gfxFT2Font::GetOrMakeFont(FontEntry *aFontEntry, const gfxFontStyle *aStyle)
|
|
|
|
{
|
2010-07-13 15:58:21 -07:00
|
|
|
nsRefPtr<gfxFont> font = gfxFontCache::GetCache()->Lookup(aFontEntry, aStyle);
|
2009-04-22 22:29:47 -07:00
|
|
|
if (!font) {
|
2009-10-30 16:13:41 -07:00
|
|
|
cairo_scaled_font_t *scaledFont = CreateScaledFont(aFontEntry, aStyle);
|
|
|
|
font = new gfxFT2Font(scaledFont, aFontEntry, aStyle);
|
|
|
|
cairo_scaled_font_destroy(scaledFont);
|
2009-01-22 22:24:29 -08:00
|
|
|
if (!font)
|
|
|
|
return nsnull;
|
|
|
|
gfxFontCache::GetCache()->AddNew(font);
|
|
|
|
}
|
|
|
|
gfxFont *f = nsnull;
|
|
|
|
font.swap(f);
|
|
|
|
return static_cast<gfxFT2Font *>(f);
|
|
|
|
}
|
|
|
|
|
2009-09-10 13:52:40 -07:00
|
|
|
void
|
|
|
|
gfxFT2Font::FillGlyphDataForChar(PRUint32 ch, CachedGlyphData *gd)
|
|
|
|
{
|
2009-10-30 16:13:41 -07:00
|
|
|
gfxFT2LockedFace faceLock(this);
|
|
|
|
FT_Face face = faceLock.get();
|
2009-09-10 13:52:40 -07:00
|
|
|
|
|
|
|
FT_UInt gid = FT_Get_Char_Index(face, ch);
|
|
|
|
|
|
|
|
if (gid == 0) {
|
|
|
|
// this font doesn't support this char!
|
|
|
|
NS_ASSERTION(gid != 0, "We don't have a glyph, but font indicated that it supported this char in tables?");
|
|
|
|
gd->glyphIndex = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-22 15:03:02 -07:00
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
|
|
FT_Error err = FT_Load_Glyph(face, gid, FT_LOAD_NO_AUTOHINT | FT_LOAD_NO_HINTING);
|
|
|
|
#else
|
2009-09-10 13:52:40 -07:00
|
|
|
FT_Error err = FT_Load_Glyph(face, gid, FT_LOAD_DEFAULT);
|
2010-04-22 15:03:02 -07:00
|
|
|
#endif
|
2009-09-10 13:52:40 -07:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
// hmm, this is weird, we failed to load a glyph that we had?
|
|
|
|
NS_WARNING("Failed to load glyph that we got from Get_Char_index");
|
|
|
|
|
|
|
|
gd->glyphIndex = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gd->glyphIndex = gid;
|
|
|
|
gd->lsbDelta = face->glyph->lsb_delta;
|
|
|
|
gd->rsbDelta = face->glyph->rsb_delta;
|
|
|
|
gd->xAdvance = face->glyph->advance.x;
|
|
|
|
}
|