2007-02-24 13:58:48 +03:00
|
|
|
/* vim: set sw=4 sts=4 et cin: */
|
|
|
|
/* ***** 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 OS/2 code in Thebes.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Peter Weilbacher <mozilla@Weilbacher.org>.
|
2007-05-20 17:45:49 +04:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006-2007
|
2007-02-24 13:58:48 +03:00
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2007-05-20 17:45:49 +04:00
|
|
|
* authors of code taken from gfxPangoFonts:
|
|
|
|
* Mozilla Foundation
|
|
|
|
* Vladimir Vukicevic <vladimir@mozilla.com>
|
|
|
|
* Masayuki Nakano <masayuki@d-toybox.com>
|
2007-02-24 13:58:48 +03: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 ***** */
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
|
|
|
|
#include "gfxOS2Platform.h"
|
|
|
|
#include "gfxOS2Surface.h"
|
|
|
|
#include "gfxOS2Fonts.h"
|
2009-01-18 23:14:14 +03:00
|
|
|
#include "nsTArray.h"
|
2010-03-06 20:56:30 +03:00
|
|
|
#include "gfxAtoms.h"
|
2007-02-24 13:58:48 +03:00
|
|
|
|
|
|
|
#include "nsIPlatformCharset.h"
|
2011-06-12 06:30:16 +04:00
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2007-02-24 13:58:48 +03:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* class gfxOS2Font
|
|
|
|
**********************************************************************/
|
|
|
|
|
2008-08-07 12:12:45 +04:00
|
|
|
gfxOS2Font::gfxOS2Font(gfxOS2FontEntry *aFontEntry, const gfxFontStyle *aFontStyle)
|
|
|
|
: gfxFont(aFontEntry, aFontStyle),
|
2007-05-20 17:45:49 +04:00
|
|
|
mFontFace(nsnull), mScaledFont(nsnull),
|
2008-05-28 16:38:09 +04:00
|
|
|
mMetrics(nsnull), mAdjustedSize(0),
|
|
|
|
mHinting(FC_HINT_MEDIUM), mAntialias(FcTrue)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2008-05-28 16:55:36 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
2008-11-24 22:18:15 +03:00
|
|
|
printf("gfxOS2Font[%p]::gfxOS2Font(%p \"%s\", aFontStyle)\n",
|
|
|
|
(void *)this, (void *)aFontEntry,
|
|
|
|
NS_LossyConvertUTF16toASCII(aFontEntry->Name()).get());
|
2008-05-28 16:38:09 +04:00
|
|
|
#endif
|
|
|
|
// try to get the preferences for hinting, antialias, and embolden options
|
2011-06-12 06:30:16 +04:00
|
|
|
PRInt32 value;
|
|
|
|
nsresult rv = Preferences::GetInt("gfx.os2.font.hinting", &value);
|
|
|
|
if (NS_SUCCEEDED(rv) && value >= FC_HINT_NONE && value <= FC_HINT_FULL) {
|
|
|
|
mHinting = value;
|
2008-05-28 16:38:09 +04:00
|
|
|
}
|
2011-06-12 06:30:16 +04:00
|
|
|
|
|
|
|
mAntialias = Preferences::GetBool("gfx.os2.font.antialiasing", mAntialias);
|
|
|
|
|
2008-05-28 16:55:36 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
2008-05-28 16:38:09 +04:00
|
|
|
printf(" font display options: hinting=%d, antialiasing=%s\n",
|
|
|
|
mHinting, mAntialias ? "on" : "off");
|
2007-02-24 13:58:48 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxOS2Font::~gfxOS2Font()
|
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf("gfxOS2Font[%#x]::~gfxOS2Font()\n", (unsigned)this);
|
2007-02-24 13:58:48 +03:00
|
|
|
#endif
|
2007-05-20 17:45:49 +04:00
|
|
|
if (mFontFace) {
|
|
|
|
cairo_font_face_destroy(mFontFace);
|
|
|
|
}
|
|
|
|
if (mScaledFont) {
|
|
|
|
cairo_scaled_font_destroy(mScaledFont);
|
|
|
|
}
|
2011-05-17 18:01:36 +04:00
|
|
|
delete mMetrics;
|
2007-05-20 17:45:49 +04:00
|
|
|
mFontFace = nsnull;
|
|
|
|
mScaledFont = nsnull;
|
2007-02-24 13:58:48 +03:00
|
|
|
mMetrics = nsnull;
|
|
|
|
}
|
|
|
|
|
2009-08-29 01:58:25 +04:00
|
|
|
// fill font metrics structure with default values in case of error
|
|
|
|
static void FillMetricsDefaults(gfxFont::Metrics *aMetrics)
|
|
|
|
{
|
|
|
|
aMetrics->emAscent = 0.8 * aMetrics->emHeight;
|
|
|
|
aMetrics->emDescent = 0.2 * aMetrics->emHeight;
|
|
|
|
aMetrics->maxAscent = aMetrics->emAscent;
|
|
|
|
aMetrics->maxDescent = aMetrics->maxDescent;
|
|
|
|
aMetrics->maxHeight = aMetrics->emHeight;
|
|
|
|
aMetrics->internalLeading = 0.0;
|
|
|
|
aMetrics->externalLeading = 0.2 * aMetrics->emHeight;
|
|
|
|
aMetrics->spaceWidth = 0.5 * aMetrics->emHeight;
|
|
|
|
aMetrics->maxAdvance = aMetrics->spaceWidth;
|
|
|
|
aMetrics->aveCharWidth = aMetrics->spaceWidth;
|
|
|
|
aMetrics->zeroOrAveCharWidth = aMetrics->spaceWidth;
|
|
|
|
aMetrics->xHeight = 0.5 * aMetrics->emHeight;
|
|
|
|
aMetrics->underlineSize = aMetrics->emHeight / 14.0;
|
|
|
|
aMetrics->underlineOffset = -aMetrics->underlineSize;
|
|
|
|
aMetrics->strikeoutOffset = 0.25 * aMetrics->emHeight;
|
|
|
|
aMetrics->strikeoutSize = aMetrics->underlineSize;
|
|
|
|
aMetrics->superscriptOffset = aMetrics->xHeight;
|
|
|
|
aMetrics->subscriptOffset = aMetrics->xHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snap a line to pixels while keeping the center and size of the
|
|
|
|
// line as close to the original position as possible.
|
|
|
|
static void SnapLineToPixels(gfxFloat& aOffset, gfxFloat& aSize)
|
|
|
|
{
|
2011-06-02 16:56:50 +04:00
|
|
|
gfxFloat snappedSize = NS_MAX(NS_floor(aSize + 0.5), 1.0);
|
2009-08-29 01:58:25 +04:00
|
|
|
// Correct offset for change in size
|
|
|
|
gfxFloat offset = aOffset - 0.5 * (aSize - snappedSize);
|
|
|
|
// Snap offset
|
|
|
|
aOffset = NS_floor(offset + 0.5);
|
|
|
|
aSize = snappedSize;
|
|
|
|
}
|
|
|
|
|
2007-07-01 00:20:51 +04:00
|
|
|
// gfxOS2Font::GetMetrics()
|
|
|
|
// return the metrics of the current font using the gfxFont metrics structure.
|
|
|
|
// If the metrics are not available yet, compute them using the FreeType
|
|
|
|
// function on the font. (This is partly based on the respective function from
|
|
|
|
// gfxPangoFonts)
|
2007-05-20 17:45:49 +04:00
|
|
|
const gfxFont::Metrics& gfxOS2Font::GetMetrics()
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_1
|
|
|
|
printf("gfxOS2Font[%#x]::GetMetrics()\n", (unsigned)this);
|
|
|
|
#endif
|
2008-11-26 22:12:52 +03:00
|
|
|
if (mMetrics) {
|
|
|
|
return *mMetrics;
|
|
|
|
}
|
2007-08-19 16:05:23 +04:00
|
|
|
|
2009-04-29 02:10:56 +04:00
|
|
|
// whatever happens below, we can always create the metrics
|
|
|
|
mMetrics = new gfxFont::Metrics;
|
|
|
|
mSpaceGlyph = 0;
|
|
|
|
|
2009-08-29 01:58:25 +04:00
|
|
|
// round size to integer pixels, this is to get full pixels for layout
|
|
|
|
// together with internal/external leading (see below)
|
|
|
|
mMetrics->emHeight = NS_floor(GetStyle()->size + 0.5);
|
|
|
|
|
2011-04-10 07:32:09 +04:00
|
|
|
cairo_scaled_font_t* scaledFont = CairoScaledFont();
|
|
|
|
if (!scaledFont) {
|
|
|
|
FillMetricsDefaults(mMetrics);
|
|
|
|
return *mMetrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_Face face = cairo_ft_scaled_font_lock_face(scaledFont);
|
2008-11-26 22:12:52 +03:00
|
|
|
if (!face) {
|
|
|
|
// Abort here already, otherwise we crash in the following
|
|
|
|
// this can happen if the font-size requested is zero.
|
2009-08-29 01:58:25 +04:00
|
|
|
FillMetricsDefaults(mMetrics);
|
2008-11-26 22:12:52 +03:00
|
|
|
return *mMetrics;
|
|
|
|
}
|
|
|
|
if (!face->charmap) {
|
|
|
|
// Also abort, if the charmap isn't loaded; then the char
|
|
|
|
// lookups won't work. This happens for fonts without Unicode
|
|
|
|
// charmap.
|
2011-04-10 07:32:09 +04:00
|
|
|
cairo_ft_scaled_font_unlock_face(scaledFont);
|
2009-08-29 01:58:25 +04:00
|
|
|
FillMetricsDefaults(mMetrics);
|
2008-11-26 22:12:52 +03:00
|
|
|
return *mMetrics;
|
|
|
|
}
|
|
|
|
|
2009-08-29 01:58:25 +04:00
|
|
|
// compute font scaling factors
|
|
|
|
gfxFloat emUnit = 1.0 * face->units_per_EM;
|
|
|
|
gfxFloat xScale = face->size->metrics.x_ppem / emUnit;
|
|
|
|
gfxFloat yScale = face->size->metrics.y_ppem / emUnit;
|
2007-05-20 17:45:49 +04:00
|
|
|
|
2008-11-26 22:12:52 +03:00
|
|
|
FT_UInt gid; // glyph ID
|
2007-08-19 16:36:21 +04:00
|
|
|
|
2008-11-26 22:12:52 +03:00
|
|
|
// properties of space
|
|
|
|
gid = FT_Get_Char_Index(face, ' ');
|
|
|
|
if (gid) {
|
2007-12-04 01:22:36 +03:00
|
|
|
// Load glyph into glyph slot. Use load_default here to get results in
|
|
|
|
// 26.6 fractional pixel format which is what is used for all other
|
|
|
|
// characters in gfxOS2FontGroup::CreateGlyphRunsFT.
|
|
|
|
FT_Load_Glyph(face, gid, FT_LOAD_DEFAULT);
|
2009-08-29 01:58:25 +04:00
|
|
|
// glyph width doesn't work for spaces, use advance instead
|
2007-12-04 01:22:36 +03:00
|
|
|
mMetrics->spaceWidth = face->glyph->advance.x >> 6;
|
2007-05-20 17:45:49 +04:00
|
|
|
// save the space glyph
|
|
|
|
mSpaceGlyph = gid;
|
2008-11-26 22:12:52 +03:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(gid, "this font doesn't have a space glyph!");
|
2009-08-29 01:58:25 +04:00
|
|
|
mMetrics->spaceWidth = face->max_advance_width * xScale;
|
2008-11-26 22:12:52 +03:00
|
|
|
}
|
2007-08-19 16:05:23 +04:00
|
|
|
|
2008-11-26 22:12:52 +03:00
|
|
|
// properties of 'x', also use its width as average width
|
|
|
|
gid = FT_Get_Char_Index(face, 'x'); // select the glyph
|
|
|
|
if (gid) {
|
|
|
|
// Load glyph into glyph slot. Here, use no_scale to get font units.
|
|
|
|
FT_Load_Glyph(face, gid, FT_LOAD_NO_SCALE);
|
|
|
|
mMetrics->xHeight = face->glyph->metrics.height * yScale;
|
2009-08-29 01:58:25 +04:00
|
|
|
mMetrics->aveCharWidth = face->glyph->metrics.horiAdvance * xScale;
|
2008-11-26 22:12:52 +03:00
|
|
|
} else {
|
|
|
|
// this font doesn't have an 'x'...
|
|
|
|
// fake these metrics using a fraction of the font size
|
|
|
|
mMetrics->xHeight = mMetrics->emHeight * 0.5;
|
|
|
|
mMetrics->aveCharWidth = mMetrics->emHeight * 0.5;
|
|
|
|
}
|
2007-08-19 16:27:23 +04:00
|
|
|
|
2008-11-26 22:12:52 +03:00
|
|
|
// properties of '0', for 'ch' units
|
|
|
|
gid = FT_Get_Char_Index(face, '0');
|
|
|
|
if (gid) {
|
|
|
|
FT_Load_Glyph(face, gid, FT_LOAD_NO_SCALE);
|
2009-07-10 11:46:21 +04:00
|
|
|
mMetrics->zeroOrAveCharWidth = face->glyph->metrics.horiAdvance * xScale;
|
2008-11-26 22:12:52 +03:00
|
|
|
} else {
|
2009-08-29 01:58:25 +04:00
|
|
|
// this font doesn't have a '0'
|
|
|
|
mMetrics->zeroOrAveCharWidth = mMetrics->aveCharWidth;
|
2008-11-26 22:12:52 +03:00
|
|
|
}
|
2008-07-16 01:31:36 +04:00
|
|
|
|
2008-11-26 22:12:52 +03:00
|
|
|
// compute an adjusted size if we need to
|
|
|
|
if (mAdjustedSize == 0 && GetStyle()->sizeAdjust != 0) {
|
|
|
|
gfxFloat aspect = mMetrics->xHeight / GetStyle()->size;
|
|
|
|
mAdjustedSize = GetStyle()->GetAdjustedSize(aspect);
|
|
|
|
mMetrics->emHeight = mAdjustedSize;
|
|
|
|
}
|
2007-08-19 16:27:23 +04:00
|
|
|
|
2009-08-29 01:58:25 +04:00
|
|
|
// now load the OS/2 TrueType table to access some more properties
|
2008-11-26 22:12:52 +03:00
|
|
|
TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
|
|
|
|
if (os2 && os2->version != 0xFFFF) { // should be there if not old Mac font
|
|
|
|
// if we are here we can improve the avgCharWidth
|
2011-06-02 16:56:50 +04:00
|
|
|
mMetrics->aveCharWidth = NS_MAX(mMetrics->aveCharWidth,
|
2009-08-29 01:58:25 +04:00
|
|
|
os2->xAvgCharWidth * xScale);
|
2007-08-19 16:27:23 +04:00
|
|
|
|
2011-06-02 16:56:50 +04:00
|
|
|
mMetrics->superscriptOffset = NS_MAX(os2->ySuperscriptYOffset * yScale, 1.0);
|
2008-11-26 22:12:52 +03:00
|
|
|
// some fonts have the incorrect sign (from gfxPangoFonts)
|
2011-06-02 16:56:50 +04:00
|
|
|
mMetrics->subscriptOffset = NS_MAX(fabs(os2->ySubscriptYOffset * yScale),
|
2009-08-29 01:58:25 +04:00
|
|
|
1.0);
|
2008-11-26 22:12:52 +03:00
|
|
|
mMetrics->strikeoutOffset = os2->yStrikeoutPosition * yScale;
|
|
|
|
mMetrics->strikeoutSize = os2->yStrikeoutSize * yScale;
|
|
|
|
} else {
|
|
|
|
// use fractions of emHeight instead of xHeight for these to be more robust
|
|
|
|
mMetrics->superscriptOffset = mMetrics->emHeight * 0.5;
|
|
|
|
mMetrics->subscriptOffset = mMetrics->emHeight * 0.2;
|
|
|
|
mMetrics->strikeoutOffset = mMetrics->emHeight * 0.3;
|
|
|
|
mMetrics->strikeoutSize = face->underline_thickness * yScale;
|
|
|
|
}
|
2009-08-29 01:58:25 +04:00
|
|
|
SnapLineToPixels(mMetrics->strikeoutOffset, mMetrics->strikeoutSize);
|
|
|
|
|
2008-11-26 22:12:52 +03:00
|
|
|
// seems that underlineOffset really has to be negative
|
|
|
|
mMetrics->underlineOffset = face->underline_position * yScale;
|
|
|
|
mMetrics->underlineSize = face->underline_thickness * yScale;
|
2007-08-19 16:05:23 +04:00
|
|
|
|
2008-11-26 22:12:52 +03:00
|
|
|
// descents are negative in FT but Thebes wants them positive
|
|
|
|
mMetrics->emAscent = face->ascender * yScale;
|
|
|
|
mMetrics->emDescent = -face->descender * yScale;
|
|
|
|
mMetrics->maxHeight = face->height * yScale;
|
2009-08-29 01:58:25 +04:00
|
|
|
// the max units determine frame heights, better be generous
|
2011-06-02 16:56:50 +04:00
|
|
|
mMetrics->maxAscent = NS_MAX(face->bbox.yMax * yScale,
|
2009-08-29 01:58:25 +04:00
|
|
|
mMetrics->emAscent);
|
2011-06-02 16:56:50 +04:00
|
|
|
mMetrics->maxDescent = NS_MAX(-face->bbox.yMin * yScale,
|
2009-08-29 01:58:25 +04:00
|
|
|
mMetrics->emDescent);
|
2011-06-02 16:56:50 +04:00
|
|
|
mMetrics->maxAdvance = NS_MAX(face->max_advance_width * xScale,
|
2009-08-29 01:58:25 +04:00
|
|
|
mMetrics->aveCharWidth);
|
|
|
|
|
|
|
|
// leadings are not available directly (only for WinFNTs);
|
|
|
|
// better compute them on our own, to get integer values and make
|
|
|
|
// layout happy (see // LockedFTFace::GetMetrics in gfxPangoFonts.cpp)
|
|
|
|
mMetrics->internalLeading = NS_floor(mMetrics->maxHeight
|
|
|
|
- mMetrics->emHeight + 0.5);
|
|
|
|
gfxFloat lineHeight = NS_floor(mMetrics->maxHeight + 0.5);
|
|
|
|
mMetrics->externalLeading = lineHeight
|
|
|
|
- mMetrics->internalLeading - mMetrics->emHeight;
|
2007-08-19 16:05:23 +04:00
|
|
|
|
2008-11-26 22:12:52 +03:00
|
|
|
SanitizeMetrics(mMetrics, PR_FALSE);
|
2008-02-16 00:31:38 +03:00
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_1
|
2008-11-26 22:12:52 +03:00
|
|
|
printf("gfxOS2Font[%#x]::GetMetrics():\n"
|
|
|
|
" %s (%s)\n"
|
|
|
|
" emHeight=%f == %f=gfxFont::style.size == %f=adjSz\n"
|
|
|
|
" maxHeight=%f xHeight=%f\n"
|
2009-08-29 01:58:25 +04:00
|
|
|
" aveCharWidth=%f(x) zeroOrAveWidth=%f(0) spaceWidth=%f\n"
|
2008-11-26 22:12:52 +03:00
|
|
|
" supOff=%f SubOff=%f strOff=%f strSz=%f\n"
|
|
|
|
" undOff=%f undSz=%f intLead=%f extLead=%f\n"
|
|
|
|
" emAsc=%f emDesc=%f maxH=%f\n"
|
|
|
|
" maxAsc=%f maxDes=%f maxAdv=%f\n",
|
|
|
|
(unsigned)this,
|
|
|
|
NS_LossyConvertUTF16toASCII(GetName()).get(),
|
|
|
|
os2 && os2->version != 0xFFFF ? "has OS/2 table" : "no OS/2 table!",
|
|
|
|
mMetrics->emHeight, GetStyle()->size, mAdjustedSize,
|
|
|
|
mMetrics->maxHeight, mMetrics->xHeight,
|
2009-08-29 01:58:25 +04:00
|
|
|
mMetrics->aveCharWidth, mMetrics->zeroOrAveCharWidth, mMetrics->spaceWidth,
|
2008-11-26 22:12:52 +03:00
|
|
|
mMetrics->superscriptOffset, mMetrics->subscriptOffset,
|
|
|
|
mMetrics->strikeoutOffset, mMetrics->strikeoutSize,
|
|
|
|
mMetrics->underlineOffset, mMetrics->underlineSize,
|
|
|
|
mMetrics->internalLeading, mMetrics->externalLeading,
|
|
|
|
mMetrics->emAscent, mMetrics->emDescent, mMetrics->maxHeight,
|
|
|
|
mMetrics->maxAscent, mMetrics->maxDescent, mMetrics->maxAdvance
|
|
|
|
);
|
2007-05-20 17:45:49 +04:00
|
|
|
#endif
|
2011-04-10 07:32:09 +04:00
|
|
|
cairo_ft_scaled_font_unlock_face(scaledFont);
|
2007-05-20 17:45:49 +04:00
|
|
|
return *mMetrics;
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-06-24 14:00:50 +04:00
|
|
|
// weight list copied from fontconfig.h
|
|
|
|
// unfortunately, the OS/2 version so far only supports regular and bold
|
|
|
|
static const PRInt8 nFcWeight = 2; // 10; // length of weight list
|
|
|
|
static const int fcWeight[] = {
|
|
|
|
//FC_WEIGHT_THIN,
|
|
|
|
//FC_WEIGHT_EXTRALIGHT, // == FC_WEIGHT_ULTRALIGHT
|
|
|
|
//FC_WEIGHT_LIGHT,
|
|
|
|
//FC_WEIGHT_BOOK,
|
|
|
|
FC_WEIGHT_REGULAR, // == FC_WEIGHT_NORMAL
|
|
|
|
//FC_WEIGHT_MEDIUM,
|
|
|
|
//FC_WEIGHT_DEMIBOLD, // == FC_WEIGHT_SEMIBOLD
|
|
|
|
FC_WEIGHT_BOLD,
|
|
|
|
//FC_WEIGHT_EXTRABOLD, // == FC_WEIGHT_ULTRABOLD
|
|
|
|
//FC_WEIGHT_BLACK // == FC_WEIGHT_HEAVY
|
|
|
|
};
|
|
|
|
|
|
|
|
// gfxOS2Font::CairoFontFace()
|
|
|
|
// return a font face usable by cairo for font rendering
|
|
|
|
// if none was created yet, use FontConfig patterns based on the current style
|
|
|
|
// to create a new font face
|
2007-05-20 17:45:49 +04:00
|
|
|
cairo_font_face_t *gfxOS2Font::CairoFontFace()
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf("gfxOS2Font[%#x]::CairoFontFace()\n", (unsigned)this);
|
2007-02-24 13:58:48 +03:00
|
|
|
#endif
|
2007-05-20 17:45:49 +04:00
|
|
|
if (!mFontFace) {
|
2007-02-24 13:58:48 +03:00
|
|
|
#ifdef DEBUG_thebes
|
2007-05-20 17:45:49 +04:00
|
|
|
printf("gfxOS2Font[%#x]::CairoFontFace(): create it for %s, %f\n",
|
2008-08-07 12:12:45 +04:00
|
|
|
(unsigned)this, NS_LossyConvertUTF16toASCII(GetName()).get(), GetStyle()->size);
|
2007-02-24 13:58:48 +03:00
|
|
|
#endif
|
2007-05-20 17:45:49 +04:00
|
|
|
FcPattern *fcPattern = FcPatternCreate();
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
// add (family) name to pattern
|
2009-09-19 01:33:03 +04:00
|
|
|
// convert name because FC stores it in UTF8 while we have it in UTF16
|
2007-05-20 17:45:49 +04:00
|
|
|
FcPatternAddString(fcPattern, FC_FAMILY,
|
2009-09-19 01:33:03 +04:00
|
|
|
(FcChar8 *)NS_ConvertUTF16toUTF8(GetName()).get());
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-06-24 14:00:50 +04:00
|
|
|
// The requirements outlined in gfxFont.h are difficult to meet without
|
|
|
|
// having a table of available font weights, so we map the gfxFont
|
|
|
|
// weight to possible FontConfig weights.
|
2010-11-10 18:49:52 +03:00
|
|
|
PRInt8 weight = GetStyle()->ComputeWeight();
|
2007-06-24 14:00:50 +04:00
|
|
|
// gfxFont weight FC weight
|
|
|
|
// 400 80
|
|
|
|
// 700 200
|
|
|
|
PRInt16 fcW = 40 * weight - 80; // match gfxFont weight to base FC weight
|
2007-08-19 16:05:23 +04:00
|
|
|
// find the correct weight in the list
|
2007-06-24 14:00:50 +04:00
|
|
|
PRInt8 i = 0;
|
|
|
|
while (i < nFcWeight && fcWeight[i] < fcW) {
|
|
|
|
i++;
|
2007-05-20 17:45:49 +04:00
|
|
|
}
|
2007-06-24 14:00:50 +04:00
|
|
|
if (i < 0) {
|
|
|
|
i = 0;
|
|
|
|
} else if (i >= nFcWeight) {
|
|
|
|
i = nFcWeight - 1;
|
|
|
|
}
|
|
|
|
fcW = fcWeight[i];
|
|
|
|
|
|
|
|
// add weight to pattern
|
|
|
|
FcPatternAddInteger(fcPattern, FC_WEIGHT, fcW);
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-06-24 14:00:50 +04:00
|
|
|
PRUint8 fcProperty;
|
2007-05-20 17:45:49 +04:00
|
|
|
// add style to pattern
|
2007-12-04 01:31:03 +03:00
|
|
|
switch (GetStyle()->style) {
|
2007-05-20 17:45:49 +04:00
|
|
|
case FONT_STYLE_ITALIC:
|
|
|
|
fcProperty = FC_SLANT_ITALIC;
|
|
|
|
break;
|
|
|
|
case FONT_STYLE_OBLIQUE:
|
|
|
|
fcProperty = FC_SLANT_OBLIQUE;
|
|
|
|
break;
|
|
|
|
case FONT_STYLE_NORMAL:
|
|
|
|
default:
|
|
|
|
fcProperty = FC_SLANT_ROMAN;
|
|
|
|
}
|
|
|
|
FcPatternAddInteger(fcPattern, FC_SLANT, fcProperty);
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
// add the size we want
|
2007-08-19 16:27:23 +04:00
|
|
|
FcPatternAddDouble(fcPattern, FC_PIXEL_SIZE,
|
2007-12-04 01:31:03 +03:00
|
|
|
mAdjustedSize ? mAdjustedSize : GetStyle()->size);
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
// finally find a matching font
|
|
|
|
FcResult fcRes;
|
|
|
|
FcPattern *fcMatch = FcFontMatch(NULL, fcPattern, &fcRes);
|
2011-04-10 07:32:09 +04:00
|
|
|
|
|
|
|
// Most code that depends on FcFontMatch() assumes it won't fail,
|
|
|
|
// then crashes when it does. For now, at least, substitute the
|
|
|
|
// default serif font when it fails to avoid those crashes.
|
|
|
|
if (!fcMatch) {
|
|
|
|
//#ifdef DEBUG
|
|
|
|
printf("Could not match font for:\n"
|
|
|
|
" family=%s, weight=%d, slant=%d, size=%f\n",
|
|
|
|
NS_LossyConvertUTF16toASCII(GetName()).get(),
|
|
|
|
GetStyle()->weight, GetStyle()->style, GetStyle()->size);
|
|
|
|
//#endif
|
|
|
|
// FcPatternAddString() will free the existing FC_FAMILY string
|
|
|
|
FcPatternAddString(fcPattern, FC_FAMILY, (FcChar8*)"SERIF");
|
|
|
|
fcMatch = FcFontMatch(NULL, fcPattern, &fcRes);
|
|
|
|
//#ifdef DEBUG
|
|
|
|
printf("Attempt to substitute default SERIF font %s\n",
|
|
|
|
fcMatch ? "succeeded" : "failed");
|
|
|
|
//#endif
|
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
FcPatternDestroy(fcPattern);
|
2008-05-28 16:38:09 +04:00
|
|
|
|
2008-11-24 22:18:39 +03:00
|
|
|
if (fcMatch) {
|
2009-08-19 11:47:09 +04:00
|
|
|
int w = FC_WEIGHT_REGULAR;
|
|
|
|
FcPatternGetInteger(fcMatch, FC_WEIGHT, 0, &w);
|
|
|
|
if (fcW >= FC_WEIGHT_DEMIBOLD && w < FC_WEIGHT_DEMIBOLD) {
|
|
|
|
// if we want a bold font, but the selected font doesn't have a
|
|
|
|
// bold counterpart, artificially embolden it
|
2008-11-24 22:18:39 +03:00
|
|
|
FcPatternAddBool(fcMatch, FC_EMBOLDEN, FcTrue);
|
|
|
|
}
|
2009-08-19 11:47:09 +04:00
|
|
|
FcPatternAddBool(fcMatch, FC_ANTIALIAS, mAntialias);
|
2008-11-24 22:18:39 +03:00
|
|
|
FcPatternAddInteger(fcMatch, FC_HINT_STYLE, mHinting);
|
|
|
|
|
|
|
|
// and ask cairo to return a font face for this
|
|
|
|
mFontFace = cairo_ft_font_face_create_for_pattern(fcMatch);
|
|
|
|
|
|
|
|
FcPatternDestroy(fcMatch);
|
2007-11-29 00:46:10 +03:00
|
|
|
}
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
NS_ASSERTION(mFontFace, "Failed to make font face");
|
|
|
|
return mFontFace;
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
cairo_scaled_font_t *gfxOS2Font::CairoScaledFont()
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf("gfxOS2Font[%#x]::CairoScaledFont()\n", (unsigned)this);
|
|
|
|
#endif
|
2008-11-26 22:13:43 +03:00
|
|
|
if (mScaledFont) {
|
|
|
|
return mScaledFont;
|
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
2008-11-26 22:13:43 +03:00
|
|
|
printf("gfxOS2Font[%#x]::CairoScaledFont(): create it for %s, %f\n",
|
|
|
|
(unsigned)this, NS_LossyConvertUTF16toASCII(GetName()).get(), GetStyle()->size);
|
2007-05-20 17:45:49 +04:00
|
|
|
#endif
|
|
|
|
|
2008-11-26 22:13:43 +03:00
|
|
|
double size = mAdjustedSize ? mAdjustedSize : GetStyle()->size;
|
|
|
|
cairo_matrix_t identityMatrix;
|
|
|
|
cairo_matrix_init_identity(&identityMatrix);
|
|
|
|
cairo_matrix_t fontMatrix;
|
|
|
|
// synthetic oblique by skewing via the font matrix
|
|
|
|
if (!mFontEntry->mItalic &&
|
|
|
|
(mStyle.style & (FONT_STYLE_ITALIC | FONT_STYLE_OBLIQUE)))
|
|
|
|
{
|
|
|
|
const double kSkewFactor = 0.2126; // 12 deg skew as used in e.g. ftview
|
|
|
|
cairo_matrix_init(&fontMatrix, size, 0, -kSkewFactor*size, size, 0, 0);
|
|
|
|
} else {
|
2007-05-20 17:45:49 +04:00
|
|
|
cairo_matrix_init_scale(&fontMatrix, size, size);
|
|
|
|
}
|
2011-04-10 07:32:09 +04:00
|
|
|
|
|
|
|
cairo_font_face_t * face = CairoFontFace();
|
|
|
|
if (!face)
|
|
|
|
return nsnull;
|
|
|
|
|
2008-11-26 22:13:43 +03:00
|
|
|
cairo_font_options_t *fontOptions = cairo_font_options_create();
|
2011-04-10 07:32:09 +04:00
|
|
|
mScaledFont = cairo_scaled_font_create(face, &fontMatrix,
|
2008-11-26 22:13:43 +03:00
|
|
|
&identityMatrix, fontOptions);
|
|
|
|
cairo_font_options_destroy(fontOptions);
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-08-25 22:33:45 +04:00
|
|
|
NS_ASSERTION(cairo_scaled_font_status(mScaledFont) == CAIRO_STATUS_SUCCESS,
|
2007-10-19 23:24:03 +04:00
|
|
|
"Failed to make scaled font");
|
2007-05-20 17:45:49 +04:00
|
|
|
return mScaledFont;
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-09-27 20:41:46 +04:00
|
|
|
PRBool gfxOS2Font::SetupCairoFont(gfxContext *aContext)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf("gfxOS2Font[%#x]::SetupCairoFont(%#x)\n",
|
2007-09-27 20:41:46 +04:00
|
|
|
(unsigned)this, (unsigned) aContext);
|
2007-02-24 13:58:48 +03:00
|
|
|
#endif
|
2007-05-20 17:45:49 +04:00
|
|
|
// gfxPangoFont checks the CTM but Windows doesn't so leave away here, too
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
// this implicitely ensures that mScaledFont is created if NULL
|
2007-08-06 16:30:13 +04:00
|
|
|
cairo_scaled_font_t *scaledFont = CairoScaledFont();
|
2011-04-10 07:32:09 +04:00
|
|
|
if (!scaledFont || cairo_scaled_font_status(scaledFont) != CAIRO_STATUS_SUCCESS) {
|
2007-08-25 22:33:45 +04:00
|
|
|
// Don't cairo_set_scaled_font as that would propagate the error to
|
|
|
|
// the cairo_t, precluding any further drawing.
|
|
|
|
return PR_FALSE;
|
2007-08-06 16:30:13 +04:00
|
|
|
}
|
2007-09-27 20:41:46 +04:00
|
|
|
cairo_set_scaled_font(aContext->GetCairo(), scaledFont);
|
2007-08-25 22:33:45 +04:00
|
|
|
return PR_TRUE;
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-12-04 01:31:03 +03:00
|
|
|
/**
|
|
|
|
* Look up the font in the gfxFont cache. If we don't find it, create one.
|
|
|
|
* In either case, add a ref and return it ---
|
|
|
|
* except for OOM in which case we do nothing and return null.
|
|
|
|
*/
|
2008-07-20 22:51:49 +04:00
|
|
|
already_AddRefed<gfxOS2Font> gfxOS2Font::GetOrMakeFont(const nsAString& aName,
|
|
|
|
const gfxFontStyle *aStyle)
|
2007-12-04 01:31:03 +03:00
|
|
|
{
|
2010-08-07 19:35:33 +04:00
|
|
|
nsRefPtr<gfxOS2FontEntry> fe = new gfxOS2FontEntry(aName);
|
|
|
|
nsRefPtr<gfxFont> font =
|
|
|
|
gfxFontCache::GetCache()->Lookup(static_cast<gfxFontEntry *>(fe), aStyle);
|
2007-12-04 01:31:03 +03:00
|
|
|
if (!font) {
|
2008-08-07 12:12:45 +04:00
|
|
|
font = new gfxOS2Font(fe, aStyle);
|
2007-12-04 01:31:03 +03:00
|
|
|
if (!font)
|
|
|
|
return nsnull;
|
|
|
|
gfxFontCache::GetCache()->AddNew(font);
|
|
|
|
}
|
|
|
|
gfxFont *f = nsnull;
|
|
|
|
font.swap(f);
|
|
|
|
return static_cast<gfxOS2Font *>(f);
|
|
|
|
}
|
|
|
|
|
2008-07-20 22:51:49 +04:00
|
|
|
/**********************************************************************
|
|
|
|
* class gfxOS2FontGroup
|
|
|
|
**********************************************************************/
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
gfxOS2FontGroup::gfxOS2FontGroup(const nsAString& aFamilies,
|
2008-10-01 07:01:53 +04:00
|
|
|
const gfxFontStyle* aStyle,
|
|
|
|
gfxUserFontSet *aUserFontSet)
|
|
|
|
: gfxFontGroup(aFamilies, aStyle, aUserFontSet)
|
2007-05-20 17:45:49 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf("gfxOS2FontGroup[%#x]::gfxOS2FontGroup(\"%s\", %#x)\n",
|
|
|
|
(unsigned)this, NS_LossyConvertUTF16toASCII(aFamilies).get(),
|
|
|
|
(unsigned)aStyle);
|
|
|
|
#endif
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-11-29 00:46:10 +03:00
|
|
|
// check for WarpSans and as we cannot display that (yet), replace
|
|
|
|
// it with Workplace Sans
|
|
|
|
int pos = 0;
|
|
|
|
if ((pos = mFamilies.Find("WarpSans", PR_FALSE, 0, -1)) > -1) {
|
|
|
|
mFamilies.Replace(pos, 8, NS_LITERAL_STRING("Workplace Sans"));
|
|
|
|
}
|
|
|
|
|
2009-01-18 23:14:14 +03:00
|
|
|
nsTArray<nsString> familyArray;
|
2007-05-20 17:45:49 +04:00
|
|
|
ForEachFont(FontCallback, &familyArray);
|
2007-11-18 01:22:04 +03:00
|
|
|
|
|
|
|
// To be able to easily search for glyphs in other fonts, append a few good
|
|
|
|
// replacement candidates to the list. The best ones are the Unicode fonts that
|
|
|
|
// are set up, and if the user was so clever to set up the User Defined fonts,
|
|
|
|
// then these are probable candidates, too.
|
|
|
|
nsString fontString;
|
2010-03-06 20:56:30 +03:00
|
|
|
gfxPlatform::GetPlatform()->GetPrefFonts(gfxAtoms::x_unicode, fontString, PR_FALSE);
|
|
|
|
ForEachFont(fontString, gfxAtoms::x_unicode, FontCallback, &familyArray);
|
|
|
|
gfxPlatform::GetPlatform()->GetPrefFonts(gfxAtoms::x_user_def, fontString, PR_FALSE);
|
|
|
|
ForEachFont(fontString, gfxAtoms::x_user_def, FontCallback, &familyArray);
|
2007-11-18 01:22:04 +03:00
|
|
|
|
2007-11-29 00:46:10 +03:00
|
|
|
// Should append some default font if there are no available fonts.
|
|
|
|
// Let's use Helv which should be available on any OS/2 system; if
|
|
|
|
// it's not there, Fontconfig replaces it with something else...
|
2009-01-18 23:14:14 +03:00
|
|
|
if (familyArray.Length() == 0) {
|
|
|
|
familyArray.AppendElement(NS_LITERAL_STRING("Helv"));
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
2007-11-18 01:22:04 +03:00
|
|
|
|
2009-01-18 23:14:14 +03:00
|
|
|
for (PRUint32 i = 0; i < familyArray.Length(); i++) {
|
|
|
|
nsRefPtr<gfxOS2Font> font = gfxOS2Font::GetOrMakeFont(familyArray[i], &mStyle);
|
2007-12-04 01:31:03 +03:00
|
|
|
if (font) {
|
|
|
|
mFonts.AppendElement(font);
|
|
|
|
}
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
gfxOS2FontGroup::~gfxOS2FontGroup()
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf("gfxOS2FontGroup[%#x]::~gfxOS2FontGroup()\n", (unsigned)this);
|
2007-02-24 13:58:48 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
gfxFontGroup *gfxOS2FontGroup::Copy(const gfxFontStyle *aStyle)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2008-10-01 07:01:53 +04:00
|
|
|
return new gfxOS2FontGroup(mFamilies, aStyle, mUserFontSet);
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
/**
|
|
|
|
* We use this to append an LTR or RTL Override character to the start of the
|
|
|
|
* string. This forces Pango to honour our direction even if there are neutral
|
|
|
|
* characters in the string.
|
|
|
|
*/
|
|
|
|
static PRInt32 AppendDirectionalIndicatorUTF8(PRBool aIsRTL, nsACString& aString)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
static const PRUnichar overrides[2][2] = { { 0x202d, 0 }, { 0x202e, 0 }}; // LRO, RLO
|
|
|
|
AppendUTF16toUTF8(overrides[aIsRTL], aString);
|
|
|
|
return 3; // both overrides map to 3 bytes in UTF8
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
gfxTextRun *gfxOS2FontGroup::MakeTextRun(const PRUnichar* aString, PRUint32 aLength,
|
|
|
|
const Parameters* aParams, PRUint32 aFlags)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2009-04-10 05:33:02 +04:00
|
|
|
NS_ASSERTION(aLength > 0, "should use MakeEmptyTextRun for zero-length text");
|
2007-11-16 23:50:06 +03:00
|
|
|
gfxTextRun *textRun = gfxTextRun::Create(aParams, aString, aLength, this, aFlags);
|
2007-05-20 17:45:49 +04:00
|
|
|
if (!textRun)
|
|
|
|
return nsnull;
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-12-11 23:51:07 +03:00
|
|
|
mEnableKerning = !(aFlags & gfxTextRunFactory::TEXT_OPTIMIZE_SPEED);
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
nsCAutoString utf8;
|
|
|
|
PRInt32 headerLen = AppendDirectionalIndicatorUTF8(textRun->IsRightToLeft(), utf8);
|
|
|
|
AppendUTF16toUTF8(Substring(aString, aString + aLength), utf8);
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
2007-08-19 16:05:23 +04:00
|
|
|
NS_ConvertUTF8toUTF16 u16(utf8);
|
|
|
|
printf("gfxOS2FontGroup[%#x]::MakeTextRun(PRUnichar %s, %d, %#x, %d)\n",
|
|
|
|
(unsigned)this, NS_LossyConvertUTF16toASCII(u16).get(), aLength, (unsigned)aParams, aFlags);
|
2007-02-24 13:58:48 +03:00
|
|
|
#endif
|
2007-08-19 16:05:23 +04:00
|
|
|
|
|
|
|
InitTextRun(textRun, (PRUint8 *)utf8.get(), utf8.Length(), headerLen);
|
|
|
|
|
2007-09-27 20:41:46 +04:00
|
|
|
textRun->FetchGlyphExtents(aParams->mContext);
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
return textRun;
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
gfxTextRun *gfxOS2FontGroup::MakeTextRun(const PRUint8* aString, PRUint32 aLength,
|
|
|
|
const Parameters* aParams, PRUint32 aFlags)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
2007-08-19 16:05:23 +04:00
|
|
|
const char *cStr = reinterpret_cast<const char *>(aString);
|
|
|
|
NS_ConvertASCIItoUTF16 us(cStr, aLength);
|
|
|
|
printf("gfxOS2FontGroup[%#x]::MakeTextRun(PRUint8 %s, %d, %#x, %d)\n",
|
|
|
|
(unsigned)this, NS_LossyConvertUTF16toASCII(us).get(), aLength, (unsigned)aParams, aFlags);
|
2007-02-24 13:58:48 +03:00
|
|
|
#endif
|
2009-04-10 05:33:02 +04:00
|
|
|
NS_ASSERTION(aLength > 0, "should use MakeEmptyTextRun for zero-length text");
|
2007-07-01 02:19:14 +04:00
|
|
|
NS_ASSERTION(aFlags & TEXT_IS_8BIT, "8bit should have been set");
|
2007-11-16 23:50:06 +03:00
|
|
|
gfxTextRun *textRun = gfxTextRun::Create(aParams, aString, aLength, this, aFlags);
|
2007-05-20 17:45:49 +04:00
|
|
|
if (!textRun)
|
|
|
|
return nsnull;
|
|
|
|
|
2007-12-11 23:51:07 +03:00
|
|
|
mEnableKerning = !(aFlags & gfxTextRunFactory::TEXT_OPTIMIZE_SPEED);
|
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
const char *chars = reinterpret_cast<const char *>(aString);
|
2007-05-20 17:45:49 +04:00
|
|
|
PRBool isRTL = textRun->IsRightToLeft();
|
2007-07-01 02:19:14 +04:00
|
|
|
if ((aFlags & TEXT_IS_ASCII) && !isRTL) {
|
|
|
|
// We don't need to send an override character here, the characters must be all
|
|
|
|
// LTR
|
|
|
|
InitTextRun(textRun, (PRUint8 *)chars, aLength, 0);
|
2007-02-24 13:58:48 +03:00
|
|
|
} else {
|
2007-07-01 02:19:14 +04:00
|
|
|
// Although chars in not necessarily ASCII (as it may point to the low
|
|
|
|
// bytes of any UCS-2 characters < 256), NS_ConvertASCIItoUTF16 seems
|
|
|
|
// to DTRT.
|
|
|
|
NS_ConvertASCIItoUTF16 unicodeString(chars, aLength);
|
2007-05-20 17:45:49 +04:00
|
|
|
nsCAutoString utf8;
|
|
|
|
PRInt32 headerLen = AppendDirectionalIndicatorUTF8(isRTL, utf8);
|
|
|
|
AppendUTF16toUTF8(unicodeString, utf8);
|
2007-07-01 02:19:14 +04:00
|
|
|
InitTextRun(textRun, (PRUint8 *)utf8.get(), utf8.Length(), headerLen);
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-09-27 20:41:46 +04:00
|
|
|
textRun->FetchGlyphExtents(aParams->mContext);
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
return textRun;
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
void gfxOS2FontGroup::InitTextRun(gfxTextRun *aTextRun, const PRUint8 *aUTF8Text,
|
|
|
|
PRUint32 aUTF8Length,
|
2007-07-01 02:19:14 +04:00
|
|
|
PRUint32 aUTF8HeaderLength)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
CreateGlyphRunsFT(aTextRun, aUTF8Text + aUTF8HeaderLength,
|
|
|
|
aUTF8Length - aUTF8HeaderLength);
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04: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)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
*aLength = 1;
|
|
|
|
if (aString[0] < 0x80) { // normal 7bit ASCII char
|
|
|
|
return aString[0];
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
if ((aString[0] >> 5) == 6) { // two leading ones -> two bytes
|
|
|
|
*aLength = 2;
|
|
|
|
return ((aString[0] & 0x1F) << 6) + (aString[1] & 0x3F);
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
if ((aString[0] >> 4) == 14) { // three leading ones -> three bytes
|
|
|
|
*aLength = 3;
|
2007-11-17 22:58:50 +03:00
|
|
|
return ((aString[0] & 0x0F) << 12) + ((aString[1] & 0x3F) << 6) +
|
2007-05-20 17:45:49 +04:00
|
|
|
(aString[2] & 0x3F);
|
|
|
|
}
|
|
|
|
if ((aString[0] >> 4) == 15) { // four leading ones -> four bytes
|
|
|
|
*aLength = 4;
|
2007-11-17 22:58:50 +03:00
|
|
|
return ((aString[0] & 0x07) << 18) + ((aString[1] & 0x3F) << 12) +
|
2007-05-20 17:45:49 +04:00
|
|
|
((aString[2] & 0x3F) << 6) + (aString[3] & 0x3F);
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
return aString[0];
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
void gfxOS2FontGroup::CreateGlyphRunsFT(gfxTextRun *aTextRun, const PRUint8 *aUTF8,
|
|
|
|
PRUint32 aUTF8Length)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf("gfxOS2FontGroup::CreateGlyphRunsFT(%#x, _aUTF8_, %d)\n",
|
|
|
|
(unsigned)aTextRun, /*aUTF8,*/ aUTF8Length);
|
2007-08-19 16:05:23 +04:00
|
|
|
for (PRUint32 i = 0; i < FontListLength(); i++) {
|
|
|
|
gfxOS2Font *font = GetFontAt(i);
|
|
|
|
printf(" i=%d, name=%s, size=%f\n", i, NS_LossyConvertUTF16toASCII(font->GetName()).get(),
|
|
|
|
font->GetStyle()->size);
|
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
#endif
|
2008-07-20 22:51:49 +04:00
|
|
|
PRUint32 lastFont = FontListLength()-1;
|
2007-11-18 01:22:04 +03:00
|
|
|
gfxOS2Font *font0 = GetFontAt(0);
|
2007-05-20 17:45:49 +04:00
|
|
|
const PRUint8 *p = aUTF8;
|
|
|
|
PRUint32 utf16Offset = 0;
|
|
|
|
gfxTextRun::CompressedGlyph g;
|
|
|
|
const PRUint32 appUnitsPerDevUnit = aTextRun->GetAppUnitsPerDevUnit();
|
2008-07-20 22:51:49 +04:00
|
|
|
gfxOS2Platform *platform = gfxOS2Platform::GetPlatform();
|
2007-05-20 17:45:49 +04:00
|
|
|
|
2007-11-18 01:22:04 +03:00
|
|
|
aTextRun->AddGlyphRun(font0, 0);
|
|
|
|
// a textRun likely has the same font for most of the characters, so we can
|
|
|
|
// lock it before the loop for efficiency
|
|
|
|
FT_Face face0 = cairo_ft_scaled_font_lock_face(font0->CairoScaledFont());
|
2007-05-20 17:45:49 +04:00
|
|
|
while (p < aUTF8 + aUTF8Length) {
|
2007-11-18 01:22:04 +03:00
|
|
|
PRBool glyphFound = PR_FALSE;
|
2007-05-20 17:45:49 +04:00
|
|
|
// convert UTF-8 character and step to the next one in line
|
|
|
|
PRUint8 chLen;
|
|
|
|
PRUint32 ch = getUTF8CharAndNext(p, &chLen);
|
|
|
|
p += chLen; // move to next char
|
|
|
|
#ifdef DEBUG_thebes_2
|
2007-11-18 01:22:04 +03:00
|
|
|
printf("\'%c\' (%d, %#x, %s) [%#x %#x]:", (char)ch, ch, ch, ch >=0x10000 ? "non-BMP!" : "BMP", ch >=0x10000 ? H_SURROGATE(ch) : 0, ch >=0x10000 ? L_SURROGATE(ch) : 0);
|
2007-05-20 17:45:49 +04:00
|
|
|
#endif
|
|
|
|
|
2008-07-20 22:51:49 +04:00
|
|
|
if (ch == 0 || platform->noFontWithChar(ch)) {
|
|
|
|
// null bytes or missing characters cannot be displayed
|
|
|
|
aTextRun->SetMissingGlyph(utf16Offset, ch);
|
2007-05-20 17:45:49 +04:00
|
|
|
} else {
|
2007-11-18 01:22:04 +03:00
|
|
|
// Try to get a glyph from all fonts available to us.
|
|
|
|
// Once we found it in one of the fonts we quit the loop early.
|
2008-07-20 22:51:49 +04:00
|
|
|
// If we don't find the glyph even in the last font, we will fall
|
|
|
|
// back to searching all fonts on the system and finally set the
|
|
|
|
// missing glyph symbol after trying the last font.
|
|
|
|
for (PRUint32 i = 0; i <= lastFont; i++) {
|
2007-11-18 01:22:04 +03:00
|
|
|
gfxOS2Font *font = font0;
|
|
|
|
FT_Face face = face0;
|
|
|
|
if (i > 0) {
|
|
|
|
font = GetFontAt(i);
|
|
|
|
face = cairo_ft_scaled_font_lock_face(font->CairoScaledFont());
|
|
|
|
#ifdef DEBUG_thebes_2
|
2008-07-20 22:51:49 +04:00
|
|
|
if (i == lastFont) {
|
2007-11-18 01:22:04 +03:00
|
|
|
printf("Last font %d (%s) for ch=%#x (pos=%d)",
|
|
|
|
i, NS_LossyConvertUTF16toASCII(font->GetName()).get(), ch, utf16Offset);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2008-07-20 22:51:49 +04:00
|
|
|
if (!face || !face->charmap) { // don't try to use fonts with non-Unicode charmaps
|
|
|
|
if (face && face != face0)
|
|
|
|
cairo_ft_scaled_font_unlock_face(font->CairoScaledFont());
|
|
|
|
continue; // next font
|
|
|
|
}
|
2007-11-18 01:22:04 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(!IsInvalidChar(ch), "Invalid char detected");
|
|
|
|
FT_UInt gid = FT_Get_Char_Index(face, ch); // find the glyph id
|
2008-07-20 22:51:49 +04:00
|
|
|
|
|
|
|
if (gid == 0 && i == lastFont) {
|
|
|
|
// missing glyph, try to find a replacement in another font
|
|
|
|
nsRefPtr<gfxOS2Font> fontX = platform->FindFontForChar(ch, font0);
|
|
|
|
if (fontX) {
|
|
|
|
font = fontX; // replace current font
|
|
|
|
cairo_ft_scaled_font_unlock_face(font->CairoScaledFont());
|
|
|
|
face = cairo_ft_scaled_font_lock_face(fontX->CairoScaledFont());
|
|
|
|
gid = FT_Get_Char_Index(face, ch);
|
|
|
|
// likely to find more chars in this font, append it
|
|
|
|
// to the font list to find it quicker next time
|
|
|
|
mFonts.AppendElement(fontX);
|
|
|
|
lastFont = FontListLength()-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// select the current font into the text run
|
|
|
|
aTextRun->AddGlyphRun(font, utf16Offset);
|
|
|
|
|
2007-11-18 01:22:04 +03:00
|
|
|
PRInt32 advance = 0;
|
|
|
|
if (gid == font->GetSpaceGlyph()) {
|
|
|
|
advance = (int)(font->GetMetrics().spaceWidth * appUnitsPerDevUnit);
|
|
|
|
} else if (gid == 0) {
|
|
|
|
advance = -1; // trigger the missing glyphs case below
|
|
|
|
} else {
|
2007-12-11 23:51:07 +03:00
|
|
|
// 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;
|
|
|
|
#ifdef DEBUG_thebes_2
|
|
|
|
printf("(kerning=%s/%s)", mEnableKerning ? "enable" : "disable", FT_HAS_KERNING(face) ? "yes" : "no");
|
|
|
|
#endif
|
|
|
|
if (mEnableKerning && FT_HAS_KERNING(face) && p < aUTF8 + aUTF8Length) {
|
|
|
|
chNext = getUTF8CharAndNext(p, &chLen);
|
|
|
|
if (chNext) {
|
|
|
|
gidNext = FT_Get_Char_Index(face, chNext);
|
|
|
|
if (gidNext && gidNext != font->GetSpaceGlyph()) {
|
|
|
|
FT_Load_Glyph(face, gidNext, FT_LOAD_DEFAULT);
|
|
|
|
lsbDeltaNext = face->glyph->lsb_delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now load the current glyph
|
2007-11-18 01:22:04 +03:00
|
|
|
FT_Load_Glyph(face, gid, FT_LOAD_DEFAULT); // load glyph into the slot
|
2007-12-11 23:51:07 +03:00
|
|
|
advance = face->glyph->advance.x;
|
|
|
|
|
|
|
|
// now add kerning to the current glyph's advance
|
|
|
|
if (chNext && gidNext) {
|
|
|
|
FT_Vector kerning;
|
|
|
|
FT_Get_Kerning(face, gid, gidNext, FT_KERNING_DEFAULT, &kerning);
|
|
|
|
advance += kerning.x;
|
|
|
|
if (face->glyph->rsb_delta - lsbDeltaNext >= 32) {
|
|
|
|
advance -= 64;
|
|
|
|
} else if (face->glyph->rsb_delta - lsbDeltaNext < -32) {
|
|
|
|
advance += 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now apply unit conversion and scaling
|
|
|
|
advance = (advance >> 6) * appUnitsPerDevUnit;
|
2007-11-18 01:22:04 +03:00
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
#ifdef DEBUG_thebes_2
|
2007-11-18 01:22:04 +03:00
|
|
|
printf(" gid=%d, advance=%d (%s)\n", gid, advance,
|
|
|
|
NS_LossyConvertUTF16toASCII(font->GetName()).get());
|
2007-05-20 17:45:49 +04:00
|
|
|
#endif
|
2007-08-19 16:05:23 +04:00
|
|
|
|
2007-11-18 01:22:04 +03:00
|
|
|
if (advance >= 0 &&
|
|
|
|
gfxTextRun::CompressedGlyph::IsSimpleAdvance(advance) &&
|
|
|
|
gfxTextRun::CompressedGlyph::IsSimpleGlyphID(gid))
|
|
|
|
{
|
|
|
|
aTextRun->SetSimpleGlyph(utf16Offset,
|
|
|
|
g.SetSimpleGlyph(advance, gid));
|
|
|
|
glyphFound = PR_TRUE;
|
|
|
|
} else if (gid == 0) {
|
|
|
|
// gid = 0 only happens when the glyph is missing from the font
|
2008-07-20 22:51:49 +04:00
|
|
|
if (i == lastFont) {
|
2007-11-18 01:22:04 +03:00
|
|
|
// set the missing glyph only when it's missing from the very
|
|
|
|
// last font
|
|
|
|
aTextRun->SetMissingGlyph(utf16Offset, ch);
|
|
|
|
}
|
|
|
|
glyphFound = PR_FALSE;
|
|
|
|
} 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;
|
|
|
|
g.SetComplex(aTextRun->IsClusterStart(utf16Offset), PR_TRUE, 1);
|
|
|
|
aTextRun->SetGlyphs(utf16Offset, g, &details);
|
|
|
|
glyphFound = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
cairo_ft_scaled_font_unlock_face(font->CairoScaledFont());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glyphFound) {
|
|
|
|
break;
|
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
}
|
2007-11-18 01:22:04 +03:00
|
|
|
} // for all fonts
|
2007-02-24 13:58:48 +03:00
|
|
|
|
2007-11-18 01:22:04 +03:00
|
|
|
NS_ASSERTION(!IS_SURROGATE(ch), "Surrogates shouldn't appear in UTF8");
|
|
|
|
if (ch >= 0x10000) {
|
|
|
|
// This character is a surrogate pair in UTF16
|
|
|
|
++utf16Offset;
|
2007-05-20 17:45:49 +04:00
|
|
|
}
|
2007-11-18 01:22:04 +03:00
|
|
|
|
2007-05-20 17:45:49 +04:00
|
|
|
++utf16Offset;
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
2007-11-18 01:22:04 +03:00
|
|
|
cairo_ft_scaled_font_unlock_face(font0->CairoScaledFont());
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
|
|
|
|
2008-12-02 22:36:12 +03:00
|
|
|
// append aFontName to aClosure string array, if not already present
|
2007-05-20 17:45:49 +04:00
|
|
|
PRBool gfxOS2FontGroup::FontCallback(const nsAString& aFontName,
|
|
|
|
const nsACString& aGenericName,
|
|
|
|
void *aClosure)
|
2007-02-24 13:58:48 +03:00
|
|
|
{
|
2009-01-18 23:14:14 +03:00
|
|
|
nsTArray<nsString> *sa = static_cast<nsTArray<nsString>*>(aClosure);
|
|
|
|
if (!aFontName.IsEmpty() && !sa->Contains(aFontName)) {
|
|
|
|
sa->AppendElement(aFontName);
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|
2007-05-20 17:45:49 +04:00
|
|
|
return PR_TRUE;
|
2007-02-24 13:58:48 +03:00
|
|
|
}
|