tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PrintTargetThebes.cpp (2809B)


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