#include "config.h"
#include "TextUtil.h"
#if ENABLE(LAYOUT_FORMATTING_CONTEXT)
#include "FontCascade.h"
#include "RenderStyle.h"
namespace WebCore {
namespace Layout {
Optional<ItemPosition> TextUtil::hyphenPositionBefore(const InlineItem&, ItemPosition, unsigned)
{
return WTF::nullopt;
}
LayoutUnit TextUtil::width(const InlineItem& inlineTextItem, ItemPosition from, ItemPosition to, LayoutUnit contentLogicalLeft)
{
auto& style = inlineTextItem.style();
auto& font = style.fontCascade();
if (!font.size() || from == to)
return 0;
auto text = inlineTextItem.textContent();
ASSERT(to <= text.length());
if (font.isFixedPitch())
return fixedPitchWidth(text, style, from, to, contentLogicalLeft);
auto hasKerningOrLigatures = font.enableKerning() || font.requiresShaping();
auto measureWithEndSpace = hasKerningOrLigatures && to < text.length() && text[to] == ' ';
if (measureWithEndSpace)
++to;
LayoutUnit width;
auto tabWidth = style.collapseWhiteSpace() ? 0 : style.tabSize();
WebCore::TextRun run(StringView(text).substring(from, to - from), contentLogicalLeft);
if (tabWidth)
run.setTabSize(true, tabWidth);
width = font.width(run);
if (measureWithEndSpace)
width -= (font.spaceWidth() + font.wordSpacing());
return std::max<LayoutUnit>(0, width);
}
LayoutUnit TextUtil::fixedPitchWidth(String text, const RenderStyle& style, ItemPosition from, ItemPosition to, LayoutUnit contentLogicalLeft)
{
auto& font = style.fontCascade();
auto monospaceCharacterWidth = font.spaceWidth();
LayoutUnit width;
for (auto i = from; i < to; ++i) {
auto character = text[i];
if (character >= ' ' || character == '\n')
width += monospaceCharacterWidth;
else if (character == '\t')
width += style.collapseWhiteSpace() ? monospaceCharacterWidth : font.tabWidth(style.tabSize(), contentLogicalLeft + width);
if (i > from && (character == ' ' || character == '\t' || character == '\n'))
width += font.wordSpacing();
}
return width;
}
}
}
#endif