| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/utility/printing_handler.h" | 5 #include "chrome/utility/printing_handler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 IPC_MESSAGE_UNHANDLED(handled = false) | 82 IPC_MESSAGE_UNHANDLED(handled = false) |
| 83 IPC_END_MESSAGE_MAP() | 83 IPC_END_MESSAGE_MAP() |
| 84 return handled; | 84 return handled; |
| 85 } | 85 } |
| 86 | 86 |
| 87 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
| 88 void PrintingHandler::OnRenderPDFPagesToMetafile( | 88 void PrintingHandler::OnRenderPDFPagesToMetafile( |
| 89 IPC::PlatformFileForTransit pdf_transit, | 89 IPC::PlatformFileForTransit pdf_transit, |
| 90 const PdfRenderSettings& settings) { | 90 const PdfRenderSettings& settings) { |
| 91 pdf_rendering_settings_ = settings; | 91 pdf_rendering_settings_ = settings; |
| 92 chrome_pdf::SetPDFPostscriptPrintingLevel(0); // Not using postscript. |
| 92 chrome_pdf::SetPDFUseGDIPrinting(pdf_rendering_settings_.mode == | 93 chrome_pdf::SetPDFUseGDIPrinting(pdf_rendering_settings_.mode == |
| 93 PdfRenderSettings::Mode::GDI_TEXT); | 94 PdfRenderSettings::Mode::GDI_TEXT); |
| 95 if (pdf_rendering_settings_.mode == |
| 96 PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2) { |
| 97 chrome_pdf::SetPDFPostscriptPrintingLevel(2); |
| 98 } else if (pdf_rendering_settings_.mode == |
| 99 PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3) { |
| 100 chrome_pdf::SetPDFPostscriptPrintingLevel(3); |
| 101 } |
| 94 base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit); | 102 base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit); |
| 95 int page_count = LoadPDF(std::move(pdf_file)); | 103 int page_count = LoadPDF(std::move(pdf_file)); |
| 96 Send( | 104 Send( |
| 97 new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount(page_count)); | 105 new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount(page_count)); |
| 98 } | 106 } |
| 99 | 107 |
| 100 void PrintingHandler::OnRenderPDFPagesToMetafileGetPage( | 108 void PrintingHandler::OnRenderPDFPagesToMetafileGetPage( |
| 101 int page_number, | 109 int page_number, |
| 102 IPC::PlatformFileForTransit output_file) { | 110 IPC::PlatformFileForTransit output_file) { |
| 103 base::File emf_file = IPC::PlatformFileForTransitToFile(output_file); | 111 base::File emf_file = IPC::PlatformFileForTransitToFile(output_file); |
| 104 float scale_factor = 1.0f; | 112 float scale_factor = 1.0f; |
| 105 bool success = | 113 bool postscript = pdf_rendering_settings_.mode == |
| 106 RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor); | 114 PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2 || |
| 115 pdf_rendering_settings_.mode == |
| 116 PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3; |
| 117 bool success = RenderPdfPageToMetafile(page_number, std::move(emf_file), |
| 118 &scale_factor, postscript); |
| 107 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone( | 119 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone( |
| 108 success, scale_factor)); | 120 success, scale_factor)); |
| 109 } | 121 } |
| 110 | 122 |
| 111 void PrintingHandler::OnRenderPDFPagesToMetafileStop() { | 123 void PrintingHandler::OnRenderPDFPagesToMetafileStop() { |
| 112 ReleaseProcessIfNeeded(); | 124 ReleaseProcessIfNeeded(); |
| 113 } | 125 } |
| 114 | 126 |
| 115 #endif // OS_WIN | 127 #endif // defined(OS_WIN) |
| 116 | 128 |
| 117 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 129 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 118 void PrintingHandler::OnRenderPDFPagesToPWGRaster( | 130 void PrintingHandler::OnRenderPDFPagesToPWGRaster( |
| 119 IPC::PlatformFileForTransit pdf_transit, | 131 IPC::PlatformFileForTransit pdf_transit, |
| 120 const PdfRenderSettings& settings, | 132 const PdfRenderSettings& settings, |
| 121 const PwgRasterSettings& bitmap_settings, | 133 const PwgRasterSettings& bitmap_settings, |
| 122 IPC::PlatformFileForTransit bitmap_transit) { | 134 IPC::PlatformFileForTransit bitmap_transit) { |
| 123 base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit); | 135 base::File pdf = IPC::PlatformFileForTransitToFile(pdf_transit); |
| 124 base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit); | 136 base::File bitmap = IPC::PlatformFileForTransitToFile(bitmap_transit); |
| 125 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, | 137 if (RenderPDFPagesToPWGRaster(std::move(pdf), settings, bitmap_settings, |
| 126 std::move(bitmap))) { | 138 std::move(bitmap))) { |
| 127 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); | 139 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded()); |
| 128 } else { | 140 } else { |
| 129 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); | 141 Send(new ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed()); |
| 130 } | 142 } |
| 131 ReleaseProcessIfNeeded(); | 143 ReleaseProcessIfNeeded(); |
| 132 } | 144 } |
| 133 #endif // ENABLE_PRINT_PREVIEW | 145 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 134 | 146 |
| 135 #if defined(OS_WIN) | 147 #if defined(OS_WIN) |
| 136 int PrintingHandler::LoadPDF(base::File pdf_file) { | 148 int PrintingHandler::LoadPDF(base::File pdf_file) { |
| 137 int64_t length64 = pdf_file.GetLength(); | 149 int64_t length64 = pdf_file.GetLength(); |
| 138 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) | 150 if (length64 <= 0 || length64 > std::numeric_limits<int>::max()) |
| 139 return 0; | 151 return 0; |
| 140 int length = static_cast<int>(length64); | 152 int length = static_cast<int>(length64); |
| 141 | 153 |
| 142 pdf_data_.resize(length); | 154 pdf_data_.resize(length); |
| 143 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) | 155 if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size())) |
| 144 return 0; | 156 return 0; |
| 145 | 157 |
| 146 int total_page_count = 0; | 158 int total_page_count = 0; |
| 147 if (!chrome_pdf::GetPDFDocInfo(&pdf_data_.front(), pdf_data_.size(), | 159 if (!chrome_pdf::GetPDFDocInfo(&pdf_data_.front(), pdf_data_.size(), |
| 148 &total_page_count, nullptr)) { | 160 &total_page_count, nullptr)) { |
| 149 return 0; | 161 return 0; |
| 150 } | 162 } |
| 151 return total_page_count; | 163 return total_page_count; |
| 152 } | 164 } |
| 153 | 165 |
| 154 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, | 166 bool PrintingHandler::RenderPdfPageToMetafile(int page_number, |
| 155 base::File output_file, | 167 base::File output_file, |
| 156 float* scale_factor) { | 168 float* scale_factor, |
| 169 bool postscript) { |
| 157 Emf metafile; | 170 Emf metafile; |
| 158 metafile.Init(); | 171 metafile.Init(); |
| 159 | 172 |
| 160 // We need to scale down DC to fit an entire page into DC available area. | 173 // We need to scale down DC to fit an entire page into DC available area. |
| 161 // Current metafile is based on screen DC and have current screen size. | 174 // Current metafile is based on screen DC and have current screen size. |
| 162 // Writing outside of those boundaries will result in the cut-off output. | 175 // Writing outside of those boundaries will result in the cut-off output. |
| 163 // On metafiles (this is the case here), scaling down will still record | 176 // On metafiles (this is the case here), scaling down will still record |
| 164 // original coordinates and we'll be able to print in full resolution. | 177 // original coordinates and we'll be able to print in full resolution. |
| 165 // Before playback we'll need to counter the scaling up that will happen | 178 // Before playback we'll need to counter the scaling up that will happen |
| 166 // in the service (print_system_win.cc). | 179 // in the service (print_system_win.cc). |
| 167 *scale_factor = gfx::CalculatePageScale( | 180 // |
| 168 metafile.context(), pdf_rendering_settings_.area.right(), | 181 // The postscript driver does not use the metafile size since it outputs |
| 169 pdf_rendering_settings_.area.bottom()); | 182 // postscript rather than a metafile. Instead it uses the printable area |
| 170 gfx::ScaleDC(metafile.context(), *scale_factor); | 183 // sent to RenderPDFPageToDC to determine the area to render. Therefore, |
| 184 // don't scale the DC to match the metafile, and send the printer physical |
| 185 // offsets to the driver. |
| 186 if (!postscript) { |
| 187 *scale_factor = gfx::CalculatePageScale( |
| 188 metafile.context(), pdf_rendering_settings_.area.right(), |
| 189 pdf_rendering_settings_.area.bottom()); |
| 190 gfx::ScaleDC(metafile.context(), *scale_factor); |
| 191 } |
| 171 | 192 |
| 172 // The underlying metafile is of type Emf and ignores the arguments passed | 193 // The underlying metafile is of type Emf and ignores the arguments passed |
| 173 // to StartPage. | 194 // to StartPage. |
| 174 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); | 195 metafile.StartPage(gfx::Size(), gfx::Rect(), 1); |
| 196 int offset_x = postscript ? pdf_rendering_settings_.offsets.x() : 0; |
| 197 int offset_y = postscript ? pdf_rendering_settings_.offsets.y() : 0; |
| 198 |
| 175 if (!chrome_pdf::RenderPDFPageToDC( | 199 if (!chrome_pdf::RenderPDFPageToDC( |
| 176 &pdf_data_.front(), pdf_data_.size(), page_number, metafile.context(), | 200 &pdf_data_.front(), pdf_data_.size(), page_number, metafile.context(), |
| 177 pdf_rendering_settings_.dpi, pdf_rendering_settings_.area.x(), | 201 pdf_rendering_settings_.dpi, |
| 178 pdf_rendering_settings_.area.y(), | 202 pdf_rendering_settings_.area.x() - offset_x, |
| 203 pdf_rendering_settings_.area.y() - offset_y, |
| 179 pdf_rendering_settings_.area.width(), | 204 pdf_rendering_settings_.area.width(), |
| 180 pdf_rendering_settings_.area.height(), true, false, true, true, | 205 pdf_rendering_settings_.area.height(), true, false, true, true, |
| 181 pdf_rendering_settings_.autorotate)) { | 206 pdf_rendering_settings_.autorotate)) { |
| 182 return false; | 207 return false; |
| 183 } | 208 } |
| 184 metafile.FinishPage(); | 209 metafile.FinishPage(); |
| 185 metafile.FinishDocument(); | 210 metafile.FinishDocument(); |
| 186 return metafile.SaveTo(&output_file); | 211 return metafile.SaveTo(&output_file); |
| 187 } | 212 } |
| 188 #endif // defined(OS_WIN) | 213 #endif // defined(OS_WIN) |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, | 327 if (print_backend->GetPrinterSemanticCapsAndDefaults(printer_name, |
| 303 &printer_info)) { | 328 &printer_info)) { |
| 304 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( | 329 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded( |
| 305 printer_name, printer_info)); | 330 printer_name, printer_info)); |
| 306 } else { | 331 } else { |
| 307 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( | 332 Send(new ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed( |
| 308 printer_name)); | 333 printer_name)); |
| 309 } | 334 } |
| 310 ReleaseProcessIfNeeded(); | 335 ReleaseProcessIfNeeded(); |
| 311 } | 336 } |
| 312 #endif // ENABLE_PRINT_PREVIEW | 337 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
| 313 | 338 |
| 314 } // namespace printing | 339 } // namespace printing |
| OLD | NEW |