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