1 | /* |
2 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "TextUtil.h" |
28 | |
29 | #if ENABLE(LAYOUT_FORMATTING_CONTEXT) |
30 | |
31 | #include "FontCascade.h" |
32 | #include "RenderStyle.h" |
33 | |
34 | namespace WebCore { |
35 | namespace Layout { |
36 | |
37 | Optional<ItemPosition> TextUtil::hyphenPositionBefore(const InlineItem&, ItemPosition, unsigned) |
38 | { |
39 | return WTF::nullopt; |
40 | } |
41 | |
42 | LayoutUnit TextUtil::width(const InlineItem& inlineTextItem, ItemPosition from, ItemPosition to, LayoutUnit contentLogicalLeft) |
43 | { |
44 | auto& style = inlineTextItem.style(); |
45 | auto& font = style.fontCascade(); |
46 | if (!font.size() || from == to) |
47 | return 0; |
48 | |
49 | auto text = inlineTextItem.textContent(); |
50 | ASSERT(to <= text.length()); |
51 | |
52 | if (font.isFixedPitch()) |
53 | return fixedPitchWidth(text, style, from, to, contentLogicalLeft); |
54 | |
55 | auto hasKerningOrLigatures = font.enableKerning() || font.requiresShaping(); |
56 | auto measureWithEndSpace = hasKerningOrLigatures && to < text.length() && text[to] == ' '; |
57 | if (measureWithEndSpace) |
58 | ++to; |
59 | LayoutUnit width; |
60 | auto tabWidth = style.collapseWhiteSpace() ? 0 : style.tabSize(); |
61 | |
62 | WebCore::TextRun run(StringView(text).substring(from, to - from), contentLogicalLeft); |
63 | if (tabWidth) |
64 | run.setTabSize(true, tabWidth); |
65 | width = font.width(run); |
66 | |
67 | if (measureWithEndSpace) |
68 | width -= (font.spaceWidth() + font.wordSpacing()); |
69 | |
70 | return std::max<LayoutUnit>(0, width); |
71 | } |
72 | |
73 | LayoutUnit TextUtil::fixedPitchWidth(String text, const RenderStyle& style, ItemPosition from, ItemPosition to, LayoutUnit contentLogicalLeft) |
74 | { |
75 | auto& font = style.fontCascade(); |
76 | auto monospaceCharacterWidth = font.spaceWidth(); |
77 | LayoutUnit width; |
78 | for (auto i = from; i < to; ++i) { |
79 | auto character = text[i]; |
80 | if (character >= ' ' || character == '\n') |
81 | width += monospaceCharacterWidth; |
82 | else if (character == '\t') |
83 | width += style.collapseWhiteSpace() ? monospaceCharacterWidth : font.tabWidth(style.tabSize(), contentLogicalLeft + width); |
84 | |
85 | if (i > from && (character == ' ' || character == '\t' || character == '\n')) |
86 | width += font.wordSpacing(); |
87 | } |
88 | |
89 | return width; |
90 | } |
91 | |
92 | } |
93 | } |
94 | #endif |
95 | |