1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Peter Bartok [email protected]
//
//
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Text;
using System.Text;
namespace ShiftUI
{
internal class Line : ICloneable, IComparable
{
#region Local Variables
internal Document document;
// Stuff that matters for our line
internal StringBuilder text; // Characters for the line
internal float[] widths; // Width of each character; always one larger than text.Length
internal int space; // Number of elements in text and widths
internal int line_no; // Line number
internal LineTag tags; // Tags describing the text
internal int offset; // Baseline can be on the X or Y axis depending if we are in multiline mode or not
internal int height; // Height of the line (height of tallest tag)
internal int ascent; // Ascent of the line (ascent of the tallest tag)
internal HorizontalAlignment alignment; // Alignment of the line
internal int align_shift; // Pixel shift caused by the alignment
internal int indent; // Left indent for the first line
internal int hanging_indent; // Hanging indent (left indent for all but the first line)
internal int right_indent; // Right indent for all lines
internal LineEnding ending;
// Stuff that's important for the tree
internal Line parent; // Our parent line
internal Line left; // Line with smaller line number
internal Line right; // Line with higher line number
internal LineColor color; // We're doing a black/red tree. this is the node color
static int DEFAULT_TEXT_LEN = 0; //
internal bool recalc; // Line changed
private static Hashtable kerning_fonts = new Hashtable (); // record which fonts use kerning
#endregion // Local Variables
#region Constructors
internal Line (Document document, LineEnding ending)
{
this.document = document;
color = LineColor.Red;
left = null;
right = null;
parent = null;
text = null;
recalc = true;
alignment = document.alignment;
this.ending = ending;
}
internal Line (Document document, int LineNo, string Text, Font font, Color color, LineEnding ending) : this (document, ending)
{
space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
text = new StringBuilder (Text, space);
line_no = LineNo;
this.ending = ending;
widths = new float[space + 1];
tags = new LineTag(this, 1);
tags.Font = font;
tags.Color = color;
}
internal Line (Document document, int LineNo, string Text, HorizontalAlignment align, Font font, Color color, LineEnding ending) : this(document, ending)
{
space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
text = new StringBuilder (Text, space);
line_no = LineNo;
this.ending = ending;
alignment = align;
widths = new float[space + 1];
tags = new LineTag(this, 1);
tags.Font = font;
tags.Color = color;
}
internal Line (Document document, int LineNo, string Text, LineTag tag, LineEnding ending) : this(document, ending)
{
space = Text.Length > DEFAULT_TEXT_LEN ? Text.Length+1 : DEFAULT_TEXT_LEN;
text = new StringBuilder (Text, space);
this.ending = ending;
line_no = LineNo;
widths = new float[space + 1];
tags = tag;
}
#endregion // Constructors
#region Internal Properties
internal HorizontalAlignment Alignment {
get { return alignment; }
set {
if (alignment != value) {
alignment = value;
recalc = true;
}
}
}
internal int HangingIndent {
get { return hanging_indent; }
set {
hanging_indent = value;
recalc = true;
}
}
// UIA: Method used via reflection in TextRangeProvider
internal int Height {
get { return height; }
set { height = value; }
}
internal int Indent {
get { return indent; }
set {
indent = value;
recalc = true;
}
}
internal int LineNo {
get { return line_no; }
set { line_no = value; }
}
internal int RightIndent {
get { return right_indent; }
set {
right_indent = value;
recalc = true;
}
}
// UIA: Method used via reflection in TextRangeProvider
internal int Width {
get {
int res = (int) widths [text.Length];
return res;
}
}
internal string Text {
get { return text.ToString(); }
set {
int prev_length = text.Length;
text = new StringBuilder(value, value.Length > DEFAULT_TEXT_LEN ? value.Length + 1 : DEFAULT_TEXT_LEN);
if (text.Length > prev_length)
Grow (text.Length - prev_length);
}
}
// UIA: Method used via reflection in TextRangeProvider
internal int X {
get {
if (document.multiline)
return align_shift;
return offset + align_shift;
}
}
// UIA: Method used via reflection in TextRangeProvider
internal int Y {
get {
if (!document.multiline)
return document.top_margin;
return document.top_margin + offset;
}
}
#endregion // Internal Properties
#region Internal Methods
/// <summary>
/// Builds a simple code to record which tags are links and how many tags
/// used to compare lines before and after to see if the scan for links
/// process has changed anything.
/// </summary>
internal void LinkRecord (StringBuilder linkRecord)
{
LineTag tag = tags;
while (tag != null) {
if (tag.IsLink)
linkRecord.Append ("L");
else
linkRecord.Append ("N");
tag = tag.Next;
}
}
/// <summary>
/// Clears all link properties from tags
/// </summary>
internal void ClearLinks ()
{
LineTag tag = tags;
while (tag != null) {
tag.IsLink = false;
tag = tag.Next;
}
}
public void DeleteCharacters(int pos, int count)
{
LineTag tag;
bool streamline = false;
// Can't delete more than the line has
if (pos >= text.Length)
return;
// Find the first tag that we are deleting from
tag = FindTag (pos + 1);
// Remove the characters from the line
text.Remove (pos, count);
if (tag == null)
return;
// Check if we're crossing tag boundaries
if ((pos + count) > (tag.Start + tag.Length - 1)) {
int left;
// We have to delete cross tag boundaries
streamline = true;
left = count;
left -= tag.Start + tag.Length - pos - 1;
tag = tag.Next;
// Update the start of each tag
while ((tag != null) && (left > 0)) {
// Cache tag.Length as is will be indireclty modified
// by changes to tag.Start
int tag_length = tag.Length;
tag.Start -= count - left;
if (tag_length > left) {
left = 0;
} else {
left -= tag_length;
tag = tag.Next;
}
}
} else {
// We got off easy, same tag
if (tag.Length == 0)
streamline = true;
}
// Delete empty orphaned tags at the end
LineTag walk = tag;
while (walk != null && walk.Next != null && walk.Next.Length == 0) {
LineTag t = walk;
walk.Next = walk.Next.Next;
if (walk.Next != null)
walk.Next.Previous = t;
walk = walk.Next;
}
// Adjust the start point of any tags following
if (tag != null) {
tag = tag.Next;
while (tag != null) {
tag.Start -= count;
tag = tag.Next;
}
}
recalc = true;
if (streamline)
Streamline (document.Lines);
}
// This doesn't do exactly what you would think, it just pulls off the \n part of the ending
internal void DrawEnding (Graphics dc, float y)
{
if (document.multiline)
return;
LineTag last = tags;
while (last.Next != null)
last = last.Next;
string end_str = null;
switch (document.LineEndingLength (ending)) {
case 0:
return;
case 1:
end_str = "\u0013";
break;
case 2:
end_str = "\u0013\u0013";
break;
case 3:
end_str = "\u0013\u0013\u0013";
break;
}
TextBoxTextRenderer.DrawText (dc, end_str, last.Font, last.Color, X + widths [TextLengthWithoutEnding ()] - document.viewport_x + document.OffsetX, y, true);
}
/// <summary> Find the tag on a line based on the character position, pos is 0-based</summary>
internal LineTag FindTag (int pos)
{
LineTag tag;
if (pos == 0)
return tags;
tag = this.tags;
if (pos >= text.Length)
pos = text.Length - 1;
while (tag != null) {
if (((tag.Start - 1) <= pos) && (pos <= (tag.Start + tag.Length - 1)))
return LineTag.GetFinalTag (tag);
tag = tag.Next;
}
return null;
}
public override int GetHashCode ()
{
return base.GetHashCode ();
}
// Get the tag that contains this x coordinate
public LineTag GetTag (int x)
{
LineTag tag = tags;
// Coord is to the left of the first character
if (x < tag.X)
return LineTag.GetFinalTag (tag);
// All we have is a linked-list of tags, so we have
// to do a linear search. But there shouldn't be
// too many tags per line in general.
while (true) {
if (x >= tag.X && x < (tag.X + tag.Width))
return tag;
if (tag.Next != null)
tag = tag.Next;
else
return LineTag.GetFinalTag (tag);
}
}
// Make sure we always have enoughs space in text and widths
internal void Grow (int minimum)
{
int length;
float[] new_widths;
length = text.Length;
if ((length + minimum) > space) {
// We need to grow; double the size
if ((length + minimum) > (space * 2)) {
new_widths = new float[length + minimum * 2 + 1];
space = length + minimum * 2;
} else {
new_widths = new float[space * 2 + 1];
space *= 2;
}
widths.CopyTo (new_widths, 0);
widths = new_widths;
}
}
public void InsertString (int pos, string s)
{
InsertString (pos, s, FindTag (pos));
}
// Inserts a string at the given position
public void InsertString (int pos, string s, LineTag tag)
{
int len = s.Length;
// Insert the text into the StringBuilder
text.Insert (pos, s);
// Update the start position of every tag after this one
tag = tag.Next;
while (tag != null) {
tag.Start += len;
tag = tag.Next;
}
// Make sure we have room in the widths array
Grow (len);
// This line needs to be recalculated
recalc = true;
}
/// <summary>
/// Go through all tags on a line and recalculate all size-related values;
/// returns true if lineheight changed
/// </summary>
internal bool RecalculateLine (Graphics g, Document doc)
{
return RecalculateLine (g, doc, kerning_fonts.ContainsKey (tags.Font.GetHashCode ()));
}
private bool RecalculateLine (Graphics g, Document doc, bool handleKerning)
{
LineTag tag;
int pos;
int len;
SizeF size;
float w;
int prev_offset;
bool retval;
bool wrapped;
Line line;
int wrap_pos;
int prev_height;
int prev_ascent;
pos = 0;
len = this.text.Length;
tag = this.tags;
prev_offset = this.offset; // For drawing optimization calculations
prev_height = this.height;
prev_ascent = this.ascent;
this.height = 0; // Reset line height
this.ascent = 0; // Reset the ascent for the line
tag.Shift = 0; // Reset shift (which should be stored as pixels, not as points)
if (ending == LineEnding.Wrap)
widths[0] = document.left_margin + hanging_indent;
else
widths[0] = document.left_margin + indent;
this.recalc = false;
retval = false;
wrapped = false;
wrap_pos = 0;
while (pos < len) {
while (tag.Length == 0) { // We should always have tags after a tag.length==0 unless len==0
//tag.Ascent = 0;
tag.Shift = (tag.Line.ascent - tag.Ascent) / 72;
tag = tag.Next;
}
// kerning is a problem. The original code in this method assumed that the
// width of a string equals the sum of the widths of its characters. This is
// not true when kerning takes place during the display process. Since it's
// impossible to find out easily whether a font does kerning, and with which
// characters, we just detect that kerning must have happened and use a slower
// (but accurate) measurement for those fonts henceforth. Without handling
// kerning, many fonts for English become unreadable during typing for many
// input strings, and text in many other languages is even worse trying to
// type in TextBoxes.
// See https://bugzilla.xamarin.com/show_bug.cgi?id=26478 for details.
float newWidth;
if (handleKerning && !Char.IsWhiteSpace(text[pos]))
{
// MeasureText doesn't measure trailing spaces, so we do the best we can for those
// in the else branch.
size = TextBoxTextRenderer.MeasureText (g, text.ToString (0, pos + 1), tag.Font);
newWidth = widths[0] + size.Width;
}
else
{
size = tag.SizeOfPosition (g, pos);
w = size.Width;
newWidth = widths[pos] + w;
}
if (Char.IsWhiteSpace (text[pos]))
wrap_pos = pos + 1;
if (doc.wrap) {
if ((wrap_pos > 0) && (wrap_pos != len) && (newWidth + 5) > (doc.viewport_width - this.right_indent)) {
// Make sure to set the last width of the line before wrapping
widths[pos + 1] = newWidth;
pos = wrap_pos;
len = text.Length;
doc.Split (this, tag, pos);
ending = LineEnding.Wrap;
len = this.text.Length;
retval = true;
wrapped = true;
} else if (pos > 1 && newWidth > (doc.viewport_width - this.right_indent)) {
// No suitable wrap position was found so break right in the middle of a word
// Make sure to set the last width of the line before wrapping
widths[pos + 1] = newWidth;
doc.Split (this, tag, pos);
ending = LineEnding.Wrap;
len = this.text.Length;
retval = true;
wrapped = true;
}
}
// Contract all wrapped lines that follow back into our line
if (!wrapped) {
pos++;
widths[pos] = newWidth;
if (pos == len) {
line = doc.GetLine (this.line_no + 1);
if ((line != null) && (ending == LineEnding.Wrap || ending == LineEnding.None)) {
// Pull the two lines together
doc.Combine (this.line_no, this.line_no + 1);
len = this.text.Length;
retval = true;
}
}
}
if (pos == (tag.Start - 1 + tag.Length)) {
// We just found the end of our current tag
tag.Height = tag.MaxHeight ();
// Check if we're the tallest on the line (so far)
if (tag.Height > this.height)
this.height = tag.Height; // Yep; make sure the line knows
if (tag.Ascent > this.ascent) {
LineTag t;
// We have a tag that has a taller ascent than the line;
t = tags;
while (t != null && t != tag) {
t.Shift = (tag.Ascent - t.Ascent) / 72;
t = t.Next;
}
// Save on our line
this.ascent = tag.Ascent;
} else {
tag.Shift = (this.ascent - tag.Ascent) / 72;
}
tag = tag.Next;
if (tag != null) {
tag.Shift = 0;
wrap_pos = pos;
}
}
}
var fullText = text.ToString();
if (!handleKerning && fullText.Length > 1 && !wrapped)
{
// Check whether kerning takes place for this string and font.
var realSize = TextBoxTextRenderer.MeasureText(g, fullText, tags.Font);
float realWidth = realSize.Width + widths[0];
// MeasureText ignores trailing whitespace, so we will too at this point.
int length = fullText.TrimEnd().Length;
float sumWidth = widths[length];
if (realWidth != sumWidth)
{
kerning_fonts.Add(tags.Font.GetHashCode (), true);
// Using a slightly incorrect width this time around isn't that bad. All that happens
// is that the cursor is a pixel or two off until the next character is typed. It's
// the accumulation of pixel after pixel that causes display problems.
}
}
while (tag != null) {
tag.Shift = (tag.Line.ascent - tag.Ascent) / 72;
tag = tag.Next;
}
if (this.height == 0) {
this.height = tags.Font.Height;
tags.Height = this.height;
tags.Shift = 0;
}
if (prev_offset != offset || prev_height != this.height || prev_ascent != this.ascent)
retval = true;
return retval;
}
/// <summary>
/// Recalculate a single line using the same char for every character in the line
/// </summary>
internal bool RecalculatePasswordLine (Graphics g, Document doc)
{
LineTag tag;
int pos;
int len;
float w;
bool ret;
pos = 0;
len = this.text.Length;
tag = this.tags;
ascent = 0;
tag.Shift = 0;
this.recalc = false;
widths[0] = document.left_margin + indent;
w = TextBoxTextRenderer.MeasureText (g, doc.password_char, tags.Font).Width;
if (this.height != (int)tag.Font.Height)
ret = true;
else
ret = false;
this.height = (int)tag.Font.Height;
tag.Height = this.height;
this.ascent = tag.Ascent;
while (pos < len) {
pos++;
widths[pos] = widths[pos - 1] + w;
}
return ret;
}
internal void Streamline (int lines)
{
LineTag current;
LineTag next;
current = this.tags;
next = current.Next;
//
// Catch what the loop below wont; eliminate 0 length
// tags, but only if there are other tags after us
// We only eliminate text tags if there is another text tag
// after it. Otherwise we wind up trying to type on picture tags
//
while ((current.Length == 0) && (next != null) && (next.IsTextTag)) {
tags = next;
tags.Previous = null;
current = next;
next = current.Next;
}
if (next == null)
return;
while (next != null) {
// Take out 0 length tags unless it's the last tag in the document
if (current.IsTextTag && next.Length == 0 && next.IsTextTag) {
if ((next.Next != null) || (line_no != lines)) {
current.Next = next.Next;
if (current.Next != null) {
current.Next.Previous = current;
}
next = current.Next;
continue;
}
}
if (current.Combine (next)) {
next = current.Next;
continue;
}
current = current.Next;
next = current.Next;
}
}
internal int TextLengthWithoutEnding ()
{
return text.Length - document.LineEndingLength (ending);
}
internal string TextWithoutEnding ()
{
return text.ToString (0, text.Length - document.LineEndingLength (ending));
}
#endregion // Internal Methods
#region Administrative
public object Clone ()
{
Line clone;
clone = new Line (document, ending);
clone.text = text;
if (left != null)
clone.left = (Line)left.Clone();
if (left != null)
clone.left = (Line)left.Clone();
return clone;
}
internal object CloneLine ()
{
Line clone;
clone = new Line (document, ending);
clone.text = text;
return clone;
}
public int CompareTo (object obj)
{
if (obj == null)
return 1;
if (! (obj is Line))
throw new ArgumentException("Object is not of type Line", "obj");
if (line_no < ((Line)obj).line_no)
return -1;
else if (line_no > ((Line)obj).line_no)
return 1;
else
return 0;
}
public override bool Equals (object obj)
{
if (obj == null)
return false;
if (!(obj is Line))
return false;
if (obj == this)
return true;
if (line_no == ((Line)obj).line_no)
return true;
return false;
}
public override string ToString()
{
return string.Format ("Line {0}", line_no);
}
#endregion // Administrative
}
}
|