#include "printing/printing_utils.h"
#include <algorithm>
#include <cstring>
#include <string>
#include <string_view>
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "printing/mojom/print.mojom.h"
#include "third_party/icu/source/common/unicode/uchar.h"
#include "ui/gfx/text_elider.h"
#if BUILDFLAG(USE_CUPS)
#include <unicode/ulocdata.h>
#include <cmath>
#include "printing/units.h"
#include "ui/gfx/geometry/size.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include "printing/printing_features.h"
#endif
namespace printing {
namespace {
constexpr size_t kMaxDocumentTitleLength = …;
#if BUILDFLAG(USE_CUPS)
constexpr gfx::Size kIsoA4Microns = …;
#endif
}
std::u16string SimplifyDocumentTitleWithLength(const std::u16string& title,
size_t length) { … }
std::u16string FormatDocumentTitleWithOwnerAndLength(
const std::u16string& owner,
const std::u16string& title,
size_t length) { … }
std::u16string SimplifyDocumentTitle(const std::u16string& title) { … }
std::u16string FormatDocumentTitleWithOwner(const std::u16string& owner,
const std::u16string& title) { … }
#if BUILDFLAG(USE_CUPS)
gfx::Size GetDefaultPaperSizeFromLocaleMicrons(std::string_view locale) { … }
bool SizesEqualWithinEpsilon(const gfx::Size& lhs,
const gfx::Size& rhs,
int epsilon) { … }
#endif
#if BUILDFLAG(IS_WIN)
gfx::Rect GetCenteredPageContentRect(const gfx::Size& paper_size,
const gfx::Size& page_size,
const gfx::Rect& page_content_rect) {
gfx::Rect content_rect = page_content_rect;
if (paper_size.width() > page_size.width()) {
int diff = paper_size.width() - page_size.width();
content_rect.set_x(content_rect.x() + diff / 2);
}
if (paper_size.height() > page_size.height()) {
int diff = paper_size.height() - page_size.height();
content_rect.set_y(content_rect.y() + diff / 2);
}
return content_rect;
}
gfx::Rect GetPrintableAreaDeviceUnits(HDC hdc) {
DCHECK(hdc);
gfx::Size physical_size_device_units(GetDeviceCaps(hdc, PHYSICALWIDTH),
GetDeviceCaps(hdc, PHYSICALHEIGHT));
gfx::Rect printable_area_device_units(
GetDeviceCaps(hdc, PHYSICALOFFSETX), GetDeviceCaps(hdc, PHYSICALOFFSETY),
GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES));
if (printable_area_device_units.IsEmpty() ||
!gfx::Rect(physical_size_device_units)
.Contains(printable_area_device_units)) {
printable_area_device_units = gfx::Rect(physical_size_device_units);
}
return printable_area_device_units;
}
DocumentDataType DetermineDocumentDataType(base::span<const uint8_t> data) {
if (LooksLikePdf(data)) {
return DocumentDataType::kPdf;
}
if (LooksLikeXps(data)) {
return DocumentDataType::kXps;
}
return DocumentDataType::kUnknown;
}
bool LooksLikeXps(base::span<const uint8_t> maybe_xps_data) {
constexpr auto kXpsStartsWith = base::span_from_cstring("PK\x03\x04");
return maybe_xps_data.size() >= 2000u &&
maybe_xps_data.first(kXpsStartsWith.size()) == kXpsStartsWith;
}
#endif
bool LooksLikePdf(base::span<const uint8_t> maybe_pdf_data) { … }
}