summaryrefslogtreecommitdiff
path: root/vcl/qa/cppunit/textlayout.cxx
blob: 36129aec6920497a5c56c6eef4c970640a1ce3ea (plain)
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
/* -*- 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/.
 */

#include <config_features.h>
#include <config_fonts.h>

#include <test/bootstrapfixture.hxx>

#include <comphelper/processfactory.hxx>

#include <vcl/unohelp.hxx>
#include <vcl/virdev.hxx>

#include <textlayout.hxx>

class VclTextLayoutTest : public test::BootstrapFixture
{
public:
    VclTextLayoutTest()
        : BootstrapFixture(true, false)
    {
    }
};

#if HAVE_MORE_FONTS

CPPUNIT_TEST_FIXTURE(VclTextLayoutTest, testBreakLines_invalid_softbreak)
{
    ScopedVclPtr<VirtualDevice> device = VclPtr<VirtualDevice>::Create(DeviceFormat::WITHOUT_ALPHA);
    device->SetOutputSizePixel(Size(1000, 1000));
    device->SetFont(vcl::Font("DejaVu Sans", "Book", Size(0, 11)));

    vcl::DefaultTextLayout aTextLayout(*device);

    const OUString sTestStr = u"textline_ text_"_ustr;
    const auto nTextWidth = device->GetTextWidth("text");

    css::uno::Reference<css::linguistic2::XHyphenator> xHyph;
    css::uno::Reference<css::i18n::XBreakIterator> xBI = vcl::unohelper::CreateBreakIterator();

    // softbreak cannot be greater than the string length

    const auto nTextLen = 13;

    auto[nBreakPos, nLineWidth]
        = aTextLayout.BreakLine(nTextWidth, sTestStr, xHyph, xBI, false, nTextWidth, nTextLen, 15);

    const sal_Int32 nExpectedBreakPos = 13;
    CPPUNIT_ASSERT_EQUAL(nExpectedBreakPos, nBreakPos);
}

CPPUNIT_TEST_FIXTURE(VclTextLayoutTest, testBreakLines_hyphens)
{
    ScopedVclPtr<VirtualDevice> device = VclPtr<VirtualDevice>::Create(DeviceFormat::WITHOUT_ALPHA);
    device->SetOutputSizePixel(Size(1000, 1000));
    device->SetFont(vcl::Font("DejaVu Sans", "Book", Size(0, 11)));

    vcl::DefaultTextLayout aTextLayout(*device);

    const OUString sTestStr = u"textline text-moretext"_ustr;
    const auto nTextWidth = device->GetTextWidth("textline text-moretex");

    css::uno::Reference<css::uno::XComponentContext> xContext(
        comphelper::getProcessComponentContext());
    css::uno::Reference<css::linguistic2::XLinguServiceManager2> xLinguMgr
        = css::linguistic2::LinguServiceManager::create(xContext);

    css::uno::Reference<css::linguistic2::XHyphenator> xHyph = xLinguMgr->getHyphenator();
    css::uno::Reference<css::i18n::XBreakIterator> xBI = vcl::unohelper::CreateBreakIterator();

    auto[nBreakPos, nLineWidth]
        = aTextLayout.BreakLine(nTextWidth, sTestStr, xHyph, xBI, true, nTextWidth, 13, 12);

    const sal_Int32 nExpectedBreakPos = 13;
    CPPUNIT_ASSERT_EQUAL(nExpectedBreakPos, nBreakPos);
}

CPPUNIT_TEST_FIXTURE(VclTextLayoutTest, testBreakLines_hyphen_word_under_two_chars)
{
    ScopedVclPtr<VirtualDevice> device = VclPtr<VirtualDevice>::Create(DeviceFormat::WITHOUT_ALPHA);
    device->SetOutputSizePixel(Size(1000, 1000));
    device->SetFont(vcl::Font("DejaVu Sans", "Book", Size(0, 11)));

    vcl::DefaultTextLayout aTextLayout(*device);

    const OUString sTestStr = u"textline text-moretext"_ustr;
    const auto nTextWidth = device->GetTextWidth("te-moretex");

    css::uno::Reference<css::uno::XComponentContext> xContext(
        comphelper::getProcessComponentContext());
    css::uno::Reference<css::linguistic2::XLinguServiceManager2> xLinguMgr
        = css::linguistic2::LinguServiceManager::create(xContext);

    css::uno::Reference<css::linguistic2::XHyphenator> xHyph = xLinguMgr->getHyphenator();
    css::uno::Reference<css::i18n::XBreakIterator> xBI = vcl::unohelper::CreateBreakIterator();

    auto[nBreakPos, nLineWidth]
        = aTextLayout.BreakLine(nTextWidth, sTestStr, xHyph, xBI, true, nTextWidth, 2, 10);

    const sal_Int32 nExpectedBreakPos = 2;
    CPPUNIT_ASSERT_EQUAL(nExpectedBreakPos, nBreakPos);
}

#endif

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */