1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #include <config_features.h>
11 
12 #include <ostream>
13 #include <vector>
14 #include <tools/long.hxx>
15 
16 #if HAVE_MORE_FONTS
17 // must be declared before inclusion of test/bootstrapfixture.hxx
18 static std::ostream& operator<<(std::ostream& rStream, const std::vector<tools::Long>& rVec);
19 #endif
20 #include <test/bootstrapfixture.hxx>
21 
22 #include <vcl/wrkwin.hxx>
23 #include <vcl/virdev.hxx>
24 // workaround MSVC2015 issue with std::unique_ptr
25 #include <sallayout.hxx>
26 #include <salgdi.hxx>
27 
28 #if HAVE_MORE_FONTS
operator <<(std::ostream & rStream,const std::vector<tools::Long> & rVec)29 static std::ostream& operator<<(std::ostream& rStream, const std::vector<tools::Long>& rVec)
30 {
31     rStream << "{ ";
32     for (size_t i = 0; i < rVec.size() - 1; i++)
33         rStream << rVec[i] << ", ";
34     rStream << rVec.back();
35     rStream << " }";
36     return rStream;
37 }
38 #endif
39 
40 class VclComplexTextTest : public test::BootstrapFixture
41 {
42 public:
VclComplexTextTest()43     VclComplexTextTest() : BootstrapFixture(true, false) {}
44 
45     /// Play with font measuring etc.
46     void testArabic();
47     void testKashida();
48     void testTdf95650(); // Windows-only issue
49     void testCaching();
50 
51     CPPUNIT_TEST_SUITE(VclComplexTextTest);
52     CPPUNIT_TEST(testArabic);
53     CPPUNIT_TEST(testKashida);
54     CPPUNIT_TEST(testTdf95650);
55     CPPUNIT_TEST(testCaching);
56     CPPUNIT_TEST_SUITE_END();
57 };
58 
testArabic()59 void VclComplexTextTest::testArabic()
60 {
61 #if HAVE_MORE_FONTS
62     OUString aOneTwoThree(
63         u"\u0648\u0627\u062d\u0650\u062f\u0652 \u0625\u062b\u064d\u0646\u064a\u0646"
64         " \u062b\u0644\u0627\u062b\u0629\u064c" );
65     ScopedVclPtrInstance<WorkWindow> pWin(static_cast<vcl::Window *>(nullptr));
66     CPPUNIT_ASSERT( pWin );
67 
68     vcl::Font aFont("DejaVu Sans", "Book", Size(0, 12));
69 
70     OutputDevice *pOutDev = pWin->GetOutDev();
71     pOutDev->SetFont( aFont );
72 
73     // absolute character widths AKA text array.
74     std::vector<tools::Long> aRefCharWidths {6,  9,  16, 16, 22, 22, 26, 29, 32, 32,
75                                       36, 40, 49, 53, 56, 63, 63, 66, 72, 72};
76     std::vector<tools::Long> aCharWidths(aOneTwoThree.getLength(), 0);
77     tools::Long nTextWidth = pOutDev->GetTextArray(aOneTwoThree, aCharWidths.data());
78 
79     CPPUNIT_ASSERT_EQUAL(aRefCharWidths, aCharWidths);
80     // this sporadically returns 75 or 74 on some of the windows tinderboxes eg. tb73
81     CPPUNIT_ASSERT_EQUAL(tools::Long(72), nTextWidth);
82     CPPUNIT_ASSERT_EQUAL(nTextWidth, aCharWidths.back());
83 
84     // text advance width and line height
85     CPPUNIT_ASSERT_EQUAL(tools::Long(72), pOutDev->GetTextWidth(aOneTwoThree));
86     CPPUNIT_ASSERT_EQUAL(tools::Long(14), pOutDev->GetTextHeight());
87 
88     // exact bounding rectangle, not essentially the same as text width/height
89     tools::Rectangle aBoundRect;
90     pOutDev->GetTextBoundRect(aBoundRect, aOneTwoThree);
91     CPPUNIT_ASSERT_DOUBLES_EQUAL(0, aBoundRect.getX(), 1); // This sometimes equals to 1
92     CPPUNIT_ASSERT_DOUBLES_EQUAL(1, aBoundRect.getY(), 1);
93     CPPUNIT_ASSERT_DOUBLES_EQUAL(71, aBoundRect.getWidth(), 1); // This sometimes equals to 70
94     CPPUNIT_ASSERT_DOUBLES_EQUAL(15, aBoundRect.getHeight(), 1);
95 
96 #if 0
97     // FIXME: This seems to be wishful thinking, GetTextRect() does not take
98     // rotation into account.
99 
100     // normal orientation
101     tools::Rectangle aInput;
102     tools::Rectangle aRect = pOutDev->GetTextRect( aInput, aOneTwoThree );
103 
104     // now rotate 270 degrees
105     vcl::Font aRotated( aFont );
106     aRotated.SetOrientation( 2700 );
107     pOutDev->SetFont( aRotated );
108     tools::Rectangle aRectRot = pOutDev->GetTextRect( aInput, aOneTwoThree );
109 
110     // Check that we did do the rotation...
111     fprintf( stderr, "%" SAL_PRIdINT64 " %" SAL_PRIdINT64 " %" SAL_PRIdINT64 " %" SAL_PRIdINT64 "\n",
112              sal_Int64(aRect.GetWidth()), sal_Int64(aRect.GetHeight()),
113              sal-Int64(aRectRot.GetWidth()), sal_Int64(aRectRot.GetHeight()) );
114     CPPUNIT_ASSERT( aRectRot.GetWidth() == aRect.GetHeight() );
115     CPPUNIT_ASSERT( aRectRot.GetHeight() == aRect.GetWidth() );
116 #endif
117 #endif
118 }
119 
testKashida()120 void VclComplexTextTest::testKashida()
121 {
122 #if HAVE_MORE_FONTS
123     // Cache the glyph list of some Arabic text.
124     ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
125     auto aText
126         = OUString(u"ﻊﻨﺻﺭ ﺎﻠﻓﻮﺴﻓﻭﺭ ﻊﻨﺻﺭ ﻒﻟﺰﻳ ﺺﻠﺑ. ﺖﺘﻛﻮﻧ ﺎﻟﺩﻭﺭﺓ ﺎﻟﺭﺎﺒﻋﺓ ﻢﻧ ١٥ ﻊﻨﺻﺭﺍ.");
127     std::unique_ptr<SalLayout> pLayout = pOutputDevice->ImplLayout(
128         aText, 0, aText.getLength(), Point(0, 0), 0, nullptr, SalLayoutFlags::GlyphItemsOnly);
129     SalLayoutGlyphs aGlyphs = pLayout->GetGlyphs();
130     CPPUNIT_ASSERT(aGlyphs.IsValid());
131     CPPUNIT_ASSERT(aGlyphs.Impl(0) != nullptr);
132 
133     // Now lay it out using the cached glyph list.
134     ImplLayoutArgs aLayoutArgs(aText, 0, aText.getLength(), SalLayoutFlags::NONE,
135                                pOutputDevice->GetFont().GetLanguageTag(), nullptr);
136     pLayout = pOutputDevice->GetGraphics()->GetTextLayout(0);
137     CPPUNIT_ASSERT(pLayout->LayoutText(aLayoutArgs, aGlyphs.Impl(0)));
138 
139     // Without the accompanying fix in place, this test would have failed with 'assertion failed'.
140     // The kashida justification flag was lost when going via the glyph cache.
141     CPPUNIT_ASSERT(aLayoutArgs.mnFlags & SalLayoutFlags::KashidaJustification);
142 #endif
143 }
144 
testTdf95650()145 void VclComplexTextTest::testTdf95650()
146 {
147     static constexpr OUStringLiteral aTxt =
148         u"\u0131\u0302\u0504\u4E44\u3031\u3030\u3531\u2D30"
149         "\u3037\u0706\u0908\u0B0A\u0D0C\u0F0E\u072E\u100A"
150         "\u0D11\u1312\u0105\u020A\u0512\u1403\u030C\u1528"
151         "\u2931\u632E\u7074\u0D20\u0E0A\u100A\uF00D\u0D20"
152         "\u030A\u0C0B\u20E0\u0A0D";
153     ScopedVclPtrInstance<WorkWindow> pWin(static_cast<vcl::Window *>(nullptr));
154     CPPUNIT_ASSERT(pWin);
155 
156     OutputDevice *pOutDev = pWin->GetOutDev();
157     // Check that the following executes without failing assertion
158     pOutDev->ImplLayout(aTxt, 9, 1, Point(), 0, nullptr, SalLayoutFlags::BiDiRtl);
159 }
160 
testCachedGlyphs(const OUString & aText,const OUString & aFontName=OUString ())161 static void testCachedGlyphs( const OUString& aText, const OUString& aFontName = OUString())
162 {
163     const std::string message = OUString("Font: " + aFontName + ", text: '" + aText + "'").toUtf8().getStr();
164     ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
165     if(!aFontName.isEmpty())
166     {
167         vcl::Font aFont( aFontName, Size(0, 12));
168         pOutputDevice->SetFont( aFont );
169     }
170     // Get the glyphs for the text.
171     std::unique_ptr<SalLayout> pLayout1 = pOutputDevice->ImplLayout(
172         aText, 0, aText.getLength(), Point(0, 0), 0, nullptr, SalLayoutFlags::GlyphItemsOnly);
173     SalLayoutGlyphs aGlyphs1 = pLayout1->GetGlyphs();
174     CPPUNIT_ASSERT_MESSAGE(message, aGlyphs1.IsValid());
175     CPPUNIT_ASSERT_MESSAGE(message, aGlyphs1.Impl(0) != nullptr);
176     // Reuse the cached glyphs to get glyphs again.
177     std::unique_ptr<SalLayout> pLayout2 = pOutputDevice->ImplLayout(
178         aText, 0, aText.getLength(), Point(0, 0), 0, nullptr, SalLayoutFlags::GlyphItemsOnly, nullptr, &aGlyphs1);
179     SalLayoutGlyphs aGlyphs2 = pLayout2->GetGlyphs();
180     CPPUNIT_ASSERT_MESSAGE(message, aGlyphs2.IsValid());
181     // And check it's the same.
182     for( int level = 0; level < MAX_FALLBACK; ++level )
183     {
184         const std::string messageLevel = OString(message.c_str()
185             + OString::Concat(", level: ") + OString::number(level)).getStr();
186         if( aGlyphs1.Impl(level) == nullptr)
187         {
188             CPPUNIT_ASSERT_MESSAGE(messageLevel, aGlyphs2.Impl(level) == nullptr);
189             continue;
190         }
191         const SalLayoutGlyphsImpl* g1 = aGlyphs1.Impl(level);
192         const SalLayoutGlyphsImpl* g2 = aGlyphs2.Impl(level);
193         CPPUNIT_ASSERT_EQUAL_MESSAGE(messageLevel, g1->GetFont().get(), g2->GetFont().get());
194         CPPUNIT_ASSERT_EQUAL_MESSAGE(messageLevel, g1->size(), g2->size());
195         for( size_t i = 0; i < g1->size(); ++i )
196         {
197             CPPUNIT_ASSERT_EQUAL_MESSAGE(messageLevel, (*g1)[i].glyphId(), (*g2)[i].glyphId());
198             CPPUNIT_ASSERT_EQUAL_MESSAGE(messageLevel, (*g1)[i].IsRTLGlyph(), (*g2)[i].IsRTLGlyph());
199             CPPUNIT_ASSERT_EQUAL_MESSAGE(messageLevel, (*g1)[i].IsVertical(), (*g2)[i].IsVertical());
200         }
201     }
202 }
203 
204 // Check that caching using SalLayoutGlyphs gives same results as without caching.
205 // This should preferably use fonts that come with LO.
testCaching()206 void VclComplexTextTest::testCaching()
207 {
208     // Just something basic, no font fallback.
209     testCachedGlyphs( "test", "Dejavu Sans" );
210     // This font does not have latin characters, will need fallback.
211     testCachedGlyphs( "test", "KacstBook" );
212 }
213 
214 CPPUNIT_TEST_SUITE_REGISTRATION(VclComplexTextTest);
215 
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
217