2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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
|
|
|
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_gfx.h"
|
2017-02-10 07:42:14 +03:00
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
#ifdef USE_SKIA
|
2011-11-02 23:55:03 +04:00
|
|
|
# include "PathSkia.h"
|
2019-03-26 18:06:56 +03:00
|
|
|
# include "skia/include/core/SkFont.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() {
|
2019-07-22 05:10:14 +03:00
|
|
|
if (StaticPrefs::gfx_text_disable_aa_AtStartup()) {
|
2017-02-10 07:42:14 +03:00
|
|
|
return AntialiasMode::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AntialiasMode::DEFAULT;
|
|
|
|
}
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
ScaledFontBase::~ScaledFontBase() {
|
|
|
|
#ifdef USE_SKIA
|
2018-08-23 21:58:21 +03:00
|
|
|
SkSafeUnref<SkTypeface>(mTypeface);
|
2012-01-09 22:54:44 +04:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
ScaledFontBase::ScaledFontBase(const RefPtr<UnscaledFont>& aUnscaledFont,
|
2017-04-07 00:41:02 +03:00
|
|
|
Float aSize)
|
|
|
|
: ScaledFont(aUnscaledFont)
|
2012-01-09 22:54:44 +04:00
|
|
|
#ifdef USE_SKIA
|
2018-08-23 21:58:21 +03: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
|
2018-08-23 21:58:21 +03:00
|
|
|
,
|
|
|
|
mScaledFont(nullptr)
|
2012-01-27 22:08:46 +04:00
|
|
|
#endif
|
2018-08-23 21:58:21 +03:00
|
|
|
,
|
|
|
|
mSize(aSize) {
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_SKIA
|
2019-05-01 11:47:10 +03:00
|
|
|
SkTypeface* ScaledFontBase::GetSkTypeface() {
|
2018-08-23 21:58:21 +03:00
|
|
|
if (!mTypeface) {
|
2019-05-01 11:47:10 +03:00
|
|
|
SkTypeface* typeface = CreateSkTypeface();
|
2018-08-23 21:58:21 +03:00
|
|
|
if (!mTypeface.compareExchange(nullptr, typeface)) {
|
|
|
|
SkSafeUnref(typeface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mTypeface;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
2018-08-23 21:58:21 +03: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() {
|
2019-05-01 11:47:10 +03:00
|
|
|
cairo_font_face_t* cairoFontFace = GetCairoFontFace();
|
2016-01-05 13:08:56 +03:00
|
|
|
if (!cairoFontFace) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_matrix_t sizeMatrix;
|
|
|
|
cairo_matrix_t identityMatrix;
|
|
|
|
|
|
|
|
cairo_matrix_init_scale(&sizeMatrix, mSize, mSize);
|
|
|
|
cairo_matrix_init_identity(&identityMatrix);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
cairo_font_options_t* fontOptions = cairo_font_options_create();
|
2016-01-05 13:08:56 +03:00
|
|
|
|
|
|
|
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
|
2019-05-01 11:47:10 +03:00
|
|
|
SkPath ScaledFontBase::GetSkiaPathForGlyphs(const GlyphBuffer& aBuffer) {
|
|
|
|
SkTypeface* typeFace = GetSkTypeface();
|
2013-12-13 02:37:00 +04:00
|
|
|
MOZ_ASSERT(typeFace);
|
|
|
|
|
2019-03-26 18:06:56 +03:00
|
|
|
SkFont font(sk_ref_sp(typeFace), SkFloatToScalar(mSize));
|
2013-12-13 02:37:00 +04:00
|
|
|
|
|
|
|
std::vector<uint16_t> indices;
|
|
|
|
indices.resize(aBuffer.mNumGlyphs);
|
|
|
|
for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
|
|
|
|
indices[i] = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:06:56 +03:00
|
|
|
struct Context {
|
2019-05-01 11:47:10 +03:00
|
|
|
const Glyph* mGlyph;
|
2019-03-26 18:06:56 +03:00
|
|
|
SkPath mPath;
|
|
|
|
} ctx = {aBuffer.mGlyphs};
|
2019-03-31 18:12:55 +03:00
|
|
|
|
2019-03-26 18:06:56 +03:00
|
|
|
font.getPaths(
|
|
|
|
indices.data(), indices.size(),
|
2019-05-01 11:47:10 +03:00
|
|
|
[](const SkPath* glyphPath, const SkMatrix& scaleMatrix, void* ctxPtr) {
|
|
|
|
Context& ctx = *reinterpret_cast<Context*>(ctxPtr);
|
2019-03-26 18:06:56 +03:00
|
|
|
if (glyphPath) {
|
|
|
|
SkMatrix transMatrix(scaleMatrix);
|
|
|
|
transMatrix.postTranslate(SkFloatToScalar(ctx.mGlyph->mPosition.x),
|
|
|
|
SkFloatToScalar(ctx.mGlyph->mPosition.y));
|
|
|
|
ctx.mPath.addPath(*glyphPath, transMatrix);
|
|
|
|
}
|
|
|
|
++ctx.mGlyph;
|
|
|
|
},
|
|
|
|
&ctx);
|
|
|
|
|
|
|
|
return ctx.mPath;
|
2013-12-13 02:37:00 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
already_AddRefed<Path> ScaledFontBase::GetPathForGlyphs(
|
2019-05-01 11:47:10 +03:00
|
|
|
const GlyphBuffer& aBuffer, const DrawTarget* aTarget) {
|
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);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
DrawTarget* dt = const_cast<DrawTarget*>(aTarget);
|
|
|
|
cairo_t* ctx = static_cast<cairo_t*>(
|
2014-01-10 23:06:16 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void ScaledFontBase::CopyGlyphsToBuilder(const GlyphBuffer& aBuffer,
|
|
|
|
PathBuilder* aBuilder,
|
|
|
|
const Matrix* aTransformHint) {
|
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) {
|
2019-05-01 11:47:10 +03:00
|
|
|
PathBuilderSkia* builder = static_cast<PathBuilderSkia*>(aBuilder);
|
2013-12-13 02:37:00 +04:00
|
|
|
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
|
|
|
|
2019-05-01 11:47:10 +03: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
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void ScaledFontBase::GetGlyphDesignMetrics(const uint16_t* aGlyphs,
|
2016-08-10 02:49:00 +03:00
|
|
|
uint32_t aNumGlyphs,
|
2019-05-01 11:47:10 +03:00
|
|
|
GlyphMetrics* aGlyphMetrics) {
|
2016-08-10 02:49:00 +03:00
|
|
|
#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;
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
cairo_font_options_t* options = cairo_font_options_create();
|
2016-08-10 02:49:00 +03:00
|
|
|
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
|
|
|
}
|
2017-11-23 22:39:11 +03:00
|
|
|
return;
|
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
|
2019-05-01 11:47:10 +03:00
|
|
|
void ScaledFontBase::SetCairoScaledFont(cairo_scaled_font_t* font) {
|
2012-01-27 22:08:46 +04:00
|
|
|
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
|