File: ODi_Office_Styles.cpp

package info (click to toggle)
abiword 3.0.7~dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 64,700 kB
  • sloc: cpp: 477,933; ansic: 16,754; makefile: 5,834; xml: 3,993; yacc: 1,818; lex: 1,104; perl: 812; python: 413; php: 183; sh: 169
file content (473 lines) | stat: -rw-r--r-- 14,442 bytes parent folder | download | duplicates (7)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* AbiSource
 * 
 * Copyright (C) 2005 Daniel d'Andrada T. de Carvalho
 * <daniel.carvalho@indt.org.br>
 * Copyright (C) 2009 Hubert Figuiere
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301 USA.
 */
 
#include "ut_std_map.h"

// Class definition include
#include "ODi_Office_Styles.h"

// Internal inlcudes
#include "ODi_Style_Style.h"
#include "ODi_Style_PageLayout.h"
#include "ODi_Style_MasterPage.h"
#include "ODi_Style_List.h"
#include "ODi_NotesConfiguration.h"
#include "ODi_ListLevelStyle.h"
#include "ODi_ElementStack.h"
#include "ODi_Abi_Data.h" 

// AbiWord includes
#include <ut_misc.h>
#include <pd_Document.h>
#include <ut_debugmsg.h>


/**
 * Destructor
 */
ODi_Office_Styles::~ODi_Office_Styles() 
{
    UT_map_delete_all_second(m_listStyles);
    UT_map_delete_all_second(m_pageLayoutStyles);
    UT_map_delete_all_second(m_masterPageStyles);
    UT_map_delete_all_second(m_notesConfigurations);
}


/**
 * Adds a <style:style> (ODi_Style_Style class).
 * 
 * @param bAutomatic true if the style is an OpenDocument "automatic style".
 * 
 * @return The address of the newly created ODi_Style_Style or NULL, if the
 *         specified style is not currently supported (like graphic styles).
 */
ODi_Style_Style* ODi_Office_Styles::addStyle(const gchar** ppAtts,
					     ODi_ElementStack& rElementStack,
					     ODi_Abi_Data & rAbiData) 
{

    const gchar* pFamily;
    const gchar* pName;
    const gchar* pDisplayName;
    ODi_Style_Style* pStyle = NULL;
    const ODi_Style_Style* pConstStyle;
    std::string replacementName;
    std::string replacementDisplayName;
    
    pFamily = UT_getAttribute("style:family", ppAtts);
    pName = UT_getAttribute("style:name", ppAtts);
    pDisplayName = UT_getAttribute("style:display-name", ppAtts);
    UT_ASSERT(pName);

    UT_return_val_if_fail(pFamily, pStyle);
    
    if(!strcmp(pFamily, "text")) {
        // AbiWord doesn't support two styles with the same name, differing only
        // on its type (eg: A "Example" character style and a "Example"
        // paragraph style).

        // Do I already have a paragraph style with this name?
        pConstStyle = m_paragraphStyleStyles.getStyle(pName, true);
        if (pConstStyle) {
            replacementName = pName;
            replacementName += "_text";
            
            if (pDisplayName) {
                replacementDisplayName = pDisplayName;
                replacementDisplayName += "_text";
            }
            
            pStyle = m_textStyleStyles.addStyle(ppAtts, rElementStack,
						rAbiData,
                                                &replacementName,
                                                &replacementDisplayName);
        } else {
	  pStyle = m_textStyleStyles.addStyle(ppAtts, rElementStack,rAbiData);
        }
        
    } else if(!strcmp(pFamily, "paragraph")) {
        // AbiWord doesn't support two styles with the same name, differing only
        // on its type (eg: A "Example" character style and a "Example"
        // paragraph style).

        // Do I already have a text style with this name?
        pConstStyle = m_textStyleStyles.getStyle(pName, true);
        if (pConstStyle) {
            replacementName = pName;
            replacementName += "_paragraph";
            
            if (pDisplayName) {
                replacementDisplayName = pDisplayName;
                replacementDisplayName += "_paragraph";
            }
            
            pStyle = m_paragraphStyleStyles.addStyle(ppAtts, 
						     rElementStack,
						     rAbiData,
                                                     &replacementName,
                                                     &replacementDisplayName);
        } else {
	  pStyle = m_paragraphStyleStyles.addStyle(ppAtts, rElementStack,rAbiData);
        }
        
    } else if(!strcmp(pFamily, "section")) {
      pStyle = m_sectionStyleStyles.addStyle(ppAtts, rElementStack,rAbiData);
        
    } else if(!strcmp(pFamily, "graphic")) {
      pStyle = m_graphicStyleStyles.addStyle(ppAtts, rElementStack,rAbiData);
        
    } else if(!strcmp(pFamily, "table")) {
      pStyle = m_tableStyleStyles.addStyle(ppAtts, rElementStack,rAbiData);
        
    } else if(!strcmp(pFamily, "table-column")) {
      pStyle = m_tableColumnStyleStyles.addStyle(ppAtts, rElementStack,rAbiData);
        
    } else if(!strcmp(pFamily, "table-row")) {
      pStyle = m_tableRowStyleStyles.addStyle(ppAtts, rElementStack,rAbiData);
        
    } else if(!strcmp(pFamily, "table-cell")) {
      pStyle = m_tableCellStyleStyles.addStyle(ppAtts, rElementStack,rAbiData);
    }
    
    return pStyle;   
}


/**
 * Adds a <style:page-layout> (ODi_Style_PageLayout class)
 * 
 * @return The address of the newly created ODi_Style_PageLayout.
 */
ODi_Style_PageLayout* ODi_Office_Styles::addPageLayout(const gchar** ppAtts,
                                                     ODi_ElementStack& rElementStack,
                                                     ODi_Abi_Data& rAbiData) {
                               
    const gchar* pAttrValue;
    ODi_Style_PageLayout* pStyle;
                             
    pStyle = new ODi_Style_PageLayout(rElementStack, rAbiData);
    pAttrValue = UT_getAttribute("style:name", ppAtts);
    m_pageLayoutStyles.insert(std::make_pair(pAttrValue, pStyle));
    
    return pStyle;
}


/**
 * Adds a <style:master-page> (ODi_Style_MasterPage class)
 * 
 * @return The address of the newly created ODi_Style_MasterPage.
 */
ODi_Style_MasterPage* ODi_Office_Styles::addMasterPage(const gchar** ppAtts,
                                                     PD_Document* pDocument,
                                                     ODi_ElementStack& rElementStack) {
                                                        
    const gchar* pAttrValue;
    ODi_Style_MasterPage* pStyle;
                             
    pStyle = new ODi_Style_MasterPage(pDocument, rElementStack);
    pAttrValue = UT_getAttribute("style:name", ppAtts);
    m_masterPageStyles.insert(std::make_pair(pAttrValue, pStyle));
    
    return pStyle;
}


/**
 * 
 */
ODi_Style_Style* ODi_Office_Styles::addDefaultStyle(const gchar** ppAtts,
						    ODi_ElementStack& rElementStack, ODi_Abi_Data & rAbiData) {
    
    const gchar* pAttr = NULL;
    
    pAttr = UT_getAttribute("style:family", ppAtts);

    if (pAttr && !strcmp("paragraph", pAttr)) {
        
      return m_paragraphStyleStyles.addDefaultStyle(rElementStack,rAbiData);
        
    } else if (pAttr && !strcmp("table", pAttr)) {
        
      return m_tableStyleStyles.addDefaultStyle(rElementStack,rAbiData);        

    } else {
        // Not currently supported
        return NULL;
    }
}


/**
 * Links every style with its parent and next ones.
 */
void ODi_Office_Styles::_linkStyles() {
    
    m_textStyleStyles.linkStyles();
    m_paragraphStyleStyles.linkStyles();
    m_sectionStyleStyles.linkStyles();
    m_graphicStyleStyles.linkStyles();
    m_tableStyleStyles.linkStyles();
    m_tableColumnStyleStyles.linkStyles();
    m_tableRowStyleStyles.linkStyles();
    m_tableCellStyleStyles.linkStyles();
        
    _linkMasterStyles();
    _linkListStyles();
}


/**
 * Link the master styles to their page layouts
 */
void ODi_Office_Styles::_linkMasterStyles() {
    
    ODi_Style_MasterPage* pMasterStyle;

    for(MasterPageMap::const_iterator iter = m_masterPageStyles.begin();
        iter != m_masterPageStyles.end(); ++iter) {

        pMasterStyle = iter->second;
        
        PageLayoutMap::const_iterator l_iter 
            = m_pageLayoutStyles.find(pMasterStyle->getLayoutName().c_str());
        if(l_iter != m_pageLayoutStyles.end()) {
            pMasterStyle->setLayoutStylePointer(l_iter->second);
        }
    }
}


/**
 * Link list level styles to the text styles that they refer to.
 */
void ODi_Office_Styles::_linkListStyles() const
{
    UT_sint32 count2;
    ODi_ListLevelStyle* pLevelStyle;
    ODi_Style_List* pListStyle;
    const ODi_Style_Style* pStyle;
    
    for(ListMap::const_iterator iter = m_listStyles.begin();
        iter != m_listStyles.end(); ++iter) {

        pListStyle = iter->second;
        UT_continue_if_fail(pListStyle);
        
        count2 = pListStyle->getLevelCount();

        // List levels start from 1.        
        for (UT_sint32 j = 1; j <= count2; j++) {
            pLevelStyle = pListStyle->getLevelStyle(j);
            
            pStyle = getTextStyle(
                pLevelStyle->getTextStyleName()->c_str(), false);
            pLevelStyle->setTextStyle(pStyle);
        }
    }
}


/**
 * 
 */
