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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "PresenterTextView.hxx"
#include "PresenterCanvasHelper.hxx"
#include "PresenterGeometryHelper.hxx"
#include "PresenterTimer.hxx"
#include <algorithm>
#include <cmath>
#include <numeric>
#include <com/sun/star/accessibility/AccessibleTextType.hpp>
#include <com/sun/star/container/XEnumerationAccess.hpp>
#include <com/sun/star/i18n/BreakIterator.hpp>
#include <com/sun/star/i18n/CharacterIteratorMode.hpp>
#include <com/sun/star/i18n/ScriptDirection.hpp>
#include <com/sun/star/i18n/WordType.hpp>
#include <com/sun/star/rendering/CompositeOperation.hpp>
#include <com/sun/star/rendering/TextDirection.hpp>
#include <com/sun/star/text/WritingMode2.hpp>
#include <o3tl/safeint.hxx>
#include <utility>
#include <comphelper/diagnose_ex.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::accessibility;
using namespace ::com::sun::star::uno;
const sal_Int64 CaretBlinkInterval = 500 * 1000 * 1000;
//#define SHOW_CHARACTER_BOXES
namespace {
sal_Int32 Signum (const sal_Int32 nValue)
{
if (nValue < 0)
return -1;
else if (nValue > 0)
return +1;
else
return 0;
}
}
namespace sdext::presenter {
//===== PresenterTextView =====================================================
PresenterTextView::PresenterTextView (
const Reference<XComponentContext>& rxContext,
const Reference<rendering::XCanvas>& rxCanvas,
const ::std::function<void (const css::awt::Rectangle&)>& rInvalidator)
: mxCanvas(rxCanvas),
maLocation(0,0),
maSize(0,0),
mpCaret(std::make_shared<PresenterTextCaret>(
rxContext,
[this] (sal_Int32 const nParagraphIndex, sal_Int32 const nCharacterIndex)
{ return this->GetCaretBounds(nParagraphIndex, nCharacterIndex); },
rInvalidator)),
mnLeftOffset(0),
mnTopOffset(0),
mbIsFormatPending(false)
{
Reference<lang::XMultiComponentFactory> xFactory =
rxContext->getServiceManager();
if ( ! xFactory.is())
return;
// Create the break iterator that we use to break text into lines.
mxBreakIterator = i18n::BreakIterator::create(rxContext);
// Create the script type detector that is used to split paragraphs into
// portions of the same text direction.
mxScriptTypeDetector.set(
xFactory->createInstanceWithContext(
u"com.sun.star.i18n.ScriptTypeDetector"_ustr,
rxContext),
UNO_QUERY_THROW);
}
void PresenterTextView::SetText (const Reference<text::XText>& rxText)
{
maParagraphs.clear();
Reference<container::XEnumerationAccess> xParagraphAccess (rxText, UNO_QUERY);
if ( ! xParagraphAccess.is())
return;
Reference<container::XEnumeration> xParagraphs =
xParagraphAccess->createEnumeration();
if ( ! xParagraphs.is())
return;
if ( ! mpFont || ! mpFont->PrepareFont(mxCanvas))
return;
sal_Int32 nCharacterCount (0);
while (xParagraphs->hasMoreElements())
{
SharedPresenterTextParagraph pParagraph = std::make_shared<PresenterTextParagraph>(
maParagraphs.size(),
mxBreakIterator,
mxScriptTypeDetector,
Reference<text::XTextRange>(xParagraphs->nextElement(), UNO_QUERY),
mpCaret);
pParagraph->SetupCellArray(mpFont);
pParagraph->SetCharacterOffset(nCharacterCount);
nCharacterCount += pParagraph->GetCharacterCount();
maParagraphs.push_back(pParagraph);
}
if (mpCaret)
mpCaret->HideCaret();
RequestFormat();
}
void PresenterTextView::SetTextChangeBroadcaster (
const ::std::function<void ()>& rBroadcaster)
{
maTextChangeBroadcaster = rBroadcaster;
}
void PresenterTextView::SetLocation (const css::geometry::RealPoint2D& rLocation)
{
maLocation = rLocation;
for (auto& rxParagraph : maParagraphs)
{
rxParagraph->SetOrigin(
maLocation.X - mnLeftOffset,
maLocation.Y - mnTopOffset);
}
}
void PresenterTextView::SetSize (const css::geometry::RealSize2D& rSize)
{
maSize = rSize;
RequestFormat();
}
double PresenterTextView::GetTotalTextHeight()
{
if (mbIsFormatPending)
{
if ( ! mpFont->PrepareFont(mxCanvas))
return 0;
Format();
}
return std::accumulate(maParagraphs.begin(), maParagraphs.end(), double(0),
[](const double& nTotalHeight, const SharedPresenterTextParagraph& rxParagraph) {
return nTotalHeight + rxParagraph->GetTotalTextHeight();
});
}
void PresenterTextView::SetFont (const PresenterTheme::SharedFontDescriptor& rpFont)
{
mpFont = rpFont;
RequestFormat();
}
void PresenterTextView::SetOffset(
const double nLeft,
const double nTop)
{
mnLeftOffset = nLeft;
mnTopOffset = nTop;
// Trigger an update of the text origin stored at the individual paragraphs.
SetLocation(maLocation);
}
void PresenterTextView::MoveCaret (
const sal_Int32 nDistance,
const sal_Int16 nTextType)
{
if ( ! mpCaret)
return;
// When the caret has not been visible yet then move it to the beginning
// of the text.
if (mpCaret->GetParagraphIndex() < 0)
{
mpCaret->SetPosition(0,0);
return;
}
sal_Int32 nParagraphIndex (mpCaret->GetParagraphIndex());
sal_Int32 nCharacterIndex (mpCaret->GetCharacterIndex());
switch (nTextType)
{
default:
case AccessibleTextType::CHARACTER:
nCharacterIndex += nDistance;
break;
case AccessibleTextType::WORD:
{
sal_Int32 nRemainingDistance (nDistance);
while (nRemainingDistance != 0)
{
SharedPresenterTextParagraph pParagraph (GetParagraph(nParagraphIndex));
if (pParagraph)
{
const sal_Int32 nDelta (Signum(nDistance));
nCharacterIndex = pParagraph->GetWordBoundary(nCharacterIndex, nDelta);
if (nCharacterIndex < 0)
{
// Go to previous or next paragraph.
nParagraphIndex += nDelta;
if (nParagraphIndex < 0)
{
nParagraphIndex = 0;
nCharacterIndex = 0;
nRemainingDistance = 0;
}
else if (o3tl::make_unsigned(nParagraphIndex) >= maParagraphs.size())
{
nParagraphIndex = maParagraphs.size()-1;
pParagraph = GetParagraph(nParagraphIndex);
if (pParagraph)
nCharacterIndex = pParagraph->GetCharacterCount();
nRemainingDistance = 0;
}
else
{
nRemainingDistance -= nDelta;
// Move caret one character to the end of
// the previous or the start of the next paragraph.
pParagraph = GetParagraph(nParagraphIndex);
if (pParagraph)
{
if (nDistance<0)
nCharacterIndex = pParagraph->GetCharacterCount();
else
nCharacterIndex = 0;
}
}
}
else
nRemainingDistance -= nDelta;
}
else
break;
}
break;
}
}
// Move the caret to the new position.
mpCaret->SetPosition(nParagraphIndex, nCharacterIndex);
}
void PresenterTextView::Paint (
const css::awt::Rectangle& rUpdateBox)
{
if ( ! mxCanvas.is())
return;
if ( ! mpFont->PrepareFont(mxCanvas))
return;
if (mbIsFormatPending)
Format();
// Setup the clipping rectangle. Horizontally we make it a little
// larger to allow characters (and the caret) to stick out of their
// bounding boxes. This can happen on some characters (like the
// uppercase J) for typographical reasons.
const sal_Int32 nAdditionalLeftBorder (10);
const sal_Int32 nAdditionalRightBorder (5);
double nX (maLocation.X - mnLeftOffset);
double nY (maLocation.Y - mnTopOffset);
const sal_Int32 nClipLeft (::std::max(
PresenterGeometryHelper::Round(maLocation.X)-nAdditionalLeftBorder, rUpdateBox.X));
const sal_Int32 nClipTop (::std::max(
PresenterGeometryHelper::Round(maLocation.Y), rUpdateBox.Y));
const sal_Int32 nClipRight (::std::min(
PresenterGeometryHelper::Round(maLocation.X+maSize.Width)+nAdditionalRightBorder, rUpdateBox.X+rUpdateBox.Width));
const sal_Int32 nClipBottom (::std::min(
PresenterGeometryHelper::Round(maLocation.Y+maSize.Height), rUpdateBox.Y+rUpdateBox.Height));
if (nClipLeft>=nClipRight || nClipTop>=nClipBottom)
return;
const awt::Rectangle aClipBox(
nClipLeft,
nClipTop,
nClipRight - nClipLeft,
nClipBottom - nClipTop);
Reference<rendering::XPolyPolygon2D> xClipPolygon (
PresenterGeometryHelper::CreatePolygon(aClipBox, mxCanvas->getDevice()));
const rendering::ViewState aViewState(
geometry::AffineMatrix2D(1,0,0, 0,1,0),
xClipPolygon);
rendering::RenderState aRenderState (
geometry::AffineMatrix2D(1,0,nX, 0,1,nY),
nullptr,
Sequence<double>(4),
rendering::CompositeOperation::SOURCE);
PresenterCanvasHelper::SetDeviceColor(aRenderState, mpFont->mnColor);
for (const auto& rxParagraph : maParagraphs)
{
rxParagraph->Paint(
mxCanvas,
maSize,
mpFont,
aViewState,
aRenderState,
mnTopOffset,
nClipTop,
nClipBottom);
}
aRenderState.AffineTransform.m02 = 0;
aRenderState.AffineTransform.m12 = 0;
#ifdef SHOW_CHARACTER_BOXES
PresenterCanvasHelper::SetDeviceColor(aRenderState, 0x00808080);
for (sal_Int32 nParagraphIndex(0), nParagraphCount(GetParagraphCount());
nParagraphIndex<nParagraphCount;
++nParagraphIndex)
{
const SharedPresenterTextParagraph pParagraph (GetParagraph(nParagraphIndex));
if ( ! pParagraph)
continue;
for (sal_Int32 nCharacterIndex(0),nCharacterCount(pParagraph->GetCharacterCount());
nCharacterIndex<nCharacterCount; ++nCharacterIndex)
{
const awt::Rectangle aBox (pParagraph->GetCharacterBounds(nCharacterIndex, false));
mxCanvas->drawPolyPolygon (
PresenterGeometryHelper::CreatePolygon(
aBox,
mxCanvas->getDevice()),
aViewState,
aRenderState);
}
}
PresenterCanvasHelper::SetDeviceColor(aRenderState, mpFont->mnColor);
#endif
if (mpCaret && mpCaret->IsVisible())
{
mxCanvas->fillPolyPolygon (
PresenterGeometryHelper::CreatePolygon(
mpCaret->GetBounds(),
mxCanvas->getDevice()),
aViewState,
aRenderState);
}
}
const SharedPresenterTextCaret& PresenterTextView::GetCaret() const
{
return mpCaret;
}
awt::Rectangle PresenterTextView::GetCaretBounds (
sal_Int32 nParagraphIndex,
const sal_Int32 nCharacterIndex) const
{
SharedPresenterTextParagraph pParagraph (GetParagraph(nParagraphIndex));
if (pParagraph)
return pParagraph->GetCharacterBounds(nCharacterIndex, true);
else
return awt::Rectangle(0,0,0,0);
}
//----- private ---------------------------------------------------------------
void PresenterTextView::RequestFormat()
{
mbIsFormatPending = true;
}
void PresenterTextView::Format()
{
mbIsFormatPending = false;
double nY (0);
for (const auto& rxParagraph : maParagraphs)
{
rxParagraph->Format(nY, maSize.Width, mpFont);
nY += rxParagraph->GetTotalTextHeight();
}
if (maTextChangeBroadcaster)
maTextChangeBroadcaster();
}
sal_Int32 PresenterTextView::GetParagraphCount() const
{
return maParagraphs.size();
}
SharedPresenterTextParagraph PresenterTextView::GetParagraph (
const sal_Int32 nParagraphIndex) const
{
if (nParagraphIndex < 0)
return SharedPresenterTextParagraph();
else if (o3tl::make_unsigned(nParagraphIndex)>=maParagraphs.size())
return SharedPresenterTextParagraph();
else
return maParagraphs[nParagraphIndex];
}
//===== PresenterTextParagraph ================================================
PresenterTextParagraph::PresenterTextParagraph (
const sal_Int32 nParagraphIndex,
const Reference<i18n::XBreakIterator>& rxBreakIterator,
const Reference<i18n::XScriptTypeDetector>& rxScriptTypeDetector,
const Reference<text::XTextRange>& rxTextRange,
SharedPresenterTextCaret xCaret)
: mnParagraphIndex(nParagraphIndex),
mpCaret(std::move(xCaret)),
mxBreakIterator(rxBreakIterator),
mxScriptTypeDetector(rxScriptTypeDetector),
mnVerticalOffset(0),
mnXOrigin(0),
mnYOrigin(0),
mnWidth(0),
mnAscent(0),
mnDescent(0),
mnLineHeight(-1),
mnWritingMode (text::WritingMode2::LR_TB),
mnCharacterOffset(0)
{
if (!rxTextRange.is())
return;
Reference<beans::XPropertySet> xProperties (rxTextRange, UNO_QUERY);
try
{
xProperties->getPropertyValue(u"WritingMode"_ustr) >>= mnWritingMode;
}
catch(beans::UnknownPropertyException&)
{
// Ignore the exception. Use the default value.
}
msParagraphText = rxTextRange->getString();
}
void PresenterTextParagraph::Paint (
const Reference<rendering::XCanvas>& rxCanvas,
const geometry::RealSize2D& rSize,
const PresenterTheme::SharedFontDescriptor& rpFont,
const rendering::ViewState& rViewState,
rendering::RenderState& rRenderState,
const double nTopOffset,
const double nClipTop,
const double nClipBottom)
{
if (mnLineHeight <= 0)
return;
sal_Int8 nTextDirection (GetTextDirection());
const double nSavedM12 (rRenderState.AffineTransform.m12);
if ( ! IsTextReferencePointLeft())
rRenderState.AffineTransform.m02 += rSize.Width;
#ifdef SHOW_CHARACTER_BOXES
for (sal_Int32 nIndex=0,nCount=maLines.size();
nIndex<nCount;
++nIndex)
{
Line& rLine (maLines[nIndex]);
rLine.ProvideLayoutedLine(msParagraphText, rpFont, nTextDirection);
}
#endif
for (sal_Int32 nIndex=0,nCount=maLines.size();
nIndex<nCount;
++nIndex, rRenderState.AffineTransform.m12 += mnLineHeight)
{
Line& rLine (maLines[nIndex]);
// Paint only visible lines.
const double nLineTop = rLine.mnBaseLine - mnAscent - nTopOffset;
if (nLineTop + mnLineHeight< nClipTop)
continue;
else if (nLineTop > nClipBottom)
break;
rLine.ProvideLayoutedLine(msParagraphText, rpFont, nTextDirection);
rRenderState.AffineTransform.m12 = nSavedM12 + rLine.mnBaseLine;
rxCanvas->drawTextLayout (
rLine.mxLayoutedLine,
rViewState,
rRenderState);
}
rRenderState.AffineTransform.m12 = nSavedM12;
if ( ! IsTextReferencePointLeft())
rRenderState.AffineTransform.m02 -= rSize.Width;
}
void PresenterTextParagraph::Format (
const double nY,
const double nWidth,
const PresenterTheme::SharedFontDescriptor& rpFont)
{
// Make sure that the text view is in a valid and sane state.
if ( ! mxBreakIterator.is() || ! mxScriptTypeDetector.is())
return;
if (nWidth<=0)
return;
if ( ! rpFont || ! rpFont->mxFont.is())
return;
sal_Int32 nPosition (0);
mnWidth = nWidth;
maLines.clear();
mnLineHeight = 0;
mnAscent = 0;
mnDescent = 0;
mnVerticalOffset = nY;
maWordBoundaries.clear();
maWordBoundaries.push_back(0);
const rendering::FontMetrics aMetrics (rpFont->mxFont->getFontMetrics());
mnAscent = aMetrics.Ascent;
mnDescent = aMetrics.Descent;
mnLineHeight = aMetrics.Ascent + aMetrics.Descent + aMetrics.ExternalLeading;
nPosition = 0;
i18n::Boundary aCurrentLine(0,0);
while (true)
{
const i18n::Boundary aWordBoundary = mxBreakIterator->nextWord(
msParagraphText,
nPosition,
lang::Locale(),
i18n::WordType::ANYWORD_IGNOREWHITESPACES);
AddWord(nWidth, aCurrentLine, aWordBoundary.startPos, rpFont);
// Remember the new word boundary for caret travelling by words.
// Prevent duplicates.
if (aWordBoundary.startPos > maWordBoundaries.back())
maWordBoundaries.push_back(aWordBoundary.startPos);
if (aWordBoundary.endPos>aWordBoundary.startPos)
AddWord(nWidth, aCurrentLine, aWordBoundary.endPos, rpFont);
if (aWordBoundary.startPos<0 || aWordBoundary.endPos<0)
break;
if (nPosition >= aWordBoundary.endPos)
break;
nPosition = aWordBoundary.endPos;
}
if (aCurrentLine.endPos>aCurrentLine.startPos)
AddLine(aCurrentLine);
}
sal_Int32 PresenterTextParagraph::GetWordBoundary(
const sal_Int32 nLocalCharacterIndex,
const sal_Int32 nDistance)
{
OSL_ASSERT(nDistance==-1 || nDistance==+1);
if (nLocalCharacterIndex < 0)
{
// The caller asked for the start or end position of the paragraph.
if (nDistance < 0)
return 0;
else
return GetCharacterCount();
}
sal_Int32 nIndex (0);
for (sal_Int32 nCount (maWordBoundaries.size()); nIndex<nCount; ++nIndex)
{
if (maWordBoundaries[nIndex] >= nLocalCharacterIndex)
{
// When inside the word (not at its start or end) then
// first move to the start or end before going the previous or
// next word.
if (maWordBoundaries[nIndex] > nLocalCharacterIndex)
if (nDistance > 0)
--nIndex;
break;
}
}
nIndex += nDistance;
if (nIndex < 0)
return -1;
else if (o3tl::make_unsigned(nIndex)>=maWordBoundaries.size())
return -1;
else
return maWordBoundaries[nIndex];
}
sal_Int32 PresenterTextParagraph::GetCaretPosition() const
{
if (mpCaret && mpCaret->GetParagraphIndex()==mnParagraphIndex)
return mpCaret->GetCharacterIndex();
else
return -1;
}
void PresenterTextParagraph::SetCaretPosition (const sal_Int32 nPosition) const
{
if (mpCaret && mpCaret->GetParagraphIndex()==mnParagraphIndex)
return mpCaret->SetPosition(mnParagraphIndex, nPosition);
}
void PresenterTextParagraph::SetOrigin (const double nXOrigin, const double nYOrigin)
{
mnXOrigin = nXOrigin;
mnYOrigin = nYOrigin;
}
awt::Point PresenterTextParagraph::GetRelativeLocation() const
{
return awt::Point(
sal_Int32(mnXOrigin),
sal_Int32(mnYOrigin + mnVerticalOffset));
}
awt::Size PresenterTextParagraph::GetSize() const
{
return awt::Size(
sal_Int32(mnWidth),
sal_Int32(GetTotalTextHeight()));
}
void PresenterTextParagraph::AddWord (
const double nWidth,
i18n::Boundary& rCurrentLine,
const sal_Int32 nWordBoundary,
const PresenterTheme::SharedFontDescriptor& rpFont)
{
sal_Int32 nLineStart (0);
if ( ! maLines.empty())
nLineStart = rCurrentLine.startPos;
const OUString sLineCandidate (
msParagraphText.copy(nLineStart, nWordBoundary-nLineStart));
css::geometry::RealRectangle2D aLineBox (
PresenterCanvasHelper::GetTextBoundingBox (
rpFont->mxFont,
sLineCandidate,
mnWritingMode));
const double nLineWidth (aLineBox.X2 - aLineBox.X1);
if (nLineWidth >= nWidth)
{
// Add new line with a single word (so far).
AddLine(rCurrentLine);
}
rCurrentLine.endPos = nWordBoundary;
}
void PresenterTextParagraph::AddLine (
i18n::Boundary& rCurrentLine)
{
Line aLine (rCurrentLine.startPos, rCurrentLine.endPos);
// Find the start and end of the line with respect to cells.
if (!maLines.empty())
{
aLine.mnLineStartCellIndex = maLines.back().mnLineEndCellIndex;
aLine.mnBaseLine = maLines.back().mnBaseLine + mnLineHeight;
}
else
{
aLine.mnLineStartCellIndex = 0;
aLine.mnBaseLine = mnVerticalOffset + mnAscent;
}
sal_Int32 nCellIndex (aLine.mnLineStartCellIndex);
double nWidth (0);
for ( ; nCellIndex<sal_Int32(maCells.size()); ++nCellIndex)
{
const Cell& rCell (maCells[nCellIndex]);
if (rCell.mnCharacterIndex+rCell.mnCharacterCount > aLine.mnLineEndCharacterIndex)
break;
nWidth += rCell.mnCellWidth;
}
aLine.mnLineEndCellIndex = nCellIndex;
aLine.mnWidth = nWidth;
maLines.push_back(aLine);
rCurrentLine.startPos = rCurrentLine.endPos;
}
double PresenterTextParagraph::GetTotalTextHeight() const
{
return maLines.size() * mnLineHeight;
}
void PresenterTextParagraph::SetCharacterOffset (const sal_Int32 nCharacterOffset)
{
mnCharacterOffset = nCharacterOffset;
}
sal_Int32 PresenterTextParagraph::GetCharacterCount() const
{
return msParagraphText.getLength();
}
sal_Unicode PresenterTextParagraph::GetCharacter (
const sal_Int32 nGlobalCharacterIndex) const
{
if (nGlobalCharacterIndex<mnCharacterOffset
|| nGlobalCharacterIndex>=mnCharacterOffset+msParagraphText.getLength())
{
return sal_Unicode();
}
else
{
return msParagraphText[nGlobalCharacterIndex - mnCharacterOffset];
}
}
const OUString& PresenterTextParagraph::GetText() const
{
return msParagraphText;
}
TextSegment PresenterTextParagraph::GetTextSegment (
const sal_Int32 nOffset,
const sal_Int32 nIndex,
const sal_Int16 nTextType) const
{
switch(nTextType)
{
case AccessibleTextType::PARAGRAPH:
return TextSegment(
msParagraphText,
mnCharacterOffset,
mnCharacterOffset+msParagraphText.getLength());
case AccessibleTextType::SENTENCE:
if (mxBreakIterator.is())
{
const sal_Int32 nStart (mxBreakIterator->beginOfSentence(
msParagraphText, nIndex-mnCharacterOffset, lang::Locale()));
const sal_Int32 nEnd (mxBreakIterator->endOfSentence(
msParagraphText, nIndex-mnCharacterOffset, lang::Locale()));
if (nStart < nEnd)
return TextSegment(
msParagraphText.copy(nStart, nEnd-nStart),
nStart+mnCharacterOffset,
nEnd+mnCharacterOffset);
}
break;
case AccessibleTextType::WORD:
if (mxBreakIterator.is())
return GetWordTextSegment(nOffset, nIndex);
break;
case AccessibleTextType::LINE:
{
auto iLine = std::find_if(maLines.begin(), maLines.end(),
[nIndex](const Line& rLine) { return nIndex < rLine.mnLineEndCharacterIndex; });
if (iLine != maLines.end())
{
return TextSegment(
msParagraphText.copy(
iLine->mnLineStartCharacterIndex,
iLine->mnLineEndCharacterIndex - iLine->mnLineStartCharacterIndex),
iLine->mnLineStartCharacterIndex,
iLine->mnLineEndCharacterIndex);
}
}
break;
// Handle GLYPH and ATTRIBUTE_RUN like CHARACTER because we can not
// do better at the moment.
case AccessibleTextType::CHARACTER:
case AccessibleTextType::GLYPH:
case AccessibleTextType::ATTRIBUTE_RUN:
return CreateTextSegment(nIndex+nOffset, nIndex+nOffset+1);
}
return TextSegment(OUString(), 0,0);
}
TextSegment PresenterTextParagraph::GetWordTextSegment (
const sal_Int32 nOffset,
const sal_Int32 nIndex) const
{
sal_Int32 nCurrentOffset (nOffset);
sal_Int32 nCurrentIndex (nIndex);
i18n::Boundary aWordBoundary;
if (nCurrentOffset == 0)
aWordBoundary = mxBreakIterator->getWordBoundary(
msParagraphText,
nIndex,
lang::Locale(),
i18n::WordType::ANYWORD_IGNOREWHITESPACES,
true);
else if (nCurrentOffset < 0)
{
while (nCurrentOffset<0 && nCurrentIndex>0)
{
aWordBoundary = mxBreakIterator->previousWord(
msParagraphText,
nCurrentIndex,
lang::Locale(),
i18n::WordType::ANYWORD_IGNOREWHITESPACES);
nCurrentIndex = aWordBoundary.startPos;
++nCurrentOffset;
}
}
else
{
while (nCurrentOffset>0 && nCurrentIndex<=GetCharacterCount())
{
aWordBoundary = mxBreakIterator->nextWord(
msParagraphText,
nCurrentIndex,
lang::Locale(),
i18n::WordType::ANYWORD_IGNOREWHITESPACES);
nCurrentIndex = aWordBoundary.endPos;
--nCurrentOffset;
}
}
return CreateTextSegment(aWordBoundary.startPos, aWordBoundary.endPos);
}
TextSegment PresenterTextParagraph::CreateTextSegment (
sal_Int32 nStartIndex,
sal_Int32 nEndIndex) const
{
if (nEndIndex <= nStartIndex)
return TextSegment(
OUString(),
nStartIndex,
nEndIndex);
else
return TextSegment(
msParagraphText.copy(nStartIndex, nEndIndex-nStartIndex),
nStartIndex,
nEndIndex);
}
awt::Rectangle PresenterTextParagraph::GetCharacterBounds (
sal_Int32 nGlobalCharacterIndex,
const bool bCaretBox)
{
// Find the line that contains the requested character and accumulate
// the previous line heights.
double nX (mnXOrigin);
double nY (mnYOrigin + mnVerticalOffset + mnAscent);
const sal_Int8 nTextDirection (GetTextDirection());
for (sal_Int32 nLineIndex=0,nLineCount=maLines.size();
nLineIndex<nLineCount;
++nLineIndex, nY+=mnLineHeight)
{
Line& rLine (maLines[nLineIndex]);
// Skip lines before the indexed character.
if (nGlobalCharacterIndex >= rLine.mnLineEndCharacterIndex)
// When in the last line then allow the index past the last char.
if (nLineIndex<nLineCount-1)
continue;
rLine.ProvideCellBoxes();
const sal_Int32 nCellIndex (nGlobalCharacterIndex - rLine.mnLineStartCharacterIndex);
// The cell bounding box is defined relative to the origin of
// the current line. Therefore we have to add the absolute
// position of the line.
geometry::RealRectangle2D rCellBox (rLine.maCellBoxes[
::std::min(nCellIndex, rLine.maCellBoxes.getLength()-1)]);
double nLeft = nX + rCellBox.X1;
double nRight = nX + rCellBox.X2;
if (nTextDirection == rendering::TextDirection::WEAK_RIGHT_TO_LEFT)
{
const double nOldRight (nRight);
nRight = rLine.mnWidth - nLeft;
nLeft = rLine.mnWidth - nOldRight;
}
double nTop = nY - mnAscent;
double nBottom;
if (bCaretBox)
{
nBottom = nTop + mnLineHeight;
if (nCellIndex >= rLine.maCellBoxes.getLength())
nLeft = nRight-2;
if (nLeft < nX)
nLeft = nX;
nRight = nLeft+2;
}
else
{
nBottom = nTop + mnAscent + mnDescent;
}
const sal_Int32 nX1 = sal_Int32(floor(nLeft));
const sal_Int32 nY1 = sal_Int32(floor(nTop));
const sal_Int32 nX2 = sal_Int32(ceil(nRight));
const sal_Int32 nY2 = sal_Int32(ceil(nBottom));
return awt::Rectangle(nX1,nY1,nX2-nX1+1,nY2-nY1+1);
}
// We are still here. That means that the given index lies past the
// last character in the paragraph.
// Return an empty box that lies past the last character. Better than nothing.
return awt::Rectangle(sal_Int32(nX+0.5), sal_Int32(nY+0.5), 0, 0);
}
sal_Int8 PresenterTextParagraph::GetTextDirection() const
{
// Find first portion that has a non-neutral text direction.
sal_Int32 nPosition (0);
sal_Int32 nTextLength (msParagraphText.getLength());
while (nPosition < nTextLength)
{
const sal_Int16 nScriptDirection (
mxScriptTypeDetector->getScriptDirection(
msParagraphText, nPosition, i18n::ScriptDirection::NEUTRAL));
switch (nScriptDirection)
{
case i18n::ScriptDirection::NEUTRAL:
// continue looping.
break;
case i18n::ScriptDirection::LEFT_TO_RIGHT:
return rendering::TextDirection::WEAK_LEFT_TO_RIGHT;
case i18n::ScriptDirection::RIGHT_TO_LEFT:
return rendering::TextDirection::WEAK_RIGHT_TO_LEFT;
}
nPosition = mxScriptTypeDetector->endOfScriptDirection(
msParagraphText, nPosition, nScriptDirection);
}
// All text in paragraph is neutral. Fall back on writing mode taken
// from the XText (which may not be properly initialized.)
sal_Int8 nTextDirection(rendering::TextDirection::WEAK_LEFT_TO_RIGHT);
switch(mnWritingMode)
{
case text::WritingMode2::LR_TB:
nTextDirection = rendering::TextDirection::WEAK_LEFT_TO_RIGHT;
break;
case text::WritingMode2::RL_TB:
nTextDirection = rendering::TextDirection::WEAK_RIGHT_TO_LEFT;
break;
default:
case text::WritingMode2::TB_RL:
case text::WritingMode2::TB_LR:
// Can not handle this. Use default and hope for the best.
break;
}
return nTextDirection;
}
bool PresenterTextParagraph::IsTextReferencePointLeft() const
{
return mnWritingMode != text::WritingMode2::RL_TB;
}
void PresenterTextParagraph::SetupCellArray (
const PresenterTheme::SharedFontDescriptor& rpFont)
{
maCells.clear();
if ( ! rpFont || ! rpFont->mxFont.is())
return;
sal_Int32 nPosition (0);
sal_Int32 nIndex (0);
const sal_Int32 nTextLength (msParagraphText.getLength());
const sal_Int8 nTextDirection (GetTextDirection());
while (nPosition < nTextLength)
{
const sal_Int32 nNewPosition (mxBreakIterator->nextCharacters(
msParagraphText,
nPosition,
lang::Locale(),
i18n::CharacterIteratorMode::SKIPCELL,
1,
nIndex));
rendering::StringContext aContext (msParagraphText, nPosition, nNewPosition-nPosition);
Reference<rendering::XTextLayout> xLayout (
rpFont->mxFont->createTextLayout(aContext, nTextDirection, 0));
css::geometry::RealRectangle2D aCharacterBox (xLayout->queryTextBounds());
maCells.emplace_back(
nPosition,
nNewPosition-nPosition,
aCharacterBox.X2-aCharacterBox.X1);
nPosition = nNewPosition;
}
}
//===== PresenterTextCaret ================================================----
PresenterTextCaret::PresenterTextCaret (
uno::Reference<uno::XComponentContext> const& xContext,
::std::function<css::awt::Rectangle (const sal_Int32,const sal_Int32)> aCharacterBoundsAccess,
::std::function<void (const css::awt::Rectangle&)> aInvalidator)
: m_xContext(xContext)
, mnParagraphIndex(-1),
mnCharacterIndex(-1),
mnCaretBlinkTaskId(0),
mbIsCaretVisible(false),
maCharacterBoundsAccess(std::move(aCharacterBoundsAccess)),
maInvalidator(std::move(aInvalidator))
{
}
PresenterTextCaret::~PresenterTextCaret()
{
try
{
HideCaret();
}
catch (uno::Exception const&)
{
TOOLS_WARN_EXCEPTION("sdext.presenter", "unexpected exception in ~PresenterTextCaret");
}
}
void PresenterTextCaret::ShowCaret()
{
if (mnCaretBlinkTaskId == 0)
{
mnCaretBlinkTaskId = PresenterTimer::ScheduleRepeatedTask (
m_xContext,
[this] (TimeValue const&) { return this->InvertCaret(); },
CaretBlinkInterval,
CaretBlinkInterval);
}
mbIsCaretVisible = true;
}
void PresenterTextCaret::HideCaret()
{
if (mnCaretBlinkTaskId != 0)
{
PresenterTimer::CancelTask(mnCaretBlinkTaskId);
mnCaretBlinkTaskId = 0;
}
mbIsCaretVisible = false;
// Reset the caret position.
mnParagraphIndex = -1;
mnCharacterIndex = -1;
}
void PresenterTextCaret::SetPosition (
const sal_Int32 nParagraphIndex,
const sal_Int32 nCharacterIndex)
{
if (mnParagraphIndex == nParagraphIndex
&& mnCharacterIndex == nCharacterIndex)
return;
if (mnParagraphIndex >= 0)
maInvalidator(maCaretBounds);
const sal_Int32 nOldParagraphIndex (mnParagraphIndex);
const sal_Int32 nOldCharacterIndex (mnCharacterIndex);
mnParagraphIndex = nParagraphIndex;
mnCharacterIndex = nCharacterIndex;
maCaretBounds = maCharacterBoundsAccess(mnParagraphIndex, mnCharacterIndex);
if (mnParagraphIndex >= 0)
ShowCaret();
else
HideCaret();
if (mnParagraphIndex >= 0)
maInvalidator(maCaretBounds);
if (maBroadcaster)
maBroadcaster(
nOldParagraphIndex,
nOldCharacterIndex,
mnParagraphIndex,
mnCharacterIndex);
}
void PresenterTextCaret::SetCaretMotionBroadcaster (
const ::std::function<void (sal_Int32,sal_Int32,sal_Int32,sal_Int32)>& rBroadcaster)
{
maBroadcaster = rBroadcaster;
}
const css::awt::Rectangle& PresenterTextCaret::GetBounds() const
{
return maCaretBounds;
}
void PresenterTextCaret::InvertCaret()
{
mbIsCaretVisible = !mbIsCaretVisible;
if (mnParagraphIndex >= 0)
maInvalidator(maCaretBounds);
}
//===== PresenterTextParagraph::Cell ==========================================
PresenterTextParagraph::Cell::Cell (
const sal_Int32 nCharacterIndex,
const sal_Int32 nCharacterCount,
const double nCellWidth)
: mnCharacterIndex(nCharacterIndex),
mnCharacterCount(nCharacterCount),
mnCellWidth(nCellWidth)
{
}
//===== PresenterTextParagraph::Line ==========================================
PresenterTextParagraph::Line::Line (
const sal_Int32 nLineStartCharacterIndex,
const sal_Int32 nLineEndCharacterIndex)
: mnLineStartCharacterIndex(nLineStartCharacterIndex),
mnLineEndCharacterIndex(nLineEndCharacterIndex),
mnLineStartCellIndex(-1), mnLineEndCellIndex(-1),
mnBaseLine(0), mnWidth(0)
{
}
void PresenterTextParagraph::Line::ProvideCellBoxes()
{
if ( mnLineStartCharacterIndex < mnLineEndCharacterIndex && !maCellBoxes.hasElements() )
{
if (mxLayoutedLine.is())
maCellBoxes = mxLayoutedLine->queryInkMeasures();
else
{
OSL_ASSERT(mxLayoutedLine.is());
}
}
}
void PresenterTextParagraph::Line::ProvideLayoutedLine (
const OUString& rsParagraphText,
const PresenterTheme::SharedFontDescriptor& rpFont,
const sal_Int8 nTextDirection)
{
if ( ! mxLayoutedLine.is())
{
const rendering::StringContext aContext (
rsParagraphText,
mnLineStartCharacterIndex,
mnLineEndCharacterIndex - mnLineStartCharacterIndex);
mxLayoutedLine = rpFont->mxFont->createTextLayout(
aContext,
nTextDirection,
0);
}
}
} // end of namespace ::sdext::presenter
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|