OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 Loading... | |
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); | |
raymes
2016/07/14 03:15:54
Can we trust this to be non-null?
dmazzoni
2016/07/14 21:04:40
This is private. I did a null check for RenderAcce
| |
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); | |
raymes
2016/07/14 03:15:54
And this?
dmazzoni
2016/07/14 21:04:40
Same, private and will only be called after we kno
| |
270 return render_frame->GetRenderView()->GetDeviceScaleFactor(); | |
271 } | |
272 | |
273 content::RenderAccessibility* PdfAccessibilityTree::GetRenderAccessibility() { | |
274 content::RenderFrame* render_frame = | |
275 host_->GetRenderFrameForInstance(instance_); | |
276 if (!render_frame) | |
277 return nullptr; | |
278 return render_frame->GetRenderAccessibility(); | |
Lei Zhang
2016/07/15 19:26:21
ternary operator?
dmazzoni
2016/07/18 04:28:32
Done
| |
258 } | 279 } |
259 | 280 |
260 // | 281 // |
261 // AXTreeSource implementation. | 282 // AXTreeSource implementation. |
262 // | 283 // |
263 | 284 |
264 bool PdfAccessibilityTree::GetTreeData(ui::AXTreeData* tree_data) const { | 285 bool PdfAccessibilityTree::GetTreeData(ui::AXTreeData* tree_data) const { |
265 return false; | 286 return false; |
266 } | 287 } |
267 | 288 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 const ui::AXNode* PdfAccessibilityTree::GetNull() const { | 321 const ui::AXNode* PdfAccessibilityTree::GetNull() const { |
301 return nullptr; | 322 return nullptr; |
302 } | 323 } |
303 | 324 |
304 void PdfAccessibilityTree::SerializeNode( | 325 void PdfAccessibilityTree::SerializeNode( |
305 const ui::AXNode* node, ui::AXNodeData* out_data) const { | 326 const ui::AXNode* node, ui::AXNodeData* out_data) const { |
306 *out_data = node->data(); | 327 *out_data = node->data(); |
307 } | 328 } |
308 | 329 |
309 } // namespace pdf | 330 } // namespace pdf |
OLD | NEW |