| 1 | /* |
| 2 | * Copyright (C) 2000 Lars Knoll (knoll@kde.org) |
| 3 | * (C) 2000 Antti Koivisto (koivisto@kde.org) |
| 4 | * (C) 2000 Dirk Mueller (mueller@kde.org) |
| 5 | * Copyright (C) 2003-2018 Apple Inc. All rights reserved. |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Library General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Library General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Library General Public License |
| 18 | * along with this library; see the file COPYING.LIB. If not, write to |
| 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #ifndef TextRun_h |
| 25 | #define TextRun_h |
| 26 | |
| 27 | #include "TextFlags.h" |
| 28 | #include "WritingMode.h" |
| 29 | #include <wtf/text/StringView.h> |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | class FloatPoint; |
| 34 | class FloatRect; |
| 35 | class FontCascade; |
| 36 | class GraphicsContext; |
| 37 | class GlyphBuffer; |
| 38 | class Font; |
| 39 | |
| 40 | struct GlyphData; |
| 41 | struct WidthIterator; |
| 42 | |
| 43 | class TextRun { |
| 44 | WTF_MAKE_FAST_ALLOCATED; |
| 45 | public: |
| 46 | explicit TextRun(const String& text, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = DefaultExpansion, TextDirection direction = TextDirection::LTR, bool directionalOverride = false, bool characterScanForCodePath = true) |
| 47 | : m_text(text) |
| 48 | , m_tabSize(0) |
| 49 | , m_xpos(xpos) |
| 50 | , m_horizontalGlyphStretch(1) |
| 51 | , m_expansion(expansion) |
| 52 | , m_expansionBehavior(expansionBehavior) |
| 53 | , m_allowTabs(false) |
| 54 | , m_direction(static_cast<unsigned>(direction)) |
| 55 | , m_directionalOverride(directionalOverride) |
| 56 | , m_characterScanForCodePath(characterScanForCodePath) |
| 57 | , m_disableSpacing(false) |
| 58 | { |
| 59 | ASSERT(!m_text.isNull()); |
| 60 | } |
| 61 | |
| 62 | explicit TextRun(StringView stringView, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = DefaultExpansion, TextDirection direction = TextDirection::LTR, bool directionalOverride = false, bool characterScanForCodePath = true) |
| 63 | : TextRun(stringView.toStringWithoutCopying(), xpos, expansion, expansionBehavior, direction, directionalOverride, characterScanForCodePath) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | TextRun subRun(unsigned startOffset, unsigned length) const |
| 68 | { |
| 69 | ASSERT_WITH_SECURITY_IMPLICATION(startOffset < m_text.length()); |
| 70 | |
| 71 | auto result { *this }; |
| 72 | |
| 73 | if (is8Bit()) |
| 74 | result.setText(data8(startOffset), length); |
| 75 | else |
| 76 | result.setText(data16(startOffset), length); |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | UChar operator[](unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_text.length()); return m_text[i]; } |
| 81 | const LChar* data8(unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_text.length()); ASSERT(is8Bit()); return &m_text.characters8()[i]; } |
| 82 | const UChar* data16(unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_text.length()); ASSERT(!is8Bit()); return &m_text.characters16()[i]; } |
| 83 | |
| 84 | const LChar* characters8() const { ASSERT(is8Bit()); return m_text.characters8(); } |
| 85 | const UChar* characters16() const { ASSERT(!is8Bit()); return m_text.characters16(); } |
| 86 | |
| 87 | bool is8Bit() const { return m_text.is8Bit(); } |
| 88 | unsigned length() const { return m_text.length(); } |
| 89 | |
| 90 | void setText(const LChar* text, unsigned length) { setText({ text, length }); } |
| 91 | void setText(const UChar* text, unsigned length) { setText({ text, length }); } |
| 92 | void setText(StringView text) { ASSERT(!text.isNull()); m_text = text.toStringWithoutCopying(); } |
| 93 | |
| 94 | float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; } |
| 95 | void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; } |
| 96 | |
| 97 | bool allowTabs() const { return m_allowTabs; } |
| 98 | unsigned tabSize() const { return m_tabSize; } |
| 99 | void setTabSize(bool, unsigned); |
| 100 | |
| 101 | float xPos() const { return m_xpos; } |
| 102 | void setXPos(float xPos) { m_xpos = xPos; } |
| 103 | float expansion() const { return m_expansion; } |
| 104 | ExpansionBehavior expansionBehavior() const { return m_expansionBehavior; } |
| 105 | TextDirection direction() const { return static_cast<TextDirection>(m_direction); } |
| 106 | bool rtl() const { return static_cast<TextDirection>(m_direction) == TextDirection::RTL; } |
| 107 | bool ltr() const { return static_cast<TextDirection>(m_direction) == TextDirection::LTR; } |
| 108 | bool directionalOverride() const { return m_directionalOverride; } |
| 109 | bool characterScanForCodePath() const { return m_characterScanForCodePath; } |
| 110 | bool spacingDisabled() const { return m_disableSpacing; } |
| 111 | |
| 112 | void disableSpacing() { m_disableSpacing = true; } |
| 113 | void setDirection(TextDirection direction) { m_direction = static_cast<unsigned>(direction); } |
| 114 | void setDirectionalOverride(bool override) { m_directionalOverride = override; } |
| 115 | void setCharacterScanForCodePath(bool scan) { m_characterScanForCodePath = scan; } |
| 116 | StringView text() const { return m_text; } |
| 117 | |
| 118 | private: |
| 119 | String m_text; |
| 120 | |
| 121 | unsigned m_tabSize; |
| 122 | |
| 123 | // m_xpos is the x position relative to the left start of the text line, not relative to the left |
| 124 | // start of the containing block. In the case of right alignment or center alignment, left start of |
| 125 | // the text line is not the same as left start of the containing block. This variable is only used |
| 126 | // to calculate the width of \t |
| 127 | float m_xpos; |
| 128 | float m_horizontalGlyphStretch; |
| 129 | |
| 130 | float m_expansion; |
| 131 | unsigned m_expansionBehavior : 4; |
| 132 | unsigned m_allowTabs : 1; |
| 133 | unsigned m_direction : 1; |
| 134 | unsigned m_directionalOverride : 1; // Was this direction set by an override character. |
| 135 | unsigned m_characterScanForCodePath : 1; |
| 136 | unsigned m_disableSpacing : 1; |
| 137 | }; |
| 138 | |
| 139 | inline void TextRun::setTabSize(bool allow, unsigned size) |
| 140 | { |
| 141 | m_allowTabs = allow; |
| 142 | m_tabSize = size; |
| 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | #endif |
| 148 | |