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() / webkit_scale_factor; | |
Alexei Svitkine (slow)
2012/05/03 19:04:41
The scaling still looks incorrect to me.
You take
arthurhsu
2012/05/07 17:28:06
Ah yeah nice catch. The division by webkit_scale_
| |
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 save_count = canvas->save(); | |
425 canvas->translate(-margin_left, -margin_top); | |
Alexei Svitkine (slow)
2012/05/03 19:04:41
Move these two canvas operations further down, jus
arthurhsu
2012/05/07 17:28:06
Done.
| |
426 int text_width = text_size.width() / webkit_scale_factor; | |
427 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, | |
428 horizontal_position, | |
429 vertical_position, offset_to_baseline, | |
430 SkScalarToDouble(text_width)); | |
431 point.set(point.x() + margin_left, point.y() + margin_top); | |
432 // Workaround clipping issue of RenderText by adjusting the y_offset to make | |
433 // sure that display rect overlaps with content area. | |
434 if (vertical_position == printing::TOP) { | |
435 // Bottom of display rect must overlap with content. | |
436 if (point.y() - y_offset + text_height < margin_top + 1) { | |
Alexei Svitkine (slow)
2012/05/03 19:04:41
I still think this can made clearer by the using o
arthurhsu
2012/05/07 17:28:06
Done.
| |
437 y_offset = point.y() + text_height - margin_top - 1; | |
438 } | |
439 } else { // BOTTOM | |
440 // Top of display rect must overlap with content. | |
441 if (point.y() - y_offset > margin_top + content_height - 1) { | |
442 y_offset = point.y() - margin_top - content_height + 1; | |
443 } | |
444 } | |
445 | |
446 gfx::Rect rect(point.x(), point.y() - y_offset, text_width, text_height); | |
447 render_text->SetDisplayRect(rect); | |
448 scoped_ptr<gfx::Canvas> gfx_canvas(new gfx::Canvas(canvas)); | |
449 render_text->Draw(gfx_canvas.get()); | |
450 gfx_canvas.reset(); | |
451 canvas->restoreToCount(save_count); | |
452 } | |
453 #endif | |
454 | |
397 // Given a text, the positions, and the paint object, this method gets the | 455 // 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. | 456 // coordinates and prints the text at those coordinates on the canvas. |
399 void PrintHeaderFooterText( | 457 void PrintHeaderFooterText( |
400 const string16& text, | 458 const string16& text, |
401 WebKit::WebCanvas* canvas, | 459 WebKit::WebCanvas* canvas, |
402 HeaderFooterPaint paint, | 460 HeaderFooterPaint paint, |
403 float webkit_scale_factor, | 461 float webkit_scale_factor, |
404 const PageSizeMargins& page_layout, | 462 const PageSizeMargins& page_layout, |
405 printing::HorizontalHeaderFooterPosition horizontal_position, | 463 printing::HorizontalHeaderFooterPosition horizontal_position, |
406 printing::VerticalHeaderFooterPosition vertical_position, | 464 printing::VerticalHeaderFooterPosition vertical_position, |
407 double offset_to_baseline) { | 465 double offset_to_baseline) { |
408 #if defined(USE_SKIA) | 466 #if defined(USE_SKIA) |
467 #if defined(OS_WIN) | |
468 PrintHeaderFooterByRenderText(text, canvas, paint, webkit_scale_factor, | |
469 page_layout, horizontal_position, vertical_position, offset_to_baseline); | |
470 #else | |
471 // TODO(arthurhsu): following code has issues with i18n BiDi, see | |
472 // crbug.com/108599. | |
409 size_t text_byte_length = text.length() * sizeof(char16); | 473 size_t text_byte_length = text.length() * sizeof(char16); |
410 double text_width_in_points = SkScalarToDouble(paint.measureText( | 474 double text_width_in_points = SkScalarToDouble(paint.measureText( |
411 text.c_str(), text_byte_length)); | 475 text.c_str(), text_byte_length)); |
412 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, | 476 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, |
413 horizontal_position, | 477 horizontal_position, |
414 vertical_position, offset_to_baseline, | 478 vertical_position, offset_to_baseline, |
415 text_width_in_points); | 479 text_width_in_points); |
416 paint.setTextSize(SkDoubleToScalar( | 480 paint.setTextSize(SkDoubleToScalar( |
417 paint.getTextSize() / webkit_scale_factor)); | 481 paint.getTextSize() / webkit_scale_factor)); |
418 canvas->drawText(text.c_str(), text_byte_length, point.x(), point.y(), | 482 canvas->drawText(text.c_str(), text_byte_length, point.x(), point.y(), |
419 paint); | 483 paint); |
484 #endif // USE_SKIA && OS_WIN | |
420 #elif defined(OS_MACOSX) | 485 #elif defined(OS_MACOSX) |
421 ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); | 486 ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); |
422 ScopedCFTypeRef<CFAttributedStringRef> cf_attr_text( | 487 ScopedCFTypeRef<CFAttributedStringRef> cf_attr_text( |
423 CFAttributedStringCreate(NULL, cf_text, paint)); | 488 CFAttributedStringCreate(NULL, cf_text, paint)); |
424 ScopedCFTypeRef<CTLineRef> line(CTLineCreateWithAttributedString( | 489 ScopedCFTypeRef<CTLineRef> line(CTLineCreateWithAttributedString( |
425 cf_attr_text)); | 490 cf_attr_text)); |
426 double text_width_in_points = | 491 double text_width_in_points = |
427 CTLineGetTypographicBounds(line, NULL, NULL, NULL) * webkit_scale_factor; | 492 CTLineGetTypographicBounds(line, NULL, NULL, NULL) * webkit_scale_factor; |
428 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, | 493 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, |
429 page_layout, horizontal_position, | 494 page_layout, horizontal_position, |
(...skipping 1390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1820 DCHECK(IsRendering()); | 1885 DCHECK(IsRendering()); |
1821 return prep_frame_view_->GetPrintCanvasSize(); | 1886 return prep_frame_view_->GetPrintCanvasSize(); |
1822 } | 1887 } |
1823 | 1888 |
1824 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1889 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1825 prep_frame_view_.reset(); | 1890 prep_frame_view_.reset(); |
1826 metafile_.reset(); | 1891 metafile_.reset(); |
1827 pages_to_render_.clear(); | 1892 pages_to_render_.clear(); |
1828 error_ = PREVIEW_ERROR_NONE; | 1893 error_ = PREVIEW_ERROR_NONE; |
1829 } | 1894 } |
OLD | NEW |