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) | |
| 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}; | |
|
tapted
2016/03/22 23:31:32
running `git cl format` should update this to a mo
Mikus
2016/03/23 17:53:21
This is what clang actually does
| |
| 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); |
|
msw
2016/03/22 18:24:10
Isn't the style of the |sized| font gfx::Font::Nor
tapted
2016/03/22 23:31:32
This confused me initially too. There's actually n
Mikus
2016/03/23 17:53:21
Acknowledged.
| |
| 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 return GetFontListWithDelta(size_delta, style).GetPrimaryFont(); |
| 572 } | 600 } |
| 573 | 601 |
| 574 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { | 602 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { |
| 575 gfx::Font::FontStyle font_style = gfx::Font::NORMAL; | 603 gfx::Font::Weight font_weight = gfx::Font::Weight::NORMAL; |
| 576 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || | 604 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || |
| 577 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) | 605 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) |
| 578 font_style = gfx::Font::BOLD; | 606 font_weight = gfx::Font::Weight::BOLD; |
| 579 | 607 |
| 580 int size_delta = 0; | 608 int size_delta = 0; |
| 581 switch (legacy_style) { | 609 switch (legacy_style) { |
| 582 case SmallFont: | 610 case SmallFont: |
| 583 case SmallBoldFont: | 611 case SmallBoldFont: |
| 584 size_delta = kSmallFontDelta; | 612 size_delta = kSmallFontDelta; |
| 585 break; | 613 break; |
| 586 case MediumFont: | 614 case MediumFont: |
| 587 case MediumBoldFont: | 615 case MediumBoldFont: |
| 588 size_delta = kMediumFontDelta; | 616 size_delta = kMediumFontDelta; |
| 589 break; | 617 break; |
| 590 case LargeFont: | 618 case LargeFont: |
| 591 case LargeBoldFont: | 619 case LargeBoldFont: |
| 592 size_delta = kLargeFontDelta; | 620 size_delta = kLargeFontDelta; |
| 593 break; | 621 break; |
| 594 case BaseFont: | 622 case BaseFont: |
| 595 case BoldFont: | 623 case BoldFont: |
| 596 break; | 624 break; |
| 597 } | 625 } |
| 598 | 626 |
| 599 return GetFontListWithDelta(size_delta, font_style); | 627 return GetFontListWithDelta(size_delta, gfx::Font::NORMAL, font_weight); |
| 600 } | 628 } |
| 601 | 629 |
| 602 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { | 630 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { |
| 603 return GetFontList(style).GetPrimaryFont(); | 631 return GetFontList(style).GetPrimaryFont(); |
| 604 } | 632 } |
| 605 | 633 |
| 606 void ResourceBundle::ReloadFonts() { | 634 void ResourceBundle::ReloadFonts() { |
| 607 base::AutoLock lock_scope(*images_and_fonts_lock_); | 635 base::AutoLock lock_scope(*images_and_fonts_lock_); |
| 608 InitDefaultFontList(); | 636 InitDefaultFontList(); |
| 609 font_cache_.clear(); | 637 font_cache_.clear(); |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 883 // static | 911 // static |
| 884 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 912 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
| 885 size_t size, | 913 size_t size, |
| 886 SkBitmap* bitmap, | 914 SkBitmap* bitmap, |
| 887 bool* fell_back_to_1x) { | 915 bool* fell_back_to_1x) { |
| 888 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 916 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
| 889 return gfx::PNGCodec::Decode(buf, size, bitmap); | 917 return gfx::PNGCodec::Decode(buf, size, bitmap); |
| 890 } | 918 } |
| 891 | 919 |
| 892 } // namespace ui | 920 } // namespace ui |
| OLD | NEW |