2014-07-03 20:34:58 +02:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/utility/printing_handler.h"
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
#include "base/files/file_util.h"
|
2014-07-03 20:34:58 +02:00
|
|
|
#include "base/lazy_instance.h"
|
|
|
|
#include "base/path_service.h"
|
|
|
|
#include "base/scoped_native_library.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
#include "build/build_config.h"
|
2014-07-03 20:34:58 +02:00
|
|
|
#include "chrome/common/chrome_paths.h"
|
|
|
|
#include "chrome/common/chrome_utility_printing_messages.h"
|
|
|
|
#include "content/public/utility/utility_thread.h"
|
|
|
|
#include "printing/page_range.h"
|
|
|
|
#include "printing/pdf_render_settings.h"
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "base/win/iat_patch_function.h"
|
|
|
|
#include "printing/emf_win.h"
|
|
|
|
#include "ui/gfx/gdi_util.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool Send(IPC::Message* message) {
|
|
|
|
return content::UtilityThread::Get()->Send(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseProcessIfNeeded() {
|
|
|
|
content::UtilityThread::Get()->ReleaseProcessIfNeeded();
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
void PreCacheFontCharacters(const LOGFONT* logfont,
|
|
|
|
const wchar_t* text,
|
|
|
|
size_t text_length) {
|
|
|
|
Send(new ChromeUtilityHostMsg_PreCacheFontCharacters(
|
|
|
|
*logfont, base::string16(text, text_length)));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 20:34:58 +02:00
|
|
|
} // namespace
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
PrintingHandler::PrintingHandler() {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
chrome_pdf::SetPDFEnsureTypefaceCharactersAccessible(PreCacheFontCharacters);
|
|
|
|
#endif
|
|
|
|
}
|
2014-07-03 20:34:58 +02:00
|
|
|
|
|
|
|
PrintingHandler::~PrintingHandler() {}
|
|
|
|
|
|
|
|
bool PrintingHandler::OnMessageReceived(const IPC::Message& message) {
|
|
|
|
bool handled = true;
|
|
|
|
IPC_BEGIN_MESSAGE_MAP(PrintingHandler, message)
|
2014-09-27 01:48:19 +02:00
|
|
|
#if defined(OS_WIN)
|
2014-07-03 20:34:58 +02:00
|
|
|
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles,
|
|
|
|
OnRenderPDFPagesToMetafile)
|
2014-09-27 01:48:19 +02:00
|
|
|
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage,
|
|
|
|
OnRenderPDFPagesToMetafileGetPage)
|
|
|
|
IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop,
|
|
|
|
OnRenderPDFPagesToMetafileStop)
|
|
|
|
#endif // OS_WIN
|
2014-07-03 20:34:58 +02:00
|
|
|
IPC_MESSAGE_UNHANDLED(handled = false)
|
|
|
|
IPC_END_MESSAGE_MAP()
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
#if defined(OS_WIN)
|
2014-07-03 20:34:58 +02:00
|
|
|
void PrintingHandler::OnRenderPDFPagesToMetafile(
|
|
|
|
IPC::PlatformFileForTransit pdf_transit,
|
2016-08-31 13:25:56 +02:00
|
|
|
const printing::PdfRenderSettings& settings,
|
|
|
|
bool print_text_with_gdi) {
|
2014-09-27 01:48:19 +02:00
|
|
|
pdf_rendering_settings_ = settings;
|
2016-08-31 13:25:56 +02:00
|
|
|
chrome_pdf::SetPDFUseGDIPrinting(print_text_with_gdi);
|
2014-07-03 20:34:58 +02:00
|
|
|
base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit);
|
2016-01-06 20:20:54 +01:00
|
|
|
int page_count = LoadPDF(std::move(pdf_file));
|
2014-09-27 01:48:19 +02:00
|
|
|
Send(
|
|
|
|
new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount(page_count));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintingHandler::OnRenderPDFPagesToMetafileGetPage(
|
|
|
|
int page_number,
|
|
|
|
IPC::PlatformFileForTransit output_file) {
|
|
|
|
base::File emf_file = IPC::PlatformFileForTransitToFile(output_file);
|
2014-07-03 20:34:58 +02:00
|
|
|
double scale_factor = 1.0;
|
2014-09-27 01:48:19 +02:00
|
|
|
bool success =
|
2016-01-06 20:20:54 +01:00
|
|
|
RenderPdfPageToMetafile(page_number, std::move(emf_file), &scale_factor);
|
2014-09-27 01:48:19 +02:00
|
|
|
Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone(
|
|
|
|
success, scale_factor));
|
2014-07-03 20:34:58 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
void PrintingHandler::OnRenderPDFPagesToMetafileStop() {
|
|
|
|
ReleaseProcessIfNeeded();
|
|
|
|
}
|
2014-07-03 20:34:58 +02:00
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
int PrintingHandler::LoadPDF(base::File pdf_file) {
|
2016-01-06 20:20:54 +01:00
|
|
|
int64_t length = pdf_file.GetLength();
|
2014-07-03 20:34:58 +02:00
|
|
|
if (length < 0)
|
2014-09-27 01:48:19 +02:00
|
|
|
return 0;
|
2014-07-03 20:34:58 +02:00
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
pdf_data_.resize(length);
|
|
|
|
if (length != pdf_file.Read(0, pdf_data_.data(), pdf_data_.size()))
|
|
|
|
return 0;
|
2014-07-03 20:34:58 +02:00
|
|
|
|
|
|
|
int total_page_count = 0;
|
2015-03-04 02:00:13 +01:00
|
|
|
if (!chrome_pdf::GetPDFDocInfo(
|
2014-09-27 01:48:19 +02:00
|
|
|
&pdf_data_.front(), pdf_data_.size(), &total_page_count, NULL)) {
|
|
|
|
return 0;
|
2014-07-03 20:34:58 +02:00
|
|
|
}
|
2014-09-27 01:48:19 +02:00
|
|
|
return total_page_count;
|
2014-07-03 20:34:58 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
bool PrintingHandler::RenderPdfPageToMetafile(int page_number,
|
|
|
|
base::File output_file,
|
|
|
|
double* scale_factor) {
|
|
|
|
printing::Emf metafile;
|
|
|
|
metafile.Init();
|
|
|
|
|
|
|
|
// We need to scale down DC to fit an entire page into DC available area.
|
|
|
|
// Current metafile is based on screen DC and have current screen size.
|
|
|
|
// Writing outside of those boundaries will result in the cut-off output.
|
|
|
|
// On metafiles (this is the case here), scaling down will still record
|
|
|
|
// original coordinates and we'll be able to print in full resolution.
|
|
|
|
// Before playback we'll need to counter the scaling up that will happen
|
|
|
|
// in the service (print_system_win.cc).
|
|
|
|
*scale_factor =
|
|
|
|
gfx::CalculatePageScale(metafile.context(),
|
|
|
|
pdf_rendering_settings_.area().right(),
|
|
|
|
pdf_rendering_settings_.area().bottom());
|
|
|
|
gfx::ScaleDC(metafile.context(), *scale_factor);
|
|
|
|
|
|
|
|
// The underlying metafile is of type Emf and ignores the arguments passed
|
|
|
|
// to StartPage.
|
|
|
|
metafile.StartPage(gfx::Size(), gfx::Rect(), 1);
|
2015-03-04 02:00:13 +01:00
|
|
|
if (!chrome_pdf::RenderPDFPageToDC(
|
2014-09-27 01:48:19 +02:00
|
|
|
&pdf_data_.front(),
|
|
|
|
pdf_data_.size(),
|
|
|
|
page_number,
|
|
|
|
metafile.context(),
|
|
|
|
pdf_rendering_settings_.dpi(),
|
|
|
|
pdf_rendering_settings_.area().x(),
|
|
|
|
pdf_rendering_settings_.area().y(),
|
|
|
|
pdf_rendering_settings_.area().width(),
|
|
|
|
pdf_rendering_settings_.area().height(),
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
pdf_rendering_settings_.autorotate())) {
|
|
|
|
return false;
|
2014-07-03 20:34:58 +02:00
|
|
|
}
|
2014-09-27 01:48:19 +02:00
|
|
|
metafile.FinishPage();
|
|
|
|
metafile.FinishDocument();
|
|
|
|
return metafile.SaveTo(&output_file);
|
2014-07-03 20:34:58 +02:00
|
|
|
}
|
2014-09-27 01:48:19 +02:00
|
|
|
|
|
|
|
#endif // OS_WIN
|
|
|
|
|