summaryrefslogtreecommitdiffstats
path: root/printing/pdf_ps_metafile_linux.cc
blob: d5fe5fbcb6a8b545fb3bfabae17ef0f25a8fd977 (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/pdf_ps_metafile_linux.h"

#include <stdio.h>

#include <cairo.h>
#include <cairo-ft.h>
#include <cairo-pdf.h>
#include <cairo-ps.h>

#include <ft2build.h>
#include FT_FREETYPE_H

#include <map>

#include "base/eintr_wrapper.h"
#include "base/file_descriptor_posix.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/singleton.h"
#include "third_party/skia/include/core/SkFontHost.h"
#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/core/SkTypeface.h"

namespace {

FT_Library g_ft_library = NULL;  // handle to FreeType library.

struct FontInfo {
  SkStream* font_stream;
  FT_Face ft_face;
  cairo_font_face_t* cairo_face;
  cairo_user_data_key_t data_key;
};

typedef std::map<uint32_t, FontInfo> MapFontId2FontInfo;

// NOTE: Only call this function when no further rendering will be performed,
// and/or the metafile is closed.
void CleanUpFonts() {
  MapFontId2FontInfo* g_font_cache = Singleton<MapFontId2FontInfo>::get();
  DCHECK(g_font_cache);

  for (MapFontId2FontInfo::iterator it = g_font_cache->begin();
       it !=g_font_cache->end();
       ++it) {
    DCHECK(it->second.cairo_face);
    DCHECK(it->second.font_stream);

    cairo_font_face_destroy(it->second.cairo_face);
    // |it->second.ft_face| is handled by Cairo.
    it->second.font_stream->unref();
  }
  g_font_cache->clear();
}

void CleanUpFreeType() {
  if (g_ft_library) {
    FT_Error ft_error = FT_Done_FreeType(g_ft_library);
    g_ft_library = NULL;
    DCHECK_EQ(ft_error, 0);
  }
}

// Tests if |surface| is valid.
bool IsSurfaceValid(cairo_surface_t* surface) {
  return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS;
}

// Tests if |context| is valid.
bool IsContextValid(cairo_t* context) {
  return cairo_status(context) == CAIRO_STATUS_SUCCESS;
}

// Destroys and resets |surface|.
void CleanUpSurface(cairo_surface_t** surface) {
  if (*surface) {
    cairo_surface_destroy(*surface);
    *surface = NULL;
  }
}

// Destroys and resets |context|.
void CleanUpContext(cairo_t** context) {
  if (*context) {
    cairo_destroy(*context);
    *context = NULL;
  }
}

// Callback function for Cairo to write PDF/PS stream.
// |dst_buffer| is actually a pointer of type `std::string*`.
cairo_status_t WriteCairoStream(void* dst_buffer,
                                const unsigned char* src_data,
                                unsigned int src_data_length) {
  DCHECK(dst_buffer);
  DCHECK(src_data);
  DCHECK_GT(src_data_length, 0u);

  std::string* buffer = reinterpret_cast<std::string*>(dst_buffer);
  buffer->append(reinterpret_cast<const char*>(src_data), src_data_length);

  return CAIRO_STATUS_SUCCESS;
}

}  // namespace

namespace printing {

PdfPsMetafile::PdfPsMetafile(const FileFormat& format)
    : format_(format),
      surface_(NULL), context_(NULL),
      page_surface_(NULL), page_context_(NULL) {
}

PdfPsMetafile::~PdfPsMetafile() {
  // Releases all resources if we forgot to do so.
  CleanUpAll();
}

bool PdfPsMetafile::Init() {
  // We need to check at least these two members to ensure Init() has not been
  // called before. Passing these two checks also implies that surface_,
  // page_surface_, and page_context_ are NULL, and current_page_ is empty.
  DCHECK(!context_);
  DCHECK(all_pages_.empty());
  DCHECK(!g_ft_library);

  // Initializes FreeType library.
  FT_Error ft_error = FT_Init_FreeType(&g_ft_library);
  if (ft_error) {
    DLOG(ERROR) << "Cannot initialize FreeType library for PdfPsMetafile.";
    g_ft_library = NULL;
    return false;
  }

  // Creates an 1 by 1 Cairo surface for entire PDF/PS file.
  // The size for each page will be overwritten later in StartPage().
  switch (format_) {
    case PDF: {
      surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream,
                                                     &all_pages_, 1, 1);
    }
    break;

    case PS: {
      surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream,
                                                    &all_pages_, 1, 1);
    }
    break;

    default:
      NOTREACHED();
      return false;
  }

  // Cairo always returns a valid pointer.
  // Hence, we have to check if it points to a "nil" object.
  if (!IsSurfaceValid(surface_)) {
    DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!";
    CleanUpSurface(&surface_);
    CleanUpFreeType();
    return false;
  }

  // Creates a context.
  context_ = cairo_create(surface_);
  if (!IsContextValid(context_)) {
    DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!";
    CleanUpContext(&context_);
    CleanUpSurface(&surface_);
    CleanUpFreeType();
    return false;
  }

  return true;
}

bool PdfPsMetafile::Init(const void* src_buffer, size_t src_buffer_size) {
  // We need to check at least these two members to ensure Init() has not been
  // called before. Passing these two checks also implies that surface_,
  // page_surface_, and page_context_ are NULL, and current_page_ is empty.
  DCHECK(!context_);
  DCHECK(all_pages_.empty());

  if (src_buffer == NULL || src_buffer_size == 0) {
    return false;
  }

  all_pages_ = std::string(reinterpret_cast<const char*>(src_buffer),
                           src_buffer_size);

  return true;
}

cairo_t* PdfPsMetafile::StartPage(double width_in_points,
                                  double height_in_points) {
  DCHECK(IsSurfaceValid(surface_));
  DCHECK(IsContextValid(context_));
  // Passing this check implies page_surface_ is NULL, and current_page_ is
  // empty.
  DCHECK(!page_context_);
  DCHECK_GT(width_in_points, 0.);
  DCHECK_GT(height_in_points, 0.);

  // Creates a target surface for the new page.
  // Cairo 1.6.0 does NOT allow the first argument be NULL,
  // but some newer versions do support NULL pointer.
  switch (format_) {
    case PDF: {
      page_surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream,
                                                          &current_page_,
                                                          width_in_points,
                                                          height_in_points);
    }
    break;

    case PS: {
      page_surface_ = cairo_ps_surface_create_for_stream(WriteCairoStream,
                                                         &current_page_,
                                                         width_in_points,
                                                         height_in_points);
    }
    break;

    default:
      NOTREACHED();
      CleanUpAll();
      return NULL;
  }

  // Cairo always returns a valid pointer.
  // Hence, we have to check if it points to a "nil" object.
  if (!IsSurfaceValid(page_surface_)) {
    DLOG(ERROR) << "Cannot create Cairo surface for PdfPsMetafile!";
    CleanUpAll();
    return NULL;
  }

  // Creates a context.
  page_context_ = cairo_create(page_surface_);
  if (!IsContextValid(page_context_)) {
    DLOG(ERROR) << "Cannot create Cairo context for PdfPsMetafile!";
    CleanUpAll();
    return NULL;
  }

  return page_context_;
}

