0

Update Chromium PDF code to use more floats instead of doubles.

Update Chromium PDF code to use more floats, because that is what PDFs
contain and what newer PDFium APIs return. Adjust related code that
process these floats as well.

Change-Id: I294eea6c66a71a41b340396d4d45dde48eabc5e4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1961566
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#724683}
This commit is contained in:
Lei Zhang
2019-12-13 17:35:41 +00:00
committed by Commit Bot
parent ae887e0743
commit 58237821fe
8 changed files with 55 additions and 56 deletions

@ -31,19 +31,19 @@ namespace {
// Don't try to apply font size thresholds to automatically identify headings
// if the median font size is not at least this many points.
const double kMinimumFontSize = 5;
const float kMinimumFontSize = 5.0f;
// Don't try to apply paragraph break thresholds to automatically identify
// paragraph breaks if the median line break is not at least this many points.
const double kMinimumLineSpacing = 5;
const float kMinimumLineSpacing = 5.0f;
// Ratio between the font size of one text run and the median on the page
// for that text run to be considered to be a heading instead of normal text.
const double kHeadingFontSizeRatio = 1.2;
const float kHeadingFontSizeRatio = 1.2f;
// Ratio between the line spacing between two lines and the median on the
// page for that line spacing to be considered a paragraph break.
const double kParagraphLineSpacingRatio = 1.2;
const float kParagraphLineSpacingRatio = 1.2f;
gfx::RectF ToGfxRectF(const PP_FloatRect& r) {
return gfx::RectF(r.point.x, r.point.y, r.size.width, r.size.height);
@ -105,7 +105,7 @@ class LineHelper {
private:
void AddRun(const PP_FloatRect& run_bounds) {
float run_width = fabs(run_bounds.size.width);
float run_width = fabsf(run_bounds.size.width);
accumulated_width_ += run_width;
accumulated_weight_top_ += run_bounds.point.y * run_width;
accumulated_weight_bottom_ +=
@ -113,7 +113,7 @@ class LineHelper {
}
void RemoveRun(const PP_FloatRect& run_bounds) {
float run_width = fabs(run_bounds.size.width);
float run_width = fabsf(run_bounds.size.width);
accumulated_width_ -= run_width;
accumulated_weight_top_ -= run_bounds.point.y * run_width;
accumulated_weight_bottom_ -=
@ -166,12 +166,12 @@ void ConnectPreviousAndNextOnLine(ui::AXNodeData* previous_on_line_node,
bool BreakParagraph(
const std::vector<ppapi::PdfAccessibilityTextRunInfo>& text_runs,
uint32_t text_run_index,
double paragraph_spacing_threshold) {
float paragraph_spacing_threshold) {
// Check to see if its also a new paragraph, i.e., if the distance between
// lines is greater than the threshold. If there's no threshold, that
// means there weren't enough lines to compute an accurate median, so
// we compare against the line size instead.
double line_spacing = fabs(text_runs[text_run_index + 1].bounds.point.y -
float line_spacing = fabsf(text_runs[text_run_index + 1].bounds.point.y -
text_runs[text_run_index].bounds.point.y);
return ((paragraph_spacing_threshold > 0 &&
line_spacing > paragraph_spacing_threshold) ||
@ -401,8 +401,8 @@ void PdfAccessibilityTree::AddPageContent(
const std::vector<PP_PrivateAccessibilityCharInfo>& chars,
const ppapi::PdfAccessibilityPageObjects& page_objects) {
DCHECK(page_node);
double heading_font_size_threshold = 0;
double paragraph_spacing_threshold = 0;
float heading_font_size_threshold = 0;
float paragraph_spacing_threshold = 0;
ComputeParagraphAndHeadingThresholds(text_runs, &heading_font_size_threshold,
&paragraph_spacing_threshold);
@ -692,14 +692,14 @@ void PdfAccessibilityTree::FindNodeOffset(uint32_t page_index,
void PdfAccessibilityTree::ComputeParagraphAndHeadingThresholds(
const std::vector<ppapi::PdfAccessibilityTextRunInfo>& text_runs,
double* out_heading_font_size_threshold,
double* out_paragraph_spacing_threshold) {
float* out_heading_font_size_threshold,
float* out_paragraph_spacing_threshold) {
// Scan over the font sizes and line spacing within this page and
// set heuristic thresholds so that text larger than the median font
// size can be marked as a heading, and spacing larger than the median
// line spacing can be a paragraph break.
std::vector<double> font_sizes;
std::vector<double> line_spacings;
std::vector<float> font_sizes;
std::vector<float> line_spacings;
for (size_t i = 0; i < text_runs.size(); ++i) {
font_sizes.push_back(text_runs[i].style.font_size);
if (i > 0) {
@ -711,7 +711,7 @@ void PdfAccessibilityTree::ComputeParagraphAndHeadingThresholds(
}
if (font_sizes.size() > 2) {
std::sort(font_sizes.begin(), font_sizes.end());
double median_font_size = font_sizes[font_sizes.size() / 2];
float median_font_size = font_sizes[font_sizes.size() / 2];
if (median_font_size > kMinimumFontSize) {
*out_heading_font_size_threshold =
median_font_size * kHeadingFontSizeRatio;
@ -719,7 +719,7 @@ void PdfAccessibilityTree::ComputeParagraphAndHeadingThresholds(
}
if (line_spacings.size() > 4) {
std::sort(line_spacings.begin(), line_spacings.end());
double median_line_spacing = line_spacings[line_spacings.size() / 2];
float median_line_spacing = line_spacings[line_spacings.size() / 2];
if (median_line_spacing > kMinimumLineSpacing) {
*out_paragraph_spacing_threshold =
median_line_spacing * kParagraphLineSpacingRatio;
@ -780,8 +780,8 @@ ui::AXNodeData* PdfAccessibilityTree::CreateNode(ax::mojom::Role role) {
}
ui::AXNodeData* PdfAccessibilityTree::CreateParagraphNode(
double font_size,
double heading_font_size_threshold) {
float font_size,
float heading_font_size_threshold) {
ui::AXNodeData* para_node = CreateNode(ax::mojom::Role::kParagraph);
para_node->AddBoolAttribute(ax::mojom::BoolAttribute::kIsLineBreakingObject,
true);

@ -117,8 +117,8 @@ class PdfAccessibilityTree : public content::PluginAXTreeSource {
void ComputeParagraphAndHeadingThresholds(
const std::vector<ppapi::PdfAccessibilityTextRunInfo>& text_runs,
double* out_heading_font_size_threshold,
double* out_paragraph_spacing_threshold);
float* out_heading_font_size_threshold,
float* out_paragraph_spacing_threshold);
std::string GetTextRunCharsAsUTF8(
const ppapi::PdfAccessibilityTextRunInfo& text_run,
const std::vector<PP_PrivateAccessibilityCharInfo>& chars,
@ -130,8 +130,8 @@ class PdfAccessibilityTree : public content::PluginAXTreeSource {
gfx::Vector2dF ToVector2dF(const PP_Point& p);
gfx::RectF ToRectF(const PP_Rect& r);
ui::AXNodeData* CreateNode(ax::mojom::Role role);
ui::AXNodeData* CreateParagraphNode(double font_size,
double heading_font_size_threshold);
ui::AXNodeData* CreateParagraphNode(float font_size,
float heading_font_size_threshold);
ui::AXNodeData* CreateStaticTextNode(uint32_t char_index);
ui::AXNodeData* CreateInlineTextBoxNode(
const ppapi::PdfAccessibilityTextRunInfo& text_run,

@ -89,7 +89,7 @@ TEST_F(AccessibilityTest, GetAccessibilityPage) {
for (size_t i = 0; i < kExpectedTextRunCount; ++i) {
const auto& expected = kExpectedTextRuns[i];
EXPECT_EQ(expected.len, text_runs[i].len) << i;
EXPECT_DOUBLE_EQ(expected.font_size, text_runs[i].style.font_size) << i;
EXPECT_FLOAT_EQ(expected.font_size, text_runs[i].style.font_size) << i;
EXPECT_FLOAT_EQ(expected.bounds_x, text_runs[i].bounds.point.x) << i;
EXPECT_FLOAT_EQ(expected.bounds_y, text_runs[i].bounds.point.y) << i;
float expected_bounds_w =

@ -34,11 +34,11 @@ namespace chrome_pdf {
namespace {
constexpr double k45DegreesInRadians = base::kPiDouble / 4;
constexpr double k90DegreesInRadians = base::kPiDouble / 2;
constexpr double k180DegreesInRadians = base::kPiDouble;
constexpr double k270DegreesInRadians = 3 * base::kPiDouble / 2;
constexpr double k360DegreesInRadians = 2 * base::kPiDouble;
constexpr float k45DegreesInRadians = base::kPiFloat / 4;
constexpr float k90DegreesInRadians = base::kPiFloat / 2;
constexpr float k180DegreesInRadians = base::kPiFloat;
constexpr float k270DegreesInRadians = 3 * base::kPiFloat / 2;
constexpr float k360DegreesInRadians = 2 * base::kPiFloat;
PDFiumPage::IsValidLinkFunction g_is_valid_link_func_for_testing = nullptr;
@ -107,7 +107,7 @@ int GetFirstNonUnicodeWhiteSpaceCharIndex(FPDF_TEXTPAGE text_page,
return i;
}
PP_PrivateDirection GetDirectionFromAngle(double angle) {
PP_PrivateDirection GetDirectionFromAngle(float angle) {
// Rotating the angle by 45 degrees to simplify the conditions statements.
// It's like if we rotated the whole cartesian coordinate system like below.
// X X
@ -122,7 +122,7 @@ PP_PrivateDirection GetDirectionFromAngle(double angle) {
// X II X
// X X
angle = fmod(angle + k45DegreesInRadians, k360DegreesInRadians);
angle = fmodf(angle + k45DegreesInRadians, k360DegreesInRadians);
// Quadrant I.
if (angle >= 0 && angle <= k90DegreesInRadians)
return PP_PRIVATEDIRECTION_LTR;
@ -136,10 +136,10 @@ PP_PrivateDirection GetDirectionFromAngle(double angle) {
return PP_PRIVATEDIRECTION_BTT;
}
double GetDistanceBetweenPoints(const pp::FloatPoint& p1,
const pp::FloatPoint& p2) {
float GetDistanceBetweenPoints(const pp::FloatPoint& p1,
const pp::FloatPoint& p2) {
pp::FloatPoint dist_vector = p1 - p2;
return sqrt(pow(dist_vector.x(), 2) + pow(dist_vector.y(), 2));
return sqrtf(powf(dist_vector.x(), 2) + powf(dist_vector.y(), 2));
}
void AddCharSizeToAverageCharSize(pp::FloatSize new_size,
@ -156,32 +156,31 @@ void AddCharSizeToAverageCharSize(pp::FloatSize new_size,
}
}
double GetRotatedCharWidth(double angle, const pp::FloatSize& size) {
return abs(cos(angle) * size.width()) + abs(sin(angle) * size.height());
float GetRotatedCharWidth(float angle, const pp::FloatSize& size) {
return fabsf(cosf(angle) * size.width()) + fabsf(sinf(angle) * size.height());
}
double GetAngleOfVector(const pp::FloatPoint& v) {
double angle = atan2(v.y(), v.x());
float GetAngleOfVector(const pp::FloatPoint& v) {
float angle = atan2f(v.y(), v.x());
if (angle < 0)
angle += k360DegreesInRadians;
return angle;
}
double GetAngleDifference(double a, double b) {
float GetAngleDifference(float a, float b) {
// This is either the difference or (360 - difference).
double x = fmod(fabs(b - a), k360DegreesInRadians);
float x = fmodf(fabsf(b - a), k360DegreesInRadians);
return x > k180DegreesInRadians ? k360DegreesInRadians - x : x;
}
bool DoubleEquals(double d1, double d2) {
bool FloatEquals(float f1, float f2) {
// The idea behind this is to use this fraction of the larger of the
// two numbers as the limit of the difference. This breaks down near
// zero, so we reuse this as the minimum absolute size we will use
// for the base of the scale too.
static const float epsilon_scale = 0.00001f;
return fabs(d1 - d2) <
epsilon_scale *
std::fmax(std::fmax(std::fabs(d1), std::fabs(d2)), epsilon_scale);
static constexpr float kEpsilonScale = 0.00001f;
return fabsf(f1 - f2) <
kEpsilonScale * fmaxf(fmaxf(fabsf(f1), fabsf(f2)), kEpsilonScale);
}
uint32_t MakeARGB(unsigned int a,
@ -353,7 +352,7 @@ bool PDFiumPage::AreTextStyleEqual(
return char_style.font_name == style.font_name &&
char_style.font_weight == style.font_weight &&
char_style.render_mode == style.render_mode &&
DoubleEquals(char_style.font_size, style.font_size) &&
FloatEquals(char_style.font_size, style.font_size) &&
char_style.fill_color == style.fill_color &&
char_style.stroke_color == style.stroke_color &&
char_style.is_italic == style.is_italic &&
@ -391,7 +390,7 @@ PDFiumPage::GetTextRunInfo(int start_char_index) {
pp::FloatRect start_char_rect =
GetFloatCharRectInPixels(page, text_page, char_index);
double text_run_font_size = info.style.font_size;
float text_run_font_size = info.style.font_size;
// Heuristic: Initialize the average character size to one-third of the font
// size to avoid having the first few characters misrepresent the average.
@ -418,7 +417,7 @@ PDFiumPage::GetTextRunInfo(int start_char_index) {
// The angle of the vector starting at the first character center-point and
// ending at the current last character center-point.
double text_run_angle = 0;
float text_run_angle = 0;
CalculatePageObjectTextRunBreaks();
const auto breakpoint_iter =
@ -453,8 +452,8 @@ PDFiumPage::GetTextRunInfo(int start_char_index) {
// Heuristic: End the text run if the difference between the text run
// angle and the angle between the center-points of the previous and
// current characters is greater than 90 degrees.
double current_angle = GetAngleOfVector(char_rect.CenterPoint() -
prev_char_rect.CenterPoint());
float current_angle = GetAngleOfVector(char_rect.CenterPoint() -
prev_char_rect.CenterPoint());
if (start_char_rect != prev_char_rect) {
text_run_angle = GetAngleOfVector(prev_char_rect.CenterPoint() -
start_char_rect.CenterPoint());
@ -470,15 +469,15 @@ PDFiumPage::GetTextRunInfo(int start_char_index) {
AddCharSizeToAverageCharSize(char_rect.Floatsize(), &avg_char_size,
&non_whitespace_chars_count);
double avg_char_width = GetRotatedCharWidth(current_angle, avg_char_size);
float avg_char_width = GetRotatedCharWidth(current_angle, avg_char_size);
double distance =
float distance =
GetDistanceBetweenPoints(char_rect.CenterPoint(),
prev_char_rect.CenterPoint()) -
GetRotatedCharWidth(current_angle, char_rect.Floatsize()) / 2 -
GetRotatedCharWidth(current_angle, prev_char_rect.Floatsize()) / 2;
if (distance > 2.5 * avg_char_width)
if (distance > 2.5f * avg_char_width)
break;
text_run_bounds = text_run_bounds.Union(char_rect);

@ -48,7 +48,7 @@ void CompareTextRuns(
EXPECT_EQ(expected_style.font_name, actual_style.font_name);
EXPECT_EQ(expected_style.font_weight, actual_style.font_weight);
EXPECT_EQ(expected_style.render_mode, actual_style.render_mode);
EXPECT_EQ(expected_style.font_size, actual_style.font_size);
EXPECT_FLOAT_EQ(expected_style.font_size, actual_style.font_size);
EXPECT_EQ(expected_style.fill_color, actual_style.fill_color);
EXPECT_EQ(expected_style.stroke_color, actual_style.stroke_color);
EXPECT_EQ(expected_style.is_italic, actual_style.is_italic);

@ -89,7 +89,7 @@ struct PP_PrivateAccessibilityTextStyleInfo {
uint32_t font_name_length;
int font_weight;
PP_TextRenderingMode render_mode;
double font_size;
float font_size;
// Colors are ARGB.
uint32_t fill_color;
uint32_t stroke_color;

@ -28,7 +28,7 @@ class PDF {
std::string font_name;
int font_weight;
PP_TextRenderingMode render_mode;
double font_size;
float font_size;
// Colors are ARGB.
uint32_t fill_color;
uint32_t stroke_color;

@ -26,7 +26,7 @@ struct PPAPI_SHARED_EXPORT PdfAccessibilityTextStyleInfo {
std::string font_name;
int font_weight;
PP_TextRenderingMode render_mode;
double font_size;
float font_size;
// Colors are ARGB.
uint32_t fill_color;
uint32_t stroke_color;