2011-11-02 19:55:03 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00: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 19:55:03 +00:00
|
|
|
|
2012-01-09 18:54:44 +00:00
|
|
|
#include "ScaledFontBase.h"
|
2012-01-27 18:08:46 +00:00
|
|
|
|
2012-01-09 18:54:44 +00:00
|
|
|
#ifdef USE_SKIA
|
2011-11-02 19:55:03 +00:00
|
|
|
#include "PathSkia.h"
|
2015-07-29 20:31:40 +00:00
|
|
|
#include "skia/include/core/SkPaint.h"
|
2012-01-09 18:54:44 +00:00
|
|
|
#endif
|
2012-01-27 18:08:46 +00:00
|
|
|
|
|
|
|
#ifdef USE_CAIRO
|
|
|
|
#include "PathCairo.h"
|
2013-09-28 14:20:24 +00:00
|
|
|
#include "DrawTargetCairo.h"
|
|
|
|
#include "HelpersCairo.h"
|
2012-01-27 18:08:46 +00:00
|
|
|
#endif
|
|
|
|
|
2011-11-02 19:55:03 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <cmath>
|
2012-01-27 18:08:46 +00:00
|
|
|
|
2011-11-02 19:55:03 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
2012-01-27 18:08:46 +00:00
|
|
|
|
2012-01-09 18:54:44 +00:00
|
|
|
ScaledFontBase::~ScaledFontBase()
|
2011-11-02 19:55:03 +00:00
|
|
|
{
|
2012-01-09 18:54:44 +00:00
|
|
|
#ifdef USE_SKIA
|
|
|
|
SkSafeUnref(mTypeface);
|
|
|
|
#endif
|
2013-06-05 17:48:59 +00:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
2012-01-27 18:08:46 +00:00
|
|
|
cairo_scaled_font_destroy(mScaledFont);
|
|
|
|
#endif
|
2011-11-02 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 18:54:44 +00:00
|
|
|
ScaledFontBase::ScaledFontBase(Float aSize)
|
|
|
|
: mSize(aSize)
|
2011-11-02 19:55:03 +00:00
|
|
|
{
|
2012-01-09 18:54:44 +00:00
|
|
|
#ifdef USE_SKIA
|
2012-08-14 18:06:12 +00:00
|
|
|
mTypeface = nullptr;
|
2012-01-09 18:54:44 +00:00
|
|
|
#endif
|
2013-06-05 17:48:59 +00:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
2012-08-14 18:06:12 +00:00
|
|
|
mScaledFont = nullptr;
|
2012-01-27 18:08:46 +00:00
|
|
|
#endif
|
2011-11-02 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 10:08:56 +00: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-12 22:37:00 +00:00
|
|
|
#ifdef USE_SKIA
|
|
|
|
SkPath
|
|
|
|
ScaledFontBase::GetSkiaPathForGlyphs(const GlyphBuffer &aBuffer)
|
|
|
|
{
|
|
|
|
SkTypeface *typeFace = GetSkTypeface();
|
|
|
|
MOZ_ASSERT(typeFace);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-10-25 02:40:59 +00:00
|
|
|
paint.setTypeface(sk_ref_sp(typeFace));
|
2013-12-12 22:37:00 +00: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 14:00:52 +00:00
|
|
|
already_AddRefed<Path>
|
2012-01-09 18:54:44 +00:00
|
|
|
ScaledFontBase::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget)
|
2011-11-02 19:55:03 +00:00
|
|
|
{
|
2012-01-09 18:54:44 +00:00
|
|
|
#ifdef USE_SKIA
|
2014-06-19 20:35:33 +00:00
|
|
|
if (aTarget->GetBackendType() == BackendType::SKIA) {
|
2013-12-12 22:37:00 +00:00
|
|
|
SkPath path = GetSkiaPathForGlyphs(aBuffer);
|
2015-04-30 19:20:30 +00:00
|
|
|
return MakeAndAddRef<PathSkia>(path, FillRule::FILL_WINDING);
|
2012-01-09 18:54:44 +00:00
|
|
|
}
|
2012-01-27 18:08:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_CAIRO
|
2014-06-19 20:35:33 +00:00
|
|
|
if (aTarget->GetBackendType() == BackendType::CAIRO) {
|
2012-01-27 18:08:46 +00:00
|
|
|
MOZ_ASSERT(mScaledFont);
|
|
|
|
|
2013-09-28 14:20:24 +00:00
|
|
|
DrawTarget *dt = const_cast<DrawTarget*>(aTarget);
|
2014-01-10 19:06:16 +00:00
|
|
|
cairo_t *ctx = static_cast<cairo_t*>(dt->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT));
|
2012-01-27 18:08:46 +00:00
|
|
|
|
2013-09-28 14:20:24 +00: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 18:08:46 +00:00
|
|
|
|
2013-09-28 14:20:24 +00:00
|
|
|
cairo_set_scaled_font(ctx, mScaledFont);
|
2012-01-27 18:08:46 +00: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-26 22:27:05 +00:00
|
|
|
cairo_new_path(ctx);
|
|
|
|
|
2013-09-28 14:20:24 +00:00
|
|
|
cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<PathCairo> newPath = new PathCairo(ctx);
|
2013-09-28 14:20:24 +00:00
|
|
|
if (isNewContext) {
|
|
|
|
cairo_destroy(ctx);
|
|
|
|
}
|
2012-01-27 18:08:46 +00:00
|
|
|
|
2014-06-13 16:09:23 +00:00
|
|
|
return newPath.forget();
|
2012-01-27 18:08:46 +00:00
|
|
|
}
|
2012-01-09 18:54:44 +00:00
|
|
|
#endif
|
2012-08-14 18:06:12 +00:00
|
|
|
return nullptr;
|
2011-11-02 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2012-03-29 18:53:44 +00:00
|
|
|
void
|
2016-10-26 17:46:00 +00:00
|
|
|
ScaledFontBase::CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder, const Matrix *aTransformHint)
|
2012-03-29 18:53:44 +00:00
|
|
|
{
|
2016-10-26 17:46:00 +00:00
|
|
|
BackendType backendType = aBuilder->GetBackendType();
|
2013-12-12 22:37:00 +00:00
|
|
|
#ifdef USE_SKIA
|
2016-10-26 17:46:00 +00:00
|
|
|
if (backendType == BackendType::SKIA) {
|
2013-12-12 22:37:00 +00:00
|
|
|
PathBuilderSkia *builder = static_cast<PathBuilderSkia*>(aBuilder);
|
|
|
|
builder->AppendPath(GetSkiaPathForGlyphs(aBuffer));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-08-20 23:58:27 +00:00
|
|
|
#ifdef USE_CAIRO
|
2016-10-26 17:46:00 +00:00
|
|
|
if (backendType == BackendType::CAIRO) {
|
2013-12-12 22:37:00 +00:00
|
|
|
MOZ_ASSERT(mScaledFont);
|
2013-08-20 23:58:27 +00:00
|
|
|
|
2013-12-12 22:37:00 +00:00
|
|
|
PathBuilderCairo* builder = static_cast<PathBuilderCairo*>(aBuilder);
|
|
|
|
cairo_t *ctx = cairo_create(DrawTargetCairo::GetDummySurface());
|
2013-08-20 23:58:27 +00:00
|
|
|
|
2013-12-12 22:37:00 +00:00
|
|
|
if (aTransformHint) {
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
GfxMatrixToCairoMatrix(*aTransformHint, mat);
|
|
|
|
cairo_set_matrix(ctx, &mat);
|
|
|
|
}
|
2013-08-20 23:58:27 +00:00
|
|
|
|
2013-12-12 22:37:00 +00: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-20 23:58:27 +00:00
|
|
|
|
2013-12-12 22:37:00 +00:00
|
|
|
cairo_set_scaled_font(ctx, mScaledFont);
|
|
|
|
cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs);
|
2013-09-28 14:20:24 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<PathCairo> cairoPath = new PathCairo(ctx);
|
2013-12-12 22:37:00 +00:00
|
|
|
cairo_destroy(ctx);
|
2013-09-28 14:20:24 +00:00
|
|
|
|
2013-12-12 22:37:00 +00:00
|
|
|
cairoPath->AppendPathToBuilder(builder);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-20 23:58:27 +00:00
|
|
|
#endif
|
2012-03-29 18:53:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 23:49:00 +00: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 02:32:09 +00:00
|
|
|
#if defined(MOZ2D_HAS_MOZ_CAIRO) && defined(CAIRO_HAS_DWRITE_FONT)
|
2016-08-09 23:49:00 +00: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 11:46:12 +00:00
|
|
|
cairo_font_options_destroy(options);
|
2016-08-09 23:49:00 +00: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 17:48:59 +00:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
2012-01-27 18:08:46 +00:00
|
|
|
void
|
|
|
|
ScaledFontBase::SetCairoScaledFont(cairo_scaled_font_t* font)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mScaledFont);
|
|
|
|
|
2013-06-29 02:50:59 +00:00
|
|
|
if (font == mScaledFont)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mScaledFont)
|
|
|
|
cairo_scaled_font_destroy(mScaledFont);
|
|
|
|
|
2012-01-27 18:08:46 +00:00
|
|
|
mScaledFont = font;
|
|
|
|
cairo_scaled_font_reference(mScaledFont);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|