opennurbs_textrun.h
1 
2 /* $NoKeywords: $ */
3 /*
4 //
5 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
6 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
7 // McNeel & Associates.
8 //
9 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
10 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
11 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
12 //
13 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
14 //
15 ////////////////////////////////////////////////////////////////
16 */
17 
18 #ifndef OPENNURBS_TEXTRUN_H_INCLUDED
19 #define OPENNURBS_TEXTRUN_H_INCLUDED
20 
21 class ON_CLASS ON_StackedText
22 {
23 public:
24  static const ON_StackedText Empty;
25 
26 public:
27  ON_StackedText() = default;
28  ~ON_StackedText();
29  ON_StackedText(const ON_StackedText& src);
30 
31  // Sets m_parent_run = nullptr.
32  // You must set m_parent_run after calling operator=().
33  ON_StackedText& operator=(const ON_StackedText& src);
34 
35  class ON_TextRun* m_top_run = nullptr;
36  class ON_TextRun* m_bottom_run = nullptr;
37  const ON_TextRun* m_parent_run = nullptr;
38  wchar_t m_separator = ON_wString::Slash;
39 
40  enum class StackStyle : unsigned char
41  {
42  kUnset = 0,
43  kHorizontalToScreen = 1,
44  kSlanted = 2,
45  };
46 
47  static ON_StackedText::StackStyle StackStyleFromUnsigned(
48  unsigned int stack_style_as_unsigned
49  );
50 
51 private:
52  friend class ON_TextRun;
53 
54  //bool WriteNested(
55  // unsigned int nested_depth,
56  // ON_BinaryArchive& archive
57  // ) const;
58 
59  //bool ReadNested(
60  // unsigned int nested_depth,
61  // ON_BinaryArchive& archive
62  // );
63 };
64 
65 // A range of text with all the same attributes
66 class ON_CLASS ON_TextRun
67 {
68 public:
69  static const ON_TextRun Empty;
70 
71  /*
72  Description:
73  ON_TextRun::NewTextRun() gets a text run from an efficiently managed pool.
74  Returns:
75  A pointer to a text run. (never nullptr).
76  */
77  static ON_TextRun* GetManagedTextRun();
78 
79 
80  /*
81  Description:
82  ON_TextRun::NewTextRun() gets a text run from an efficiently managed pool
83  and copies src
84  Returns:
85  A pointer to a text run. (never nullptr).
86  */
87  static ON_TextRun* GetManagedTextRun(
88  const ON_TextRun& src
89  );
90 
91  /*
92  Description:
93  Return a managed ON_TextRun.
94  */
95  static bool ReturnManagedTextRun(
96  ON_TextRun* managed_text_run
97  );
98 
99  /*
100  Returns:
101  True if the memory for this ON_TextRun is managed.
102  It was created by calling ON_TextRun::GetManagedTextRun(). If it is active,
103  then is must be deleted by calling ON_TextRun::ReturnManagedTextRun();
104  */
105  bool IsManagedTextRun() const;
106 
107  /*
108  Returns:
109  True if the memory for this ON_TextRun is managed and the text run is active.
110  It was created by calling ON_TextRun::GetManagedTextRun() and should be
111  deleted by calling ON_TextRun::ReturnManagedTextRun();
112  */
113  bool IsActiveManagedTextRun() const;
114 
115 public:
116  ON_TextRun() = default;
117  ~ON_TextRun();
118  ON_TextRun(const ON_TextRun& src);
119  ON_TextRun& operator=(const ON_TextRun& src);
120 
121 private:
122  ON_TextRun(bool bManagedTextRun);
123 
124 public:
125  enum class RunType : unsigned char
126  {
127  kNone = 0,
128  kText = 1,
129  kNewline = 2,
130  kSoftreturn = 3,
131  kParagraph = 4,
132  kColumn = 5,
133  kField = 6,
134  kFieldValue = 7,
135  kFontdef = 8,
136  kHeader = 9,
137  kFonttbl = 10,
138  kColortbl = 11,
139  };
140 
141  static ON_TextRun::RunType RunTypeFromUnsigned(
142  unsigned int run_type_as_unsigned
143  );
144 
145  enum class Stacked : unsigned char
146  {
147  kNone = 0,
148  kStacked = 1,
149  kTop = 2,
150  kBottom = 3
151  };
152 
153  static ON_TextRun::Stacked StackedFromUnsigned(
154  unsigned int stacked_as_unsigned
155  );
156 
157  enum class RunDirection : unsigned char
158  {
159  kLtR = 0,
160  kRtL = 1,
161  };
162 
163  static ON_TextRun::RunDirection RunDirectionFromUnsigned(
164  unsigned int run_direction_as_unsigned
165  );
166 
167 public:
168 
169  ON_SHA1_Hash TextRunContentHash() const;
170  ON_SHA1_Hash TextRunContentHash(
171  bool bEvaluateFields
172  ) const;
173 
174 
175  void Init(
176  const class ON_Font* managed_font,
177  double height,
178  double stackscale,
179  ON_Color color,
180  bool bold,
181  bool italic,
182  bool underlined,
183  bool strikethrough,
184  bool deletedisplay = true);
185 
186  bool IsText() const;
187  bool IsNewline() const;
188  bool IsColumn() const;
189  bool IsValid() const;
190 
191  RunType Type() const;
192  void SetType(ON_TextRun::RunType);
193  RunDirection FlowDirection() const;
194 
195  Stacked IsStacked() const;
196  void SetStacked(Stacked stacked);
197  void SetStackedOff();
198 
199  // Set or get the WCS model unit height of the text
200  // not including any annotatition scaling
201  double TextHeight() const;
202 
203  void SetTextHeight(double h);
204 
205  ON_Color Color() const;
206  void SetColor(ON_Color color);
207 
208  void SetFont(const ON_Font* font);
209  const ON_Font* Font() const;
210 
211  // bbox is stored as ON_BoundingBox, but is always 2d. z=0
212  const ON_BoundingBox& BoundingBox() const;
213  void SetBoundingBox(ON_2dPoint pmin, ON_2dPoint pmax);
214 
215  const ON_2dVector& Offset() const;
216  void SetOffset(ON_2dVector offset);
217 
218  const ON_2dVector& Advance() const;
219  void SetAdvance(ON_2dVector advance);
220 
221  // This returns the scale of m_height / HeightOfI.
222  // It doesn't take into account anything about annotation scaling
223  // This is the scale for converting ON_TextRun bounding boxes and
224  // offsets to basic model units
225  double HeightScale(const ON_Font* font) const;
226 
227  void SetStackFractionHeight(double stackscale);
228  double StackHeightFraction() const;
229  static double DefaultStackFractionHeight();
230 
231  //bool Write(
232  // ON_BinaryArchive&
233  // ) const;
234  //bool Read(
235  // ON_BinaryArchive&
236  // );
237 
238  void Get2dCorners(ON_2dPoint corners[4]) const;
239 
240 private:
241  static
242  void SetUnicodeString(ON__UINT32*& dest, size_t count, const ON__UINT32* cp);
243 
244 public:
245  void SetUnicodeString(size_t count, const ON__UINT32* cp);
246  static size_t CodepointCount(const ON__UINT32* cp);
247 
248  // Returns string in m_display_string, which may be the result of parsing text fields
249  void SetDisplayString(const wchar_t* str);
250 
251  // The display string is the TextString() with formulae evaluated.
252  const wchar_t* DisplayString() const;
253 
254  // Returns the string in m_text_string, which is a wchar_t version of the basic text for this run,
255  // and may contain unevaluated field formulae
256  const wchar_t* TextString() const;
257 
258  const ON__UINT32* UnicodeString() const;
259 
260  bool GetGlyphContours(
261  const ON_Font* text_font,
262  bool bSingleStrokeFont,
263  const ON_Xform& text_xform,
265  ) const;
266 
267  friend class ON_TextBuilder;
268 
269  // Wrap text to a specified width in model space
270  int WrapTextRun(
271  int call_count,
272  int start_char_offset,
273  double width,
274  double &y_offset,
275  double& currentwidth,
276  class ON_TextRunArray& newruns) const;
277 
278  const ON_Font* m_managed_font = nullptr; // Font used to draw, pick, or otherwise evaluate this
279  // ON_TextRun. This pointer is run-time only and must be
280  // set and point to a valid ON_Font for any font related
281  // operations to work.
282 
283 // ON_UUID m_Font_id = ON_nil_uuid;
284 private:
285  ON__UINT32* m_codepoints = nullptr; // Parsed text as UTF32
286 
287  // Set from m_codepoints when required.
288  // Always use TextString() to get this value
289  mutable ON_wString m_text_string = ON_wString::EmptyString;
290  // Parsed text as wchar_t*
291  // If this string is non-empty, it is valid and can be used
292  // instead of m_codepoints
293  // This string may have unevaluated field definitions - %<field_name>%
294 
295  mutable ON_wString m_display_string = ON_wString::EmptyString;
296  // Text to display when this is drawn.
297  // If this text has field definitions, m_display_string will have the
298  // evaluation results to display
299  // This string may change often if there are fields to evaluate.
300 
301  mutable ON_SHA1_Hash m_text_run_hash = ON_SHA1_Hash::ZeroDigest;
302  mutable ON_SHA1_Hash m_text_run_display_hash = ON_SHA1_Hash::ZeroDigest;
303 
304  ON_TextRun::Stacked m_text_stacked = ON_TextRun::Stacked::kNone; // 0: Normal text, 1: Run is stacked container, 2: Run is top of stacked fraction, 3: Run is bottom of stacked fraction
305 
306 private:
307  void Internal_ContentChanged() const;
308 public:
309  ON_StackedText* m_stacked_text = nullptr; // pointers to runs for the top and bottom parts
310 
311  ON_Color m_color = ON_Color::UnsetColor;
312 private:
315 
316 private:
317 
318  // This value of m_managed_status is not copied
319  // 0: Not managed
320  // 1: Managed by GetManagedTextRun() / ReturnManagedTextRun()
321  const unsigned char m_managed_status = 0;
322  unsigned char m_active_status = 0; // 0: active, 1: inactive managed text_run
323 
324 private:
325  double m_run_text_height = 1.0; // (ECS) text height in model units or page units
326 
327 public:
328  ON_2dVector m_offset = ON_2dVector::ZeroVector; // (ECS) offset to lower left of bounding box from ON_TextContent plane origin
329 
330  ON_2dVector m_advance = ON_2dVector::ZeroVector; // (ECS) distance and direction from m_offset to start of next run
331 
332 private:
333  ON_BoundingBox m_bbox = ON_BoundingBox::EmptyBoundingBox; // (ECS) 3d Bounding box oriented to ON_TextContent object's plane (z == 0) with run offset already included
335 public:
336  double m_height_scale = -1.0; // Font HeightOfI / text height - Converts from font units to model units or page units
337  double m_stackscale = 0.7; // fraction for scaling textheight in stacked text
339  // indent and margins are in model units or page units
340  // These apply to Paragraph type runs (m_type == rtParagraph)
341  double m_indent = 0.0; // First line indentation for this paragraph
342  double m_left_margin = 0.0; // left margin in formatting rect for this paragraph
343  double m_right_margin = 0.0; // right margin in formatting rect for this paragraph
344 
345  int m_line_index = -1; // line position in ON_TextContent
346 
347 private:
348  ON__UINT_PTR m_reserved=0;
349 
350 private:
351  friend class ON_StackedText;
352  //bool WriteNested(
353  // unsigned int nested_depth,
354  // ON_BinaryArchive& archive
355  // ) const;
356  //bool ReadNested(
357  // unsigned int nested_depth,
358  // ON_BinaryArchive& archive
359  // );
360 private:
361  void Internal_Destroy();
362  void Internal_CopyFrom(const ON_TextRun& src);
363 };
364 
365 
366 class ON_CLASS ON_TextRunArray : private ON_SimpleArray< ON_TextRun* >
367 {
368  //ON_TextRun*& AppendNew();
369  //void Append(int, ON_TextRun* const *);
370  //void Append(ON_TextRun* const);
371 
372 public:
373  static const ON_TextRunArray EmptyArray;
374 
375  ON_TextRunArray() = default;
376 
377  // Calls Destroy(true,true)
378  ~ON_TextRunArray();
379 
380  // Duplicate runs are managed text runs
381  ON_TextRunArray& operator=(const ON_TextRunArray& src);
382 
383  // Duplicate runs are managed text runs
384  ON_TextRunArray(const ON_TextRunArray& src);
385 
386 public:
387 
388  /*
389  Returns:
390  A hash of the information that determines the text content with evaluated fields.
391  */
392  ON_SHA1_Hash TextRunArrayContentHash() const;
393 
394  /*
395  Parameters:
396  bEvaluateFields - [in]
397  true - hash text with fields evaluated
398  false - hash text with fields unevaluated
399  Returns:
400  A hash of the information that determines the text content
401  without evaluating the fields.
402  */
403  ON_SHA1_Hash TextRunArrayContentHash(
404  bool bEvaluateFields
405  ) const;
406 
407  // Run must be a managed run or on the heap.
408  // The destructor will return managed runs and delete unmanaged runs.
409  void InsertRun(int i, ON_TextRun*& run);
410 
411  void RemoveRun(int i);
412 
413  // Run must be a managed run or on the heap.
414  // The destructor will return managed runs and delete unmanaged runs.
415  void AppendRun(ON_TextRun*& run);
416 
417  bool Get2dCorners(ON_2dPoint corners[4]) const;
418 
419  const ON_TextRun* operator[](int i) const;
420  ON_TextRun* operator[](int i);
421  int Count() const;
422  unsigned int UnsignedCount() const;
423  ON_TextRun*const* Array() const;
424 
425  /*
426  Parameters:
427  bReturnManagedRuns - [in]
428  True: Managed runs will be returned.
429  False: Caller must explicityly handle managed runs.
430  bDeleteUnmanagedRuns - [in]
431  True: Unmanaged runs are deleted.
432  False: Caller must explicityly handle unmanaged runs.
433  */
434  void SetTextHeight(double height);
435 
436 private:
437  void Internal_Destroy();
438  void Internal_CopyFrom(const ON_TextRunArray& src);
439 };
440 
441 
442 #endif
static const ON_StackedText Empty
Definition: opennurbs_textrun.h:24
static const ON_BoundingBox EmptyBoundingBox
Definition: opennurbs_bounding_box.h:28
static const wchar_t Slash
Definition: opennurbs_string.h:2036
A range of text with all the same attributes.
Definition: opennurbs_textrun.h:66
Definition: opennurbs_sha1.h:19
Definition: opennurbs_textrun.h:363
Definition: opennurbs_string.h:2020
Stacked
Definition: opennurbs_textrun.h:142
static const ON_Color UnsetColor
Definition: opennurbs_color.h:32
Definition: opennurbs_color.h:24
Definition: opennurbs_point.h:277
Definition: opennurbs_bounding_box.h:25
RunDirection
Definition: opennurbs_textrun.h:154
Definition: opennurbs_xform.h:28
An ON_Font is a face in a font family. It corresponds to a Windows LOGFONT, a .NET System...
Definition: opennurbs_font.h:225
StackStyle
Definition: opennurbs_textrun.h:40
RunType
Definition: opennurbs_textrun.h:122
Definition: opennurbs_textiterator.h:96
Definition: opennurbs_array.h:409
static const ON_wString EmptyString
Definition: opennurbs_string.h:2026
static const ON_TextRun Empty
Definition: opennurbs_textrun.h:69
static const ON_2dVector ZeroVector
Definition: opennurbs_point.h:852
static const ON_SHA1_Hash ZeroDigest
Definition: opennurbs_sha1.h:22
Definition: opennurbs_point.h:839
Definition: opennurbs_textrun.h:21