File: ODi_Style_Style_Family.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 (520 lines) | stat: -rw-r--r-- 15,856 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* AbiSource
 * 
 * Copyright (C) 2005 Daniel d'Andrada T. de Carvalho
 * <daniel.carvalho@indt.org.br>
 * Copryight (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.
 */
 
// Class definition include
#include "ODi_Style_Style_Family.h"

// Internal includes
#include "ODi_ElementStack.h"
#include "ODi_Abi_Data.h"

// AbiWord includes
#include "ut_misc.h"
#include "ut_std_map.h"

/**
 * Destructor
 */
ODi_Style_Style_Family::~ODi_Style_Style_Family() 
{
    UT_map_delete_all_second(m_styles);
    UT_map_delete_all_second(m_styles_contentStream);
    
    DELETEP(m_pDefaultStyle);
}


/**
 * @param pReplacementName Name to replace the actual name of the style that is
 *                         being added. NULL if the name shouldn't be replaced.
 */
ODi_Style_Style* ODi_Style_Style_Family::addStyle(const gchar** ppAtts,
                             ODi_ElementStack& rElementStack,
			     ODi_Abi_Data & rAbiData,
                             std::string* pReplacementName,
                             std::string* pReplacementDisplayName) {
                                
    ODi_Style_Style* pStyle = NULL;
    bool bOnContentStream;
    const gchar* pName;
    
    bOnContentStream = rElementStack.hasElement("office:document-content");
    
    pName = UT_getAttribute("style:name", ppAtts);
    UT_ASSERT(pName);

    
    if (bOnContentStream) {
        
        if (pReplacementName) {
            StyleMap::const_iterator iter = m_styles_contentStream.find(pReplacementName->c_str());

            if (iter == m_styles_contentStream.end()) {
	      pStyle = new ODi_Style_Style(rElementStack,rAbiData);
                
                m_styles_contentStream.insert(std::make_pair(pReplacementName->c_str(),
                                                                  pStyle));
                                                   
                pStyle->setName(*pReplacementName);
                pStyle->setDisplayName(*pReplacementDisplayName);
            }
            
        } else {
            StyleMap::const_iterator iter = m_styles_contentStream.find(pName);

            if (iter == m_styles_contentStream.end()) {
	      pStyle = new ODi_Style_Style(rElementStack,rAbiData);
                
                m_styles_contentStream.insert(std::make_pair(pName, pStyle));
            }
        }
        
        
        
    } else {
        
        if (pReplacementName) {
            StyleMap::const_iterator iter = m_styles.find(pReplacementName->c_str());
            
            if (iter == m_styles.end()) {
	      pStyle = new ODi_Style_Style(rElementStack,rAbiData);
                
                m_styles.insert(std::make_pair(pReplacementName->c_str(), pStyle));
                                                   
                pStyle->setName(*pReplacementName);
                pStyle->setDisplayName(*pReplacementDisplayName);
            }
            
        } else {
            StyleMap::const_iterator iter = m_styles.find(pName);
            
            if (iter == m_styles.end()) {
	      pStyle = new ODi_Style_Style(rElementStack,rAbiData);
                
                m_styles.insert(std::make_pair(pName, pStyle));
            }
        }
    }


    if (pReplacementName != NULL) {
        std::string originalName = pName;
        
        if (bOnContentStream) {
            m_removedStyleStyles_contentStream[pName]
				= pReplacementName->c_str();
        } else {
            m_removedStyleStyles[pName] = pReplacementName->c_str();
        }
	}
    
    return pStyle;
}



/** Remove any style that doesn't have properties */
void ODi_Style_Style_Family::_removeEmptyStyles(const StyleMap & map, bool bOnContentStream)
{
    ODi_Style_Style* pStyle = NULL;
    bool foundNone;
    

    if(map.empty()) {
        return;
    }
    // TODO still very inefficient... but more than it used
    // to be.
    do {

        foundNone = true;

        for (StyleMap::const_iterator iter = map.begin();
             iter != map.end(); ++iter) {
            if ( !iter->second->hasProperties() ) {
                pStyle = iter->second;
                foundNone = false;
                break;
            }
        }


        if (pStyle) {
            removeStyleStyle(pStyle, bOnContentStream);
            DELETEP(pStyle);
        }
    } while(!foundNone);
}


/**
 * Fix any problems encountered on the added styles.
 */
void ODi_Style_Style_Family::fixStyles()
{
    // Problem 1: We can't have styles without properties
    //
    // The "Standard" paragraph style usually comes empty
    // (I have never seen otherwise)
    
    _removeEmptyStyles(m_styles, false);
    _removeEmptyStyles(m_styles_contentStream, true);
}


void ODi_Style_Style_Family::_buildAbiPropsAttrString(ODi_FontFaceDecls& rFontFaceDecls,
                                                      const StyleMap & map)
{
    for(StyleMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) {
        iter->second->buildAbiPropsAttrString(rFontFaceDecls);
    }
}

/**
 * 
 */
void ODi_Style_Style_Family::buildAbiPropsAttrString(
                                            ODi_FontFaceDecls& rFontFaceDecls)
{
    if (m_pDefaultStyle != NULL) {
        m_pDefaultStyle->buildAbiPropsAttrString(rFontFaceDecls);
    }
    
    _buildAbiPropsAttrString(rFontFaceDecls, m_styles);
    _buildAbiPropsAttrString(rFontFaceDecls, m_styles_contentStream);
}



void ODi_Style_Style_Family::_reparentStyles(const StyleMap & map, const std::string & removedName,
                                             const std::string & replacementName)
{
    for(StyleMap::const_iterator iter = map.begin(); iter != map.end(); ++iter) {
        
        ODi_Style_Style* pStyle = iter->second;
        
        if (pStyle->getParentName() == removedName) {
            pStyle->setParentName(replacementName);
        }
        
        if (pStyle->getNextStyleName() == removedName) {
            pStyle->setNextStyleName(replacementName);
        }
    }
}


/**
 * 
 */
void ODi_Style_Style_Family::removeStyleStyle(ODi_Style_Style* pRemovedStyle,
                                             bool bOnContentStream) 
{
    std::string replacementName;


    _findSuitableReplacement(replacementName, pRemovedStyle, bOnContentStream);
    
    // Remove the style itself
    if (bOnContentStream) {
        m_styles_contentStream.erase(pRemovedStyle->getName().c_str());
            
        m_removedStyleStyles_contentStream[pRemovedStyle->getName().c_str()]
			= replacementName.c_str();
    } else {
        m_styles.erase(pRemovedStyle->getName().c_str());
        m_removedStyleStyles[pRemovedStyle->getName().c_str()] = replacementName.c_str();
    }
    


    // Fix all references to it.
    // Note that automatic styles can't refer to each other.
    
    if (pRemovedStyle->isAutomatic()) {
        // It's an automatic style, nobody have references him.
        return;
    }
    
    if (replacementName == "<NULL>") {
        replacementName.clear();
    }
    
    // Some automatic styles defined on the content stream may have
    // references to this common style.
    _reparentStyles(m_styles_contentStream, pRemovedStyle->getName(),
                    replacementName);
    // Now fix references from the styles defined on the styles stream.
    _reparentStyles(m_styles, pRemovedStyle->getName(),
                    replacementName);    
}


/**
 * 
 */
void ODi_Style_Style_Family::defineAbiStyles(PD_Document* pDocument) const 
{
    if (m_pDefaultStyle) {
        m_pDefaultStyle->defineAbiStyle(pDocument);
    }
    
    for(StyleMap::const_iterator iter = m_styles.begin();
        iter != m_styles.end(); ++iter) {

        iter->second->defineAbiStyle(pDocument);
    }
}


/**
 * Finds a suitable replacement for the style that will be removed.
 * 
 * A suitable replacement is a parent style that has properties or, if none is
 * found, the default paragraph style.
 * 
 * @param rReplacementName Will receive the name of the replacement style.
 * @param pRemovedStyle The style that will be removed.
 */
void ODi_Style_Style_Family::_findSuitableReplacement(
                                            std::string& rReplacementName,
                                            const ODi_Style_Style* pRemovedStyle,
                                            bool bOnContentStream)
{
    // Test for a "dead-end"
    if (pRemovedStyle->getParentName().empty()) {
        
        if (m_pDefaultStyle) {
            // Pretty simple. We use the default style.
            if (*(pRemovedStyle->getFamily()) == "paragraph") {
                // AbiWord uses "Normal" as the name of its default style.
                rReplacementName = "Normal";
            } else {
                rReplacementName = m_pDefaultStyle->getName();
            }
        } else {
            // We have no choice. There will be no substitute for this style.
            rReplacementName = "<NULL>";
        }
        
        return;
    }

    ODi_Style_Style* pStyle = NULL;
    
    if (bOnContentStream) {
        StyleMap::const_iterator iter = m_styles_contentStream.find(pRemovedStyle->getParentName().c_str());
        if(iter !=  m_styles_contentStream.end()) {
            pStyle = iter->second;
        }
    }

    if (!pStyle) {
        // Must be a regular style, defined on the Styles stream.
        StyleMap::const_iterator iter = m_styles.find(pRemovedStyle->getParentName().c_str());
        if(iter !=  m_styles.end()) {
            pStyle = iter->second;
        }
    }
    
    
    if (pStyle) {
        if (pStyle->hasProperties()) {
            // Alright, we've found it.
            rReplacementName = pStyle->getName();
        } 
        else {
            // Let's look deeper
            _findSuitableReplacement(rReplacementName, pStyle, bOnContentStream);
        }
    } else {
        std::string aString;
        // Was this parent already removed?
        if (bOnContentStream) {
            aString = m_removedStyleStyles_contentStream[pRemovedStyle->getParentName().c_str()];
        }
        
        if (!pStyle) {
            aString = m_removedStyleStyles[pRemovedStyle->getParentName().c_str()];
        }
        
        if(!aString.empty()) {
            rReplacementName = aString;
        }
        else {
            // I give up...
            if (m_pDefaultStyle) {
                // Pretty simple. We use the default style.
                if (*(pRemovedStyle->getFamily()) == "paragraph") {
                    // AbiWord uses "Normal" as the name of its default style.
                    rReplacementName = "Normal";
                } 
                else {
                    rReplacementName = m_pDefaultStyle->getName();
                }
            } 
            else {
                // We have no choice. There will be no substitute for this style.
                rReplacementName = "<NULL>";
            }
        }
    }
}


/**
 * It links, if applicable, each style with its parent and its next style.
 * 
 * By "linking" I mean that a given style will have a pointer to its parent
 * and its next style.
 */
void ODi_Style_Style_Family::linkStyles() 
{
    _linkStyles(m_styles, false);
    _linkStyles(m_styles_contentStream, true);
}


/**
 * 
 */
const ODi_Style_Style* ODi_Style_Style_Family::getStyle(const gchar* pStyleName,
                                                      bool bOnContentStream) const
{
    UT_return_val_if_fail(pStyleName, NULL);

    const ODi_Style_Style* pStyle = NULL;
    
    // Is it the default style?
    if (m_pDefaultStyle && (m_pDefaultStyle->getName() == pStyleName)) {
        pStyle = m_pDefaultStyle;
    }
    
    if (!pStyle) {
        // It's not the default style. Let's search our style lists.
        
        if (bOnContentStream) {
            StyleMap::const_iterator iter = m_styles_contentStream.find(pStyleName);
            if(iter !=  m_styles_contentStream.end()) {
                pStyle = iter->second;
            }
        }

        if (!pStyle) {
            // Must be a regular style, defined on the Styles stream.
            StyleMap::const_iterator iter = m_styles.find(pStyleName);
            if(iter !=  m_styles.end()) {
                pStyle = iter->second;
            }
        }
    }
    
    if (!pStyle) {
        // We haven't found it. Have we removed it (done on _fixStyles())?
        
        std::string replacementName;
        
        if (bOnContentStream) {
            std::map<std::string, std::string>::const_iterator iter
                = m_removedStyleStyles_contentStream.find(pStyleName);
            if(iter != m_removedStyleStyles_contentStream.end()) {
                replacementName = iter->second;
            }
        }

        if (replacementName.empty()) {
            std::map<std::string, std::string>::const_iterator iter
                = m_removedStyleStyles.find(pStyleName);
            if(iter != m_removedStyleStyles.end()) {
                replacementName = iter->second;
            }
        }
        
        if (!replacementName.empty()) {
            // We will send back its replacement.
            return getStyle(replacementName.c_str(),
                            bOnContentStream);
        } else {
            // This style never existed.
            // Let's return the default one instead, if there is one.
            if (m_pDefaultStyle != NULL) {
                pStyle = m_pDefaultStyle;
            } 
            else {
                pStyle = NULL;
            }
        }
        
    }
    
    return pStyle;
}


/**
 * Helper function for linkStyles()
 */   
void ODi_Style_Style_Family::_linkStyles(const StyleMap & map, bool onContentStream)
{
    ODi_Style_Style* pStyle;
    const ODi_Style_Style* pOtherStyle;
    
    for(StyleMap::const_iterator iter = map.begin();
        iter != map.end(); ++iter) {

        pStyle = iter->second;
        
        // Link to its parent style, if there is one.
        if (!pStyle->getParentName().empty()) {
            
            pOtherStyle = getStyle(pStyle->getParentName().c_str(),
                                         onContentStream);

            if (pOtherStyle) {
                pStyle->setParentStylePointer(pOtherStyle);
            } 
            else {
                // We don't have this style!
                // Let's pretend that it never existed.
                UT_ASSERT_HARMLESS(pOtherStyle);
                pStyle->setParentName(NULL);
            }
        }
        
        // Link to its next style, if there is one.
        if (!pStyle->getNextStyleName().empty()) {
            
            pOtherStyle = getStyle(pStyle->getNextStyleName().c_str(),
                                         onContentStream);

            if (pOtherStyle) {
                pStyle->setNextStylePointer(pOtherStyle);
            } 
            else {
                // We don't have this style!
                // Let's pretend that it never existed.
                UT_ASSERT_HARMLESS(pOtherStyle);
                pStyle->setNextStyleName(NULL);
            }
        }
    }
}