1 /*
2  * Copyright (C) 2013 Google, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_CSS_CUSTOM_FONT_DATA_H_
22 #define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_CSS_CUSTOM_FONT_DATA_H_
23 
24 #include "third_party/blink/renderer/core/css/css_font_face_source.h"
25 #include "third_party/blink/renderer/platform/fonts/custom_font_data.h"
26 #include "third_party/blink/renderer/platform/heap/persistent.h"
27 
28 namespace blink {
29 
30 class CSSCustomFontData final : public CustomFontData {
31  public:
32   enum FallbackVisibility { kInvisibleFallback, kVisibleFallback };
33 
Create(CSSFontFaceSource * source,FallbackVisibility visibility)34   static scoped_refptr<CSSCustomFontData> Create(
35       CSSFontFaceSource* source,
36       FallbackVisibility visibility) {
37     return base::AdoptRef(new CSSCustomFontData(source, visibility));
38   }
39 
40   ~CSSCustomFontData() override = default;
41 
ShouldSkipDrawing()42   bool ShouldSkipDrawing() const override {
43     if (font_face_source_)
44       font_face_source_->PaintRequested();
45     return fallback_visibility_ == kInvisibleFallback && is_loading_;
46   }
47 
BeginLoadIfNeeded()48   void BeginLoadIfNeeded() const override {
49     if (!is_loading_ && font_face_source_) {
50       is_loading_ = true;
51       font_face_source_->BeginLoadIfNeeded();
52     }
53   }
54 
IsLoading()55   bool IsLoading() const override { return is_loading_; }
IsLoadingFallback()56   bool IsLoadingFallback() const override { return true; }
ClearFontFaceSource()57   void ClearFontFaceSource() override { font_face_source_ = nullptr; }
58 
59  private:
CSSCustomFontData(CSSFontFaceSource * source,FallbackVisibility visibility)60   CSSCustomFontData(CSSFontFaceSource* source, FallbackVisibility visibility)
61       : font_face_source_(source),
62         fallback_visibility_(visibility),
63         is_loading_(false) {
64     if (source)
65       is_loading_ = source->IsLoading();
66   }
67 
68   // TODO(Oilpan): consider moving (Custom)FontFace hierarchy to the heap,
69   // thereby making this reference a Member<>.
70   WeakPersistent<CSSFontFaceSource> font_face_source_;
71   FallbackVisibility fallback_visibility_;
72   mutable bool is_loading_;
73 };
74 
75 }  // namespace blink
76 
77 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_CSS_CUSTOM_FONT_DATA_H_
78