1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Matt Woodrow <mwoodrow@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "ScaledFontBase.h"
39 :
40 : #include "gfxFont.h"
41 :
42 : #ifdef USE_SKIA
43 : #include "PathSkia.h"
44 : #include "skia/SkPaint.h"
45 : #include "skia/SkPath.h"
46 : #endif
47 :
48 : #ifdef USE_CAIRO
49 : #include "PathCairo.h"
50 : #endif
51 :
52 : #include <vector>
53 : #include <cmath>
54 :
55 : using namespace std;
56 :
57 : namespace mozilla {
58 : namespace gfx {
59 :
60 0 : ScaledFontBase::~ScaledFontBase()
61 : {
62 : #ifdef USE_SKIA
63 0 : SkSafeUnref(mTypeface);
64 : #endif
65 : #ifdef USE_CAIRO
66 0 : cairo_scaled_font_destroy(mScaledFont);
67 : #endif
68 0 : }
69 :
70 0 : ScaledFontBase::ScaledFontBase(Float aSize)
71 0 : : mSize(aSize)
72 : {
73 : #ifdef USE_SKIA
74 0 : mTypeface = NULL;
75 : #endif
76 : #ifdef USE_CAIRO
77 0 : mScaledFont = NULL;
78 : #endif
79 0 : }
80 :
81 : TemporaryRef<Path>
82 0 : ScaledFontBase::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget)
83 : {
84 : #ifdef USE_SKIA
85 0 : if (aTarget->GetType() == BACKEND_SKIA) {
86 0 : SkPaint paint;
87 0 : paint.setTypeface(GetSkTypeface());
88 0 : paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
89 0 : paint.setTextSize(SkFloatToScalar(mSize));
90 :
91 0 : std::vector<uint16_t> indices;
92 0 : std::vector<SkPoint> offsets;
93 0 : indices.resize(aBuffer.mNumGlyphs);
94 0 : offsets.resize(aBuffer.mNumGlyphs);
95 :
96 0 : for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
97 0 : indices[i] = aBuffer.mGlyphs[i].mIndex;
98 0 : offsets[i].fX = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.x);
99 0 : offsets[i].fY = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.y);
100 : }
101 :
102 0 : SkPath path;
103 0 : paint.getPosTextPath(&indices.front(), aBuffer.mNumGlyphs*2, &offsets.front(), &path);
104 0 : return new PathSkia(path, FILL_WINDING);
105 : }
106 : #endif
107 : #ifdef USE_CAIRO
108 0 : if (aTarget->GetType() == BACKEND_CAIRO) {
109 0 : MOZ_ASSERT(mScaledFont);
110 :
111 0 : RefPtr<PathBuilder> builder_iface = aTarget->CreatePathBuilder();
112 0 : PathBuilderCairo* builder = static_cast<PathBuilderCairo*>(builder_iface.get());
113 :
114 : // Manually build the path for the PathBuilder.
115 0 : RefPtr<CairoPathContext> context = builder->GetPathContext();
116 :
117 0 : cairo_set_scaled_font(*context, mScaledFont);
118 :
119 : // Convert our GlyphBuffer into an array of Cairo glyphs.
120 0 : std::vector<cairo_glyph_t> glyphs(aBuffer.mNumGlyphs);
121 0 : for (uint32_t i = 0; i < aBuffer.mNumGlyphs; ++i) {
122 0 : glyphs[i].index = aBuffer.mGlyphs[i].mIndex;
123 0 : glyphs[i].x = aBuffer.mGlyphs[i].mPosition.x;
124 0 : glyphs[i].y = aBuffer.mGlyphs[i].mPosition.y;
125 : }
126 :
127 0 : cairo_glyph_path(*context, &glyphs[0], aBuffer.mNumGlyphs);
128 :
129 0 : return builder->Finish();
130 : }
131 : #endif
132 0 : return NULL;
133 : }
134 :
135 : #ifdef USE_CAIRO
136 : void
137 0 : ScaledFontBase::SetCairoScaledFont(cairo_scaled_font_t* font)
138 : {
139 0 : MOZ_ASSERT(!mScaledFont);
140 :
141 0 : mScaledFont = font;
142 0 : cairo_scaled_font_reference(mScaledFont);
143 0 : }
144 : #endif
145 :
146 : }
147 : }
|