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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 return gfx::ImageSkiaRep(image, scale); | 164 return gfx::ImageSkiaRep(image, scale); |
165 } | 165 } |
166 | 166 |
167 private: | 167 private: |
168 ResourceBundle* rb_; | 168 ResourceBundle* rb_; |
169 const int resource_id_; | 169 const int resource_id_; |
170 | 170 |
171 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); | 171 DISALLOW_COPY_AND_ASSIGN(ResourceBundleImageSource); |
172 }; | 172 }; |
173 | 173 |
| 174 struct ResourceBundle::FontKey { |
| 175 FontKey(int in_size_delta, |
| 176 gfx::Font::FontStyle in_style, |
| 177 gfx::Font::Weight in_weight) |
| 178 : size_delta(in_size_delta), style(in_style), weight(in_weight) {} |
| 179 |
| 180 ~FontKey() {} |
| 181 |
| 182 bool operator==(const FontKey& rhs) const { |
| 183 return std::tie(size_delta, style, weight) == |
| 184 std::tie(rhs.size_delta, rhs.style, rhs.weight); |
| 185 } |
| 186 |
| 187 bool operator<(const FontKey& rhs) const { |
| 188 return std::tie(size_delta, style, weight) < |
| 189 std::tie(rhs.size_delta, rhs.style, rhs.weight); |
| 190 } |
| 191 |
| 192 int size_delta; |
| 193 gfx::Font::FontStyle style; |
| 194 gfx::Font::Weight weight; |
| 195 }; |
| 196 |
174 // static | 197 // static |
175 std::string ResourceBundle::InitSharedInstanceWithLocale( | 198 std::string ResourceBundle::InitSharedInstanceWithLocale( |
176 const std::string& pref_locale, | 199 const std::string& pref_locale, |
177 Delegate* delegate, | 200 Delegate* delegate, |
178 LoadResources load_resources) { | 201 LoadResources load_resources) { |
179 InitSharedInstance(delegate); | 202 InitSharedInstance(delegate); |
180 if (load_resources == LOAD_COMMON_RESOURCES) | 203 if (load_resources == LOAD_COMMON_RESOURCES) |
181 g_shared_instance_->LoadCommonResources(); | 204 g_shared_instance_->LoadCommonResources(); |
182 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); | 205 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); |
183 g_shared_instance_->InitDefaultFontList(); | 206 g_shared_instance_->InitDefaultFontList(); |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), | 565 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), |
543 data.length() / 2); | 566 data.length() / 2); |
544 } else if (encoding == ResourceHandle::UTF8) { | 567 } else if (encoding == ResourceHandle::UTF8) { |
545 msg = base::UTF8ToUTF16(data); | 568 msg = base::UTF8ToUTF16(data); |
546 } | 569 } |
547 return msg; | 570 return msg; |
548 } | 571 } |
549 | 572 |
550 const gfx::FontList& ResourceBundle::GetFontListWithDelta( | 573 const gfx::FontList& ResourceBundle::GetFontListWithDelta( |
551 int size_delta, | 574 int size_delta, |
552 gfx::Font::FontStyle style) { | 575 gfx::Font::FontStyle style, |
| 576 gfx::Font::Weight weight) { |
553 base::AutoLock lock_scope(*images_and_fonts_lock_); | 577 base::AutoLock lock_scope(*images_and_fonts_lock_); |
554 | 578 |
555 typedef std::pair<int, gfx::Font::FontStyle> Key; | 579 const FontKey styled_key(size_delta, style, weight); |
556 const Key styled_key(size_delta, style); | |
557 | 580 |
558 auto found = font_cache_.find(styled_key); | 581 auto found = font_cache_.find(styled_key); |
559 if (found != font_cache_.end()) | 582 if (found != font_cache_.end()) |
560 return found->second; | 583 return found->second; |
561 | 584 |
562 const Key base_key(0, gfx::Font::NORMAL); | 585 const FontKey base_key(0, gfx::Font::NORMAL, gfx::Font::Weight::NORMAL); |
563 gfx::FontList& base = font_cache_[base_key]; | 586 gfx::FontList& base = font_cache_[base_key]; |
564 if (styled_key == base_key) | 587 if (styled_key == base_key) |
565 return base; | 588 return base; |
566 | 589 |
567 // Fonts of a given style are derived from the unstyled font of the same size. | 590 // Fonts of a given style are derived from the unstyled font of the same size. |
568 // Cache the unstyled font by first inserting a default-constructed font list. | 591 // Cache the unstyled font by first inserting a default-constructed font list. |
569 // Then, derive it for the initial insertion, or use the iterator that points | 592 // Then, derive it for the initial insertion, or use the iterator that points |
570 // to the existing entry that the insertion collided with. | 593 // to the existing entry that the insertion collided with. |
571 const Key sized_key(size_delta, gfx::Font::NORMAL); | 594 const FontKey sized_key(size_delta, gfx::Font::NORMAL, |
| 595 gfx::Font::Weight::NORMAL); |
572 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList())); | 596 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList())); |
573 if (sized.second) | 597 if (sized.second) |
574 sized.first->second = base.DeriveWithSizeDelta(size_delta); | 598 sized.first->second = base.DeriveWithSizeDelta(size_delta); |
575 if (styled_key == sized_key) | 599 if (styled_key == sized_key) |
576 return sized.first->second; | 600 return sized.first->second; |
577 | 601 |
578 auto styled = font_cache_.insert(std::make_pair(styled_key, gfx::FontList())); | 602 auto styled = font_cache_.insert(std::make_pair(styled_key, gfx::FontList())); |
579 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. | 603 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. |
580 styled.first->second = sized.first->second.DeriveWithStyle( | 604 styled.first->second = sized.first->second.Derive( |
581 sized.first->second.GetFontStyle() | style); | 605 0, sized.first->second.GetFontStyle() | style, weight); |
| 606 |
582 return styled.first->second; | 607 return styled.first->second; |
583 } | 608 } |
584 | 609 |
585 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, | 610 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, |
586 gfx::Font::FontStyle style) { | 611 gfx::Font::FontStyle style, |
587 return GetFontListWithDelta(size_delta, style).GetPrimaryFont(); | 612 gfx::Font::Weight weight) { |
| 613 return GetFontListWithDelta(size_delta, style, weight).GetPrimaryFont(); |
588 } | 614 } |
589 | 615 |
590 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { | 616 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { |
591 gfx::Font::FontStyle font_style = gfx::Font::NORMAL; | 617 gfx::Font::Weight font_weight = gfx::Font::Weight::NORMAL; |
592 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || | 618 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || |
593 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) | 619 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) |
594 font_style = gfx::Font::BOLD; | 620 font_weight = gfx::Font::Weight::BOLD; |
595 | 621 |
596 int size_delta = 0; | 622 int size_delta = 0; |
597 switch (legacy_style) { | 623 switch (legacy_style) { |
598 case SmallFont: | 624 case SmallFont: |
599 case SmallBoldFont: | 625 case SmallBoldFont: |
600 size_delta = kSmallFontDelta; | 626 size_delta = kSmallFontDelta; |
601 break; | 627 break; |
602 case MediumFont: | 628 case MediumFont: |
603 case MediumBoldFont: | 629 case MediumBoldFont: |
604 size_delta = kMediumFontDelta; | 630 size_delta = kMediumFontDelta; |
605 break; | 631 break; |
606 case LargeFont: | 632 case LargeFont: |
607 case LargeBoldFont: | 633 case LargeBoldFont: |
608 size_delta = kLargeFontDelta; | 634 size_delta = kLargeFontDelta; |
609 break; | 635 break; |
610 case BaseFont: | 636 case BaseFont: |
611 case BoldFont: | 637 case BoldFont: |
612 break; | 638 break; |
613 } | 639 } |
614 | 640 |
615 return GetFontListWithDelta(size_delta, font_style); | 641 return GetFontListWithDelta(size_delta, gfx::Font::NORMAL, font_weight); |
616 } | 642 } |
617 | 643 |
618 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { | 644 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { |
619 return GetFontList(style).GetPrimaryFont(); | 645 return GetFontList(style).GetPrimaryFont(); |
620 } | 646 } |
621 | 647 |
622 void ResourceBundle::ReloadFonts() { | 648 void ResourceBundle::ReloadFonts() { |
623 base::AutoLock lock_scope(*images_and_fonts_lock_); | 649 base::AutoLock lock_scope(*images_and_fonts_lock_); |
624 InitDefaultFontList(); | 650 InitDefaultFontList(); |
625 font_cache_.clear(); | 651 font_cache_.clear(); |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 // static | 922 // static |
897 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 923 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
898 size_t size, | 924 size_t size, |
899 SkBitmap* bitmap, | 925 SkBitmap* bitmap, |
900 bool* fell_back_to_1x) { | 926 bool* fell_back_to_1x) { |
901 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 927 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
902 return gfx::PNGCodec::Decode(buf, size, bitmap); | 928 return gfx::PNGCodec::Decode(buf, size, bitmap); |
903 } | 929 } |
904 | 930 |
905 } // namespace ui | 931 } // namespace ui |
OLD | NEW |