-
Notifications
You must be signed in to change notification settings - Fork 0
/
QFont.cs
1030 lines (769 loc) · 34.2 KB
/
QFont.cs
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
namespace QuickFont
{
public class QFont : IDisposable
{
private IFontRenderer renderer;
//private QFontRenderOptions options = new QFontRenderOptions();
private Stack<QFontRenderOptions> optionsStack = new Stack<QFontRenderOptions>();
private QFontData fontData;
private FontLoadDescription fontLoadDescription;
public QFontRenderOptions Options
{
get {
if (optionsStack.Count == 0)
{
optionsStack.Push(new QFontRenderOptions());
}
return optionsStack.Peek() ;
}
private set { //not sure if we should even allow this...
optionsStack.Pop();
optionsStack.Push(value);
}
}
#region Constructors and font builders
/// <summary>
/// Reloads the font using the original loader/builder options. This may be useful if the underlying
/// files change, or, more commonly, if the "TransformToCurrentOrthogProjection" option was used when
/// creating the font, and the orthog projection has since changed (e.g. resizing the window). This
/// will do nothing for fonts created directly from a Font object.
/// </summary>
public void Reload()
{
switch (fontLoadDescription.Method)
{
case FontLoadMethod.QFontFile:
{
fontData.Dispose(); //dispose old data
LoadQFontFromQFontFile(fontLoadDescription);
break;
}
case FontLoadMethod.FontFile:
{
fontData.Dispose(); //dispose old data
LoadQFontFromFontFile(fontLoadDescription);
break;
}
}
}
private void LoadTextures(QFontData fdata)
{
if (fdata.dropShadow != null) LoadTextures(fdata.dropShadow);
foreach (var page in fdata.Pages)
{
page.Texture = renderer.CreateTexture(page.BitmapData);
page.Free();
}
}
private QFont(IFontRenderer renderer) { this.renderer = renderer; }
internal QFont(IFontRenderer renderer, QFontData fontData): this(renderer) { this.fontData = fontData; }
public QFont(IFontRenderer renderer, Font font, QFontBuilderConfiguration config = null): this(renderer)
{
if (config == null)
config = new QFontBuilderConfiguration();
fontData = BuildFont(font, config, null);
LoadTextures(fontData);
if (config.ShadowConfig != null)
Options.DropShadowActive = true;
}
private void LoadQFontFromFontFile(FontLoadDescription loadDescription)
{
var config = loadDescription.BuilderConfig;
var fileName = loadDescription.Path;
var size = loadDescription.Size;
var style = loadDescription.Style;
if (config == null)
config = new QFontBuilderConfiguration();
//dont move this into a separate method - it needs to stay in scope!
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(fileName);
var fontFamily = pfc.Families[0];
if (!fontFamily.IsStyleAvailable(style))
throw new ArgumentException("Font file: " + fileName + " does not support style: " + style);
var font = new Font(fontFamily, size * config.SuperSampleLevels, style);
//var font = ObtainFont(fileName, size * fontScale * config.SuperSampleLevels, style)
fontData = BuildFont(font, config, null);
LoadTextures(fontData);
fontData.scaleDueToTransformToViewport = 1f;
font.Dispose();
if (config.ShadowConfig != null)
Options.DropShadowActive = true;
}
private void LoadQFontFromQFontFile(FontLoadDescription loadDescription)
{
var loaderConfig = loadDescription.LoaderConfig;
var filePath = loadDescription.Path;
var downSampleFactor = loadDescription.DownSampleFactor;
if (loaderConfig == null)
loaderConfig = new QFontLoaderConfiguration();
fontData = Builder.LoadQFontDataFromFile(filePath, downSampleFactor, loaderConfig);
LoadTextures(fontData);
fontData.scaleDueToTransformToViewport = 1f;
if (loaderConfig.ShadowConfig != null)
Options.DropShadowActive = true;
}
public QFont(IFontRenderer renderer, string fileName, float size, FontStyle style = FontStyle.Regular, QFontBuilderConfiguration config = null)
{
this.renderer = renderer;
fontLoadDescription = new FontLoadDescription(fileName, size, style, config);
LoadQFontFromFontFile(fontLoadDescription);
}
public static void CreateTextureFontFiles(Font font, string newFontName, QFontBuilderConfiguration config = null)
{
var fontData = BuildFont(font, config, newFontName);
Builder.SaveQFontDataToFile(fontData, newFontName);
}
public static void CreateTextureFontFiles(string fileName, float size, string newFontName, FontStyle style = FontStyle.Regular, QFontBuilderConfiguration config = null)
{
QFontData fontData;
if (config == null)
config = new QFontBuilderConfiguration();
//dont move this into a separate method - it needs to stay in scope!
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(fileName);
var fontFamily = pfc.Families[0];
if (!fontFamily.IsStyleAvailable(style))
throw new ArgumentException("Font file: " + fileName + " does not support style: " + style);
var font = new Font(fontFamily, size * config.SuperSampleLevels, style);
//var font = ObtainFont(fileName, size * config.SuperSampleLevels, style);
try
{
fontData = BuildFont(font, config, newFontName);
}
finally
{
if (font != null)
font.Dispose();
}
Builder.SaveQFontDataToFile(fontData, newFontName);
}
public static QFont FromQFontFile(IFontRenderer renderer, string filePath, float downSampleFactor = 1.0f, QFontLoaderConfiguration loaderConfig = null)
{
QFont qfont = new QFont(renderer);
qfont.fontLoadDescription = new FontLoadDescription(filePath,downSampleFactor,loaderConfig);
qfont.LoadQFontFromQFontFile(qfont.fontLoadDescription);
return qfont;
}
private static QFontData BuildFont(Font font, QFontBuilderConfiguration config, string saveName)
{
Builder builder = new Builder(font, config);
return builder.BuildFontData(saveName);
}
#endregion
/// <summary>
/// Pushes the specified QFont options onto the options stack
/// </summary>
/// <param name="newOptions"></param>
public void PushOptions(QFontRenderOptions newOptions)
{
optionsStack.Push(newOptions);
}
/// <summary>
/// Creates a clone of the current font options and pushes
/// it onto the stack
/// </summary>
public void PushOptions()
{
PushOptions(Options.CreateClone());
}
public void PopOptions()
{
if (optionsStack.Count > 1)
{
optionsStack.Pop();
}
else
{
throw new Exception("Attempted to pop from options stack when there is only one Options object on the stack.");
}
}
public float LineSpacing
{
get { return (float)Math.Ceiling(fontData.maxGlyphHeight * Options.LineSpacing); }
}
public bool IsMonospacingActive
{
get { return fontData.IsMonospacingActive(Options); }
}
public float MonoSpaceWidth
{
get { return fontData.GetMonoSpaceWidth(Options); }
}
private void RenderDropShadow(float x, float y, char c, QFontGlyph nonShadowGlyph)
{
//note can cast drop shadow offset to int, but then you can't move the shadow smoothly...
if (fontData.dropShadow != null && Options.DropShadowActive)
{
var fData = fontData;
fontData = fontData.dropShadow;
RenderGlyph(
x + (fontData.meanGlyphWidth * Options.DropShadowOffset.X + nonShadowGlyph.rect.Width * 0.5f),
y + (fontData.meanGlyphWidth * Options.DropShadowOffset.Y + nonShadowGlyph.rect.Height * 0.5f + nonShadowGlyph.yOffset), c, true);
fontData = fData;
}
}
public void RenderGlyph(float x, float y, char c, bool isDropShadow)
{
var glyph = fontData.CharSetMapping[c];
//note: it's not immediately obvious, but this combined with the paramteters to
//RenderGlyph for the shadow mean that we render the shadow centrally (despite it being a different size)
//under the glyph
if (isDropShadow)
{
x -= (int)(glyph.rect.Width * 0.5f);
y -= (int)(glyph.rect.Height * 0.5f + glyph.yOffset);
}
RenderDropShadow(x, y, c, glyph);
var sheet = fontData.Pages[glyph.page].Texture;
Quad q = new Quad
{
Texture = sheet,
Color = isDropShadow ? Color.FromArgb((int)(Options.DropShadowOpacity * 255), 255, 255, 255) : Options.Color,
U1 = (float) (glyph.rect.X)/sheet.Width,
V1 = (float) (glyph.rect.Y)/sheet.Height,
U2 = (float) (glyph.rect.X + glyph.rect.Width)/sheet.Width,
V2 = (float) (glyph.rect.Y + glyph.rect.Height)/sheet.Height,
X1 = x,
Y1 = y + glyph.yOffset,
X2 = x + glyph.rect.Width,
Y2 = y + glyph.yOffset + glyph.rect.Height
};
renderer.DrawQuad(q);
}
private float MeasureNextlineLength(string text)
{
float xOffset = 0;
for(int i=0; i < text.Length;i++)
{
char c = text[i];
if (c == '\r' || c == '\n')
{
break;
}
if (IsMonospacingActive)
{
xOffset += MonoSpaceWidth;
}
else
{
//space
if (c == ' ')
{
xOffset += (float)Math.Ceiling(fontData.meanGlyphWidth * Options.WordSpacing);
}
//normal character
else if (fontData.CharSetMapping.ContainsKey(c))
{
QFontGlyph glyph = fontData.CharSetMapping[c];
xOffset += (float)Math.Ceiling(glyph.rect.Width + fontData.meanGlyphWidth * Options.CharacterSpacing + fontData.GetKerningPairCorrection(i, text, null));
}
}
}
return xOffset;
}
private PointF LockToPixel(PointF input)
{
if (Options.LockToPixel)
{
float r = Options.LockToPixelRatio;
return new PointF((1 - r) * input.X + r * ((int)Math.Round(input.X)), (1 - r) * input.Y + r * ((int)Math.Round(input.Y)));
}
return input;
}
public void Print(ProcessedText processedText, PointF position)
{
position = LockToPixel(position);
renderer.Begin(position);
Print(processedText);
renderer.End();
}
public void Print(string text, float maxWidth, QFontAlignment alignment, PointF position)
{
position = LockToPixel(position);
renderer.Begin(position);
Print(text, maxWidth, alignment);
renderer.End();
}
public void Print(string text, QFontAlignment alignment = QFontAlignment.Left, PointF position = default(PointF))
{
position = LockToPixel(position);
renderer.Begin(position);
PrintOrMeasure(text, alignment, false);
renderer.End();
}
public SizeF Measure(string text, QFontAlignment alignment = QFontAlignment.Left)
{
return PrintOrMeasure(text, alignment, true);
}
/// <summary>
/// Measures the actual width and height of the block of text.
/// </summary>
/// <param name="text"></param>
/// <param name="maxWidth"></param>
/// <param name="alignment"></param>
/// <returns></returns>
public SizeF Measure(string text, float maxWidth, QFontAlignment alignment)
{
var processedText = ProcessText(text, maxWidth, alignment);
return Measure(processedText);
}
/// <summary>
/// Measures the actual width and height of the block of text
/// </summary>
/// <param name="processedText"></param>
/// <returns></returns>
public SizeF Measure(ProcessedText processedText)
{
return PrintOrMeasure(processedText, true);
}
private SizeF PrintOrMeasure(string text, QFontAlignment alignment, bool measureOnly)
{
float maxXpos = float.MinValue;
float minXPos = float.MaxValue;
float xOffset = 0f;
float yOffset = 0f;
text = text.Replace("\r\n", "\r");
if (alignment == QFontAlignment.Right)
xOffset -= MeasureNextlineLength(text);
else if (alignment == QFontAlignment.Centre)
xOffset -= (int)(0.5f * MeasureNextlineLength(text));
for(int i = 0; i < text.Length; i++)
{
char c = text[i];
//newline
if (c == '\r' || c == '\n')
{
yOffset += LineSpacing;
xOffset = 0f;
if (alignment == QFontAlignment.Right)
xOffset -= MeasureNextlineLength(text.Substring(i + 1));
else if (alignment == QFontAlignment.Centre)
xOffset -= (int)(0.5f * MeasureNextlineLength(text.Substring(i + 1)));
}
else
{
minXPos = Math.Min(xOffset, minXPos);
//normal character
if (c != ' ' && fontData.CharSetMapping.ContainsKey(c))
{
QFontGlyph glyph = fontData.CharSetMapping[c];
if(!measureOnly)
RenderGlyph(xOffset, yOffset, c, false);
}
if (IsMonospacingActive)
xOffset += MonoSpaceWidth;
else
{
if (c == ' ')
xOffset += (float)Math.Ceiling(fontData.meanGlyphWidth * Options.WordSpacing);
//normal character
else if (fontData.CharSetMapping.ContainsKey(c))
{
QFontGlyph glyph = fontData.CharSetMapping[c];
xOffset += (float)Math.Ceiling(glyph.rect.Width + fontData.meanGlyphWidth * Options.CharacterSpacing + fontData.GetKerningPairCorrection(i, text, null));
}
}
maxXpos = Math.Max(xOffset, maxXpos);
}
}
float maxWidth = 0f;
if (minXPos != float.MaxValue)
maxWidth = maxXpos - minXPos;
return new SizeF(maxWidth, yOffset + LineSpacing);
}
private void RenderWord(float x, float y, TextNode node)
{
if (node.Type != TextNodeType.Word)
return;
int charGaps = node.Text.Length - 1;
bool isCrumbleWord = CrumbledWord(node);
if (isCrumbleWord)
charGaps++;
int pixelsPerGap = 0;
int leftOverPixels = 0;
if (charGaps != 0)
{
pixelsPerGap = (int)node.LengthTweak / charGaps;
leftOverPixels = (int)node.LengthTweak - pixelsPerGap * charGaps;
}
for(int i = 0; i < node.Text.Length; i++){
char c = node.Text[i];
if(fontData.CharSetMapping.ContainsKey(c)){
var glyph = fontData.CharSetMapping[c];
RenderGlyph(x,y,c, false);
if (IsMonospacingActive)
x += MonoSpaceWidth;
else
x += (int)Math.Ceiling(glyph.rect.Width + fontData.meanGlyphWidth * Options.CharacterSpacing + fontData.GetKerningPairCorrection(i, node.Text, node));
x += pixelsPerGap;
if (leftOverPixels > 0)
{
x += 1.0f;
leftOverPixels--;
}
else if (leftOverPixels < 0)
{
x -= 1.0f;
leftOverPixels++;
}
}
}
}
/// <summary>
/// Computes the length of the next line, and whether the line is valid for
/// justification.
/// </summary>
/// <param name="node"></param>
/// <param name="maxLength"></param>
/// <param name="justifable"></param>
/// <returns></returns>
private float TextNodeLineLength(TextNode node, float maxLength)
{
if (node == null)
return 0;
bool atLeastOneNodeCosumedOnLine = false;
float length = 0;
for (; node != null; node = node.Next)
{
if (node.Type == TextNodeType.LineBreak)
break;
if (SkipTrailingSpace(node, length, maxLength) && atLeastOneNodeCosumedOnLine)
break;
if (length + node.Length <= maxLength || !atLeastOneNodeCosumedOnLine)
{
atLeastOneNodeCosumedOnLine = true;
length += node.Length;
}
else
{
break;
}
}
return length;
}
private bool CrumbledWord(TextNode node)
{
return (node.Type == TextNodeType.Word && node.Next != null && node.Next.Type == TextNodeType.Word);
}
/// <summary>
/// Computes the length of the next line, and whether the line is valid for
/// justification.
/// </summary>
private void JustifyLine(TextNode node, float targetLength)
{
bool justifiable = false;
if (node == null)
return;
var headNode = node; //keep track of the head node
//start by finding the length of the block of text that we know will actually fit:
int charGaps = 0;
int spaceGaps = 0;
bool atLeastOneNodeCosumedOnLine = false;
float length = 0;
var expandEndNode = node; //the node at the end of the smaller list (before adding additional word)
for (; node != null; node = node.Next)
{
if (node.Type == TextNodeType.LineBreak)
break;
if (SkipTrailingSpace(node, length, targetLength) && atLeastOneNodeCosumedOnLine)
{
justifiable = true;
break;
}
if (length + node.Length < targetLength || !atLeastOneNodeCosumedOnLine)
{
expandEndNode = node;
if (node.Type == TextNodeType.Space)
spaceGaps++;
if (node.Type == TextNodeType.Word)
{
charGaps += (node.Text.Length - 1);
//word was part of a crumbled word, so there's an extra char cap between the two words
if (CrumbledWord(node))
charGaps++;
}
atLeastOneNodeCosumedOnLine = true;
length += node.Length;
}
else
{
justifiable = true;
break;
}
}
//now we check how much additional length is added by adding an additional word to the line
float extraLength = 0f;
int extraSpaceGaps = 0;
int extraCharGaps = 0;
bool contractPossible = false;
TextNode contractEndNode = null;
for (node = expandEndNode.Next; node != null; node = node.Next)
{
if (node.Type == TextNodeType.LineBreak)
break;
if (node.Type == TextNodeType.Space)
{
extraLength += node.Length;
extraSpaceGaps++;
}
else if (node.Type == TextNodeType.Word)
{
contractEndNode = node;
contractPossible = true;
extraLength += node.Length;
extraCharGaps += (node.Text.Length - 1);
break;
}
}
if (justifiable)
{
//last part of this condition is to ensure that the full contraction is possible (it is all or nothing with contractions, since it looks really bad if we don't manage the full)
bool contract = contractPossible && (extraLength + length - targetLength) * Options.JustifyContractionPenalty < (targetLength - length) &&
((targetLength - (length + extraLength + 1)) / targetLength > -Options.JustifyCapContract);
if((!contract && length < targetLength) || (contract && length + extraLength > targetLength)) //calculate padding pixels per word and char
{
if (contract)
{
length += extraLength + 1;
charGaps += extraCharGaps;
spaceGaps += extraSpaceGaps;
}
int totalPixels = (int)(targetLength - length); //the total number of pixels that need to be added to line to justify it
int spacePixels = 0; //number of pixels to spread out amongst spaces
int charPixels = 0; //number of pixels to spread out amongst char gaps
if (contract)
{
if (totalPixels / targetLength < -Options.JustifyCapContract)
totalPixels = (int)(-Options.JustifyCapContract * targetLength);
}
else
{
if (totalPixels / targetLength > Options.JustifyCapExpand)
totalPixels = (int)(Options.JustifyCapExpand * targetLength);
}
//work out how to spread pixles between character gaps and word spaces
if (charGaps == 0)
{
spacePixels = totalPixels;
}
else if (spaceGaps == 0)
{
charPixels = totalPixels;
}
else
{
if(contract)
charPixels = (int)(totalPixels * Options.JustifyCharacterWeightForContract * charGaps / spaceGaps);
else
charPixels = (int)(totalPixels * Options.JustifyCharacterWeightForExpand * charGaps / spaceGaps);
if ((!contract && charPixels > totalPixels) ||
(contract && charPixels < totalPixels) )
charPixels = totalPixels;
spacePixels = totalPixels - charPixels;
}
int pixelsPerChar = 0; //minimum number of pixels to add per char
int leftOverCharPixels = 0; //number of pixels remaining to only add for some chars
if (charGaps != 0)
{
pixelsPerChar = charPixels / charGaps;
leftOverCharPixels = charPixels - pixelsPerChar * charGaps;
}
int pixelsPerSpace = 0; //minimum number of pixels to add per space
int leftOverSpacePixels = 0; //number of pixels remaining to only add for some spaces
if (spaceGaps != 0)
{
pixelsPerSpace = spacePixels / spaceGaps;
leftOverSpacePixels = spacePixels - pixelsPerSpace * spaceGaps;
}
//now actually iterate over all nodes and set tweaked length
for (node = headNode; node != null; node = node.Next)
{
if (node.Type == TextNodeType.Space)
{
node.LengthTweak = pixelsPerSpace;
if (leftOverSpacePixels > 0)
{
node.LengthTweak += 1;
leftOverSpacePixels--;
}
else if (leftOverSpacePixels < 0)
{
node.LengthTweak -= 1;
leftOverSpacePixels++;
}
}
else if (node.Type == TextNodeType.Word)
{
int cGaps = (node.Text.Length - 1);
if (CrumbledWord(node))
cGaps++;
node.LengthTweak = cGaps * pixelsPerChar;
if (leftOverCharPixels >= cGaps)
{
node.LengthTweak += cGaps;
leftOverCharPixels -= cGaps;
}
else if (leftOverCharPixels <= -cGaps)
{
node.LengthTweak -= cGaps;
leftOverCharPixels += cGaps;
}
else
{
node.LengthTweak += leftOverCharPixels;
leftOverCharPixels = 0;
}
}
if ((!contract && node == expandEndNode) || (contract && node == contractEndNode))
break;
}
}
}
}
/// <summary>
/// Checks whether to skip trailing space on line because the next word does not
/// fit.
///
/// We only check one space - the assumption is that if there is more than one,
/// it is a deliberate attempt to insert spaces.
/// </summary>
/// <param name="node"></param>
/// <param name="lengthSoFar"></param>
/// <param name="boundWidth"></param>
/// <returns></returns>
private bool SkipTrailingSpace(TextNode node, float lengthSoFar, float boundWidth)
{
if (node.Type == TextNodeType.Space && node.Next != null && node.Next.Type == TextNodeType.Word && node.ModifiedLength + node.Next.ModifiedLength + lengthSoFar > boundWidth)
{
return true;
}
return false;
}
/// <summary>
/// Prints text inside the given bounds.
/// </summary>
/// <param name="text"></param>
/// <param name="bounds"></param>
/// <param name="alignment"></param>
public void Print(string text, float maxWidth, QFontAlignment alignment)
{
var processedText = ProcessText(text, maxWidth, alignment);
Print(processedText);
}
/// <summary>
/// Creates node list object associated with the text.
/// </summary>
/// <param name="text"></param>
/// <param name="bounds"></param>
/// <returns></returns>
public ProcessedText ProcessText(string text, float maxWidth, QFontAlignment alignment)
{
//TODO: bring justify and alignment calculations in here
var nodeList = new TextNodeList(text);
nodeList.MeasureNodes(fontData, Options);
//we "crumble" words that are two long so that that can be split up
var nodesToCrumble = new List<TextNode>();
foreach (TextNode node in nodeList)
if (node.Length >= maxWidth && node.Type == TextNodeType.Word)
nodesToCrumble.Add(node);
foreach (var node in nodesToCrumble)
nodeList.Crumble(node, 1);
//need to measure crumbled words
nodeList.MeasureNodes(fontData, Options);
var processedText = new ProcessedText();
processedText.textNodeList = nodeList;
processedText.maxWidth = maxWidth;
processedText.alignment = alignment;
return processedText;
}
/// <summary>
/// Prints text as previously processed with a boundary and alignment.
/// </summary>
/// <param name="processedText"></param>
public void Print(ProcessedText processedText)
{
renderer.Begin(PointF.Empty);
PrintOrMeasure(processedText, false);
renderer.End();
}
private SizeF PrintOrMeasure(ProcessedText processedText, bool measureOnly)
{
float maxMeasuredWidth = 0f;
float maxWidth = processedText.maxWidth;
var alignment = processedText.alignment;
//TODO - use these instead of translate when rendering by position (at some point)
float xPos = 0f;
float yPos = 0f;
float xOffset = xPos;
float yOffset = yPos;
var nodeList = processedText.textNodeList;
for (TextNode node = nodeList.Head; node != null; node = node.Next)
node.LengthTweak = 0f; //reset tweaks
if (alignment == QFontAlignment.Right)
xOffset -= (float)Math.Ceiling(TextNodeLineLength(nodeList.Head, maxWidth) - maxWidth);
else if (alignment == QFontAlignment.Centre)
xOffset -= (float)Math.Ceiling(0.5f * TextNodeLineLength(nodeList.Head, maxWidth) );
else if (alignment == QFontAlignment.Justify)
JustifyLine(nodeList.Head, maxWidth);
bool atLeastOneNodeCosumedOnLine = false;
float length = 0f;
for (TextNode node = nodeList.Head; node != null; node = node.Next)
{
bool newLine = false;
if (node.Type == TextNodeType.LineBreak)
{
newLine = true;
}
else
{
if (SkipTrailingSpace(node, length, maxWidth) && atLeastOneNodeCosumedOnLine)
{
newLine = true;
}
else if (length + node.ModifiedLength <= maxWidth || !atLeastOneNodeCosumedOnLine)
{
atLeastOneNodeCosumedOnLine = true;
if(!measureOnly)
RenderWord(xOffset + length, yOffset, node);
length += node.ModifiedLength;
maxMeasuredWidth = Math.Max(length, maxMeasuredWidth);
}
else
{
newLine = true;
if (node.Previous != null)
node = node.Previous;
}
}
if (newLine)
{
yOffset += LineSpacing;