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_MACOSX) // USE_SKIA && !OS_MACOSX | |
53 #include "ui/gfx/canvas.h" | |
54 #include "ui/gfx/render_text.h" | |
55 #endif // USE_SKIA && !defined(OS_MACOSX) | |
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 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 void PrintHeaderFooterText( | 403 void PrintHeaderFooterText( |
400 const string16& text, | 404 const string16& text, |
401 WebKit::WebCanvas* canvas, | 405 WebKit::WebCanvas* canvas, |
402 HeaderFooterPaint paint, | 406 HeaderFooterPaint paint, |
403 float webkit_scale_factor, | 407 float webkit_scale_factor, |
404 const PageSizeMargins& page_layout, | 408 const PageSizeMargins& page_layout, |
405 printing::HorizontalHeaderFooterPosition horizontal_position, | 409 printing::HorizontalHeaderFooterPosition horizontal_position, |
406 printing::VerticalHeaderFooterPosition vertical_position, | 410 printing::VerticalHeaderFooterPosition vertical_position, |
407 double offset_to_baseline) { | 411 double offset_to_baseline) { |
408 #if defined(USE_SKIA) | 412 #if defined(USE_SKIA) |
413 #if defined(OS_WIN) | |
Alexei Svitkine (slow)
2012/05/02 15:04:05
It might be a good idea to refactor this to a sepa
arthurhsu
2012/05/03 00:28:44
Done.
| |
414 // TODO(arthurhsu): Following code works on Windows only so far. | |
415 // See crbug.com/108599 and its blockers for more information. | |
416 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateRenderText()); | |
417 DCHECK(render_text.get()); | |
Alexei Svitkine (slow)
2012/05/02 15:04:05
What does this DCHECK() buy you? If it's NULL, the
arthurhsu
2012/05/03 00:28:44
Done.
| |
418 render_text->SetText(text); | |
419 int font_size = printing::kSettingHeaderFooterFontSize / webkit_scale_factor; | |
420 render_text->SetFontSize(font_size); | |
421 int font_height = render_text->GetStringSize().height() / webkit_scale_factor; | |
Alexei Svitkine (slow)
2012/05/02 15:04:05
It would be more accurate to name this |text_heigh
Alexei Svitkine (slow)
2012/05/02 15:23:58
Can you explain the webkit_scale_factor?
It seems
xji
2012/05/02 17:44:12
it re-SetFontSize() based on this factor on line 4
arthurhsu
2012/05/03 00:28:44
Done.
| |
422 int y_offset = font_height - font_size; | |
423 SkScalar margin_left = page_layout.margin_left / webkit_scale_factor; | |
424 SkScalar margin_top = page_layout.margin_top / webkit_scale_factor; | |
425 SkScalar content_height = page_layout.content_height / webkit_scale_factor; | |
426 | |
427 int save_count = canvas->save(); | |
428 canvas->translate(-margin_left, -margin_top); | |
429 scoped_ptr<gfx::Canvas> gfx_canvas(new gfx::Canvas(canvas)); | |
Alexei Svitkine (slow)
2012/05/01 14:56:39
I'm wondering whether you've tried using Canvas::D
arthurhsu
2012/05/01 17:26:38
Actually, I tried not to use it for debugging purp
Alexei Svitkine (slow)
2012/05/02 15:04:05
I suggest moving the gfx_canvas declaration to rig
arthurhsu
2012/05/03 00:28:44
Done.
| |
430 int text_width = render_text->GetStringSize().width() / webkit_scale_factor; | |
Alexei Svitkine (slow)
2012/05/02 15:04:05
Suggest just calling GetStringSize() once and stor
arthurhsu
2012/05/03 00:28:44
Done.
| |
431 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, | |
432 horizontal_position, | |
433 vertical_position, offset_to_baseline, | |
434 SkScalarToDouble(text_width)); | |
435 point.set(point.x() + margin_left, point.y() + margin_top); | |
436 // Workaround clipping issue of RenderText. | |
437 if (vertical_position == printing::TOP) { | |
438 if (point.y() - y_offset + font_height < margin_top + 1) { | |
Alexei Svitkine (slow)
2012/05/02 15:23:58
I think this logic can be written in a way that's
arthurhsu
2012/05/03 00:28:44
I've added more comments on this.
On 2012/05/02 1
| |
439 y_offset = point.y() + font_height - margin_top - 1; | |
440 } | |
441 } else { // BOTTOM | |
442 if (point.y() - y_offset > margin_top + content_height - 1) { | |
443 y_offset = point.y() - margin_top - content_height + 1; | |
444 } | |
445 } | |
446 | |
447 gfx::Rect rect(point.x(), point.y() - y_offset, text_width, font_height); | |
448 render_text->SetDisplayRect(rect); | |
449 render_text->Draw(gfx_canvas.get()); | |
450 gfx_canvas.reset(); | |
Alexei Svitkine (slow)
2012/05/02 15:04:05
Is it necessary to explicitly reset it here? You'r
arthurhsu
2012/05/03 00:28:44
I do not wish to assume that dtor of gfx::Canvas d
Alexei Svitkine (slow)
2012/05/03 19:04:41
If you want to be that paranoid, then I suggest us
arthurhsu
2012/05/07 17:28:06
Done.
| |
451 canvas->restoreToCount(save_count); | |
452 #else | |
453 // TODO(arthurhsu): following code has issues with i18n BiDi, see | |
454 // crbug.com/108599. | |
409 size_t text_byte_length = text.length() * sizeof(char16); | 455 size_t text_byte_length = text.length() * sizeof(char16); |
410 double text_width_in_points = SkScalarToDouble(paint.measureText( | 456 double text_width_in_points = SkScalarToDouble(paint.measureText( |
411 text.c_str(), text_byte_length)); | 457 text.c_str(), text_byte_length)); |
412 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, | 458 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, |
413 horizontal_position, | 459 horizontal_position, |
414 vertical_position, offset_to_baseline, | 460 vertical_position, offset_to_baseline, |
415 text_width_in_points); | 461 text_width_in_points); |
416 paint.setTextSize(SkDoubleToScalar( | 462 paint.setTextSize(SkDoubleToScalar( |
417 paint.getTextSize() / webkit_scale_factor)); | 463 paint.getTextSize() / webkit_scale_factor)); |
418 canvas->drawText(text.c_str(), text_byte_length, point.x(), point.y(), | 464 canvas->drawText(text.c_str(), text_byte_length, point.x(), point.y(), |
419 paint); | 465 paint); |
466 #endif // USE_SKIA && OS_WIN | |
420 #elif defined(OS_MACOSX) | 467 #elif defined(OS_MACOSX) |
421 ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); | 468 ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); |
422 ScopedCFTypeRef<CFAttributedStringRef> cf_attr_text( | 469 ScopedCFTypeRef<CFAttributedStringRef> cf_attr_text( |
423 CFAttributedStringCreate(NULL, cf_text, paint)); | 470 CFAttributedStringCreate(NULL, cf_text, paint)); |
424 ScopedCFTypeRef<CTLineRef> line(CTLineCreateWithAttributedString( | 471 ScopedCFTypeRef<CTLineRef> line(CTLineCreateWithAttributedString( |
425 cf_attr_text)); | 472 cf_attr_text)); |
426 double text_width_in_points = | 473 double text_width_in_points = |
427 CTLineGetTypographicBounds(line, NULL, NULL, NULL) * webkit_scale_factor; | 474 CTLineGetTypographicBounds(line, NULL, NULL, NULL) * webkit_scale_factor; |
428 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, | 475 SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, |
429 page_layout, horizontal_position, | 476 page_layout, horizontal_position, |
(...skipping 1390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1820 DCHECK(IsRendering()); | 1867 DCHECK(IsRendering()); |
1821 return prep_frame_view_->GetPrintCanvasSize(); | 1868 return prep_frame_view_->GetPrintCanvasSize(); |
1822 } | 1869 } |
1823 | 1870 |
1824 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1871 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1825 prep_frame_view_.reset(); | 1872 prep_frame_view_.reset(); |
1826 metafile_.reset(); | 1873 metafile_.reset(); |
1827 pages_to_render_.clear(); | 1874 pages_to_render_.clear(); |
1828 error_ = PREVIEW_ERROR_NONE; | 1875 error_ = PREVIEW_ERROR_NONE; |
1829 } | 1876 } |
OLD | NEW |