Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/thebes/PrintTargetPDF.cpp
Line
Count
Source (jump to first uncovered line)
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
6
#include "PrintTargetPDF.h"
7
8
#include "cairo.h"
9
#include "cairo-pdf.h"
10
11
namespace mozilla {
12
namespace gfx {
13
14
static cairo_status_t
15
write_func(void *closure, const unsigned char *data, unsigned int length)
16
0
{
17
0
  nsCOMPtr<nsIOutputStream> out = reinterpret_cast<nsIOutputStream*>(closure);
18
0
  do {
19
0
    uint32_t wrote = 0;
20
0
    if (NS_FAILED(out->Write((const char*)data, length, &wrote))) {
21
0
      break;
22
0
    }
23
0
    data += wrote; length -= wrote;
24
0
  } while (length > 0);
25
0
  NS_ASSERTION(length == 0, "not everything was written to the file");
26
0
  return CAIRO_STATUS_SUCCESS;
27
0
}
28
29
PrintTargetPDF::PrintTargetPDF(cairo_surface_t* aCairoSurface,
30
                               const IntSize& aSize,
31
                               nsIOutputStream *aStream)
32
  : PrintTarget(aCairoSurface, aSize)
33
  , mStream(aStream)
34
0
{
35
0
}
36
37
PrintTargetPDF::~PrintTargetPDF()
38
0
{
39
0
  // We get called first, then PrintTarget's dtor.  That means that mStream
40
0
  // is destroyed before PrintTarget's dtor calls cairo_surface_destroy.  This
41
0
  // can be a problem if Finish() hasn't been called on us, since
42
0
  // cairo_surface_destroy will then call cairo_surface_finish and that will
43
0
  // end up invoking write_func above with the by now dangling pointer mStream
44
0
  // that mCairoSurface stored.  To prevent that from happening we must call
45
0
  // Flush here before mStream is deleted.
46
0
  Finish();
47
0
}
48
49
/* static */ already_AddRefed<PrintTargetPDF>
50
PrintTargetPDF::CreateOrNull(nsIOutputStream *aStream,
51
                             const IntSize& aSizeInPoints)
52
0
{
53
0
  cairo_surface_t* surface =
54
0
    cairo_pdf_surface_create_for_stream(write_func, (void*)aStream,
55
0
                                        aSizeInPoints.width,
56
0
                                        aSizeInPoints.height);
57
0
  if (cairo_surface_status(surface)) {
58
0
    return nullptr;
59
0
  }
60
0
61
0
  // The new object takes ownership of our surface reference.
62
0
  RefPtr<PrintTargetPDF> target = new PrintTargetPDF(surface, aSizeInPoints,
63
0
                                                     aStream);
64
0
  return target.forget();
65
0
}
66
67
nsresult
68
PrintTargetPDF::EndPage()
69
0
{
70
0
  cairo_surface_show_page(mCairoSurface);
71
0
  return PrintTarget::EndPage();
72
0
}
73
74
void
75
PrintTargetPDF::Finish()
76
0
{
77
0
  if (mIsFinished) {
78
0
    return; // We don't want to call Close() on mStream more than once
79
0
  }
80
0
  PrintTarget::Finish();
81
0
  mStream->Close();
82
0
}
83
84
} // namespace gfx
85
} // namespace mozilla