1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /* AbiSource
4  *
5  * Copyright (C) 2007 Philippe Milot <PhilMilot@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  */
22 
23 // Class definition include
24 #include <OXML_Element_Text.h>
25 
26 // AbiWord includes
27 #include <ut_types.h>
28 #include <ut_string.h>
29 #include <pd_Document.h>
30 
OXML_Element_Text()31 OXML_Element_Text::OXML_Element_Text() :
32 	OXML_Element("", T_TAG, SPAN),
33 	m_pString(NULL),
34 	m_range(UNKNOWN_RANGE)
35 {
36 }
37 
~OXML_Element_Text()38 OXML_Element_Text::~OXML_Element_Text()
39 {
40 	DELETEP(m_pString);
41 }
42 
OXML_Element_Text(const gchar * text,int length)43 OXML_Element_Text::OXML_Element_Text(const gchar * text, int length) :
44 	OXML_Element("", T_TAG, SPAN)
45 {
46 	setText(text, length);
47 }
48 
setText(const gchar * text,int)49 void OXML_Element_Text::setText(const gchar * text, int /*length*/)
50 {
51 	try {
52 		std::string str(text);
53 		m_pString = new UT_UCS4String(str);
54 	} catch(...) {
55 		m_pString = NULL;
56 	}
57 }
58 
getText_UCS4String()59 const UT_UCS4Char * OXML_Element_Text::getText_UCS4String()
60 {
61 	UT_return_val_if_fail(m_pString != NULL, NULL);
62 	return m_pString->ucs4_str();
63 }
64 
getText()65 const char * OXML_Element_Text::getText()
66 {
67 	UT_return_val_if_fail(m_pString != NULL, NULL);
68 	if(getType() == LIST)
69 	{
70 		const char* pStr = m_pString->utf8_str();
71 		if(pStr && (strlen(pStr) > 0) && (pStr[0] == '\t'))
72 			return pStr+1; //get rid of the initial tab
73 	}
74 	return m_pString->utf8_str();
75 }
76 
serialize(IE_Exp_OpenXML * exporter)77 UT_Error OXML_Element_Text::serialize(IE_Exp_OpenXML* exporter)
78 {
79 	UT_Error err = UT_OK;
80 	bool bList = false;
81 	const gchar* szValue = NULL;
82 	err = getAttribute("style", szValue);
83 	if(err == UT_OK && szValue)
84 	{
85 		if(!strcmp(szValue, "List Paragraph"))
86 		{
87 			bList = true;
88 		}
89 	}
90 	err = getAttribute("type", szValue);
91 	if(err == UT_OK && szValue)
92 	{
93 		if(!strcmp(szValue, "list_label"))
94 		{
95 			bList = true;
96 		}
97 	}
98 	err = getProperty("list-style", szValue);
99 	if(err == UT_OK && szValue)
100 	{
101 		bList = true;
102 	}
103 
104 	err = exporter->startText(TARGET);
105 	if(err != UT_OK)
106 		return err;
107 
108 	const UT_UCS4Char * text = getText_UCS4String();
109 	if(text)
110 		err = exporter->writeText(TARGET, text, bList);
111 
112 	if(err != UT_OK)
113 		return err;
114 
115 	return exporter->finishText(TARGET);
116 }
117 
addToPT(PD_Document * pDocument)118 UT_Error OXML_Element_Text::addToPT(PD_Document * pDocument)
119 {
120 	UT_return_val_if_fail(pDocument != NULL && m_pString != NULL, UT_ERROR);
121 
122 	//a text tag does not have children, so no need to call addChildrenToPT()
123 	return pDocument->appendSpan(m_pString->ucs4_str(), m_pString->length()) ? UT_OK : UT_ERROR;
124 }
125 
126