OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include <memory> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 const std::string& data) | 41 const std::string& data) |
42 : pdf_library_(pdf_library), | 42 : pdf_library_(pdf_library), |
43 doc_(doc), | 43 doc_(doc), |
44 data_(data), | 44 data_(data), |
45 page_count_(FPDF_GetPageCount(doc_)) {} | 45 page_count_(FPDF_GetPageCount(doc_)) {} |
46 | 46 |
47 ~PDFDocument() { FPDF_CloseDocument(doc_); } | 47 ~PDFDocument() { FPDF_CloseDocument(doc_); } |
48 | 48 |
49 uint32_t page_count() { return page_count_; } | 49 uint32_t page_count() { return page_count_; } |
50 | 50 |
51 skia::RefPtr<SkImage> DrawPage(int page_index, const mojo::Size& size) { | 51 sk_sp<SkImage> DrawPage(int page_index, const mojo::Size& size) { |
52 FPDF_PAGE page = FPDF_LoadPage(doc_, page_index); | 52 FPDF_PAGE page = FPDF_LoadPage(doc_, page_index); |
53 | 53 |
54 double width_pts = FPDF_GetPageWidth(page); | 54 double width_pts = FPDF_GetPageWidth(page); |
55 double height_pts = FPDF_GetPageHeight(page); | 55 double height_pts = FPDF_GetPageHeight(page); |
56 | 56 |
57 int width, height; | 57 int width, height; |
58 if (size.width * height_pts < size.height * width_pts) { | 58 if (size.width * height_pts < size.height * width_pts) { |
59 width = size.width; | 59 width = size.width; |
60 height = height_pts * size.width / width_pts; | 60 height = height_pts * size.width / width_pts; |
61 } else { | 61 } else { |
62 width = width_pts * size.height / height_pts; | 62 width = width_pts * size.height / height_pts; |
63 height = size.height; | 63 height = size.height; |
64 } | 64 } |
65 | 65 |
66 int stride = width * 4; | 66 int stride = width * 4; |
67 skia::RefPtr<SkData> pixels = | 67 sk_sp<SkData> pixels = SkData::MakeUninitialized(stride * height); |
68 skia::AdoptRef(SkData::NewUninitialized(stride * height)); | |
69 DCHECK(pixels); | 68 DCHECK(pixels); |
70 | 69 |
71 FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(width, height, FPDFBitmap_BGRA, | 70 FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(width, height, FPDFBitmap_BGRA, |
72 pixels->writable_data(), stride); | 71 pixels->writable_data(), stride); |
73 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); | 72 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
74 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); | 73 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
75 FPDFBitmap_Destroy(bitmap); | 74 FPDFBitmap_Destroy(bitmap); |
76 | 75 |
77 FPDF_ClosePage(page); | 76 FPDF_ClosePage(page); |
78 | 77 |
79 SkImageInfo info = SkImageInfo::Make(width, height, kBGRA_8888_SkColorType, | 78 SkImageInfo info = SkImageInfo::Make(width, height, kBGRA_8888_SkColorType, |
80 kOpaque_SkAlphaType); | 79 kOpaque_SkAlphaType); |
81 return skia::AdoptRef(SkImage::NewRasterData(info, pixels.get(), stride)); | 80 return SkImage::MakeRasterData(info, pixels, stride); |
82 } | 81 } |
83 | 82 |
84 private: | 83 private: |
85 std::shared_ptr<PDFLibrary> pdf_library_; | 84 std::shared_ptr<PDFLibrary> pdf_library_; |
86 FPDF_DOCUMENT doc_; | 85 FPDF_DOCUMENT doc_; |
87 std::string data_; | 86 std::string data_; |
88 uint32_t page_count_; | 87 uint32_t page_count_; |
89 }; | 88 }; |
90 | 89 |
91 class PDFLibrary : public std::enable_shared_from_this<PDFLibrary> { | 90 class PDFLibrary : public std::enable_shared_from_this<PDFLibrary> { |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 } | 236 } |
238 | 237 |
239 void GotoPreviousPage() { | 238 void GotoPreviousPage() { |
240 if (page_ > 0) { | 239 if (page_ > 0) { |
241 page_--; | 240 page_--; |
242 Redraw(); | 241 Redraw(); |
243 } | 242 } |
244 } | 243 } |
245 | 244 |
246 void Redraw() { | 245 void Redraw() { |
247 cached_image_.clear(); | 246 cached_image_.reset(); |
248 choreographer_.ScheduleDraw(); | 247 choreographer_.ScheduleDraw(); |
249 } | 248 } |
250 | 249 |
251 std::shared_ptr<PDFDocument> pdf_document_; | 250 std::shared_ptr<PDFDocument> pdf_document_; |
252 uint32_t page_ = 0u; | 251 uint32_t page_ = 0u; |
253 skia::RefPtr<SkImage> cached_image_; | 252 sk_sp<SkImage> cached_image_; |
254 | 253 |
255 mojo::ui::Choreographer choreographer_; | 254 mojo::ui::Choreographer choreographer_; |
256 mojo::ui::InputHandler input_handler_; | 255 mojo::ui::InputHandler input_handler_; |
257 | 256 |
258 DISALLOW_COPY_AND_ASSIGN(PDFDocumentView); | 257 DISALLOW_COPY_AND_ASSIGN(PDFDocumentView); |
259 }; | 258 }; |
260 | 259 |
261 class PDFContentViewProviderApp : public mojo::ui::ViewProviderApp { | 260 class PDFContentViewProviderApp : public mojo::ui::ViewProviderApp { |
262 public: | 261 public: |
263 PDFContentViewProviderApp(const std::shared_ptr<PDFLibrary>& pdf_library, | 262 PDFContentViewProviderApp(const std::shared_ptr<PDFLibrary>& pdf_library, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 | 307 |
309 DISALLOW_COPY_AND_ASSIGN(PDFContentViewerApp); | 308 DISALLOW_COPY_AND_ASSIGN(PDFContentViewerApp); |
310 }; | 309 }; |
311 | 310 |
312 } // namespace examples | 311 } // namespace examples |
313 | 312 |
314 MojoResult MojoMain(MojoHandle application_request) { | 313 MojoResult MojoMain(MojoHandle application_request) { |
315 mojo::ApplicationRunnerChromium runner(new examples::PDFContentViewerApp()); | 314 mojo::ApplicationRunnerChromium runner(new examples::PDFContentViewerApp()); |
316 return runner.Run(application_request); | 315 return runner.Run(application_request); |
317 } | 316 } |
OLD | NEW |