2011-11-02 23:55:03 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
#include "ScaledFontBase.h"
|
2012-01-27 22:08:46 +04:00
|
|
|
|
2017-02-10 07:42:14 +03:00
|
|
|
#include "gfxPrefs.h"
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
#ifdef USE_SKIA
|
2011-11-02 23:55:03 +04:00
|
|
|
#include "PathSkia.h"
|
2015-07-29 23:31:40 +03:00
|
|
|
#include "skia/include/core/SkPaint.h"
|
2012-01-09 22:54:44 +04:00
|
|
|
#endif
|
2012-01-27 22:08:46 +04:00
|
|
|
|
|
|
|
#ifdef USE_CAIRO
|
|
|
|
#include "PathCairo.h"
|
2013-09-28 18:20:24 +04:00
|
|
|
#include "DrawTargetCairo.h"
|
|
|
|
#include "HelpersCairo.h"
|
2012-01-27 22:08:46 +04:00
|
|
|
#endif
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
#include <vector>
|
|
|
|
#include <cmath>
|
2012-01-27 22:08:46 +04:00
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
2012-01-27 22:08:46 +04:00
|
|
|
|
2017-08-30 20:45:11 +03:00
|
|
|
Atomic<uint32_t> UnscaledFont::sDeletionCounter(0);
|
2017-04-07 00:41:02 +03:00
|
|
|
|
|
|
|
UnscaledFont::~UnscaledFont()
|
|
|
|
{
|
|
|
|
sDeletionCounter++;
|
|
|
|
}
|
|
|
|
|
2017-08-30 20:45:11 +03:00
|
|
|
Atomic<uint32_t> ScaledFont::sDeletionCounter(0);
|
|
|
|
|
|
|
|
ScaledFont::~ScaledFont()
|
|
|
|
{
|
|
|
|
sDeletionCounter++;
|
|
|
|
}
|
|
|
|
|
2017-02-10 07:42:14 +03:00
|
|
|
AntialiasMode
|
|
|
|
ScaledFont::GetDefaultAAMode()
|
|
|
|
{
|
|
|
|
if (gfxPrefs::DisableAllTextAA()) {
|
|
|
|
return AntialiasMode::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AntialiasMode::DEFAULT;
|
|
|
|
}
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
ScaledFontBase::~ScaledFontBase()
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
2012-01-09 22:54:44 +04:00
|
|
|
#ifdef USE_SKIA
|
|
|
|
SkSafeUnref(mTypeface);
|
|
|
|
#endif
|
2013-06-05 21:48:59 +04:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
2012-01-27 22:08:46 +04:00
|
|
|
cairo_scaled_font_destroy(mScaledFont);
|
|
|
|
#endif
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2017-04-07 00:41:02 +03:00
|
|
|
ScaledFontBase::ScaledFontBase(const RefPtr<UnscaledFont>& aUnscaledFont,
|
|
|
|
Float aSize)
|
|
|
|
: ScaledFont(aUnscaledFont)
|
|
|
|
, mSize(aSize)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
2012-01-09 22:54:44 +04:00
|
|
|
#ifdef USE_SKIA
|
2012-08-14 22:06:12 +04:00
|
|
|
mTypeface = nullptr;
|
2012-01-09 22:54:44 +04:00
|
|
|
#endif
|
2013-06-05 21:48:59 +04:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
2012-08-14 22:06:12 +04:00
|
|
|
mScaledFont = nullptr;
|
2012-01-27 22:08:46 +04:00
|
|
|
#endif
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:56 +03:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
|
|
|
bool
|
|
|
|
ScaledFontBase::PopulateCairoScaledFont()
|
|
|
|
{
|
|
|
|
cairo_font_face_t* cairoFontFace = GetCairoFontFace();
|
|
|
|
if (!cairoFontFace) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_matrix_t sizeMatrix;
|
|
|
|
cairo_matrix_t identityMatrix;
|
|
|
|
|
|
|
|
cairo_matrix_init_scale(&sizeMatrix, mSize, mSize);
|
|
|
|
cairo_matrix_init_identity(&identityMatrix);
|
|
|
|
|
|
|
|
cairo_font_options_t *fontOptions = cairo_font_options_create();
|
|
|
|
|
|
|
|
mScaledFont = cairo_scaled_font_create(cairoFontFace, &sizeMatrix,
|
|
|
|
&identityMatrix, fontOptions);
|
|
|
|
|
|
|
|
cairo_font_options_destroy(fontOptions);
|
|
|
|
cairo_font_face_destroy(cairoFontFace);
|
|
|
|
|
|
|
|
return (cairo_scaled_font_status(mScaledFont) == CAIRO_STATUS_SUCCESS);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-13 02:37:00 +04:00
|
|
|
#ifdef USE_SKIA
|
|
|
|
SkPath
|
|
|
|
ScaledFontBase::GetSkiaPathForGlyphs(const GlyphBuffer &aBuffer)
|
|
|
|
{
|
|
|
|
SkTypeface *typeFace = GetSkTypeface();
|
|
|
|
MOZ_ASSERT(typeFace);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-10-25 05:40:59 +03:00
|
|
|
paint.setTypeface(sk_ref_sp(typeFace));
|
2013-12-13 02:37:00 +04:00
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
|
|
|
paint.setTextSize(SkFloatToScalar(mSize));
|
|
|
|
|
|
|
|
std::vector<uint16_t> indices;
|
|
|
|
std::vector<SkPoint> offsets;
|
|
|
|
indices.resize(aBuffer.mNumGlyphs);
|
|
|
|
offsets.resize(aBuffer.mNumGlyphs);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
|
|
|
|
indices[i] = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
offsets[i].fX = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.x);
|
|
|
|
offsets[i].fY = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
paint.getPosTextPath(&indices.front(), aBuffer.mNumGlyphs*2, &offsets.front(), &path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<Path>
|
2012-01-09 22:54:44 +04:00
|
|
|
ScaledFontBase::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
2012-01-09 22:54:44 +04:00
|
|
|
#ifdef USE_SKIA
|
2014-06-20 00:35:33 +04:00
|
|
|
if (aTarget->GetBackendType() == BackendType::SKIA) {
|
2013-12-13 02:37:00 +04:00
|
|
|
SkPath path = GetSkiaPathForGlyphs(aBuffer);
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<PathSkia>(path, FillRule::FILL_WINDING);
|
2012-01-09 22:54:44 +04:00
|
|
|
}
|
2012-01-27 22:08:46 +04:00
|
|
|
#endif
|
|
|
|
#ifdef USE_CAIRO
|
2014-06-20 00:35:33 +04:00
|
|
|
if (aTarget->GetBackendType() == BackendType::CAIRO) {
|
2012-01-27 22:08:46 +04:00
|
|
|
MOZ_ASSERT(mScaledFont);
|
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
DrawTarget *dt = const_cast<DrawTarget*>(aTarget);
|
2014-01-10 23:06:16 +04:00
|
|
|
cairo_t *ctx = static_cast<cairo_t*>(dt->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT));
|
2012-01-27 22:08:46 +04:00
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
bool isNewContext = !ctx;
|
|
|
|
if (!ctx) {
|
|
|
|
ctx = cairo_create(DrawTargetCairo::GetDummySurface());
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
GfxMatrixToCairoMatrix(aTarget->GetTransform(), mat);
|
|
|
|
cairo_set_matrix(ctx, &mat);
|
|
|
|
}
|
2012-01-27 22:08:46 +04:00
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
cairo_set_scaled_font(ctx, mScaledFont);
|
2012-01-27 22:08:46 +04:00
|
|
|
|
|
|
|
// Convert our GlyphBuffer into an array of Cairo glyphs.
|
|
|
|
std::vector<cairo_glyph_t> glyphs(aBuffer.mNumGlyphs);
|
|
|
|
for (uint32_t i = 0; i < aBuffer.mNumGlyphs; ++i) {
|
|
|
|
glyphs[i].index = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
glyphs[i].x = aBuffer.mGlyphs[i].mPosition.x;
|
|
|
|
glyphs[i].y = aBuffer.mGlyphs[i].mPosition.y;
|
|
|
|
}
|
|
|
|
|
2013-11-27 02:27:05 +04:00
|
|
|
cairo_new_path(ctx);
|
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathCairo> newPath = new PathCairo(ctx);
|
2013-09-28 18:20:24 +04:00
|
|
|
if (isNewContext) {
|
|
|
|
cairo_destroy(ctx);
|
|
|
|
}
|
2012-01-27 22:08:46 +04:00
|
|
|
|
2014-06-13 20:09:23 +04:00
|
|
|
return newPath.forget();
|
2012-01-27 22:08:46 +04:00
|
|
|
}
|
2012-01-09 22:54:44 +04:00
|
|
|
#endif
|
2017-06-02 22:03:56 +03:00
|
|
|
#ifdef USE_SKIA
|
|
|
|
RefPtr<PathBuilder> builder = aTarget->CreatePathBuilder();
|
|
|
|
SkPath skPath = GetSkiaPathForGlyphs(aBuffer);
|
|
|
|
RefPtr<Path> path = MakeAndAddRef<PathSkia>(skPath, FillRule::FILL_WINDING);
|
|
|
|
path->StreamToSink(builder);
|
|
|
|
return builder->Finish();
|
|
|
|
#endif
|
2017-08-12 07:31:21 +03:00
|
|
|
return nullptr;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2012-03-29 22:53:44 +04:00
|
|
|
void
|
2016-10-26 20:46:00 +03:00
|
|
|
ScaledFontBase::CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder, const Matrix *aTransformHint)
|
2012-03-29 22:53:44 +04:00
|
|
|
{
|
2016-10-26 20:46:00 +03:00
|
|
|
BackendType backendType = aBuilder->GetBackendType();
|
2013-12-13 02:37:00 +04:00
|
|
|
#ifdef USE_SKIA
|
2016-10-26 20:46:00 +03:00
|
|
|
if (backendType == BackendType::SKIA) {
|
2013-12-13 02:37:00 +04:00
|
|
|
PathBuilderSkia *builder = static_cast<PathBuilderSkia*>(aBuilder);
|
|
|
|
builder->AppendPath(GetSkiaPathForGlyphs(aBuffer));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-08-21 03:58:27 +04:00
|
|
|
#ifdef USE_CAIRO
|
2016-10-26 20:46:00 +03:00
|
|
|
if (backendType == BackendType::CAIRO) {
|
2013-12-13 02:37:00 +04:00
|
|
|
MOZ_ASSERT(mScaledFont);
|
2013-08-21 03:58:27 +04:00
|
|
|
|
2013-12-13 02:37:00 +04:00
|
|
|
PathBuilderCairo* builder = static_cast<PathBuilderCairo*>(aBuilder);
|
|
|
|
cairo_t *ctx = cairo_create(DrawTargetCairo::GetDummySurface());
|
2013-08-21 03:58:27 +04:00
|
|
|
|
2013-12-13 02:37:00 +04:00
|
|
|
if (aTransformHint) {
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
GfxMatrixToCairoMatrix(*aTransformHint, mat);
|
|
|
|
cairo_set_matrix(ctx, &mat);
|
|
|
|
}
|
2013-08-21 03:58:27 +04:00
|
|
|
|
2013-12-13 02:37:00 +04:00
|
|
|
// Convert our GlyphBuffer into an array of Cairo glyphs.
|
|
|
|
std::vector<cairo_glyph_t> glyphs(aBuffer.mNumGlyphs);
|
|
|
|
for (uint32_t i = 0; i < aBuffer.mNumGlyphs; ++i) {
|
|
|
|
glyphs[i].index = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
glyphs[i].x = aBuffer.mGlyphs[i].mPosition.x;
|
|
|
|
glyphs[i].y = aBuffer.mGlyphs[i].mPosition.y;
|
|
|
|
}
|
2013-08-21 03:58:27 +04:00
|
|
|
|
2013-12-13 02:37:00 +04:00
|
|
|
cairo_set_scaled_font(ctx, mScaledFont);
|
|
|
|
cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs);
|
2013-09-28 18:20:24 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathCairo> cairoPath = new PathCairo(ctx);
|
2013-12-13 02:37:00 +04:00
|
|
|
cairo_destroy(ctx);
|
2013-09-28 18:20:24 +04:00
|
|
|
|
2013-12-13 02:37:00 +04:00
|
|
|
cairoPath->AppendPathToBuilder(builder);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-12 07:31:21 +03:00
|
|
|
#endif
|
|
|
|
#ifdef USE_SKIA
|
2017-05-16 22:23:48 +03:00
|
|
|
if (backendType == BackendType::RECORDING) {
|
|
|
|
SkPath skPath = GetSkiaPathForGlyphs(aBuffer);
|
|
|
|
RefPtr<Path> path = MakeAndAddRef<PathSkia>(skPath, FillRule::FILL_WINDING);
|
|
|
|
path->StreamToSink(aBuilder);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-21 03:58:27 +04:00
|
|
|
#endif
|
2017-08-12 07:31:21 +03:00
|
|
|
MOZ_ASSERT(false, "Path not being copied");
|
2012-03-29 22:53:44 +04:00
|
|
|
}
|
|
|
|
|
2016-08-10 02:49:00 +03:00
|
|
|
void
|
|
|
|
ScaledFontBase::GetGlyphDesignMetrics(const uint16_t* aGlyphs, uint32_t aNumGlyphs, GlyphMetrics* aGlyphMetrics)
|
|
|
|
{
|
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
|
|
|
if (mScaledFont) {
|
|
|
|
for (uint32_t i = 0; i < aNumGlyphs; i++) {
|
|
|
|
cairo_glyph_t glyph;
|
|
|
|
cairo_text_extents_t extents;
|
|
|
|
glyph.index = aGlyphs[i];
|
|
|
|
glyph.x = 0;
|
|
|
|
glyph.y = 0;
|
|
|
|
|
|
|
|
cairo_scaled_font_glyph_extents(mScaledFont, &glyph, 1, &extents);
|
|
|
|
|
|
|
|
aGlyphMetrics[i].mXBearing = extents.x_bearing;
|
|
|
|
aGlyphMetrics[i].mXAdvance = extents.x_advance;
|
|
|
|
aGlyphMetrics[i].mYBearing = extents.y_bearing;
|
|
|
|
aGlyphMetrics[i].mYAdvance = extents.y_advance;
|
|
|
|
aGlyphMetrics[i].mWidth = extents.width;
|
|
|
|
aGlyphMetrics[i].mHeight = extents.height;
|
|
|
|
|
|
|
|
cairo_font_options_t *options = cairo_font_options_create();
|
|
|
|
cairo_scaled_font_get_font_options(mScaledFont, options);
|
|
|
|
|
|
|
|
if (cairo_font_options_get_antialias(options) != CAIRO_ANTIALIAS_NONE) {
|
|
|
|
if (cairo_scaled_font_get_type(mScaledFont) == CAIRO_FONT_TYPE_WIN32) {
|
|
|
|
if (aGlyphMetrics[i].mWidth > 0 && aGlyphMetrics[i].mHeight > 0) {
|
|
|
|
aGlyphMetrics[i].mWidth -= 3.0f;
|
|
|
|
aGlyphMetrics[i].mXBearing += 1.0f;
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 05:32:09 +03:00
|
|
|
#if defined(MOZ2D_HAS_MOZ_CAIRO) && defined(CAIRO_HAS_DWRITE_FONT)
|
2016-08-10 02:49:00 +03:00
|
|
|
else if (cairo_scaled_font_get_type(mScaledFont) == CAIRO_FONT_TYPE_DWRITE) {
|
|
|
|
if (aGlyphMetrics[i].mWidth > 0 && aGlyphMetrics[i].mHeight > 0) {
|
|
|
|
aGlyphMetrics[i].mWidth -= 2.0f;
|
|
|
|
aGlyphMetrics[i].mXBearing += 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2016-08-18 14:46:12 +03:00
|
|
|
cairo_font_options_destroy(options);
|
2016-08-10 02:49:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Don't know how to get the glyph metrics...
|
|
|
|
MOZ_CRASH("The specific backend type is not supported for GetGlyphDesignMetrics.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 21:48:59 +04:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
2012-01-27 22:08:46 +04:00
|
|
|
void
|
|
|
|
ScaledFontBase::SetCairoScaledFont(cairo_scaled_font_t* font)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mScaledFont);
|
|
|
|
|
2013-06-29 06:50:59 +04:00
|
|
|
if (font == mScaledFont)
|
|
|
|
return;
|
2017-02-10 07:42:14 +03:00
|
|
|
|
2013-06-29 06:50:59 +04:00
|
|
|
if (mScaledFont)
|
|
|
|
cairo_scaled_font_destroy(mScaledFont);
|
|
|
|
|
2012-01-27 22:08:46 +04:00
|
|
|
mScaledFont = font;
|
|
|
|
cairo_scaled_font_reference(mScaledFont);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|