Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/StaticPresData.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_StaticPresData_h
8
#define mozilla_StaticPresData_h
9
10
#include "nsAutoPtr.h"
11
#include "nsCoord.h"
12
#include "nsCOMPtr.h"
13
#include "nsFont.h"
14
#include "nsAtom.h"
15
#include "nsLanguageAtomService.h"
16
17
namespace mozilla {
18
19
struct LangGroupFontPrefs {
20
  // Font sizes default to zero; they will be set in GetFontPreferences
21
  LangGroupFontPrefs()
22
    : mLangGroup(nullptr)
23
    , mMinimumFontSize(0)
24
    , mDefaultVariableFont()
25
    , mDefaultFixedFont(mozilla::eFamily_monospace, 0)
26
    , mDefaultSerifFont(mozilla::eFamily_serif, 0)
27
    , mDefaultSansSerifFont(mozilla::eFamily_sans_serif, 0)
28
    , mDefaultMonospaceFont(mozilla::eFamily_monospace, 0)
29
    , mDefaultCursiveFont(mozilla::eFamily_cursive, 0)
30
    , mDefaultFantasyFont(mozilla::eFamily_fantasy, 0)
31
3
  {
32
3
    mDefaultVariableFont.fontlist.SetDefaultFontType(mozilla::eFamily_serif);
33
3
    // We create mDefaultVariableFont.fontlist with defaultType as the
34
3
    // fallback font, and not as part of the font list proper. This way,
35
3
    // it can be overwritten should there be a language change.
36
3
  }
37
38
  void Reset()
39
0
  {
40
0
    // Throw away any other LangGroupFontPrefs objects:
41
0
    mNext = nullptr;
42
0
43
0
    // Make GetFontPreferences reinitialize mLangGroupFontPrefs:
44
0
    mLangGroup = nullptr;
45
0
  }
46
47
0
  size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
48
0
    size_t n = 0;
49
0
    LangGroupFontPrefs* curr = mNext;
50
0
    while (curr) {
51
0
      n += aMallocSizeOf(curr);
52
0
53
0
      // Measurement of the following members may be added later if DMD finds
54
0
      // it is worthwhile:
55
0
      // - mLangGroup
56
0
      // - mDefault*Font
57
0
58
0
      curr = curr->mNext;
59
0
    }
60
0
    return n;
61
0
  }
62
63
  // Initialize this with the data for a given language
64
  void Initialize(nsAtom* aLangGroupAtom);
65
66
  RefPtr<nsAtom> mLangGroup;
67
  nscoord mMinimumFontSize;
68
  nsFont mDefaultVariableFont;
69
  nsFont mDefaultFixedFont;
70
  nsFont mDefaultSerifFont;
71
  nsFont mDefaultSansSerifFont;
72
  nsFont mDefaultMonospaceFont;
73
  nsFont mDefaultCursiveFont;
74
  nsFont mDefaultFantasyFont;
75
  nsAutoPtr<LangGroupFontPrefs> mNext;
76
};
77
78
/**
79
 * Some functionality that has historically lived on nsPresContext does not
80
 * actually need to be per-document. This singleton class serves as a host
81
 * for that functionality. We delegate to it from nsPresContext where
82
 * appropriate, and use it standalone in some cases as well.
83
 */
