1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_DWRITE_FONT_PROXY_IMPL_WIN_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_DWRITE_FONT_PROXY_IMPL_WIN_H_
7 
8 #include <dwrite.h>
9 #include <dwrite_2.h>
10 #include <dwrite_3.h>
11 #include <wrl.h>
12 #include <string>
13 #include <vector>
14 
15 #include "base/location.h"
16 #include "base/macros.h"
17 #include "base/memory/read_only_shared_memory_region.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/strings/string16.h"
20 #include "content/browser/renderer_host/dwrite_font_lookup_table_builder_win.h"
21 #include "content/common/content_export.h"
22 #include "content/public/browser/browser_message_filter.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "mojo/public/cpp/bindings/pending_receiver.h"
25 #include "third_party/blink/public/mojom/dwrite_font_proxy/dwrite_font_proxy.mojom.h"
26 
27 namespace content {
28 
29 // Implements a message filter that handles the dwrite font proxy messages.
30 // If DWrite is enabled, calls into the system font collection to obtain
31 // results. Otherwise, acts as if the system collection contains no fonts.
32 class CONTENT_EXPORT DWriteFontProxyImpl
33     : public blink::mojom::DWriteFontProxy {
34  public:
35   DWriteFontProxyImpl();
36   ~DWriteFontProxyImpl() override;
37 
38   static void Create(
39       mojo::PendingReceiver<blink::mojom::DWriteFontProxy> receiver);
40 
41   void SetWindowsFontsPathForTesting(base::string16 path);
42 
43  protected:
44   // blink::mojom::DWriteFontProxy:
45   void FindFamily(const base::string16& family_name,
46                   FindFamilyCallback callback) override;
47   void GetFamilyCount(GetFamilyCountCallback callback) override;
48   void GetFamilyNames(uint32_t family_index,
49                       GetFamilyNamesCallback callback) override;
50   void GetFontFiles(uint32_t family_index,
51                     GetFontFilesCallback callback) override;
52   void MapCharacters(const base::string16& text,
53                      blink::mojom::DWriteFontStylePtr font_style,
54                      const base::string16& locale_name,
55                      uint32_t reading_direction,
56                      const base::string16& base_family_name,
57                      MapCharactersCallback callback) override;
58   void MatchUniqueFont(const base::string16& unique_font_name,
59                        MatchUniqueFontCallback callback) override;
60   void GetUniqueFontLookupMode(
61       GetUniqueFontLookupModeCallback callback) override;
62 
63   void GetUniqueNameLookupTableIfAvailable(
64       GetUniqueNameLookupTableIfAvailableCallback callback) override;
65 
66   void GetUniqueNameLookupTable(
67       GetUniqueNameLookupTableCallback callback) override;
68 
69   void FallbackFamilyAndStyleForCodepoint(
70       const std::string& base_family_name,
71       const std::string& locale_name,
72       uint32_t codepoint,
73       FallbackFamilyAndStyleForCodepointCallback callback) override;
74 
75   void InitializeDirectWrite();
76 
77  private:
78   bool IsLastResortFallbackFont(uint32_t font_index);
79 
80  private:
81   bool direct_write_initialized_ = false;
82   Microsoft::WRL::ComPtr<IDWriteFontCollection> collection_;
83   Microsoft::WRL::ComPtr<IDWriteFactory> factory_;
84   Microsoft::WRL::ComPtr<IDWriteFactory2> factory2_;
85   Microsoft::WRL::ComPtr<IDWriteFactory3> factory3_;
86   Microsoft::WRL::ComPtr<IDWriteFontFallback> font_fallback_;
87   base::string16 windows_fonts_path_;
88   base::MappedReadOnlyRegion font_unique_name_table_memory_;
89 
90   // Temp code to help track down crbug.com/561873
91   std::vector<uint32_t> last_resort_fonts_;
92 
93   DISALLOW_COPY_AND_ASSIGN(DWriteFontProxyImpl);
94 };
95 
96 }  // namespace content
97 
98 #endif  // CONTENT_BROWSER_RENDERER_HOST_DWRITE_FONT_PROXY_IMPL_WIN_H_
99