-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtextblock.cc
More file actions
3377 lines (2851 loc) · 116 KB
/
Copy pathtextblock.cc
File metadata and controls
3377 lines (2851 loc) · 116 KB
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
/*
* Dillo Widget
*
* Copyright 2005-2007, 2012-2014 Sebastian Geerken <sgeerken@dillo.org>
* Copyright 2023-2024 Rodrigo Arias Mallo <rodarima@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "textblock.hh"
#include "../lout/msg.h"
#include "../lout/misc.hh"
#include "../lout/unicode.hh"
#include "../lout/debug.hh"
#include <stdio.h>
#include <math.h> // remove again?
#include <limits.h>
/*
* Local variables
*/
/* The tooltip under mouse pointer in current textblock. No ref. hold.
* (having one per view looks not worth the extra clutter). */
static dw::core::style::Tooltip *hoverTooltip = NULL;
using namespace lout;
using namespace lout::misc;
using namespace lout::unicode;
namespace dw {
int Textblock::CLASS_ID = -1;
Textblock::WordImgRenderer::WordImgRenderer (Textblock *textblock,
int wordNo)
{
//printf ("new WordImgRenderer %p\n", this);
this->textblock = textblock;
this->wordNo = wordNo;
dataSet = false;
}
Textblock::WordImgRenderer::~WordImgRenderer ()
{
//printf ("delete WordImgRenderer %p\n", this);
}
void Textblock::WordImgRenderer::setData (int xWordWidget, int lineNo)
{
dataSet = true;
this->xWordWidget = xWordWidget;
this->lineNo = lineNo;
}
bool Textblock::WordImgRenderer::readyToDraw ()
{
//print ();
//printf ("\n");
return dataSet && textblock->wasAllocated ()
&& wordNo < textblock->words->size()
&& lineNo < textblock->lines->size();
}
void Textblock::WordImgRenderer::getBgArea (int *x, int *y, int *width,
int *height)
{
// TODO Subtract margin and border (padding box)?
Line *line = textblock->lines->getRef (lineNo);
*x = textblock->allocation.x + this->xWordWidget;
*y = textblock->lineYOffsetCanvas (line);
*width = textblock->words->getRef(wordNo)->size.width;
*height = line->borderAscent + line->borderDescent;
}
void Textblock::WordImgRenderer::getRefArea (int *xRef, int *yRef,
int *widthRef, int *heightRef)
{
// See comment in Widget::drawBox about the reference area.
textblock->getPaddingArea (xRef, yRef, widthRef, heightRef);
}
core::style::Style *Textblock::WordImgRenderer::getStyle ()
{
return textblock->words->getRef(wordNo)->style;
}
void Textblock::WordImgRenderer::draw (int x, int y, int width, int height)
{
textblock->queueDrawArea (x - textblock->allocation.x,
y - textblock->allocation.y, width, height);
}
void Textblock::SpaceImgRenderer::getBgArea (int *x, int *y, int *width,
int *height)
{
WordImgRenderer::getBgArea (x, y, width, height);
*x += *width;
*width = textblock->words->getRef(wordNo)->effSpace;
}
core::style::Style *Textblock::SpaceImgRenderer::getStyle ()
{
return textblock->words->getRef(wordNo)->spaceStyle;
}
// ----------------------------------------------------------------------
Textblock::DivChar Textblock::divChars[NUM_DIV_CHARS] = {
// soft hyphen (U+00AD)
{ "\xc2\xad", true, false, true, PENALTY_HYPHEN, -1 },
// simple hyphen-minus: same penalties like automatic or soft hyphens
{ "-", false, true, true, -1, PENALTY_HYPHEN },
// (unconditional) hyphen (U+2010): handled exactly like minus-hyphen.
{ "\xe2\x80\x90", false, true, true, -1, PENALTY_HYPHEN },
// em dash (U+2014): breaks on both sides are allowed (but see below).
{ "\xe2\x80\x94", false, true, false,
PENALTY_EM_DASH_LEFT, PENALTY_EM_DASH_RIGHT }
};
// Standard values are defined here. The values are already multiplied
// with 100.
//
// Some examples (details are described in doc/dw-line-breaking.doc):
//
// 0 = Perfect line; as penalty used for normal spaces.
// 1 (100 here) = A justified line with spaces having 150% or 67% of
// the ideal space width has this as badness.
//
// 8 (800 here) = A justified line with spaces twice as wide as
// ideally has this as badness.
//
// The second value is used when the line before ends with a hyphen,
// dash etc.
int Textblock::penalties[PENALTY_NUM][2] = {
// Penalties for all hyphens.
{ 100, 800 },
// Penalties for a break point *left* of an em-dash: rather large,
// so that a break on the *right* side is preferred.
{ 800, 800 },
// Penalties for a break point *right* of an em-dash: like hyphens.
{ 100, 800 }
};
int Textblock::stretchabilityFactor = 100;
/**
* The character which is used to draw a hyphen at the end of a line,
* either caused by automatic hyphenation, or by soft hyphens.
*
* Initially, soft hyphens were used, but they are not drawn on some
* platforms. Also, unconditional hyphens (U+2010) are not available
* in many fonts; so, a simple hyphen-minus is used.
*/
const char *Textblock::hyphenDrawChar = "-";
void Textblock::setPenaltyHyphen (int penaltyHyphen)
{
penalties[PENALTY_HYPHEN][0] = penaltyHyphen;
}
void Textblock::setPenaltyHyphen2 (int penaltyHyphen2)
{
penalties[PENALTY_HYPHEN][1] = penaltyHyphen2;
}
void Textblock::setPenaltyEmDashLeft (int penaltyLeftEmDash)
{
penalties[PENALTY_EM_DASH_LEFT][0] = penaltyLeftEmDash;
penalties[PENALTY_EM_DASH_LEFT][1] = penaltyLeftEmDash;
}
void Textblock::setPenaltyEmDashRight (int penaltyRightEmDash)
{
penalties[PENALTY_EM_DASH_RIGHT][0] = penaltyRightEmDash;
}
void Textblock::setPenaltyEmDashRight2 (int penaltyRightEmDash2)
{
penalties[PENALTY_EM_DASH_RIGHT][1] = penaltyRightEmDash2;
}
void Textblock::setStretchabilityFactor (int stretchabilityFactor)
{
Textblock::stretchabilityFactor = stretchabilityFactor;
}
Textblock::Textblock (bool limitTextWidth, bool treatAsInline)
{
DBG_OBJ_CREATE ("dw::Textblock");
registerName ("dw::Textblock", &CLASS_ID);
setButtonSensitive(true);
hasListitemValue = false;
leftInnerPadding = 0;
line1Offset = 0;
ignoreLine1OffsetSometimes = false;
mustQueueResize = false;
DBG_OBJ_SET_BOOL ("mustQueueResize", mustQueueResize);
redrawY = 0;
DBG_OBJ_SET_NUM ("redrawY", redrawY);
lastWordDrawn = -1;
DBG_OBJ_SET_NUM ("lastWordDrawn", lastWordDrawn);
DBG_OBJ_ASSOC_CHILD (&sizeRequestParams);
/*
* The initial sizes of lines and words should not be
* too high, since this will waste much memory with tables
* containing many small cells. The few more calls to realloc
* should not decrease the speed considerably.
* (Current setting is for minimal memory usage. An interesting fact
* is that high values decrease speed due to memory handling overhead!)
* TODO: Some tests would be useful.
*/
paragraphs = new misc::SimpleVector <Paragraph> (1);
lines = new misc::SimpleVector <Line> (1);
nonTemporaryLines = 0;
words = new misc::NotSoSimpleVector <Word> (1);
anchors = new misc::SimpleVector <Anchor> (1);
wrapRefLines = wrapRefParagraphs = -1;
wrapRefLinesFCX = wrapRefLinesFCY = -1;
DBG_OBJ_SET_NUM ("lines.size", lines->size ());
DBG_OBJ_SET_NUM ("words.size", words->size ());
DBG_OBJ_SET_NUM ("wrapRefLines", wrapRefLines);
DBG_OBJ_SET_NUM ("wrapRefParagraphs", wrapRefParagraphs);
DBG_OBJ_SET_NUM ("wrapRefLinesFCX", wrapRefLinesFCX);
DBG_OBJ_SET_NUM ("wrapRefLinesFCY", wrapRefLinesFCY);
hoverLink = -1;
// -1 means undefined.
lineBreakWidth = -1;
DBG_OBJ_SET_NUM ("lineBreakWidth", lineBreakWidth);
this->limitTextWidth = limitTextWidth;
this->treatAsInline = treatAsInline;
for (int layer = 0; layer < core::HIGHLIGHT_NUM_LAYERS; layer++) {
/* hlStart[layer].index > hlEnd[layer].index means no highlighting */
hlStart[layer].index = 1;
hlStart[layer].nChar = 0;
hlEnd[layer].index = 0;
hlEnd[layer].nChar = 0;
DBG_OBJ_ARRATTRSET_NUM ("hlStart", layer, "index", hlStart[layer].index);
DBG_OBJ_ARRATTRSET_NUM ("hlStart", layer, "nChar", hlStart[layer].nChar);
DBG_OBJ_ARRATTRSET_NUM ("hlEnd", layer, "index", hlEnd[layer].index);
DBG_OBJ_ARRATTRSET_NUM ("hlEnd", layer, "nChar", hlEnd[layer].nChar);
}
numSizeReferences = 0;
initNewLine ();
}
Textblock::~Textblock ()
{
/* make sure not to call a free'd tooltip (very fast overkill) */
hoverTooltip = NULL;
for (int i = 0; i < words->size(); i++)
cleanupWord (i);
for (int i = 0; i < anchors->size(); i++) {
Anchor *anchor = anchors->getRef (i);
/* This also frees the names (see removeAnchor() and related). */
removeAnchor(anchor->name);
}
delete paragraphs;
delete lines;
delete words;
delete anchors;
/* Make sure we don't own widgets anymore. Necessary before call of
parent class destructor. (???) */
words = NULL;
DBG_OBJ_DELETE ();
}
/**
* The ascent of a textblock is the ascent of the first line, plus
* padding/border/margin. This can be used to align the first lines
* of several textblocks in a horizontal line.
*/
void Textblock::sizeRequestImpl (core::Requisition *requisition, int numPos,
Widget **references, int *x, int *y)
{
DBG_OBJ_ENTER ("resize", 0, "sizeRequestImpl", "%d, ...", numPos);
sizeRequestParams.fill (numPos, references, x, y);
// We have to rewrap the whole textblock, if (i) the available width (which
// is the line break width) has changed, or (ii) if the position within the
// float container, and so possibly borders relative to this textblock, have
// changed.
//
// (The latter is a simplification: an over-correct implementation would test
// all OOFMs on whether affectsLeftBorder() or affectsRightBorder() returns
// true. Also, this may be optimized by distinguishing between floats
// generated by this textblock (which would not make rewrapping necessary)
// and floats generated by other textblocks (which would).)
int newLineBreakWidth = getAvailWidth (true);
int newFCX, newFCY;
bool fcDefined = findSizeRequestReference (OOFM_FLOATS, &newFCX, &newFCY);
if (newLineBreakWidth != lineBreakWidth ||
(fcDefined && (newFCX != wrapRefLinesFCX ||
newFCY != wrapRefLinesFCY))) {
lineBreakWidth = newLineBreakWidth;
wrapRefLines = 0;
DBG_OBJ_SET_NUM ("lineBreakWidth", lineBreakWidth);
DBG_OBJ_SET_NUM ("wrapRefLines", wrapRefLines);
if (!fcDefined) {
wrapRefLinesFCX = newFCX;
wrapRefLinesFCY = newFCY;
DBG_OBJ_SET_NUM ("wrapRefLinesFCX", wrapRefLinesFCX);
DBG_OBJ_SET_NUM ("wrapRefLinesFCY", wrapRefLinesFCY);
}
}
rewrap ();
showMissingLines ();
if (lines->size () > 0) {
Line *firstLine = lines->getRef(0), *lastLine = lines->getLastRef ();
// Note: the breakSpace of the last line is ignored, so breaks
// at the end of a textblock are not visible.
requisition->width =
lastLine->maxLineWidth + leftInnerPadding + boxDiffWidth ();
// Also regard collapsing of this widget top margin and the top
// margin of the first line box:
requisition->ascent = calcVerticalBorder (getStyle()->padding.top,
getStyle()->borderWidth.top,
getStyle()->marginTop()
+ extraSpace.top,
firstLine->borderAscent,
firstLine->marginAscent);
// And here, regard collapsing of this widget bottom margin and the
// bottom margin of the last line box:
requisition->descent =
// (BTW, this line:
lastLine->top - firstLine->borderAscent + lastLine->borderAscent +
// ... is 0 for a block with one line, so special handling
// for this case is not necessary.)
calcVerticalBorder (getStyle()->padding.bottom,
getStyle()->borderWidth.bottom,
getStyle()->marginBottom() + extraSpace.bottom,
lastLine->borderDescent, lastLine->marginDescent);
} else {
requisition->width = leftInnerPadding + boxDiffWidth ();
requisition->ascent = boxOffsetY ();
requisition->descent = boxRestHeight ();
}
if (usesMaxGeneratorWidth ()) {
DBG_OBJ_MSGF ("resize", 1,
"before considering lineBreakWidth (= %d): %d * (%d + %d)",
lineBreakWidth, requisition->width, requisition->ascent,
requisition->descent);
if (requisition->width < lineBreakWidth)
requisition->width = lineBreakWidth;
} else
DBG_OBJ_MSG ("resize", 1, "lineBreakWidth needs no consideration");
DBG_OBJ_MSGF ("resize", 1, "before correction: %d * (%d + %d)",
requisition->width, requisition->ascent, requisition->descent);
correctRequisition (requisition, core::splitHeightPreserveAscent, true,
false);
// Dealing with parts out of flow, which may overlap the borders of
// the text block. Base lines are ignored here: they do not play a
// role (currently) and caring about them (for the future) would
// cause too much problems.
// Notice that the order is not typical: correctRequisition should
// be the last call. However, calling correctRequisition after
// outOfFlowMgr->getSize may result again in a size which is too
// small for floats, so triggering again (and again) the resize
// idle function resulting in CPU hogging. See also
// getExtremesImpl.
//
// Is this really what we want? An alternative could be that
// OutOfFlowMgr::getSize honours CSS attributes an corrected sizes.
correctRequisitionByOOF (requisition, core::splitHeightPreserveAscent);
DBG_OBJ_MSGF ("resize", 1, "final: %d * (%d + %d)",
requisition->width, requisition->ascent, requisition->descent);
DBG_OBJ_LEAVE ();
}
int Textblock::numSizeRequestReferences ()
{
return numSizeReferences;
}
core::Widget *Textblock::sizeRequestReference (int index)
{
return sizeReferences[index];
}
int Textblock::calcVerticalBorder (int widgetPadding, int widgetBorder,
int widgetMargin, int lineBorderTotal,
int lineMarginTotal)
{
DBG_OBJ_ENTER ("resize", 0, "calcVerticalBorder", "%d, %d, %d, %d, %d",
widgetPadding, widgetBorder, widgetMargin, lineBorderTotal,
lineMarginTotal);
int result;
if (widgetPadding == 0 && widgetBorder == 0) {
if (lineMarginTotal - lineBorderTotal >= widgetMargin)
result = lineMarginTotal;
else
result = widgetMargin + lineBorderTotal;
} else
result = lineMarginTotal + widgetPadding + widgetBorder + widgetMargin;
DBG_OBJ_LEAVE_VAL ("%d", result);
return result;
}
/**
* Get the extremes of a word within a textblock.
*/
void Textblock::getWordExtremes (Word *word, core::Extremes *extremes)
{
if (word->content.type == core::Content::WIDGET_IN_FLOW)
word->content.widget->getExtremes (extremes);
else
extremes->minWidth = extremes->minWidthIntrinsic = extremes->maxWidth =
extremes->maxWidthIntrinsic = extremes->adjustmentWidth =
word->size.width;
}
void Textblock::getExtremesSimpl (core::Extremes *extremes)
{
DBG_OBJ_ENTER0 ("resize", 0, "getExtremesSimpl");
fillParagraphs ();
if (paragraphs->size () == 0) {
/* empty page */
extremes->minWidth = 0;
extremes->minWidthIntrinsic = 0;
extremes->maxWidth = 0;
extremes->maxWidthIntrinsic = 0;
extremes->adjustmentWidth = 0;
} else {
Paragraph *lastPar = paragraphs->getLastRef ();
extremes->minWidth = lastPar->maxParMin;
extremes->minWidthIntrinsic = lastPar->maxParMinIntrinsic;
extremes->maxWidth = lastPar->maxParMax;
extremes->maxWidthIntrinsic = lastPar->maxParMaxIntrinsic;
extremes->adjustmentWidth = lastPar->maxParAdjustmentWidth;
DBG_OBJ_MSGF ("resize", 1, "paragraphs[%d]->maxParMin = %d (%d)",
paragraphs->size () - 1, lastPar->maxParMin,
lastPar->maxParMinIntrinsic);
DBG_OBJ_MSGF ("resize", 1, "paragraphs[%d]->maxParMax = %d (%d)",
paragraphs->size () - 1, lastPar->maxParMax,
lastPar->maxParMaxIntrinsic);
}
DBG_OBJ_MSGF ("resize", 0, "after considering paragraphs: %d (%d) / %d (%d)",
extremes->minWidth, extremes->minWidthIntrinsic,
extremes->maxWidth, extremes->maxWidthIntrinsic);
int diff = leftInnerPadding + boxDiffWidth ();
extremes->minWidth += diff;
extremes->minWidthIntrinsic += diff;
extremes->maxWidth += diff;
extremes->maxWidthIntrinsic += diff;
extremes->adjustmentWidth += diff;
DBG_OBJ_MSGF ("resize", 0, "after adding diff: %d (%d) / %d (%d)",
extremes->minWidth, extremes->minWidthIntrinsic,
extremes->maxWidth, extremes->maxWidthIntrinsic);
// For the order, see similar reasoning in sizeRequestImpl.
correctExtremes (extremes, true);
DBG_OBJ_MSGF ("resize", 0, "after correction: %d (%d) / %d (%d)",
extremes->minWidth, extremes->minWidthIntrinsic,
extremes->maxWidth, extremes->maxWidthIntrinsic);
correctExtremesByOOF (extremes);
DBG_OBJ_MSGF ("resize", 0,
"finally, after considering OOFM: %d (%d) / %d (%d)",
extremes->minWidth, extremes->minWidthIntrinsic,
extremes->maxWidth, extremes->maxWidthIntrinsic);
DBG_OBJ_LEAVE ();
}
int Textblock::numGetExtremesReferences ()
{
return numSizeReferences;
}
core::Widget *Textblock::getExtremesReference (int index)
{
return sizeReferences[index];
}
void Textblock::notifySetAsTopLevel ()
{
OOFAwareWidget::notifySetAsTopLevel ();
numSizeReferences = 0;
DBG_OBJ_SET_NUM ("numSizeReferences", numSizeReferences);
}
void Textblock::notifySetParent ()
{
OOFAwareWidget::notifySetParent ();
numSizeReferences = 0;
for (int i = 0; i < NUM_OOFM; i++) {
if (oofContainer[i] != this) {
// avoid duplicates
bool found = false;
for (int j = 0; !found && j < numSizeReferences; j++)
if (oofContainer[i] == oofContainer[j])
found = true;
if (!found)
sizeReferences[numSizeReferences++] = oofContainer[i];
}
}
DBG_OBJ_SET_NUM ("numSizeReferences", numSizeReferences);
for (int i = 0; i < numSizeReferences; i++)
DBG_OBJ_ARRSET_PTR ("sizeReferences", i, sizeReferences[i]);
}
void Textblock::sizeAllocateImpl (core::Allocation *allocation)
{
DBG_OBJ_ENTER ("resize", 0, "sizeAllocateImpl", "%d, %d; %d * (%d + %d)",
allocation->x, allocation->y, allocation->width,
allocation->ascent, allocation->descent);
showMissingLines ();
sizeAllocateStart (allocation);
int lineIndex, wordIndex;
Line *line;
Word *word;
int xCursor;
core::Allocation childAllocation;
core::Allocation *oldChildAllocation;
if (allocation->x != this->allocation.x ||
allocation->y != this->allocation.y ||
allocation->width != this->allocation.width) {
redrawY = 0;
DBG_OBJ_SET_NUM ("redrawY", redrawY);
}
DBG_OBJ_MSG_START ();
for (lineIndex = 0; lineIndex < lines->size (); lineIndex++) {
DBG_OBJ_MSGF ("resize", 1, "line %d", lineIndex);
DBG_OBJ_MSG_START ();
// Especially for floats, allocation->width may be different
// from the line break width, so that for centered and right
// text, the offsets have to be recalculated again. However, if
// the allocation width is greater than the line break width,
// due to wide unbreakable lines (large image etc.), use the
// original line break width.
//
// TODO: test case?
calcTextOffset (lineIndex, misc::min (allocation->width, lineBreakWidth));
line = lines->getRef (lineIndex);
xCursor = line->textOffset;
DBG_OBJ_MSGF ("resize", 1, "xCursor = %d (initially)", xCursor);
for (wordIndex = line->firstWord; wordIndex <= line->lastWord;
wordIndex++) {
word = words->getRef (wordIndex);
if (wordIndex == lastWordDrawn + 1) {
redrawY = misc::min (redrawY, lineYOffsetWidget (line, allocation));
DBG_OBJ_SET_NUM ("redrawY", redrawY);
}
if (word->content.type == core::Content::WIDGET_IN_FLOW) {
DBG_OBJ_MSGF ("resize", 1,
"allocating widget in flow: line %d, word %d",
lineIndex, wordIndex);
// TODO For word->flags & Word::TOPLEFT_OF_LINE, make
// allocation consistent with calcSizeOfWidgetInFlow():
childAllocation.x = xCursor + allocation->x;
/** \todo Justification within the line is done here. */
/* align=top:
childAllocation.y = line->top + allocation->y;
*/
/* align=bottom (base line) */
/* Commented lines break the n2 and n3 test cases at
* https://dillo-browser.github.io/old/test/img/ */
childAllocation.y = lineYOffsetCanvas (line, allocation)
+ (line->borderAscent - word->size.ascent);
childAllocation.width = word->size.width;
childAllocation.ascent = word->size.ascent;
childAllocation.descent = word->size.descent;
oldChildAllocation = word->content.widget->getAllocation();
if (childAllocation.x != oldChildAllocation->x ||
childAllocation.y != oldChildAllocation->y ||
childAllocation.width != oldChildAllocation->width) {
/* The child widget has changed its position or its width
* so we need to redraw from this line onwards.
*/
redrawY =
misc::min (redrawY, lineYOffsetWidget (line, allocation));
DBG_OBJ_SET_NUM ("redrawY", redrawY);
if (word->content.widget->wasAllocated ()) {
redrawY = misc::min (redrawY,
oldChildAllocation->y - this->allocation.y);
DBG_OBJ_SET_NUM ("redrawY", redrawY);
}
} else if (childAllocation.ascent + childAllocation.descent !=
oldChildAllocation->ascent + oldChildAllocation->descent) {
/* The child widget has changed its height. We need to redraw
* from where it changed.
* It's important not to draw from the line base, because the
* child might be a table covering the whole page so we would
* end up redrawing the whole screen over and over.
* The drawing of the child content is left to the child itself.
* However this optimization is only possible if the widget is
* the only word in the line apart from an optional BREAK.
* Otherwise the height change of the widget could change the
* position of other words in the line, requiring a
* redraw of the complete line.
*/
if (line->lastWord == line->firstWord ||
(line->lastWord == line->firstWord + 1 &&
words->getRef (line->lastWord)->content.type ==
core::Content::BREAK)) {
int childChangedY =
misc::min(childAllocation.y - allocation->y +
childAllocation.ascent + childAllocation.descent,
oldChildAllocation->y - this->allocation.y +
oldChildAllocation->ascent +
oldChildAllocation->descent);
redrawY = misc::min (redrawY, childChangedY);
DBG_OBJ_SET_NUM ("redrawY", redrawY);
} else {
redrawY =
misc::min (redrawY, lineYOffsetWidget (line, allocation));
DBG_OBJ_SET_NUM ("redrawY", redrawY);
}
}
word->content.widget->sizeAllocate (&childAllocation);
}
xCursor += (word->size.width + word->effSpace);
DBG_OBJ_MSGF ("resize", 1, "xCursor = %d (after word %d)",
xCursor, wordIndex);
DBG_MSG_WORD ("resize", 1, "<i>that is:</i> ", wordIndex, "");
}
DBG_OBJ_MSG_END ();
}
DBG_OBJ_MSG_END ();
sizeAllocateEnd ();
for (int i = 0; i < anchors->size(); i++) {
Anchor *anchor = anchors->getRef(i);
int y;
if (anchor->wordIndex >= words->size() ||
// Also regard not-yet-existing lines.
lines->size () <= 0 ||
anchor->wordIndex > lines->getLastRef()->lastWord) {
y = allocation->y + allocation->ascent + allocation->descent;
} else {
Line *line = lines->getRef(findLineOfWord (anchor->wordIndex));
y = lineYOffsetCanvas (line, allocation);
}
changeAnchor (anchor->name, y);
}
DBG_OBJ_LEAVE ();
}
void Textblock::calcExtraSpaceImpl (int numPos, Widget **references, int *x,
int *y)
{
DBG_OBJ_ENTER0 ("resize", 0, "Textblock::calcExtraSpaceImpl");
sizeRequestParams.fill (numPos, references, x, y);
OOFAwareWidget::calcExtraSpaceImpl (numPos, references, x, y);
int clearPosition = 0;
for (int i = 0; i < NUM_OOFM; i++)
if (searchOutOfFlowMgr (i) && findSizeRequestReference (i, NULL, NULL))
clearPosition =
misc::max (clearPosition,
searchOutOfFlowMgr(i)->getClearPosition (this));
extraSpace.top = misc::max (extraSpace.top, clearPosition);
DBG_OBJ_LEAVE ();
}
int Textblock::getAvailWidthOfChild (Widget *child, bool forceValue)
{
DBG_OBJ_ENTER ("resize", 0, "Textblock::getAvailWidthOfChild", "%p, %s",
child, forceValue ? "true" : "false");
int width;
if (isWidgetOOF (child) && getWidgetOutOfFlowMgr(child) &&
getWidgetOutOfFlowMgr(child)->dealingWithSizeOfChild (child))
width =
getWidgetOutOfFlowMgr(child)->getAvailWidthOfChild (child,forceValue);
else {
if (child->getStyle()->width == core::style::LENGTH_AUTO) {
// No width specified: similar to standard implementation (see
// there), but "leftInnerPadding" has to be considered, too.
DBG_OBJ_MSG ("resize", 1, "no specification");
if (forceValue) {
width = misc::max (getAvailWidth (true) - boxDiffWidth ()
- leftInnerPadding,
0);
if (width != -1) {
/* Clamp to min-width and max-width if given, taking into
* account leftInnerPadding. */
int maxWidth = child->calcWidth (child->getStyle()->maxWidth,
-1, this, -1, false).total;
if (maxWidth != -1 && width > maxWidth - leftInnerPadding)
width = maxWidth - leftInnerPadding;
int minWidth = child->calcWidth (child->getStyle()->minWidth,
-1, this, -1, false).total;
if (minWidth != -1 && width < minWidth - leftInnerPadding)
width = minWidth - leftInnerPadding;
}
} else {
width = -1;
}
} else
width = Widget::getAvailWidthOfChild (child, forceValue);
if (forceValue && this == child->getContainer () &&
!usesMaxGeneratorWidth ()) {
core::Extremes extremes;
getExtremes (&extremes);
if (width > extremes.maxWidth - boxDiffWidth () - leftInnerPadding)
width = extremes.maxWidth - boxDiffWidth () - leftInnerPadding;
}
}
DBG_OBJ_LEAVE_VAL ("%d", width);
return width;
}
int Textblock::getAvailHeightOfChild (core::Widget *child, bool forceValue)
{
if (isWidgetOOF(child) && getWidgetOutOfFlowMgr(child) &&
getWidgetOutOfFlowMgr(child)->dealingWithSizeOfChild (child))
return getWidgetOutOfFlowMgr(child)->getAvailHeightOfChild (child,
forceValue);
else
return Widget::getAvailHeightOfChild (child, forceValue);
}
void Textblock::containerSizeChangedForChildren ()
{
DBG_OBJ_ENTER0 ("resize", 0, "containerSizeChangedForChildren");
for (int i = 0; i < words->size (); i++) {
Word *word = words->getRef (i);
if (word->content.type == core::Content::WIDGET_IN_FLOW)
word->content.widget->containerSizeChanged ();
}
containerSizeChangedForChildrenOOF ();
DBG_OBJ_LEAVE ();
}
bool Textblock::affectsSizeChangeContainerChild (Widget *child)
{
DBG_OBJ_ENTER ("resize", 0,
"Textblock/affectsSizeChangeContainerChild", "%p", child);
// See Textblock::getAvailWidthOfChild() and Textblock::oofSizeChanged():
// Extremes changes affect the size of the child, too:
bool ret;
if (!usesMaxGeneratorWidth () &&
(extremesQueued () || extremesChanged ()))
ret = true;
else
ret = Widget::affectsSizeChangeContainerChild (child);
DBG_OBJ_LEAVE_VAL ("%s", boolToStr(ret));
return ret;
}
bool Textblock::usesAvailWidth ()
{
return true;
}
void Textblock::resizeDrawImpl ()
{
DBG_OBJ_ENTER0 ("draw", 0, "resizeDrawImpl");
queueDrawArea (0, redrawY, allocation.width, getHeight () - redrawY);
if (lines->size () > 0) {
Line *lastLine = lines->getRef (lines->size () - 1);
/* Remember the last word that has been drawn so we can ensure to
* draw any new added words (see sizeAllocateImpl()).
*/
lastWordDrawn = lastLine->lastWord;
DBG_OBJ_SET_NUM ("lastWordDrawn", lastWordDrawn);
}
redrawY = getHeight ();
DBG_OBJ_SET_NUM ("redrawY", redrawY);
DBG_OBJ_LEAVE ();
}
void Textblock::markSizeChange (int ref)
{
DBG_OBJ_ENTER ("resize", 0, "markSizeChange", "%d", ref);
if (isParentRefOOF (ref))
getParentRefOutOfFlowMgr(ref)
->markSizeChange (getParentRefOOFSubRef (ref));
else {
/* By the way: ref == -1 may have two different causes: (i) flush()
calls "queueResize (-1, true)", when no rewrapping is necessary;
and (ii) a word may have parentRef == -1 , when it is not yet
added to a line. In the latter case, nothing has to be done
now, but addLine(...) will do everything necessary. */
if (ref != -1) {
if (wrapRefLines == -1)
wrapRefLines = getParentRefInFlowSubRef (ref);
else
wrapRefLines = misc::min (wrapRefLines,
getParentRefInFlowSubRef (ref));
}
DBG_OBJ_SET_NUM ("wrapRefLines", wrapRefLines);
// It seems that sometimes (even without floats) the lines
// structure is changed, so that wrapRefLines may refers to a
// line which does not exist anymore. Should be examined
// again. Until then, setting wrapRefLines to the same value is
// a workaround.
markExtremesChange (ref);
}
DBG_OBJ_LEAVE ();
}
void Textblock::markExtremesChange (int ref)
{
DBG_OBJ_ENTER ("resize", 1, "markExtremesChange", "%d", ref);
if (isParentRefOOF (ref))
getParentRefOutOfFlowMgr(ref)
->markExtremesChange (getParentRefOOFSubRef (ref));
else {
/* By the way: ref == -1 may have two different causes: (i) flush()
calls "queueResize (-1, true)", when no rewrapping is necessary;
and (ii) a word may have parentRef == -1 , when it is not yet
added to a line. In the latter case, nothing has to be done
now, but addLine(...) will do everything necessary. */
if (ref != -1) {
if (wrapRefParagraphs == -1)
wrapRefParagraphs = getParentRefInFlowSubRef (ref);
else
wrapRefParagraphs =
misc::min (wrapRefParagraphs, getParentRefInFlowSubRef (ref));
}
DBG_OBJ_SET_NUM ("wrapRefParagraphs", wrapRefParagraphs);
}
DBG_OBJ_LEAVE ();
}
bool Textblock::isBlockLevel ()
{
return true;
}
bool Textblock::buttonPressImpl (core::EventButton *event)
{
return sendSelectionEvent (core::SelectionState::BUTTON_PRESS, event);
}
bool Textblock::buttonReleaseImpl (core::EventButton *event)
{
return sendSelectionEvent (core::SelectionState::BUTTON_RELEASE, event);
}
/*
* Handle motion inside the widget
* (special care is necessary when switching from another widget,
* because hoverLink and hoverTooltip are meaningless then).
*/
bool Textblock::motionNotifyImpl (core::EventMotion *event)
{
if (event->state & core::BUTTON1_MASK)
return sendSelectionEvent (core::SelectionState::BUTTON_MOTION, event);
else {
bool inSpace;
int linkOld = hoverLink;
core::style::Tooltip *tooltipOld = hoverTooltip;
const Word *word = findWord (event->xWidget, event->yWidget, &inSpace);
// cursor from word or widget style
if (word == NULL) {
setCursor (getStyle()->cursor);
hoverLink = -1;
hoverTooltip = NULL;
} else {
core::style::Style *style = inSpace ? word->spaceStyle : word->style;
setCursor (style->cursor);
hoverLink = style->x_link;
hoverTooltip = style->x_tooltip;
}
// Show/hide tooltip
if (tooltipOld != hoverTooltip) {
if (tooltipOld)
tooltipOld->onLeave ();
if (hoverTooltip)
hoverTooltip->onEnter ();
} else if (hoverTooltip)
hoverTooltip->onMotion ();
_MSG("MN tb=%p tooltipOld=%p hoverTooltip=%p\n",
this, tooltipOld, hoverTooltip);
if (hoverLink != linkOld) {
/* LinkEnter with hoverLink == -1 is the same as LinkLeave */
return layout->emitLinkEnter (this, hoverLink, -1, -1, -1);
} else {
return hoverLink != -1;
}
}
}
void Textblock::enterNotifyImpl (core::EventCrossing *event)