1/*
2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2006, 2007, 2009, 2010 Apple Inc. All right reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "config.h"
23#include "BidiContext.h"
24#include <wtf/Vector.h>
25
26namespace WebCore {
27
28struct SameSizeAsBidiContext : public RefCounted<SameSizeAsBidiContext> {
29    uint32_t bitfields : 16;
30    void* parent;
31};
32
33COMPILE_ASSERT(sizeof(BidiContext) == sizeof(SameSizeAsBidiContext), BidiContext_should_stay_small);
34
35inline PassRefPtr<BidiContext> BidiContext::createUncached(unsigned char level, UCharDirection direction, bool override, BidiEmbeddingSource source, BidiContext* parent)
36{
37    return adoptRef(new BidiContext(level, direction, override, source, parent));
38}
39
40PassRefPtr<BidiContext> BidiContext::create(unsigned char level, UCharDirection direction, bool override, BidiEmbeddingSource source, BidiContext* parent)
41{
42    ASSERT(direction == (level % 2 ? U_RIGHT_TO_LEFT : U_LEFT_TO_RIGHT));
43
44    if (parent)
45        return createUncached(level, direction, override, source, parent);
46
47    ASSERT(level <= 1);
48    if (!level) {
49        if (!override) {
50            static BidiContext* ltrContext = createUncached(0, U_LEFT_TO_RIGHT, false, FromStyleOrDOM, 0).leakRef();
51            return ltrContext;
52        }
53
54        static BidiContext* ltrOverrideContext = createUncached(0, U_LEFT_TO_RIGHT, true, FromStyleOrDOM, 0).leakRef();
55        return ltrOverrideContext;
56    }
57
58    if (!override) {
59        static BidiContext* rtlContext = createUncached(1, U_RIGHT_TO_LEFT, false, FromStyleOrDOM, 0).leakRef();
60        return rtlContext;
61    }
62
63    static BidiContext* rtlOverrideContext = createUncached(1, U_RIGHT_TO_LEFT, true, FromStyleOrDOM, 0).leakRef();
64    return rtlOverrideContext;
65}
66
67static inline PassRefPtr<BidiContext> copyContextAndRebaselineLevel(BidiContext* context, BidiContext* parent)
68{
69    ASSERT(context);
70    unsigned char newLevel = parent ? parent->level() : 0;
71    if (context->dir() == U_RIGHT_TO_LEFT)
72        newLevel = nextGreaterOddLevel(newLevel);
73    else if (parent)
74        newLevel = nextGreaterEvenLevel(newLevel);
75
76    return BidiContext::create(newLevel, context->dir(), context->override(), context->source(), parent);
77}
78
79// The BidiContext stack must be immutable -- they're re-used for re-layout after
80// DOM modification/editing -- so we copy all the non-unicode contexts, and
81// recalculate their levels.
82PassRefPtr<BidiContext> BidiContext::copyStackRemovingUnicodeEmbeddingContexts()
83{
84    Vector<BidiContext*, 64> contexts;
85    for (BidiContext* iter = this; iter; iter = iter->parent()) {
86        if (iter->source() != FromUnicode)
87            contexts.append(iter);
88    }
89    ASSERT(contexts.size());
90
91    RefPtr<BidiContext> topContext = copyContextAndRebaselineLevel(contexts.last(), 0);
92    for (int i = contexts.size() - 1; i > 0; --i)
93        topContext = copyContextAndRebaselineLevel(contexts[i - 1], topContext.get());
94
95    return topContext.release();
96}
97
98bool operator==(const BidiContext& c1, const BidiContext& c2)
99{
100    if (&c1 == &c2)
101        return true;
102    if (c1.level() != c2.level() || c1.override() != c2.override() || c1.dir() != c2.dir() || c1.source() != c2.source())
103        return false;
104    if (!c1.parent())
105        return !c2.parent();
106    return c2.parent() && *c1.parent() == *c2.parent();
107}
108
109} // namespace WebCore
110