Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(906)

Side by Side Diff: ui/base/resource/resource_bundle.cc

Issue 1819753003: Allow various font weights in gfx. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use the std::tie in resource_bundle Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 std::tie(size_delta, style, weight) ==
171 std::tie(rhs.size_delta, rhs.style, rhs.weight);
172 }
173
174 bool operator<(const FontKey& rhs) const {
175 return std::tie(size_delta, style, weight) <
176 std::tie(rhs.size_delta, rhs.style, rhs.weight);
177 }
178
179 int size_delta;
180 gfx::Font::FontStyle style;
181 gfx::Font::Weight 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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), 551 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()),
529 data.length() / 2); 552 data.length() / 2);
530 } else if (encoding == ResourceHandle::UTF8) { 553 } else if (encoding == ResourceHandle::UTF8) {
531 msg = base::UTF8ToUTF16(data); 554 msg = base::UTF8ToUTF16(data);
532 } 555 }
533 return msg; 556 return msg;
534 } 557 }
535 558
536 const gfx::FontList& ResourceBundle::GetFontListWithDelta( 559 const gfx::FontList& ResourceBundle::GetFontListWithDelta(
537 int size_delta, 560 int size_delta,
538 gfx::Font::FontStyle style) { 561 gfx::Font::FontStyle style,
562 gfx::Font::Weight weight) {
539 base::AutoLock lock_scope(*images_and_fonts_lock_); 563 base::AutoLock lock_scope(*images_and_fonts_lock_);
540 564
541 typedef std::pair<int, gfx::Font::FontStyle> Key; 565 const FontKey styled_key(size_delta, style, weight);
542 const Key styled_key(size_delta, style);
543 566
544 auto found = font_cache_.find(styled_key); 567 auto found = font_cache_.find(styled_key);
545 if (found != font_cache_.end()) 568 if (found != font_cache_.end())
546 return found->second; 569 return found->second;
547 570
548 const Key base_key(0, gfx::Font::NORMAL); 571 const FontKey base_key(0, gfx::Font::NORMAL, gfx::Font::Weight::NORMAL);
549 gfx::FontList& base = font_cache_[base_key]; 572 gfx::FontList& base = font_cache_[base_key];
550 if (styled_key == base_key) 573 if (styled_key == base_key)
551 return base; 574 return base;
552 575
553 // Fonts of a given style are derived from the unstyled font of the same size. 576 // Fonts of a given style are derived from the unstyled font of the same size.
554 // Cache the unstyled font by first inserting a default-constructed font list. 577 // Cache the unstyled font by first inserting a default-constructed font list.
555 // Then, derive it for the initial insertion, or use the iterator that points 578 // Then, derive it for the initial insertion, or use the iterator that points
556 // to the existing entry that the insertion collided with. 579 // to the existing entry that the insertion collided with.
557 const Key sized_key(size_delta, gfx::Font::NORMAL); 580 const FontKey sized_key(size_delta, gfx::Font::NORMAL,
581 gfx::Font::Weight::NORMAL);
558 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList())); 582 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList()));
559 if (sized.second) 583 if (sized.second)
560 sized.first->second = base.DeriveWithSizeDelta(size_delta); 584 sized.first->second = base.DeriveWithSizeDelta(size_delta);
561 if (styled_key == sized_key) 585 if (styled_key == sized_key)
562 return sized.first->second; 586 return sized.first->second;
563 587
564 auto styled = font_cache_.insert(std::make_pair(styled_key, gfx::FontList())); 588 auto styled = font_cache_.insert(std::make_pair(styled_key, gfx::FontList()));
565 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it. 589 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it.
566 styled.first->second = sized.first->second.DeriveWithStyle( 590 styled.first->second = sized.first->second.Derive(
567 sized.first->second.GetFontStyle() | style); 591 0, sized.first->second.GetFontStyle() | style, weight);
592
568 return styled.first->second; 593 return styled.first->second;
569 } 594 }
570 595
571 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta, 596 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta,
572 gfx::Font::FontStyle style) { 597 gfx::Font::FontStyle style,
573 return GetFontListWithDelta(size_delta, style).GetPrimaryFont(); 598 gfx::Font::Weight weight) {
599 return GetFontListWithDelta(size_delta, style, weight).GetPrimaryFont();
574 } 600 }
575 601
576 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) { 602 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) {
577 gfx::Font::FontStyle font_style = gfx::Font::NORMAL; 603 gfx::Font::Weight font_weight = gfx::Font::Weight::NORMAL;
578 if (legacy_style == BoldFont || legacy_style == SmallBoldFont || 604 if (legacy_style == BoldFont || legacy_style == SmallBoldFont ||
579 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont) 605 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont)
580 font_style = gfx::Font::BOLD; 606 font_weight = gfx::Font::Weight::BOLD;
581 607
582 int size_delta = 0; 608 int size_delta = 0;
583 switch (legacy_style) { 609 switch (legacy_style) {
584 case SmallFont: 610 case SmallFont:
585 case SmallBoldFont: 611 case SmallBoldFont:
586 size_delta = kSmallFontDelta; 612 size_delta = kSmallFontDelta;
587 break; 613 break;
588 case MediumFont: 614 case MediumFont:
589 case MediumBoldFont: 615 case MediumBoldFont:
590 size_delta = kMediumFontDelta; 616 size_delta = kMediumFontDelta;
591 break; 617 break;
592 case LargeFont: 618 case LargeFont:
593 case LargeBoldFont: 619 case LargeBoldFont:
594 size_delta = kLargeFontDelta; 620 size_delta = kLargeFontDelta;
595 break; 621 break;
596 case BaseFont: 622 case BaseFont:
597 case BoldFont: 623 case BoldFont:
598 break; 624 break;
599 } 625 }
600 626
601 return GetFontListWithDelta(size_delta, font_style); 627 return GetFontListWithDelta(size_delta, gfx::Font::NORMAL, font_weight);
602 } 628 }
603 629
604 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { 630 const gfx::Font& ResourceBundle::GetFont(FontStyle style) {
605 return GetFontList(style).GetPrimaryFont(); 631 return GetFontList(style).GetPrimaryFont();
606 } 632 }
607 633
608 void ResourceBundle::ReloadFonts() { 634 void ResourceBundle::ReloadFonts() {
609 base::AutoLock lock_scope(*images_and_fonts_lock_); 635 base::AutoLock lock_scope(*images_and_fonts_lock_);
610 InitDefaultFontList(); 636 InitDefaultFontList();
611 font_cache_.clear(); 637 font_cache_.clear();
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 // static 897 // static
872 bool ResourceBundle::DecodePNG(const unsigned char* buf, 898 bool ResourceBundle::DecodePNG(const unsigned char* buf,
873 size_t size, 899 size_t size,
874 SkBitmap* bitmap, 900 SkBitmap* bitmap,
875 bool* fell_back_to_1x) { 901 bool* fell_back_to_1x) {
876 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 902 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
877 return gfx::PNGCodec::Decode(buf, size, bitmap); 903 return gfx::PNGCodec::Decode(buf, size, bitmap);
878 } 904 }
879 905
880 } // namespace ui 906 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698