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

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

Issue 1689623004: Start removing enum ui::ResourceBundle::FontStyle, fix MacViews font sizes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix transcription error Created 4 years, 10 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
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/ui_base.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 #endif 55 #endif
56 56
57 #if defined(OS_MACOSX) && !defined(OS_IOS) 57 #if defined(OS_MACOSX) && !defined(OS_IOS)
58 #include "base/mac/mac_util.h" 58 #include "base/mac/mac_util.h"
59 #endif 59 #endif
60 60
61 namespace ui { 61 namespace ui {
62 62
63 namespace { 63 namespace {
64 64
65 // Font sizes relative to base font.
66 const int kSmallFontSizeDelta = -1;
67 const int kMediumFontSizeDelta = 3;
68 const int kLargeFontSizeDelta = 8;
69
70 // PNG-related constants. 65 // PNG-related constants.
71 const unsigned char kPngMagic[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 }; 66 const unsigned char kPngMagic[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
72 const size_t kPngChunkMetadataSize = 12; // length, type, crc32 67 const size_t kPngChunkMetadataSize = 12; // length, type, crc32
73 const unsigned char kPngScaleChunkType[4] = { 'c', 's', 'C', 'l' }; 68 const unsigned char kPngScaleChunkType[4] = { 'c', 's', 'C', 'l' };
74 const unsigned char kPngDataChunkType[4] = { 'I', 'D', 'A', 'T' }; 69 const unsigned char kPngDataChunkType[4] = { 'I', 'D', 'A', 'T' };
75 70
76 #if !defined(OS_MACOSX) 71 #if !defined(OS_MACOSX)
77 const char kPakFileSuffix[] = ".pak"; 72 const char kPakFileSuffix[] = ".pak";
78 #endif 73 #endif
79 74
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 base::string16 msg; 523 base::string16 msg;
529 if (encoding == ResourceHandle::UTF16) { 524 if (encoding == ResourceHandle::UTF16) {
530 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()), 525 msg = base::string16(reinterpret_cast<const base::char16*>(data.data()),
531 data.length() / 2); 526 data.length() / 2);
532 } else if (encoding == ResourceHandle::UTF8) { 527 } else if (encoding == ResourceHandle::UTF8) {
533 msg = base::UTF8ToUTF16(data); 528 msg = base::UTF8ToUTF16(data);
534 } 529 }
535 return msg; 530 return msg;
536 } 531 }
537 532
538 const gfx::FontList& ResourceBundle::GetFontList(FontStyle style) { 533 const gfx::FontList& ResourceBundle::GetFontListWithDelta(
539 { 534 int size_delta,
540 base::AutoLock lock_scope(*images_and_fonts_lock_); 535 gfx::Font::FontStyle style) {
541 LoadFontsIfNecessary(); 536 base::AutoLock lock_scope(*images_and_fonts_lock_);
537
538 typedef std::pair<int, gfx::Font::FontStyle> Key;
539 const Key styled_key(size_delta, style);
540
541 auto found = font_cache_.find(styled_key);
542 if (found != font_cache_.end())
543 return found->second;
544
545 const Key base_key(0, gfx::Font::NORMAL);
546 gfx::FontList& base = font_cache_[base_key];
547 if (styled_key == base_key)
548 return base;
549
550 // Fonts of a given style are derived from the unstyled font of the same size.
551 // Cache the unstyled font by first inserting a default-constructed font list.
552 // Then, derive it for the initial insertion, or use the iterator that points
553 // to the existing entry that the insertion collided with.
554 const Key sized_key(size_delta, gfx::Font::NORMAL);
555 auto sized = font_cache_.insert(std::make_pair(sized_key, gfx::FontList()));
556 if (sized.second)
557 sized.first->second = base.DeriveWithSizeDelta(size_delta);
558 if (styled_key == sized_key)
559 return sized.first->second;
560
561 auto styled = font_cache_.insert(std::make_pair(styled_key, gfx::FontList()));
562 DCHECK(styled.second); // Otherwise font_cache_.find(..) would have found it.
563 styled.first->second = sized.first->second.DeriveWithStyle(
564 sized.first->second.GetFontStyle() | style);
565 return styled.first->second;
566 }
567
568 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta,
569 gfx::Font::FontStyle style) {
570 return GetFontListWithDelta(size_delta, style).GetPrimaryFont();
571 }
572
573 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) {
574 gfx::Font::FontStyle font_style = gfx::Font::NORMAL;
575 if (legacy_style == BoldFont || legacy_style == SmallBoldFont ||
576 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont)
577 font_style = gfx::Font::BOLD;
578
579 int size_delta = 0;
580 switch (legacy_style) {
581 case SmallFont:
582 case SmallBoldFont:
583 size_delta = kSmallFontDelta;
584 break;
585 case MediumFont:
586 case MediumBoldFont:
587 size_delta = kMediumFontDelta;
588 break;
589 case LargeFont:
590 case LargeBoldFont:
591 size_delta = kLargeFontDelta;
592 break;
593 case BaseFont:
594 case BoldFont:
595 break;
542 } 596 }
543 switch (style) { 597
544 case BoldFont: 598 return GetFontListWithDelta(size_delta, font_style);
545 return *bold_font_list_;
546 case SmallFont:
547 return *small_font_list_;
548 case MediumFont:
549 return *medium_font_list_;
550 case SmallBoldFont:
551 return *small_bold_font_list_;
552 case MediumBoldFont:
553 return *medium_bold_font_list_;
554 case LargeFont:
555 return *large_font_list_;
556 case LargeBoldFont:
557 return *large_bold_font_list_;
558 default:
559 return *base_font_list_;
560 }
561 } 599 }
562 600
563 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { 601 const gfx::Font& ResourceBundle::GetFont(FontStyle style) {
564 return GetFontList(style).GetPrimaryFont(); 602 return GetFontList(style).GetPrimaryFont();
565 } 603 }
566 604
567 void ResourceBundle::ReloadFonts() { 605 void ResourceBundle::ReloadFonts() {
568 base::AutoLock lock_scope(*images_and_fonts_lock_); 606 base::AutoLock lock_scope(*images_and_fonts_lock_);
569 InitDefaultFontList(); 607 InitDefaultFontList();
570 base_font_list_.reset(); 608 font_cache_.clear();
571 LoadFontsIfNecessary();
572 } 609 }
573 610
574 ScaleFactor ResourceBundle::GetMaxScaleFactor() const { 611 ScaleFactor ResourceBundle::GetMaxScaleFactor() const {
575 #if defined(OS_CHROMEOS) || defined(OS_WIN) 612 #if defined(OS_CHROMEOS) || defined(OS_WIN)
576 return max_scale_factor_; 613 return max_scale_factor_;
577 #else 614 #else
578 return GetSupportedScaleFactors().back(); 615 return GetSupportedScaleFactors().back();
579 #endif 616 #endif
580 } 617 }
581 618
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 // TODO(yukishiino): Remove SetDefaultFontDescription() once the migration to 775 // TODO(yukishiino): Remove SetDefaultFontDescription() once the migration to
739 // the font list is done. We will no longer need SetDefaultFontDescription() 776 // the font list is done. We will no longer need SetDefaultFontDescription()
740 // after every client gets started using a FontList instead of a Font. 777 // after every client gets started using a FontList instead of a Font.
741 gfx::PlatformFontLinux::SetDefaultFontDescription(font_family); 778 gfx::PlatformFontLinux::SetDefaultFontDescription(font_family);
742 #else 779 #else
743 // Use a single default font as the default font list. 780 // Use a single default font as the default font list.
744 gfx::FontList::SetDefaultFontDescription(std::string()); 781 gfx::FontList::SetDefaultFontDescription(std::string());
745 #endif 782 #endif
746 } 783 }
747 784
748 void ResourceBundle::LoadFontsIfNecessary() {
749 images_and_fonts_lock_->AssertAcquired();
750 if (base_font_list_)
751 return;
752
753 base_font_list_.reset(new gfx::FontList());
754 bold_font_list_.reset(new gfx::FontList(base_font_list_->DeriveWithStyle(
755 base_font_list_->GetFontStyle() | gfx::Font::BOLD)));
756
757 small_font_list_.reset(new gfx::FontList(
758 base_font_list_->DeriveWithSizeDelta(kSmallFontSizeDelta)));
759 small_bold_font_list_.reset(
760 new gfx::FontList(small_font_list_->DeriveWithStyle(
761 small_font_list_->GetFontStyle() | gfx::Font::BOLD)));
762
763 medium_font_list_.reset(new gfx::FontList(
764 base_font_list_->DeriveWithSizeDelta(kMediumFontSizeDelta)));
765 medium_bold_font_list_.reset(
766 new gfx::FontList(medium_font_list_->DeriveWithStyle(
767 medium_font_list_->GetFontStyle() | gfx::Font::BOLD)));
768
769 large_font_list_.reset(new gfx::FontList(
770 base_font_list_->DeriveWithSizeDelta(kLargeFontSizeDelta)));
771 large_bold_font_list_.reset(
772 new gfx::FontList(large_font_list_->DeriveWithStyle(
773 large_font_list_->GetFontStyle() | gfx::Font::BOLD)));
774 }
775
776 bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, 785 bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle,
777 int resource_id, 786 int resource_id,
778 SkBitmap* bitmap, 787 SkBitmap* bitmap,
779 bool* fell_back_to_1x) const { 788 bool* fell_back_to_1x) const {
780 DCHECK(fell_back_to_1x); 789 DCHECK(fell_back_to_1x);
781 scoped_refptr<base::RefCountedMemory> memory( 790 scoped_refptr<base::RefCountedMemory> memory(
782 data_handle.GetStaticMemory(static_cast<uint16_t>(resource_id))); 791 data_handle.GetStaticMemory(static_cast<uint16_t>(resource_id)));
783 if (!memory.get()) 792 if (!memory.get())
784 return false; 793 return false;
785 794
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
873 // static 882 // static
874 bool ResourceBundle::DecodePNG(const unsigned char* buf, 883 bool ResourceBundle::DecodePNG(const unsigned char* buf,
875 size_t size, 884 size_t size,
876 SkBitmap* bitmap, 885 SkBitmap* bitmap,
877 bool* fell_back_to_1x) { 886 bool* fell_back_to_1x) {
878 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 887 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
879 return gfx::PNGCodec::Decode(buf, size, bitmap); 888 return gfx::PNGCodec::Decode(buf, size, bitmap);
880 } 889 }
881 890
882 } // namespace ui 891 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/ui_base.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698