Commit a34decbf authored by Koji Ishii's avatar Koji Ishii Committed by Commit Bot

Fix Font::TabWidth not to return -NaN

This patch fixes the |float| variation of |Font::TabWidth|
not to return -NaN by adding the same guard as its
|LayoutUnit| variation.

Bug: 1002999
Change-Id: I4fc1fdb233b4156a425c92d97f7f9444be527d12
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1798070Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Commit-Queue: Emil A Eklund <eae@chromium.org>
Commit-Queue: Koji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695829}
parent d57b2587
......@@ -550,6 +550,24 @@ void Font::ExpandRangeToIncludePartialGlyphs(const TextRun& text_run,
buffer.ExpandRangeToIncludePartialGlyphs(from, to);
}
float Font::TabWidth(const SimpleFontData* font_data,
const TabSize& tab_size,
float position) const {
float base_tab_width = TabWidth(font_data, tab_size);
if (!base_tab_width)
return GetFontDescription().LetterSpacing();
float distance_to_tab_stop = base_tab_width - fmodf(position, base_tab_width);
// Let the minimum width be the half of the space width so that it's always
// recognizable. if the distance to the next tab stop is less than that,
// advance an additional tab stop.
if (distance_to_tab_stop < font_data->SpaceWidth() / 2)
distance_to_tab_stop += base_tab_width;
return distance_to_tab_stop;
}
LayoutUnit Font::TabWidth(const TabSize& tab_size, LayoutUnit position) const {
const SimpleFontData* font_data = PrimaryFont();
if (!font_data)
......
......@@ -276,21 +276,6 @@ inline float Font::TabWidth(const SimpleFontData* font_data,
return base_tab_width ? base_tab_width : GetFontDescription().LetterSpacing();
}
inline float Font::TabWidth(const SimpleFontData* font_data,
const TabSize& tab_size,
float position) const {
float base_tab_width = TabWidth(font_data, tab_size);
float distance_to_tab_stop = base_tab_width - fmodf(position, base_tab_width);
// Let the minimum width be the half of the space width so that it's always
// recognizable. if the distance to the next tab stop is less than that,
// advance an additional tab stop.
if (distance_to_tab_stop < font_data->SpaceWidth() / 2)
distance_to_tab_stop += base_tab_width;
return distance_to_tab_stop;
}
} // namespace blink
#endif
......@@ -9,6 +9,7 @@
#include "third_party/blink/renderer/platform/fonts/text_run_paint_info.h"
#include "third_party/blink/renderer/platform/testing/font_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/text/tab_size.h"
#include "third_party/blink/renderer/platform/text/text_run.h"
using blink::test::CreateTestFont;
......@@ -86,4 +87,12 @@ TEST_F(FontTest, ExpandRange) {
EXPECT_EQ(GetExpandedRange("tneiciffe", false, 0, 9), Vector<int>({0, 9}));
}
TEST_F(FontTest, TabWidthZero) {
Font font =
CreateTestFont("Ahem", test::PlatformTestDataPath("Ahem.woff"), 0);
TabSize tab_size(8);
EXPECT_EQ(font.TabWidth(tab_size, .0f), .0f);
EXPECT_EQ(font.TabWidth(tab_size, LayoutUnit()), LayoutUnit());
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment