• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "xfa/fgas/layout/cfx_rtfbreak.h"
8 
9 #include <memory>
10 #include <utility>
11 
12 #include "core/fxge/cfx_font.h"
13 #include "core/fxge/cfx_gemodule.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/xfa_unit_test_support.h"
16 #include "third_party/base/ptr_util.h"
17 #include "xfa/fgas/font/cfgas_fontmgr.h"
18 #include "xfa/fgas/font/cfgas_gefont.h"
19 #include "xfa/fgas/layout/cfx_char.h"
20 
21 class CFX_RTFBreakTest : public testing::Test {
22  public:
SetUp()23   void SetUp() override {
24     font_ =
25         CFGAS_GEFont::LoadFont(L"Arial Black", 0, 0, GetGlobalFontManager());
26     ASSERT_TRUE(font_.Get());
27   }
28 
CreateBreak(uint32_t layout_styles)29   std::unique_ptr<CFX_RTFBreak> CreateBreak(uint32_t layout_styles) {
30     auto rtf_break = pdfium::MakeUnique<CFX_RTFBreak>(layout_styles);
31     rtf_break->SetFont(font_);
32     return rtf_break;
33   }
34 
35  private:
36   RetainPtr<CFGAS_GEFont> font_;
37 };
38 
39 // As soon as you get one of the control characters the break is complete
40 // and must be consumed before you get any more characters ....
41 
TEST_F(CFX_RTFBreakTest,AddChars)42 TEST_F(CFX_RTFBreakTest, AddChars) {
43   auto rtf_break = CreateBreak(FX_LAYOUTSTYLE_ExpandTab);
44 
45   WideString str(L"Input String.");
46   for (wchar_t ch : str)
47     EXPECT_EQ(CFX_BreakType::None, rtf_break->AppendChar(ch));
48 
49   EXPECT_EQ(CFX_BreakType::Paragraph, rtf_break->AppendChar(L'\n'));
50   ASSERT_EQ(1, rtf_break->CountBreakPieces());
51   EXPECT_EQ(str + L"\n", rtf_break->GetBreakPieceUnstable(0)->GetString());
52 
53   rtf_break->ClearBreakPieces();
54   rtf_break->Reset();
55   EXPECT_EQ(0, rtf_break->GetCurrentLineForTesting()->GetLineEnd());
56 
57   str = L"Second str.";
58   for (wchar_t ch : str)
59     EXPECT_EQ(CFX_BreakType::None, rtf_break->AppendChar(ch));
60 
61   // Force the end of the break at the end of the string.
62   rtf_break->EndBreak(CFX_BreakType::Paragraph);
63   ASSERT_EQ(1, rtf_break->CountBreakPieces());
64   EXPECT_EQ(str, rtf_break->GetBreakPieceUnstable(0)->GetString());
65 }
66 
TEST_F(CFX_RTFBreakTest,ControlCharacters)67 TEST_F(CFX_RTFBreakTest, ControlCharacters) {
68   auto rtf_break = CreateBreak(FX_LAYOUTSTYLE_ExpandTab);
69   EXPECT_EQ(CFX_BreakType::Line, rtf_break->AppendChar(L'\v'));
70   EXPECT_EQ(CFX_BreakType::Page, rtf_break->AppendChar(L'\f'));
71   const wchar_t kUnicodeParagraphSeparator = 0x2029;
72   EXPECT_EQ(CFX_BreakType::Paragraph,
73             rtf_break->AppendChar(kUnicodeParagraphSeparator));
74   EXPECT_EQ(CFX_BreakType::Paragraph, rtf_break->AppendChar(L'\n'));
75 
76   ASSERT_EQ(1, rtf_break->CountBreakPieces());
77   EXPECT_EQ(L"\v", rtf_break->GetBreakPieceUnstable(0)->GetString());
78 }
79 
TEST_F(CFX_RTFBreakTest,BidiLine)80 TEST_F(CFX_RTFBreakTest, BidiLine) {
81   auto rtf_break = CreateBreak(FX_LAYOUTSTYLE_ExpandTab);
82   rtf_break->SetLineBreakTolerance(1);
83   rtf_break->SetFontSize(12);
84 
85   WideString input = WideString::FromUTF8(ByteStringView("\xa\x0\xa\xa", 4));
86   for (wchar_t ch : input)
87     rtf_break->AppendChar(ch);
88 
89   std::vector<CFX_Char> chars =
90       rtf_break->GetCurrentLineForTesting()->m_LineChars;
91   CFX_Char::BidiLine(&chars, chars.size());
92   EXPECT_EQ(3u, chars.size());
93 }
94