File: textstyleformatterfactory.cpp

package info (click to toggle)
source-highlight 3.1.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,332 kB
  • ctags: 5,233
  • sloc: sh: 11,270; cpp: 10,206; ansic: 9,515; makefile: 1,865; lex: 1,200; yacc: 1,021; php: 213; perl: 211; awk: 98; erlang: 94; lisp: 90; java: 75; ruby: 69; python: 61; asm: 43; ml: 38; ada: 36; haskell: 27; xml: 23; cs: 11; sql: 8; tcl: 6; sed: 4
file content (167 lines) | stat: -rw-r--r-- 5,646 bytes parent folder | download | duplicates (6)
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
//
// Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2004-2008
//
// Copyright: See COPYING file that comes with this distribution
//

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "textstyleformatterfactory.h"
#include "formattermanager.h"
#include "textstylebuilder.h"
#include "textstyleformatter.h"
#include "keys.h"

namespace srchilite {

TextStyleFormatterFactory::TextStyleFormatterFactory(TextStylesPtr _textStyles,
        PreFormatter *_preformatter, CTagsFormatter *_ctagsFormatter,
        FormatterManager *_formatterManager) :
    textStyles(_textStyles), preformatter(_preformatter),
            ctagsFormatter(_ctagsFormatter),
            formatterManager(_formatterManager) {
}

TextStyleFormatterFactory::~TextStyleFormatterFactory() {
}

bool TextStyleFormatterFactory::createFormatter(const string &key,
        const string &color, const string &bgcolor,
        StyleConstantsPtr styleconstants) {
    if (formatterManager->hasFormatter(key).get())
        return false;

    if (!textStyles->onestyle.empty()) {
        TextStyleFormatter *textStyleFormatter = new TextStyleFormatter(textStyles->onestyle.subst_style(key));
        formatterManager->addFormatter(key, FormatterPtr(textStyleFormatter));
        formatterCollection.push_back(textStyleFormatter);

        bool noref = false; // whether this element must not have references

        // we still check the noref property in the style file, and set the
        // ctags formatter accordingly
        if (styleconstants.get()) {
            for (StyleConstantsIterator it = styleconstants->begin(); it
                    != styleconstants->end(); ++it) {
                if ((*it) == ISNOREF) {
                    noref = true;
                    break;
                }
            }
        }

        if (!noref)
            textStyleFormatter->setCTagsFormatter(ctagsFormatter);

        return true;
    }

    TextStyleBuilder textStyleBuilder(textStyles->starting_template,
            textStyles->style_separator);

    textStyleBuilder.start();
    bool noref = false; // whether this element must not have references

    if (key == "linenum") {
        // for line numbers we have the style for the container of the linenum
        // and the style of the line number (e.g., the line number can be
        // generated in bold, and inside a table column)
        textStyleBuilder.add(textStyles->linenum);
    }

    if (styleconstants.get()) {
        for (StyleConstantsIterator it = styleconstants->begin(); it
                != styleconstants->end(); ++it) {
            switch (*it) {
            case ISBOLD:
                textStyleBuilder.add(textStyles->bold);
                break;
            case ISITALIC:
                textStyleBuilder.add(textStyles->italics);
                break;
            case ISUNDERLINE:
                textStyleBuilder.add(textStyles->underline);
                break;
            case ISFIXED:
                textStyleBuilder.add(textStyles->fixed);
                break;
            case ISNOTFIXED:
                textStyleBuilder.add(textStyles->notfixed);
                break;
            case ISNOREF:
                noref = true;
                break;
            }
        }
    }

    if (color.size()) {
        textStyleBuilder.add(textStyles->color.subst_style(preprocessColor(color)));
    }

    if (bgcolor.size()) {
        textStyleBuilder.add(textStyles->bg_color.subst_style(preprocessColor(bgcolor)));
    }

    TextStyle style = textStyleBuilder.end();

    TextStyleFormatter *textStyleFormatter = new TextStyleFormatter(style);
    if (!noref)
        textStyleFormatter->setCTagsFormatter(ctagsFormatter);

    formatterManager->addFormatter(key, FormatterPtr(textStyleFormatter));
    formatterCollection.push_back(textStyleFormatter);
    return true;
}

string TextStyleFormatterFactory::preprocessColor(const string &color) {
    if (color[0] == '"' && color[color.size()-1] == '"')
        return color.substr(1, color.size()-2);
    else
        return textStyles->colorMap->getColor(color);
}

bool TextStyleFormatterFactory::createMissingFormatter(const string &key1,
        const string &key2) {
    // the formatter for key1 is not missing
    if (formatterManager->hasFormatter(key1).get())
        return false;

    // the formatter for key2 is missing
    if (!formatterManager->hasFormatter(key2).get())
        return false;

    // the formatter for key1 will have the same style as the formatter for key2
    formatterManager->addFormatter(key1, formatterManager->getFormatter(key2));

    // there's no need to update the formatterCollection, since no new formatter was created

    return true;
}

void TextStyleFormatterFactory::addDefaultFormatter() {
    FormatterPtr defaultFormatter = formatterManager->hasFormatter(NORMAL);

    // if the style for normal was not specified
    if (!defaultFormatter.get()) {
        TextStyleFormatter *textStyleFormatter = 0;

        if (textStyles->onestyle.empty())
            textStyleFormatter = new TextStyleFormatter();
        else
            textStyleFormatter = new TextStyleFormatter(textStyles->onestyle.subst_style(NORMAL));

        defaultFormatter = FormatterPtr(textStyleFormatter);
        formatterManager->addFormatter(NORMAL, defaultFormatter);
        textStyleFormatter->setCTagsFormatter(ctagsFormatter);
        formatterCollection.push_back(textStyleFormatter);
    } else {
        formatterCollection.push_back(dynamic_cast<TextStyleFormatter *>(defaultFormatter.get()));
    }

    formatterManager->setDefaultFormatter(defaultFormatter);
}

}