Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Side by Side Diff: ui/gfx/render_text.cc

Issue 11299262: ui: Use skia::RefPtr<T> for implicit safe reference counting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: winbuild Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/platform_font_pango.cc ('k') | ui/gfx/skbitmap_operations.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/render_text.h" 5 #include "ui/gfx/render_text.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/i18n/break_iterator.h" 9 #include "base/i18n/break_iterator.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 colors->push_back(c0); 136 colors->push_back(c0);
137 } 137 }
138 positions->push_back(p0); 138 positions->push_back(p0);
139 colors->push_back(c0); 139 colors->push_back(c0);
140 positions->push_back(p1); 140 positions->push_back(p1);
141 colors->push_back(c1); 141 colors->push_back(c1);
142 } 142 }
143 143
144 // Creates a SkShader to fade the text, with |left_part| specifying the left 144 // Creates a SkShader to fade the text, with |left_part| specifying the left
145 // fade effect, if any, and |right_part| specifying the right fade effect. 145 // fade effect, if any, and |right_part| specifying the right fade effect.
146 SkShader* CreateFadeShader(const gfx::Rect& text_rect, 146 skia::RefPtr<SkShader> CreateFadeShader(const gfx::Rect& text_rect,
147 const gfx::Rect& left_part, 147 const gfx::Rect& left_part,
148 const gfx::Rect& right_part, 148 const gfx::Rect& right_part,
149 SkColor color) { 149 SkColor color) {
150 // Fade alpha of 51/255 corresponds to a fade of 0.2 of the original color. 150 // Fade alpha of 51/255 corresponds to a fade of 0.2 of the original color.
151 const SkColor fade_color = SkColorSetA(color, 51); 151 const SkColor fade_color = SkColorSetA(color, 51);
152 std::vector<SkScalar> positions; 152 std::vector<SkScalar> positions;
153 std::vector<SkColor> colors; 153 std::vector<SkColor> colors;
154 154
155 if (!left_part.IsEmpty()) 155 if (!left_part.IsEmpty())
156 AddFadeEffect(text_rect, left_part, fade_color, color, 156 AddFadeEffect(text_rect, left_part, fade_color, color,
157 &positions, &colors); 157 &positions, &colors);
158 if (!right_part.IsEmpty()) 158 if (!right_part.IsEmpty())
159 AddFadeEffect(text_rect, right_part, color, fade_color, 159 AddFadeEffect(text_rect, right_part, color, fade_color,
160 &positions, &colors); 160 &positions, &colors);
161 DCHECK(!positions.empty()); 161 DCHECK(!positions.empty());
162 162
163 // Terminate |positions| with 1.0, as required by Skia. 163 // Terminate |positions| with 1.0, as required by Skia.
164 if (positions.back() != 1.0) { 164 if (positions.back() != 1.0) {
165 positions.push_back(1.0); 165 positions.push_back(1.0);
166 colors.push_back(colors.back()); 166 colors.push_back(colors.back());
167 } 167 }
168 168
169 SkPoint points[2]; 169 SkPoint points[2];
170 points[0].iset(text_rect.x(), text_rect.y()); 170 points[0].iset(text_rect.x(), text_rect.y());
171 points[1].iset(text_rect.right(), text_rect.y()); 171 points[1].iset(text_rect.right(), text_rect.y());
172 172
173 return SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0], 173 return skia::AdoptRef(
174 colors.size(), SkShader::kClamp_TileMode); 174 SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0],
175 colors.size(), SkShader::kClamp_TileMode));
175 } 176 }
176 177
177 } // namespace 178 } // namespace
178 179
179 namespace gfx { 180 namespace gfx {
180 181
181 namespace internal { 182 namespace internal {
182 183
183 // Value of |underline_thickness_| that indicates that underline metrics have 184 // Value of |underline_thickness_| that indicates that underline metrics have
184 // not been set explicitly. 185 // not been set explicitly.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 231
231 void SkiaTextRenderer::SetTextSize(SkScalar size) { 232 void SkiaTextRenderer::SetTextSize(SkScalar size) {
232 paint_.setTextSize(size); 233 paint_.setTextSize(size);
233 } 234 }
234 235
235 void SkiaTextRenderer::SetFontFamilyWithStyle(const std::string& family, 236 void SkiaTextRenderer::SetFontFamilyWithStyle(const std::string& family,
236 int style) { 237 int style) {
237 DCHECK(!family.empty()); 238 DCHECK(!family.empty());
238 239
239 SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style); 240 SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style);
240 SkTypeface* typeface = SkTypeface::CreateFromName(family.c_str(), skia_style); 241 skia::RefPtr<SkTypeface> typeface =
241 SkAutoUnref auto_unref(typeface); 242 skia::AdoptRef(SkTypeface::CreateFromName(family.c_str(), skia_style));
242 if (typeface) { 243 if (typeface) {
243 // |paint_| adds its own ref. So don't |release()| it from the ref ptr here. 244 // |paint_| adds its own ref. So don't |release()| it from the ref ptr here.
244 SetTypeface(typeface); 245 SetTypeface(typeface.get());
245 246
246 // Enable fake bold text if bold style is needed but new typeface does not 247 // Enable fake bold text if bold style is needed but new typeface does not
247 // have it. 248 // have it.
248 paint_.setFakeBoldText((skia_style & SkTypeface::kBold) && 249 paint_.setFakeBoldText((skia_style & SkTypeface::kBold) &&
249 !typeface->isBold()); 250 !typeface->isBold());
250 } 251 }
251 } 252 }
252 253
253 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { 254 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) {
254 paint_.setColor(foreground); 255 paint_.setColor(foreground);
(...skipping 654 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 if (fade_right) { 910 if (fade_right) {
910 right_part = solid_part; 911 right_part = solid_part;
911 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); 912 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0);
912 solid_part.Inset(0, 0, gradient_width, 0); 913 solid_part.Inset(0, 0, gradient_width, 0);
913 } 914 }
914 915
915 gfx::Rect text_rect = display_rect(); 916 gfx::Rect text_rect = display_rect();
916 text_rect.Inset(GetAlignmentOffset().x(), 0, 0, 0); 917 text_rect.Inset(GetAlignmentOffset().x(), 0, 0, 0);
917 918
918 const SkColor color = default_style().foreground; 919 const SkColor color = default_style().foreground;
919 SkShader* shader = CreateFadeShader(text_rect, left_part, right_part, color); 920 skia::RefPtr<SkShader> shader =
920 SkAutoUnref auto_unref(shader); 921 CreateFadeShader(text_rect, left_part, right_part, color);
921 if (shader) { 922 if (shader)
922 // |renderer| adds its own ref. So don't |release()| it from the ref ptr. 923 renderer->SetShader(shader.get(), display_rect());
923 renderer->SetShader(shader, display_rect());
924 }
925 } 924 }
926 925
927 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { 926 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) {
928 SkDrawLooper* looper = gfx::CreateShadowDrawLooper(text_shadows_); 927 skia::RefPtr<SkDrawLooper> looper =
929 SkAutoUnref auto_unref(looper); 928 gfx::CreateShadowDrawLooper(text_shadows_);
930 renderer->SetDrawLooper(looper); 929 renderer->SetDrawLooper(looper.get());
931 } 930 }
932 931
933 // static 932 // static
934 bool RenderText::RangeContainsCaret(const ui::Range& range, 933 bool RenderText::RangeContainsCaret(const ui::Range& range,
935 size_t caret_pos, 934 size_t caret_pos,
936 LogicalCursorDirection caret_affinity) { 935 LogicalCursorDirection caret_affinity) {
937 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). 936 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9).
938 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? 937 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ?
939 caret_pos - 1 : caret_pos + 1; 938 caret_pos - 1 : caret_pos + 1;
940 return range.Contains(ui::Range(caret_pos, adjacent)); 939 return range.Contains(ui::Range(caret_pos, adjacent));
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 if (cursor_enabled() && cursor_visible() && focused()) { 1018 if (cursor_enabled() && cursor_visible() && focused()) {
1020 const Rect& bounds = GetUpdatedCursorBounds(); 1019 const Rect& bounds = GetUpdatedCursorBounds();
1021 if (bounds.width() != 0) 1020 if (bounds.width() != 0)
1022 canvas->FillRect(bounds, cursor_color_); 1021 canvas->FillRect(bounds, cursor_color_);
1023 else 1022 else
1024 canvas->DrawRect(bounds, cursor_color_); 1023 canvas->DrawRect(bounds, cursor_color_);
1025 } 1024 }
1026 } 1025 }
1027 1026
1028 } // namespace gfx 1027 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/platform_font_pango.cc ('k') | ui/gfx/skbitmap_operations.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698