File: BidiContext.cpp

package info (click to toggle)
webkit2gtk 2.42.2-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 362,452 kB
  • sloc: cpp: 2,881,971; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 288; pascal: 60
file content (133 lines) | stat: -rw-r--r-- 5,068 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2003-2017 Apple Inc. All right reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "BidiContext.h"

#include <mutex>
#include <wtf/NeverDestroyed.h>
#include <wtf/Vector.h>

namespace WebCore {

struct SameSizeAsBidiContext : public ThreadSafeRefCounted<SameSizeAsBidiContext> {
    uint32_t bitfields : 16;
    void* parent;
};

static_assert(sizeof(BidiContext) == sizeof(SameSizeAsBidiContext), "BidiContext should stay small");

inline BidiContext::BidiContext(unsigned char level, UCharDirection direction, bool override, BidiEmbeddingSource source, BidiContext* parent)
    : m_level(level)
    , m_direction(direction)
    , m_override(override)
    , m_source(source)
    , m_parent(parent)
{
}

inline Ref<BidiContext> BidiContext::createUncached(unsigned char level, UCharDirection direction, bool override, BidiEmbeddingSource source, BidiContext* parent)
{
    return adoptRef(*new BidiContext(level, direction, override, source, parent));
}

Ref<BidiContext> BidiContext::create(unsigned char level, UCharDirection direction, bool override, BidiEmbeddingSource source, BidiContext* parent)
{
    ASSERT(direction == (level % 2 ? U_RIGHT_TO_LEFT : U_LEFT_TO_RIGHT));

    if (parent)
        return createUncached(level, direction, override, source, parent);

    ASSERT(level <= 1);
    if (!level) {
        if (!override) {
            static NeverDestroyed<RefPtr<BidiContext>> ltrContext;
            static std::once_flag ltrContextOnceFlag;
            std::call_once(ltrContextOnceFlag, [&]() {
                ltrContext.get() = createUncached(0, U_LEFT_TO_RIGHT, false, FromStyleOrDOM, 0);
            });
            return *ltrContext.get();
        }

        static NeverDestroyed<RefPtr<BidiContext>> ltrOverrideContext;
        static std::once_flag ltrOverrideContextOnceFlag;
        std::call_once(ltrOverrideContextOnceFlag, [&]() {
            ltrOverrideContext.get() = createUncached(0, U_LEFT_TO_RIGHT, true, FromStyleOrDOM, 0);
        });
        return *ltrOverrideContext.get();
    }

    if (!override) {
        static NeverDestroyed<RefPtr<BidiContext>> rtlContext;
        static std::once_flag rtlContextOnceFlag;
        std::call_once(rtlContextOnceFlag, [&]() {
            rtlContext.get() = createUncached(1, U_RIGHT_TO_LEFT, false, FromStyleOrDOM, 0);
        });
        return *rtlContext.get();
    }

    static NeverDestroyed<RefPtr<BidiContext>> rtlOverrideContext;
    static std::once_flag rtlOverrideContextOnceFlag;
    std::call_once(rtlOverrideContextOnceFlag, [&]() {
        rtlOverrideContext.get() = createUncached(1, U_RIGHT_TO_LEFT, true, FromStyleOrDOM, 0);
    });
    return *rtlOverrideContext.get();
}

static inline Ref<BidiContext> copyContextAndRebaselineLevel(BidiContext& context, BidiContext* parent)
{
    auto newLevel = parent ? parent->level() : 0;
    if (context.dir() == U_RIGHT_TO_LEFT)
        newLevel = nextGreaterOddLevel(newLevel);
    else if (parent)
        newLevel = nextGreaterEvenLevel(newLevel);
    return BidiContext::create(newLevel, context.dir(), context.override(), context.source(), parent);
}

// The BidiContext stack must be immutable -- they're re-used for re-layout after
// DOM modification/editing -- so we copy all the non-Unicode contexts, and
// recalculate their levels.
Ref<BidiContext> BidiContext::copyStackRemovingUnicodeEmbeddingContexts()
{
    Vector<BidiContext*, 64> contexts;
    for (auto* ancestor = this; ancestor; ancestor = ancestor->parent()) {
        if (ancestor->source() != FromUnicode)
            contexts.append(ancestor);
    }
    ASSERT(contexts.size());
    auto topContext = copyContextAndRebaselineLevel(*contexts.last(), nullptr);
    for (unsigned i = contexts.size() - 1; i; --i)
        topContext = copyContextAndRebaselineLevel(*contexts[i - 1], topContext.ptr());
    return topContext;
}

bool operator==(const BidiContext& c1, const BidiContext& c2)
{
    if (&c1 == &c2)
        return true;
    if (c1.level() != c2.level() || c1.override() != c2.override() || c1.dir() != c2.dir() || c1.source() != c2.source())
        return false;
    if (!c1.parent())
        return !c2.parent();
    return c2.parent() && *c1.parent() == *c2.parent();
}

} // namespace WebCore