Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SimpleTextImager.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Frameworks_Led_SimpleTextImager_h_
5#define _Stroika_Frameworks_Led_SimpleTextImager_h_ 1
6
7/*
8@MODULE: SimpleTextImager
9@DESCRIPTION:
10 <p>The SimpleTextImager module provides a very fast, lightweight text imaging implementation. This implementation
11 does not support wordwrapping (or multiple rows per line (Partition element)</p>
12
13 */
14
15#include "Stroika/Frameworks/StroikaPreComp.h"
16
17#include <limits.h> // for UINT_MAX
18#include <string.h>
19
20#include "PartitioningTextImager.h"
21
22namespace Stroika::Frameworks::Led {
23
24#if qStroika_Frameworks_Led_SupportGDI
25 DISABLE_COMPILER_MSC_WARNING_START (4250) // inherits via dominance warning
26
27 /*
28 @CLASS: SimpleTextImager
29 @BASES: @'PartitioningTextImager'
30 @DESCRIPTION:
31 <p>SimpleTextImager is a simple subclass of @'PartitioningTextImager'. NB: this is a change
32 from Led 2.2, when as an implementation detail, it subclassed from MultiRowTextImager.</p>
33 <p>SimpleTextImager assumes that all rows have the same height (specified by ReCalcRowHeight)
34 and that all lines (paragraphs) are displayed in a single row. It is these assumptions that allow
35 this class to display text with much less runtime overhead (both memory and time).</p>
36 <p>You should be able to assign an arbitrary partition object to a @'SimpleTextImager' via
37 the @'SimpleTextImager::SetPartition' function. And these partition objects should be sharable among
38 multiple SimpleTextImager's.</p>
39 <p>NB: Much of this class is subject to change (mostly simplication). This code was originally
40 based on the code for MultiRowTextImager, and as such, it has alot of generality built in
41 which isn't appropriate (like assuming rows all same height allows alot to be simplfied in
42 computations etc).</p>
43 <p>Unless you must, avoid counting too much on the internals and details of this class.</p>
44 */
45 class SimpleTextImager : public PartitioningTextImager {
46 private:
47 using inherited = PartitioningTextImager;
48
49 protected:
50 SimpleTextImager ();
51 virtual ~SimpleTextImager ();
52
53 private:
54 SimpleTextImager (const SimpleTextImager&) = delete;
55 nonvirtual void operator= (const SimpleTextImager&) = delete;
56
57 protected:
58 virtual void HookLosingTextStore () override;
59 nonvirtual void HookLosingTextStore_ ();
60 virtual void HookGainedNewTextStore () override;
61 nonvirtual void HookGainedNewTextStore_ ();
62
63 public:
64 nonvirtual void SetPartition (const PartitionPtr& partitionPtr);
65
66 private:
67 bool fICreatedPartition;
68
69 public:
70 virtual PartitionPtr MakeDefaultPartition () const override;
71
72 private:
73 class MyPartitionWatcher : public Partition::PartitionWatcher {
74 public:
75 nonvirtual void Init (PartitionPtr partition, SimpleTextImager* imager);
76 nonvirtual void UnInit (PartitionPtr partition);
77
78 public:
79 virtual void AboutToSplit (PartitionMarker* pm, size_t at, void** infoRecord) const noexcept override;
80 virtual void DidSplit (void* infoRecord) const noexcept override;
81 virtual void AboutToCoalece (PartitionMarker* pm, void** infoRecord) const noexcept override;
82 virtual void DidCoalece (void* infoRecord) const noexcept override;
83
84 private:
85 SimpleTextImager* fImager;
86 };
87
88 private:
89 friend class MyPartitionWatcher;
90 MyPartitionWatcher fMyPartitionWatcher;
91
92 public:
93 nonvirtual DistanceType GetRowHeight () const;
94
95 protected:
96 nonvirtual void InvalidateRowHeight ();
97 virtual DistanceType ReCalcRowHeight () const;
98
99 private:
100 DistanceType fRowHeight;
101
102 protected:
103 virtual DistanceType MeasureSegmentHeight (size_t from, size_t to) const override;
104 virtual DistanceType MeasureSegmentBaseLine (size_t from, size_t to) const override;
105
106 protected:
107 using PartitionMarker = Partition::PartitionMarker;
108
109 public:
110 /*
111 @CLASS: SimpleTextImager::RowReference
112 @DESCRIPTION:
113 <p>A utility class to represent a row. It is a struct with a parition marker, and a row number.
114 These things are NOT long-lived. And shouldn't be saved anyplace, as no attempt is made to keep
115 them automaticlly up to date as the text is modified.
116 They are just a convenient, short-hand way to navigate through rows of text.</p>
117 <p>NOTE - this RowReference stuff is just a design vestige from an earlier implementation. At some point,
118 this module should be rewritten/cleaned up to eliminate this (LGP - 2001-10-20).
119 */
120 class RowReference {
121 public:
122 RowReference (const RowReference& from);
123 RowReference (PartitionMarker* partitionMarker);
124
125 private:
126 RowReference (); // left undefined to assure never called...
127 public:
128 nonvirtual RowReference& operator= (const RowReference& rhs);
129
130 public:
131 nonvirtual PartitionMarker* GetPartitionMarker () const;
132
133 private:
134 PartitionMarker* fPartitionMarker;
135 };
136
137 // Row Reference support routines...
138 public:
139 nonvirtual bool GetNextRowReference (RowReference* adjustMeInPlace) const; // return true if there is a next, and false if at end
140 nonvirtual bool GetPreviousRowReference (RowReference* adjustMeInPlace) const; // return true if there is a previous, and false if at the beginning
141
142 // NB: if ith==1, that means do NOTHING - for convenience...
143 nonvirtual bool GetIthRowReferenceFromHere (RowReference* adjustMeInPlace, ptrdiff_t ith) const; // return true if there is an ith, and false if we run off end... (ith==0 implies no change, < 0 means go back)
144 nonvirtual RowReference GetIthRowReferenceFromHere (RowReference fromHere, ptrdiff_t ith) const; // ERROR if ith doesn't exist... (ith==0 implies no change, < 0 means go back)
145 nonvirtual RowReference GetIthRowReference (size_t ith) const; // ERROR if ith doesn't exist...(1 th is first row)
146
147 nonvirtual size_t GetRowNumber (RowReference rowRef) const; // Use of row numbers is discouraged, but this routine
148 // can be helpful in implementing those APIs anyhow
149
150 // Count the # of rows from one rowreference to the other (order doesn't matter)
151 nonvirtual size_t CountRowDifference (RowReference lhs, RowReference rhs) const;
152
153 /*
154 * Window/Scrolling support.
155 */
156 public:
157 virtual size_t GetTopRowInWindow () const override;
158 virtual size_t GetTotalRowsInWindow () const override;
159 virtual size_t GetLastRowInWindow () const override;
160 virtual void SetTopRowInWindow (size_t newTopRow) override;
161 virtual size_t GetMarkerPositionOfStartOfWindow () const override;
162 virtual size_t GetMarkerPositionOfEndOfWindow () const override;
163 virtual size_t GetMarkerPositionOfStartOfLastRowOfWindow () const override;
164 virtual ptrdiff_t CalculateRowDeltaFromCharDeltaFromTopOfWindow (long deltaChars) const override;
165 virtual ptrdiff_t CalculateCharDeltaFromRowDeltaFromTopOfWindow (ptrdiff_t deltaRows) const override;
166 virtual void ScrollByIfRoom (ptrdiff_t downByRows); // if downBy negative then up
167 // OK to ask to scroll further
168 // than allowed - return true
169 // if any scrolling (not necesarily
170 // same amont requested) done
171 public:
172 virtual void ScrollSoShowing (size_t markerPos, size_t andTryToShowMarkerPos = 0) override;
173
174 protected:
175 nonvirtual RowReference GetTopRowReferenceInWindow () const;
176 nonvirtual RowReference GetLastRowReferenceInWindow () const;
177 virtual void SetTopRowInWindow (RowReference row);
178
179 protected:
180 nonvirtual void SetTopRowInWindow_ (RowReference row); // just sets the fields without any hook functions
181 // getting called. This is important sometimes when
182 // it would be unsafe for subclasses to get a chance
183 // to call methods while our data structures are not
184 // not completely up-to-date.
185
186 protected:
187 virtual void AssureWholeWindowUsedIfNeeded () override;
188
189 public:
190 virtual DistanceType ComputeMaxHScrollPos () const override;
191
192 public:
193 virtual Led_Rect GetCharLocation (size_t afterPosition) const override;
194 virtual size_t GetCharAtLocation (const Led_Point& where) const override;
195 virtual Led_Rect GetCharWindowLocation (size_t afterPosition) const override;
196 virtual size_t GetCharAtWindowLocation (const Led_Point& where) const override;
197
198 public:
199 virtual size_t GetStartOfRow (size_t rowNumber) const override;
200 virtual size_t GetStartOfRowContainingPosition (size_t charPosition) const override;
201 virtual size_t GetEndOfRow (size_t rowNumber) const override;
202 virtual size_t GetEndOfRowContainingPosition (size_t charPosition) const override;
203 virtual size_t GetRealEndOfRow (size_t rowNumber) const override;
204 virtual size_t GetRealEndOfRowContainingPosition (size_t charPosition) const override;
205 virtual size_t GetRowContainingPosition (size_t charPosition) const override;
206 virtual size_t GetRowCount () const override;
207 virtual Led_Rect GetCharLocationRowRelativeByPosition (size_t afterPosition, size_t positionOfTopRow, size_t maxRowsToCheck) const override;
208
209 public:
210 nonvirtual size_t GetStartOfRow (RowReference row) const;
211 nonvirtual size_t GetEndOfRow (RowReference row) const;
212 nonvirtual size_t GetRealEndOfRow (RowReference row) const;
213 nonvirtual RowReference GetRowReferenceContainingPosition (size_t charPosition) const;
214 nonvirtual size_t GetRowLength (RowReference row) const;
215
216 public:
217 virtual DistanceType GetRowHeight (size_t rowNumber) const override;
218
219 public:
220 virtual DistanceType GetRowRelativeBaselineOfRowContainingPosition (size_t charPosition) const override;
221
222 public:
223 nonvirtual DistanceType GetHeightOfRows (size_t startingRow, size_t rowCount) const;
224 nonvirtual DistanceType GetHeightOfRows (RowReference startingRow, size_t rowCount) const;
225
226 public:
227 virtual void GetStableTypingRegionContaingMarkerRange (size_t fromMarkerPos, size_t toMarkerPos, size_t* expandedFromMarkerPos,
228 size_t* expandedToMarkerPos) const override;
229
230 public:
231 virtual void Draw (const Led_Rect& subsetToDraw, bool printing) override;
232
233 public:
234 virtual void DrawPartitionElement (PartitionMarker* pm, size_t startSubRow, size_t maxSubRow, Tablet* tablet, OffscreenTablet* offscreenTablet,
235 bool printing, const Led_Rect& subsetToDraw, Led_Rect* remainingDrawArea, size_t* rowsDrawn);
236
237 protected:
238 virtual Led_Rect GetCharLocationRowRelative (size_t afterPosition, RowReference topRow, size_t maxRowsToCheck = UINT_MAX) const;
239 virtual size_t GetCharAtLocationRowRelative (const Led_Point& where, RowReference topRow, size_t maxRowsToCheck = UINT_MAX) const;
240
241 public:
242 nonvirtual DistanceType GetInterLineSpace () const;
243 nonvirtual void SetInterLineSpace (DistanceType interlineSpace);
244
245 private:
246 DistanceType fInterlineSpace;
247
248 public:
249 virtual DistanceType GetInterLineSpace (PartitionMarker* pm) const;
250 virtual void ChangedInterLineSpace (PartitionMarker* pm);
251
252 // Hook to invalidate cached info based on fontmetrics
253 public:
254 virtual void SetDefaultFont (const IncrementalFontSpecification& defaultFont) override;
255
256 // To assure our top-line scroll info not left corrupt...
257 protected:
258 virtual void DidUpdateText (const UpdateInfo& updateInfo) noexcept override;
259
260 // override to invalidate caches.
261 public:
262 virtual void SetWindowRect (const Led_Rect& windowRect) override;
263
264 protected:
265 virtual void InvalidateAllCaches () override;
266
267 private:
268 nonvirtual RowReference AdjustPotentialTopRowReferenceSoWholeWindowUsed (const RowReference& potentialTopRow);
269 nonvirtual bool PositionWouldFitInWindowWithThisTopRow (size_t markerPos, const RowReference& newTopRow);
270
271 private:
272 PartitionMarker* fTopLinePartitionMarkerInWindow;
273
274 // Support for GetTotalRowsInWindow
275 //
276 // Override ComputeRowsThatWouldFitInWindowWithTopRow () to change the policy of how we
277 // pack rows into a window
278 private:
279 mutable size_t fTotalRowsInWindow; // zero means invalid cached - fill cache on call to GetTotalRowsInWindow
280 protected:
281 nonvirtual size_t GetTotalRowsInWindow_ () const;
282 nonvirtual void InvalidateTotalRowsInWindow ();
283 virtual size_t ComputeRowsThatWouldFitInWindowWithTopRow (const RowReference& newTopRow) const;
284
285 protected:
286 virtual bool ContainsMappedDisplayCharacters (const Led_tChar* text, size_t nTChars) const override;
287 virtual size_t RemoveMappedDisplayCharacters (Led_tChar* copyText, size_t nTChars) const override;
288 };
289 DISABLE_COMPILER_MSC_WARNING_END (4250) // inherits via dominance warning
290#endif
291
292}
293
294/*
295 ********************************************************************************
296 ***************************** Implementation Details ***************************
297 ********************************************************************************
298 */
299#include "SimpleTextImager.inl"
300
301#endif /*_Stroika_Frameworks_Led_SimpleTextImager_h_*/