Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / WebFontImpl.cpp
blob3486dd1cc676846fffb1539787858ed82870eb84
1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #include "config.h"
32 #include "web/WebFontImpl.h"
34 #include "platform/fonts/FontCache.h"
35 #include "platform/fonts/FontDescription.h"
36 #include "platform/graphics/GraphicsContext.h"
37 #include "platform/graphics/paint/DrawingRecorder.h"
38 #include "platform/graphics/paint/SkPictureBuilder.h"
39 #include "platform/text/TextRun.h"
40 #include "public/platform/WebFloatPoint.h"
41 #include "public/platform/WebFloatRect.h"
42 #include "public/platform/WebRect.h"
43 #include "public/web/WebFontDescription.h"
44 #include "public/web/WebTextRun.h"
46 namespace blink {
48 WebFont* WebFont::create(const WebFontDescription& desc)
50 return new WebFontImpl(desc);
53 WebFontImpl::WebFontImpl(const FontDescription& desc)
54 : m_font(desc)
56 m_font.update(nullptr);
59 WebFontDescription WebFontImpl::fontDescription() const
61 return WebFontDescription(m_font.fontDescription());
64 int WebFontImpl::ascent() const
66 return m_font.fontMetrics().ascent();
69 int WebFontImpl::descent() const
71 return m_font.fontMetrics().descent();
74 int WebFontImpl::height() const
76 return m_font.fontMetrics().height();
79 int WebFontImpl::lineSpacing() const
81 return m_font.fontMetrics().lineSpacing();
84 float WebFontImpl::xHeight() const
86 return m_font.fontMetrics().xHeight();
89 void WebFontImpl::drawText(WebCanvas* canvas, const WebTextRun& run, const WebFloatPoint& leftBaseline,
90 WebColor color, const WebRect& clip) const
92 FontCachePurgePreventer fontCachePurgePreventer;
93 FloatRect textClipRect(clip);
94 TextRun textRun(run);
95 TextRunPaintInfo runInfo(textRun);
96 runInfo.bounds = textClipRect;
98 IntRect intRect(clip);
99 SkPictureBuilder pictureBuilder(intRect);
100 GraphicsContext* context = &pictureBuilder.context();
102 ASSERT(!DrawingRecorder::useCachedDrawingIfPossible(*context, *this, DisplayItem::WebFont));
104 DrawingRecorder drawingRecorder(*context, *this, DisplayItem::WebFont, intRect);
105 context->save();
106 context->setFillColor(color);
107 context->clip(textClipRect);
108 context->drawText(m_font, runInfo, leftBaseline);
109 context->restore();
112 pictureBuilder.endRecording()->playback(canvas);
115 int WebFontImpl::calculateWidth(const WebTextRun& run) const
117 return m_font.width(run, 0);
120 int WebFontImpl::offsetForPosition(const WebTextRun& run, float position) const
122 return m_font.offsetForPosition(run, position, true);
125 WebFloatRect WebFontImpl::selectionRectForText(const WebTextRun& run, const WebFloatPoint& leftBaseline, int height, int from, int to) const
127 return m_font.selectionRectForText(run, leftBaseline, height, from, to);
130 } // namespace blink