1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_STRING_BIDI_H
24 #define COMMON_STRING_BIDI_H
25 
26 #include "common/str.h"
27 #include "common/ustr.h"
28 #include "common/language.h"
29 
30 namespace Common {
31 
32 /**
33  * List of paragraph directions
34  */
35 enum BiDiParagraph {
36 	BIDI_PAR_ON = 0,
37 	BIDI_PAR_RTL = 1,
38 	BIDI_PAR_LTR = 2
39 };
40 
41 class UnicodeBiDiText {
42 private:
43 	uint32 *_log_to_vis_index; // from fribidi conversion
44 	uint32 *_vis_to_log_index; // from fribidi conversion
45 	void initWithU32String(const Common::U32String &str);
46 	Common::String bidiByLine(Common::String line, va_list args);
47 public:
48 	const Common::U32String logical; // original string, ordered logically
49 	Common::U32String visual; // from fribidi conversion, ordered visually
50 	uint32 _pbase_dir;
51 
52 	UnicodeBiDiText(const Common::U32String &str, BiDiParagraph dir = BIDI_PAR_ON);
53 	/* This constructor shouldn't be used outside of unicode-bidi.cpp file */
54 	UnicodeBiDiText(const Common::String &str, const Common::CodePage page, uint32 *pbase_dir);
55 	~UnicodeBiDiText();
56 
57 	/**
58 	 * Implicit conversion to U32String to get the visual representation.
59 	 */
60 	operator const U32String &() const { return visual; }
61 
62 	uint32 getVisualPosition(uint32 logicalPos) const;
63 	uint32 getLogicalPosition(uint32 visualPos) const;
size()64 	uint32 size() const { return logical.size(); }
65 };
66 
67 /* just call the constructor for convenience */
68 UnicodeBiDiText convertBiDiU32String(const U32String &input, BiDiParagraph dir = BIDI_PAR_ON);
69 String convertBiDiString(const String &input, const Common::Language lang, BiDiParagraph dir = BIDI_PAR_ON);
70 String convertBiDiString(const String &input, const Common::CodePage page, BiDiParagraph dir = BIDI_PAR_ON);
71 
72 // calls convertBiDiString for each line in isolation
73 String convertBiDiStringByLines(const String &input, const Common::CodePage page, BiDiParagraph dir = BIDI_PAR_ON);
74 
75 } // End of namespace Common
76 
77 #endif
78