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

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

Issue 19666006: Supports FontList in Textfield. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removes ResourceBundle::Delegate::GetFontList. Created 7 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_unittest.cc » ('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 <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted_memory.h" 12 #include "base/memory/ref_counted_memory.h"
13 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/stl_util.h" 15 #include "base/stl_util.h"
16 #include "base/strings/string_piece.h" 16 #include "base/strings/string_piece.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "build/build_config.h" 19 #include "build/build_config.h"
20 #include "grit/app_locale_settings.h"
20 #include "net/base/big_endian.h" 21 #include "net/base/big_endian.h"
21 #include "skia/ext/image_operations.h" 22 #include "skia/ext/image_operations.h"
22 #include "third_party/skia/include/core/SkBitmap.h" 23 #include "third_party/skia/include/core/SkBitmap.h"
23 #include "ui/base/l10n/l10n_util.h" 24 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/layout.h" 25 #include "ui/base/layout.h"
25 #include "ui/base/resource/data_pack.h" 26 #include "ui/base/resource/data_pack.h"
26 #include "ui/base/ui_base_paths.h" 27 #include "ui/base/ui_base_paths.h"
27 #include "ui/base/ui_base_switches.h" 28 #include "ui/base/ui_base_switches.h"
28 #include "ui/gfx/codec/jpeg_codec.h" 29 #include "ui/gfx/codec/jpeg_codec.h"
29 #include "ui/gfx/codec/png_codec.h" 30 #include "ui/gfx/codec/png_codec.h"
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 string16 msg; 451 string16 msg;
451 if (encoding == ResourceHandle::UTF16) { 452 if (encoding == ResourceHandle::UTF16) {
452 msg = string16(reinterpret_cast<const char16*>(data.data()), 453 msg = string16(reinterpret_cast<const char16*>(data.data()),
453 data.length() / 2); 454 data.length() / 2);
454 } else if (encoding == ResourceHandle::UTF8) { 455 } else if (encoding == ResourceHandle::UTF8) {
455 msg = UTF8ToUTF16(data); 456 msg = UTF8ToUTF16(data);
456 } 457 }
457 return msg; 458 return msg;
458 } 459 }
459 460
460 const gfx::Font& ResourceBundle::GetFont(FontStyle style) { 461 const gfx::FontList& ResourceBundle::GetFontList(FontStyle style) {
461 { 462 {
462 base::AutoLock lock_scope(*images_and_fonts_lock_); 463 base::AutoLock lock_scope(*images_and_fonts_lock_);
463 LoadFontsIfNecessary(); 464 LoadFontsIfNecessary();
464 } 465 }
465 switch (style) { 466 switch (style) {
466 case BoldFont: 467 case BoldFont:
467 return *bold_font_; 468 return *bold_font_list_;
468 case SmallFont: 469 case SmallFont:
469 return *small_font_; 470 return *small_font_list_;
470 case MediumFont: 471 case MediumFont:
471 return *medium_font_; 472 return *medium_font_list_;
472 case SmallBoldFont: 473 case SmallBoldFont:
473 return *small_bold_font_; 474 return *small_bold_font_list_;
474 case MediumBoldFont: 475 case MediumBoldFont:
475 return *medium_bold_font_; 476 return *medium_bold_font_list_;
476 case LargeFont: 477 case LargeFont:
477 return *large_font_; 478 return *large_font_list_;
478 case LargeBoldFont: 479 case LargeBoldFont:
479 return *large_bold_font_; 480 return *large_bold_font_list_;
480 default: 481 default:
481 return *base_font_; 482 return *base_font_list_;
482 } 483 }
483 } 484 }
484 485
486 const gfx::Font& ResourceBundle::GetFont(FontStyle style) {
487 return GetFontList(style).GetPrimaryFont();
488 }
489
485 void ResourceBundle::ReloadFonts() { 490 void ResourceBundle::ReloadFonts() {
486 base::AutoLock lock_scope(*images_and_fonts_lock_); 491 base::AutoLock lock_scope(*images_and_fonts_lock_);
487 base_font_.reset(); 492 base_font_list_.reset();
488 LoadFontsIfNecessary(); 493 LoadFontsIfNecessary();
489 } 494 }
490 495
491 ResourceBundle::ResourceBundle(Delegate* delegate) 496 ResourceBundle::ResourceBundle(Delegate* delegate)
492 : delegate_(delegate), 497 : delegate_(delegate),
493 images_and_fonts_lock_(new base::Lock), 498 images_and_fonts_lock_(new base::Lock),
494 locale_resources_data_lock_(new base::Lock), 499 locale_resources_data_lock_(new base::Lock),
495 max_scale_factor_(SCALE_FACTOR_100P) { 500 max_scale_factor_(SCALE_FACTOR_100P) {
496 } 501 }
497 502
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 void ResourceBundle::AddDataPack(DataPack* data_pack) { 537 void ResourceBundle::AddDataPack(DataPack* data_pack) {
533 data_packs_.push_back(data_pack); 538 data_packs_.push_back(data_pack);
534 539
535 if (GetScaleFactorScale(data_pack->GetScaleFactor()) > 540 if (GetScaleFactorScale(data_pack->GetScaleFactor()) >
536 GetScaleFactorScale(max_scale_factor_)) 541 GetScaleFactorScale(max_scale_factor_))
537 max_scale_factor_ = data_pack->GetScaleFactor(); 542 max_scale_factor_ = data_pack->GetScaleFactor();
538 } 543 }
539 544
540 void ResourceBundle::LoadFontsIfNecessary() { 545 void ResourceBundle::LoadFontsIfNecessary() {
541 images_and_fonts_lock_->AssertAcquired(); 546 images_and_fonts_lock_->AssertAcquired();
542 if (!base_font_.get()) { 547 if (!base_font_list_.get()) {
543 if (delegate_) { 548 if (delegate_) {
544 base_font_.reset(delegate_->GetFont(BaseFont).release()); 549 base_font_list_ = GetFontListFromDelegate(BaseFont);
545 bold_font_.reset(delegate_->GetFont(BoldFont).release()); 550 bold_font_list_ = GetFontListFromDelegate(BoldFont);
546 small_font_.reset(delegate_->GetFont(SmallFont).release()); 551 small_font_list_ = GetFontListFromDelegate(SmallFont);
547 small_bold_font_.reset(delegate_->GetFont(SmallBoldFont).release()); 552 small_bold_font_list_ = GetFontListFromDelegate(SmallBoldFont);
548 medium_font_.reset(delegate_->GetFont(MediumFont).release()); 553 medium_font_list_ = GetFontListFromDelegate(MediumFont);
549 medium_bold_font_.reset(delegate_->GetFont(MediumBoldFont).release()); 554 medium_bold_font_list_ = GetFontListFromDelegate(MediumBoldFont);
550 large_font_.reset(delegate_->GetFont(LargeFont).release()); 555 large_font_list_ = GetFontListFromDelegate(LargeFont);
551 large_bold_font_.reset(delegate_->GetFont(LargeBoldFont).release()); 556 large_bold_font_list_ = GetFontListFromDelegate(LargeBoldFont);
552 } 557 }
553 558
554 if (!base_font_.get()) 559 if (!base_font_list_.get()) {
555 base_font_.reset(new gfx::Font()); 560 #if defined(OS_CHROMEOS)
556 561 base_font_list_.reset(new gfx::FontList(
557 if (!bold_font_.get()) { 562 l10n_util::GetStringUTF8(IDS_UI_FONT_FAMILY_CROS)));
558 bold_font_.reset(new gfx::Font()); 563 #else
559 *bold_font_ = 564 base_font_list_.reset(new gfx::FontList());
560 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); 565 #endif
561 } 566 }
562 567
563 if (!small_font_.get()) { 568 if (!bold_font_list_.get()) {
564 small_font_.reset(new gfx::Font()); 569 bold_font_list_.reset(new gfx::FontList());
565 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); 570 *bold_font_list_ = base_font_list_->DeriveFontList(
571 base_font_list_->GetFontStyle() | gfx::Font::BOLD);
566 } 572 }
567 573
568 if (!small_bold_font_.get()) { 574 if (!small_font_list_.get()) {
569 small_bold_font_.reset(new gfx::Font()); 575 small_font_list_.reset(new gfx::FontList());
570 *small_bold_font_ = base_font_->DeriveFont( 576 *small_font_list_ = base_font_list_->DeriveFontListWithSize(
571 kSmallFontSizeDelta, base_font_->GetStyle() | gfx::Font::BOLD); 577 base_font_list_->GetFontSize() + kSmallFontSizeDelta);
572 } 578 }
573 579
574 if (!medium_font_.get()) { 580 if (!small_bold_font_list_.get()) {
575 medium_font_.reset(new gfx::Font()); 581 small_bold_font_list_.reset(new gfx::FontList());
576 *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); 582 *small_bold_font_list_ = small_font_list_->DeriveFontList(
583 small_font_list_->GetFontStyle() | gfx::Font::BOLD);
577 } 584 }
578 585
579 if (!medium_bold_font_.get()) { 586 if (!medium_font_list_.get()) {
580 medium_bold_font_.reset(new gfx::Font()); 587 medium_font_list_.reset(new gfx::FontList());
581 *medium_bold_font_ = 588 *medium_font_list_ = base_font_list_->DeriveFontListWithSize(
582 base_font_->DeriveFont(kMediumFontSizeDelta, 589 base_font_list_->GetFontSize() + kMediumFontSizeDelta);
583 base_font_->GetStyle() | gfx::Font::BOLD);
584 } 590 }
585 591
586 if (!large_font_.get()) { 592 if (!medium_bold_font_list_.get()) {
587 large_font_.reset(new gfx::Font()); 593 medium_bold_font_list_.reset(new gfx::FontList());
588 *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); 594 *medium_bold_font_list_ = medium_font_list_->DeriveFontList(
595 medium_font_list_->GetFontStyle() | gfx::Font::BOLD);
589 } 596 }
590 597
591 if (!large_bold_font_.get()) { 598 if (!large_font_list_.get()) {
592 large_bold_font_.reset(new gfx::Font()); 599 large_font_list_.reset(new gfx::FontList());
593 *large_bold_font_ = 600 *large_font_list_ = base_font_list_->DeriveFontListWithSize(
594 base_font_->DeriveFont(kLargeFontSizeDelta, 601 base_font_list_->GetFontSize() + kLargeFontSizeDelta);
595 base_font_->GetStyle() | gfx::Font::BOLD); 602 }
603
604 if (!large_bold_font_list_.get()) {
605 large_bold_font_list_.reset(new gfx::FontList());
606 *large_bold_font_list_ = large_font_list_->DeriveFontList(
607 large_font_list_->GetFontStyle() | gfx::Font::BOLD);
596 } 608 }
597 } 609 }
598 } 610 }
599 611
612 scoped_ptr<gfx::FontList> ResourceBundle::GetFontListFromDelegate(
613 FontStyle style) {
614 DCHECK(delegate_);
615 scoped_ptr<gfx::Font> font = delegate_->GetFont(style);
616 if (font.get())
617 return scoped_ptr<gfx::FontList>(new gfx::FontList(*font));
618 else
tony 2013/08/01 16:48:07 Nit: else not needed.
Yuki 2013/08/02 06:00:29 Sent out a CL http://crrev.com/21734002 Thanks.
619 return scoped_ptr<gfx::FontList>();
620 }
621
600 bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, 622 bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle,
601 int resource_id, 623 int resource_id,
602 SkBitmap* bitmap, 624 SkBitmap* bitmap,
603 bool* fell_back_to_1x) const { 625 bool* fell_back_to_1x) const {
604 DCHECK(fell_back_to_1x); 626 DCHECK(fell_back_to_1x);
605 scoped_refptr<base::RefCountedMemory> memory( 627 scoped_refptr<base::RefCountedMemory> memory(
606 data_handle.GetStaticMemory(resource_id)); 628 data_handle.GetStaticMemory(resource_id));
607 if (!memory.get()) 629 if (!memory.get())
608 return false; 630 return false;
609 631
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 // static 728 // static
707 bool ResourceBundle::DecodePNG(const unsigned char* buf, 729 bool ResourceBundle::DecodePNG(const unsigned char* buf,
708 size_t size, 730 size_t size,
709 SkBitmap* bitmap, 731 SkBitmap* bitmap,
710 bool* fell_back_to_1x) { 732 bool* fell_back_to_1x) {
711 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); 733 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size);
712 return gfx::PNGCodec::Decode(buf, size, bitmap); 734 return gfx::PNGCodec::Decode(buf, size, bitmap);
713 } 735 }
714 736
715 } // namespace ui 737 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/resource_bundle.h ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698