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::FontWeight 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 return size_delta < rhs.size_delta || style < rhs.style || | |
|
msw
2016/03/22 01:53:43
This key comparator may be problematic, the conver
tapted
2016/03/22 04:04:17
yeah - comparators need to obey "strict weak order
Mikus
2016/03/22 14:19:50
Done.
Mikus
2016/03/22 14:19:50
Done.
| |
| 176 weight < rhs.weight; | |
| 177 } | |
| 178 | |
| 179 int size_delta; | |
| 180 gfx::Font::FontStyle style; | |
| 181 gfx::Font::FontWeight weight; | |
| 182 }; | |
| 183 | |
| 161 // static | 184 // static |
| 162 std::string ResourceBundle::InitSharedInstanceWithLocale( | 185 std::string ResourceBundle::InitSharedInstanceWithLocale( |
| 163 const std::string& pref_locale, | 186 const std::string& pref_locale, |
| 164 Delegate* delegate, | 187 Delegate* delegate, |
| 165 LoadResources load_resources) { | 188 LoadResources load_resources) { |
| 166 InitSharedInstance(delegate); | 189 InitSharedInstance(delegate); |
| 167 if (load_resources == LOAD_COMMON_RESOURCES) | 190 if (load_resources == LOAD_COMMON_RESOURCES) |
| 168 g_shared_instance_->LoadCommonResources(); | 191 g_shared_instance_->LoadCommonResources(); |
| 169 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); | 192 std::string result = g_shared_instance_->LoadLocaleResources(pref_locale); |
| 170 g_shared_instance_->InitDefaultFontList(); | 193 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()), | 549 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), |
| 527 data.length() / 2); | 550 data.length() / 2); |
| 528 } else if (encoding == ResourceHandle::UTF8) { | 551 } else if (encoding == ResourceHandle::UTF8) { |
| 529 msg = base::UTF8ToUTF16(data); | 552 msg = base::UTF8ToUTF16(data); |
| 530 } | 553 } |
| 531 return msg; | 554 return msg; |
| 532 } | 555 } |
| 533 | 556 |
| 534 const gfx::FontList& ResourceBundle::GetFontListWithDelta( | 557 const gfx::FontList& ResourceBundle::GetFontListWithDelta( |
| 535 int size_delta, | 558 int size_delta, |
| 536 gfx::Font::FontStyle style) { | 559 gfx::Font::FontStyle style, |
| 560 gfx::Font::FontWeight weight) { | |
| 537 base::AutoLock lock_scope(*images_and_fonts_lock_); | 561 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 538 | 562 |
| 539 typedef std::pair<int, gfx::Font::FontStyle> Key; | 563 const FontKey key = {size_delta, style, weight}; |
|
tapted
2016/03/22 04:04:17
These will call the constructor using C++11 "Unifo
Mikus
2016/03/22 14:19:50
But this is a POD object, we can use this for stru
tapted
2016/03/22 23:31:32
You can't use aggregate initialization if there's
| |
| 540 const Key styled_key(size_delta, style); | |
| 541 | 564 |
| 542 auto found = font_cache_.find(styled_key); | 565 auto found = font_cache_.find(key); |
| 543 if (found != font_cache_.end()) | 566 if (found != font_cache_.end()) |
| 544 return found->second; | 567 return found->second; |
| 545 | 568 |
| 546 const Key base_key(0, gfx::Font::NORMAL); | 569 const FontKey base_key = {0, gfx::Font::NORMAL, gfx::Font::WEIGHT_NORMAL}; |
| 547 gfx::FontList& base = font_cache_[base_key]; | 570 gfx::FontList& base = font_cache_[base_key]; |
| 548 if (styled_key == base_key) | 571 if (key == base_key) |
| 549 return base; | 572 return base; |
| 550 | 573 |
| 551 // Fonts of a given style are derived from the unstyled font of the same size. | 574 // 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. | 575 // 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 | 576 // Then, derive it for the initial insertion, or use the iterator that points |
| 554 // to the existing entry that the insertion collided with. | 577 // to the existing entry that the insertion collided with. |
| 555 const Key sized_key(size_delta, gfx::Font::NORMAL); | 578 const FontKey sized_key = {size_delta, gfx::Font::NORMAL, weight}; |
|
tapted
2016/03/22 04:04:17
This needs to pass gfx::Font::WEIGHT_NORMAL, not |
Mikus
2016/03/22 14:19:50
Done.
| |
| 556 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList())); | 579 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList())); |
| 557 if (sized.second) | 580 if (sized.second) |
| 558 sized.first->second = base.DeriveWithSizeDelta(size_delta); | 581 sized.first->second = base.DeriveWithSizeDelta(size_delta); |
| 559 if (styled_key == sized_key) | 582 if (key == sized_key) |
|
tapted
2016/03/22 04:04:17
Unless I've missed something, this should go back
Mikus
2016/03/22 14:19:50
Done.
| |
| 560 return sized.first->second; | 583 return sized.first->second; |
| 561 | 584 |
| 562 auto styled = font_cache_.insert(std::make_pair(styled_key, gfx::FontList())); | 585 auto styled = font_cache_.insert(std::make_pair(sized_key, gfx::FontList())); |
|
tapted
2016/03/22 04:04:17
sized_key -> styled_key
| |
| 563 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. | 586 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. |
| 564 styled.first->second = sized.first->second.DeriveWithStyle( | 587 styled.first->second = sized.first->second.DeriveWithStyle( |
| 565 sized.first->second.GetFontStyle() | style); | 588 sized.first->second.GetFontStyle() | style); |
| 589 styled.first->second = sized.first->second.DeriveWithWeight(weight); | |
|
msw
2016/03/22 01:53:43
Can/should we avoid the second derive call and jus
Mikus
2016/03/22 14:19:50
Done.
| |
| 590 | |
| 566 return styled.first->second; | 591 return styled.first->second; |
| 567 } | 592 } |
| 568 | 593 |
| 569 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, | 594 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, |
| 570 gfx::Font::FontStyle style) { | 595 gfx::Font::FontStyle style) { |
| 571 return GetFontListWithDelta(size_delta, style).GetPrimaryFont(); | 596 return GetFontListWithDelta(size_delta, style).GetPrimaryFont(); |
| 572 } | 597 } |
| 573 | 598 |
| 574 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { | 599 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { |
| 575 gfx::Font::FontStyle font_style = gfx::Font::NORMAL; | 600 gfx::Font::FontWeight font_weight = gfx::Font::WEIGHT_NORMAL; |
| 576 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || | 601 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || |
| 577 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) | 602 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) |
| 578 font_style = gfx::Font::BOLD; | 603 font_weight = gfx::Font::WEIGHT_BOLD; |
| 579 | 604 |
| 580 int size_delta = 0; | 605 int size_delta = 0; |
| 581 switch (legacy_style) { | 606 switch (legacy_style) { |
| 582 case SmallFont: | 607 case SmallFont: |
| 583 case SmallBoldFont: | 608 case SmallBoldFont: |
| 584 size_delta = kSmallFontDelta; | 609 size_delta = kSmallFontDelta; |
| 585 break; | 610 break; |
| 586 case MediumFont: | 611 case MediumFont: |
| 587 case MediumBoldFont: | 612 case MediumBoldFont: |
| 588 size_delta = kMediumFontDelta; | 613 size_delta = kMediumFontDelta; |
| 589 break; | 614 break; |
| 590 case LargeFont: | 615 case LargeFont: |
| 591 case LargeBoldFont: | 616 case LargeBoldFont: |
| 592 size_delta = kLargeFontDelta; | 617 size_delta = kLargeFontDelta; |
| 593 break; | 618 break; |
| 594 case BaseFont: | 619 case BaseFont: |
| 595 case BoldFont: | 620 case BoldFont: |
| 596 break; | 621 break; |
| 597 } | 622 } |
| 598 | 623 |
| 599 return GetFontListWithDelta(size_delta, font_style); | 624 return GetFontListWithDelta(size_delta, gfx::Font::NORMAL, font_weight); |
| 600 } | 625 } |
| 601 | 626 |
| 602 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { | 627 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { |
| 603 return GetFontList(style).GetPrimaryFont(); | 628 return GetFontList(style).GetPrimaryFont(); |
| 604 } | 629 } |
| 605 | 630 |
| 606 void ResourceBundle::ReloadFonts() { | 631 void ResourceBundle::ReloadFonts() { |
| 607 base::AutoLock lock_scope(*images_and_fonts_lock_); | 632 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 608 InitDefaultFontList(); | 633 InitDefaultFontList(); |
| 609 font_cache_.clear(); | 634 font_cache_.clear(); |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 883 // static | 908 // static |
| 884 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 909 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
| 885 size_t size, | 910 size_t size, |
| 886 SkBitmap* bitmap, | 911 SkBitmap* bitmap, |
| 887 bool* fell_back_to_1x) { | 912 bool* fell_back_to_1x) { |
| 888 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 913 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
| 889 return gfx::PNGCodec::Decode(buf, size, bitmap); | 914 return gfx::PNGCodec::Decode(buf, size, bitmap); |
| 890 } | 915 } |
| 891 | 916 |
| 892 } // namespace ui | 917 } // namespace ui |
| OLD | NEW |