Skip to content

Commit

Permalink
Add generic text printing
Browse files Browse the repository at this point in the history
Depends on https://pdfium-review.googlesource.com/c/7194

BUG=734850

Review-Url: https://codereview.chromium.org/2970473002
Cr-Commit-Position: refs/heads/master@{#486892}
  • Loading branch information
rbpotter authored and Commit Bot committed Jul 14, 2017
1 parent 4272e0f commit 0437a17
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 18 deletions.
3 changes: 2 additions & 1 deletion chrome/browser/printing/pdf_to_emf_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ PdfConverterUtilityProcessHostClient::GetFileFromTemp(
std::unique_ptr<base::File, content::BrowserThread::DeleteOnFileThread>
temp_file) {
if (settings_.mode == PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2 ||
settings_.mode == PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3) {
settings_.mode == PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3 ||
settings_.mode == PdfRenderSettings::Mode::TEXTONLY) {
return base::MakeUnique<PostScriptMetaFile>(temp_dir_,
std::move(temp_file));
}
Expand Down
15 changes: 15 additions & 0 deletions chrome/browser/printing/print_job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,21 @@ void PrintJob::OnPdfPageConverted(int page_number,
base::Bind(&PrintJob::OnPdfPageConverted, this));
}

void PrintJob::StartPdfToTextConversion(
const scoped_refptr<base::RefCountedMemory>& bytes,
const gfx::Size& page_size) {
DCHECK(!pdf_conversion_state_);
pdf_conversion_state_ =
base::MakeUnique<PdfConversionState>(gfx::Size(), gfx::Rect());
const int kPrinterDpi = settings().dpi();
gfx::Rect page_area = gfx::Rect(0, 0, page_size.width(), page_size.height());
PdfRenderSettings settings(page_area, gfx::Point(0, 0), kPrinterDpi,
/*autorotate=*/true,
PdfRenderSettings::Mode::TEXTONLY);
pdf_conversion_state_->Start(
bytes, settings, base::Bind(&PrintJob::OnPdfConversionStarted, this));
}

void PrintJob::StartPdfToPostScriptConversion(
const scoped_refptr<base::RefCountedMemory>& bytes,
const gfx::Rect& content_area,
Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/printing/print_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class PrintJob : public PrintJobWorkerOwner,
const gfx::Rect& content_area,
const gfx::Point& physical_offset,
bool ps_level2);

void StartPdfToTextConversion(
const scoped_refptr<base::RefCountedMemory>& bytes,
const gfx::Size& page_size);
#endif // defined(OS_WIN)

protected:
Expand Down
7 changes: 5 additions & 2 deletions chrome/browser/printing/print_view_manager_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,11 @@ void PrintViewManagerBase::OnDidPrintPage(
document->DebugDumpData(bytes.get(), FILE_PATH_LITERAL(".pdf"));

const auto& settings = document->settings();
if ((settings.printer_is_ps2() || settings.printer_is_ps3()) &&
!base::FeatureList::IsEnabled(features::kDisablePostScriptPrinting)) {
if (settings.printer_is_textonly()) {
print_job_->StartPdfToTextConversion(bytes, params.page_size);
} else if ((settings.printer_is_ps2() || settings.printer_is_ps3()) &&
!base::FeatureList::IsEnabled(
features::kDisablePostScriptPrinting)) {
print_job_->StartPdfToPostScriptConversion(bytes, params.content_area,
params.physical_offsets,
settings.printer_is_ps2());
Expand Down
14 changes: 9 additions & 5 deletions chrome/utility/printing_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,22 @@ void PrintingHandler::OnRenderPDFPagesToMetafile(
pdf_rendering_settings_ = settings;
chrome_pdf::SetPDFUseGDIPrinting(pdf_rendering_settings_.mode ==
PdfRenderSettings::Mode::GDI_TEXT);
int postscript_level;
int printing_mode;
switch (pdf_rendering_settings_.mode) {
case PdfRenderSettings::Mode::TEXTONLY:
printing_mode = chrome_pdf::PrintingMode::kTextOnly;
break;
case PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2:
postscript_level = 2;
printing_mode = chrome_pdf::PrintingMode::kPostScript2;
break;
case PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3:
postscript_level = 3;
printing_mode = chrome_pdf::PrintingMode::kPostScript3;
break;
default:
postscript_level = 0; // Not using postscript.
// Not using postscript or text only.
printing_mode = chrome_pdf::PrintingMode::kEmf;
}
chrome_pdf::SetPDFPostscriptPrintingLevel(postscript_level);
chrome_pdf::SetPDFUsePrintMode(printing_mode);

base::File pdf_file = IPC::PlatformFileForTransitToFile(pdf_transit);
int page_count = LoadPDF(std::move(pdf_file));
Expand Down
4 changes: 2 additions & 2 deletions pdf/pdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ void SetPDFUseGDIPrinting(bool enable) {
PDFEngineExports::Get()->SetPDFUseGDIPrinting(enable);
}

void SetPDFPostscriptPrintingLevel(int postscript_level) {
PDFEngineExports::Get()->SetPDFPostscriptPrintingLevel(postscript_level);
void SetPDFUsePrintMode(int mode) {
PDFEngineExports::Get()->SetPDFUsePrintMode(mode);
}
#endif // defined(OS_WIN)

Expand Down
10 changes: 9 additions & 1 deletion pdf/pdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ void PPP_ShutdownModule();
const void* PPP_GetInterface(const char* interface_name);

#if defined(OS_WIN)
// Printing modes - type to convert PDF to for printing
enum PrintingMode {
kEmf = 0,
kTextOnly = 1,
kPostScript2 = 2,
kPostScript3 = 3,
};

// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
// |buffer_size| is the size of |pdf_buffer| in bytes.
Expand Down Expand Up @@ -83,7 +91,7 @@ void SetPDFEnsureTypefaceCharactersAccessible(

void SetPDFUseGDIPrinting(bool enable);

void SetPDFPostscriptPrintingLevel(int postscript_level);
void SetPDFUsePrintMode(int mode);
#endif // defined(OS_WIN)

// |page_count| and |max_page_width| are optional and can be NULL.
Expand Down
3 changes: 1 addition & 2 deletions pdf/pdf_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,7 @@ class PDFEngineExports {
PDFEnsureTypefaceCharactersAccessible func) = 0;

virtual void SetPDFUseGDIPrinting(bool enable) = 0;

virtual void SetPDFPostscriptPrintingLevel(int postscript_level) = 0;
virtual void SetPDFUsePrintMode(int mode) = 0;
#endif // defined(OS_WIN)

// See the definition of RenderPDFPageToBitmap in pdf.cc for details.
Expand Down
10 changes: 10 additions & 0 deletions pdf/pdfium/pdfium_assert_matching_enums.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "build/build_config.h"
#include "pdf/pdf.h"
#include "ppapi/c/pp_input_event.h"
#include "ppapi/c/private/ppb_pdf.h"
#include "ppapi/c/private/ppp_pdf.h"
#include "third_party/pdfium/public/fpdf_edit.h"
#include "third_party/pdfium/public/fpdf_fwlevent.h"
#include "third_party/pdfium/public/fpdf_sysfontinfo.h"
#include "ui/events/keycodes/keyboard_codes.h"
Expand Down Expand Up @@ -210,3 +213,10 @@ STATIC_ASSERT_ENUM(PP_PRIVATEDUPLEXMODE_NONE, DuplexUndefined);
STATIC_ASSERT_ENUM(PP_PRIVATEDUPLEXMODE_SIMPLEX, Simplex);
STATIC_ASSERT_ENUM(PP_PRIVATEDUPLEXMODE_SHORT_EDGE, DuplexFlipShortEdge);
STATIC_ASSERT_ENUM(PP_PRIVATEDUPLEXMODE_LONG_EDGE, DuplexFlipLongEdge);

#if defined(OS_WIN)
STATIC_ASSERT_ENUM(chrome_pdf::kEmf, FPDF_PRINTMODE_EMF);
STATIC_ASSERT_ENUM(chrome_pdf::kTextOnly, FPDF_PRINTMODE_TEXTONLY);
STATIC_ASSERT_ENUM(chrome_pdf::kPostScript2, FPDF_PRINTMODE_POSTSCRIPT2);
STATIC_ASSERT_ENUM(chrome_pdf::kPostScript3, FPDF_PRINTMODE_POSTSCRIPT3);
#endif
5 changes: 2 additions & 3 deletions pdf/pdfium/pdfium_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4154,10 +4154,9 @@ void PDFiumEngineExports::SetPDFUseGDIPrinting(bool enable) {
FPDF_SetPrintTextWithGDI(enable);
}

void PDFiumEngineExports::SetPDFPostscriptPrintingLevel(int postscript_level) {
FPDF_SetPrintPostscriptLevel(postscript_level);
void PDFiumEngineExports::SetPDFUsePrintMode(int mode) {
FPDF_SetPrintMode(mode);
}

#endif // defined(OS_WIN)

bool PDFiumEngineExports::RenderPDFPageToBitmap(
Expand Down
3 changes: 1 addition & 2 deletions pdf/pdfium/pdfium_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,7 @@ class PDFiumEngineExports : public PDFEngineExports {
PDFEnsureTypefaceCharactersAccessible func) override;

void SetPDFUseGDIPrinting(bool enable) override;

void SetPDFPostscriptPrintingLevel(int postscript_level) override;
void SetPDFUsePrintMode(int mode) override;
#endif // defined(OS_WIN)
bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
Expand Down
1 change: 1 addition & 0 deletions printing/pdf_render_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct PdfRenderSettings {
enum Mode {
NORMAL = 0,
#if defined(OS_WIN)
TEXTONLY,
GDI_TEXT,
POSTSCRIPT_LEVEL2,
POSTSCRIPT_LEVEL3,
Expand Down
4 changes: 4 additions & 0 deletions printing/print_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class PRINTING_EXPORT PrintSettings {
#if defined(OS_WIN)
enum PrinterType {
TYPE_NONE = 0,
TYPE_TEXTONLY,
TYPE_XPS,
TYPE_POSTSCRIPT_LEVEL2,
TYPE_POSTSCRIPT_LEVEL3
Expand Down Expand Up @@ -171,6 +172,9 @@ class PRINTING_EXPORT PrintSettings {
bool print_text_with_gdi() const { return print_text_with_gdi_; }

void set_printer_type(PrinterType type) { printer_type_ = type; }
bool printer_is_textonly() const {
return printer_type_ == PrinterType::TYPE_TEXTONLY;
}
bool printer_is_xps() const { return printer_type_ == PrinterType::TYPE_XPS;}
bool printer_is_ps2() const {
return printer_type_ == PrinterType::TYPE_POSTSCRIPT_LEVEL2;
Expand Down
8 changes: 8 additions & 0 deletions printing/print_settings_initializer_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ bool IsPrinterXPS(HDC hdc) {
return IsTechnology(hdc, kXPSDriver);
}

bool IsPrinterTextOnly(HDC hdc) {
return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM;
}
} // namespace

// static
Expand Down Expand Up @@ -153,6 +156,11 @@ void PrintSettingsInitializerWin::InitPrintSettings(
PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL3);
return;
}
// Detects the generic / text only driver.
if (IsPrinterTextOnly(hdc)) {
print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_TEXTONLY);
return;
}
if (IsPrinterXPS(hdc)) {
print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS);
return;
Expand Down

0 comments on commit 0437a17

Please sign in to comment.