/*
 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2003, 2004, 2006, 2007, 2008 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.
 *
 */

#ifndef BidiCharacterRun_h
#define BidiCharacterRun_h

#include "platform/text/BidiContext.h"
#include "platform/text/TextDirection.h"

namespace blink {

struct BidiCharacterRun {
  BidiCharacterRun(bool override,
                   unsigned char level,
                   int start,
                   int stop,
                   WTF::Unicode::CharDirection dir,
                   WTF::Unicode::CharDirection override_dir)
      : override_(override),
        level_(level),
        next_(0),
        start_(start),
        stop_(stop) {
    DCHECK_LE(start_, stop_);
    if (dir == WTF::Unicode::kOtherNeutral)
      dir = override_dir;

    level_ = level;

    // add level of run (cases I1 & I2)
    if (level_ % 2) {
      if (dir == WTF::Unicode::kLeftToRight ||
          dir == WTF::Unicode::kArabicNumber ||
          dir == WTF::Unicode::kEuropeanNumber)
        level_++;
    } else {
      if (dir == WTF::Unicode::kRightToLeft)
        level_++;
      else if (dir == WTF::Unicode::kArabicNumber ||
               dir == WTF::Unicode::kEuropeanNumber)
        level_ += 2;
    }
  }

  BidiCharacterRun(int start, int stop, unsigned char level)
      : override_(false), level_(level), next_(0), start_(start), stop_(stop) {}

  // BidiCharacterRun are allocated out of the rendering partition.
  PLATFORM_EXPORT void* operator new(size_t);
  PLATFORM_EXPORT void operator delete(void*);

  int Start() const { return start_; }
  int Stop() const { return stop_; }
  unsigned char Level() const { return level_; }
  bool Reversed(bool visually_ordered) const {
    return level_ % 2 && !visually_ordered;
  }
  bool DirOverride(bool visually_ordered) {
    return override_ || visually_ordered;
  }
  TextDirection Direction() const {
    return Reversed(false) ? TextDirection::kRtl : TextDirection::kLtr;
  }

  BidiCharacterRun* Next() const { return next_; }
  void SetNext(BidiCharacterRun* next) { next_ = next; }

  // Do not add anything apart from bitfields until after m_next. See
  // https://bugs.webkit.org/show_bug.cgi?id=100173
  bool override_ : 1;
  // Used by BidiRun subclass which is a layering violation but enables us to
  // save 8 bytes per object on 64-bit.
  bool has_hyphen_ : 1;
  unsigned char level_;
  BidiCharacterRun* next_;
  int start_;
  int stop_;
};

}  // namespace blink

#endif  // BidiCharacterRun_h
