Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: pdf/pdfium/pdfium_engine.cc

Issue 2645813007: PDF: Fix a failing find-in-page corner case. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pdf/pdfium/pdfium_engine.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "pdf/pdfium/pdfium_engine.h" 5 #include "pdf/pdfium/pdfium_engine.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 2154 matching lines...) Expand 10 before | Expand all | Expand 10 after
2165 } 2165 }
2166 2166
2167 FPDFText_FindClose(find); 2167 FPDFText_FindClose(find);
2168 } 2168 }
2169 2169
2170 void PDFiumEngine::SearchUsingICU(const base::string16& term, 2170 void PDFiumEngine::SearchUsingICU(const base::string16& term,
2171 bool case_sensitive, 2171 bool case_sensitive,
2172 bool first_search, 2172 bool first_search,
2173 int character_to_start_searching_from, 2173 int character_to_start_searching_from,
2174 int current_page) { 2174 int current_page) {
2175 base::string16 page_text; 2175 DCHECK(!term.empty());
2176 int text_length = pages_[current_page]->GetCharCount(); 2176
2177 const int original_text_length = pages_[current_page]->GetCharCount();
2178 int text_length = original_text_length;
2177 if (character_to_start_searching_from) { 2179 if (character_to_start_searching_from) {
2178 text_length -= character_to_start_searching_from; 2180 text_length -= character_to_start_searching_from;
2179 } else if (!first_search && 2181 } else if (!first_search &&
2180 last_character_index_to_search_ != -1 && 2182 last_character_index_to_search_ != -1 &&
2181 current_page == last_page_to_search_) { 2183 current_page == last_page_to_search_) {
2182 text_length = last_character_index_to_search_; 2184 text_length = last_character_index_to_search_;
2183 } 2185 }
2184 if (text_length <= 0) 2186 if (text_length <= 0)
2185 return; 2187 return;
2186 2188
2189 base::string16 page_text;
2187 PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter(&page_text, 2190 PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter(&page_text,
2188 text_length, 2191 text_length,
2189 false); 2192 false);
2190 unsigned short* data = 2193 unsigned short* data =
2191 reinterpret_cast<unsigned short*>(api_string_adapter.GetData()); 2194 reinterpret_cast<unsigned short*>(api_string_adapter.GetData());
2192 int written = FPDFText_GetText(pages_[current_page]->GetTextPage(), 2195 int written = FPDFText_GetText(pages_[current_page]->GetTextPage(),
2193 character_to_start_searching_from, 2196 character_to_start_searching_from,
2194 text_length, 2197 text_length,
2195 data); 2198 data);
2196 api_string_adapter.Close(written); 2199 api_string_adapter.Close(written);
2197 2200
2198 std::vector<PDFEngine::Client::SearchStringResult> results; 2201 std::vector<PDFEngine::Client::SearchStringResult> results;
2199 client_->SearchString( 2202 client_->SearchString(
2200 page_text.c_str(), term.c_str(), case_sensitive, &results); 2203 page_text.c_str(), term.c_str(), case_sensitive, &results);
2201 for (const auto& result : results) { 2204 for (const auto& result : results) {
2202 // Need to map the indexes from the page text, which may have generated 2205 // Need to map the indexes from the page text, which may have generated
2203 // characters like space etc, to character indices from the page. 2206 // characters like space etc, to character indices from the page.
2204 int temp_start = result.start_index + character_to_start_searching_from; 2207 int temp_start = result.start_index + character_to_start_searching_from;
2205 int start = FPDFText_GetCharIndexFromTextIndex( 2208 int start = FPDFText_GetCharIndexFromTextIndex(
2206 pages_[current_page]->GetTextPage(), temp_start); 2209 pages_[current_page]->GetTextPage(), temp_start);
2207 int end = FPDFText_GetCharIndexFromTextIndex( 2210 int end = FPDFText_GetCharIndexFromTextIndex(
2208 pages_[current_page]->GetTextPage(), 2211 pages_[current_page]->GetTextPage(),
2209 temp_start + result.length); 2212 temp_start + result.length);
2213
2214 // If |term| occurs at the end of a page, then |end| will be -1 due to the
2215 // index being out of bounds. Compensate for this case so the range
2216 // character count calculation below works out.
2217 if (temp_start + result.length == original_text_length) {
2218 DCHECK_EQ(-1, end);
2219 end = original_text_length;
2220 }
2221 DCHECK_LT(start, end);
2222 DCHECK_EQ(term.size(), static_cast<size_t>(end - start));
2210 AddFindResult(PDFiumRange(pages_[current_page].get(), start, end - start)); 2223 AddFindResult(PDFiumRange(pages_[current_page].get(), start, end - start));
2211 } 2224 }
2212 } 2225 }
2213 2226
2214 void PDFiumEngine::AddFindResult(const PDFiumRange& result) { 2227 void PDFiumEngine::AddFindResult(const PDFiumRange& result) {
2215 // Figure out where to insert the new location, since we could have 2228 // Figure out where to insert the new location, since we could have
2216 // started searching midway and now we wrapped. 2229 // started searching midway and now we wrapped.
2217 size_t result_index; 2230 size_t result_index;
2218 int page_index = result.page_index(); 2231 int page_index = result.page_index();
2219 int char_index = result.char_index(); 2232 int char_index = result.char_index();
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
2267 2280
2268 // Update the selection before telling the client to scroll, since it could 2281 // Update the selection before telling the client to scroll, since it could
2269 // paint then. 2282 // paint then.
2270 selection_.clear(); 2283 selection_.clear();
2271 selection_.push_back(find_results_[current_find_index_.GetIndex()]); 2284 selection_.push_back(find_results_[current_find_index_.GetIndex()]);
2272 2285
2273 // If the result is not in view, scroll to it. 2286 // If the result is not in view, scroll to it.
2274 pp::Rect bounding_rect; 2287 pp::Rect bounding_rect;
2275 pp::Rect visible_rect = GetVisibleRect(); 2288 pp::Rect visible_rect = GetVisibleRect();
2276 // Use zoom of 1.0 since visible_rect is without zoom. 2289 // Use zoom of 1.0 since visible_rect is without zoom.
2277 std::vector<pp::Rect> rects; 2290 std::vector<pp::Rect> rects =
2278 rects = find_results_[current_find_index_.GetIndex()].GetScreenRects( 2291 find_results_[current_find_index_.GetIndex()].GetScreenRects(
2279 pp::Point(), 1.0, current_rotation_); 2292 pp::Point(), 1.0, current_rotation_);
2280 for (const auto& rect : rects) 2293 for (const auto& rect : rects)
2281 bounding_rect = bounding_rect.Union(rect); 2294 bounding_rect = bounding_rect.Union(rect);
2282 if (!visible_rect.Contains(bounding_rect)) { 2295 if (!visible_rect.Contains(bounding_rect)) {
2283 pp::Point center = bounding_rect.CenterPoint(); 2296 pp::Point center = bounding_rect.CenterPoint();
2284 // Make the page centered. 2297 // Make the page centered.
2285 int new_y = static_cast<int>(center.y() * current_zoom_) - 2298 int new_y = static_cast<int>(center.y() * current_zoom_) -
2286 static_cast<int>(visible_rect.height() * current_zoom_ / 2); 2299 static_cast<int>(visible_rect.height() * current_zoom_ / 2);
2287 if (new_y < 0) 2300 if (new_y < 0)
2288 new_y = 0; 2301 new_y = 0;
2289 client_->ScrollToY(new_y); 2302 client_->ScrollToY(new_y);
(...skipping 1892 matching lines...) Expand 10 before | Expand all | Expand 10 after
4182 FPDF_DOCUMENT doc = 4195 FPDF_DOCUMENT doc =
4183 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4196 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4184 if (!doc) 4197 if (!doc)
4185 return false; 4198 return false;
4186 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4199 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4187 FPDF_CloseDocument(doc); 4200 FPDF_CloseDocument(doc);
4188 return success; 4201 return success;
4189 } 4202 }
4190 4203
4191 } // namespace chrome_pdf 4204 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/pdfium/pdfium_engine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698