blob: 86e7f37b1720aebd558d5392b9bcd3208bf82c63 [file] [log] [blame]
// Copyright 2014 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc.
#include <stdint.h>
#include <vector>
#include "core/fxcrt/widestring.h"
// Processes characters and group them into segments based on text direction.
class CFX_BidiChar {
enum class Direction { kNeutral, kLeft, kRight };
struct Segment {
int32_t start; // Start position.
int32_t count; // Character count.
Direction direction; // Segment direction.
// Append a character and classify it as left, right, or neutral.
// Returns true if the character has a different direction than the
// existing direction to indicate there is a segment to process.
bool AppendChar(wchar_t wch);
// Call this after the last character has been appended. AppendChar()
// must not be called after this.
// Returns true if there is still a segment to process.
bool EndChar();
// Call after a change in direction is indicated by the above to get
// information about the segment to process.
const Segment& GetSegmentInfo() const { return m_LastSegment; }
void StartNewSegment(CFX_BidiChar::Direction direction);
Segment m_CurrentSegment;
Segment m_LastSegment;
class CFX_BidiString {
using const_iterator = std::vector<CFX_BidiChar::Segment>::const_iterator;
explicit CFX_BidiString(const WideString& str);
// Overall direction is always LEFT or RIGHT, never NEUTRAL.
CFX_BidiChar::Direction OverallDirection() const;
// Force the overall direction to be R2L regardless of what was detected.
void SetOverallDirectionRight();
const_iterator begin() const { return m_Order.begin(); }
const_iterator end() const { return m_Order.end(); }
const WideString& m_Str;
std::vector<CFX_BidiChar::Segment> m_Order;
CFX_BidiChar::Direction m_eOverallDirection = CFX_BidiChar::Direction::kLeft;