Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 #include "webkit/glue/webpreferences.h" | 42 #include "webkit/glue/webpreferences.h" |
| 43 | 43 |
| 44 #if defined(OS_POSIX) | 44 #if defined(OS_POSIX) |
| 45 #include "base/process_util.h" | 45 #include "base/process_util.h" |
| 46 #endif | 46 #endif |
| 47 | 47 |
| 48 #if defined(USE_SKIA) | 48 #if defined(USE_SKIA) |
| 49 #include "skia/ext/vector_canvas.h" | 49 #include "skia/ext/vector_canvas.h" |
| 50 #include "skia/ext/vector_platform_device_skia.h" | 50 #include "skia/ext/vector_platform_device_skia.h" |
| 51 #include "third_party/skia/include/core/SkTypeface.h" | 51 #include "third_party/skia/include/core/SkTypeface.h" |
| 52 #if defined(OS_WIN) // Currently Windows only | |
| 53 #include "ui/gfx/canvas.h" | |
| 54 #include "ui/gfx/render_text.h" | |
| 55 #endif // USE_SKIA && defined(OS_WIN) | |
| 52 #elif defined(OS_MACOSX) | 56 #elif defined(OS_MACOSX) |
| 53 #include <CoreGraphics/CGContext.h> | 57 #include <CoreGraphics/CGContext.h> |
| 54 | 58 |
| 55 #include "base/mac/scoped_cftyperef.h" | 59 #include "base/mac/scoped_cftyperef.h" |
| 56 #include "base/sys_string_conversions.h" | 60 #include "base/sys_string_conversions.h" |
| 57 #include "ui/gfx/scoped_cg_context_save_gstate_mac.h" | 61 #include "ui/gfx/scoped_cg_context_save_gstate_mac.h" |
| 58 #endif | 62 #endif |
| 59 | 63 |
| 60 #if defined(OS_MACOSX) | 64 #if defined(OS_MACOSX) |
| 61 using base::mac::ScopedCFTypeRef; | 65 using base::mac::ScopedCFTypeRef; |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 387 break; | 391 break; |
| 388 default: | 392 default: |
| 389 NOTREACHED(); | 393 NOTREACHED(); |
| 390 } | 394 } |
| 391 | 395 |
| 392 SkPoint point = SkPoint::Make(x / webkit_scale_factor, | 396 SkPoint point = SkPoint::Make(x / webkit_scale_factor, |
| 393 y / webkit_scale_factor); | 397 y / webkit_scale_factor); |
| 394 return point; | 398 return point; |
| 395 } | 399 } |
| 396 | 400 |
| 401 #if defined(USE_SKIA) && defined(OS_WIN) | |
| 402 void PrintHeaderFooterByRenderText( | |
| 403 const string16& text, | |
| 404 WebKit::WebCanvas* canvas, | |
| 405 HeaderFooterPaint paint, | |
| 406 float webkit_scale_factor, | |
| 407 const PageSizeMargins& page_layout, | |
| 408 printing::HorizontalHeaderFooterPosition horizontal_position, | |
| 409 printing::VerticalHeaderFooterPosition vertical_position, | |
| 410 double offset_to_baseline) { | |
| 411 // TODO(arthurhsu): Following code works on Windows only so far. | |
| 412 // See crbug.com/108599 and its blockers for more information. | |
| 413 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateRenderText()); | |
| 414 render_text->SetText(text); | |
| 415 int font_size = printing::kSettingHeaderFooterFontSize / webkit_scale_factor; | |
| 416 render_text->SetFontSize(font_size); | |
| 417 gfx::Size text_size = render_text->GetStringSize(); | |
| 418 int text_height = text_size.height(); | |
| 419 int y_offset = text_height - font_size; | |
| 420 SkScalar margin_left = page_layout.margin_left / webkit_scale_factor; | |
| 421 SkScalar margin_top = page_layout.margin_top / webkit_scale_factor; | |
| 422 SkScalar content_height = page_layout.content_height / webkit_scale_factor; | |
| 423 | |
| 424 int text_width = text_size.width(); | |
| 425 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, | |
| 426 horizontal_position, | |
| 427 vertical_position, offset_to_baseline, | |
| 428 SkScalarToDouble(text_width)); | |
| 429 point.set(point.x() + margin_left, point.y() + margin_top); | |
| 430 // Workaround clipping issue of RenderText by adjusting the y_offset to make | |
| 431 // sure that display rect overlaps with content area. | |
| 432 if (vertical_position == printing::TOP) { | |
| 433 // Bottom of display rect must overlap with content. | |
| 434 int display_rect_y = point.y() - y_offset + text_height; | |
| 435 int content_y = margin_top + 1; | |
| 436 if (display_rect_y < content_y) { | |
| 437 y_offset = point.y() + text_height - content_y; | |
| 438 } | |
| 439 } else { // BOTTOM | |
| 440 // Top of display rect must overlap with content. | |
| 441 int display_rect_y = point.y() - y_offset; | |
| 442 int content_y = margin_top + content_height - 1; | |
| 443 if (display_rect_y > content_y) { | |
| 444 y_offset = point.y() - content_y; | |
| 445 } | |
| 446 } | |
| 447 | |
| 448 gfx::Rect rect(point.x(), point.y() - y_offset, text_width, text_height); | |
|
Alexei Svitkine (slow)
2012/05/07 18:22:43
(Move it below this line.)
arthurhsu
2012/05/07 18:31:05
Done.
| |
| 449 int save_count = canvas->save(); | |
| 450 canvas->translate(-margin_left, -margin_top); | |
| 451 { | |
| 452 gfx::Canvas gfx_canvas(canvas); | |
| 453 render_text->SetDisplayRect(rect); | |
|
Alexei Svitkine (slow)
2012/05/07 18:22:43
I was suggesting you move it to after the gfx::Rec
arthurhsu
2012/05/07 18:31:05
Done.
| |
| 454 render_text->Draw(&gfx_canvas); | |
| 455 } | |
| 456 canvas->restoreToCount(save_count); | |
| 457 } | |
| 458 #endif | |
| 459 | |
| 397 // Given a text, the positions, and the paint object, this method gets the | 460 // Given a text, the positions, and the paint object, this method gets the |
| 398 // coordinates and prints the text at those coordinates on the canvas. | 461 // coordinates and prints the text at those coordinates on the canvas. |
| 399 void PrintHeaderFooterText( | 462 void PrintHeaderFooterText( |
| 400 const string16& text, | 463 const string16& text, |
| 401 WebKit::WebCanvas* canvas, | 464 WebKit::WebCanvas* canvas, |
| 402 HeaderFooterPaint paint, | 465 HeaderFooterPaint paint, |
| 403 float webkit_scale_factor, | 466 float webkit_scale_factor, |
| 404 const PageSizeMargins& page_layout, | 467 const PageSizeMargins& page_layout, |
| 405 printing::HorizontalHeaderFooterPosition horizontal_position, | 468 printing::HorizontalHeaderFooterPosition horizontal_position, |
| 406 printing::VerticalHeaderFooterPosition vertical_position, | 469 printing::VerticalHeaderFooterPosition vertical_position, |
| 407 double offset_to_baseline) { | 470 double offset_to_baseline) { |
| 408 #if defined(USE_SKIA) | 471 #if defined(USE_SKIA) |
| 472 #if defined(OS_WIN) | |
| 473 PrintHeaderFooterByRenderText(text, canvas, paint, webkit_scale_factor, | |
| 474 page_layout, horizontal_position, vertical_position, offset_to_baseline); | |
| 475 #else | |
| 476 // TODO(arthurhsu): following code has issues with i18n BiDi, see | |
| 477 // crbug.com/108599. | |
| 409 size_t text_byte_length = text.length() * sizeof(char16); | 478 size_t text_byte_length = text.length() * sizeof(char16); |
| 410 double text_width_in_points = SkScalarToDouble(paint.measureText( | 479 double text_width_in_points = SkScalarToDouble(paint.measureText( |
| 411 text.c_str(), text_byte_length)); | 480 text.c_str(), text_byte_length)); |
| 412 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, | 481 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, |
| 413 horizontal_position, | 482 horizontal_position, |
| 414 vertical_position, offset_to_baseline, | 483 vertical_position, offset_to_baseline, |
| 415 text_width_in_points); | 484 text_width_in_points); |
| 416 paint.setTextSize(SkDoubleToScalar( | 485 paint.setTextSize(SkDoubleToScalar( |
| 417 paint.getTextSize() / webkit_scale_factor)); | 486 paint.getTextSize() / webkit_scale_factor)); |
| 418 canvas->drawText(text.c_str(), text_byte_length, point.x(), point.y(), | 487 canvas->drawText(text.c_str(), text_byte_length, point.x(), point.y(), |
| 419 paint); | 488 paint); |
| 489 #endif // USE_SKIA && OS_WIN | |
| 420 #elif defined(OS_MACOSX) | 490 #elif defined(OS_MACOSX) |
| 421 ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); | 491 ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); |
| 422 ScopedCFTypeRef<CFAttributedStringRef> cf_attr_text( | 492 ScopedCFTypeRef<CFAttributedStringRef> cf_attr_text( |
| 423 CFAttributedStringCreate(NULL, cf_text, paint)); | 493 CFAttributedStringCreate(NULL, cf_text, paint)); |
| 424 ScopedCFTypeRef<CTLineRef> line(CTLineCreateWithAttributedString( | 494 ScopedCFTypeRef<CTLineRef> line(CTLineCreateWithAttributedString( |
| 425 cf_attr_text)); | 495 cf_attr_text)); |
| 426 double text_width_in_points = | 496 double text_width_in_points = |
| 427 CTLineGetTypographicBounds(line, NULL, NULL, NULL) * webkit_scale_factor; | 497 CTLineGetTypographicBounds(line, NULL, NULL, NULL) * webkit_scale_factor; |
| 428 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, | 498 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, |
| 429 page_layout, horizontal_position, | 499 page_layout, horizontal_position, |
| (...skipping 1390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1820 DCHECK(IsRendering()); | 1890 DCHECK(IsRendering()); |
| 1821 return prep_frame_view_->GetPrintCanvasSize(); | 1891 return prep_frame_view_->GetPrintCanvasSize(); |
| 1822 } | 1892 } |
| 1823 | 1893 |
| 1824 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1894 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1825 prep_frame_view_.reset(); | 1895 prep_frame_view_.reset(); |
| 1826 metafile_.reset(); | 1896 metafile_.reset(); |
| 1827 pages_to_render_.clear(); | 1897 pages_to_render_.clear(); |
| 1828 error_ = PREVIEW_ERROR_NONE; | 1898 error_ = PREVIEW_ERROR_NONE; |
| 1829 } | 1899 } |
| OLD | NEW |