StyleStructContext.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_StyleStructContext_h
  6. #define mozilla_StyleStructContext_h
  7. #include "CounterStyleManager.h"
  8. #include "mozilla/LookAndFeel.h"
  9. #include "nsPresContext.h"
  10. class nsDeviceContext;
  11. /**
  12. * Construction context for style structs.
  13. *
  14. * Certain Gecko style structs have historically taken an nsPresContext
  15. * argument in their constructor, which is used to compute various things. This
  16. * makes Gecko style structs document-specific (which Servo style structs are
  17. * not), and means that the initial values for style-structs cannot be shared
  18. * across the entire runtime (as is the case in Servo).
  19. *
  20. * We'd like to remove this constraint so that Gecko can get the benefits of the
  21. * Servo model, and so that Gecko aligns better with the Servo style system when
  22. * using it. Unfortunately, this may require a fair amount of work, especially
  23. * related to text zoom.
  24. *
  25. * So as an intermediate step, we define a reduced API set of "things that are
  26. * needed when constructing style structs". This just wraps and forwards to an
  27. * nsPresContext in the Gecko case, and returns some default not-always-correct
  28. * values in the Servo case. We can then focus on reducing this API surface to
  29. * zero, at which point this can be removed.
  30. *
  31. * We don't put the type in namespace mozilla, since we expect it to be
  32. * temporary, and the namespacing would clutter up nsStyleStruct.h.
  33. */
  34. #ifdef MOZ_STYLO
  35. #define SERVO_DEFAULT(default_val) { if (!mPresContext) { return default_val; } }
  36. #else
  37. #define SERVO_DEFAULT(default_val) { MOZ_ASSERT(mPresContext); }
  38. #endif
  39. class StyleStructContext {
  40. public:
  41. MOZ_IMPLICIT StyleStructContext(nsPresContext* aPresContext)
  42. : mPresContext(aPresContext) { MOZ_ASSERT(aPresContext); }
  43. static StyleStructContext ServoContext() { return StyleStructContext(); }
  44. // XXXbholley: Need to make text zoom work with stylo. This probably means
  45. // moving the zoom handling out of computed values and into a post-
  46. // computation.
  47. float TextZoom()
  48. {
  49. SERVO_DEFAULT(1.0);
  50. return mPresContext->TextZoom();
  51. }
  52. const nsFont* GetDefaultFont(uint8_t aFontID)
  53. {
  54. // NB: The servo case only differ from the default case in terms of which
  55. // font pref cache gets used. The distinction is probably unnecessary.
  56. SERVO_DEFAULT(mozilla::StaticPresData::Get()->
  57. GetDefaultFont(aFontID, GetLanguageFromCharset()));
  58. return mPresContext->GetDefaultFont(aFontID, GetLanguageFromCharset());
  59. }
  60. uint32_t GetBidi()
  61. {
  62. SERVO_DEFAULT(0);
  63. return mPresContext->GetBidi();
  64. }
  65. int32_t AppUnitsPerDevPixel();
  66. nscoord DevPixelsToAppUnits(int32_t aPixels)
  67. {
  68. return NSIntPixelsToAppUnits(aPixels, AppUnitsPerDevPixel());
  69. }
  70. typedef mozilla::LookAndFeel LookAndFeel;
  71. nscolor DefaultColor()
  72. {
  73. SERVO_DEFAULT(LookAndFeel::GetColor(LookAndFeel::eColorID_WindowForeground,
  74. NS_RGB(0x00, 0x00, 0x00)));
  75. return mPresContext->DefaultColor();
  76. }
  77. mozilla::CounterStyle* BuildCounterStyle(const nsSubstring& aName)
  78. {
  79. SERVO_DEFAULT(mozilla::CounterStyleManager::GetBuiltinStyle(NS_STYLE_LIST_STYLE_DISC));
  80. return mPresContext->CounterStyleManager()->BuildCounterStyle(aName);
  81. }
  82. nsIAtom* GetLanguageFromCharset() const
  83. {
  84. SERVO_DEFAULT(nsGkAtoms::x_western);
  85. return mPresContext->GetLanguageFromCharset();
  86. }
  87. already_AddRefed<nsIAtom> GetContentLanguage() const
  88. {
  89. SERVO_DEFAULT(do_AddRef(nsGkAtoms::x_western));
  90. return mPresContext->GetContentLanguage();
  91. }
  92. private:
  93. nsDeviceContext* DeviceContext()
  94. {
  95. SERVO_DEFAULT(HackilyFindSomeDeviceContext());
  96. return mPresContext->DeviceContext();
  97. }
  98. nsDeviceContext* HackilyFindSomeDeviceContext();
  99. StyleStructContext() : mPresContext(nullptr) {}
  100. nsPresContext* mPresContext;
  101. };
  102. #undef SERVO_DEFAULT
  103. #endif // mozilla_StyleStructContext_h