Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / WebFontImpl.cpp
1 /*
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 "web/WebFontImpl.h"
33
34 #include "platform/fonts/FontCache.h"
35 #include "platform/fonts/FontDescription.h"
36 #include "platform/graphics/GraphicsContext.h"
37 #include "platform/text/TextRun.h"
38 #include "public/platform/WebFloatPoint.h"
39 #include "public/platform/WebFloatRect.h"
40 #include "public/platform/WebRect.h"
41 #include "public/web/WebFontDescription.h"
42 #include "public/web/WebTextRun.h"
43 #include <skia/ext/platform_canvas.h>
44
45 namespace blink {
46
47 WebFont* WebFont::create(const WebFontDescription& desc)
48 {
49     return new WebFontImpl(desc);
50 }
51
52 WebFontImpl::WebFontImpl(const FontDescription& desc)
53     : m_font(desc)
54 {
55     m_font.update(nullptr);
56 }
57
58 WebFontDescription WebFontImpl::fontDescription() const
59 {
60     return WebFontDescription(m_font.fontDescription());
61 }
62
63 int WebFontImpl::ascent() const
64 {
65     return m_font.fontMetrics().ascent();
66 }
67
68 int WebFontImpl::descent() const
69 {
70     return m_font.fontMetrics().descent();
71 }
72
73 int WebFontImpl::height() const
74 {
75     return m_font.fontMetrics().height();
76 }
77
78 int WebFontImpl::lineSpacing() const
79 {
80     return m_font.fontMetrics().lineSpacing();
81 }
82
83 float WebFontImpl::xHeight() const
84 {
85     return m_font.fontMetrics().xHeight();
86 }
87
88 void WebFontImpl::drawText(WebCanvas* canvas, const WebTextRun& run, const WebFloatPoint& leftBaseline,
89                            WebColor color, const WebRect& clip, bool canvasIsOpaque,
90                            int from, int to) const
91 {
92     FontCachePurgePreventer fontCachePurgePreventer;
93     FloatRect textClipRect(clip);
94     TextRun textRun(run);
95     TextRunPaintInfo runInfo(textRun);
96     runInfo.from = from;
97     runInfo.to = to == -1 ? textRun.length() : to;
98     runInfo.bounds = textClipRect;
99     GraphicsContext gc(canvas);
100
101     gc.save();
102     gc.setCertainlyOpaque(canvasIsOpaque);
103     gc.setFillColor(color);
104     gc.clip(textClipRect);
105     m_font.drawText(&gc, runInfo, leftBaseline);
106     gc.restore();
107 }
108
109 int WebFontImpl::calculateWidth(const WebTextRun& run) const
110 {
111     FontCachePurgePreventer fontCachePurgePreventer;
112     return m_font.width(run, 0);
113 }
114
115 int WebFontImpl::offsetForPosition(const WebTextRun& run, float position) const
116 {
117     FontCachePurgePreventer fontCachePurgePreventer;
118     return m_font.offsetForPosition(run, position, true);
119 }
120
121 WebFloatRect WebFontImpl::selectionRectForText(const WebTextRun& run, const WebFloatPoint& leftBaseline, int height, int from, int to) const
122 {
123     FontCachePurgePreventer fontCachePurgePreventer;
124     return m_font.selectionRectForText(run, leftBaseline, height, from, to);
125 }
126
127 } // namespace blink