PrintTargetThebes.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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. #include "PrintTargetThebes.h"
  6. #include "gfxASurface.h"
  7. #include "gfxPlatform.h"
  8. #include "mozilla/gfx/Logging.h"
  9. namespace mozilla {
  10. namespace gfx {
  11. /* static */ already_AddRefed<PrintTargetThebes>
  12. PrintTargetThebes::CreateOrNull(gfxASurface* aSurface)
  13. {
  14. MOZ_ASSERT(aSurface);
  15. if (!aSurface || aSurface->CairoStatus()) {
  16. return nullptr;
  17. }
  18. RefPtr<PrintTargetThebes> target = new PrintTargetThebes(aSurface);
  19. return target.forget();
  20. }
  21. PrintTargetThebes::PrintTargetThebes(gfxASurface* aSurface)
  22. : PrintTarget(nullptr, aSurface->GetSize())
  23. , mGfxSurface(aSurface)
  24. {
  25. }
  26. already_AddRefed<DrawTarget>
  27. PrintTargetThebes::MakeDrawTarget(const IntSize& aSize,
  28. DrawEventRecorder* aRecorder)
  29. {
  30. // This should not be called outside of BeginPage()/EndPage() calls since
  31. // some backends can only provide a valid DrawTarget at that time.
  32. MOZ_ASSERT(mHasActivePage, "We can't guarantee a valid DrawTarget");
  33. RefPtr<gfx::DrawTarget> dt =
  34. gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(mGfxSurface, aSize);
  35. if (!dt || !dt->IsValid()) {
  36. return nullptr;
  37. }
  38. if (aRecorder) {
  39. dt = CreateRecordingDrawTarget(aRecorder, dt);
  40. if (!dt || !dt->IsValid()) {
  41. return nullptr;
  42. }
  43. }
  44. return dt.forget();
  45. }
  46. already_AddRefed<DrawTarget>
  47. PrintTargetThebes::GetReferenceDrawTarget(DrawEventRecorder* aRecorder)
  48. {
  49. if (!mRefDT) {
  50. RefPtr<gfx::DrawTarget> dt =
  51. gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(mGfxSurface, mSize);
  52. if (!dt || !dt->IsValid()) {
  53. return nullptr;
  54. }
  55. if (aRecorder) {
  56. dt = CreateRecordingDrawTarget(aRecorder, dt);
  57. if (!dt || !dt->IsValid()) {
  58. return nullptr;
  59. }
  60. }
  61. mRefDT = dt->CreateSimilarDrawTarget(IntSize(1,1), dt->GetFormat());
  62. }
  63. return do_AddRef(mRefDT);
  64. }
  65. nsresult
  66. PrintTargetThebes::BeginPrinting(const nsAString& aTitle,
  67. const nsAString& aPrintToFileName)
  68. {
  69. return mGfxSurface->BeginPrinting(aTitle, aPrintToFileName);
  70. }
  71. nsresult
  72. PrintTargetThebes::EndPrinting()
  73. {
  74. return mGfxSurface->EndPrinting();
  75. }
  76. nsresult
  77. PrintTargetThebes::AbortPrinting()
  78. {
  79. #ifdef DEBUG
  80. mHasActivePage = false;
  81. #endif
  82. return mGfxSurface->AbortPrinting();
  83. }
  84. nsresult
  85. PrintTargetThebes::BeginPage()
  86. {
  87. #ifdef DEBUG
  88. mHasActivePage = true;
  89. #endif
  90. return mGfxSurface->BeginPage();
  91. }
  92. nsresult
  93. PrintTargetThebes::EndPage()
  94. {
  95. #ifdef DEBUG
  96. mHasActivePage = false;
  97. #endif
  98. return mGfxSurface->EndPage();
  99. }
  100. void
  101. PrintTargetThebes::Finish()
  102. {
  103. return mGfxSurface->Finish();
  104. }
  105. } // namespace gfx
  106. } // namespace mozilla