diff options
author | thestig <thestig@chromium.org> | 2016-01-04 21:32:51 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-05 05:33:45 +0000 |
commit | ccb5fc8ff322c02db30604d0df003626eaff5f34 (patch) | |
tree | 14c7668905764c2da54ddb27d675817cad330f50 | |
parent | 910fc8840b56c94f2a172b3ae7b5c1b3b5108985 (diff) | |
download | chromium_src-ccb5fc8ff322c02db30604d0df003626eaff5f34.zip chromium_src-ccb5fc8ff322c02db30604d0df003626eaff5f34.tar.gz chromium_src-ccb5fc8ff322c02db30604d0df003626eaff5f34.tar.bz2 |
Another round of PDF plugin cleanups.
- Use for-range loops / std::vector::insert() when possible.
- Mark methods const.
- Use std::{string,vector}::empty() instead of size() when possible.
Review URL: https://codereview.chromium.org/1555153003
Cr-Commit-Position: refs/heads/master@{#367486}
-rw-r--r-- | pdf/chunk_stream.cc | 2 | ||||
-rw-r--r-- | pdf/chunk_stream.h | 2 | ||||
-rw-r--r-- | pdf/out_of_process_instance.cc | 26 | ||||
-rw-r--r-- | pdf/paint_aggregator.cc | 18 | ||||
-rw-r--r-- | pdf/paint_manager.cc | 15 | ||||
-rw-r--r-- | pdf/pdfium/pdfium_engine.cc | 180 | ||||
-rw-r--r-- | pdf/pdfium/pdfium_page.cc | 45 | ||||
-rw-r--r-- | pdf/pdfium/pdfium_page.h | 7 | ||||
-rw-r--r-- | pdf/pdfium/pdfium_range.cc | 2 | ||||
-rw-r--r-- | pdf/pdfium/pdfium_range.h | 4 |
10 files changed, 143 insertions, 158 deletions
diff --git a/pdf/chunk_stream.cc b/pdf/chunk_stream.cc index bdb5399..adb3cb6 100644 --- a/pdf/chunk_stream.cc +++ b/pdf/chunk_stream.cc @@ -35,7 +35,7 @@ void ChunkStream::Preallocate(size_t stream_size) { stream_size_ = stream_size; } -size_t ChunkStream::GetSize() { +size_t ChunkStream::GetSize() const { return data_.size(); } diff --git a/pdf/chunk_stream.h b/pdf/chunk_stream.h index 048f958..d2d8d2a 100644 --- a/pdf/chunk_stream.h +++ b/pdf/chunk_stream.h @@ -23,7 +23,7 @@ class ChunkStream { void Clear(); void Preallocate(size_t stream_size); - size_t GetSize(); + size_t GetSize() const; bool WriteData(size_t offset, void* buffer, size_t size); bool ReadData(size_t offset, size_t size, void* buffer) const; diff --git a/pdf/out_of_process_instance.cc b/pdf/out_of_process_instance.cc index 875642f..869fdf2 100644 --- a/pdf/out_of_process_instance.cc +++ b/pdf/out_of_process_instance.cc @@ -702,11 +702,11 @@ void OutOfProcessInstance::OnPaint( engine_->PrePaint(); - for (size_t i = 0; i < paint_rects.size(); i++) { + for (const auto& paint_rect : paint_rects) { // Intersect with plugin area since there could be pending invalidates from // when the plugin area was larger. pp::Rect rect = - paint_rects[i].Intersect(pp::Rect(pp::Point(), plugin_size_)); + paint_rect.Intersect(pp::Rect(pp::Point(), plugin_size_)); if (rect.IsEmpty()) continue; @@ -717,14 +717,14 @@ void OutOfProcessInstance::OnPaint( std::vector<pp::Rect> pdf_ready; std::vector<pp::Rect> pdf_pending; engine_->Paint(pdf_rect, &image_data_, &pdf_ready, &pdf_pending); - for (size_t j = 0; j < pdf_ready.size(); ++j) { - pdf_ready[j].Offset(available_area_.point()); + for (auto& ready_rect : pdf_ready) { + ready_rect.Offset(available_area_.point()); ready->push_back( - PaintManager::ReadyRect(pdf_ready[j], image_data_, false)); + PaintManager::ReadyRect(ready_rect, image_data_, false)); } - for (size_t j = 0; j < pdf_pending.size(); ++j) { - pdf_pending[j].Offset(available_area_.point()); - pending->push_back(pdf_pending[j]); + for (auto& pending_rect : pdf_pending) { + pending_rect.Offset(available_area_.point()); + pending->push_back(pending_rect); } } @@ -738,10 +738,10 @@ void OutOfProcessInstance::OnPaint( FillRect(region, background_color_); } - for (size_t j = 0; j < background_parts_.size(); ++j) { - pp::Rect intersection = background_parts_[j].location.Intersect(rect); + for (const auto& background_part : background_parts_) { + pp::Rect intersection = background_part.location.Intersect(rect); if (!intersection.IsEmpty()) { - FillRect(intersection, background_parts_[j].color); + FillRect(intersection, background_part.color); ready->push_back( PaintManager::ReadyRect(intersection, image_data_, false)); } @@ -922,8 +922,8 @@ void OutOfProcessInstance::UpdateTickMarks( const std::vector<pp::Rect>& tickmarks) { float inverse_scale = 1.0f / device_scale_; std::vector<pp::Rect> scaled_tickmarks = tickmarks; - for (size_t i = 0; i < scaled_tickmarks.size(); i++) - ScaleRect(inverse_scale, &scaled_tickmarks[i]); + for (auto& tickmark : scaled_tickmarks) + ScaleRect(inverse_scale, &tickmark); tickmarks_ = scaled_tickmarks; } diff --git a/pdf/paint_aggregator.cc b/pdf/paint_aggregator.cc index c14dded..50d77fe 100644 --- a/pdf/paint_aggregator.cc +++ b/pdf/paint_aggregator.cc @@ -105,15 +105,15 @@ PaintAggregator::PaintUpdate PaintAggregator::GetPendingUpdate() { // Include the scroll damage (if any) in the paint rects. // Code invalidates damaged rect here, it pick it up from the list of paint // rects in the next block. - if (ret.has_scroll && !update_.synthesized_scroll_damage_rect_) { + if (ret.has_scroll && !update_.synthesized_scroll_damage_rect_) { update_.synthesized_scroll_damage_rect_ = true; pp::Rect scroll_damage = update_.GetScrollDamage(); InvalidateRectInternal(scroll_damage, false); } ret.paint_rects.reserve(update_.paint_rects.size() + 1); - for (size_t i = 0; i < update_.paint_rects.size(); i++) - ret.paint_rects.push_back(update_.paint_rects[i]); + ret.paint_rects.insert(ret.paint_rects.end(), update_.paint_rects.begin(), + update_.paint_rects.end()); return ret; } @@ -217,14 +217,12 @@ void PaintAggregator::ScrollRect(const pp::Rect& clip_rect, } } - for (size_t i = 0; i < leftover_rects.size(); ++i) - InvalidateRectInternal(leftover_rects[i], false); + for (const auto& leftover_rect : leftover_rects) + InvalidateRectInternal(leftover_rect, false); - for (size_t i = 0; i < update_.ready_rects.size(); ++i) { - if (update_.scroll_rect.Contains(update_.ready_rects[i].rect)) { - update_.ready_rects[i].rect = - ScrollPaintRect(update_.ready_rects[i].rect, amount); - } + for (auto& update_rect : update_.ready_rects) { + if (update_.scroll_rect.Contains(update_rect.rect)) + update_rect.rect = ScrollPaintRect(update_rect.rect, amount); } if (update_.synthesized_scroll_damage_rect_) { diff --git a/pdf/paint_manager.cc b/pdf/paint_manager.cc index b1f543d..226994c 100644 --- a/pdf/paint_manager.cc +++ b/pdf/paint_manager.cc @@ -211,8 +211,7 @@ void PaintManager::DoPaint() { std::vector<PaintAggregator::ReadyRect> ready_now; if (pending.empty()) { std::vector<PaintAggregator::ReadyRect> temp_ready; - for (size_t i = 0; i < ready.size(); ++i) - temp_ready.push_back(ready[i]); + temp_ready.insert(temp_ready.end(), ready.begin(), ready.end()); aggregator_.SetIntermediateResults(temp_ready, pending); ready_now = aggregator_.GetReadyRects(); aggregator_.ClearPendingUpdate(); @@ -224,17 +223,17 @@ void PaintManager::DoPaint() { view_size_changed_waiting_for_paint_ = false; } else { std::vector<PaintAggregator::ReadyRect> ready_later; - for (size_t i = 0; i < ready.size(); ++i) { + for (const auto& ready_rect : ready) { // Don't flush any part (i.e. scrollbars) if we're resizing the browser, // as that'll lead to flashes. Until we flush, the browser will use the // previous image, but if we flush, it'll revert to using the blank image. // We make an exception for the first paint since we want to show the // default background color instead of the pepper default of black. - if (ready[i].flush_now && + if (ready_rect.flush_now && (!view_size_changed_waiting_for_paint_ || first_paint_)) { - ready_now.push_back(ready[i]); + ready_now.push_back(ready_rect); } else { - ready_later.push_back(ready[i]); + ready_later.push_back(ready_rect); } } // Take the rectangles, except the ones that need to be flushed right away, @@ -248,9 +247,9 @@ void PaintManager::DoPaint() { } } - for (size_t i = 0; i < ready_now.size(); ++i) { + for (const auto& ready_rect : ready_now) { graphics_.PaintImageData( - ready_now[i].image_data, ready_now[i].offset, ready_now[i].rect); + ready_rect.image_data, ready_rect.offset, ready_rect.rect); } int32_t result = graphics_.Flush( diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc index 4151bb2..87c7dcc 100644 --- a/pdf/pdfium/pdfium_engine.cc +++ b/pdf/pdfium/pdfium_engine.cc @@ -609,8 +609,8 @@ PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) } PDFiumEngine::~PDFiumEngine() { - for (size_t i = 0; i < pages_.size(); ++i) - pages_[i]->Unload(); + for (auto& page : pages_) + page->Unload(); if (doc_) { FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WC); @@ -882,11 +882,9 @@ FPDF_FILEHANDLER* PDFiumEngine::Form_OpenFile(FPDF_FORMFILLINFO* param, int file_flag, FPDF_WIDESTRING url, const char* mode) { - std::string url_str = "NULL"; - if (url != NULL) { - url_str = - base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(url)); - } + std::string url_str = url ? + base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(url)) : "NULL"; + // TODO: need to implement open file from the url // Use a file path for the ease of testing FILE* file = fopen(XFA_TESTFILE("tem.txt"), mode); @@ -978,8 +976,8 @@ void PDFiumEngine::ScrolledToYPosition(int position) { } void PDFiumEngine::PrePaint() { - for (size_t i = 0; i < progressive_paints_.size(); ++i) - progressive_paints_[i].painted_ = false; + for (auto& paint : progressive_paints_) + paint.painted_ = false; } void PDFiumEngine::Paint(const pp::Rect& rect, @@ -1139,11 +1137,11 @@ void PDFiumEngine::OnPendingRequestComplete() { // need to run the code below in that case. bool update_pages = false; std::vector<int> still_pending; - for (size_t i = 0; i < pending_pages_.size(); ++i) { - if (CheckPageAvailable(pending_pages_[i], &still_pending)) { + for (int pending_page : pending_pages_) { + if (CheckPageAvailable(pending_page, &still_pending)) { update_pages = true; - if (IsPageVisible(pending_pages_[i])) - client_->Invalidate(GetPageScreenRect(pending_pages_[i])); + if (IsPageVisible(pending_page)) + client_->Invalidate(GetPageScreenRect(pending_page)); } } pending_pages_.swap(still_pending); @@ -1280,8 +1278,8 @@ bool PDFiumEngine::HandleEvent(const pp::InputEvent& event) { DCHECK(defer_page_unload_); defer_page_unload_ = false; - for (size_t i = 0; i < deferred_page_unloads_.size(); ++i) - pages_[deferred_page_unloads_[i]]->Unload(); + for (int page_index : deferred_page_unloads_) + pages_[page_index]->Unload(); deferred_page_unloads_.clear(); return rv; } @@ -1389,8 +1387,7 @@ pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( // Collect pages to print and sizes of source pages. std::vector<uint32_t> page_numbers = GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count); - for (size_t i = 0; i < page_numbers.size(); ++i) { - uint32_t page_number = page_numbers[i]; + for (uint32_t page_number : page_numbers) { FPDF_PAGE pdf_page = FPDF_LoadPage(doc_, page_number); double source_page_width = FPDF_GetPageWidth(pdf_page); double source_page_height = FPDF_GetPageHeight(pdf_page); @@ -1503,10 +1500,9 @@ pp::Buffer_Dev PDFiumEngine::PrintPagesAsPDF( std::vector<uint32_t> page_numbers = GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count); - for (size_t i = 0; i < page_numbers.size(); ++i) { - uint32_t page_number = page_numbers[i]; + for (uint32_t page_number : page_numbers) { pages_[page_number]->GetPage(); - if (!IsPageVisible(page_numbers[i])) + if (!IsPageVisible(page_number)) pages_[page_number]->Unload(); } @@ -1569,9 +1565,9 @@ PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::Point& point, pp::Point point_in_page( static_cast<int>((point.x() + position_.x()) / current_zoom_), static_cast<int>((point.y() + position_.y()) / current_zoom_)); - for (size_t i = 0; i < visible_pages_.size(); ++i) { - if (pages_[visible_pages_[i]]->rect().Contains(point_in_page)) { - page = visible_pages_[i]; + for (int visible_page : visible_pages_) { + if (pages_[visible_page]->rect().Contains(point_in_page)) { + page = visible_page; break; } } @@ -1580,8 +1576,8 @@ PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::Point& point, // If the page hasn't finished rendering, calling into the page sometimes // leads to hangs. - for (size_t i = 0; i < progressive_paints_.size(); ++i) { - if (progressive_paints_[i].page_index == page) + for (const auto& paint : progressive_paints_) { + if (paint.page_index == page) return PDFiumPage::NONSELECTABLE_AREA; } @@ -1592,14 +1588,14 @@ PDFiumPage::Area PDFiumEngine::GetCharIndex(const pp::Point& point, bool PDFiumEngine::OnMouseDown(const pp::MouseInputEvent& event) { if (event.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_RIGHT) { - if (!selection_.size()) + if (selection_.empty()) return false; std::vector<pp::Rect> selection_rect_vector; GetAllScreenRectsUnion(&selection_, GetVisibleRect().point(), &selection_rect_vector); pp::Point point = event.GetPosition(); - for (size_t i = 0; i < selection_rect_vector.size(); ++i) { - if (selection_rect_vector[i].Contains(point.x(), point.y())) + for (const auto& rect : selection_rect_vector) { + if (rect.Contains(point.x(), point.y())) return false; } SelectionChangeInvalidator selection_invalidator(this); @@ -1815,7 +1811,7 @@ bool PDFiumEngine::OnMouseMove(const pp::MouseInputEvent& event) { } } - if (selection_.size() == 0) + if (selection_.empty()) return false; int last = selection_.size() - 1; @@ -2071,15 +2067,15 @@ void PDFiumEngine::SearchUsingICU(const base::string16& term, std::vector<PDFEngine::Client::SearchStringResult> results; client_->SearchString( page_text.c_str(), term.c_str(), case_sensitive, &results); - for (size_t i = 0; i < results.size(); ++i) { + for (const auto& result : results) { // Need to map the indexes from the page text, which may have generated // characters like space etc, to character indices from the page. - int temp_start = results[i].start_index + character_to_start_searching_from; + int temp_start = result.start_index + character_to_start_searching_from; int start = FPDFText_GetCharIndexFromTextIndex( pages_[current_page]->GetTextPage(), temp_start); int end = FPDFText_GetCharIndexFromTextIndex( pages_[current_page]->GetTextPage(), - temp_start + results[i].length); + temp_start + result.length); AddFindResult(PDFiumRange(pages_[current_page], start, end - start)); } } @@ -2150,8 +2146,8 @@ bool PDFiumEngine::SelectFindResult(bool forward) { std::vector<pp::Rect> rects; rects = find_results_[current_find_index_.GetIndex()].GetScreenRects( pp::Point(), 1.0, current_rotation_); - for (size_t i = 0; i < rects.size(); ++i) - bounding_rect = bounding_rect.Union(rects[i]); + for (const auto& rect : rects) + bounding_rect = bounding_rect.Union(rect); if (!visible_rect.Contains(bounding_rect)) { pp::Point center = bounding_rect.CenterPoint(); // Make the page centered. @@ -2193,14 +2189,13 @@ void PDFiumEngine::StopFind() { void PDFiumEngine::GetAllScreenRectsUnion(std::vector<PDFiumRange>* rect_range, const pp::Point& offset_point, std::vector<pp::Rect>* rect_vector) { - for (std::vector<PDFiumRange>::iterator it = rect_range->begin(); - it != rect_range->end(); ++it) { - pp::Rect rect; + for (auto& range : *rect_range) { + pp::Rect result_rect; std::vector<pp::Rect> rects = - it->GetScreenRects(offset_point, current_zoom_, current_rotation_); - for (size_t j = 0; j < rects.size(); ++j) - rect = rect.Union(rects[j]); - rect_vector->push_back(rect); + range.GetScreenRects(offset_point, current_zoom_, current_rotation_); + for (const auto& rect : rects) + result_rect = result_rect.Union(rect); + rect_vector->push_back(result_rect); } } @@ -2307,11 +2302,10 @@ void PDFiumEngine::SelectAll() { SelectionChangeInvalidator selection_invalidator(this); selection_.clear(); - for (size_t i = 0; i < pages_.size(); ++i) - if (pages_[i]->available()) { - selection_.push_back(PDFiumRange(pages_[i], 0, - pages_[i]->GetCharCount())); - } + for (const auto& page : pages_) { + if (page->available()) + selection_.push_back(PDFiumRange(page, 0, page->GetCharCount())); + } } int PDFiumEngine::GetNumberOfPages() { @@ -2502,7 +2496,7 @@ bool PDFiumEngine::TryLoadingDoc(bool with_password, if (doc_) return true; - const char* password_cstr = NULL; + const char* password_cstr = nullptr; if (with_password) { password_cstr = password.c_str(); password_tries_remaining_--; @@ -2515,7 +2509,7 @@ bool PDFiumEngine::TryLoadingDoc(bool with_password, if (!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD) *needs_password = true; - return doc_ != NULL; + return !!doc_; } void PDFiumEngine::GetPasswordAndLoad() { @@ -2701,12 +2695,7 @@ void PDFiumEngine::CalculateVisiblePages() { } bool PDFiumEngine::IsPageVisible(int index) const { - for (size_t i = 0; i < visible_pages_.size(); ++i) { - if (visible_pages_[i] == index) - return true; - } - - return false; + return ContainsValue(visible_pages_, index); } bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) { @@ -2835,9 +2824,9 @@ void PDFiumEngine::FinishPaint(int progressive_index, } void PDFiumEngine::CancelPaints() { - for (size_t i = 0; i < progressive_paints_.size(); ++i) { - FPDF_RenderPage_Close(pages_[progressive_paints_[i].page_index]->GetPage()); - FPDFBitmap_Destroy(progressive_paints_[i].bitmap); + for (const auto& paint : progressive_paints_) { + FPDF_RenderPage_Close(pages_[paint.page_index]->GetPage()); + FPDFBitmap_Destroy(paint.bitmap); } progressive_paints_.clear(); } @@ -2933,13 +2922,14 @@ void PDFiumEngine::DrawSelections(int progressive_index, std::vector<pp::Rect> highlighted_rects; pp::Rect visible_rect = GetVisibleRect(); - for (size_t k = 0; k < selection_.size(); ++k) { - if (selection_[k].page_index() != page_index) + for (auto& range : selection_) { + if (range.page_index() != page_index) continue; - std::vector<pp::Rect> rects = selection_[k].GetScreenRects( + + std::vector<pp::Rect> rects = range.GetScreenRects( visible_rect.point(), current_zoom_, current_rotation_); - for (size_t j = 0; j < rects.size(); ++j) { - pp::Rect visible_selection = rects[j].Intersect(dirty_in_screen); + for (const auto& rect : rects) { + pp::Rect visible_selection = rect.Intersect(dirty_in_screen); if (visible_selection.IsEmpty()) continue; @@ -2949,8 +2939,8 @@ void PDFiumEngine::DrawSelections(int progressive_index, } } - for (size_t k = 0; k < form_highlights_.size(); ++k) { - pp::Rect visible_selection = form_highlights_[k].Intersect(dirty_in_screen); + for (const auto& highlight : form_highlights_) { + pp::Rect visible_selection = highlight.Intersect(dirty_in_screen); if (visible_selection.IsEmpty()) continue; @@ -3060,8 +3050,8 @@ void PDFiumEngine::Highlight(void* buffer, return; pp::Rect new_rect = rect; - for (size_t i = 0; i < highlighted_rects->size(); ++i) - new_rect = new_rect.Subtract((*highlighted_rects)[i]); + for (const auto& highlighted : *highlighted_rects) + new_rect = new_rect.Subtract(highlighted); highlighted_rects->push_back(new_rect); int l = new_rect.x(); @@ -3089,45 +3079,43 @@ PDFiumEngine::SelectionChangeInvalidator::SelectionChangeInvalidator( PDFiumEngine::SelectionChangeInvalidator::~SelectionChangeInvalidator() { // Offset the old selections if the document scrolled since we recorded them. pp::Point offset = previous_origin_ - engine_->GetVisibleRect().point(); - for (size_t i = 0; i < old_selections_.size(); ++i) - old_selections_[i].Offset(offset); + for (auto& old_selection : old_selections_) + old_selection.Offset(offset); std::vector<pp::Rect> new_selections; GetVisibleSelectionsScreenRects(&new_selections); - for (size_t i = 0; i < new_selections.size(); ++i) { - for (size_t j = 0; j < old_selections_.size(); ++j) { - if (!old_selections_[j].IsEmpty() && - new_selections[i] == old_selections_[j]) { + for (auto& new_selection : new_selections) { + for (auto& old_selection : old_selections_) { + if (!old_selection.IsEmpty() && new_selection == old_selection) { // Rectangle was selected before and after, so no need to invalidate it. // Mark the rectangles by setting them to empty. - new_selections[i] = old_selections_[j] = pp::Rect(); + new_selection = old_selection = pp::Rect(); break; } } } - for (size_t i = 0; i < old_selections_.size(); ++i) { - if (!old_selections_[i].IsEmpty()) - engine_->client_->Invalidate(old_selections_[i]); + for (const auto& old_selection : old_selections_) { + if (!old_selection.IsEmpty()) + engine_->client_->Invalidate(old_selection); } - for (size_t i = 0; i < new_selections.size(); ++i) { - if (!new_selections[i].IsEmpty()) - engine_->client_->Invalidate(new_selections[i]); + for (const auto& new_selection : new_selections) { + if (!new_selection.IsEmpty()) + engine_->client_->Invalidate(new_selection); } engine_->OnSelectionChanged(); } -void -PDFiumEngine::SelectionChangeInvalidator::GetVisibleSelectionsScreenRects( +void PDFiumEngine::SelectionChangeInvalidator::GetVisibleSelectionsScreenRects( std::vector<pp::Rect>* rects) { pp::Rect visible_rect = engine_->GetVisibleRect(); - for (size_t i = 0; i < engine_->selection_.size(); ++i) { - int page_index = engine_->selection_[i].page_index(); + for (auto& range : engine_->selection_) { + int page_index = range.page_index(); if (!engine_->IsPageVisible(page_index)) continue; // This selection is on a page that's not currently visible. std::vector<pp::Rect> selection_rects = - engine_->selection_[i].GetScreenRects( + range.GetScreenRects( visible_rect.point(), engine_->current_zoom_, engine_->current_rotation_); @@ -3158,14 +3146,16 @@ void PDFiumEngine::MouseDownState::Reset() { bool PDFiumEngine::MouseDownState::Matches( const PDFiumPage::Area& area, const PDFiumPage::LinkTarget& target) const { - if (area_ == area) { - if (area == PDFiumPage::WEBLINK_AREA) - return target_.url == target.url; - if (area == PDFiumPage::DOCLINK_AREA) - return target_.page == target.page; - return true; - } - return false; + if (area_ != area) + return false; + + if (area == PDFiumPage::WEBLINK_AREA) + return target_.url == target.url; + + if (area == PDFiumPage::DOCLINK_AREA) + return target_.page == target.page; + + return true; } PDFiumEngine::FindTextIndex::FindTextIndex() @@ -3211,9 +3201,9 @@ void PDFiumEngine::DeviceToPage(int page_index, } int PDFiumEngine::GetVisiblePageIndex(FPDF_PAGE page) { - for (size_t i = 0; i < visible_pages_.size(); ++i) { - if (pages_[visible_pages_[i]]->GetPage() == page) - return visible_pages_[i]; + for (int page_index : visible_pages_) { + if (pages_[page_index]->GetPage() == page) + return page_index; } return -1; } diff --git a/pdf/pdfium/pdfium_page.cc b/pdf/pdfium/pdfium_page.cc index 5e0b192..95a8fba 100644 --- a/pdf/pdfium/pdfium_page.cc +++ b/pdf/pdfium/pdfium_page.cc @@ -175,7 +175,7 @@ base::Value* PDFiumPage::GetTextBoxAsValue(double page_height, pp::Rect rect( PageToScreen(pp::Point(), 1.0, left, top, right, bottom, rotation)); GetLinks(rect, &targets); - area = targets.size() == 0 ? TEXT_AREA : WEBLINK_AREA; + area = targets.empty() ? TEXT_AREA : WEBLINK_AREA; } int char_index = FPDFText_GetCharIndexAtPos(GetTextPage(), left, top, @@ -198,17 +198,17 @@ base::Value* PDFiumPage::GetTextBoxAsValue(double page_height, text_nodes->Append(CreateURLNode(text_utf8, targets[0].url)); } else if (area == WEBLINK_AREA && !link) { size_t start = 0; - for (size_t i = 0; i < targets.size(); ++i) { + for (const auto& target : targets) { // If there is an extra NULL character at end, find() will not return any // matches. There should not be any though. - if (!targets[i].url.empty()) - DCHECK(targets[i].url[targets[i].url.size() - 1] != '\0'); + if (!target.url.empty()) + DCHECK_NE(target.url.back(), '\0'); // PDFium may change the case of generated links. - std::string lowerCaseURL = base::ToLowerASCII(targets[i].url); + std::string lowerCaseURL = base::ToLowerASCII(target.url); std::string lowerCaseText = base::ToLowerASCII(text_utf8); size_t pos = lowerCaseText.find(lowerCaseURL, start); - size_t length = targets[i].url.size(); + size_t length = target.url.size(); if (pos == std::string::npos) { // Check if the link is a "mailto:" URL if (lowerCaseURL.compare(0, 7, "mailto:") == 0) { @@ -223,15 +223,15 @@ base::Value* PDFiumPage::GetTextBoxAsValue(double page_height, } std::string before_text = text_utf8.substr(start, pos - start); - if (before_text.size() > 0) + if (!before_text.empty()) text_nodes->Append(CreateTextNode(before_text)); std::string link_text = text_utf8.substr(pos, length); - text_nodes->Append(CreateURLNode(link_text, targets[i].url)); + text_nodes->Append(CreateURLNode(link_text, target.url)); start = pos + length; } std::string before_text = text_utf8.substr(start); - if (before_text.size() > 0) + if (!before_text.empty()) text_nodes->Append(CreateTextNode(before_text)); } else { text_nodes->Append(CreateTextNode(text_utf8)); @@ -328,9 +328,9 @@ int PDFiumPage::GetCharCount() { } PDFiumPage::Area PDFiumPage::GetLinkTarget( - FPDF_LINK link, PDFiumPage::LinkTarget* target) { + FPDF_LINK link, PDFiumPage::LinkTarget* target) const { FPDF_DEST dest = FPDFLink_GetDest(engine_->doc(), link); - if (dest != NULL) + if (dest) return GetDestinationTarget(dest, target); FPDF_ACTION action = FPDFLink_GetAction(link); @@ -368,11 +368,9 @@ PDFiumPage::Area PDFiumPage::GetLinkTarget( } PDFiumPage::Area PDFiumPage::GetDestinationTarget( - FPDF_DEST destination, PDFiumPage::LinkTarget* target) { - int page_index = FPDFDest_GetPageIndex(engine_->doc(), destination); - if (target) { - target->page = page_index; - } + FPDF_DEST destination, PDFiumPage::LinkTarget* target) const { + if (target) + target->page = FPDFDest_GetPageIndex(engine_->doc(), destination); return DOCLINK_AREA; } @@ -390,8 +388,8 @@ int PDFiumPage::GetLink(int char_index, PDFiumPage::LinkTarget* target) { pp::Point origin( PageToScreen(pp::Point(), 1.0, left, top, right, bottom, 0).point()); for (size_t i = 0; i < links_.size(); ++i) { - for (size_t j = 0; j < links_[i].rects.size(); ++j) { - if (links_[i].rects[j].Contains(origin)) { + for (const auto& rect : links_[i].rects) { + if (rect.Contains(origin)) { if (target) target->url = links_[i].url; return i; @@ -403,16 +401,15 @@ int PDFiumPage::GetLink(int char_index, PDFiumPage::LinkTarget* target) { std::vector<int> PDFiumPage::GetLinks(pp::Rect text_area, std::vector<LinkTarget>* targets) { + std::vector<int> links; if (!available_) - return std::vector<int>(); + return links; CalculateLinks(); - std::vector<int> links; - for (size_t i = 0; i < links_.size(); ++i) { - for (size_t j = 0; j < links_[i].rects.size(); ++j) { - if (links_[i].rects[j].Intersects(text_area)) { + for (const auto& rect : links_[i].rects) { + if (rect.Intersects(text_area)) { if (targets) { LinkTarget target; target.url = links_[i].url; @@ -487,7 +484,7 @@ pp::Rect PDFiumPage::PageToScreen(const pp::Point& offset, double top, double right, double bottom, - int rotation) { + int rotation) const { if (!available_) return pp::Rect(); diff --git a/pdf/pdfium/pdfium_page.h b/pdf/pdfium/pdfium_page.h index eee57e0..da30504 100644 --- a/pdf/pdfium/pdfium_page.h +++ b/pdf/pdfium/pdfium_page.h @@ -30,6 +30,7 @@ class PDFiumPage { const pp::Rect& r, bool available); ~PDFiumPage(); + // Unloads the PDFium data for this page from memory. void Unload(); // Gets the FPDF_PAGE for this page, loading and parsing it if necessary. @@ -79,7 +80,7 @@ class PDFiumPage { double top, double right, double bottom, - int rotation); + int rotation) const; int index() const { return index_; } pp::Rect rect() const { return rect_; } @@ -102,9 +103,9 @@ class PDFiumPage { void CalculateLinks(); // Returns link type and target associated with a link. Returns // NONSELECTABLE_AREA if link detection failed. - Area GetLinkTarget(FPDF_LINK link, LinkTarget* target); + Area GetLinkTarget(FPDF_LINK link, LinkTarget* target) const; // Returns target associated with a destination. - Area GetDestinationTarget(FPDF_DEST destination, LinkTarget* target); + Area GetDestinationTarget(FPDF_DEST destination, LinkTarget* target) const; // Returns the text in the supplied box as a Value Node base::Value* GetTextBoxAsValue(double page_height, double left, double top, double right, double bottom, int rotation); diff --git a/pdf/pdfium/pdfium_range.cc b/pdf/pdfium/pdfium_range.cc index b0474fc..3b8371f 100644 --- a/pdf/pdfium/pdfium_range.cc +++ b/pdf/pdfium/pdfium_range.cc @@ -57,7 +57,7 @@ std::vector<pp::Rect> PDFiumRange::GetScreenRects(const pp::Point& offset, return cached_screen_rects_; } -base::string16 PDFiumRange::GetText() { +base::string16 PDFiumRange::GetText() const { int index = char_index_; int count = char_count_; base::string16 rv; diff --git a/pdf/pdfium/pdfium_range.h b/pdf/pdfium/pdfium_range.h index ed8daf6..a0f9ea1 100644 --- a/pdf/pdfium/pdfium_range.h +++ b/pdf/pdfium/pdfium_range.h @@ -34,13 +34,13 @@ class PDFiumRange { int rotation); // Gets the string of characters in this range. - base::string16 GetText(); + base::string16 GetText() const; private: PDFiumPage* page_; // Index of first character. int char_index_; - // How many characters are part of this range (negative if backwards). + // How many characters are part of this range (negative if backwards). int char_count_; // Cache of ScreenRect, and the associated variables used when caching it. |