summaryrefslogtreecommitdiffstats
path: root/gfx/2d/ScaledFontBase.cpp
blob: d62dcb0127ac3aff5daad38646817f93fb59a885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* -*- 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
 * 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/. */

#include "ScaledFontBase.h"

#include "PathSkia.h"
#include "skia/include/core/SkFont.h"

#ifdef USE_CAIRO
#  include "PathCairo.h"
#  include "DrawTargetCairo.h"
#  include "HelpersCairo.h"
#endif

#include <vector>
#include <cmath>

namespace mozilla {
namespace gfx {

Atomic<uint32_t> UnscaledFont::sDeletionCounter(0);

UnscaledFont::~UnscaledFont() { sDeletionCounter++; }

Atomic<uint32_t> ScaledFont::sDeletionCounter(0);

ScaledFont::~ScaledFont() { sDeletionCounter++; }

ScaledFontBase::~ScaledFontBase() {
  SkSafeUnref<SkTypeface>(mTypeface);
  cairo_scaled_font_destroy(mScaledFont);
}

ScaledFontBase::ScaledFontBase(const RefPtr<UnscaledFont>& aUnscaledFont,
                               Float aSize)
    : ScaledFont(aUnscaledFont),
      mTypeface(nullptr),
      mScaledFont(nullptr),
      mSize(aSize) {}

SkTypeface* ScaledFontBase::GetSkTypeface() {
  if (!mTypeface) {
    SkTypeface* typeface = CreateSkTypeface();
    if (!mTypeface.compareExchange(nullptr, typeface)) {
      SkSafeUnref(typeface);
    }
  }
  return mTypeface;
}

cairo_scaled_font_t* ScaledFontBase::GetCairoScaledFont() {
  if (mScaledFont) {
    return mScaledFont;
  }

  cairo_font_options_t* fontOptions = cairo_font_options_create();
  cairo_font_face_t* fontFace = CreateCairoFontFace(fontOptions);
  if (!fontFace) {
    cairo_font_options_destroy(fontOptions);
    return nullptr;
  }

  cairo_matrix_t sizeMatrix;
  cairo_matrix_t identityMatrix;

  cairo_matrix_init_scale(&sizeMatrix, mSize, mSize);
  cairo_matrix_init_identity(&identityMatrix);

  cairo_scaled_font_t* scaledFont = cairo_scaled_font_create(
      fontFace, &sizeMatrix, &identityMatrix, fontOptions);

  cairo_font_options_destroy(fontOptions);
  cairo_font_face_destroy(fontFace);

  if (cairo_scaled_font_status(scaledFont) != CAIRO_STATUS_SUCCESS) {
    cairo_scaled_font_destroy(scaledFont);
    return nullptr;
  }

  PrepareCairoScaledFont(scaledFont);
  mScaledFont = scaledFont;
  return mScaledFont;
}

SkPath ScaledFontBase::GetSkiaPathForGlyphs(const GlyphBuffer& aBuffer) {
  SkTypeface* typeFace = GetSkTypeface();
  MOZ_ASSERT(typeFace);

  SkFont font(sk_ref_sp(typeFace), SkFloatToScalar(mSize));

  std::vector<uint16_t> indices;
  indices.resize(aBuffer.mNumGlyphs);
  for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
    indices[i] = aBuffer.mGlyphs[i].mIndex;
  }

  struct Context {
    const Glyph* mGlyph;
    SkPath mPath;
  } ctx = {aBuffer.mGlyphs};

  font.getPaths(
      indices.data(), indices.size(),
      [](const SkPath* glyphPath, const SkMatrix& scaleMatrix, void* ctxPtr) {
        Context& ctx = *reinterpret_cast<Context*>(ctxPtr);
        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;
}

already_AddRefed<Path> ScaledFontBase::GetPathForGlyphs(
    const GlyphBuffer& aBuffer, const DrawTarget* aTarget) {
  if (aTarget->GetBackendType() == BackendType::SKIA) {
    SkPath path = GetSkiaPathForGlyphs(aBuffer);
    return MakeAndAddRef<PathSkia>(path, FillRule::FILL_WINDING);
  }
#ifdef USE_CAIRO
  if (aTarget->GetBackendType() == BackendType::CAIRO) {
    MOZ_ASSERT(mScaledFont);

    DrawTarget* dt = const_cast<DrawTarget*>(aTarget);
    cairo_t* ctx = static_cast<cairo_t*>(
        dt->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT));

    bool isNewContext = !ctx;
    if (!ctx) {
      ctx = cairo_create(DrawTargetCairo::GetDummySurface());
      cairo_matrix_t mat;
      GfxMatrixToCairoMatrix(aTarget->GetTransform(), mat);
      cairo_set_matrix(ctx, &mat);
    }

    cairo_set_scaled_font(ctx, mScaledFont);

    // 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;
    }

    cairo_new_path(ctx);

    cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs);

    RefPtr<PathCairo> newPath = new PathCairo(ctx);
    if (isNewContext) {
      cairo_destroy(ctx);
    }

    return newPath.forget();
  }
#endif
  RefPtr<PathBuilder> builder = aTarget->CreatePathBuilder();
  SkPath skPath = GetSkiaPathForGlyphs(aBuffer);
  RefPtr<Path> path = MakeAndAddRef<PathSkia>(skPath, FillRule::FILL_WINDING);
  path->StreamToSink(builder);
  return builder->Finish();
}

void ScaledFontBase::CopyGlyphsToBuilder(const GlyphBuffer& aBuffer,
                                         PathBuilder* aBuilder,
                                         const Matrix* aTransformHint) {
  BackendType backendType = aBuilder->GetBackendType();
  if (backendType == BackendType::SKIA) {
    PathBuilderSkia* builder = static_cast<PathBuilderSkia*>(aBuilder);
    builder->AppendPath(GetSkiaPathForGlyphs(aBuffer));
    return;
  }
#ifdef USE_CAIRO
  if (backendType == BackendType::CAIRO) {
    MOZ_ASSERT(mScaledFont);

    PathBuilderCairo* builder = static_cast<PathBuilderCairo*>(aBuilder);
    cairo_t* ctx = cairo_create(DrawTargetCairo::GetDummySurface());

    if (aTransformHint) {
      cairo_matrix_t mat;
      GfxMatrixToCairoMatrix(*aTransformHint, mat);
      cairo_set_matrix(ctx, &mat);
    }

    // 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;
    }

    cairo_set_scaled_font(ctx, mScaledFont);
    cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs);

    RefPtr<PathCairo> cairoPath = new PathCairo(ctx);
    cairo_destroy(ctx);

    cairoPath->AppendPathToBuilder(builder);
    return;
  }
#endif
  if (backendType == BackendType::RECORDING) {
    SkPath skPath = GetSkiaPathForGlyphs(aBuffer);
    RefPtr<Path> path = MakeAndAddRef<PathSkia>(skPath, FillRule::FILL_WINDING);
    path->StreamToSink(aBuilder);
    return;
  }
  MOZ_ASSERT(false, "Path not being copied");
}

}  // namespace gfx
}  // namespace mozilla