OLD | NEW |
---|---|
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 2059 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2070 // Only one page but didn't start midway. | 2070 // Only one page but didn't start midway. |
2071 ((pages_.size() == 1 && last_character_index_to_search_ == -1) || | 2071 ((pages_.size() == 1 && last_character_index_to_search_ == -1) || |
2072 // Started midway, but only 1 page and we already looped around. | 2072 // Started midway, but only 1 page and we already looped around. |
2073 (pages_.size() == 1 && !first_search) || | 2073 (pages_.size() == 1 && !first_search) || |
2074 // Started midway, and we've just looped around. | 2074 // Started midway, and we've just looped around. |
2075 (pages_.size() > 1 && current_page == next_page_to_search_)); | 2075 (pages_.size() > 1 && current_page == next_page_to_search_)); |
2076 | 2076 |
2077 if (end_of_search) { | 2077 if (end_of_search) { |
2078 // Send the final notification. | 2078 // Send the final notification. |
2079 client_->NotifyNumberOfFindResultsChanged(find_results_.size(), true); | 2079 client_->NotifyNumberOfFindResultsChanged(find_results_.size(), true); |
2080 | |
2081 // When searching is complete, resume finding at a particular index. | |
2082 // Assuming the user has not clicked the find button in the meanwhile. | |
2083 if (resume_find_index_.valid() && !current_find_index_.valid()) { | |
2084 size_t resume_index = resume_find_index_.GetIndex(); | |
2085 if (resume_index >= find_results_.size()) { | |
2086 // This might happen if the PDF has some dynamically generated text? | |
2087 resume_index = 0; | |
2088 } | |
2089 current_find_index_.SetIndex(resume_index); | |
2090 client_->NotifySelectedFindResultChanged(resume_index); | |
2091 } | |
2092 resume_find_index_.Invalidate(); | |
2093 } else { | 2080 } else { |
2094 pp::CompletionCallback callback = | 2081 pp::CompletionCallback callback = |
2095 find_factory_.NewCallback(&PDFiumEngine::ContinueFind); | 2082 find_factory_.NewCallback(&PDFiumEngine::ContinueFind); |
2096 pp::Module::Get()->core()->CallOnMainThread(0, callback, | 2083 pp::Module::Get()->core()->CallOnMainThread(0, callback, |
2097 case_sensitive ? 1 : 0); | 2084 case_sensitive ? 1 : 0); |
2098 } | 2085 } |
2099 } | 2086 } |
2100 | 2087 |
2101 void PDFiumEngine::SearchUsingPDFium(const base::string16& term, | 2088 void PDFiumEngine::SearchUsingPDFium(const base::string16& term, |
2102 bool case_sensitive, | 2089 bool case_sensitive, |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2191 int char_index = result.char_index(); | 2178 int char_index = result.char_index(); |
2192 for (result_index = 0; result_index < find_results_.size(); ++result_index) { | 2179 for (result_index = 0; result_index < find_results_.size(); ++result_index) { |
2193 if (find_results_[result_index].page_index() > page_index || | 2180 if (find_results_[result_index].page_index() > page_index || |
2194 (find_results_[result_index].page_index() == page_index && | 2181 (find_results_[result_index].page_index() == page_index && |
2195 find_results_[result_index].char_index() > char_index)) { | 2182 find_results_[result_index].char_index() > char_index)) { |
2196 break; | 2183 break; |
2197 } | 2184 } |
2198 } | 2185 } |
2199 find_results_.insert(find_results_.begin() + result_index, result); | 2186 find_results_.insert(find_results_.begin() + result_index, result); |
2200 UpdateTickMarks(); | 2187 UpdateTickMarks(); |
2201 | |
2202 if (current_find_index_.valid()) { | |
2203 if (result_index <= current_find_index_.GetIndex()) { | |
2204 // Update the current match index | |
2205 size_t find_index = current_find_index_.IncrementIndex(); | |
2206 DCHECK_LT(find_index, find_results_.size()); | |
2207 client_->NotifySelectedFindResultChanged(current_find_index_.GetIndex()); | |
2208 } | |
2209 } else if (!resume_find_index_.valid()) { | |
2210 // Both indices are invalid. Select the first match. | |
2211 SelectFindResult(true); | |
2212 } | |
2213 client_->NotifyNumberOfFindResultsChanged(find_results_.size(), false); | 2188 client_->NotifyNumberOfFindResultsChanged(find_results_.size(), false); |
2214 } | 2189 } |
2215 | 2190 |
2216 bool PDFiumEngine::SelectFindResult(bool forward) { | 2191 bool PDFiumEngine::SelectFindResult(bool forward) { |
2217 if (find_results_.empty()) { | 2192 if (find_results_.empty()) { |
2218 NOTREACHED(); | 2193 NOTREACHED(); |
2219 return false; | 2194 return false; |
2220 } | 2195 } |
2221 | 2196 |
2222 SelectionChangeInvalidator selection_invalidator(this); | 2197 SelectionChangeInvalidator selection_invalidator(this); |
2223 | 2198 |
2224 // Move back/forward through the search locations we previously found. | 2199 // Move back/forward through the search locations we previously found. |
2225 size_t new_index; | 2200 size_t new_index; |
2226 const size_t last_index = find_results_.size() - 1; | 2201 const size_t last_index = find_results_.size() - 1; |
2227 if (current_find_index_.valid()) { | 2202 |
2203 if (resume_find_index_.valid()) { | |
2204 new_index = resume_find_index_.GetIndex(); | |
2205 resume_find_index_.Invalidate(); | |
2206 } else if (current_find_index_.valid()) { | |
2228 size_t current_index = current_find_index_.GetIndex(); | 2207 size_t current_index = current_find_index_.GetIndex(); |
2229 if (forward) { | 2208 if (forward) { |
2209 if (current_index >= last_index) { | |
Lei Zhang
2017/04/24 21:11:12
It feels like if we have to special-case going for
paulmeyer
2017/04/24 21:44:22
Whoops, you're very right. I had meant to put that
| |
2210 current_find_index_.Invalidate(); | |
2211 client_->NotifySelectedFindResultChanged(-1); | |
2212 client_->NotifyNumberOfFindResultsChanged(find_results_.size(), true); | |
2213 return true; | |
2214 } | |
2230 new_index = (current_index >= last_index) ? 0 : current_index + 1; | 2215 new_index = (current_index >= last_index) ? 0 : current_index + 1; |
Lei Zhang
2017/04/24 21:11:12
This always evals to: new_index = current_index +
paulmeyer
2017/04/24 21:44:22
Done.
| |
2231 } else { | 2216 } else { |
2232 new_index = (current_find_index_.GetIndex() == 0) ? last_index | 2217 new_index = (current_find_index_.GetIndex() == 0) ? last_index |
2233 : current_index - 1; | 2218 : current_index - 1; |
2234 } | 2219 } |
2235 } else { | 2220 } else { |
2236 new_index = forward ? 0 : last_index; | 2221 new_index = forward ? 0 : last_index; |
2237 } | 2222 } |
2238 current_find_index_.SetIndex(new_index); | 2223 current_find_index_.SetIndex(new_index); |
2239 | 2224 |
2240 // Update the selection before telling the client to scroll, since it could | 2225 // Update the selection before telling the client to scroll, since it could |
(...skipping 1888 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4129 FPDF_DOCUMENT doc = | 4114 FPDF_DOCUMENT doc = |
4130 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); | 4115 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); |
4131 if (!doc) | 4116 if (!doc) |
4132 return false; | 4117 return false; |
4133 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 4118 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
4134 FPDF_CloseDocument(doc); | 4119 FPDF_CloseDocument(doc); |
4135 return success; | 4120 return success; |
4136 } | 4121 } |
4137 | 4122 |
4138 } // namespace chrome_pdf | 4123 } // namespace chrome_pdf |
OLD | NEW |