Chromium Code Reviews| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 return gfx::ImageSkiaRep(image, scale); | 151 return gfx::ImageSkiaRep(image, scale); |
| 152 } | 152 } |
| 153 | 153 |
| 154 private: | 154 private: |
| 155 ResourceBundle* rb_; | 155 ResourceBundle* rb_; |
| 156 const int resource_id_; | 156 const int resource_id_; |
| 157 | 157 |
| 158 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); | 158 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); |
| 159 }; | 159 }; |
| 160 | 160 |
| 161 struct ResourceBundle::FontKey { | |
| 162 FontKey(int in_size_delta, | |
| 163 gfx::Font::FontStyle in_style, | |
| 164 gfx::Font::Weight in_weight) | |
| 165 : size_delta(in_size_delta), style(in_style), weight(in_weight) {} | |
| 166 | |
| 167 ~FontKey() {} | |
| 168 | |
| 169 bool operator==(const FontKey& rhs) const { | |
| 170 return size_delta == rhs.size_delta && style == rhs.style && | |
| 171 weight == rhs.weight; | |
| 172 } | |
| 173 | |
| 174 bool operator<(const FontKey& rhs) const { | |
| 175 if (size_delta != rhs.size_delta) | |
|
sky
2016/03/31 17:04:08
use std::tie
Mikus
2016/04/04 12:16:24
Done.
| |
| 176 return size_delta < rhs.size_delta; | |
| 177 if (style != rhs.style) | |
| 178 return style < rhs.style; | |
| 179 return weight < rhs.weight; | |
| 180 } | |
| 181 | |
| 182 int size_delta; | |
| 183 gfx::Font::FontStyle style; | |
| 184 gfx::Font::Weight weight; | |
| 185 }; | |
| 186 | |
| 161 // static | 187 // static |
| 162 std::string ResourceBundle::InitSharedInstanceWithLocale( | 188 std::string ResourceBundle::InitSharedInstanceWithLocale( |
| 163 const std::string& pref_locale, | 189 const std::string& pref_locale, |
| 164 Delegate* delegate, | 190 Delegate* delegate, |
| 165 LoadResources load_resources) { | 191 LoadResources load_resources) { |
| 166 InitSharedInstance(delegate); | 192 InitSharedInstance(delegate); |
| 167 if (load_resources == LOAD_COMMON_RESOURCES) | 193 if (load_resources == LOAD_COMMON_RESOURCES) |
| 168 g_shared_instance_->LoadCommonResources(); | 194 g_shared_instance_->LoadCommonResources(); |
| 169 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); | 195 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); |
| 170 g_shared_instance_->InitDefaultFontList(); | 196 g_shared_instance_->InitDefaultFontList(); |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 526 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), | 552 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), |
| 527 data.length() / 2); | 553 data.length() / 2); |
| 528 } else if (encoding == ResourceHandle::UTF8) { | 554 } else if (encoding == ResourceHandle::UTF8) { |
| 529 msg = base::UTF8ToUTF16(data); | 555 msg = base::UTF8ToUTF16(data); |
| 530 } | 556 } |
| 531 return msg; | 557 return msg; |
| 532 } | 558 } |
| 533 | 559 |
| 534 const gfx::FontList& ResourceBundle::GetFontListWithDelta( | 560 const gfx::FontList& ResourceBundle::GetFontListWithDelta( |
| 535 int size_delta, | 561 int size_delta, |
| 536 gfx::Font::FontStyle style) { | 562 gfx::Font::FontStyle style, |
| 563 gfx::Font::Weight weight) { | |
| 537 base::AutoLock lock_scope(*images_and_fonts_lock_); | 564 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 538 | 565 |
| 539 typedef std::pair<int, gfx::Font::FontStyle> Key; | 566 const FontKey styled_key(size_delta, style, weight); |
| 540 const Key styled_key(size_delta, style); | |
| 541 | 567 |
| 542 auto found = font_cache_.find(styled_key); | 568 auto found = font_cache_.find(styled_key); |
| 543 if (found != font_cache_.end()) | 569 if (found != font_cache_.end()) |
| 544 return found->second; | 570 return found->second; |
| 545 | 571 |
| 546 const Key base_key(0, gfx::Font::NORMAL); | 572 const FontKey base_key(0, gfx::Font::NORMAL, gfx::Font::Weight::NORMAL); |
| 547 gfx::FontList& base = font_cache_[base_key]; | 573 gfx::FontList& base = font_cache_[base_key]; |
| 548 if (styled_key == base_key) | 574 if (styled_key == base_key) |
| 549 return base; | 575 return base; |
| 550 | 576 |
| 551 // Fonts of a given style are derived from the unstyled font of the same size. | 577 // Fonts of a given style are derived from the unstyled font of the same size. |
| 552 // Cache the unstyled font by first inserting a default-constructed font list. | 578 // Cache the unstyled font by first inserting a default-constructed font list. |
| 553 // Then, derive it for the initial insertion, or use the iterator that points | 579 // Then, derive it for the initial insertion, or use the iterator that points |
| 554 // to the existing entry that the insertion collided with. | 580 // to the existing entry that the insertion collided with. |
| 555 const Key sized_key(size_delta, gfx::Font::NORMAL); | 581 const FontKey sized_key(size_delta, gfx::Font::NORMAL, |
| 582 gfx::Font::Weight::NORMAL); | |
| 556 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList())); | 583 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList())); |
| 557 if (sized.second) | 584 if (sized.second) |
| 558 sized.first->second = base.DeriveWithSizeDelta(size_delta); | 585 sized.first->second = base.DeriveWithSizeDelta(size_delta); |
| 559 if (styled_key == sized_key) | 586 if (styled_key == sized_key) |
| 560 return sized.first->second; | 587 return sized.first->second; |
| 561 | 588 |
| 562 auto styled = font_cache_.insert(std::make_pair(styled_key, gfx::FontList())); | 589 auto styled = font_cache_.insert(std::make_pair(styled_key, gfx::FontList())); |
| 563 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. | 590 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. |
| 564 styled.first->second = sized.first->second.DeriveWithStyle( | 591 styled.first->second = sized.first->second.Derive( |
| 565 sized.first->second.GetFontStyle() | style); | 592 0, sized.first->second.GetFontStyle() | style, weight); |
| 593 | |
| 566 return styled.first->second; | 594 return styled.first->second; |
| 567 } | 595 } |
| 568 | 596 |
| 569 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, | 597 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, |
| 570 gfx::Font::FontStyle style) { | 598 gfx::Font::FontStyle style, |
| 571 return GetFontListWithDelta(size_delta, style).GetPrimaryFont(); | 599 gfx::Font::Weight weight) { |
| 600 return GetFontListWithDelta(size_delta, style, weight).GetPrimaryFont(); | |
| 572 } | 601 } |
| 573 | 602 |
| 574 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { | 603 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { |
| 575 gfx::Font::FontStyle font_style = gfx::Font::NORMAL; | 604 gfx::Font::Weight font_weight = gfx::Font::Weight::NORMAL; |
| 576 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || | 605 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || |
| 577 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) | 606 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) |
| 578 font_style = gfx::Font::BOLD; | 607 font_weight = gfx::Font::Weight::BOLD; |
| 579 | 608 |
| 580 int size_delta = 0; | 609 int size_delta = 0; |
| 581 switch (legacy_style) { | 610 switch (legacy_style) { |
| 582 case SmallFont: | 611 case SmallFont: |
| 583 case SmallBoldFont: | 612 case SmallBoldFont: |
| 584 size_delta = kSmallFontDelta; | 613 size_delta = kSmallFontDelta; |
| 585 break; | 614 break; |
| 586 case MediumFont: | 615 case MediumFont: |
| 587 case MediumBoldFont: | 616 case MediumBoldFont: |
| 588 size_delta = kMediumFontDelta; | 617 size_delta = kMediumFontDelta; |
| 589 break; | 618 break; |
| 590 case LargeFont: | 619 case LargeFont: |
| 591 case LargeBoldFont: | 620 case LargeBoldFont: |
| 592 size_delta = kLargeFontDelta; | 621 size_delta = kLargeFontDelta; |
| 593 break; | 622 break; |
| 594 case BaseFont: | 623 case BaseFont: |
| 595 case BoldFont: | 624 case BoldFont: |
| 596 break; | 625 break; |
| 597 } | 626 } |
| 598 | 627 |
| 599 return GetFontListWithDelta(size_delta, font_style); | 628 return GetFontListWithDelta(size_delta, gfx::Font::NORMAL, font_weight); |
| 600 } | 629 } |
| 601 | 630 |
| 602 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { | 631 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { |
| 603 return GetFontList(style).GetPrimaryFont(); | 632 return GetFontList(style).GetPrimaryFont(); |
| 604 } | 633 } |
| 605 | 634 |
| 606 void ResourceBundle::ReloadFonts() { | 635 void ResourceBundle::ReloadFonts() { |
| 607 base::AutoLock lock_scope(*images_and_fonts_lock_); | 636 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 608 InitDefaultFontList(); | 637 InitDefaultFontList(); |
| 609 font_cache_.clear(); | 638 font_cache_.clear(); |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 883 // static | 912 // static |
| 884 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 913 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
| 885 size_t size, | 914 size_t size, |
| 886 SkBitmap* bitmap, | 915 SkBitmap* bitmap, |
| 887 bool* fell_back_to_1x) { | 916 bool* fell_back_to_1x) { |
| 888 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 917 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
| 889 return gfx::PNGCodec::Decode(buf, size, bitmap); | 918 return gfx::PNGCodec::Decode(buf, size, bitmap); |
| 890 } | 919 } |
| 891 | 920 |
| 892 } // namespace ui | 921 } // namespace ui |
| OLD | NEW |