InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
textiterator.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Nat McCully
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #ifndef __TextIterator__
25 #define __TextIterator__
26 
27 //#include <iterator>
28 
29 #include "DataWrapper.h"
30 #include "WideString.h"
31 
32 class ITextModel;
33 
34 #ifdef PUBLIC_BUILD
35 #endif
36 
37 
38 
39 
40 class TextChunk
41 {
42 public:
43  TextChunk() : fTextModelPtr(nil), fLength(0), fStartingIndex(0)
44  {}
45 
46  TextChunk(const ITextModel* textModel, TextIndex index);
47  TextChunk(const TextChunk& rhs); //define this to work around metrowerks 5 inline bug
48 
49  ~TextChunk();
50 
51  void ForceContainTextIndex(TextIndex index);
52  bool16 Contains(TextIndex index)
53  { return index >= fStartingIndex && index < fStartingIndex + fLength; }
54  int32 Length() const
55  { return fLength; }
56  WideString::const_iterator begin() const
57  { return fWrapper.GetIteratorAt(0); }
58  WideString::const_iterator end() const
59  { return fWrapper.GetIteratorAt(Length()); }
60 
61  TextChunk& operator=(const TextChunk& o);
62  TextChunk& operator++ ()
63  {
64  ForceContainTextIndex(fStartingIndex + fLength);
65  return *this;
66  }
67  TextChunk& operator-- ()
68  {
69  ForceContainTextIndex(fStartingIndex - 1);
70  return *this;
71  }
72 
73  WideString::const_iterator IndexToIterator(TextIndex index, int32 &numCharsLeft) const
74  {
75  ASSERT(index >= fStartingIndex && index < fStartingIndex + fLength);
76  numCharsLeft = (fStartingIndex + fLength) - index;
77  return fWrapper.GetIteratorAt(index - fStartingIndex);
78  }
79  const UTF16TextChar* IndexToPointer(TextIndex index, int32 &numUTF16sLeft) const
80  {
81  ASSERT(index >= fStartingIndex && index < fStartingIndex + fLength);
82  int32 numChars = 0;
83  WideString::const_iterator iter = IndexToIterator(index, numChars);
84  numUTF16sLeft = (iter + numChars).PtrAt() - iter.PtrAt();
85  return iter.PtrAt();
86  }
87 
88  const ITextModel * GetTextModel() const
89  { return fTextModelPtr; }
90 
91  const WideString* GetData() const
92  { return fWrapper.GetData(); }
93 
94  friend class ComposeScannerCache; // since this holds a TextIterator but is on the same boss as the story, special access to the members is necessary.
95 
96 protected:
97  void Normalize(TextIndex chunkBegin);
98 
99 protected:
100  ITextModel *fTextModelPtr;
101  DataWrapper<textchar> fWrapper;
102  int32 fLength;
103  TextIndex fStartingIndex;
104 };
105 
106 
107 
108 
110  //: public std::iterator<random_access_iterator_tag, textchar, ptrdiff_t, const textchar*, const textchar&>
111 {
112 public:
113  typedef UTF32TextChar value_type;
114  typedef std::ptrdiff_t difference_type;
115  typedef const UTF32TextChar* pointer;
116  typedef const UTF32TextChar& reference;
117  typedef std::bidirectional_iterator_tag iterator_category;
118 
119  TextIterator(const ITextModel* textModel, TextIndex pos);
120  TextIterator(const WideString::const_iterator& stringIter, TextIndex pos);
121  TextIterator(const TextIterator&);
122 
123  TextIndex Position() const
124  { return fPosition; }
125  bool16 IsNull() const
126  { return fCurrent.PtrAt() == nil; }
127 
128  void AppendToStringAndIncrement(WideString * str, int32 numChars);
129 
130  TextIterator& operator=(const TextIterator& o);
131  TextIterator::value_type operator * () const
132  { ASSERT(fCurrent.PtrAt() != nil); return *fCurrent; }
133  WideString::const_iterator operator -> () const
134  { ASSERT(fCurrent.PtrAt() != nil); return fCurrent; }
135  TextIterator& operator ++ ();
136  TextIterator operator ++ (int)
137  { TextIterator tmp(*this); ++(*this); return tmp; }
138  TextIterator& operator -- ();
139  TextIterator operator -- (int)
140  { TextIterator tmp(*this); --(*this); return tmp; }
141  TextIterator& operator += (int32 n);
142  TextIterator operator + (int32 n) const
143  { return TextIterator(*this) += n; }
144  TextIterator& operator -= (int32 n);
145  TextIterator operator - (int32 n) const
146  { return TextIterator(*this) -= n; }
147  int32 operator - (const TextIterator& rhs) const
148  { return int32(fPosition - rhs.fPosition); }
149  TextIterator::value_type operator [] (int32 i) const
150  { TextIterator tmp(*this); tmp += i; return *tmp; }
151 
152  friend TextIterator operator + (int32 n, const TextIterator& rhs)
153  { return TextIterator(rhs) += n; }
154  friend bool operator ==(const TextIterator& x, const TextIterator& y)
155  { return x.fCurrent.PtrAt() == y.fCurrent.PtrAt(); }
156  friend bool operator !=(const TextIterator& x, const TextIterator& y)
157  { return x.fCurrent.PtrAt() != y.fCurrent.PtrAt(); }
158  friend bool operator < (const TextIterator& x, const TextIterator& y)
159  { return x.fPosition < y.fPosition; }
160  friend bool operator <=(const TextIterator& x, const TextIterator& y)
161  { return x.fPosition <= y.fPosition; }
162  friend bool operator > (const TextIterator& x, const TextIterator& y)
163  { return x.fPosition > y.fPosition; }
164  friend bool operator >=(const TextIterator& x, const TextIterator& y)
165  { return x.fPosition >= y.fPosition; }
166 
167  const ITextModel* QueryTextModel() const;
168 
169 #ifdef DEBUG
170  const ITextModel * GetTextModel() const
171  { return fChunk.GetTextModel(); }
172 #endif
173 
174  friend class ComposeScannerCache; // since this holds a TextIterator but is on the same boss as the story, special access to the members is necessary.
175 
176 private:
177  TextIterator() : fCurrent(nil, 0), fPosition(0) {}
178 
179 protected:
180  TextChunk fChunk;
182  TextIndex fPosition;
183 
184 };
185 
186 
187 #ifdef PUBLIC_BUILD
188 #endif
189 
190 #endif
191