Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_page.h" | 5 #include "pdf/pdfium/pdfium_page.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 const char kPageHeight[] = "height"; | 25 const char kPageHeight[] = "height"; |
| 26 const char kPageTextBox[] = "textBox"; | 26 const char kPageTextBox[] = "textBox"; |
| 27 const char kTextBoxLeft[] = "left"; | 27 const char kTextBoxLeft[] = "left"; |
| 28 const char kTextBoxTop[] = "top"; | 28 const char kTextBoxTop[] = "top"; |
| 29 const char kTextBoxWidth[] = "width"; | 29 const char kTextBoxWidth[] = "width"; |
| 30 const char kTextBoxHeight[] = "height"; | 30 const char kTextBoxHeight[] = "height"; |
| 31 const char kTextBoxFontSize[] = "fontSize"; | 31 const char kTextBoxFontSize[] = "fontSize"; |
| 32 const char kTextBoxNodes[] = "textNodes"; | 32 const char kTextBoxNodes[] = "textNodes"; |
| 33 const char kTextNodeType[] = "type"; | 33 const char kTextNodeType[] = "type"; |
| 34 const char kTextNodeText[] = "text"; | 34 const char kTextNodeText[] = "text"; |
| 35 const char kTextNodeURL[] = "url"; | |
| 36 const char kTextNodeTypeText[] = "text"; | 35 const char kTextNodeTypeText[] = "text"; |
| 37 const char kTextNodeTypeURL[] = "url"; | 36 |
| 38 const char kDocLinkURLPrefix[] = "#page"; | 37 pp::Rect PageRectToGViewRect(FPDF_PAGE page, const pp::Rect& input) { |
| 38 int output_width = FPDF_GetPageWidth(page); | |
| 39 int output_height = FPDF_GetPageHeight(page); | |
| 40 | |
| 41 int min_x; | |
| 42 int min_y; | |
| 43 int max_x; | |
| 44 int max_y; | |
| 45 FPDF_PageToDevice(page, 0, 0, output_width, output_height, 0, | |
| 46 input.x(), input.y(), &min_x, &min_y); | |
| 47 FPDF_PageToDevice(page, 0, 0, output_width, output_height, 0, | |
| 48 input.right(), input.bottom(), &max_x, &max_y); | |
| 49 | |
| 50 if (max_x < min_x) | |
| 51 std::swap(min_x, max_x); | |
| 52 if (max_y < min_y) | |
| 53 std::swap(min_y, max_y); | |
| 54 | |
| 55 pp::Rect output_rect(min_x, min_y, max_x - min_x, max_y - min_y); | |
| 56 output_rect.Intersect(pp::Rect(0, 0, output_width, output_height)); | |
| 57 return output_rect; | |
| 58 } | |
| 59 | |
| 60 pp::Rect GetCharRectInGViewCoords(FPDF_PAGE page, FPDF_TEXTPAGE text_page, | |
| 61 int index) { | |
| 62 double left, right, bottom, top; | |
| 63 FPDFText_GetCharBox(text_page, index, &left, &right, &bottom, &top); | |
| 64 if (right < left) | |
| 65 std::swap(left, right); | |
| 66 if (bottom < top) | |
| 67 std::swap(top, bottom); | |
| 68 pp::Rect page_coords(left, top, right - left, bottom - top); | |
| 69 return PageRectToGViewRect(page, page_coords); | |
| 70 } | |
| 71 | |
| 72 // This is the character PDFium inserts where a word is broken across lines. | |
| 73 const unsigned int kSoftHyphen = 0x02; | |
| 74 | |
| 75 // The following characters should all be recognized as Unicode newlines: | |
| 76 // LF: Line Feed, U+000A | |
| 77 // VT: Vertical Tab, U+000B | |
| 78 // FF: Form Feed, U+000C | |
| 79 // CR: Carriage Return, U+000D | |
| 80 // CR+LF: CR (U+000D) followed by LF (U+000A) | |
| 81 // NEL: Next Line, U+0085 | |
| 82 // LS: Line Separator, U+2028 | |
| 83 // PS: Paragraph Separator, U+2029. | |
| 84 // Source: http://en.wikipedia.org/wiki/Newline#Unicode . | |
| 85 const unsigned int kUnicodeNewlines[] = { | |
| 86 0xA, 0xB, 0xC, 0xD, 0X85, 0x2028, 0x2029 | |
| 87 }; | |
| 88 | |
| 89 bool IsSoftHyphen(unsigned int character) { | |
| 90 return kSoftHyphen == character; | |
| 91 } | |
| 92 | |
| 93 bool OverlapsOnYAxis(const pp::Rect &a, const pp::Rect& b) { | |
| 94 return !(a.IsEmpty() || b.IsEmpty() || | |
| 95 a.bottom() < b.y() || b.bottom() < a.y()); | |
| 96 } | |
| 97 | |
| 98 bool IsEol(unsigned int character) { | |
| 99 const unsigned int* first = kUnicodeNewlines; | |
| 100 const unsigned int* last = kUnicodeNewlines + arraysize(kUnicodeNewlines); | |
| 101 return std::find(first, last, character) != last; | |
| 102 } | |
| 39 | 103 |
| 40 } // namespace | 104 } // namespace |
| 41 | 105 |
| 42 namespace chrome_pdf { | 106 namespace chrome_pdf { |
| 43 | 107 |
| 44 PDFiumPage::PDFiumPage(PDFiumEngine* engine, | 108 PDFiumPage::PDFiumPage(PDFiumEngine* engine, |
| 45 int i, | 109 int i, |
| 46 const pp::Rect& r, | 110 const pp::Rect& r, |
| 47 bool available) | 111 bool available) |
| 48 : engine_(engine), | 112 : engine_(engine), |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 } | 187 } |
| 124 return text_page_; | 188 return text_page_; |
| 125 } | 189 } |
| 126 | 190 |
| 127 base::Value* PDFiumPage::GetAccessibleContentAsValue(int rotation) { | 191 base::Value* PDFiumPage::GetAccessibleContentAsValue(int rotation) { |
| 128 base::DictionaryValue* node = new base::DictionaryValue(); | 192 base::DictionaryValue* node = new base::DictionaryValue(); |
| 129 | 193 |
| 130 if (!available_) | 194 if (!available_) |
| 131 return node; | 195 return node; |
| 132 | 196 |
| 133 double width = FPDF_GetPageWidth(GetPage()); | 197 FPDF_PAGE page = GetPage(); |
| 134 double height = FPDF_GetPageHeight(GetPage()); | 198 FPDF_TEXTPAGE text_page = GetTextPage(); |
| 135 | 199 |
| 136 base::ListValue* text = new base::ListValue(); | 200 double width = FPDF_GetPageWidth(page); |
| 137 int box_count = FPDFText_CountRects(GetTextPage(), 0, GetCharCount()); | 201 double height = FPDF_GetPageHeight(page); |
| 138 for (int i = 0; i < box_count; i++) { | |
| 139 double left, top, right, bottom; | |
| 140 FPDFText_GetRect(GetTextPage(), i, &left, &top, &right, &bottom); | |
| 141 text->Append( | |
| 142 GetTextBoxAsValue(height, left, top, right, bottom, rotation)); | |
| 143 } | |
| 144 | 202 |
| 145 node->SetDouble(kPageWidth, width); | 203 node->SetDouble(kPageWidth, width); |
| 146 node->SetDouble(kPageHeight, height); | 204 node->SetDouble(kPageHeight, height); |
| 205 base::ListValue* text = new base::ListValue(); | |
| 206 | |
| 207 int chars_count = FPDFText_CountChars(text_page); | |
| 208 pp::Rect line_rect; | |
| 209 pp::Rect word_rect; | |
| 210 bool seen_literal_text_in_word = false; | |
| 211 | |
| 212 // Iterate over all of the chars on the page. Explicitly run the loop | |
| 213 // with |i == chars_count|, which is one past the last character, and | |
| 214 // pretend it's a newline character in order to ensure we always flush | |
| 215 // the last line. | |
| 216 base::string16 line; | |
| 217 for (int i = 0; i <= chars_count; i++) { | |
| 218 unsigned int character; | |
| 219 pp::Rect char_rect; | |
| 220 | |
| 221 if (i < chars_count) { | |
| 222 character = FPDFText_GetUnicode(text_page, i); | |
| 223 char_rect = GetCharRectInGViewCoords(page, text_page, i); | |
| 224 } else { | |
| 225 // Make the last character a newline so the last line isn't lost. | |
| 226 character = '\n'; | |
| 227 } | |
| 228 | |
| 229 // There are spurious STX chars appearing in place | |
| 230 // of ligatures. Apply a heuristic to check that some vertical displacement | |
| 231 // is involved before assuming they are line-breaks. | |
| 232 bool is_intraword_linebreak = false; | |
| 233 if (i < chars_count - 1 && IsSoftHyphen(character)) { | |
| 234 // check if the next char and this char are in different lines. | |
| 235 pp::Rect next_char_rect = GetCharRectInGViewCoords( | |
| 236 page, text_page, i + 1); | |
| 237 | |
| 238 // TODO(dmazzoni): this assumes horizontal text. | |
| 239 // https://crbug.com/580311 | |
| 240 is_intraword_linebreak = !OverlapsOnYAxis(char_rect, next_char_rect); | |
| 241 } | |
| 242 if (is_intraword_linebreak || | |
| 243 base::IsUnicodeWhitespace(character) || | |
| 244 IsEol(character)) { | |
| 245 if (!word_rect.IsEmpty() && seen_literal_text_in_word) { | |
| 246 word_rect = pp::Rect(); | |
| 247 seen_literal_text_in_word = false; | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 if (is_intraword_linebreak || IsEol(character)) { | |
| 252 if (!line_rect.IsEmpty()) { | |
| 253 if (is_intraword_linebreak) { | |
| 254 // Add a 0-width hyphen. | |
| 255 line.push_back('-'); | |
| 256 } | |
| 257 | |
| 258 base::DictionaryValue* text_node = new base::DictionaryValue(); | |
| 259 text_node->SetString(kTextNodeType, kTextNodeTypeText); | |
| 260 text_node->SetString(kTextNodeText, line); | |
| 261 | |
| 262 base::ListValue* text_nodes = new base::ListValue(); | |
| 263 text_nodes->Append(text_node); | |
| 264 | |
| 265 base::DictionaryValue* line_node = new base::DictionaryValue(); | |
| 266 line_node->SetDouble(kTextBoxLeft, line_rect.x()); | |
| 267 line_node->SetDouble(kTextBoxTop, line_rect.y()); | |
| 268 line_node->SetDouble(kTextBoxWidth, line_rect.width()); | |
| 269 line_node->SetDouble(kTextBoxHeight, line_rect.height()); | |
| 270 line_node->SetDouble(kTextBoxFontSize, | |
| 271 FPDFText_GetFontSize(text_page, i)); | |
| 272 line_node->Set(kTextBoxNodes, text_nodes); | |
| 273 text->Append(line_node); | |
| 274 | |
| 275 line.clear(); | |
| 276 line_rect = pp::Rect(); | |
| 277 word_rect = pp::Rect(); | |
| 278 seen_literal_text_in_word = false; | |
| 279 } | |
| 280 continue; | |
| 281 } | |
| 282 seen_literal_text_in_word = seen_literal_text_in_word || | |
| 283 !base::IsUnicodeWhitespace(character); | |
| 284 line.push_back(character); | |
| 285 | |
| 286 if (!char_rect.IsEmpty()) { | |
| 287 line_rect = line_rect.Union(char_rect); | |
| 288 | |
| 289 if (!base::IsUnicodeWhitespace(character)) | |
| 290 word_rect = word_rect.Union(char_rect); | |
| 291 } | |
| 292 } | |
| 293 | |
| 147 node->Set(kPageTextBox, text); // Takes ownership of |text| | 294 node->Set(kPageTextBox, text); // Takes ownership of |text| |
|
Lei Zhang
2016/02/04 02:32:49
Given how far away |text| is initialized, I would
dmazzoni
2016/02/04 17:18:44
Done.
| |
| 148 | 295 |
| 149 return node; | 296 return node; |
| 150 } | 297 } |
| 151 | 298 |
| 152 base::Value* PDFiumPage::GetTextBoxAsValue(double page_height, | |
| 153 double left, double top, | |
| 154 double right, double bottom, | |
| 155 int rotation) { | |
| 156 base::string16 text_utf16; | |
| 157 int char_count = | |
| 158 FPDFText_GetBoundedText(GetTextPage(), left, top, right, bottom, NULL, 0); | |
| 159 if (char_count > 0) { | |
| 160 unsigned short* data = reinterpret_cast<unsigned short*>( | |
| 161 base::WriteInto(&text_utf16, char_count + 1)); | |
| 162 FPDFText_GetBoundedText(GetTextPage(), | |
| 163 left, top, right, bottom, | |
| 164 data, char_count); | |
| 165 } | |
| 166 std::string text_utf8 = base::UTF16ToUTF8(text_utf16); | |
| 167 | |
| 168 FPDF_LINK link = FPDFLink_GetLinkAtPoint(GetPage(), left, top); | |
| 169 Area area; | |
| 170 std::vector<LinkTarget> targets; | |
| 171 if (link) { | |
| 172 targets.push_back(LinkTarget()); | |
| 173 area = GetLinkTarget(link, &targets[0]); | |
| 174 } else { | |
| 175 pp::Rect rect( | |
| 176 PageToScreen(pp::Point(), 1.0, left, top, right, bottom, rotation)); | |
| 177 GetLinks(rect, &targets); | |
| 178 area = targets.empty() ? TEXT_AREA : WEBLINK_AREA; | |
| 179 } | |
| 180 | |
| 181 int char_index = FPDFText_GetCharIndexAtPos(GetTextPage(), left, top, | |
| 182 kTolerance, kTolerance); | |
| 183 double font_size = FPDFText_GetFontSize(GetTextPage(), char_index); | |
| 184 | |
| 185 base::DictionaryValue* node = new base::DictionaryValue(); | |
| 186 node->SetDouble(kTextBoxLeft, left); | |
| 187 node->SetDouble(kTextBoxTop, page_height - top); | |
| 188 node->SetDouble(kTextBoxWidth, right - left); | |
| 189 node->SetDouble(kTextBoxHeight, top - bottom); | |
| 190 node->SetDouble(kTextBoxFontSize, font_size); | |
| 191 | |
| 192 base::ListValue* text_nodes = new base::ListValue(); | |
| 193 | |
| 194 if (area == DOCLINK_AREA) { | |
| 195 std::string url = kDocLinkURLPrefix + base::IntToString(targets[0].page); | |
| 196 text_nodes->Append(CreateURLNode(text_utf8, url)); | |
| 197 } else if (area == WEBLINK_AREA && link) { | |
| 198 text_nodes->Append(CreateURLNode(text_utf8, targets[0].url)); | |
| 199 } else if (area == WEBLINK_AREA && !link) { | |
| 200 size_t start = 0; | |
| 201 for (const auto& target : targets) { | |
| 202 // If there is an extra NULL character at end, find() will not return any | |
| 203 // matches. There should not be any though. | |
| 204 if (!target.url.empty()) | |
| 205 DCHECK_NE(target.url.back(), '\0'); | |
| 206 | |
| 207 // PDFium may change the case of generated links. | |
| 208 std::string lowerCaseURL = base::ToLowerASCII(target.url); | |
| 209 std::string lowerCaseText = base::ToLowerASCII(text_utf8); | |
| 210 size_t pos = lowerCaseText.find(lowerCaseURL, start); | |
| 211 size_t length = target.url.size(); | |
| 212 if (pos == std::string::npos) { | |
| 213 // Check if the link is a "mailto:" URL | |
| 214 if (lowerCaseURL.compare(0, 7, "mailto:") == 0) { | |
| 215 pos = lowerCaseText.find(lowerCaseURL.substr(7), start); | |
| 216 length -= 7; | |
| 217 } | |
| 218 | |
| 219 if (pos == std::string::npos) { | |
| 220 // No match has been found. This should never happen. | |
| 221 continue; | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 std::string before_text = text_utf8.substr(start, pos - start); | |
| 226 if (!before_text.empty()) | |
| 227 text_nodes->Append(CreateTextNode(before_text)); | |
| 228 std::string link_text = text_utf8.substr(pos, length); | |
| 229 text_nodes->Append(CreateURLNode(link_text, target.url)); | |
| 230 | |
| 231 start = pos + length; | |
| 232 } | |
| 233 std::string before_text = text_utf8.substr(start); | |
| 234 if (!before_text.empty()) | |
| 235 text_nodes->Append(CreateTextNode(before_text)); | |
| 236 } else { | |
| 237 text_nodes->Append(CreateTextNode(text_utf8)); | |
| 238 } | |
| 239 | |
| 240 node->Set(kTextBoxNodes, text_nodes); // Takes ownership of |text_nodes|. | |
| 241 return node; | |
| 242 } | |
| 243 | |
| 244 base::Value* PDFiumPage::CreateTextNode(const std::string& text) { | |
| 245 base::DictionaryValue* node = new base::DictionaryValue(); | |
| 246 node->SetString(kTextNodeType, kTextNodeTypeText); | |
| 247 node->SetString(kTextNodeText, text); | |
| 248 return node; | |
| 249 } | |
| 250 | |
| 251 base::Value* PDFiumPage::CreateURLNode(const std::string& text, | |
| 252 const std::string& url) { | |
| 253 base::DictionaryValue* node = new base::DictionaryValue(); | |
| 254 node->SetString(kTextNodeType, kTextNodeTypeURL); | |
| 255 node->SetString(kTextNodeText, text); | |
| 256 node->SetString(kTextNodeURL, url); | |
| 257 return node; | |
| 258 } | |
| 259 | |
| 260 PDFiumPage::Area PDFiumPage::GetCharIndex(const pp::Point& point, | 299 PDFiumPage::Area PDFiumPage::GetCharIndex(const pp::Point& point, |
| 261 int rotation, | 300 int rotation, |
| 262 int* char_index, | 301 int* char_index, |
| 263 int* form_type, | 302 int* form_type, |
| 264 LinkTarget* target) { | 303 LinkTarget* target) { |
| 265 if (!available_) | 304 if (!available_) |
| 266 return NONSELECTABLE_AREA; | 305 return NONSELECTABLE_AREA; |
| 267 pp::Point point2 = point - rect_.point(); | 306 pp::Point point2 = point - rect_.point(); |
| 268 double new_x; | 307 double new_x; |
| 269 double new_y; | 308 double new_y; |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 page_->loading_count_--; | 564 page_->loading_count_--; |
| 526 } | 565 } |
| 527 | 566 |
| 528 PDFiumPage::Link::Link() { | 567 PDFiumPage::Link::Link() { |
| 529 } | 568 } |
| 530 | 569 |
| 531 PDFiumPage::Link::~Link() { | 570 PDFiumPage::Link::~Link() { |
| 532 } | 571 } |
| 533 | 572 |
| 534 } // namespace chrome_pdf | 573 } // namespace chrome_pdf |
| OLD | NEW |