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 <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 Loading... |
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 Loading... |
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_.reset(delegate_->GetFontList(BaseFont).release()); |
545 bold_font_.reset(delegate_->GetFont(BoldFont).release()); | 550 bold_font_list_.reset(delegate_->GetFontList(BoldFont).release()); |
546 small_font_.reset(delegate_->GetFont(SmallFont).release()); | 551 small_font_list_.reset(delegate_->GetFontList(SmallFont).release()); |
547 small_bold_font_.reset(delegate_->GetFont(SmallBoldFont).release()); | 552 small_bold_font_list_.reset( |
548 medium_font_.reset(delegate_->GetFont(MediumFont).release()); | 553 delegate_->GetFontList(SmallBoldFont).release()); |
549 medium_bold_font_.reset(delegate_->GetFont(MediumBoldFont).release()); | 554 medium_font_list_.reset(delegate_->GetFontList(MediumFont).release()); |
550 large_font_.reset(delegate_->GetFont(LargeFont).release()); | 555 medium_bold_font_list_.reset( |
551 large_bold_font_.reset(delegate_->GetFont(LargeBoldFont).release()); | 556 delegate_->GetFontList(MediumBoldFont).release()); |
| 557 large_font_list_.reset(delegate_->GetFontList(LargeFont).release()); |
| 558 large_bold_font_list_.reset( |
| 559 delegate_->GetFontList(LargeBoldFont).release()); |
552 } | 560 } |
553 | 561 |
554 if (!base_font_.get()) | 562 if (!base_font_list_.get()) { |
555 base_font_.reset(new gfx::Font()); | 563 #if defined(OS_CHROMEOS) |
556 | 564 base_font_list_.reset(new gfx::FontList( |
557 if (!bold_font_.get()) { | 565 l10n_util::GetStringUTF8(IDS_UI_FONT_FAMILY_CROS))); |
558 bold_font_.reset(new gfx::Font()); | 566 #else |
559 *bold_font_ = | 567 base_font_list_.reset(new gfx::FontList()); |
560 base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); | 568 #endif |
561 } | 569 } |
562 | 570 |
563 if (!small_font_.get()) { | 571 if (!bold_font_list_.get()) { |
564 small_font_.reset(new gfx::Font()); | 572 bold_font_list_.reset(new gfx::FontList()); |
565 *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); | 573 *bold_font_list_ = base_font_list_->DeriveFontList( |
| 574 base_font_list_->GetFontStyle() | gfx::Font::BOLD); |
566 } | 575 } |
567 | 576 |
568 if (!small_bold_font_.get()) { | 577 if (!small_font_list_.get()) { |
569 small_bold_font_.reset(new gfx::Font()); | 578 small_font_list_.reset(new gfx::FontList()); |
570 *small_bold_font_ = base_font_->DeriveFont( | 579 *small_font_list_ = base_font_list_->DeriveFontListWithSize( |
571 kSmallFontSizeDelta, base_font_->GetStyle() | gfx::Font::BOLD); | 580 base_font_list_->GetFontSize() + kSmallFontSizeDelta); |
572 } | 581 } |
573 | 582 |
574 if (!medium_font_.get()) { | 583 if (!small_bold_font_list_.get()) { |
575 medium_font_.reset(new gfx::Font()); | 584 small_bold_font_list_.reset(new gfx::FontList()); |
576 *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); | 585 *small_bold_font_list_ = small_font_list_->DeriveFontList( |
| 586 small_font_list_->GetFontStyle() | gfx::Font::BOLD); |
577 } | 587 } |
578 | 588 |
579 if (!medium_bold_font_.get()) { | 589 if (!medium_font_list_.get()) { |
580 medium_bold_font_.reset(new gfx::Font()); | 590 medium_font_list_.reset(new gfx::FontList()); |
581 *medium_bold_font_ = | 591 *medium_font_list_ = base_font_list_->DeriveFontListWithSize( |
582 base_font_->DeriveFont(kMediumFontSizeDelta, | 592 base_font_list_->GetFontSize() + kMediumFontSizeDelta); |
583 base_font_->GetStyle() | gfx::Font::BOLD); | |
584 } | 593 } |
585 | 594 |
586 if (!large_font_.get()) { | 595 if (!medium_bold_font_list_.get()) { |
587 large_font_.reset(new gfx::Font()); | 596 medium_bold_font_list_.reset(new gfx::FontList()); |
588 *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); | 597 *medium_bold_font_list_ = medium_font_list_->DeriveFontList( |
| 598 medium_font_list_->GetFontStyle() | gfx::Font::BOLD); |
589 } | 599 } |
590 | 600 |
591 if (!large_bold_font_.get()) { | 601 if (!large_font_list_.get()) { |
592 large_bold_font_.reset(new gfx::Font()); | 602 large_font_list_.reset(new gfx::FontList()); |
593 *large_bold_font_ = | 603 *large_font_list_ = base_font_list_->DeriveFontListWithSize( |
594 base_font_->DeriveFont(kLargeFontSizeDelta, | 604 base_font_list_->GetFontSize() + kLargeFontSizeDelta); |
595 base_font_->GetStyle() | gfx::Font::BOLD); | 605 } |
| 606 |
| 607 if (!large_bold_font_list_.get()) { |
| 608 large_bold_font_list_.reset(new gfx::FontList()); |
| 609 *large_bold_font_list_ = large_font_list_->DeriveFontList( |
| 610 large_font_list_->GetFontStyle() | gfx::Font::BOLD); |
596 } | 611 } |
597 } | 612 } |
598 } | 613 } |
599 | 614 |
600 bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, | 615 bool ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, |
601 int resource_id, | 616 int resource_id, |
602 SkBitmap* bitmap, | 617 SkBitmap* bitmap, |
603 bool* fell_back_to_1x) const { | 618 bool* fell_back_to_1x) const { |
604 DCHECK(fell_back_to_1x); | 619 DCHECK(fell_back_to_1x); |
605 scoped_refptr<base::RefCountedMemory> memory( | 620 scoped_refptr<base::RefCountedMemory> memory( |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 // static | 721 // static |
707 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 722 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
708 size_t size, | 723 size_t size, |
709 SkBitmap* bitmap, | 724 SkBitmap* bitmap, |
710 bool* fell_back_to_1x) { | 725 bool* fell_back_to_1x) { |
711 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 726 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
712 return gfx::PNGCodec::Decode(buf, size, bitmap); | 727 return gfx::PNGCodec::Decode(buf, size, bitmap); |
713 } | 728 } |
714 | 729 |
715 } // namespace ui | 730 } // namespace ui |
OLD | NEW |