1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <config_features.h>
11 #include <config_fonts.h>
15 #include <tools/long.hxx>
16 #include <vcl/glyphitemcache.hxx>
19 // must be declared before inclusion of test/bootstrapfixture.hxx
20 static std::ostream
& operator<<(std::ostream
& rStream
, const std::vector
<sal_Int32
>& rVec
);
22 #include <test/bootstrapfixture.hxx>
24 #include <vcl/wrkwin.hxx>
25 #include <vcl/virdev.hxx>
26 // workaround MSVC2015 issue with std::unique_ptr
27 #include <sallayout.hxx>
31 #include <ImplLayoutArgs.hxx>
34 static std::ostream
& operator<<(std::ostream
& rStream
, const std::vector
<sal_Int32
>& rVec
)
37 for (size_t i
= 0; i
< rVec
.size() - 1; i
++)
38 rStream
<< rVec
[i
] << ", ";
39 rStream
<< rVec
.back();
45 class VclComplexTextTest
: public test::BootstrapFixture
48 VclComplexTextTest() : BootstrapFixture(true, false) {}
50 /// Play with font measuring etc.
52 void testTdf95650(); // Windows-only issue
54 void testCachingSubstring();
58 CPPUNIT_TEST_SUITE(VclComplexTextTest
);
59 CPPUNIT_TEST(testArabic
);
60 CPPUNIT_TEST(testTdf95650
);
61 CPPUNIT_TEST(testCaching
);
62 CPPUNIT_TEST(testCachingSubstring
);
63 CPPUNIT_TEST(testCaret
);
64 CPPUNIT_TEST(testGdefCaret
);
65 CPPUNIT_TEST_SUITE_END();
68 void VclComplexTextTest::testArabic()
71 OUString
aOneTwoThree(u
"واحِدْ إثٍنين ثلاثةٌ");
73 vcl::Font
aFont("DejaVu Sans", "Book", Size(0, 12));
75 ScopedVclPtrInstance
<VirtualDevice
> pOutDev
;
76 pOutDev
->SetFont( aFont
);
78 // absolute character widths AKA text array.
79 std::vector
<sal_Int32
> aRefCharWidths
{6, 9, 16, 16, 22, 22, 26, 29, 32, 32,
80 36, 40, 49, 53, 56, 63, 63, 66, 72, 72};
81 KernArray aCharWidths
;
82 tools::Long nTextWidth
= pOutDev
->GetTextArray(aOneTwoThree
, &aCharWidths
);
84 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
85 // this sporadically returns 75 or 74 on some of the windows tinderboxes eg. tb73
86 CPPUNIT_ASSERT_EQUAL(tools::Long(72), nTextWidth
);
87 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
89 // text advance width and line height
90 CPPUNIT_ASSERT_EQUAL(tools::Long(72), pOutDev
->GetTextWidth(aOneTwoThree
));
91 CPPUNIT_ASSERT_EQUAL(tools::Long(14), pOutDev
->GetTextHeight());
93 // exact bounding rectangle, not essentially the same as text width/height
94 tools::Rectangle aBoundRect
;
95 pOutDev
->GetTextBoundRect(aBoundRect
, aOneTwoThree
);
96 CPPUNIT_ASSERT_DOUBLES_EQUAL(0, aBoundRect
.Left(), 1); // This sometimes equals to 1
97 CPPUNIT_ASSERT_DOUBLES_EQUAL(1, aBoundRect
.Top(), 1);
98 CPPUNIT_ASSERT_DOUBLES_EQUAL(71, aBoundRect
.GetWidth(), 2); // This sometimes equals to 70
99 CPPUNIT_ASSERT_DOUBLES_EQUAL(15, aBoundRect
.getOpenHeight(), 1);
101 // normal orientation
102 tools::Rectangle aInput
;
103 tools::Rectangle aRect
= pOutDev
->GetTextRect( aInput
, aOneTwoThree
);
105 // now rotate 270 degrees
106 vcl::Font
aRotated( aFont
);
107 aRotated
.SetOrientation( 2700_deg10
);
108 pOutDev
->SetFont( aRotated
);
109 tools::Rectangle aRectRot
= pOutDev
->GetTextRect( aInput
, aOneTwoThree
);
111 // Check that we did do the rotation...
112 CPPUNIT_ASSERT_EQUAL( aRectRot
.GetWidth(), aRect
.GetHeight() );
113 CPPUNIT_ASSERT_EQUAL( aRectRot
.GetHeight(), aRect
.GetWidth() );
117 void VclComplexTextTest::testTdf95650()
119 static constexpr OUStringLiteral aTxt
=
120 u
"\u0131\u0302\u0504\u4E44\u3031\u3030\u3531\u2D30"
121 "\u3037\u0706\u0908\u0B0A\u0D0C\u0F0E\u072E\u100A"
122 "\u0D11\u1312\u0105\u020A\u0512\u1403\u030C\u1528"
123 "\u2931\u632E\u7074\u0D20\u0E0A\u100A\uF00D\u0D20"
124 "\u030A\u0C0B\u20E0\u0A0D";
125 ScopedVclPtrInstance
<VirtualDevice
> pOutDev
;
126 // Check that the following executes without failing assertion
127 pOutDev
->ImplLayout(aTxt
, 9, 1, Point(), 0, {}, {}, SalLayoutFlags::BiDiRtl
);
130 static void checkCompareGlyphs( const SalLayoutGlyphs
& aGlyphs1
, const SalLayoutGlyphs
& aGlyphs2
,
131 const std::string
& message
)
133 CPPUNIT_ASSERT_EQUAL_MESSAGE(message
, aGlyphs1
.IsValid(), aGlyphs2
.IsValid());
134 // And check it's the same.
135 for( int level
= 0; level
< MAX_FALLBACK
; ++level
)
137 const std::string messageLevel
= OString(message
.c_str()
138 + OString::Concat(", level: ") + OString::number(level
)).getStr();
139 if( aGlyphs1
.Impl(level
) == nullptr)
141 CPPUNIT_ASSERT_MESSAGE(messageLevel
, aGlyphs2
.Impl(level
) == nullptr);
144 const SalLayoutGlyphsImpl
* g1
= aGlyphs1
.Impl(level
);
145 const SalLayoutGlyphsImpl
* g2
= aGlyphs2
.Impl(level
);
146 CPPUNIT_ASSERT_EQUAL_MESSAGE(messageLevel
, g1
->GetFont().get(), g2
->GetFont().get());
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(messageLevel
, g1
->size(), g2
->size());
148 for( size_t i
= 0; i
< g1
->size(); ++i
)
150 const bool equal
= (*g1
)[i
].isLayoutEquivalent((*g2
)[i
]);
151 CPPUNIT_ASSERT_MESSAGE(messageLevel
, equal
);
156 static void testCachedGlyphs( const OUString
& aText
, const OUString
& aFontName
)
158 const std::string message
= OUString("Font: " + aFontName
+ ", text: '" + aText
+ "'").toUtf8().getStr();
159 ScopedVclPtrInstance
<VirtualDevice
> pOutputDevice
;
160 vcl::Font
aFont( aFontName
, Size(0, 12));
161 pOutputDevice
->SetFont( aFont
);
162 SalLayoutGlyphsCache::self()->clear();
163 // Get the glyphs for the text.
164 std::unique_ptr
<SalLayout
> pLayout1
= pOutputDevice
->ImplLayout(
165 aText
, 0, aText
.getLength(), Point(0, 0), 0, {}, {}, SalLayoutFlags::GlyphItemsOnly
);
166 SalLayoutGlyphs aGlyphs1
= pLayout1
->GetGlyphs();
167 // Reuse the cached glyphs to get glyphs again.
168 std::unique_ptr
<SalLayout
> pLayout2
= pOutputDevice
->ImplLayout(
169 aText
, 0, aText
.getLength(), Point(0, 0), 0, {}, {}, SalLayoutFlags::GlyphItemsOnly
, nullptr, &aGlyphs1
);
170 SalLayoutGlyphs aGlyphs2
= pLayout2
->GetGlyphs();
171 checkCompareGlyphs(aGlyphs1
, aGlyphs2
, message
+ " (reuse)");
172 // Get cached glyphs from SalLayoutGlyphsCache.
173 const SalLayoutGlyphs
* aGlyphs3
= SalLayoutGlyphsCache::self()->GetLayoutGlyphs(
174 pOutputDevice
, aText
, 0, aText
.getLength(), 0);
175 CPPUNIT_ASSERT_MESSAGE(message
, aGlyphs3
!= nullptr);
176 checkCompareGlyphs(aGlyphs1
, *aGlyphs3
, message
+ " (cache)");
179 // Check that caching using SalLayoutGlyphs gives same results as without caching.
180 // This should preferably use fonts that come with LO.
181 void VclComplexTextTest::testCaching()
183 // Just something basic, no font fallback.
184 testCachedGlyphs( "test", "Dejavu Sans" );
185 // This font does not have latin characters, will need fallback.
186 testCachedGlyphs( "test", "Noto Kufi Arabic" );
189 static void testCachedGlyphsSubstring( const OUString
& aText
, const OUString
& aFontName
, bool rtl
)
191 const std::string prefix
= OUString("Font: " + aFontName
+ ", text: '" + aText
+ "'").toUtf8().getStr();
192 ScopedVclPtrInstance
<VirtualDevice
> pOutputDevice
;
193 // BiDiStrong is needed, otherwise SalLayoutGlyphsImpl::cloneCharRange() will not do anything.
194 vcl::text::ComplexTextLayoutFlags layoutFlags
= vcl::text::ComplexTextLayoutFlags::BiDiStrong
;
196 layoutFlags
|= vcl::text::ComplexTextLayoutFlags::BiDiRtl
;
197 pOutputDevice
->SetLayoutMode( layoutFlags
);
198 vcl::Font
aFont( aFontName
, Size(0, 12));
199 pOutputDevice
->SetFont( aFont
);
200 SalLayoutGlyphsCache::self()->clear();
201 std::shared_ptr
<const vcl::text::TextLayoutCache
> layoutCache
= OutputDevice::CreateTextLayoutCache(aText
);
202 // Get the glyphs for the entire text once, to ensure the cache can built subsets from it.
203 pOutputDevice
->ImplLayout( aText
, 0, aText
.getLength(), Point(0, 0), 0, {}, {}, SalLayoutFlags::GlyphItemsOnly
,
205 // Now check for all subsets. Some of them possibly do not make sense in practice, but the code
206 // should cope with them.
207 for( sal_Int32 len
= 1; len
<= aText
.getLength(); ++len
)
208 for( sal_Int32 pos
= 0; pos
< aText
.getLength() - len
; ++pos
)
210 std::string message
= prefix
+ " (" + std::to_string(pos
) + "/" + std::to_string(len
) + ")";
211 std::unique_ptr
<SalLayout
> pLayout1
= pOutputDevice
->ImplLayout(
212 aText
, pos
, len
, Point(0, 0), 0, {}, {}, SalLayoutFlags::GlyphItemsOnly
, layoutCache
.get());
213 SalLayoutGlyphs aGlyphs1
= pLayout1
->GetGlyphs();
214 const SalLayoutGlyphs
* aGlyphs2
= SalLayoutGlyphsCache::self()->GetLayoutGlyphs(
215 pOutputDevice
, aText
, pos
, len
, 0, layoutCache
.get());
216 CPPUNIT_ASSERT_MESSAGE(message
, aGlyphs2
!= nullptr);
217 checkCompareGlyphs(aGlyphs1
, *aGlyphs2
, message
);
222 // Check that SalLayoutGlyphsCache works properly when it builds a subset
223 // of glyphs using SalLayoutGlyphsImpl::cloneCharRange().
224 // This should preferably use fonts that come with LO.
225 void VclComplexTextTest::testCachingSubstring()
227 // Just something basic.
228 testCachedGlyphsSubstring( "test", "Dejavu Sans", false );
229 // And complex arabic text, taken from tdf104649.docx .
230 OUString
text(u
"فصل (پاره 2): درخواست حاجت از ديگران و برآوردن حاجت ديگران 90");
231 testCachedGlyphsSubstring( text
, "Dejavu Sans", true );
232 // The text is RTL, but Writer will sometimes try to lay it out as LTR, for whatever reason
233 // (tdf#149264)./ So make sure that gets handled properly too (SalLayoutGlyphsCache should
234 // not use glyph subsets in that case).
235 testCachedGlyphsSubstring( text
, "Dejavu Sans", false );
238 void VclComplexTextTest::testCaret()
241 // Test caret placement in fonts *without* ligature carets in GDEF table.
243 vcl::Font
aFont("DejaVu Sans", "Book", Size(0, 200));
245 ScopedVclPtrInstance
<VirtualDevice
> pOutDev
;
246 pOutDev
->SetFont( aFont
);
249 KernArray aCharWidths
;
250 std::vector
<sal_Int32
> aRefCharWidths
;
251 tools::Long nTextWidth
, nTextWidth2
;
256 // 1) Regular DX array, the ligature width is given to the first components
257 // and the next ones are all zero width.
258 aRefCharWidths
= { 114, 114, 178, 234, 353, 353 };
259 nTextWidth
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/false);
260 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
261 CPPUNIT_ASSERT_EQUAL(tools::Long(353), nTextWidth
);
262 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
264 // 2) Caret placement DX array, ligature width is distributed over its
266 aRefCharWidths
= { 57, 114, 178, 234, 293, 353 };
267 nTextWidth
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/true);
268 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
269 CPPUNIT_ASSERT_EQUAL(tools::Long(353), nTextWidth
);
270 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
272 // 3) caret placement with combining marks, they should not add to ligature
274 aText
= u
"لَاَ بلَاَ";
275 aRefCharWidths
= { 57, 57, 114, 114, 178, 234, 293, 293, 353, 353 };
276 nTextWidth2
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/true);
277 CPPUNIT_ASSERT_EQUAL(aCharWidths
[0], aCharWidths
[1]);
278 CPPUNIT_ASSERT_EQUAL(aCharWidths
[2], aCharWidths
[3]);
279 CPPUNIT_ASSERT_EQUAL(aCharWidths
[6], aCharWidths
[7]);
280 CPPUNIT_ASSERT_EQUAL(aCharWidths
[8], aCharWidths
[9]);
281 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
282 CPPUNIT_ASSERT_EQUAL(tools::Long(353), nTextWidth2
);
283 CPPUNIT_ASSERT_EQUAL(nTextWidth
, nTextWidth2
);
284 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
287 aText
= u
"fi fl ffi ffl";
289 // 1) Regular DX array, the ligature width is given to the first components
290 // and the next ones are all zero width.
291 aRefCharWidths
= { 126, 126, 190, 316, 316, 380, 573, 573, 573, 637, 830, 830, 830 };
292 nTextWidth
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/false);
293 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
294 CPPUNIT_ASSERT_EQUAL(tools::Long(830), nTextWidth
);
295 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
297 // 2) Caret placement DX array, ligature width is distributed over its
299 aRefCharWidths
= { 63, 126, 190, 253, 316, 380, 444, 508, 573, 637, 701, 765, 830 };
300 nTextWidth
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/true);
301 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
302 CPPUNIT_ASSERT_EQUAL(tools::Long(830), nTextWidth
);
303 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
307 void VclComplexTextTest::testGdefCaret()
310 // Test caret placement in fonts *with* ligature carets in GDEF table.
312 ScopedVclPtrInstance
<VirtualDevice
> pOutDev
;
316 KernArray aCharWidths
;
317 std::vector
<sal_Int32
> aRefCharWidths
;
318 tools::Long nTextWidth
, nTextWidth2
;
321 aFont
= vcl::Font("Noto Naskh Arabic", "Regular", Size(0, 200));
322 pOutDev
->SetFont(aFont
);
326 // 1) Regular DX array, the ligature width is given to the first components
327 // and the next ones are all zero width.
328 aRefCharWidths
= { 104, 104, 148, 203, 325, 325 };
329 nTextWidth
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/false);
330 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
331 CPPUNIT_ASSERT_EQUAL(tools::Long(325), nTextWidth
);
332 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
334 // 2) Caret placement DX array, ligature width is distributed over its
336 aRefCharWidths
= { 53, 104, 148, 203, 265, 325 };
337 nTextWidth
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/true);
338 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
339 CPPUNIT_ASSERT_EQUAL(tools::Long(325), nTextWidth
);
340 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
342 // 3) caret placement with combining marks, they should not add to ligature
344 aText
= u
"لَاَ بلَاَ";
345 aRefCharWidths
= { 53, 53, 104, 104, 148, 203, 265, 265, 325, 325 };
346 nTextWidth2
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/true);
347 CPPUNIT_ASSERT_EQUAL(aCharWidths
[0], aCharWidths
[1]);
348 CPPUNIT_ASSERT_EQUAL(aCharWidths
[2], aCharWidths
[3]);
349 CPPUNIT_ASSERT_EQUAL(aCharWidths
[6], aCharWidths
[7]);
350 CPPUNIT_ASSERT_EQUAL(aCharWidths
[8], aCharWidths
[9]);
351 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
352 CPPUNIT_ASSERT_EQUAL(tools::Long(325), nTextWidth2
);
353 CPPUNIT_ASSERT_EQUAL(nTextWidth
, nTextWidth2
);
354 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
357 aFont
= vcl::Font("Amiri", "Regular", Size(0, 200));
358 pOutDev
->SetFont(aFont
);
360 aText
= u
"fi ffi fl ffl fb ffb";
362 // 1) Regular DX array, the ligature width is given to the first components
363 // and the next ones are all zero width.
364 aRefCharWidths
= { 104, 104, 162, 321, 321, 321, 379, 487, 487, 545, 708,
365 708, 708, 766, 926, 926, 984, 1198, 1198, 1198 };
366 nTextWidth
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/false);
367 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
368 CPPUNIT_ASSERT_EQUAL(tools::Long(1198), nTextWidth
);
369 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
371 // 2) Caret placement DX array, ligature width is distributed over its
373 aRefCharWidths
= { 53, 104, 162, 215, 269, 321, 379, 433, 487, 545, 599,
374 654, 708, 766, 826, 926, 984, 1038, 1097, 1198 };
375 nTextWidth
= pOutDev
->GetTextArray(aText
, &aCharWidths
, 0, -1, /*bCaret*/true);
376 CPPUNIT_ASSERT_EQUAL(aRefCharWidths
, aCharWidths
.get_subunit_array());
377 CPPUNIT_ASSERT_EQUAL(tools::Long(1198), nTextWidth
);
378 CPPUNIT_ASSERT_EQUAL(sal_Int32(nTextWidth
), aCharWidths
.back());
382 CPPUNIT_TEST_SUITE_REGISTRATION(VclComplexTextTest
);
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */