Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "printing/pdf_metafile_skia.h" | |
| 6 | |
| 7 #include "base/eintr_wrapper.h" | |
| 8 #include "base/file_descriptor_posix.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "skia/ext/vector_platform_device_skia.h" | |
| 12 #include "third_party/skia/include/core/SkRefCnt.h" | |
| 13 #include "third_party/skia/include/core/SkStream.h" | |
| 14 #include "third_party/skia/include/pdf/SkPDFDevice.h" | |
| 15 #include "third_party/skia/include/pdf/SkPDFDocument.h" | |
| 16 #include "third_party/skia/include/pdf/SkPDFPage.h" | |
| 17 #include "ui/gfx/point.h" | |
| 18 #include "ui/gfx/rect.h" | |
| 19 #include "ui/gfx/size.h" | |
| 20 | |
| 21 namespace printing { | |
| 22 | |
| 23 struct PdfMetafileSkiaData { | |
| 24 SkRefPtr<SkPDFDevice> current_page_; | |
| 25 SkPDFDocument pdf_doc_; | |
| 26 SkDynamicMemoryWStream pdf_stream_; | |
| 27 }; | |
| 28 | |
| 29 PdfMetafileSkia::~PdfMetafileSkia() {} | |
| 30 | |
| 31 bool PdfMetafileSkia::Init() { | |
| 32 return true; | |
| 33 } | |
| 34 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | |
| 35 uint32 src_buffer_size) { | |
| 36 return data_->pdf_stream_.write(src_buffer, src_buffer_size); | |
| 37 } | |
| 38 | |
| 39 skia::PlatformDevice* PdfMetafileSkia::StartPageForVectorCanvas( | |
| 40 const gfx::Size& page_size, const gfx::Point& content_origin, | |
| 41 const float& scale_factor) { | |
| 42 DCHECK(data_->current_page_.get() == NULL); | |
| 43 | |
| 44 skia::VectorPlatformDeviceSkia* device = | |
| 45 new skia::VectorPlatformDeviceSkia(page_size.width(), page_size.height(), | |
| 46 SkPDFDevice::kFlip_OriginTransform); | |
| 47 device->setInitialTransform(content_origin.x(), content_origin.y(), | |
| 48 scale_factor); | |
| 49 data_->current_page_ = device->PdfDevice(); | |
| 50 return device; | |
| 51 } | |
| 52 | |
| 53 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, | |
| 54 const gfx::Point& content_origin, | |
| 55 const float& scale_factor) { | |
| 56 NOTREACHED(); | |
| 57 return NULL; | |
| 58 } | |
| 59 | |
| 60 bool PdfMetafileSkia::FinishPage() { | |
| 61 DCHECK(data_->current_page_.get()); | |
| 62 | |
| 63 data_->pdf_doc_.appendPage(data_->current_page_); | |
| 64 data_->current_page_ = NULL; | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 bool PdfMetafileSkia::FinishDocument() { | |
| 69 // Don't do anything if we've already set the data in InitFromData. | |
| 70 if (data_->pdf_stream_.getOffset()) | |
| 71 return true; | |
| 72 | |
| 73 if (data_->current_page_.get()) | |
| 74 FinishPage(); | |
|
Lei Zhang
2011/04/06 20:43:56
nit: indentation
vandebo (ex-Chrome)
2011/04/06 22:01:37
Done.
| |
| 75 data_->pdf_doc_.emitPDF(&data_->pdf_stream_); | |
|
Lei Zhang
2011/04/06 20:43:56
check emitPDF's return value?
vandebo (ex-Chrome)
2011/04/06 22:01:37
Done.
| |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 uint32 PdfMetafileSkia::GetDataSize() const { | |
| 80 return data_->pdf_stream_.getOffset(); | |
| 81 } | |
| 82 | |
| 83 bool PdfMetafileSkia::GetData(void* dst_buffer, | |
| 84 uint32 dst_buffer_size) const { | |
| 85 if (dst_buffer_size > GetDataSize()) | |
|
Lei Zhang
2011/04/06 20:43:56
is this reversed?
dst_buffer_size = 10, GetDataSi
vandebo (ex-Chrome)
2011/04/06 22:01:37
Done.
| |
| 86 return false; | |
| 87 | |
| 88 memcpy(dst_buffer, data_->pdf_stream_.getStream(), dst_buffer_size); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const { | |
| 93 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | |
| 94 if (file_util::WriteFile(file_path, data_->pdf_stream_.getStream(), | |
| 95 GetDataSize()) != static_cast<int>(GetDataSize())) { | |
| 96 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str(); | |
| 97 return false; | |
| 98 } | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { | |
| 103 // TODO(vandebo) add a method to get the page size for a given page to | |
| 104 // SkPDFDocument. | |
| 105 NOTIMPLEMENTED(); | |
| 106 return gfx::Rect(); | |
| 107 } | |
| 108 | |
| 109 unsigned int PdfMetafileSkia::GetPageCount() const { | |
| 110 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. | |
| 111 NOTIMPLEMENTED(); | |
| 112 return 0; | |
| 113 } | |
| 114 | |
| 115 gfx::NativeDrawingContext PdfMetafileSkia::context() const { | |
| 116 NOTREACHED(); | |
| 117 return NULL; | |
| 118 } | |
| 119 | |
| 120 #if defined(OS_WIN) | |
| 121 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, | |
| 122 const RECT* rect) const { | |
| 123 NOTREACHED(); | |
| 124 return false; | |
| 125 } | |
| 126 | |
| 127 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const { | |
| 128 NOTREACHED(); | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 HENHMETAFILE PdfMetafileSkia::emf() const { | |
| 133 NOTREACHED(); | |
| 134 return NULL; | |
| 135 } | |
| 136 #endif // if defined(OS_WIN) | |
| 137 | |
| 138 #if defined(OS_CHROMEOS) | |
| 139 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { | |
| 140 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | |
| 141 | |
| 142 if (fd.fd < 0) { | |
| 143 DLOG(ERROR) << "Invalid file descriptor!"; | |
| 144 return false; | |
| 145 } | |
| 146 | |
| 147 bool result = true; | |
| 148 if (file_util::WriteFileDescriptor(fd.fd, data_->pdf_stream_.getStream(), | |
| 149 GetDataSize()) != | |
| 150 static_cast<int>(GetDataSize())) { | |
| 151 DLOG(ERROR) << "Failed to save file with fd " << fd.fd; | |
| 152 result = false; | |
| 153 } | |
| 154 | |
| 155 if (fd.auto_close) { | |
| 156 if (HANDLE_EINTR(close(fd.fd)) < 0) { | |
| 157 DPLOG(WARNING) << "close"; | |
| 158 result = false; | |
| 159 } | |
| 160 } | |
| 161 return result; | |
| 162 } | |
| 163 #endif | |
| 164 | |
| 165 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) {} | |
| 166 | |
| 167 } // namespace printing | |
| OLD | NEW |