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

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: Changes for Mac 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
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(style);
564 return styled.first->second;
565 }
566
567 const gfx::Font& ResourceBundle::GetFontWithDelta(int size_delta,
568 gfx::Font::FontStyle style) {
569 return GetFontListWithDelta(size_delta, style).GetPrimaryFont();
570 }
571
572 const gfx::FontList& ResourceBundle::GetFontList(FontStyle legacy_style) {
573 gfx::Font::FontStyle font_style = gfx::Font::NORMAL;
574 if (legacy_style == BoldFont || legacy_style == SmallBoldFont ||
575 legacy_style == MediumBoldFont || legacy_style == LargeBoldFont)
576 font_style = gfx::Font::BOLD;
577
578 int size_delta = 0;
579 switch (legacy_style) {
580 case SmallFont:
581 case SmallBoldFont:
582 size_delta = kSmallFontDelta;
583 break;
584 case MediumFont:
585 case MediumBoldFont:
586 size_delta = kMediumFontDelta;
587 break;
588 case LargeFont:
589 case LargeBoldFont:
590 size_delta = kLargeFontDelta;
591 break;
592 case BaseFont:
593 case BoldFont:
594 break;
542 } 595 }
543 switch (style) { 596
544 case BoldFont: 597 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 } 598 }
562 599
563 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { 600 const gfx::Font& ResourceBundle::GetFont(FontStyle style) {
564 return GetFontList(style).GetPrimaryFont(); 601 return GetFontList(style).GetPrimaryFont();
565 } 602 }
566 603
567 void ResourceBundle::ReloadFonts() { 604 void ResourceBundle::ReloadFonts() {
568 base::AutoLock lock_scope(*images_and_fonts_lock_); 605 base::AutoLock lock_scope(*images_and_fonts_lock_);
569 InitDefaultFontList(); 606 InitDefaultFontList();
570 base_font_list_.reset(); 607 font_cache_.clear();
571 LoadFontsIfNecessary();
572 } 608 }
573 609
574 ScaleFactor ResourceBundle::GetMaxScaleFactor() const { 610 ScaleFactor ResourceBundle::GetMaxScaleFactor() const {
575 #if defined(OS_CHROMEOS) || defined(OS_WIN) 611 #if defined(OS_CHROMEOS) || defined(OS_WIN)
576 return max_scale_factor_; 612 return max_scale_factor_;
577 #else 613 #else
578 return GetSupportedScaleFactors().back(); 614 return GetSupportedScaleFactors().back();
579 #endif 615 #endif
580 } 616 }
581 617
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 // TODO(yukishiino): Remove SetDefaultFontDescription() once the migration to 774 // TODO(yukishiino): Remove SetDefaultFontDescription() once the migration to
739 // the font list is done. We will no longer need SetDefaultFontDescription() 775 // the font list is done. We will no longer need SetDefaultFontDescription()
740 // after every client gets started using a FontList instead of a Font. 776 // after every client gets started using a FontList instead of a Font.
741 gfx::PlatformFontLinux::SetDefaultFontDescription(font_family); 777 gfx::PlatformFontLinux::SetDefaultFontDescription(font_family);
742 #else 778 #else
743 // Use a single default font as the default font list. 779 // Use a single default font as the default font list.
744 gfx::FontList::SetDefaultFontDescription(std::string()); 780 gfx::FontList::SetDefaultFontDescription(std::string());
745 #endif 781 #endif
746 } 782 }
747 783
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, 784 bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle,
777 int resource_id, 785 int resource_id,
778 SkBitmap* bitmap, 786 SkBitmap* bitmap,
779 bool* fell_back_to_1x) const { 787 bool* fell_back_to_1x) const {
780 DCHECK(fell_back_to_1x); 788 DCHECK(fell_back_to_1x);
781 scoped_refptr<base::RefCountedMemory> memory( 789 scoped_refptr<base::RefCountedMemory> memory(
782 data_handle.GetStaticMemory(static_cast<uint16_t>(resource_id))); 790 data_handle.GetStaticMemory(static_cast<uint16_t>(resource_id)));
783 if (!memory.get()) 791 if (!memory.get())
784 return false; 792 return false;
785 793
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
873 // static 881 // static
874 bool ResourceBundle::DecodePNG(const unsigned char* buf, 882 bool ResourceBundle::DecodePNG(const unsigned char* buf,
875 size_t size, 883 size_t size,
876 SkBitmap* bitmap, 884 SkBitmap* bitmap,
877 bool* fell_back_to_1x) { 885 bool* fell_back_to_1x) {
878 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 886 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
879 return gfx::PNGCodec::Decode(buf, size, bitmap); 887 return gfx::PNGCodec::Decode(buf, size, bitmap);
880 } 888 }
881 889
882 } // namespace ui 890 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698