84
class StaticPresData
85
{
86
public:
87
  // Initialization and shutdown of the singleton. Called exactly once.
88
  static void Init();
89
  static void Shutdown();
90
91
  // Gets an instance of the singleton. Infallible between the calls to Init
92
  // and Shutdown.
93
  static StaticPresData* Get();
94
95
  /**
96
   * This table maps border-width enums 'thin', 'medium', 'thick'
97
   * to actual nscoord values.
98
   */
99
0
  const nscoord* GetBorderWidthTable() { return mBorderWidthTable; }
100
101
  /**
102
   * Given a language, get the language group name, which can
103
   * be used as an argument to LangGroupFontPrefs::Initialize()
104
   *
105
   * aNeedsToCache is used for two things.  If null, it indicates that
106
   * the nsLanguageAtomService is safe to cache the result of the
107
   * language group lookup, either because we're on the main thread,
108
   * or because we're on a style worker thread but the font lock has
109
   * been acquired.  If non-null, it indicates that it's not safe to
110
   * cache the result of the language group lookup (because we're on
111
   * a style worker thread without the lock acquired).  In this case,
112
   * GetLanguageGroup will store true in *aNeedsToCache true if we
113
   * would have cached the result of a new lookup, and false if we
114
   * were able to use an existing cached result.  Thus, callers that
115
   * get a true *aNeedsToCache outparam value should make an effort
116
   * to re-call GetLanguageGroup when it is safe to cache, to avoid
117
   * recomputing the language group again later.
118
   */
119
  nsAtom* GetLangGroup(nsAtom* aLanguage, bool* aNeedsToCache = nullptr) const;
120
121
  /**
122
   * Same as GetLangGroup, but will not cache the result
123
   *
124
   */
125
  already_AddRefed<nsAtom> GetUncachedLangGroup(nsAtom* aLanguage) const;
126
127
  /**
128
   * Fetch the user's font preferences for the given aLanguage's
129
   * langugage group.
130
   *
131
   * The original code here is pretty old, and includes an optimization
132
   * whereby language-specific prefs are read per-document, and the
133
   * results are stored in a linked list, which is assumed to be very short
134
   * since most documents only ever use one language.
135
   *
136
   * Storing this per-session rather than per-document would almost certainly
137
   * be fine. But just to be on the safe side, we leave the old mechanism as-is,
138
   * with an additional per-session cache that new callers can use if they don't
139
   * have a PresContext.
140
   *
141
   * See comment on GetLangGroup for the usage of aNeedsToCache.
142
   */
143
  const LangGroupFontPrefs* GetFontPrefsForLangHelper(nsAtom* aLanguage,
144
                                                      const LangGroupFontPrefs* aPrefs,
145
                                                      bool* aNeedsToCache = nullptr) const;
146
  /**
147
   * Get the default font for the given language and generic font ID.
148
   * aLanguage may not be nullptr.
149
   *
150
   * This object is read-only, you must copy the font to modify it.
151
   *
152
   * When aFontID is kPresContext_DefaultVariableFontID or
153
   * kPresContext_DefaultFixedFontID (which equals
154
   * kGenericFont_moz_fixed, which is used for the -moz-fixed generic),
155
   * the nsFont returned has its name as a CSS generic family (serif or
156
   * sans-serif for the former, monospace for the latter), and its size
157
   * as the default font size for variable or fixed fonts for the
158
   * language group.
159
   *
160
   * For aFontID corresponding to a CSS Generic, the nsFont returned has
161
   * its name set to that generic font's name, and its size set to
162
   * the user's preference for font size for that generic and the
163
   * given language.
164
   */
165
  const nsFont* GetDefaultFontHelper(uint8_t aFontID,
166
                                     nsAtom* aLanguage,
167
                                     const LangGroupFontPrefs* aPrefs) const;
168
169
  /*
170
   * These versions operate on the font pref cache on StaticPresData.
171
   */
172
173
  const nsFont* GetDefaultFont(uint8_t aFontID, nsAtom* aLanguage) const
174
0
  {
175
0
    MOZ_ASSERT(aLanguage);
176
0
    return GetDefaultFontHelper(aFontID, aLanguage, GetFontPrefsForLang(aLanguage));
177
0
  }
178
  const LangGroupFontPrefs* GetFontPrefsForLang(nsAtom* aLanguage, bool* aNeedsToCache = nullptr) const
179
0
  {
180
0
    MOZ_ASSERT(aLanguage);
181
0
    return GetFontPrefsForLangHelper(aLanguage, &mStaticLangGroupFontPrefs, aNeedsToCache);
182
0
  }
183
184
0
  void ResetCachedFontPrefs() { mStaticLangGroupFontPrefs.Reset(); }
185
186
private:
187
  StaticPresData();
188
0
  ~StaticPresData() {}
189
190
  nsLanguageAtomService* mLangService;
191
  nscoord mBorderWidthTable[3];
192
  LangGroupFontPrefs mStaticLangGroupFontPrefs;
193
};
194
195
} // namespace mozilla
196
197
#endif // mozilla_StaticPresData_h