void ODi_Office_Styles::_defineAbiStyles(PD_Document* pDocument) const 
{
    m_textStyleStyles.defineAbiStyles(pDocument);
    m_paragraphStyleStyles.defineAbiStyles(pDocument);
    
    // AbiWord doesn't have section, graphic and table styles.

    // All styles defined on the content stream are automatic, so, I'm not
    // defining them.

    for(ListMap::const_iterator iter = m_listStyles.begin();
        iter != m_listStyles.end(); ++iter) {

        iter->second->defineAbiList(pDocument);
    }
    
    if (!m_listStyles.empty()) {
        UT_DebugOnly<bool> ok = pDocument->fixListHierarchy();
        UT_ASSERT(ok);
    }

    // I will just use the first master page style (if there is one).
    if (!m_masterPageStyles.empty()) {

        ODi_Style_MasterPage* masterPage = m_masterPageStyles.begin()->second;
        masterPage->definePageSizeTag(pDocument);
    }
}


/**
 * 
 */
void ODi_Office_Styles::_buildAbiPropsAttrString(
                                            ODi_FontFaceDecls& rFontFaceDecls) 
{
    m_textStyleStyles.buildAbiPropsAttrString(rFontFaceDecls);
    m_paragraphStyleStyles.buildAbiPropsAttrString(rFontFaceDecls);
    m_sectionStyleStyles.buildAbiPropsAttrString(rFontFaceDecls);
    m_tableStyleStyles.buildAbiPropsAttrString(rFontFaceDecls);
    

    for(ListMap::const_iterator iter = m_listStyles.begin();
        iter != m_listStyles.end(); ++iter) {

        iter->second->buildAbiPropertiesString();
    }
}


/**
 * Fix any problems encountered on the added styles
 */
void ODi_Office_Styles::_fixStyles() {
    m_textStyleStyles.fixStyles();
    m_paragraphStyleStyles.fixStyles();
}


/**
 * Returns the specified paragraph style
 * 
 * @param pStyleName The name of the style wanted.
 */
const ODi_Style_Style* ODi_Office_Styles::getParagraphStyle(
                                             const gchar* pStyleName,
                                             bool bOnContentStream) const
{
    return m_paragraphStyleStyles.getStyle(pStyleName, bOnContentStream);
}


/**
 * 
 */
const ODi_Style_Style* ODi_Office_Styles::getTextStyle(const gchar* pStyleName,
                                              bool bOnContentStream) const
{
    return m_textStyleStyles.getStyle(pStyleName, bOnContentStream);
}
    

/**
 * 
 */ 
const ODi_Style_Style* ODi_Office_Styles::getSectionStyle(const gchar* pStyleName,
                                                 bool bOnContentStream) const
{
    return m_sectionStyleStyles.getStyle(pStyleName, bOnContentStream);
}


/**
 * 
 */
const ODi_Style_Style* ODi_Office_Styles::getGraphicStyle(const gchar* pStyleName,
                                          bool bOnContentStream) const
{
    return m_graphicStyleStyles.getStyle(pStyleName, bOnContentStream);
}


/**
 * 
 */
const ODi_Style_Style* ODi_Office_Styles::getTableStyle(const gchar* pStyleName,
                                                  bool bOnContentStream) const
{
    return m_tableStyleStyles.getStyle(pStyleName, bOnContentStream);
}


/**
 * 
 */
const ODi_Style_Style* ODi_Office_Styles::getTableColumnStyle(
                                                  const gchar* pStyleName,
                                                  bool bOnContentStream) const
{
    return m_tableColumnStyleStyles.getStyle(pStyleName, bOnContentStream);
}


/**
 * 
 */
const ODi_Style_Style* ODi_Office_Styles::getTableRowStyle(
                                                  const gchar* pStyleName,
                                                  bool bOnContentStream) const
{
    return m_tableRowStyleStyles.getStyle(pStyleName, bOnContentStream);
}


/**
 * 
 */
const ODi_Style_Style* ODi_Office_Styles::getTableCellStyle(
                                                  const gchar* pStyleName,
                                                  bool bOnContentStream) const
{
    return m_tableCellStyleStyles.getStyle(pStyleName, bOnContentStream);
}


/**
 * Adds a list style (<text:list-style>)
 */
ODi_Style_List* ODi_Office_Styles::addList(const gchar** ppAtts,
                                         ODi_ElementStack& rElementStack)
{
    const gchar* pAttrValue = NULL;
    ODi_Style_List* pStyle = NULL;

    pStyle = new ODi_Style_List(rElementStack);
    pAttrValue = UT_getAttribute("style:name", ppAtts);
    xxx_UT_DEBUGMSG(("Adding list |%s| to outline collection \n",pAttrValue));
    m_listStyles.insert(std::make_pair(pAttrValue, pStyle));
            
    return pStyle;
}


/**
 * Adds a notes configuration (<text:notes-configuration>)
 */
ODi_NotesConfiguration* ODi_Office_Styles::addNotesConfiguration(
                                               const gchar** ppAtts,
                                               ODi_ElementStack& rElementStack) 
{
    const gchar* pAttrValue = NULL;
    ODi_NotesConfiguration* pNotesConfig = NULL;

    pNotesConfig = new ODi_NotesConfiguration(rElementStack);
    pAttrValue = UT_getAttribute("text:note-class", ppAtts);
                                            
    m_notesConfigurations.insert(std::make_pair(pAttrValue, pNotesConfig));
    
    return pNotesConfig;
}