• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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 "pdf/pdfium/pdfium_range.h"
6 
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 
10 namespace chrome_pdf {
11 
PDFiumRange(PDFiumPage * page,int char_index,int char_count)12 PDFiumRange::PDFiumRange(PDFiumPage* page, int char_index, int char_count)
13     : page_(page),
14       char_index_(char_index),
15       char_count_(char_count),
16       cached_screen_rects_zoom_(0) {
17 }
18 
~PDFiumRange()19 PDFiumRange::~PDFiumRange() {
20 }
21 
SetCharCount(int char_count)22 void PDFiumRange::SetCharCount(int char_count) {
23   char_count_ = char_count;
24 
25   cached_screen_rects_offset_ = pp::Point();
26   cached_screen_rects_zoom_ = 0;
27 }
28 
GetScreenRects(const pp::Point & offset,double zoom,int rotation)29 std::vector<pp::Rect> PDFiumRange::GetScreenRects(const pp::Point& offset,
30                                                   double zoom,
31                                                   int rotation) {
32   if (offset == cached_screen_rects_offset_ &&
33       zoom == cached_screen_rects_zoom_) {
34     return cached_screen_rects_;
35   }
36 
37   cached_screen_rects_.clear();
38   cached_screen_rects_offset_ = offset;
39   cached_screen_rects_zoom_ = zoom;
40 
41   int char_index = char_index_;
42   int char_count = char_count_;
43   if (char_count < 0) {
44     char_count *= -1;
45     char_index -= char_count - 1;
46   }
47 
48   int count = FPDFText_CountRects(page_->GetTextPage(), char_index, char_count);
49   for (int i = 0; i < count; ++i) {
50     double left, top, right, bottom;
51     FPDFText_GetRect(page_->GetTextPage(), i, &left, &top, &right, &bottom);
52     cached_screen_rects_.push_back(
53         page_->PageToScreen(offset, zoom, left, top, right, bottom, rotation));
54   }
55 
56   return cached_screen_rects_;
57 }
58 
GetText()59 base::string16 PDFiumRange::GetText() {
60   int index = char_index_;
61   int count = char_count_;
62   if (!count)
63     return base::string16();
64   if (count < 0) {
65     count *= -1;
66     index -= count - 1;
67   }
68 
69   base::string16 rv;
70   unsigned short* data =
71       reinterpret_cast<unsigned short*>(WriteInto(&rv, count + 1));
72   if (data) {
73     int written = FPDFText_GetText(page_->GetTextPage(), index, count, data);
74     rv.reserve(written);
75   }
76   return rv;
77 }
78 
79 }  // namespace chrome_pdf
80