blob: f6bffae825a0186a4c6aa1d3477d821790165a88 [file] [log] [blame]
Ben Murdoche69819b2013-07-17 14:56:49 +01001/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY 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 "WebFontImpl.h"
33
34#include <skia/ext/platform_canvas.h>
35#include "WebFontDescription.h"
36#include "WebTextRun.h"
Torne (Richard Coles)a854de02013-12-18 16:25:25 +000037#include "platform/fonts/FontCache.h"
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000038#include "platform/fonts/FontDescription.h"
Torne (Richard Coles)a854de02013-12-18 16:25:25 +000039#include "platform/graphics/GraphicsContext.h"
40#include "platform/text/TextRun.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010041#include "public/platform/WebFloatPoint.h"
42#include "public/platform/WebFloatRect.h"
43#include "public/platform/WebRect.h"
44
45using namespace WebCore;
46
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000047namespace blink {
Ben Murdoche69819b2013-07-17 14:56:49 +010048
49WebFont* WebFont::create(const WebFontDescription& desc)
50{
Torne (Richard Coles)09380292014-02-21 12:17:33 +000051 return new WebFontImpl(desc);
Ben Murdoche69819b2013-07-17 14:56:49 +010052}
53
Torne (Richard Coles)09380292014-02-21 12:17:33 +000054WebFontImpl::WebFontImpl(const FontDescription& desc)
55 : m_font(desc)
Ben Murdoche69819b2013-07-17 14:56:49 +010056{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000057 m_font.update(nullptr);
Ben Murdoche69819b2013-07-17 14:56:49 +010058}
59
60WebFontDescription WebFontImpl::fontDescription() const
61{
Torne (Richard Coles)09380292014-02-21 12:17:33 +000062 return WebFontDescription(m_font.fontDescription());
Ben Murdoche69819b2013-07-17 14:56:49 +010063}
64
65int WebFontImpl::ascent() const
66{
67 return m_font.fontMetrics().ascent();
68}
69
70int WebFontImpl::descent() const
71{
72 return m_font.fontMetrics().descent();
73}
74
75int WebFontImpl::height() const
76{
77 return m_font.fontMetrics().height();
78}
79
80int WebFontImpl::lineSpacing() const
81{
82 return m_font.fontMetrics().lineSpacing();
83}
84
85float WebFontImpl::xHeight() const
86{
87 return m_font.fontMetrics().xHeight();
88}
89
90void WebFontImpl::drawText(WebCanvas* canvas, const WebTextRun& run, const WebFloatPoint& leftBaseline,
91 WebColor color, const WebRect& clip, bool canvasIsOpaque,
92 int from, int to) const
93{
94 FontCachePurgePreventer fontCachePurgePreventer;
95 WebCore::FloatRect textClipRect(clip);
96 TextRun textRun(run);
97 TextRunPaintInfo runInfo(textRun);
98 runInfo.from = from;
99 runInfo.to = to == -1 ? textRun.length() : to;
100 runInfo.bounds = textClipRect;
101 GraphicsContext gc(canvas);
102
103 gc.save();
104 gc.setCertainlyOpaque(canvasIsOpaque);
105 gc.setFillColor(color);
106 gc.clip(textClipRect);
107 m_font.drawText(&gc, runInfo, leftBaseline);
108 gc.restore();
Ben Murdoche69819b2013-07-17 14:56:49 +0100109}
110
111int WebFontImpl::calculateWidth(const WebTextRun& run) const
112{
113 FontCachePurgePreventer fontCachePurgePreventer;
114 return m_font.width(run, 0);
115}
116
117int WebFontImpl::offsetForPosition(const WebTextRun& run, float position) const
118{
119 FontCachePurgePreventer fontCachePurgePreventer;
120 return m_font.offsetForPosition(run, position, true);
121}
122
123WebFloatRect WebFontImpl::selectionRectForText(const WebTextRun& run, const WebFloatPoint& leftBaseline, int height, int from, int to) const
124{
125 FontCachePurgePreventer fontCachePurgePreventer;
126 return m_font.selectionRectForText(run, leftBaseline, height, from, to);
127}
128
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000129} // namespace blink