1 // Copyright 2016 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 "core/fpdfapi/parser/cpdf_string.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
12 #include "core/fxcrt/fx_stream.h"
13 #include "third_party/base/ptr_util.h"
14 
CPDF_String()15 CPDF_String::CPDF_String() : m_bHex(false) {}
16 
CPDF_String(WeakPtr<ByteStringPool> pPool,const ByteString & str,bool bHex)17 CPDF_String::CPDF_String(WeakPtr<ByteStringPool> pPool,
18                          const ByteString& str,
19                          bool bHex)
20     : m_String(str), m_bHex(bHex) {
21   if (pPool)
22     m_String = pPool->Intern(m_String);
23 }
24 
CPDF_String(WeakPtr<ByteStringPool> pPool,const WideString & str)25 CPDF_String::CPDF_String(WeakPtr<ByteStringPool> pPool, const WideString& str)
26     : m_String(PDF_EncodeText(str)), m_bHex(false) {
27   if (pPool)
28     m_String = pPool->Intern(m_String);
29 }
30 
~CPDF_String()31 CPDF_String::~CPDF_String() {}
32 
GetType() const33 CPDF_Object::Type CPDF_String::GetType() const {
34   return STRING;
35 }
36 
Clone() const37 std::unique_ptr<CPDF_Object> CPDF_String::Clone() const {
38   auto pRet = pdfium::MakeUnique<CPDF_String>();
39   pRet->m_String = m_String;
40   pRet->m_bHex = m_bHex;
41   return std::move(pRet);
42 }
43 
GetString() const44 ByteString CPDF_String::GetString() const {
45   return m_String;
46 }
47 
SetString(const ByteString & str)48 void CPDF_String::SetString(const ByteString& str) {
49   m_String = str;
50 }
51 
IsString() const52 bool CPDF_String::IsString() const {
53   return true;
54 }
55 
AsString()56 CPDF_String* CPDF_String::AsString() {
57   return this;
58 }
59 
AsString() const60 const CPDF_String* CPDF_String::AsString() const {
61   return this;
62 }
63 
GetUnicodeText() const64 WideString CPDF_String::GetUnicodeText() const {
65   return PDF_DecodeText(m_String);
66 }
67 
WriteTo(IFX_ArchiveStream * archive) const68 bool CPDF_String::WriteTo(IFX_ArchiveStream* archive) const {
69   return archive->WriteString(
70       PDF_EncodeString(GetString(), IsHex()).AsStringView());
71 }
72