1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/resource/resource_scale_factor.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <iterator>
#include <limits>
#include <memory>
#include <vector>
#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
namespace ui {
namespace {
std::vector<ResourceScaleFactor>* g_supported_resource_scale_factors = nullptr;
constexpr auto kResourceScaleFactorScales = std::to_array<float>({
1.0f,
1.0f,
2.0f,
3.0f,
});
static_assert(NUM_SCALE_FACTORS == std::size(kResourceScaleFactorScales),
"kResourceScaleFactorScales has incorrect size");
// The difference to fall back to the smaller scale factor rather than the
// larger one. For example, assume 1.20 is requested but only 1.0 and 2.0 are
// supported. In that case, not fall back to 2.0 but 1.0, and then expand
// the image to 1.20.
const float kFallbackToSmallerScaleDiff = 0.20f;
} // namespace
float GetScaleForResourceScaleFactor(ResourceScaleFactor scale_factor) {
return kResourceScaleFactorScales[scale_factor];
}
void SetSupportedResourceScaleFactors(
const std::vector<ResourceScaleFactor>& scale_factors) {
CHECK(!scale_factors.empty());
if (g_supported_resource_scale_factors != nullptr) {
delete g_supported_resource_scale_factors;
}
g_supported_resource_scale_factors =
new std::vector<ResourceScaleFactor>(scale_factors);
std::sort(g_supported_resource_scale_factors->begin(),
g_supported_resource_scale_factors->end(),
[](ResourceScaleFactor lhs, ResourceScaleFactor rhs) {
return kResourceScaleFactorScales[lhs] <
kResourceScaleFactorScales[rhs];
});
}
const std::vector<ResourceScaleFactor>& GetSupportedResourceScaleFactors() {
CHECK_NE(g_supported_resource_scale_factors, nullptr)
<< "ResourceBundle needs to be initialized.";
return *g_supported_resource_scale_factors;
}
ResourceScaleFactor GetSupportedResourceScaleFactor(float scale) {
CHECK_NE(g_supported_resource_scale_factors, nullptr)
<< "ResourceBundle needs to be initialized.";
ResourceScaleFactor closest_match = k100Percent;
float smallest_diff = std::numeric_limits<float>::max();
for (const auto scale_factor : *g_supported_resource_scale_factors) {
const float diff =
std::abs(kResourceScaleFactorScales[scale_factor] - scale);
if (diff < smallest_diff) {
closest_match = scale_factor;
smallest_diff = diff;
} else {
break;
}
}
CHECK_NE(closest_match, kScaleFactorNone);
return closest_match;
}
ResourceScaleFactor GetSupportedResourceScaleFactorForRescale(float scale) {
CHECK_NE(g_supported_resource_scale_factors, nullptr)
<< "ResourceBundle needs to be initialized.";
// Returns an exact match, a smaller scale within
// `kFallbackToSmallerScaleDiff` units, the nearest larger scale, or the max
// supported scale.
for (auto supported_scale : *g_supported_resource_scale_factors) {
if (kResourceScaleFactorScales[supported_scale] +
kFallbackToSmallerScaleDiff >=
scale) {
return supported_scale;
}
}
return GetMaxSupportedResourceScaleFactor();
}
ResourceScaleFactor GetMaxSupportedResourceScaleFactor() {
CHECK_NE(g_supported_resource_scale_factors, nullptr)
<< "ResourceBundle needs to be initialized.";
ResourceScaleFactor max_scale = g_supported_resource_scale_factors->back();
CHECK_NE(max_scale, kScaleFactorNone);
return max_scale;
}
float GetScaleForMaxSupportedResourceScaleFactor() {
return kResourceScaleFactorScales[GetMaxSupportedResourceScaleFactor()];
}
bool IsScaleFactorSupported(ResourceScaleFactor scale_factor) {
CHECK_NE(g_supported_resource_scale_factors, nullptr)
<< "ResourceBundle needs to be initialized.";
return base::Contains(*g_supported_resource_scale_factors, scale_factor);
}
namespace test {
ScopedSetSupportedResourceScaleFactors::ScopedSetSupportedResourceScaleFactors(
const std::vector<ResourceScaleFactor>& new_scale_factors) {
if (g_supported_resource_scale_factors) {
original_scale_factors_ =
std::make_unique<std::vector<ResourceScaleFactor>>(
*g_supported_resource_scale_factors);
}
SetSupportedResourceScaleFactors(new_scale_factors);
}
ScopedSetSupportedResourceScaleFactors::
~ScopedSetSupportedResourceScaleFactors() {
if (original_scale_factors_) {
SetSupportedResourceScaleFactors(*original_scale_factors_);
} else {
delete g_supported_resource_scale_factors;
g_supported_resource_scale_factors = nullptr;
}
}
} // namespace test
} // namespace ui
|