DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (960f50c2e0a9)

VCS Links

Macros

Line Code
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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/. */

#include "PrintTarget.h"

#include "cairo.h"
#ifdef CAIRO_HAS_QUARTZ_SURFACE
#include "cairo-quartz.h"
#endif
#ifdef CAIRO_HAS_WIN32_SURFACE
#include "cairo-win32.h"
#endif
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/HelpersCairo.h"
#include "mozilla/gfx/Logging.h"
#include "nsReadableUtils.h"
#include "nsString.h"
#include "nsUTF8Utils.h"

// IPP spec disallow the job-name which is over 255 characters.
// RFC: https://tools.ietf.org/html/rfc2911#section-4.1.2
#define IPP_JOB_NAME_LIMIT_LENGTH 255

namespace mozilla {
namespace gfx {

PrintTarget::PrintTarget(cairo_surface_t* aCairoSurface, const IntSize& aSize)
  : mCairoSurface(aCairoSurface)
  , mSize(aSize)
  , mIsFinished(false)
#ifdef DEBUG
  , mHasActivePage(false)
  , mRecorder(nullptr)
#endif

{
#if 0
  // aCairoSurface is null when our PrintTargetThebes subclass's ctor calls us.
  // Once PrintTargetThebes is removed, enable this assertion.
  MOZ_ASSERT(aCairoSurface && !cairo_surface_status(aCairoSurface),
             "CreateOrNull factory methods should not call us without a "
             "valid cairo_surface_t*");
#endif

  // CreateOrNull factory methods hand over ownership of aCairoSurface,
  // so we don't call cairo_surface_reference(aSurface) here.

  // This code was copied from gfxASurface::Init:
#ifdef MOZ_TREE_CAIRO
  if (mCairoSurface &&
      cairo_surface_get_content(mCairoSurface) != CAIRO_CONTENT_COLOR) {
    cairo_surface_set_subpixel_antialiasing(mCairoSurface,
                                            CAIRO_SUBPIXEL_ANTIALIASING_DISABLED);
  }
#endif
}

PrintTarget::~PrintTarget()
{
  // null surfaces are allowed here
  cairo_surface_destroy(mCairoSurface);
  mCairoSurface = nullptr;
}

already_AddRefed<DrawTarget>
PrintTarget::MakeDrawTarget(const IntSize& aSize,
                            DrawEventRecorder* aRecorder)
{
  MOZ_ASSERT(mCairoSurface,
             "We shouldn't have been constructed without a cairo surface");

  // This should not be called outside of BeginPage()/EndPage() calls since
  // some backends can only provide a valid DrawTarget at that time.
  MOZ_ASSERT(mHasActivePage, "We can't guarantee a valid DrawTarget");

  if (cairo_surface_status(mCairoSurface)) {
    return nullptr;
  }

  // Note than aSize may not be the same as mSize (the size of mCairoSurface).
  // See the comments in our header.  If the sizes are different a clip will
  // be applied to mCairoSurface.
  RefPtr<DrawTarget> dt =
    Factory::CreateDrawTargetForCairoSurface(mCairoSurface, aSize);
  if (!dt || !dt->IsValid()) {
    return nullptr;
  }

  if (aRecorder) {
    dt = CreateWrapAndRecordDrawTarget(aRecorder, dt);
    if (!dt || !dt->IsValid()) {
      return nullptr;
    }
  }

  return dt.forget();
}

already_AddRefed<DrawTarget>
PrintTarget::GetReferenceDrawTarget(DrawEventRecorder* aRecorder)
{
  if (!mRefDT) {
    const IntSize size(1, 1);

    cairo_surface_t* similar;
    switch (cairo_surface_get_type(mCairoSurface)) {
#ifdef CAIRO_HAS_WIN32_SURFACE
    case CAIRO_SURFACE_TYPE_WIN32:
      similar = cairo_win32_surface_create_with_dib(
        CairoContentToCairoFormat(cairo_surface_get_content(mCairoSurface)),
        size.width, size.height);
      break;
#endif
#ifdef CAIRO_HAS_QUARTZ_SURFACE
    case CAIRO_SURFACE_TYPE_QUARTZ:
      similar = cairo_quartz_surface_create_cg_layer(
                  mCairoSurface, cairo_surface_get_content(mCairoSurface),
                  size.width, size.height);
      break;
#endif
    default:
      similar = cairo_surface_create_similar(
                  mCairoSurface, cairo_surface_get_content(mCairoSurface),
                  size.width, size.height);
      break;
    }

    if (cairo_surface_status(similar)) {
      return nullptr;
    }

    RefPtr<DrawTarget> dt =
      Factory::CreateDrawTargetForCairoSurface(similar, size);

    // The DT addrefs the surface, so we need drop our own reference to it:
    cairo_surface_destroy(similar);

    if (!dt || !dt->IsValid()) {
      return nullptr;
    }
    mRefDT = dt.forget();
  }

  if (aRecorder) {
    if (!mRecordingRefDT) {
      RefPtr<DrawTarget> dt = CreateWrapAndRecordDrawTarget(aRecorder, mRefDT);
      if (!dt || !dt->IsValid()) {
        return nullptr;
      }
      mRecordingRefDT = dt.forget();
#ifdef DEBUG
      mRecorder = aRecorder;
#endif
    }
#ifdef DEBUG
    else {
      MOZ_ASSERT(aRecorder == mRecorder,
                 "Caching mRecordingRefDT assumes the aRecorder is an invariant");
    }
#endif

    return do_AddRef(mRecordingRefDT);
  }

  return do_AddRef(mRefDT);
}

/* static */
void
PrintTarget::AdjustPrintJobNameForIPP(const nsAString& aJobName,
                                      nsCString& aAdjustedJobName)
{
  CopyUTF16toUTF8(aJobName, aAdjustedJobName);

  if (aAdjustedJobName.Length() > IPP_JOB_NAME_LIMIT_LENGTH) {
    uint32_t length =
      RewindToPriorUTF8Codepoint(aAdjustedJobName.get(),
                                 (IPP_JOB_NAME_LIMIT_LENGTH - 3U));
    aAdjustedJobName.SetLength(length);
    aAdjustedJobName.AppendLiteral("...");
  }
}

/* static */
void
PrintTarget::AdjustPrintJobNameForIPP(const nsAString& aJobName,
                                      nsString& aAdjustedJobName)
{
  nsAutoCString jobName;
  AdjustPrintJobNameForIPP(aJobName, jobName);

  CopyUTF8toUTF16(jobName, aAdjustedJobName);
}

/* static */ already_AddRefed<DrawTarget>
PrintTarget::CreateWrapAndRecordDrawTarget(DrawEventRecorder* aRecorder,
                                       DrawTarget* aDrawTarget)
{
  MOZ_ASSERT(aRecorder);
  MOZ_ASSERT(aDrawTarget);

  RefPtr<DrawTarget> dt;

  if (aRecorder) {
    // It doesn't really matter what we pass as the DrawTarget here.
    dt = gfx::Factory::CreateWrapAndRecordDrawTarget(aRecorder, aDrawTarget);
  }

  if (!dt || !dt->IsValid()) {
    gfxCriticalNote
      << "Failed to create a recording DrawTarget for PrintTarget";
    return nullptr;
  }

  return dt.forget();
}

void
PrintTarget::Finish()
{
  if (mIsFinished) {
    return;
  }
  mIsFinished = true;

  // null surfaces are allowed here
  cairo_surface_finish(mCairoSurface);
}

} // namespace gfx
} // namespace mozilla