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
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580 | /* -*- 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 <iostream>
#include <iomanip>
#include <sal/config.h>
#include <sal/log.hxx>
#include <cstdio>
#include <math.h>
#include <salgdi.hxx>
#include <sallayout.hxx>
#include <basegfx/polygon/b2dpolypolygon.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <i18nlangtag/lang.h>
#include <vcl/svapp.hxx>
#include <unicode/ubidi.h>
#include <unicode/uchar.h>
#include <algorithm>
#include <memory>
#include <impglyphitem.hxx>
// Glyph Flags
#define GF_FONTMASK 0xF0000000
#define GF_FONTSHIFT 28
std::ostream &operator <<(std::ostream& s, ImplLayoutArgs const &rArgs)
{
#ifndef SAL_LOG_INFO
(void) rArgs;
#else
s << "ImplLayoutArgs{";
s << "Flags=";
if (rArgs.mnFlags == SalLayoutFlags::NONE)
s << 0;
else {
bool need_or = false;
s << "{";
#define TEST(x) if (rArgs.mnFlags & SalLayoutFlags::x) { if (need_or) s << "|"; s << #x; need_or = true; }
TEST(BiDiRtl);
TEST(BiDiStrong);
TEST(RightAlign);
TEST(DisableKerning);
TEST(KerningAsian);
TEST(Vertical);
TEST(KashidaJustification);
TEST(ForFallback);<--- Variable 'need_or' is assigned a value that is never used.
#undef TEST
s << "}";
}
const int nLength = rArgs.mrStr.getLength();
s << ",Length=" << nLength;
s << ",MinCharPos=" << rArgs.mnMinCharPos;
s << ",EndCharPos=" << rArgs.mnEndCharPos;
s << ",Str=\"";
int lim = nLength;
if (lim > 10)
lim = 7;
for (int i = 0; i < lim; i++) {
if (rArgs.mrStr[i] == '\n')
s << "\\n";
else if (rArgs.mrStr[i] < ' ' || (rArgs.mrStr[i] >= 0x7F && rArgs.mrStr[i] <= 0xFF))
s << "\\0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(rArgs.mrStr[i]) << std::setfill(' ') << std::setw(1) << std::dec;
else if (rArgs.mrStr[i] < 0x7F)
s << static_cast<char>(rArgs.mrStr[i]);
else
s << "\\u" << std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(rArgs.mrStr[i]) << std::setfill(' ') << std::setw(1) << std::dec;
}
if (nLength > lim)
s << "...";
s << "\"";
s << ",DXArray=";
if (rArgs.mpDXArray) {
s << "[";
int count = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
lim = count;
if (lim > 10)
lim = 7;
for (int i = 0; i < lim; i++) {
s << rArgs.mpDXArray[i];
if (i < lim-1)
s << ",";
}
if (count > lim) {
if (count > lim + 1)
s << "...";
s << rArgs.mpDXArray[count-1];
}
s << "]";
} else
s << "NULL";
s << ",LayoutWidth=" << rArgs.mnLayoutWidth;
s << "}";
#endif
return s;
}
sal_UCS4 GetMirroredChar( sal_UCS4 nChar )
{
nChar = u_charMirror( nChar );
return nChar;
}
sal_UCS4 GetLocalizedChar( sal_UCS4 nChar, LanguageType eLang )
{
// currently only conversion from ASCII digits is interesting
if( (nChar < '0') || ('9' < nChar) )
return nChar;
int nOffset;
// eLang & LANGUAGE_MASK_PRIMARY catches language independent of region.
// CAVEAT! To some like Mongolian MS assigned the same primary language
// although the script type is different!
LanguageType pri = primary(eLang);
if( pri == primary(LANGUAGE_ARABIC_SAUDI_ARABIA) )
nOffset = 0x0660 - '0'; // arabic-indic digits
else if ( pri.anyOf(
primary(LANGUAGE_FARSI),
primary(LANGUAGE_URDU_PAKISTAN),
primary(LANGUAGE_PUNJABI), //???
primary(LANGUAGE_SINDHI)))
nOffset = 0x06F0 - '0'; // eastern arabic-indic digits
else if ( pri == primary(LANGUAGE_BENGALI) )
nOffset = 0x09E6 - '0'; // bengali
else if ( pri == primary(LANGUAGE_HINDI) )
nOffset = 0x0966 - '0'; // devanagari
else if ( pri.anyOf(
primary(LANGUAGE_AMHARIC_ETHIOPIA),
primary(LANGUAGE_TIGRIGNA_ETHIOPIA)))
// TODO case:
nOffset = 0x1369 - '0'; // ethiopic
else if ( pri == primary(LANGUAGE_GUJARATI) )
nOffset = 0x0AE6 - '0'; // gujarati
#ifdef LANGUAGE_GURMUKHI // TODO case:
else if ( pri == primary(LANGUAGE_GURMUKHI) )<--- Skipping configuration 'LANGUAGE_GURMUKHI' since the value of 'LANGUAGE_GURMUKHI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
nOffset = 0x0A66 - '0'; // gurmukhi
#endif
else if ( pri == primary(LANGUAGE_KANNADA) )
nOffset = 0x0CE6 - '0'; // kannada
else if ( pri == primary(LANGUAGE_KHMER))
nOffset = 0x17E0 - '0'; // khmer
else if ( pri == primary(LANGUAGE_LAO) )
nOffset = 0x0ED0 - '0'; // lao
else if ( pri == primary(LANGUAGE_MALAYALAM) )
nOffset = 0x0D66 - '0'; // malayalam
else if ( pri == primary(LANGUAGE_MONGOLIAN_MONGOLIAN_LSO))
{
if (eLang.anyOf(
LANGUAGE_MONGOLIAN_MONGOLIAN_MONGOLIA,
LANGUAGE_MONGOLIAN_MONGOLIAN_CHINA,
LANGUAGE_MONGOLIAN_MONGOLIAN_LSO))
nOffset = 0x1810 - '0'; // mongolian
else
nOffset = 0; // mongolian cyrillic
}
else if ( pri == primary(LANGUAGE_BURMESE) )
nOffset = 0x1040 - '0'; // myanmar
else if ( pri == primary(LANGUAGE_ODIA) )
nOffset = 0x0B66 - '0'; // odia
else if ( pri == primary(LANGUAGE_TAMIL) )
nOffset = 0x0BE7 - '0'; // tamil
else if ( pri == primary(LANGUAGE_TELUGU) )
nOffset = 0x0C66 - '0'; // telugu
else if ( pri == primary(LANGUAGE_THAI) )
nOffset = 0x0E50 - '0'; // thai
else if ( pri == primary(LANGUAGE_TIBETAN) )
nOffset = 0x0F20 - '0'; // tibetan
else
{
nOffset = 0;
}
nChar += nOffset;
return nChar;
}
static bool IsControlChar( sal_UCS4 cChar )
{
// C0 control characters
if( (0x0001 <= cChar) && (cChar <= 0x001F) )
return true;
// formatting characters
if( (0x200E <= cChar) && (cChar <= 0x200F) )
return true;
if( (0x2028 <= cChar) && (cChar <= 0x202E) )
return true;
// deprecated formatting characters
if( (0x206A <= cChar) && (cChar <= 0x206F) )
return true;
if( 0x2060 == cChar )
return true;
// byte order markers and invalid unicode
if( (cChar == 0xFEFF) || (cChar == 0xFFFE) || (cChar == 0xFFFF) )
return true;
return false;
}
void ImplLayoutRuns::AddPos( int nCharPos, bool bRTL )
{
// check if charpos could extend current run
int nIndex = maRuns.size();
if( nIndex >= 2 )
{
int nRunPos0 = maRuns[ nIndex-2 ];
int nRunPos1 = maRuns[ nIndex-1 ];
if( ((nCharPos + int(bRTL)) == nRunPos1) && ((nRunPos0 > nRunPos1) == bRTL) )
{
// extend current run by new charpos
maRuns[ nIndex-1 ] = nCharPos + int(!bRTL);
return;
}
// ignore new charpos when it is in current run
if( (nRunPos0 <= nCharPos) && (nCharPos < nRunPos1) )
return;
if( (nRunPos1 <= nCharPos) && (nCharPos < nRunPos0) )
return;
}
// else append a new run consisting of the new charpos
maRuns.push_back( nCharPos + (bRTL ? 1 : 0) );
maRuns.push_back( nCharPos + (bRTL ? 0 : 1) );
}
void ImplLayoutRuns::AddRun( int nCharPos0, int nCharPos1, bool bRTL )
{
if( nCharPos0 == nCharPos1 )
return;
// swap if needed
if( bRTL == (nCharPos0 < nCharPos1) )
{
int nTemp = nCharPos0;
nCharPos0 = nCharPos1;
nCharPos1 = nTemp;
}
if (maRuns.size() >= 2 && nCharPos0 == maRuns[maRuns.size() - 2] && nCharPos1 == maRuns[maRuns.size() - 1])
{
//this run is the same as the last
return;
}
// append new run
maRuns.push_back( nCharPos0 );
maRuns.push_back( nCharPos1 );
}
bool ImplLayoutRuns::PosIsInRun( int nCharPos ) const
{
if( mnRunIndex >= static_cast<int>(maRuns.size()) )
return false;
int nMinCharPos = maRuns[ mnRunIndex+0 ];
int nEndCharPos = maRuns[ mnRunIndex+1 ];
if( nMinCharPos > nEndCharPos ) // reversed in RTL case
{
int nTemp = nMinCharPos;
nMinCharPos = nEndCharPos;
nEndCharPos = nTemp;
}
if( nCharPos < nMinCharPos )
return false;
if( nCharPos >= nEndCharPos )
return false;
return true;
}
bool ImplLayoutRuns::PosIsInAnyRun( int nCharPos ) const
{
bool bRet = false;
int nRunIndex = mnRunIndex;
ImplLayoutRuns *pThis = const_cast<ImplLayoutRuns*>(this);
pThis->ResetPos();
for (size_t i = 0; i < maRuns.size(); i+=2)
{
bRet = PosIsInRun( nCharPos );
if( bRet )
break;
pThis->NextRun();
}
pThis->mnRunIndex = nRunIndex;
return bRet;
}
bool ImplLayoutRuns::GetNextPos( int* nCharPos, bool* bRightToLeft )
{
// negative nCharPos => reset to first run
if( *nCharPos < 0 )
mnRunIndex = 0;
// return false when all runs completed
if( mnRunIndex >= static_cast<int>(maRuns.size()) )
return false;
int nRunPos0 = maRuns[ mnRunIndex+0 ];
int nRunPos1 = maRuns[ mnRunIndex+1 ];
*bRightToLeft = (nRunPos0 > nRunPos1);
if( *nCharPos < 0 )
{
// get first valid nCharPos in run
*nCharPos = nRunPos0;
}
else
{
// advance to next nCharPos for LTR case
if( !*bRightToLeft )
++(*nCharPos);
// advance to next run if current run is completed
if( *nCharPos == nRunPos1 )
{
if( (mnRunIndex += 2) >= static_cast<int>(maRuns.size()) )
return false;
nRunPos0 = maRuns[ mnRunIndex+0 ];
nRunPos1 = maRuns[ mnRunIndex+1 ];
*bRightToLeft = (nRunPos0 > nRunPos1);
*nCharPos = nRunPos0;
}
}
// advance to next nCharPos for RTL case
if( *bRightToLeft )
--(*nCharPos);
return true;
}
bool ImplLayoutRuns::GetRun( int* nMinRunPos, int* nEndRunPos, bool* bRightToLeft ) const
{
if( mnRunIndex >= static_cast<int>(maRuns.size()) )
return false;
int nRunPos0 = maRuns[ mnRunIndex+0 ];
int nRunPos1 = maRuns[ mnRunIndex+1 ];
*bRightToLeft = (nRunPos1 < nRunPos0) ;
if( !*bRightToLeft )
{
*nMinRunPos = nRunPos0;
*nEndRunPos = nRunPos1;
}
else
{
*nMinRunPos = nRunPos1;
*nEndRunPos = nRunPos0;
}
return true;
}
ImplLayoutArgs::ImplLayoutArgs(const OUString& rStr,
int nMinCharPos, int nEndCharPos, SalLayoutFlags nFlags, const LanguageTag& rLanguageTag,
vcl::TextLayoutCache const*const pLayoutCache)
:
maLanguageTag( rLanguageTag ),
mnFlags( nFlags ),
mrStr( rStr ),
mnMinCharPos( nMinCharPos ),
mnEndCharPos( nEndCharPos ),
m_pTextLayoutCache(pLayoutCache),
mpDXArray( nullptr ),
mnLayoutWidth( 0 ),
mnOrientation( 0 )
{
if( mnFlags & SalLayoutFlags::BiDiStrong )
{
// handle strong BiDi mode
// do not bother to BiDi analyze strong LTR/RTL
// TODO: can we assume these strings do not have unicode control chars?
// if not remove the control characters from the runs
bool bRTL(mnFlags & SalLayoutFlags::BiDiRtl);
AddRun( mnMinCharPos, mnEndCharPos, bRTL );
}
else
{
// handle weak BiDi mode
UBiDiLevel nLevel = (mnFlags & SalLayoutFlags::BiDiRtl)? 1 : 0;
// prepare substring for BiDi analysis
// TODO: reuse allocated pParaBidi
UErrorCode rcI18n = U_ZERO_ERROR;
const int nLength = mrStr.getLength();
UBiDi* pParaBidi = ubidi_openSized(nLength, 0, &rcI18n);
if( !pParaBidi )
return;
ubidi_setPara(pParaBidi, reinterpret_cast<const UChar *>(mrStr.getStr()), nLength, nLevel, nullptr, &rcI18n);
UBiDi* pLineBidi = pParaBidi;
int nSubLength = mnEndCharPos - mnMinCharPos;
if (nSubLength != nLength)
{
pLineBidi = ubidi_openSized( nSubLength, 0, &rcI18n );
ubidi_setLine( pParaBidi, mnMinCharPos, mnEndCharPos, pLineBidi, &rcI18n );
}
// run BiDi algorithm
const int nRunCount = ubidi_countRuns( pLineBidi, &rcI18n );
//maRuns.resize( 2 * nRunCount );
for( int i = 0; i < nRunCount; ++i )
{
int32_t nMinPos, nRunLength;
const UBiDiDirection nDir = ubidi_getVisualRun( pLineBidi, i, &nMinPos, &nRunLength );
const int nPos0 = nMinPos + mnMinCharPos;
const int nPos1 = nPos0 + nRunLength;
const bool bRTL = (nDir == UBIDI_RTL);
AddRun( nPos0, nPos1, bRTL );
}
// cleanup BiDi engine
if( pLineBidi != pParaBidi )
ubidi_close( pLineBidi );
ubidi_close( pParaBidi );
}
// prepare calls to GetNextPos/GetNextRun
maRuns.ResetPos();
}
// add a run after splitting it up to get rid of control chars
void ImplLayoutArgs::AddRun( int nCharPos0, int nCharPos1, bool bRTL )
{
SAL_WARN_IF( nCharPos0 > nCharPos1, "vcl", "ImplLayoutArgs::AddRun() nCharPos0>=nCharPos1" );
// remove control characters from runs by splitting them up
if( !bRTL )
{
for( int i = nCharPos0; i < nCharPos1; ++i )
if( IsControlChar( mrStr[i] ) )
{
// add run until control char
maRuns.AddRun( nCharPos0, i, bRTL );
nCharPos0 = i + 1;
}
}
else
{
for( int i = nCharPos1; --i >= nCharPos0; )
if( IsControlChar( mrStr[i] ) )
{
// add run until control char
maRuns.AddRun( i+1, nCharPos1, bRTL );
nCharPos1 = i;
}
}
// add remainder of run
maRuns.AddRun( nCharPos0, nCharPos1, bRTL );
}
bool ImplLayoutArgs::PrepareFallback()
{
// short circuit if no fallback is needed
if( maFallbackRuns.IsEmpty() )
{
maRuns.Clear();
return false;
}
// convert the fallback requests to layout requests
bool bRTL;
int nMin, nEnd;
// get the individual fallback requests
std::vector<int> aPosVector;
aPosVector.reserve(mrStr.getLength());
maFallbackRuns.ResetPos();
for(; maFallbackRuns.GetRun( &nMin, &nEnd, &bRTL ); maFallbackRuns.NextRun() )
for( int i = nMin; i < nEnd; ++i )
aPosVector.push_back( i );
maFallbackRuns.Clear();
// sort the individual fallback requests
std::sort( aPosVector.begin(), aPosVector.end() );
// adjust fallback runs to have the same order and limits of the original runs
ImplLayoutRuns aNewRuns;
maRuns.ResetPos();
for(; maRuns.GetRun( &nMin, &nEnd, &bRTL ); maRuns.NextRun() )
{
if( !bRTL) {
auto it = std::lower_bound( aPosVector.begin(), aPosVector.end(), nMin );
for(; (it != aPosVector.end()) && (*it < nEnd); ++it )
aNewRuns.AddPos( *it, bRTL );
} else {
auto it = std::upper_bound( aPosVector.begin(), aPosVector.end(), nEnd );
while( (it != aPosVector.begin()) && (*--it >= nMin) )
aNewRuns.AddPos( *it, bRTL );
}
}
maRuns = aNewRuns; // TODO: use vector<>::swap()
maRuns.ResetPos();
return true;
}
bool ImplLayoutArgs::GetNextRun( int* nMinRunPos, int* nEndRunPos, bool* bRTL )
{
bool bValid = maRuns.GetRun( nMinRunPos, nEndRunPos, bRTL );
maRuns.NextRun();
return bValid;
}
SalLayout::SalLayout()
: mnMinCharPos( -1 ),
mnEndCharPos( -1 ),
mnUnitsPerPixel( 1 ),
mnOrientation( 0 ),
maDrawOffset( 0, 0 )
{}
SalLayout::~SalLayout()
{}
void SalLayout::AdjustLayout( ImplLayoutArgs& rArgs )
{
mnMinCharPos = rArgs.mnMinCharPos;
mnEndCharPos = rArgs.mnEndCharPos;
mnOrientation = rArgs.mnOrientation;
}
Point SalLayout::GetDrawPosition( const Point& rRelative ) const
{
Point aPos = maDrawBase;
Point aOfs = rRelative + maDrawOffset;
if( mnOrientation == 0 )
aPos += aOfs;
else
{
// cache trigonometric results
static int nOldOrientation = 0;
static double fCos = 1.0, fSin = 0.0;
if( nOldOrientation != mnOrientation )
{
nOldOrientation = mnOrientation;
double fRad = mnOrientation * (M_PI / 1800.0);
fCos = cos( fRad );
fSin = sin( fRad );
}
double fX = aOfs.X();
double fY = aOfs.Y();
long nX = static_cast<long>( +fCos * fX + fSin * fY );
long nY = static_cast<long>( +fCos * fY - fSin * fX );
aPos += Point( nX, nY );
}
return aPos;
}
bool SalLayout::GetOutline(basegfx::B2DPolyPolygonVector& rVector) const
{
bool bAllOk = true;
bool bOneOk = false;
basegfx::B2DPolyPolygon aGlyphOutline;
Point aPos;
const GlyphItem* pGlyph;
int nStart = 0;
while (GetNextGlyph(&pGlyph, aPos, nStart))
{
// get outline of individual glyph, ignoring "empty" glyphs
bool bSuccess = pGlyph->GetGlyphOutline(aGlyphOutline);
bAllOk &= bSuccess;
bOneOk |= bSuccess;
// only add non-empty outlines
if( bSuccess && (aGlyphOutline.count() > 0) )
{
if( aPos.X() || aPos.Y() )
{
aGlyphOutline.transform(basegfx::utils::createTranslateB2DHomMatrix(aPos.X(), aPos.Y()));
}
// insert outline at correct position
rVector.push_back( aGlyphOutline );
}
}
return (bAllOk && bOneOk);
}
bool SalLayout::GetBoundRect(tools::Rectangle& rRect) const
{
bool bRet = false;
rRect.SetEmpty();
tools::Rectangle aRectangle;
Point aPos;
const GlyphItem* pGlyph;
int nStart = 0;
while (GetNextGlyph(&pGlyph, aPos, nStart))
{
// get bounding rectangle of individual glyph
if (pGlyph->GetGlyphBoundRect(aRectangle))
{
// merge rectangle
aRectangle += aPos;
if (rRect.IsEmpty())
rRect = aRectangle;
else
rRect.Union(aRectangle);
bRet = true;
}
}
return bRet;
}
DeviceCoordinate GenericSalLayout::FillDXArray( DeviceCoordinate* pCharWidths ) const
{
if (pCharWidths)
GetCharWidths(pCharWidths);
return GetTextWidth();
}
// the text width is the maximum logical extent of all glyphs
DeviceCoordinate GenericSalLayout::GetTextWidth() const
{
if (!m_GlyphItems.IsValid())
return 0;
// initialize the extent
DeviceCoordinate nMinPos = 0;
DeviceCoordinate nMaxPos = 0;
for (auto const& aGlyphItem : *m_GlyphItems.Impl())
{
// update the text extent with the glyph extent
DeviceCoordinate nXPos = aGlyphItem.m_aLinearPos.getX();
if( nMinPos > nXPos )
nMinPos = nXPos;
nXPos += aGlyphItem.m_nNewWidth - aGlyphItem.xOffset();
if( nMaxPos < nXPos )
nMaxPos = nXPos;
}
DeviceCoordinate nWidth = nMaxPos - nMinPos;
return nWidth;
}
void GenericSalLayout::Justify( DeviceCoordinate nNewWidth )
{
nNewWidth *= mnUnitsPerPixel;
DeviceCoordinate nOldWidth = GetTextWidth();
if( !nOldWidth || nNewWidth==nOldWidth )
return;
if (!m_GlyphItems.IsValid())
{
return;
}
// find rightmost glyph, it won't get stretched
std::vector<GlyphItem>::iterator pGlyphIterRight = m_GlyphItems.Impl()->begin();
pGlyphIterRight += m_GlyphItems.Impl()->size() - 1;
std::vector<GlyphItem>::iterator pGlyphIter;
// count stretchable glyphs
int nStretchable = 0;
int nMaxGlyphWidth = 0;
for(pGlyphIter = m_GlyphItems.Impl()->begin(); pGlyphIter != pGlyphIterRight; ++pGlyphIter)
{
if( !pGlyphIter->IsDiacritic() )
++nStretchable;
if (nMaxGlyphWidth < pGlyphIter->origWidth())
nMaxGlyphWidth = pGlyphIter->origWidth();
}
// move rightmost glyph to requested position
nOldWidth -= pGlyphIterRight->origWidth();
if( nOldWidth <= 0 )
return;
if( nNewWidth < nMaxGlyphWidth)
nNewWidth = nMaxGlyphWidth;
nNewWidth -= pGlyphIterRight->origWidth();
pGlyphIterRight->m_aLinearPos.setX( nNewWidth );
// justify glyph widths and positions
int nDiffWidth = nNewWidth - nOldWidth;
if( nDiffWidth >= 0) // expanded case
{
// expand width by distributing space between glyphs evenly
int nDeltaSum = 0;
for( pGlyphIter = m_GlyphItems.Impl()->begin(); pGlyphIter != pGlyphIterRight; ++pGlyphIter )
{
// move glyph to justified position
pGlyphIter->m_aLinearPos.AdjustX(nDeltaSum );
// do not stretch non-stretchable glyphs
if( pGlyphIter->IsDiacritic() || (nStretchable <= 0) )
continue;
// distribute extra space equally to stretchable glyphs
int nDeltaWidth = nDiffWidth / nStretchable--;
nDiffWidth -= nDeltaWidth;
pGlyphIter->m_nNewWidth += nDeltaWidth;
nDeltaSum += nDeltaWidth;
}
}
else // condensed case
{
// squeeze width by moving glyphs proportionally
double fSqueeze = static_cast<double>(nNewWidth) / nOldWidth;
if(m_GlyphItems.Impl()->size() > 1)
{
for( pGlyphIter = m_GlyphItems.Impl()->begin(); ++pGlyphIter != pGlyphIterRight;)
{
int nX = pGlyphIter->m_aLinearPos.getX();
nX = static_cast<int>(nX * fSqueeze);
pGlyphIter->m_aLinearPos.setX( nX );
}
}
// adjust glyph widths to new positions
for( pGlyphIter = m_GlyphItems.Impl()->begin(); pGlyphIter != pGlyphIterRight; ++pGlyphIter )
pGlyphIter->m_nNewWidth = pGlyphIter[1].m_aLinearPos.getX() - pGlyphIter[0].m_aLinearPos.getX();
}
}
// returns asian kerning values in quarter of character width units
// to enable automatic halfwidth substitution for fullwidth punctuation
// return value is negative for l, positive for r, zero for neutral
// TODO: handle vertical layout as proposed in commit 43bf2ad49c2b3989bbbe893e4fee2e032a3920f5?
static int lcl_CalcAsianKerning(sal_UCS4 c, bool bLeft)
{
// http://www.asahi-net.or.jp/~sd5a-ucd/freetexts/jis/x4051/1995/appendix.html
static const signed char nTable[0x30] =
{
0, -2, -2, 0, 0, 0, 0, 0, +2, -2, +2, -2, +2, -2, +2, -2,
+2, -2, 0, 0, +2, -2, +2, -2, 0, 0, 0, 0, 0, +2, -2, -2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, -2, +2, +2, -2, -2
};
int nResult = 0;
if( (c >= 0x3000) && (c < 0x3030) )
nResult = nTable[ c - 0x3000 ];
else switch( c )
{
case 0x30FB:
nResult = bLeft ? -1 : +1; // 25% left/right/top/bottom
break;
case 0x2019: case 0x201D:
case 0xFF01: case 0xFF09: case 0xFF0C:
case 0xFF1A: case 0xFF1B:
nResult = -2;
break;
case 0x2018: case 0x201C:
case 0xFF08:
nResult = +2;
break;
default:
break;
}
return nResult;
}
static bool lcl_CanApplyAsianKerning(sal_Unicode cp)
{
return (0x3000 == (cp & 0xFF00)) || (0xFF00 == (cp & 0xFF00)) || (0x2010 == (cp & 0xFFF0));
}
void GenericSalLayout::ApplyAsianKerning(const OUString& rStr)
{
const int nLength = rStr.getLength();
long nOffset = 0;
for (std::vector<GlyphItem>::iterator pGlyphIter = m_GlyphItems.Impl()->begin(),
pGlyphIterEnd = m_GlyphItems.Impl()->end();
pGlyphIter != pGlyphIterEnd; ++pGlyphIter)
{
const int n = pGlyphIter->charPos();
if (n < nLength - 1)
{
// ignore code ranges that are not affected by asian punctuation compression
const sal_Unicode cCurrent = rStr[n];
if (!lcl_CanApplyAsianKerning(cCurrent))
continue;
const sal_Unicode cNext = rStr[n + 1];
if (!lcl_CanApplyAsianKerning(cNext))
continue;
// calculate compression values
const int nKernCurrent = +lcl_CalcAsianKerning(cCurrent, true);
if (nKernCurrent == 0)
continue;
const int nKernNext = -lcl_CalcAsianKerning(cNext, false);
if (nKernNext == 0)
continue;
// apply punctuation compression to logical glyph widths
int nDelta = (nKernCurrent < nKernNext) ? nKernCurrent : nKernNext;
if (nDelta < 0)
{
nDelta = (nDelta * pGlyphIter->origWidth() + 2) / 4;
if( pGlyphIter+1 == pGlyphIterEnd )
pGlyphIter->m_nNewWidth += nDelta;
nOffset += nDelta;
}
}
// adjust the glyph positions to the new glyph widths
if( pGlyphIter+1 != pGlyphIterEnd )
pGlyphIter->m_aLinearPos.AdjustX(nOffset);
}
}
void GenericSalLayout::GetCaretPositions( int nMaxIndex, long* pCaretXArray ) const
{
// initialize result array
for (int i = 0; i < nMaxIndex; ++i)
pCaretXArray[i] = -1;
// calculate caret positions using glyph array
for (auto const& aGlyphItem : *m_GlyphItems.Impl())
{
long nXPos = aGlyphItem.m_aLinearPos.getX();
long nXRight = nXPos + aGlyphItem.origWidth();
int n = aGlyphItem.charPos();
int nCurrIdx = 2 * (n - mnMinCharPos);
// tdf#86399 if this is not the start of a cluster, don't overwrite the caret bounds of the cluster start
if (aGlyphItem.IsInCluster() && pCaretXArray[nCurrIdx] != -1)
continue;
if (!aGlyphItem.IsRTLGlyph() )
{
// normal positions for LTR case
pCaretXArray[ nCurrIdx ] = nXPos;
pCaretXArray[ nCurrIdx+1 ] = nXRight;
}
else
{
// reverse positions for RTL case
pCaretXArray[ nCurrIdx ] = nXRight;
pCaretXArray[ nCurrIdx+1 ] = nXPos;
}
}
}
sal_Int32 GenericSalLayout::GetTextBreak( DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor ) const
{
int nCharCapacity = mnEndCharPos - mnMinCharPos;
std::unique_ptr<DeviceCoordinate[]> const pCharWidths(new DeviceCoordinate[nCharCapacity]);
GetCharWidths(pCharWidths.get());
DeviceCoordinate nWidth = 0;
for( int i = mnMinCharPos; i < mnEndCharPos; ++i )
{
nWidth += pCharWidths[ i - mnMinCharPos ] * nFactor;
if( nWidth > nMaxWidth )
return i;
nWidth += nCharExtra;
}
return -1;
}
bool GenericSalLayout::GetNextGlyph(const GlyphItem** pGlyph,
Point& rPos, int& nStart,
const PhysicalFontFace**) const
{
std::vector<GlyphItem>::const_iterator pGlyphIter = m_GlyphItems.Impl()->begin();
std::vector<GlyphItem>::const_iterator pGlyphIterEnd = m_GlyphItems.Impl()->end();
pGlyphIter += nStart;
// find next glyph in substring
for(; pGlyphIter != pGlyphIterEnd; ++nStart, ++pGlyphIter )
{
int n = pGlyphIter->charPos();
if( (mnMinCharPos <= n) && (n < mnEndCharPos) )
break;
}
// return zero if no more glyph found
if( nStart >= static_cast<int>(m_GlyphItems.Impl()->size()) )
return false;
if( pGlyphIter == pGlyphIterEnd )
return false;
// update return data with glyph info
*pGlyph = &(*pGlyphIter);
++nStart;
// calculate absolute position in pixel units
Point aRelativePos = pGlyphIter->m_aLinearPos;
aRelativePos.setX( aRelativePos.X() / mnUnitsPerPixel );
aRelativePos.setY( aRelativePos.Y() / mnUnitsPerPixel );
rPos = GetDrawPosition( aRelativePos );
return true;
}
void GenericSalLayout::MoveGlyph( int nStart, long nNewXPos )
{
if( nStart >= static_cast<int>(m_GlyphItems.Impl()->size()) )
return;
std::vector<GlyphItem>::iterator pGlyphIter = m_GlyphItems.Impl()->begin();
pGlyphIter += nStart;
// the nNewXPos argument determines the new cell position
// as RTL-glyphs are right justified in their cell
// the cell position needs to be adjusted to the glyph position
if( pGlyphIter->IsRTLGlyph() )
nNewXPos += pGlyphIter->m_nNewWidth - pGlyphIter->origWidth();
// calculate the x-offset to the old position
long nXDelta = nNewXPos - pGlyphIter->m_aLinearPos.getX();
// adjust all following glyph positions if needed
if( nXDelta != 0 )
{
for( std::vector<GlyphItem>::iterator pGlyphIterEnd = m_GlyphItems.Impl()->end(); pGlyphIter != pGlyphIterEnd; ++pGlyphIter )
{
pGlyphIter->m_aLinearPos.AdjustX(nXDelta );
}
}
}
void GenericSalLayout::DropGlyph( int nStart )
{
if( nStart >= static_cast<int>(m_GlyphItems.Impl()->size()))
return;
std::vector<GlyphItem>::iterator pGlyphIter = m_GlyphItems.Impl()->begin();
pGlyphIter += nStart;
pGlyphIter->dropGlyph();
}
void GenericSalLayout::Simplify( bool bIsBase )
{
// remove dropped glyphs inplace
size_t j = 0;
for(size_t i = 0; i < m_GlyphItems.Impl()->size(); i++ )
{
if (bIsBase && (*m_GlyphItems.Impl())[i].IsDropped())
continue;
if (!bIsBase && (*m_GlyphItems.Impl())[i].glyphId() == 0)
continue;
if( i != j )
{
(*m_GlyphItems.Impl())[j] = (*m_GlyphItems.Impl())[i];
}
j += 1;
}
m_GlyphItems.Impl()->erase(m_GlyphItems.Impl()->begin() + j, m_GlyphItems.Impl()->end());
}
MultiSalLayout::MultiSalLayout( std::unique_ptr<SalLayout> pBaseLayout )
: SalLayout()
, mnLevel( 1 )
, mbIncomplete( false )
{
assert(dynamic_cast<GenericSalLayout*>(pBaseLayout.get()));
mpLayouts[ 0 ].reset(static_cast<GenericSalLayout*>(pBaseLayout.release()));
mnUnitsPerPixel = mpLayouts[ 0 ]->GetUnitsPerPixel();
}
void MultiSalLayout::SetIncomplete(bool bIncomplete)
{
mbIncomplete = bIncomplete;
maFallbackRuns[mnLevel-1] = ImplLayoutRuns();
}
MultiSalLayout::~MultiSalLayout()
{
}
void MultiSalLayout::AddFallback( std::unique_ptr<SalLayout> pFallback,
ImplLayoutRuns const & rFallbackRuns)
{
assert(dynamic_cast<GenericSalLayout*>(pFallback.get()));
if( mnLevel >= MAX_FALLBACK )
return;
mpLayouts[ mnLevel ].reset(static_cast<GenericSalLayout*>(pFallback.release()));
maFallbackRuns[ mnLevel-1 ] = rFallbackRuns;
++mnLevel;
}
bool MultiSalLayout::LayoutText( ImplLayoutArgs& rArgs, const SalLayoutGlyphs* )
{
if( mnLevel <= 1 )
return false;
if (!mbIncomplete)
maFallbackRuns[ mnLevel-1 ] = rArgs.maRuns;
return true;
}
void MultiSalLayout::AdjustLayout( ImplLayoutArgs& rArgs )
{
SalLayout::AdjustLayout( rArgs );
ImplLayoutArgs aMultiArgs = rArgs;
std::unique_ptr<DeviceCoordinate[]> pJustificationArray;
if( !rArgs.mpDXArray && rArgs.mnLayoutWidth )
{
// for stretched text in a MultiSalLayout the target width needs to be
// distributed by individually adjusting its virtual character widths
DeviceCoordinate nTargetWidth = aMultiArgs.mnLayoutWidth;
nTargetWidth *= mnUnitsPerPixel; // convert target width to base font units
aMultiArgs.mnLayoutWidth = 0;
// we need to get the original unmodified layouts ready
for( int n = 0; n < mnLevel; ++n )
mpLayouts[n]->SalLayout::AdjustLayout( aMultiArgs );
// then we can measure the unmodified metrics
int nCharCount = rArgs.mnEndCharPos - rArgs.mnMinCharPos;
pJustificationArray.reset(new DeviceCoordinate[nCharCount]);
FillDXArray( pJustificationArray.get() );
// #i17359# multilayout is not simplified yet, so calculating the
// unjustified width needs handholding; also count the number of
// stretchable virtual char widths
DeviceCoordinate nOrigWidth = 0;
int nStretchable = 0;
for( int i = 0; i < nCharCount; ++i )
{
// convert array from widths to sum of widths
nOrigWidth += pJustificationArray[i];
if( pJustificationArray[i] > 0 )
++nStretchable;
}
// now we are able to distribute the extra width over the virtual char widths
if( nOrigWidth && (nTargetWidth != nOrigWidth) )
{
DeviceCoordinate nDiffWidth = nTargetWidth - nOrigWidth;
DeviceCoordinate nWidthSum = 0;
for( int i = 0; i < nCharCount; ++i )
{
DeviceCoordinate nJustWidth = pJustificationArray[i];
if( (nJustWidth > 0) && (nStretchable > 0) )
{
DeviceCoordinate nDeltaWidth = nDiffWidth / nStretchable;
nJustWidth += nDeltaWidth;
nDiffWidth -= nDeltaWidth;
--nStretchable;
}
nWidthSum += nJustWidth;
pJustificationArray[i] = nWidthSum;
}
if( nWidthSum != nTargetWidth )
pJustificationArray[ nCharCount-1 ] = nTargetWidth;
// the justification array is still in base level units
// => convert it to pixel units
if( mnUnitsPerPixel > 1 )
{
for( int i = 0; i < nCharCount; ++i )
{
DeviceCoordinate nVal = pJustificationArray[ i ];
nVal += (mnUnitsPerPixel + 1) / 2;
pJustificationArray[ i ] = nVal / mnUnitsPerPixel;
}
}
// change the mpDXArray temporarily (just for the justification)
aMultiArgs.mpDXArray = pJustificationArray.get();
}
}
// Compute rtl flags, since in some scripts glyphs/char order can be
// reversed for a few character sequences e.g. Myanmar
std::vector<bool> vRtl(rArgs.mnEndCharPos - rArgs.mnMinCharPos, false);
rArgs.ResetPos();
bool bRtl;
int nRunStart, nRunEnd;
while (rArgs.GetNextRun(&nRunStart, &nRunEnd, &bRtl))
{
if (bRtl) std::fill(vRtl.begin() + (nRunStart - rArgs.mnMinCharPos),
vRtl.begin() + (nRunEnd - rArgs.mnMinCharPos), true);
}
rArgs.ResetPos();
// prepare "merge sort"
int nStartOld[ MAX_FALLBACK ];
int nStartNew[ MAX_FALLBACK ];
const GlyphItem* pGlyphs[MAX_FALLBACK];
bool bValid[MAX_FALLBACK] = { false };
Point aPos;
int nLevel = 0, n;
for( n = 0; n < mnLevel; ++n )
{
// now adjust the individual components
if( n > 0 )
{
aMultiArgs.maRuns = maFallbackRuns[ n-1 ];
aMultiArgs.mnFlags |= SalLayoutFlags::ForFallback;
}
mpLayouts[n]->AdjustLayout( aMultiArgs );
// remove unused parts of component
if( n > 0 )
{
if (mbIncomplete && (n == mnLevel-1))
mpLayouts[n]->Simplify( true );
else
mpLayouts[n]->Simplify( false );
}
// prepare merging components
nStartNew[ nLevel ] = nStartOld[ nLevel ] = 0;
bValid[nLevel] = mpLayouts[n]->GetNextGlyph(&pGlyphs[nLevel], aPos, nStartNew[nLevel]);
if( (n > 0) && !bValid[ nLevel ] )
{
// an empty fallback layout can be released
mpLayouts[n].reset();
}
else
{
// reshuffle used fallbacks if needed
if( nLevel != n )
{
mpLayouts[ nLevel ] = std::move(mpLayouts[ n ]);
maFallbackRuns[ nLevel ] = maFallbackRuns[ n ];
}
++nLevel;
}
}
mnLevel = nLevel;
// prepare merge the fallback levels
long nXPos = 0;
double fUnitMul = 1.0;<--- Variable 'fUnitMul' is assigned a value that is never used.
for( n = 0; n < nLevel; ++n )
maFallbackRuns[n].ResetPos();
int nFirstValid = -1;
for( n = 0; n < nLevel; ++n )
{
if(bValid[n])
{
nFirstValid = n;
break;
}
}
assert(nFirstValid >= 0);
// get the next codepoint index that needs fallback
int nActiveCharPos = pGlyphs[nFirstValid]->charPos();
int nActiveCharIndex = nActiveCharPos - mnMinCharPos;
// get the end index of the active run
int nLastRunEndChar = (nActiveCharIndex >= 0 && vRtl[nActiveCharIndex]) ?
rArgs.mnEndCharPos : rArgs.mnMinCharPos - 1;
int nRunVisibleEndChar = pGlyphs[nFirstValid]->charPos();
// merge the fallback levels
while( bValid[nFirstValid] && (nLevel > 0))
{
// find best fallback level
for( n = 0; n < nLevel; ++n )
if( bValid[n] && !maFallbackRuns[n].PosIsInAnyRun( nActiveCharPos ) )
// fallback level n wins when it requested no further fallback
break;
int nFBLevel = n;
if( n < nLevel )
{
// use base(n==0) or fallback(n>=1) level
fUnitMul = mnUnitsPerPixel;
fUnitMul /= mpLayouts[n]->GetUnitsPerPixel();
long nNewPos = static_cast<long>(nXPos/fUnitMul + 0.5);
mpLayouts[n]->MoveGlyph( nStartOld[n], nNewPos );
}
else
{
n = 0; // keep NotDef in base level
fUnitMul = 1.0;
}
if( n > 0 )
{
// drop the NotDef glyphs in the base layout run if a fallback run exists
while (
(maFallbackRuns[n-1].PosIsInRun(pGlyphs[nFirstValid]->charPos())) &&
(!maFallbackRuns[n].PosIsInAnyRun(pGlyphs[nFirstValid]->charPos()))
)
{
mpLayouts[0]->DropGlyph( nStartOld[0] );
nStartOld[0] = nStartNew[0];
bValid[nFirstValid] = mpLayouts[0]->GetNextGlyph(&pGlyphs[nFirstValid], aPos, nStartNew[0]);
if( !bValid[nFirstValid] )
break;
}
}
// skip to end of layout run and calculate its advance width
DeviceCoordinate nRunAdvance = 0;
bool bKeepNotDef = (nFBLevel >= nLevel);
for(;;)
{
nRunAdvance += pGlyphs[n]->m_nNewWidth;
// proceed to next glyph
nStartOld[n] = nStartNew[n];
int nOrigCharPos = pGlyphs[n]->charPos();
bValid[n] = mpLayouts[n]->GetNextGlyph(&pGlyphs[n], aPos, nStartNew[n]);
// break after last glyph of active layout
if( !bValid[n] )
{
// performance optimization (when a fallback layout is no longer needed)
if( n >= nLevel-1 )
--nLevel;
break;
}
//If the next character is one which belongs to the next level, then we
//are finished here for now, and we'll pick up after the next level has
//been processed
if ((n+1 < nLevel) && (pGlyphs[n]->charPos() != nOrigCharPos))
{
if (nOrigCharPos < pGlyphs[n]->charPos())
{
if (pGlyphs[n+1]->charPos() > nOrigCharPos && (pGlyphs[n+1]->charPos() < pGlyphs[n]->charPos()))
break;
}
else if (nOrigCharPos > pGlyphs[n]->charPos())
{
if (pGlyphs[n+1]->charPos() > pGlyphs[n]->charPos() && (pGlyphs[n+1]->charPos() < nOrigCharPos))
break;
}
}
// break at end of layout run
if( n > 0 )
{
// skip until end of fallback run
if (!maFallbackRuns[n-1].PosIsInRun(pGlyphs[n]->charPos()))
break;
}
else
{
// break when a fallback is needed and available
bool bNeedFallback = maFallbackRuns[0].PosIsInRun(pGlyphs[nFirstValid]->charPos());
if( bNeedFallback )
if (!maFallbackRuns[nLevel-1].PosIsInRun(pGlyphs[nFirstValid]->charPos()))
break;
// break when change from resolved to unresolved base layout run
if( bKeepNotDef && !bNeedFallback )
{ maFallbackRuns[0].NextRun(); break; }
bKeepNotDef = bNeedFallback;
}
// check for reordered glyphs
if (aMultiArgs.mpDXArray &&
nRunVisibleEndChar < mnEndCharPos &&
nRunVisibleEndChar >= mnMinCharPos &&
pGlyphs[n]->charPos() < mnEndCharPos &&
pGlyphs[n]->charPos() >= mnMinCharPos)
{
if (vRtl[nActiveCharPos - mnMinCharPos])
{
if (aMultiArgs.mpDXArray[nRunVisibleEndChar-mnMinCharPos]
>= aMultiArgs.mpDXArray[pGlyphs[n]->charPos() - mnMinCharPos])
{
nRunVisibleEndChar = pGlyphs[n]->charPos();
}
}
else if (aMultiArgs.mpDXArray[nRunVisibleEndChar-mnMinCharPos]
<= aMultiArgs.mpDXArray[pGlyphs[n]->charPos() - mnMinCharPos])
{
nRunVisibleEndChar = pGlyphs[n]->charPos();
}
}
}
// if a justification array is available
// => use it directly to calculate the corresponding run width
if( aMultiArgs.mpDXArray )
{
// the run advance is the width from the first char
// in the run to the first char in the next run
nRunAdvance = 0;
nActiveCharIndex = nActiveCharPos - mnMinCharPos;
if (nActiveCharIndex >= 0 && vRtl[nActiveCharIndex])
{
if (nRunVisibleEndChar > mnMinCharPos && nRunVisibleEndChar <= mnEndCharPos)
nRunAdvance -= aMultiArgs.mpDXArray[nRunVisibleEndChar - 1 - mnMinCharPos];
if (nLastRunEndChar > mnMinCharPos && nLastRunEndChar <= mnEndCharPos)
nRunAdvance += aMultiArgs.mpDXArray[nLastRunEndChar - 1 - mnMinCharPos];
}
else
{
if (nRunVisibleEndChar >= mnMinCharPos)
nRunAdvance += aMultiArgs.mpDXArray[nRunVisibleEndChar - mnMinCharPos];
if (nLastRunEndChar >= mnMinCharPos)
nRunAdvance -= aMultiArgs.mpDXArray[nLastRunEndChar - mnMinCharPos];
}
nLastRunEndChar = nRunVisibleEndChar;
nRunVisibleEndChar = pGlyphs[nFirstValid]->charPos();
// the requested width is still in pixel units
// => convert it to base level font units
nRunAdvance *= mnUnitsPerPixel;
}
else
{
// the measured width is still in fallback font units
// => convert it to base level font units
if( n > 0 ) // optimization: because (fUnitMul==1.0) for (n==0)
nRunAdvance = static_cast<long>(nRunAdvance*fUnitMul + 0.5);
}
// calculate new x position (in base level units)
nXPos += nRunAdvance;
// prepare for next fallback run
nActiveCharPos = pGlyphs[nFirstValid]->charPos();
// it essential that the runs don't get ahead of themselves and in the
// if( bKeepNotDef && !bNeedFallback ) statement above, the next run may
// have already been reached on the base level
for( int i = nFBLevel; --i >= 0;)
{
if (maFallbackRuns[i].GetRun(&nRunStart, &nRunEnd, &bRtl))
{
if (bRtl)
{
if (nRunStart > nActiveCharPos)
maFallbackRuns[i].NextRun();
}
else
{
if (nRunEnd <= nActiveCharPos)
maFallbackRuns[i].NextRun();
}
}
}
}
mpLayouts[0]->Simplify( true );
}
void MultiSalLayout::InitFont() const
{
if( mnLevel > 0 )
mpLayouts[0]->InitFont();
}
void MultiSalLayout::DrawText( SalGraphics& rGraphics ) const
{
for( int i = mnLevel; --i >= 0; )
{
SalLayout& rLayout = *mpLayouts[ i ];
rLayout.DrawBase() += maDrawBase;
rLayout.DrawOffset() += maDrawOffset;
rLayout.InitFont();
rLayout.DrawText( rGraphics );
rLayout.DrawOffset() -= maDrawOffset;
rLayout.DrawBase() -= maDrawBase;
}
// NOTE: now the baselevel font is active again
}
sal_Int32 MultiSalLayout::GetTextBreak( DeviceCoordinate nMaxWidth, DeviceCoordinate nCharExtra, int nFactor ) const
{
if( mnLevel <= 0 )
return -1;
if( mnLevel == 1 )
return mpLayouts[0]->GetTextBreak( nMaxWidth, nCharExtra, nFactor );
int nCharCount = mnEndCharPos - mnMinCharPos;
std::unique_ptr<DeviceCoordinate[]> const pCharWidths(new DeviceCoordinate[nCharCount]);
std::unique_ptr<DeviceCoordinate[]> const pFallbackCharWidths(new DeviceCoordinate[nCharCount]);
mpLayouts[0]->FillDXArray( pCharWidths.get() );
for( int n = 1; n < mnLevel; ++n )
{
SalLayout& rLayout = *mpLayouts[ n ];
rLayout.FillDXArray( pFallbackCharWidths.get() );
double fUnitMul = mnUnitsPerPixel;
fUnitMul /= rLayout.GetUnitsPerPixel();
for( int i = 0; i < nCharCount; ++i )
{
if( pCharWidths[ i ] == 0 )
{
DeviceCoordinate w = pFallbackCharWidths[i];
w = static_cast<DeviceCoordinate>(w * fUnitMul + 0.5);
pCharWidths[ i ] = w;
}
}
}
DeviceCoordinate nWidth = 0;
for( int i = 0; i < nCharCount; ++i )
{
nWidth += pCharWidths[ i ] * nFactor;
if( nWidth > nMaxWidth )
return (i + mnMinCharPos);
nWidth += nCharExtra;
}
return -1;
}
DeviceCoordinate MultiSalLayout::FillDXArray( DeviceCoordinate* pCharWidths ) const
{
DeviceCoordinate nMaxWidth = 0;
// prepare merging of fallback levels
std::unique_ptr<DeviceCoordinate[]> pTempWidths;
const int nCharCount = mnEndCharPos - mnMinCharPos;
if( pCharWidths )
{
for( int i = 0; i < nCharCount; ++i )
pCharWidths[i] = 0;
pTempWidths.reset(new DeviceCoordinate[nCharCount]);
}
for( int n = mnLevel; --n >= 0; )
{
// query every fallback level
DeviceCoordinate nTextWidth = mpLayouts[n]->FillDXArray( pTempWidths.get() );
if( !nTextWidth )
continue;
// merge results from current level
double fUnitMul = mnUnitsPerPixel;
fUnitMul /= mpLayouts[n]->GetUnitsPerPixel();
nTextWidth = static_cast<DeviceCoordinate>(nTextWidth * fUnitMul + 0.5);
if( nMaxWidth < nTextWidth )
nMaxWidth = nTextWidth;
if( !pCharWidths )
continue;
// calculate virtual char widths using most probable fallback layout
for( int i = 0; i < nCharCount; ++i )
{
// #i17359# restriction:
// one char cannot be resolved from different fallbacks
if( pCharWidths[i] != 0 )
continue;
DeviceCoordinate nCharWidth = pTempWidths[i];
if( !nCharWidth )
continue;
nCharWidth = static_cast<DeviceCoordinate>(nCharWidth * fUnitMul + 0.5);
pCharWidths[i] = nCharWidth;
}
}
return nMaxWidth;
}
void MultiSalLayout::GetCaretPositions( int nMaxIndex, long* pCaretXArray ) const
{
SalLayout& rLayout = *mpLayouts[ 0 ];
rLayout.GetCaretPositions( nMaxIndex, pCaretXArray );
if( mnLevel > 1 )
{
std::unique_ptr<long[]> const pTempPos(new long[nMaxIndex]);
for( int n = 1; n < mnLevel; ++n )
{
mpLayouts[ n ]->GetCaretPositions( nMaxIndex, pTempPos.get() );
double fUnitMul = mnUnitsPerPixel;
fUnitMul /= mpLayouts[n]->GetUnitsPerPixel();
for( int i = 0; i < nMaxIndex; ++i )
if( pTempPos[i] >= 0 )
{
long w = pTempPos[i];
w = static_cast<long>(w*fUnitMul + 0.5);
pCaretXArray[i] = w;
}
}
}
}
bool MultiSalLayout::GetNextGlyph(const GlyphItem** pGlyph,
Point& rPos, int& nStart,
const PhysicalFontFace** pFallbackFont) const
{
// NOTE: nStart is tagged with current font index
int nLevel = static_cast<unsigned>(nStart) >> GF_FONTSHIFT;
nStart &= ~GF_FONTMASK;
for(; nLevel < mnLevel; ++nLevel, nStart=0 )
{
GenericSalLayout& rLayout = *mpLayouts[ nLevel ];
rLayout.InitFont();
const PhysicalFontFace* pFontFace = rLayout.GetFont().GetFontFace();
if (rLayout.GetNextGlyph(pGlyph, rPos, nStart))
{
int nFontTag = nLevel << GF_FONTSHIFT;
nStart |= nFontTag;
if (pFallbackFont)
*pFallbackFont = pFontFace;
rPos += maDrawBase;
rPos += maDrawOffset;
return true;
}
}
// #111016# reset to base level font when done
mpLayouts[0]->InitFont();
return false;
}
bool MultiSalLayout::GetOutline(basegfx::B2DPolyPolygonVector& rPPV) const
{
bool bRet = false;
for( int i = mnLevel; --i >= 0; )
{
SalLayout& rLayout = *mpLayouts[ i ];
rLayout.DrawBase() = maDrawBase;
rLayout.DrawOffset() += maDrawOffset;
rLayout.InitFont();
bRet |= rLayout.GetOutline(rPPV);
rLayout.DrawOffset() -= maDrawOffset;
}
return bRet;
}
bool MultiSalLayout::IsKashidaPosValid(int nCharPos) const
{
// Check the base layout
bool bValid = mpLayouts[0]->IsKashidaPosValid(nCharPos);
// If base layout returned false, it might be because the character was not
// supported there, so we check fallback layouts.
if (!bValid)
{
for (int i = 1; i < mnLevel; ++i)
{
// - 1 because there is no fallback run for the base layout, IIUC.
if (maFallbackRuns[i - 1].PosIsInAnyRun(nCharPos))
{
bValid = mpLayouts[i]->IsKashidaPosValid(nCharPos);
break;
}
}
}
return bValid;
}
const SalLayoutGlyphs* SalLayout::GetGlyphs() const
{
// No access to the glyphs by default.
return nullptr;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|