bool PdfPsMetafile::FinishPage(float shrink) {
  DCHECK(IsSurfaceValid(surface_));
  DCHECK(IsContextValid(context_));
  DCHECK(IsSurfaceValid(page_surface_));
  DCHECK(IsContextValid(page_context_));
  DCHECK_GT(shrink, 0);

  // Flushes all rendering for current page.
  cairo_surface_flush(page_surface_);

  // TODO(myhuang): Use real page settings.
  // We hard-coded page settings here for testing purpose.
  // The paper size is US Letter (8.5 in. by 11 in.).
  // The default margins are:
  //   Left = 0.25 in.
  //   Right = 0.25 in.
  //   Top = 0.25 in.
  //   Bottom = 0.56 in.
  const double kDPI = 72.0;  // Dots (points) per inch.
  const double kWidthInInch = 8.5;
  const double kHeightInInch = 11.0;
  const double kWidthInPoint = kWidthInInch * kDPI;
  const double kHeightInPoint = kHeightInInch * kDPI;
  switch (format_) {
    case PDF: {
      cairo_pdf_surface_set_size(surface_, kWidthInPoint, kHeightInPoint);
    }
    break;

    case PS: {
      cairo_ps_surface_set_size(surface_, kWidthInPoint, kHeightInPoint);
    }
    break;

    default:
      NOTREACHED();
      CleanUpAll();
      return false;
  }

  // Checks if our surface is still valid after resizing.
  if (!IsSurfaceValid(surface_)) {
    DLOG(ERROR) << "Cannot resize Cairo surface for PdfPsMetafile!";
    CleanUpAll();
    return false;
  }

  // Saves context's states.
  cairo_save(context_);
  // Copies current page onto the surface of final result.
  // Margins are done by coordinates transformation.
  // Please NOTE that we have to call cairo_scale() before we call
  // cairo_set_source_surface().
  const double scale_factor = 1. / shrink;
  cairo_scale(context_, scale_factor, scale_factor);
  const double kLeftMarginInInch = 0.25;
  const double kTopMarginInInch = 0.25;
  const double kLeftMarginInPoint = kLeftMarginInInch * kDPI;
  const double kTopMarginInPoint = kTopMarginInInch * kDPI;
  const double kScaledLeftMarginInPoint = kLeftMarginInPoint * shrink;
  const double kScaledTopMarginInPoint = kTopMarginInPoint * shrink;
  cairo_set_source_surface(context_,
                           page_surface_,
                           kScaledLeftMarginInPoint,
                           kScaledTopMarginInPoint);
  // In Cairo 1.6.0, if we use the following API, either the renderer will
  // crash, or we will get an empty page. This might be a bug in Cairo.
  // cairo_set_operator(context_, CAIRO_OPERATOR_SOURCE);
  const double kRightMarginInInch = 0.25;
  const double kBottomMarginInInch = 0.56;
  const double kPrintableWidthInInch =
      kWidthInInch - kLeftMarginInInch - kRightMarginInInch;
  const double kPrintableHeightInInch =
      kHeightInInch - kTopMarginInInch - kBottomMarginInInch;
  const double kScaledPrintableWidthInPoint =
      kPrintableWidthInInch * kDPI * shrink;
  const double kScaledPrintableHeightInPoint =
      kPrintableHeightInInch * kDPI * shrink;
  cairo_rectangle(context_,
                  kScaledLeftMarginInPoint,
                  kScaledTopMarginInPoint,
                  kScaledPrintableWidthInPoint,
                  kScaledPrintableHeightInPoint);
  cairo_fill(context_);

  // Finishes the duplication of current page.
  cairo_show_page(context_);
  cairo_surface_flush(surface_);

  // Destroys resources for current page.
  CleanUpContext(&page_context_);
  CleanUpSurface(&page_surface_);
  current_page_.clear();

  // Restores context's states.
  cairo_restore(context_);

  return true;
}

void PdfPsMetafile::Close() {
  DCHECK(IsSurfaceValid(surface_));
  DCHECK(IsContextValid(context_));
  // Passing this check implies page_surface_ is NULL, and current_page_ is
  // empty.
  DCHECK(!page_context_);

  cairo_surface_finish(surface_);
  DCHECK(!all_pages_.empty());  // Make sure we did get something.

  CleanUpContext(&context_);
  CleanUpSurface(&surface_);
  CleanUpFonts();
  CleanUpFreeType();
}

// static
bool PdfPsMetafile::SelectFontById(cairo_t* context, uint32_t font_id) {
  DCHECK(IsContextValid(context));
  DCHECK(SkFontHost::ValidFontID(font_id));
  DCHECK(g_ft_library);

  // Checks if we have a cache hit.
  MapFontId2FontInfo* g_font_cache = Singleton<MapFontId2FontInfo>::get();
  DCHECK(g_font_cache);

  MapFontId2FontInfo::iterator it = g_font_cache->find(font_id);
  if (it != g_font_cache->end()) {
    cairo_set_font_face(context, it->second.cairo_face);
    if (IsContextValid(context)) {
      return true;
    } else {
      NOTREACHED() << "Cannot set font face in Cairo!";
      return false;
    }
  }

  // Cache missed. We need to load and create the font.
  FontInfo new_font_info = {0};
  new_font_info.font_stream = SkFontHost::OpenStream(font_id);
  DCHECK(new_font_info.font_stream);
  size_t stream_size = new_font_info.font_stream->getLength();
  DCHECK(stream_size) << "The Font stream has nothing!";

  FT_Error ft_error = FT_New_Memory_Face(
      g_ft_library,
      static_cast<FT_Byte*>(
          const_cast<void*>(new_font_info.font_stream->getMemoryBase())),
      stream_size,
      0,
      &new_font_info.ft_face);

  if (ft_error) {
    new_font_info.font_stream->unref();
    DLOG(ERROR) << "Cannot create FT_Face!";
    SkASSERT(false);
    return false;
  }

  new_font_info.cairo_face = cairo_ft_font_face_create_for_ft_face(
      new_font_info.ft_face, 0);
  DCHECK(new_font_info.cairo_face) << "Cannot create font in Cairo!";

  // Manage |new_font_info.ft_face|'s life by Cairo.
  cairo_status_t status = cairo_font_face_set_user_data(
      new_font_info.cairo_face,
      &new_font_info.data_key,
      new_font_info.ft_face,
      reinterpret_cast<cairo_destroy_func_t>(FT_Done_Face));

  if (status != CAIRO_STATUS_SUCCESS) {
    DLOG(ERROR) << "Cannot set font's user data in Cairo!";
    cairo_font_face_destroy(new_font_info.cairo_face);
    FT_Done_Face(new_font_info.ft_face);
    new_font_info.font_stream->unref();
    SkASSERT(false);
    return false;
  }

  // Inserts |new_font_info| info |g_font_cache|.
  (*g_font_cache)[font_id] = new_font_info;

  cairo_set_font_face(context, new_font_info.cairo_face);
  if (IsContextValid(context)) {
    return true;
  }

  DLOG(ERROR) << "Connot set font face in Cairo!";
  return false;
}

unsigned int PdfPsMetafile::GetDataSize() const {
  // We need to check at least these two members to ensure that either Init()
  // has been called to initialize |all_pages_|, or metafile has been closed.
  // Passing these two checks also implies that surface_, page_surface_, and
  // page_context_ are NULL, and current_page_ is empty.
  DCHECK(!context_);
  DCHECK(!all_pages_.empty());

  return all_pages_.size();
}

bool PdfPsMetafile::GetData(void* dst_buffer, size_t dst_buffer_size) const {
  DCHECK(dst_buffer);
  DCHECK_GT(dst_buffer_size, 0u);
  // We need to check at least these two members to ensure that either Init()
  // has been called to initialize |all_pages_|, or metafile has been closed.
  // Passing these two checks also implies that surface_, page_surface_, and
  // page_context_ are NULL, and current_page_ is empty.
  DCHECK(!context_);
  DCHECK(!all_pages_.empty());

  size_t data_size = GetDataSize();
  if (dst_buffer_size > data_size) {
    return false;
  }

  memcpy(dst_buffer, all_pages_.data(), dst_buffer_size);

  return true;
}

bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const {
  // We need to check at least these two members to ensure that either Init()
  // has been called to initialize |all_pages_|, or metafile has been closed.
  // Passing these two checks also implies that surface_, page_surface_, and
  // page_context_ are NULL, and current_page_ is empty.
  DCHECK(!context_);
  DCHECK(!all_pages_.empty());

  if (fd.fd < 0) {
    DLOG(ERROR) << "Invalid file descriptor!";
    return false;
  }

  bool success = true;
  if (file_util::WriteFileDescriptor(fd.fd, all_pages_.data(),
                                     GetDataSize()) < 0) {
    DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
    success = false;
  }

  if (fd.auto_close)
    HANDLE_EINTR(close(fd.fd));
  return success;
}

void PdfPsMetafile::CleanUpAll() {
  CleanUpContext(&context_);
  CleanUpSurface(&surface_);
  CleanUpContext(&page_context_);
  CleanUpSurface(&page_surface_);
  current_page_.clear();
  all_pages_.clear();
  CleanUpFonts();
  CleanUpFreeType();
}

}  // namespace printing