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

Side by Side Diff: components/pdf/renderer/pdf_accessibility_tree.cc

Issue 2100753003: Enable PDF accessibility when RenderFrame's accessibility mode changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Work around cross-platform whitespace difference in print preview test Created 4 years, 5 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <algorithm> 5 #include <algorithm>
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversion_utils.h" 9 #include "base/strings/utf_string_conversion_utils.h"
10 #include "components/pdf/renderer/pdf_accessibility_tree.h" 10 #include "components/pdf/renderer/pdf_accessibility_tree.h"
11 #include "content/public/renderer/render_accessibility.h" 11 #include "content/public/renderer/render_accessibility.h"
12 #include "content/public/renderer/render_frame.h" 12 #include "content/public/renderer/render_frame.h"
13 #include "content/public/renderer/render_view.h" 13 #include "content/public/renderer/render_view.h"
14 #include "content/public/renderer/renderer_ppapi_host.h"
14 #include "grit/components_strings.h" 15 #include "grit/components_strings.h"
15 #include "ui/base/l10n/l10n_util.h" 16 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/gfx/geometry/rect_conversions.h" 17 #include "ui/gfx/geometry/rect_conversions.h"
17 18
18 namespace pdf { 19 namespace pdf {
19 20
20 namespace { 21 namespace {
21 22
22 // Don't try to apply font size thresholds to automatically identify headings 23 // Don't try to apply font size thresholds to automatically identify headings
23 // if the median font size is not at least this many points. 24 // if the median font size is not at least this many points.
(...skipping 11 matching lines...) Expand all
35 // page for that line spacing to be considered a paragraph break. 36 // page for that line spacing to be considered a paragraph break.
36 const double kParagraphLineSpacingRatio = 1.2; 37 const double kParagraphLineSpacingRatio = 1.2;
37 38
38 gfx::RectF ToGfxRectF(const PP_FloatRect& r) { 39 gfx::RectF ToGfxRectF(const PP_FloatRect& r) {
39 return gfx::RectF(r.point.x, r.point.y, r.size.width, r.size.height); 40 return gfx::RectF(r.point.x, r.point.y, r.size.width, r.size.height);
40 } 41 }
41 42
42 } 43 }
43 44
44 PdfAccessibilityTree::PdfAccessibilityTree( 45 PdfAccessibilityTree::PdfAccessibilityTree(
45 content::RenderView* render_view) 46 content::RendererPpapiHost* host,
46 : render_view_(render_view), 47 PP_Instance instance)
47 render_accessibility_(nullptr), 48 : host_(host),
49 instance_(instance),
48 zoom_(1.0) { 50 zoom_(1.0) {
49 content::RenderFrame* render_frame = render_view->GetMainRenderFrame();
50 render_accessibility_ = render_frame->GetRenderAccessibility();
51 } 51 }
52 52
53 PdfAccessibilityTree::~PdfAccessibilityTree() { 53 PdfAccessibilityTree::~PdfAccessibilityTree() {
54 } 54 }
55 55
56 void PdfAccessibilityTree::SetAccessibilityViewportInfo( 56 void PdfAccessibilityTree::SetAccessibilityViewportInfo(
57 const PP_PrivateAccessibilityViewportInfo& viewport_info) { 57 const PP_PrivateAccessibilityViewportInfo& viewport_info) {
58 zoom_ = viewport_info.zoom; 58 zoom_ = viewport_info.zoom;
59 CHECK_GT(zoom_, 0); 59 CHECK_GT(zoom_, 0);
60 scroll_ = ToVector2dF(viewport_info.scroll); 60 scroll_ = ToVector2dF(viewport_info.scroll);
61 scroll_.Scale(1.0 / zoom_); 61 scroll_.Scale(1.0 / zoom_);
62 offset_ = ToVector2dF(viewport_info.offset); 62 offset_ = ToVector2dF(viewport_info.offset);
63 offset_.Scale(1.0 / zoom_); 63 offset_.Scale(1.0 / zoom_);
64 } 64 }
65 65
66 void PdfAccessibilityTree::SetAccessibilityDocInfo( 66 void PdfAccessibilityTree::SetAccessibilityDocInfo(
67 const PP_PrivateAccessibilityDocInfo& doc_info) { 67 const PP_PrivateAccessibilityDocInfo& doc_info) {
68 doc_info_ = doc_info; 68 doc_info_ = doc_info;
69 doc_node_ = CreateNode(ui::AX_ROLE_GROUP); 69 doc_node_ = CreateNode(ui::AX_ROLE_GROUP);
70 } 70 }
71 71
72 void PdfAccessibilityTree::SetAccessibilityPageInfo( 72 void PdfAccessibilityTree::SetAccessibilityPageInfo(
73 const PP_PrivateAccessibilityPageInfo& page_info, 73 const PP_PrivateAccessibilityPageInfo& page_info,
74 const std::vector<PP_PrivateAccessibilityTextRunInfo>& text_runs, 74 const std::vector<PP_PrivateAccessibilityTextRunInfo>& text_runs,
75 const std::vector<PP_PrivateAccessibilityCharInfo>& chars) { 75 const std::vector<PP_PrivateAccessibilityCharInfo>& chars) {
76 content::RenderAccessibility* render_accessibility = GetRenderAccessibility();
77 if (!render_accessibility)
78 return;
79
76 uint32_t page_index = page_info.page_index; 80 uint32_t page_index = page_info.page_index;
77 CHECK_GE(page_index, 0U); 81 CHECK_GE(page_index, 0U);
78 CHECK_LT(page_index, doc_info_.page_count); 82 CHECK_LT(page_index, doc_info_.page_count);
79 83
80 ui::AXNodeData* page_node = CreateNode(ui::AX_ROLE_REGION); 84 ui::AXNodeData* page_node = CreateNode(ui::AX_ROLE_REGION);
81 page_node->AddStringAttribute( 85 page_node->AddStringAttribute(
82 ui::AX_ATTR_NAME, 86 ui::AX_ATTR_NAME,
83 l10n_util::GetPluralStringFUTF8( 87 l10n_util::GetPluralStringFUTF8(
84 IDS_PDF_PAGE_INDEX, page_index + 1)); 88 IDS_PDF_PAGE_INDEX, page_index + 1));
85 89
(...skipping 26 matching lines...) Expand all
112 116
113 // If we don't have a paragraph, create one. 117 // If we don't have a paragraph, create one.
114 if (!para_node) { 118 if (!para_node) {
115 para_node = CreateNode(ui::AX_ROLE_PARAGRAPH); 119 para_node = CreateNode(ui::AX_ROLE_PARAGRAPH);
116 page_node->child_ids.push_back(para_node->id); 120 page_node->child_ids.push_back(para_node->id);
117 121
118 if (heading_font_size_threshold > 0 && 122 if (heading_font_size_threshold > 0 &&
119 text_run.font_size > heading_font_size_threshold) { 123 text_run.font_size > heading_font_size_threshold) {
120 para_node->role = ui::AX_ROLE_HEADING; 124 para_node->role = ui::AX_ROLE_HEADING;
121 para_node->AddIntAttribute(ui::AX_ATTR_HIERARCHICAL_LEVEL, 2); 125 para_node->AddIntAttribute(ui::AX_ATTR_HIERARCHICAL_LEVEL, 2);
126 para_node->AddStringAttribute(ui::AX_ATTR_HTML_TAG, "h2");
122 } 127 }
123 128
124 // This node is for the text inside the paragraph, it includes 129 // This node is for the text inside the paragraph, it includes
125 // the text of all of the text runs. 130 // the text of all of the text runs.
126 static_text_node = CreateNode(ui::AX_ROLE_STATIC_TEXT); 131 static_text_node = CreateNode(ui::AX_ROLE_STATIC_TEXT);
127 para_node->child_ids.push_back(static_text_node->id); 132 para_node->child_ids.push_back(static_text_node->id);
128 } 133 }
129 134
130 // Add this text run to the current static text node. 135 // Add this text run to the current static text node.
131 ui::AXNodeData* inline_text_box_node = CreateNode( 136 ui::AXNodeData* inline_text_box_node = CreateNode(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 Finish(); 169 Finish();
165 } 170 }
166 171
167 void PdfAccessibilityTree::Finish() { 172 void PdfAccessibilityTree::Finish() {
168 ui::AXTreeUpdate update; 173 ui::AXTreeUpdate update;
169 update.root_id = doc_node_->id; 174 update.root_id = doc_node_->id;
170 for (const auto& node : nodes_) 175 for (const auto& node : nodes_)
171 update.nodes.push_back(*node); 176 update.nodes.push_back(*node);
172 177
173 CHECK(tree_.Unserialize(update)) << update.ToString() << tree_.error(); 178 CHECK(tree_.Unserialize(update)) << update.ToString() << tree_.error();
174 render_accessibility_->SetPdfTreeSource(this); 179 content::RenderAccessibility* render_accessibility = GetRenderAccessibility();
180 if (render_accessibility)
181 render_accessibility->SetPdfTreeSource(this);
175 } 182 }
176 183
177 void PdfAccessibilityTree::ComputeParagraphAndHeadingThresholds( 184 void PdfAccessibilityTree::ComputeParagraphAndHeadingThresholds(
178 const std::vector<PP_PrivateAccessibilityTextRunInfo>& text_runs, 185 const std::vector<PP_PrivateAccessibilityTextRunInfo>& text_runs,
179 double* out_heading_font_size_threshold, 186 double* out_heading_font_size_threshold,
180 double* out_line_spacing_threshold) { 187 double* out_line_spacing_threshold) {
181 // Scan over the font sizes and line spacing within this page and 188 // Scan over the font sizes and line spacing within this page and
182 // set heuristic thresholds so that text larger than the median font 189 // set heuristic thresholds so that text larger than the median font
183 // size can be marked as a heading, and spacing larger than the median 190 // size can be marked as a heading, and spacing larger than the median
184 // line spacing can be a paragraph break. 191 // line spacing can be a paragraph break.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 245
239 gfx::Vector2dF PdfAccessibilityTree::ToVector2dF(const PP_Point& p) { 246 gfx::Vector2dF PdfAccessibilityTree::ToVector2dF(const PP_Point& p) {
240 return gfx::Vector2dF(p.x, p.y); 247 return gfx::Vector2dF(p.x, p.y);
241 } 248 }
242 249
243 gfx::RectF PdfAccessibilityTree::ToRectF(const PP_Rect& r) { 250 gfx::RectF PdfAccessibilityTree::ToRectF(const PP_Rect& r) {
244 return gfx::RectF(r.point.x, r.point.y, r.size.width, r.size.height); 251 return gfx::RectF(r.point.x, r.point.y, r.size.width, r.size.height);
245 } 252 }
246 253
247 ui::AXNodeData* PdfAccessibilityTree::CreateNode(ui::AXRole role) { 254 ui::AXNodeData* PdfAccessibilityTree::CreateNode(ui::AXRole role) {
255 content::RenderAccessibility* render_accessibility = GetRenderAccessibility();
256 DCHECK(render_accessibility);
257
248 ui::AXNodeData* node = new ui::AXNodeData(); 258 ui::AXNodeData* node = new ui::AXNodeData();
249 node->id = render_accessibility_->GenerateAXID(); 259 node->id = render_accessibility->GenerateAXID();
250 node->role = role; 260 node->role = role;
251 node->state = 1 << ui::AX_STATE_ENABLED | 1 << ui::AX_STATE_READ_ONLY; 261 node->state = 1 << ui::AX_STATE_ENABLED | 1 << ui::AX_STATE_READ_ONLY;
252 nodes_.push_back(base::WrapUnique(node)); 262 nodes_.push_back(base::WrapUnique(node));
253 return node; 263 return node;
254 } 264 }
255 265
256 float PdfAccessibilityTree::GetDeviceScaleFactor() const { 266 float PdfAccessibilityTree::GetDeviceScaleFactor() const {
257 return render_view_->GetDeviceScaleFactor(); 267 content::RenderFrame* render_frame =
268 host_->GetRenderFrameForInstance(instance_);
269 DCHECK(render_frame);
270 return render_frame->GetRenderView()->GetDeviceScaleFactor();
271 }
272
273 content::RenderAccessibility* PdfAccessibilityTree::GetRenderAccessibility() {
274 content::RenderFrame* render_frame =
275 host_->GetRenderFrameForInstance(instance_);
276 return render_frame ? render_frame->GetRenderAccessibility() : nullptr;
258 } 277 }
259 278
260 // 279 //
261 // AXTreeSource implementation. 280 // AXTreeSource implementation.
262 // 281 //
263 282
264 bool PdfAccessibilityTree::GetTreeData(ui::AXTreeData* tree_data) const { 283 bool PdfAccessibilityTree::GetTreeData(ui::AXTreeData* tree_data) const {
265 return false; 284 return false;
266 } 285 }
267 286
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 const ui::AXNode* PdfAccessibilityTree::GetNull() const { 319 const ui::AXNode* PdfAccessibilityTree::GetNull() const {
301 return nullptr; 320 return nullptr;
302 } 321 }
303 322
304 void PdfAccessibilityTree::SerializeNode( 323 void PdfAccessibilityTree::SerializeNode(
305 const ui::AXNode* node, ui::AXNodeData* out_data) const { 324 const ui::AXNode* node, ui::AXNodeData* out_data) const {
306 *out_data = node->data(); 325 *out_data = node->data();
307 } 326 }
308 327
309 } // namespace pdf 328 } // namespace pdf
OLDNEW
« no previous file with comments | « components/pdf/renderer/pdf_accessibility_tree.h ('k') | components/pdf/renderer/pepper_pdf_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698