summaryrefslogtreecommitdiff
path: root/layout/style/StyleStructContext.h
blob: f4b59972ec37a8641db0b64b236f4c321edbd07d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_StyleStructContext_h
#define mozilla_StyleStructContext_h

#include "CounterStyleManager.h"
#include "mozilla/LookAndFeel.h"
#include "nsPresContext.h"

class nsDeviceContext;

/**
 * Construction context for style structs.
 *
 * Certain Gecko style structs have historically taken an nsPresContext
 * argument in their constructor, which is used to compute various things. This
 * makes Gecko style structs document-specific (which Servo style structs are
 * not), and means that the initial values for style-structs cannot be shared
 * across the entire runtime (as is the case in Servo).
 *
 * We'd like to remove this constraint so that Gecko can get the benefits of the
 * Servo model, and so that Gecko aligns better with the Servo style system when
 * using it. Unfortunately, this may require a fair amount of work, especially
 * related to text zoom.
 *
 * So as an intermediate step, we define a reduced API set of "things that are
 * needed when constructing style structs". This just wraps and forwards to an
 * nsPresContext in the Gecko case, and returns some default not-always-correct
 * values in the Servo case. We can then focus on reducing this API surface to
 * zero, at which point this can be removed.
 *
 * We don't put the type in namespace mozilla, since we expect it to be
 * temporary, and the namespacing would clutter up nsStyleStruct.h.
 */
#ifdef MOZ_STYLO
#define SERVO_DEFAULT(default_val) { if (!mPresContext) { return default_val; } }
#else
#define SERVO_DEFAULT(default_val) { MOZ_ASSERT(mPresContext); }
#endif
class StyleStructContext {
public:
  MOZ_IMPLICIT StyleStructContext(nsPresContext* aPresContext)
    : mPresContext(aPresContext) { MOZ_ASSERT(aPresContext); }
  static StyleStructContext ServoContext() { return StyleStructContext(); }

  // XXXbholley: Need to make text zoom work with stylo. This probably means
  // moving the zoom handling out of computed values and into a post-
  // computation.
  float TextZoom()
  {
    SERVO_DEFAULT(1.0);
    return mPresContext->TextZoom();
  }

  const nsFont* GetDefaultFont(uint8_t aFontID)
  {
    // NB: The servo case only differ from the default case in terms of which
    // font pref cache gets used. The distinction is probably unnecessary.
    SERVO_DEFAULT(mozilla::StaticPresData::Get()->
                  GetDefaultFont(aFontID, GetLanguageFromCharset()));
    return mPresContext->GetDefaultFont(aFontID, GetLanguageFromCharset());
  }

  uint32_t GetBidi()
  {
    SERVO_DEFAULT(0);
    return mPresContext->GetBidi();
  }

  int32_t AppUnitsPerDevPixel();

  nscoord DevPixelsToAppUnits(int32_t aPixels)
  {
    return NSIntPixelsToAppUnits(aPixels, AppUnitsPerDevPixel());
  }

  typedef mozilla::LookAndFeel LookAndFeel;
  nscolor DefaultColor()
  {
    SERVO_DEFAULT(LookAndFeel::GetColor(LookAndFeel::eColorID_WindowForeground,
                                        NS_RGB(0x00, 0x00, 0x00)));
    return mPresContext->DefaultColor();
  }

  mozilla::CounterStyle* BuildCounterStyle(const nsSubstring& aName)
  {
    SERVO_DEFAULT(mozilla::CounterStyleManager::GetBuiltinStyle(NS_STYLE_LIST_STYLE_DISC));
    return mPresContext->CounterStyleManager()->BuildCounterStyle(aName);
  }

  nsIAtom* GetLanguageFromCharset() const
  {
    SERVO_DEFAULT(nsGkAtoms::x_western);
    return mPresContext->GetLanguageFromCharset();
  }

  already_AddRefed<nsIAtom> GetContentLanguage() const
  {
    SERVO_DEFAULT(do_AddRef(nsGkAtoms::x_western));
    return mPresContext->GetContentLanguage();
  }

private:
  nsDeviceContext* DeviceContext()
  {
    SERVO_DEFAULT(HackilyFindSomeDeviceContext());
    return mPresContext->DeviceContext();
  }

  nsDeviceContext* HackilyFindSomeDeviceContext();

  StyleStructContext() : mPresContext(nullptr) {}
  nsPresContext* mPresContext;
};

#undef SERVO_DEFAULT

#endif // mozilla_StyleStructContext_h