Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Ankit Kumar 🌪️ | 9c1ef786 | 2021-02-12 08:53:13 | [diff] [blame] | 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/accessibility_helper.h" |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/numerics/safe_math.h" |
Ankit Kumar 🌪️ | 9c1ef786 | 2021-02-12 08:53:13 | [diff] [blame] | 12 | #include "pdf/accessibility_structs.h" |
Anton Bikineev | 11eb7ff | 2021-05-15 18:21:28 | [diff] [blame] | 13 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Ankit Kumar 🌪️ | 9c1ef786 | 2021-02-12 08:53:13 | [diff] [blame] | 14 | |
| 15 | namespace chrome_pdf { |
| 16 | |
Ankit Kumar 🌪️ | 9c1ef786 | 2021-02-12 08:53:13 | [diff] [blame] | 17 | bool IsCharWithinTextRun(const AccessibilityTextRunInfo& text_run, |
| 18 | uint32_t text_run_start_char_index, |
| 19 | uint32_t char_index) { |
| 20 | return char_index >= text_run_start_char_index && |
| 21 | char_index - text_run_start_char_index < text_run.len; |
| 22 | } |
| 23 | |
Ankit Kumar 🌪️ | ab90ffc | 2021-02-12 10:41:33 | [diff] [blame] | 24 | // If a valid text run range is not found for the char range then return the |
| 25 | // fallback value. |
Ankit Kumar 🌪️ | 9c1ef786 | 2021-02-12 08:53:13 | [diff] [blame] | 26 | AccessibilityTextRunRangeInfo GetEnclosingTextRunRangeForCharRange( |
| 27 | const std::vector<AccessibilityTextRunInfo>& text_runs, |
| 28 | int start_char_index, |
| 29 | int char_count) { |
| 30 | // Initialize with fallback value. |
Ankit Kumar 🌪️ | ab90ffc | 2021-02-12 10:41:33 | [diff] [blame] | 31 | AccessibilityTextRunRangeInfo text_range = {text_runs.size(), 0}; |
Ankit Kumar 🌪️ | 9c1ef786 | 2021-02-12 08:53:13 | [diff] [blame] | 32 | if (start_char_index < 0 || char_count <= 0) |
| 33 | return text_range; |
| 34 | |
Ankit Kumar 🌪️ | ab90ffc | 2021-02-12 10:41:33 | [diff] [blame] | 35 | base::CheckedNumeric<uint32_t> checked_end_char_index = char_count - 1; |
| 36 | checked_end_char_index += start_char_index; |
Ankit Kumar 🌪️ | 9c1ef786 | 2021-02-12 08:53:13 | [diff] [blame] | 37 | if (!checked_end_char_index.IsValid()) |
| 38 | return text_range; |
| 39 | uint32_t end_char_index = checked_end_char_index.ValueOrDie(); |
| 40 | uint32_t current_char_index = 0; |
Peter Kasting | ad0607dc | 2021-06-13 15:24:24 | [diff] [blame] | 41 | absl::optional<size_t> start_text_run; |
Ankit Kumar 🌪️ | 9c1ef786 | 2021-02-12 08:53:13 | [diff] [blame] | 42 | for (size_t i = 0; i < text_runs.size(); ++i) { |
| 43 | if (!start_text_run.has_value() && |
| 44 | IsCharWithinTextRun(text_runs[i], current_char_index, |
| 45 | start_char_index)) { |
| 46 | start_text_run = i; |
| 47 | } |
| 48 | |
| 49 | if (start_text_run.has_value() && |
| 50 | IsCharWithinTextRun(text_runs[i], current_char_index, end_char_index)) { |
| 51 | text_range.index = start_text_run.value(); |
| 52 | text_range.count = i - text_range.index + 1; |
| 53 | break; |
| 54 | } |
| 55 | current_char_index += text_runs[i].len; |
| 56 | } |
| 57 | return text_range; |
| 58 | } |
| 59 | |
| 60 | } // namespace chrome_pdf |