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

Side by Side Diff: ui/gfx/canvas.h

Issue 22835002: Supports gfx::FontList in gfx::Canvas and ui::ElideText family. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updates based on review comments. Created 7 years, 4 months 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
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 #ifndef UI_GFX_CANVAS_H_ 5 #ifndef UI_GFX_CANVAS_H_
6 #define UI_GFX_CANVAS_H_ 6 #define UI_GFX_CANVAS_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
13 #include "skia/ext/platform_canvas.h" 13 #include "skia/ext/platform_canvas.h"
14 #include "skia/ext/refptr.h" 14 #include "skia/ext/refptr.h"
15 #include "ui/gfx/image/image_skia.h" 15 #include "ui/gfx/image/image_skia.h"
16 #include "ui/gfx/native_widget_types.h" 16 #include "ui/gfx/native_widget_types.h"
17 #include "ui/gfx/shadow_value.h" 17 #include "ui/gfx/shadow_value.h"
18 18
19 namespace gfx { 19 namespace gfx {
20 20
21 class Rect; 21 class Rect;
22 class Font; 22 class Font;
23 class FontList;
23 class Point; 24 class Point;
24 class Size; 25 class Size;
25 class Transform; 26 class Transform;
26 27
27 // Canvas is a SkCanvas wrapper that provides a number of methods for 28 // Canvas is a SkCanvas wrapper that provides a number of methods for
28 // common operations used throughout an application built using ui/gfx. 29 // common operations used throughout an application built using ui/gfx.
29 // 30 //
30 // All methods that take integer arguments (as is used throughout views) 31 // All methods that take integer arguments (as is used throughout views)
31 // end with Int. If you need to use methods provided by SkCanvas, you'll 32 // end with Int. If you need to use methods provided by SkCanvas, you'll
32 // need to do a conversion. In particular you'll need to use |SkIntToScalar()|, 33 // need to do a conversion. In particular you'll need to use |SkIntToScalar()|,
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 // Recreates the backing platform canvas with DIP |size| and |scale_factor|. 115 // Recreates the backing platform canvas with DIP |size| and |scale_factor|.
115 // If the canvas is not opaque, it is explicitly cleared. 116 // If the canvas is not opaque, it is explicitly cleared.
116 // This method is public so that canvas_skia_paint can recreate the platform 117 // This method is public so that canvas_skia_paint can recreate the platform
117 // canvas after having initialized the canvas. 118 // canvas after having initialized the canvas.
118 // TODO(pkotwicz): Push the scale factor into skia::PlatformCanvas such that 119 // TODO(pkotwicz): Push the scale factor into skia::PlatformCanvas such that
119 // this method can be private. 120 // this method can be private.
120 void RecreateBackingCanvas(const gfx::Size& size, 121 void RecreateBackingCanvas(const gfx::Size& size,
121 ui::ScaleFactor scale_factor, 122 ui::ScaleFactor scale_factor,
122 bool is_opaque); 123 bool is_opaque);
123 124
124 // Compute the size required to draw some text with the provided font. 125 // Compute the size required to draw some text with the provided fonts.
125 // Attempts to fit the text with the provided width and height. Increases 126 // Attempts to fit the text with the provided width and height. Increases
126 // height and then width as needed to make the text fit. This method 127 // height and then width as needed to make the text fit. This method
127 // supports multiple lines. On Skia only a line_height can be specified and 128 // supports multiple lines. On Skia only a line_height can be specified and
128 // specifying a 0 value for it will cause the default height to be used. 129 // specifying a 0 value for it will cause the default height to be used.
129 static void SizeStringInt(const base::string16& text, 130 static void SizeStringInt(const base::string16& text,
131 const gfx::FontList& font_list,
132 int* width,
133 int* height,
134 int line_height,
135 int flags);
136 // Obsolete version. Use the above version which takes gfx::FontList.
137 static void SizeStringInt(const base::string16& text,
130 const gfx::Font& font, 138 const gfx::Font& font,
131 int* width, int* height, 139 int* width,
140 int* height,
132 int line_height, 141 int line_height,
133 int flags); 142 int flags);
134 143
135 // Returns the number of horizontal pixels needed to display the specified 144 // Returns the number of horizontal pixels needed to display the specified
136 // |text| with |font|. 145 // |text| with |font_list|.
146 static int GetStringWidth(const base::string16& text,
147 const gfx::FontList& font_list);
148 // Obsolete version. Use the above version which takes gfx::FontList.
137 static int GetStringWidth(const base::string16& text, const gfx::Font& font); 149 static int GetStringWidth(const base::string16& text, const gfx::Font& font);
138 150
139 // Returns the default text alignment to be used when drawing text on a 151 // Returns the default text alignment to be used when drawing text on a
140 // gfx::Canvas based on the directionality of the system locale language. 152 // gfx::Canvas based on the directionality of the system locale language.
141 // This function is used by gfx::Canvas::DrawStringInt when the text alignment 153 // This function is used by gfx::Canvas::DrawStringInt when the text alignment
142 // is not specified. 154 // is not specified.
143 // 155 //
144 // This function returns either gfx::Canvas::TEXT_ALIGN_LEFT or 156 // This function returns either gfx::Canvas::TEXT_ALIGN_LEFT or
145 // gfx::Canvas::TEXT_ALIGN_RIGHT. 157 // gfx::Canvas::TEXT_ALIGN_RIGHT.
146 static int DefaultCanvasTextAlignment(); 158 static int DefaultCanvasTextAlignment();
147 159
148 // Draws text with a 1-pixel halo around it of the given color. 160 // Draws text with a 1-pixel halo around it of the given color.
149 // On Windows, it allows ClearType to be drawn to an otherwise transparenct 161 // On Windows, it allows ClearType to be drawn to an otherwise transparent
150 // bitmap for drag images. Drag images have only 1-bit of transparency, so 162 // bitmap for drag images. Drag images have only 1-bit of transparency, so
151 // we don't do any fancy blurring. 163 // we don't do any fancy blurring.
152 // On Linux, text with halo is created by stroking it with 2px |halo_color| 164 // On Linux, text with halo is created by stroking it with 2px |halo_color|
153 // then filling it with |text_color|. 165 // then filling it with |text_color|.
154 // On Mac, NOTIMPLEMENTED. 166 // On Mac, NOTIMPLEMENTED.
155 // TODO(dhollowa): Skia-native implementation is underway. Cut over to 167 // TODO(dhollowa): Skia-native implementation is underway. Cut over to
156 // that when ready. http::/crbug.com/109946 168 // that when ready. http::/crbug.com/109946
157 void DrawStringWithHalo(const base::string16& text, 169 void DrawStringWithHalo(const base::string16& text,
170 const gfx::FontList& font_list,
171 SkColor text_color,
172 SkColor halo_color,
173 const gfx::Rect& display_rect,
174 int flags);
175 // Obsolete version. Use the above version which takes gfx::FontList.
176 void DrawStringWithHalo(const base::string16& text,
158 const gfx::Font& font, 177 const gfx::Font& font,
159 SkColor text_color, 178 SkColor text_color,
160 SkColor halo_color, 179 SkColor halo_color,
161 int x, int y, int w, int h, 180 int x,
181 int y,
182 int w,
183 int h,
162 int flags); 184 int flags);
163 185
164 // Extracts an ImageSkiaRep from the contents of this canvas. 186 // Extracts an ImageSkiaRep from the contents of this canvas.
165 gfx::ImageSkiaRep ExtractImageRep() const; 187 gfx::ImageSkiaRep ExtractImageRep() const;
166 188
167 // Draws a dashed rectangle of the specified color. 189 // Draws a dashed rectangle of the specified color.
168 void DrawDashedRect(const gfx::Rect& rect, SkColor color); 190 void DrawDashedRect(const gfx::Rect& rect, SkColor color);
169 191
170 // Saves a copy of the drawing state onto a stack, operating on this copy 192 // Saves a copy of the drawing state onto a stack, operating on this copy
171 // until a balanced call to Restore() is made. 193 // until a balanced call to Restore() is made.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 // Helper for DrawImageInt(..., paint) that constructs a temporary paint and 282 // Helper for DrawImageInt(..., paint) that constructs a temporary paint and
261 // calls paint.setAlpha(alpha). 283 // calls paint.setAlpha(alpha).
262 void DrawImageInt(const gfx::ImageSkia&, int x, int y, uint8 alpha); 284 void DrawImageInt(const gfx::ImageSkia&, int x, int y, uint8 alpha);
263 285
264 // Draws an image with the origin at the specified location, using the 286 // Draws an image with the origin at the specified location, using the
265 // specified paint. The upper left corner of the bitmap is rendered at the 287 // specified paint. The upper left corner of the bitmap is rendered at the
266 // specified location. 288 // specified location.
267 // Parameters are specified relative to current canvas scale not in pixels. 289 // Parameters are specified relative to current canvas scale not in pixels.
268 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. 290 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
269 void DrawImageInt(const gfx::ImageSkia& image, 291 void DrawImageInt(const gfx::ImageSkia& image,
270 int x, int y, 292 int x,
293 int y,
271 const SkPaint& paint); 294 const SkPaint& paint);
272 295
273 // Draws a portion of an image in the specified location. The src parameters 296 // Draws a portion of an image in the specified location. The src parameters
274 // correspond to the region of the bitmap to draw in the region defined 297 // correspond to the region of the bitmap to draw in the region defined
275 // by the dest coordinates. 298 // by the dest coordinates.
276 // 299 //
277 // If the width or height of the source differs from that of the destination, 300 // If the width or height of the source differs from that of the destination,
278 // the image will be scaled. When scaling down, a mipmap will be generated. 301 // the image will be scaled. When scaling down, a mipmap will be generated.
279 // Set |filter| to use filtering for images, otherwise the nearest-neighbor 302 // Set |filter| to use filtering for images, otherwise the nearest-neighbor
280 // algorithm is used for resampling. 303 // algorithm is used for resampling.
281 // 304 //
282 // An optional custom SkPaint can be provided. 305 // An optional custom SkPaint can be provided.
283 // Parameters are specified relative to current canvas scale not in pixels. 306 // Parameters are specified relative to current canvas scale not in pixels.
284 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. 307 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
285 void DrawImageInt(const gfx::ImageSkia& image, 308 void DrawImageInt(const gfx::ImageSkia& image,
286 int src_x, int src_y, int src_w, int src_h, 309 int src_x,
287 int dest_x, int dest_y, int dest_w, int dest_h, 310 int src_y,
311 int src_w,
312 int src_h,
313 int dest_x,
314 int dest_y,
315 int dest_w,
316 int dest_h,
288 bool filter); 317 bool filter);
289 void DrawImageInt(const gfx::ImageSkia& image, 318 void DrawImageInt(const gfx::ImageSkia& image,
290 int src_x, int src_y, int src_w, int src_h, 319 int src_x,
291 int dest_x, int dest_y, int dest_w, int dest_h, 320 int src_y,
321 int src_w,
322 int src_h,
323 int dest_x,
324 int dest_y,
325 int dest_w,
326 int dest_h,
292 bool filter, 327 bool filter,
293 const SkPaint& paint); 328 const SkPaint& paint);
294 329
295 // Draws an |image| with the top left corner at |x| and |y|, clipped to 330 // Draws an |image| with the top left corner at |x| and |y|, clipped to
296 // |path|. 331 // |path|.
297 // Parameters are specified relative to current canvas scale not in pixels. 332 // Parameters are specified relative to current canvas scale not in pixels.
298 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1. 333 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
299 void DrawImageInPath(const gfx::ImageSkia& image, 334 void DrawImageInPath(const gfx::ImageSkia& image,
300 int x, 335 int x,
301 int y, 336 int y,
302 const SkPath& path, 337 const SkPath& path,
303 const SkPaint& paint); 338 const SkPaint& paint);
304 339
305 // Draws text with the specified color, font and location. The text is 340 // Draws text with the specified color, fonts and location. The text is
306 // aligned to the left, vertically centered, clipped to the region. If the 341 // aligned to the left, vertically centered, clipped to the region. If the
307 // text is too big, it is truncated and '...' is added to the end. 342 // text is too big, it is truncated and '...' is added to the end.
343 void DrawString(const base::string16& text,
344 const gfx::FontList& font_list,
345 SkColor color,
346 const gfx::Rect& display_rect);
347 // Obsolete versions. Use the above versions which take gfx::FontList.
308 void DrawStringInt(const base::string16& text, 348 void DrawStringInt(const base::string16& text,
309 const gfx::Font& font, 349 const gfx::Font& font,
310 SkColor color, 350 SkColor color,
311 int x, int y, int w, int h); 351 int x,
352 int y,
353 int w,
354 int h);
312 void DrawStringInt(const base::string16& text, 355 void DrawStringInt(const base::string16& text,
313 const gfx::Font& font, 356 const gfx::Font& font,
314 SkColor color, 357 SkColor color,
315 const gfx::Rect& display_rect); 358 const gfx::Rect& display_rect);
316 359
317 // Draws text with the specified color, font and location. The last argument 360 // Draws text with the specified color, fonts and location. The last argument
318 // specifies flags for how the text should be rendered. It can be one of 361 // specifies flags for how the text should be rendered. It can be one of
319 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. 362 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT.
363 void DrawStringWithFlags(const base::string16& text,
364 const gfx::FontList& font_list,
365 SkColor color,
366 const gfx::Rect& display_rect,
367 int flags);
368 // Obsolete version. Use the above version which takes gfx::FontList.
320 void DrawStringInt(const base::string16& text, 369 void DrawStringInt(const base::string16& text,
321 const gfx::Font& font, 370 const gfx::Font& font,
322 SkColor color, 371 SkColor color,
323 int x, int y, int w, int h, 372 int x,
373 int y,
374 int w,
375 int h,
324 int flags); 376 int flags);
325 377
326 // Similar to above DrawStringInt method but with text shadows support. 378 // Similar to above DrawStringInt method but with text shadows support.
327 // Currently it's only implemented for canvas skia. Specifying a 0 line_height 379 // Currently it's only implemented for canvas skia. Specifying a 0 line_height
328 // will cause the default height to be used. 380 // will cause the default height to be used.
329 void DrawStringWithShadows(const base::string16& text, 381 void DrawStringWithShadows(const base::string16& text,
382 const gfx::FontList& font_list,
383 SkColor color,
384 const gfx::Rect& text_bounds,
385 int line_height,
386 int flags,
387 const ShadowValues& shadows);
388 // Obsolete version. Use the above version which takes gfx::FontList.
389 void DrawStringWithShadows(const base::string16& text,
330 const gfx::Font& font, 390 const gfx::Font& font,
331 SkColor color, 391 SkColor color,
332 const gfx::Rect& text_bounds, 392 const gfx::Rect& text_bounds,
333 int line_height, 393 int line_height,
334 int flags, 394 int flags,
335 const ShadowValues& shadows); 395 const ShadowValues& shadows);
336 396
337 // Draws a dotted gray rectangle used for focus purposes. 397 // Draws a dotted gray rectangle used for focus purposes.
338 void DrawFocusRect(const gfx::Rect& rect); 398 void DrawFocusRect(const gfx::Rect& rect);
339 399
340 // Tiles the image in the specified region. 400 // Tiles the image in the specified region.
341 // Parameters are specified relative to current canvas scale not in pixels. 401 // Parameters are specified relative to current canvas scale not in pixels.
342 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. 402 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
343 void TileImageInt(const gfx::ImageSkia& image, 403 void TileImageInt(const gfx::ImageSkia& image,
344 int x, int y, int w, int h); 404 int x,
405 int y,
406 int w,
407 int h);
345 void TileImageInt(const gfx::ImageSkia& image, 408 void TileImageInt(const gfx::ImageSkia& image,
346 int src_x, int src_y, 409 int src_x,
347 int dest_x, int dest_y, int w, int h); 410 int src_y,
411 int dest_x,
412 int dest_y,
413 int w,
414 int h);
348 void TileImageInt(const gfx::ImageSkia& image, 415 void TileImageInt(const gfx::ImageSkia& image,
349 int src_x, int src_y, 416 int src_x,
350 float tile_scale_x, float tile_scale_y, 417 int src_y,
351 int dest_x, int dest_y, int w, int h); 418 float tile_scale_x,
419 float tile_scale_y,
420 int dest_x,
421 int dest_y,
422 int w,
423 int h);
352 424
353 // Returns a native drawing context for platform specific drawing routines to 425 // Returns a native drawing context for platform specific drawing routines to
354 // use. Must be balanced by a call to EndPlatformPaint(). 426 // use. Must be balanced by a call to EndPlatformPaint().
355 NativeDrawingContext BeginPlatformPaint(); 427 NativeDrawingContext BeginPlatformPaint();
356 428
357 // Signifies the end of platform drawing using the native drawing context 429 // Signifies the end of platform drawing using the native drawing context
358 // returned by BeginPlatformPaint(). 430 // returned by BeginPlatformPaint().
359 void EndPlatformPaint(); 431 void EndPlatformPaint();
360 432
361 // Apply transformation on the canvas. 433 // Apply transformation on the canvas.
362 void Transform(const gfx::Transform& transform); 434 void Transform(const gfx::Transform& transform);
363 435
364 // Draws the given string with the beginning and/or the end using a fade 436 // Draws the given string with the beginning and/or the end using a fade
365 // gradient. When truncating the head 437 // gradient. When truncating the head
366 // |desired_characters_to_truncate_from_head| specifies the maximum number of 438 // |desired_characters_to_truncate_from_head| specifies the maximum number of
367 // characters that can be truncated. 439 // characters that can be truncated.
368 void DrawFadeTruncatingString( 440 void DrawFadeTruncatingString(
369 const base::string16& text, 441 const base::string16& text,
370 TruncateFadeMode truncate_mode, 442 TruncateFadeMode truncate_mode,
371 size_t desired_characters_to_truncate_from_head, 443 size_t desired_characters_to_truncate_from_head,
444 const gfx::FontList& font_list,
445 SkColor color,
446 const gfx::Rect& display_rect);
447 // Obsolete version. Use the above version which takes gfx::FontList.
448 void DrawFadeTruncatingString(
449 const base::string16& text,
450 TruncateFadeMode truncate_mode,
451 size_t desired_characters_to_truncate_from_head,
372 const gfx::Font& font, 452 const gfx::Font& font,
373 SkColor color, 453 SkColor color,
374 const gfx::Rect& display_rect); 454 const gfx::Rect& display_rect);
375 455
376 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } 456 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); }
377 SkCanvas* sk_canvas() const { return canvas_; } 457 SkCanvas* sk_canvas() const { return canvas_; }
378 ui::ScaleFactor scale_factor() const { return scale_factor_; } 458 ui::ScaleFactor scale_factor() const { return scale_factor_; }
379 459
380 private: 460 private:
381 Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor); 461 Canvas(SkCanvas* canvas, ui::ScaleFactor scale_factor);
(...skipping 21 matching lines...) Expand all
403 483
404 skia::RefPtr<skia::PlatformCanvas> owned_canvas_; 484 skia::RefPtr<skia::PlatformCanvas> owned_canvas_;
405 SkCanvas* canvas_; 485 SkCanvas* canvas_;
406 486
407 DISALLOW_COPY_AND_ASSIGN(Canvas); 487 DISALLOW_COPY_AND_ASSIGN(Canvas);
408 }; 488 };
409 489
410 } // namespace gfx 490 } // namespace gfx
411 491
412 #endif // UI_GFX_CANVAS_H_ 492 #endif // UI_GFX_CANVAS_H_
OLDNEW
« ui/base/text/text_elider.cc ('K') | « ui/base/text/text_elider.cc ('k') | ui/gfx/canvas.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698