OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "printing/pdf_ps_metafile_cairo.h" | 5 #include "printing/pdf_ps_metafile_cairo.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include <cairo.h> | 9 #include <cairo.h> |
10 #include <cairo-pdf.h> | 10 #include <cairo-pdf.h> |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 case PS: | 91 case PS: |
92 surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream, | 92 surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream, |
93 &data_, 1, 1); | 93 &data_, 1, 1); |
94 break; | 94 break; |
95 | 95 |
96 default: | 96 default: |
97 NOTREACHED(); | 97 NOTREACHED(); |
98 return false; | 98 return false; |
99 } | 99 } |
100 | 100 |
101 // Don't let WebKit draw over the margins. | |
102 cairo_surface_set_device_offset(surface_, | |
103 static_cast<int>(kLeftMargin), | |
104 static_cast<int>(kTopMargin)); | |
105 | |
106 // Cairo always returns a valid pointer. | 101 // Cairo always returns a valid pointer. |
107 // Hence, we have to check if it points to a "nil" object. | 102 // Hence, we have to check if it points to a "nil" object. |
108 if (!IsSurfaceValid(surface_)) { | 103 if (!IsSurfaceValid(surface_)) { |
109 DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!"; | 104 DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!"; |
110 CleanUpSurface(&surface_); | 105 CleanUpSurface(&surface_); |
111 return false; | 106 return false; |
112 } | 107 } |
113 | 108 |
114 // Creates a context. | 109 // Creates a context. |
115 context_ = cairo_create(surface_); | 110 context_ = cairo_create(surface_); |
(...skipping 16 matching lines...) Expand all Loading... |
132 if (src_buffer == NULL || src_buffer_size == 0) | 127 if (src_buffer == NULL || src_buffer_size == 0) |
133 return false; | 128 return false; |
134 | 129 |
135 data_ = std::string(reinterpret_cast<const char*>(src_buffer), | 130 data_ = std::string(reinterpret_cast<const char*>(src_buffer), |
136 src_buffer_size); | 131 src_buffer_size); |
137 | 132 |
138 return true; | 133 return true; |
139 } | 134 } |
140 | 135 |
141 cairo_t* PdfPsMetafile::StartPage(double width_in_points, | 136 cairo_t* PdfPsMetafile::StartPage(double width_in_points, |
142 double height_in_points) { | 137 double height_in_points, |
| 138 double margin_top_in_points, |
| 139 double margin_right_in_points, |
| 140 double margin_bottom_in_points, |
| 141 double margin_left_in_points) { |
143 DCHECK(IsSurfaceValid(surface_)); | 142 DCHECK(IsSurfaceValid(surface_)); |
144 DCHECK(IsContextValid(context_)); | 143 DCHECK(IsContextValid(context_)); |
145 // Passing this check implies page_surface_ is NULL, and current_page_ is | 144 // Passing this check implies page_surface_ is NULL, and current_page_ is |
146 // empty. | 145 // empty. |
147 DCHECK_GT(width_in_points, 0.); | 146 DCHECK_GT(width_in_points, 0.); |
148 DCHECK_GT(height_in_points, 0.); | 147 DCHECK_GT(height_in_points, 0.); |
149 | 148 |
150 // We build in extra room for the margins. The Cairo PDF backend will scale | 149 // We build in extra room for the margins. The Cairo PDF backend will scale |
151 // the output to fit a page. | 150 // the output to fit a page. |
152 double width = width_in_points + kLeftMargin + kRightMargin; | 151 double width = |
153 double height = height_in_points + kTopMargin + kBottomMargin; | 152 width_in_points + margin_left_in_points + margin_right_in_points; |
| 153 double height = |
| 154 height_in_points + margin_top_in_points + margin_bottom_in_points; |
| 155 |
| 156 // Don't let WebKit draw over the margins. |
| 157 cairo_surface_set_device_offset(surface_, |
| 158 margin_left_in_points, |
| 159 margin_top_in_points); |
154 | 160 |
155 switch (format_) { | 161 switch (format_) { |
156 case PDF: | 162 case PDF: |
157 cairo_pdf_surface_set_size(surface_, width, height); | 163 cairo_pdf_surface_set_size(surface_, width, height); |
158 break; | 164 break; |
159 | 165 |
160 case PS: | 166 case PS: |
161 cairo_ps_surface_set_size(surface_, width, height); | 167 cairo_ps_surface_set_size(surface_, width, height); |
162 break; | 168 break; |
163 | 169 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 return success; | 242 return success; |
237 } | 243 } |
238 | 244 |
239 void PdfPsMetafile::CleanUpAll() { | 245 void PdfPsMetafile::CleanUpAll() { |
240 CleanUpContext(&context_); | 246 CleanUpContext(&context_); |
241 CleanUpSurface(&surface_); | 247 CleanUpSurface(&surface_); |
242 data_.clear(); | 248 data_.clear(); |
243 skia::VectorPlatformDevice::ClearFontCache(); | 249 skia::VectorPlatformDevice::ClearFontCache(); |
244 } | 250 } |
245 | 251 |
246 const double PdfPsMetafile::kTopMargin = 0.25 * printing::kPointsPerInch; | 252 const double PdfPsMetafile::kTopMarginInInch = 0.25; |
247 const double PdfPsMetafile::kBottomMargin = 0.56 * printing::kPointsPerInch; | 253 const double PdfPsMetafile::kBottomMarginInInch = 0.56; |
248 const double PdfPsMetafile::kLeftMargin = 0.25 * printing::kPointsPerInch; | 254 const double PdfPsMetafile::kLeftMarginInInch = 0.25; |
249 const double PdfPsMetafile::kRightMargin = 0.25 * printing::kPointsPerInch; | 255 const double PdfPsMetafile::kRightMarginInInch = 0.25; |
250 | 256 |
251 } // namespace printing | 257 } // namespace printing |
OLD | NEW |