Chromium Code Reviews| Index: chrome/renderer/print_web_view_helper.cc |
| diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc |
| index b31cd851cfe5213c2219515b3dde4681cd01e5f7..64693732a28ffc87b70d4d350c3a5c0b1a3f8df5 100644 |
| --- a/chrome/renderer/print_web_view_helper.cc |
| +++ b/chrome/renderer/print_web_view_helper.cc |
| @@ -49,6 +49,10 @@ |
| #include "skia/ext/vector_canvas.h" |
| #include "skia/ext/vector_platform_device_skia.h" |
| #include "third_party/skia/include/core/SkTypeface.h" |
| +#if defined(OS_WIN) // Currently Windows only |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/render_text.h" |
| +#endif // USE_SKIA && defined(OS_WIN) |
| #elif defined(OS_MACOSX) |
| #include <CoreGraphics/CGContext.h> |
| @@ -394,6 +398,60 @@ SkPoint GetHeaderFooterPosition( |
| return point; |
| } |
| +#if defined(USE_SKIA) && defined(OS_WIN) |
| +void PrintHeaderFooterByRenderText( |
| + const string16& text, |
| + WebKit::WebCanvas* canvas, |
| + HeaderFooterPaint paint, |
| + float webkit_scale_factor, |
| + const PageSizeMargins& page_layout, |
| + printing::HorizontalHeaderFooterPosition horizontal_position, |
| + printing::VerticalHeaderFooterPosition vertical_position, |
| + double offset_to_baseline) { |
| + // TODO(arthurhsu): Following code works on Windows only so far. |
| + // See crbug.com/108599 and its blockers for more information. |
| + scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateRenderText()); |
| + render_text->SetText(text); |
| + int font_size = printing::kSettingHeaderFooterFontSize / webkit_scale_factor; |
| + render_text->SetFontSize(font_size); |
| + gfx::Size text_size = render_text->GetStringSize(); |
| + 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_
|
| + int y_offset = text_height - font_size; |
| + SkScalar margin_left = page_layout.margin_left / webkit_scale_factor; |
| + SkScalar margin_top = page_layout.margin_top / webkit_scale_factor; |
| + SkScalar content_height = page_layout.content_height / webkit_scale_factor; |
| + |
| + int save_count = canvas->save(); |
| + 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.
|
| + int text_width = text_size.width() / webkit_scale_factor; |
| + SkPoint point = GetHeaderFooterPosition(webkit_scale_factor, page_layout, |
| + horizontal_position, |
| + vertical_position, offset_to_baseline, |
| + SkScalarToDouble(text_width)); |
| + point.set(point.x() + margin_left, point.y() + margin_top); |
| + // Workaround clipping issue of RenderText by adjusting the y_offset to make |
| + // sure that display rect overlaps with content area. |
| + if (vertical_position == printing::TOP) { |
| + // Bottom of display rect must overlap with content. |
| + 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.
|
| + y_offset = point.y() + text_height - margin_top - 1; |
| + } |
| + } else { // BOTTOM |
| + // Top of display rect must overlap with content. |
| + if (point.y() - y_offset > margin_top + content_height - 1) { |
| + y_offset = point.y() - margin_top - content_height + 1; |
| + } |
| + } |
| + |
| + gfx::Rect rect(point.x(), point.y() - y_offset, text_width, text_height); |
| + render_text->SetDisplayRect(rect); |
| + scoped_ptr<gfx::Canvas> gfx_canvas(new gfx::Canvas(canvas)); |
| + render_text->Draw(gfx_canvas.get()); |
| + gfx_canvas.reset(); |
| + canvas->restoreToCount(save_count); |
| +} |
| +#endif |
| + |
| // Given a text, the positions, and the paint object, this method gets the |
| // coordinates and prints the text at those coordinates on the canvas. |
| void PrintHeaderFooterText( |
| @@ -406,6 +464,12 @@ void PrintHeaderFooterText( |
| printing::VerticalHeaderFooterPosition vertical_position, |
| double offset_to_baseline) { |
| #if defined(USE_SKIA) |
| +#if defined(OS_WIN) |
| + PrintHeaderFooterByRenderText(text, canvas, paint, webkit_scale_factor, |
| + page_layout, horizontal_position, vertical_position, offset_to_baseline); |
| +#else |
| + // TODO(arthurhsu): following code has issues with i18n BiDi, see |
| + // crbug.com/108599. |
| size_t text_byte_length = text.length() * sizeof(char16); |
| double text_width_in_points = SkScalarToDouble(paint.measureText( |
| text.c_str(), text_byte_length)); |
| @@ -417,6 +481,7 @@ void PrintHeaderFooterText( |
| paint.getTextSize() / webkit_scale_factor)); |
| canvas->drawText(text.c_str(), text_byte_length, point.x(), point.y(), |
| paint); |
| +#endif // USE_SKIA && OS_WIN |
| #elif defined(OS_MACOSX) |
| ScopedCFTypeRef<CFStringRef> cf_text(base::SysUTF16ToCFStringRef(text)); |
| ScopedCFTypeRef<CFAttributedStringRef> cf_attr_text( |