OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
6 | 6 |
| 7 #include <limits> |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/big_endian.h" | 10 #include "base/big_endian.h" |
10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
12 #include "base/files/file.h" | 13 #include "base/files/file.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/ref_counted_memory.h" | 15 #include "base/memory/ref_counted_memory.h" |
15 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
16 #include "base/path_service.h" | 17 #include "base/path_service.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // TODO(yukishiino): Remove SetDefaultFontDescription() once the migration to | 77 // TODO(yukishiino): Remove SetDefaultFontDescription() once the migration to |
77 // the font list is done. We will no longer need SetDefaultFontDescription() | 78 // the font list is done. We will no longer need SetDefaultFontDescription() |
78 // after every client gets started using a FontList instead of a Font. | 79 // after every client gets started using a FontList instead of a Font. |
79 gfx::PlatformFontPango::SetDefaultFontDescription(font_family); | 80 gfx::PlatformFontPango::SetDefaultFontDescription(font_family); |
80 #else | 81 #else |
81 // Use a single default font as the default font list. | 82 // Use a single default font as the default font list. |
82 gfx::FontList::SetDefaultFontDescription(std::string()); | 83 gfx::FontList::SetDefaultFontDescription(std::string()); |
83 #endif | 84 #endif |
84 } | 85 } |
85 | 86 |
| 87 #if defined(OS_ANDROID) |
| 88 // Returns the scale factor closest to |scale| from the full list of factors. |
| 89 // Note that it does NOT rely on the list of supported scale factors. |
| 90 // Finding the closest match is inefficient and shouldn't be done frequently. |
| 91 ScaleFactor FindClosestScaleFactorUnsafe(float scale) { |
| 92 float smallest_diff = std::numeric_limits<float>::max(); |
| 93 ScaleFactor closest_match = SCALE_FACTOR_100P; |
| 94 for (int i = SCALE_FACTOR_100P; i < NUM_SCALE_FACTORS; ++i) { |
| 95 const ScaleFactor scale_factor = static_cast<ScaleFactor>(i); |
| 96 float diff = std::abs(GetScaleForScaleFactor(scale_factor) - scale); |
| 97 if (diff < smallest_diff) { |
| 98 closest_match = scale_factor; |
| 99 smallest_diff = diff; |
| 100 } |
| 101 } |
| 102 return closest_match; |
| 103 } |
| 104 #endif // OS_ANDROID |
| 105 |
86 } // namespace | 106 } // namespace |
87 | 107 |
88 // An ImageSkiaSource that loads bitmaps for the requested scale factor from | 108 // An ImageSkiaSource that loads bitmaps for the requested scale factor from |
89 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the | 109 // ResourceBundle on demand for a given |resource_id|. If the bitmap for the |
90 // requested scale factor does not exist, it will return the 1x bitmap scaled | 110 // requested scale factor does not exist, it will return the 1x bitmap scaled |
91 // by the scale factor. This may lead to broken UI if the correct size of the | 111 // by the scale factor. This may lead to broken UI if the correct size of the |
92 // scaled image is not exactly |scale_factor| * the size of the 1x resource. | 112 // scaled image is not exactly |scale_factor| * the size of the 1x resource. |
93 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images | 113 // When --highlight-missing-scaled-resources flag is specified, scaled 1x images |
94 // are higlighted by blending them with red. | 114 // are higlighted by blending them with red. |
95 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { | 115 class ResourceBundle::ResourceBundleImageSource : public gfx::ImageSkiaSource { |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 } | 527 } |
508 | 528 |
509 ScaleFactor ResourceBundle::GetMaxScaleFactor() const { | 529 ScaleFactor ResourceBundle::GetMaxScaleFactor() const { |
510 #if defined(OS_CHROMEOS) || defined(OS_WIN) | 530 #if defined(OS_CHROMEOS) || defined(OS_WIN) |
511 return max_scale_factor_; | 531 return max_scale_factor_; |
512 #else | 532 #else |
513 return GetSupportedScaleFactors().back(); | 533 return GetSupportedScaleFactors().back(); |
514 #endif | 534 #endif |
515 } | 535 } |
516 | 536 |
| 537 bool ResourceBundle::IsScaleFactorSupported(ScaleFactor scale_factor) { |
| 538 const std::vector<ScaleFactor>& supported_scale_factors = |
| 539 ui::GetSupportedScaleFactors(); |
| 540 return std::find(supported_scale_factors.begin(), |
| 541 supported_scale_factors.end(), |
| 542 scale_factor) != supported_scale_factors.end(); |
| 543 } |
| 544 |
517 ResourceBundle::ResourceBundle(Delegate* delegate) | 545 ResourceBundle::ResourceBundle(Delegate* delegate) |
518 : delegate_(delegate), | 546 : delegate_(delegate), |
519 images_and_fonts_lock_(new base::Lock), | 547 images_and_fonts_lock_(new base::Lock), |
520 locale_resources_data_lock_(new base::Lock), | 548 locale_resources_data_lock_(new base::Lock), |
521 max_scale_factor_(SCALE_FACTOR_100P) { | 549 max_scale_factor_(SCALE_FACTOR_100P) { |
522 } | 550 } |
523 | 551 |
524 ResourceBundle::~ResourceBundle() { | 552 ResourceBundle::~ResourceBundle() { |
525 FreeImages(); | 553 FreeImages(); |
526 UnloadLocaleResources(); | 554 UnloadLocaleResources(); |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 // static | 816 // static |
789 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 817 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
790 size_t size, | 818 size_t size, |
791 SkBitmap* bitmap, | 819 SkBitmap* bitmap, |
792 bool* fell_back_to_1x) { | 820 bool* fell_back_to_1x) { |
793 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 821 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
794 return gfx::PNGCodec::Decode(buf, size, bitmap); | 822 return gfx::PNGCodec::Decode(buf, size, bitmap); |
795 } | 823 } |
796 | 824 |
797 } // namespace ui | 825 } // namespace ui |
OLD | NEW |