blob: 2727c6b6ebb9931e634a77d3fe053bc4035d986e [file] [log] [blame]
// Copyright 2021 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 "pdf/ui/document_properties.h"
#include <string>
#include "base/i18n/number_formatting.h"
#include "base/i18n/rtl.h"
#include "base/strings/string_number_conversions.h"
#include "components/strings/grit/components_strings.h"
#include "pdf/document_metadata.h"
#include "printing/units.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/icu/source/i18n/unicode/ulocdata.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/size.h"
using printing::kMicronsPerInch;
using printing::kPointsPerInch;
namespace chrome_pdf {
namespace {
// Scales `length_points` to be in inches instead of points.
constexpr float ConvertPointsToInches(int length_points) {
constexpr float kInchesPerPoint = 1.0f / kPointsPerInch;
return length_points * kInchesPerPoint;
// Scales `length_points` to be in millimeters instead of points.
constexpr float ConvertPointsToMillimeters(int length_points) {
constexpr float kMillimetersPerInch = 25.4f;
constexpr float kMillimetersPerPoint = kMillimetersPerInch / kPointsPerInch;
return length_points * kMillimetersPerPoint;
// Formats a length given in points. The formatted length is in inches and
// contains two fractional digits.
std::u16string FormatLengthInInches(int length_points) {
return base::FormatDouble(ConvertPointsToInches(length_points),
// Formats a length given in points. The formatted length is in millimeters and
// contains no fractional digits.
std::u16string FormatLengthInMillimeters(int length_points) {
return base::FormatDouble(ConvertPointsToMillimeters(length_points),
// Returns the localized string for the orientation.
std::u16string GetOrientation(const gfx::Size& size) {
// TODO( Add a string for square sizes such that they are
// not displayed as "portrait".
return l10n_util::GetStringUTF16(
size.height() >= size.width() ? IDS_PDF_PROPERTIES_PAGE_SIZE_PORTRAIT
bool ShowInches() {
UErrorCode error_code = U_ZERO_ERROR;
UMeasurementSystem system = ulocdata_getMeasurementSystem(
base::i18n::GetConfiguredLocale().c_str(), &error_code);
// On error, assume the units are SI.
return U_SUCCESS(error_code) && system == UMS_US;
} // namespace
std::u16string FormatPageSize(const absl::optional<gfx::Size>& size_points) {
if (!size_points.has_value())
// TODO(dhoss): Consider using `icu::number::NumberFormatter`.
if (ShowInches()) {
return l10n_util::GetStringFUTF16(
return l10n_util::GetStringFUTF16(
std::string FormatPdfVersion(PdfVersion version) {
switch (version) {
case PdfVersion::k1_0:
return "1.0";
case PdfVersion::k1_1:
return "1.1";
case PdfVersion::k1_2:
return "1.2";
case PdfVersion::k1_3:
return "1.3";
case PdfVersion::k1_4:
return "1.4";
case PdfVersion::k1_5:
return "1.5";
case PdfVersion::k1_6:
return "1.6";
case PdfVersion::k1_7:
return "1.7";
case PdfVersion::k2_0:
return "2.0";
case PdfVersion::kUnknown:
case PdfVersion::k1_8: // Not an actual version
return std::string();
// The default case is excluded from the above switch statement to ensure that
// all supported versions are determinantly handled.
} // namespace chrome_pdf