Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(778)

Unified Diff: printing/backend/cups_ipp_util.cc

Issue 2105463002: Create a new print backend for the updated CUPS APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix printing gyp file. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: printing/backend/cups_ipp_util.cc
diff --git a/printing/backend/cups_ipp_util.cc b/printing/backend/cups_ipp_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..eb9687f9ae91da9e2c80b8eb64fb0176fb824f7e
--- /dev/null
+++ b/printing/backend/cups_ipp_util.cc
@@ -0,0 +1,238 @@
+// Copyright 2016 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 "printing/backend/cups_ipp_util.h"
+
+#include <cups/cups.h>
+
+#include <algorithm>
+#include <string>
+#include <vector>
+
+#include "base/logging.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_piece.h"
+#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
+#include "printing/backend/cups_printer.h"
+#include "printing/backend/print_backend_consts.h"
+#include "printing/units.h"
+
+namespace printing {
+
+namespace {
+
+const char kIppCollate[] = "sheet-collate"; // RFC 3381
+const char kIppCopies[] = CUPS_COPIES;
+const char kIppColor[] = CUPS_PRINT_COLOR_MODE;
+const char kIppMedia[] = CUPS_MEDIA;
+const char kIppDuplex[] = CUPS_SIDES;
+
+const int kMicronsPerMM = 1000;
+const double kMMPerInch = 25.4;
+const double kMicronsPerInch = kMMPerInch * kMicronsPerMM;
+
+enum Unit {
+ INCHES,
+ MILLIMETERS,
+};
+
+bool PrinterSupportsValue(const CupsPrinter& printer,
+ base::StringPiece name,
+ base::StringPiece value) {
+ std::vector<base::StringPiece> values =
+ printer.GetSupportedOptionValueStrings(name);
+ auto iter = std::find(values.begin(), values.end(), value);
+ return iter != values.end();
+}
+
+DuplexMode PrinterDefaultDuplex(const CupsPrinter& printer) {
+ ipp_attribute_t* attr = printer.GetDefaultOptionValue(kIppDuplex);
+ if (attr == nullptr) {
+ return UNKNOWN_DUPLEX_MODE;
+ }
+
+ const char* value = ippGetString(attr, 0, NULL);
+ if (base::EqualsCaseInsensitiveASCII(value, CUPS_SIDES_ONE_SIDED))
+ return SIMPLEX;
+ if (base::EqualsCaseInsensitiveASCII(value, CUPS_SIDES_TWO_SIDED_PORTRAIT))
+ return LONG_EDGE;
+ if (base::EqualsCaseInsensitiveASCII(value, CUPS_SIDES_TWO_SIDED_LANDSCAPE))
+ return SHORT_EDGE;
+
+ return UNKNOWN_DUPLEX_MODE;
+}
+
+gfx::Size DimensionsToMicrons(base::StringPiece value) {
+ Unit unit;
+ base::StringPiece dims;
+ size_t unit_position;
+ if ((unit_position = value.find("mm")) != base::StringPiece::npos) {
+ unit = MILLIMETERS;
+ dims = value.substr(0, unit_position);
+ } else if ((unit_position = value.find("in")) != base::StringPiece::npos) {
+ unit = INCHES;
+ dims = value.substr(0, unit_position);
+ } else {
+ LOG(WARNING) << "Could not parse paper dimensions";
+ return {0, 0};
+ }
+
+ std::vector<std::string> pieces = base::SplitString(
+ dims, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
+ double width;
+ double height;
+
+ if (pieces.size() != 2 || !base::StringToDouble(pieces[0], &width) ||
+ !base::StringToDouble(pieces[1], &height)) {
+ return {0, 0};
+ }
+
+ int width_microns;
+ int height_microns;
+
+ switch (unit) {
+ case MILLIMETERS:
+ width_microns = width * kMicronsPerMM;
+ height_microns = height * kMicronsPerMM;
+ break;
+ case INCHES:
+ width_microns = width * kMicronsPerInch;
+ height_microns = height * kMicronsPerInch;
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+
+ return gfx::Size{width_microns, height_microns};
+}
+
+PrinterSemanticCapsAndDefaults::Paper ParsePaper(base::StringPiece value) {
+ // <name>_<width>x<height>{in,mm}
+ // e.g. na_letter_8.5x11in, iso_a4_210x297mm
+
+ std::vector<base::StringPiece> pieces = base::SplitStringPiece(
+ value, "_", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
+ if (pieces.size() < 2) {
+ // we expect at least a display string and a dimension string
+ return PrinterSemanticCapsAndDefaults::Paper();
+ }
+
+ base::StringPiece dimensions = pieces.back();
+
+ std::string display = pieces[0].as_string();
+ for (size_t i = 1; i <= pieces.size() - 2; ++i) {
+ display.append(" ");
+ pieces[i].AppendToString(&display);
+ }
+
+ PrinterSemanticCapsAndDefaults::Paper paper;
+ paper.display_name = display;
+ paper.vendor_id = value.as_string();
+ paper.size_um = DimensionsToMicrons(dimensions);
+
+ return paper;
+}
+
+} // namespace
+
+void ExtractColor(const CupsPrinter& printer,
+ PrinterSemanticCapsAndDefaults* printer_info) {
+ std::vector<base::StringPiece> color_modes =
+ printer.GetSupportedOptionValueStrings(kIppColor);
+ auto iter = std::find(color_modes.begin(), color_modes.end(),
+ CUPS_PRINT_COLOR_MODE_COLOR);
+ if (iter != color_modes.end()) {
+ printer_info->color_model = COLORMODE_COLOR;
+ } else {
+ printer_info->color_model = UNKNOWN_COLOR_MODEL;
+ }
+
+ iter = std::find(color_modes.begin(), color_modes.end(),
+ CUPS_PRINT_COLOR_MODE_MONOCHROME);
+ if (iter != color_modes.end()) {
+ printer_info->bw_model = COLORMODE_MONOCHROME;
+ } else {
+ printer_info->bw_model = UNKNOWN_COLOR_MODEL;
+ }
+
+ printer_info->color_changeable =
+ (printer_info->color_model != UNKNOWN_COLOR_MODEL &&
+ printer_info->bw_model != UNKNOWN_COLOR_MODEL);
+
+ // default color
+ ipp_attribute_t* attr = printer.GetDefaultOptionValue(kIppColor);
+ if (attr != nullptr) {
+ const char* value = ippGetString(attr, 0, NULL);
+ printer_info->color_default = !base::EqualsCaseInsensitiveASCII(
+ value, CUPS_PRINT_COLOR_MODE_MONOCHROME);
+ }
+}
+
+PrinterSemanticCapsAndDefaults::Paper DefaultPaper(const CupsPrinter& printer) {
+ ipp_attribute_t* attr = printer.GetDefaultOptionValue(kIppMedia);
+ if (attr == nullptr) {
+ return PrinterSemanticCapsAndDefaults::Paper();
+ }
+
+ const char* value = ippGetString(attr, 0, NULL);
+ return ParsePaper(value);
+}
+
+void ExtractPaper(const CupsPrinter& printer,
+ PrinterSemanticCapsAndDefaults* printer_info) {
+ // paper
+ std::vector<base::StringPiece> papers =
+ printer.GetSupportedOptionValueStrings(kIppMedia);
+ for (base::StringPiece paper : papers) {
+ PrinterSemanticCapsAndDefaults::Paper p = ParsePaper(paper);
+ printer_info->papers.push_back(p);
+ }
+
+ // default paper
+ printer_info->default_paper = DefaultPaper(printer);
+}
+
+void ExtractCopies(const CupsPrinter& printer,
+ PrinterSemanticCapsAndDefaults* printer_info) {
+ // copies
+ ipp_attribute_t* attr = printer.GetSupportedOptionValues(kIppCopies);
+ if (attr != nullptr) {
+ int upper_bound;
+ int lower_bound = ippGetRange(attr, 0, &upper_bound);
+ printer_info->copies_capable = (lower_bound != -1) && (upper_bound >= 2);
+ } else {
+ printer_info->copies_capable = false;
+ }
+}
+
+void ExtractCollate(const CupsPrinter& printer,
+ PrinterSemanticCapsAndDefaults* printer_info) {
+ ipp_attribute_t* attr;
+
+ // collate
+ std::vector<base::StringPiece> values =
+ printer.GetSupportedOptionValueStrings(kIppCollate);
+ auto iter = std::find(values.begin(), values.end(), "collated");
+ printer_info->collate_capable = iter != values.end();
+
+ // collate default
+ attr = printer.GetDefaultOptionValue(kIppCollate);
+ if (attr != nullptr) {
+ base::StringPiece name = ippGetString(attr, 0, NULL);
+ printer_info->collate_default = name.compare("collated") == 0;
+ } else {
+ printer_info->collate_default = false;
+ }
+}
+
+void ExtractDuplex(const CupsPrinter& printer,
+ PrinterSemanticCapsAndDefaults* printer_info) {
+ printer_info->duplex_capable =
+ PrinterSupportsValue(printer, kIppDuplex, CUPS_SIDES_TWO_SIDED_PORTRAIT);
+ printer_info->duplex_default = PrinterDefaultDuplex(printer);
+}
+
+} // namespace printing

Powered by Google App Engine
This is Rietveld 408576698