• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "WebFontImpl.h"
33 
34 #include "Font.h"
35 #include "FontDescription.h"
36 #include "GraphicsContext.h"
37 #include "painting/GraphicsContextBuilder.h"
38 #include "TextRun.h"
39 #include "WebFloatPoint.h"
40 #include "WebFloatRect.h"
41 #include "WebFontDescription.h"
42 #include "WebRect.h"
43 #include "WebTextRun.h"
44 
45 using namespace WebCore;
46 
47 namespace WebKit {
48 
create(const WebFontDescription & desc)49 WebFont* WebFont::create(const WebFontDescription& desc)
50 {
51     return new WebFontImpl(desc, desc.letterSpacing, desc.wordSpacing);
52 }
53 
WebFontImpl(const FontDescription & desc,short letterSpacing,short wordSpacing)54 WebFontImpl::WebFontImpl(const FontDescription& desc, short letterSpacing, short wordSpacing)
55     : m_font(desc, letterSpacing, wordSpacing)
56 {
57     m_font.update(0);
58 }
59 
fontDescription() const60 WebFontDescription WebFontImpl::fontDescription() const
61 {
62     return WebFontDescription(m_font.fontDescription(), m_font.letterSpacing(), m_font.wordSpacing());
63 }
64 
ascent() const65 int WebFontImpl::ascent() const
66 {
67     return m_font.fontMetrics().ascent();
68 }
69 
descent() const70 int WebFontImpl::descent() const
71 {
72     return m_font.fontMetrics().descent();
73 }
74 
height() const75 int WebFontImpl::height() const
76 {
77     return m_font.fontMetrics().height();
78 }
79 
lineSpacing() const80 int WebFontImpl::lineSpacing() const
81 {
82     return m_font.fontMetrics().lineSpacing();
83 }
84 
xHeight() const85 float WebFontImpl::xHeight() const
86 {
87     return m_font.fontMetrics().xHeight();
88 }
89 
drawText(WebCanvas * canvas,const WebTextRun & run,const WebFloatPoint & leftBaseline,WebColor color,const WebRect & clip,bool canvasIsOpaque,int from,int to) const90 void 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     // FIXME hook canvasIsOpaque up to the platform-specific indicators for
95     // whether subpixel AA can be used for this draw. On Windows, this is
96     // PlatformContextSkia::setDrawingToImageBuffer.
97 
98     GraphicsContextBuilder builder(canvas);
99     GraphicsContext& gc = builder.context();
100 
101     gc.save();
102     gc.setFillColor(color, ColorSpaceDeviceRGB);
103     gc.clip(WebCore::FloatRect(clip));
104     m_font.drawText(&gc, run, leftBaseline, from, to);
105     gc.restore();
106 }
107 
calculateWidth(const WebTextRun & run) const108 int WebFontImpl::calculateWidth(const WebTextRun& run) const
109 {
110     return m_font.width(run, 0);
111 }
112 
offsetForPosition(const WebTextRun & run,float position) const113 int WebFontImpl::offsetForPosition(const WebTextRun& run, float position) const
114 {
115     return m_font.offsetForPosition(run, position, true);
116 }
117 
selectionRectForText(const WebTextRun & run,const WebFloatPoint & leftBaseline,int height,int from,int to) const118 WebFloatRect WebFontImpl::selectionRectForText(const WebTextRun& run, const WebFloatPoint& leftBaseline, int height, int from, int to) const
119 {
120     return m_font.selectionRectForText(run, leftBaseline, height, from, to);
121 }
122 
123 } // namespace WebKit
124