1/*
2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2006, 2007, 2008 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#ifndef BidiCharacterRun_h
23#define BidiCharacterRun_h
24
25#include "platform/text/BidiContext.h"
26#include "platform/text/TextDirection.h"
27
28namespace WebCore {
29
30struct BidiCharacterRun {
31    BidiCharacterRun(int start, int stop, BidiContext* context, WTF::Unicode::Direction dir)
32        : m_override(context->override())
33        , m_next(0)
34        , m_start(start)
35        , m_stop(stop)
36    {
37        ASSERT(m_start <= m_stop);
38        if (dir == WTF::Unicode::OtherNeutral)
39            dir = context->dir();
40
41        m_level = context->level();
42
43        // add level of run (cases I1 & I2)
44        if (m_level % 2) {
45            if (dir == WTF::Unicode::LeftToRight || dir == WTF::Unicode::ArabicNumber || dir == WTF::Unicode::EuropeanNumber)
46                m_level++;
47        } else {
48            if (dir == WTF::Unicode::RightToLeft)
49                m_level++;
50            else if (dir == WTF::Unicode::ArabicNumber || dir == WTF::Unicode::EuropeanNumber)
51                m_level += 2;
52        }
53    }
54
55    // BidiCharacterRun are allocated out of the rendering partition.
56    PLATFORM_EXPORT void* operator new(size_t);
57    PLATFORM_EXPORT void operator delete(void*);
58
59    int start() const { return m_start; }
60    int stop() const { return m_stop; }
61    unsigned char level() const { return m_level; }
62    bool reversed(bool visuallyOrdered) const { return m_level % 2 && !visuallyOrdered; }
63    bool dirOverride(bool visuallyOrdered) { return m_override || visuallyOrdered; }
64    TextDirection direction() const { return reversed(false) ? RTL : LTR; }
65
66    BidiCharacterRun* next() const { return m_next; }
67    void setNext(BidiCharacterRun* next) { m_next = next; }
68
69    // Do not add anything apart from bitfields until after m_next. See https://bugs.webkit.org/show_bug.cgi?id=100173
70    bool m_override : 1;
71    bool m_hasHyphen : 1; // Used by BidiRun subclass which is a layering violation but enables us to save 8 bytes per object on 64-bit.
72    unsigned char m_level;
73    BidiCharacterRun* m_next;
74    int m_start;
75    int m_stop;
76};
77
78} // namespace WebCore
79
80#endif // BidiCharacterRun_h
81