1/*
2 * Copyright (C) 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
4 * Copyright (C) 2007, 2008 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
6 * Copyright (C) 2010 Holger Hans Peter Freyther
7 * Copyright (C) 2014 Igalia S.L.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "FontCascade.h"
33
34#if USE(CAIRO)
35
36#include "AffineTransform.h"
37#include "CairoOperations.h"
38#include "CairoUtilities.h"
39#include "Font.h"
40#include "GlyphBuffer.h"
41#include "Gradient.h"
42#include "GraphicsContext.h"
43#include "ImageBuffer.h"
44#include "Pattern.h"
45#include "PlatformContextCairo.h"
46#include "PlatformPathCairo.h"
47#include "ShadowBlur.h"
48
49namespace WebCore {
50
51void FontCascade::drawGlyphs(GraphicsContext& context, const Font& font, const GlyphBuffer& glyphBuffer,
52 unsigned from, unsigned numGlyphs, const FloatPoint& point, FontSmoothingMode)
53{
54 if (!font.platformData().size())
55 return;
56
57 auto xOffset = point.x();
58 Vector<cairo_glyph_t> glyphs(numGlyphs);
59 {
60 ASSERT(from + numGlyphs <= glyphBuffer.size());
61 auto* glyphsData = glyphBuffer.glyphs(from);
62 auto* advances = glyphBuffer.advances(from);
63
64 auto yOffset = point.y();
65 for (size_t i = 0; i < numGlyphs; ++i) {
66 glyphs[i] = { glyphsData[i], xOffset, yOffset };
67 xOffset += advances[i].width();
68 yOffset -= advances[i].height();
69 }
70 }
71
72 cairo_scaled_font_t* scaledFont = font.platformData().scaledFont();
73 double syntheticBoldOffset = font.syntheticBoldOffset();
74
75 ASSERT(context.hasPlatformContext());
76 auto& state = context.state();
77 Cairo::drawGlyphs(*context.platformContext(), Cairo::FillSource(state), Cairo::StrokeSource(state),
78 Cairo::ShadowState(state), point, scaledFont, syntheticBoldOffset, glyphs, xOffset,
79 state.textDrawingMode, state.strokeThickness, state.shadowOffset, state.shadowColor);
80}
81
82Path Font::platformPathForGlyph(Glyph glyph) const
83{
84 Path path;
85 path.ensurePlatformPath();
86
87 cairo_glyph_t cairoGlyph = { glyph, 0, 0 };
88 cairo_set_scaled_font(path.platformPath()->context(), platformData().scaledFont());
89 cairo_glyph_path(path.platformPath()->context(), &cairoGlyph, 1);
90
91 float syntheticBoldOffset = this->syntheticBoldOffset();
92 if (syntheticBoldOffset) {
93 cairo_translate(path.platformPath()->context(), syntheticBoldOffset, 0);
94 cairo_glyph_path(path.platformPath()->context(), &cairoGlyph, 1);
95 }
96 return path;
97}
98
99} // namespace WebCore
100
101#endif // USE(CAIRO)
102