Rev 20448 | Rev 20462 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
20448 | fschmid | 1 | /* |
2 | Copyright (C) 2010, 2011 Brad Hards <bradh@frogmouth.net> |
||
3 | |||
4 | This library is free software: you can redistribute it and/or modify |
||
5 | it under the terms of the GNU Lesser General Public License as published by |
||
6 | the Free Software Foundation, either version 2.1 of the License, or |
||
7 | (at your option) any later version. |
||
8 | |||
9 | This library is distributed in the hope that it will be useful, |
||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
12 | GNU Lesser General Public License for more details. |
||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public License |
||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
16 | */ |
||
17 | |||
18 | #include "StyleSheetDestination.h" |
||
19 | |||
20 | #include "rtfreader.h" |
||
21 | #include "controlword.h" |
||
20461 | fschmid | 22 | #include "commonstrings.h" |
20448 | fschmid | 23 | |
24 | namespace RtfReader |
||
25 | { |
||
26 | |||
20461 | fschmid | 27 | StyleSheetDestination::StyleSheetDestination(Reader *reader, AbstractRtfOutput *output, const QString &name) : Destination(reader, output, name) |
28 | { |
||
29 | m_currentStyleHandleNumber = 0; /* default */ |
||
30 | m_textStyle.setParent(CommonStrings::DefaultParagraphStyle); |
||
31 | m_textStyle.setLineSpacingMode(ParagraphStyle::AutomaticLineSpacing); |
||
32 | m_textStyle.charStyle().setFontSize(120.0); |
||
33 | QList<ParagraphStyle::TabRecord> tbs; |
||
34 | tbs.clear(); |
||
35 | m_textStyle.setTabValues(tbs); |
||
36 | } |
||
20448 | fschmid | 37 | |
20461 | fschmid | 38 | StyleSheetDestination::~StyleSheetDestination() |
39 | { |
||
40 | } |
||
20448 | fschmid | 41 | |
20461 | fschmid | 42 | void StyleSheetDestination::handleControlWord(const QString &controlWord, bool hasValue, const int value) |
43 | { |
||
44 | if (controlWord == "ql") |
||
45 | m_textStyle.setAlignment(ParagraphStyle::Leftaligned); |
||
46 | else if (controlWord == "qj") |
||
47 | m_textStyle.setAlignment(ParagraphStyle::Justified); |
||
48 | else if (controlWord == "qc") |
||
49 | m_textStyle.setAlignment(ParagraphStyle::Centered); |
||
50 | else if (controlWord == "qr") |
||
51 | m_textStyle.setAlignment(ParagraphStyle::Rightaligned); |
||
52 | else if ((controlWord == "li") && hasValue) |
||
53 | m_textStyle.setLeftMargin(pixelsFromTwips(value)); |
||
54 | else if ((controlWord == "ri") && hasValue) |
||
55 | m_textStyle.setRightMargin(pixelsFromTwips(value)); |
||
56 | else if ((controlWord == "sb") && hasValue) |
||
57 | m_textStyle.setGapBefore(pixelsFromTwips(value)); |
||
58 | else if (controlWord == "sb") |
||
59 | m_textStyle.setGapBefore(pixelsFromTwips(0)); |
||
60 | else if ((controlWord == "sa") && hasValue) |
||
61 | m_textStyle.setGapAfter(pixelsFromTwips(value)); |
||
62 | else if (controlWord == "sa") |
||
63 | m_textStyle.setGapAfter(pixelsFromTwips(0)); |
||
64 | else if (controlWord == "sl") |
||
65 | { |
||
66 | if (value == 0) |
||
67 | m_textStyle.setLineSpacingMode(ParagraphStyle::AutomaticLineSpacing); |
||
68 | else |
||
69 | { |
||
70 | m_textStyle.setLineSpacingMode(ParagraphStyle::FixedLineSpacing); |
||
71 | m_textStyle.setLineSpacing(pixelsFromTwips(qAbs(value))); |
||
72 | } |
||
73 | } |
||
74 | else if (controlWord == "fs") |
||
75 | { |
||
76 | if (hasValue && ( value != 0)) |
||
77 | m_textStyle.charStyle().setFontSize(value * 5.0); |
||
78 | else |
||
79 | m_textStyle.charStyle().setFontSize(120.0); |
||
80 | } |
||
81 | else if (controlWord == "f") |
||
82 | m_textStyle.charStyle().setFontVariant(QString("%1").arg(value)); |
||
83 | else if (controlWord == "ul") |
||
84 | { |
||
85 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
86 | if (!hasValue || (hasValue && value != 0)) |
||
87 | styleEffects |= ScStyle_Underline; |
||
88 | else |
||
89 | styleEffects &= ~ScStyle_Underline; |
||
90 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
91 | } |
||
92 | else if (controlWord == "ulw") |
||
93 | { |
||
94 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
95 | if (!hasValue || (hasValue && value != 0)) |
||
96 | styleEffects |= ScStyle_UnderlineWords; |
||
97 | else |
||
98 | styleEffects &= ~ScStyle_UnderlineWords; |
||
99 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
100 | } |
||
101 | else if (controlWord == "outl") |
||
102 | { |
||
103 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
104 | if (!hasValue || (hasValue && value != 0)) |
||
105 | styleEffects |= ScStyle_Outline; |
||
106 | else |
||
107 | styleEffects &= ~ScStyle_Outline; |
||
108 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
109 | } |
||
110 | else if (controlWord == "shad") |
||
111 | { |
||
112 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
113 | if (!hasValue || (hasValue && value != 0)) |
||
114 | styleEffects |= ScStyle_Shadowed; |
||
115 | else |
||
116 | styleEffects &= ~ScStyle_Shadowed; |
||
117 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
118 | } |
||
119 | else if (controlWord == "scaps") |
||
120 | { |
||
121 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
122 | if (!hasValue || (hasValue && value != 0)) |
||
123 | styleEffects |= ScStyle_SmallCaps; |
||
124 | else |
||
125 | styleEffects &= ~ScStyle_SmallCaps; |
||
126 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
127 | } |
||
128 | else if (controlWord == "caps") |
||
129 | { |
||
130 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
131 | if (!hasValue || (hasValue && value != 0)) |
||
132 | styleEffects |= ScStyle_AllCaps; |
||
133 | else |
||
134 | styleEffects &= ~ScStyle_AllCaps; |
||
135 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
136 | } |
||
137 | else if (controlWord == "strike") |
||
138 | { |
||
139 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
140 | if (!hasValue || (hasValue && value != 0)) |
||
141 | styleEffects |= ScStyle_Strikethrough; |
||
142 | else |
||
143 | styleEffects &= ~ScStyle_Strikethrough; |
||
144 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
145 | } |
||
146 | else if (controlWord == "super") |
||
147 | { |
||
148 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
149 | styleEffects |= ScStyle_Superscript; |
||
150 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
151 | } |
||
152 | else if (controlWord == "sub") |
||
153 | { |
||
154 | StyleFlag styleEffects = m_textStyle.charStyle().effects(); |
||
155 | styleEffects |= ScStyle_Subscript; |
||
156 | m_textStyle.charStyle().setFeatures(styleEffects.featureList()); |
||
157 | } |
||
158 | else if (controlWord == "charscalex") |
||
159 | m_textStyle.charStyle().setScaleH(value * 10.0); |
||
160 | else if (controlWord == "dn") |
||
161 | m_textStyle.charStyle().setBaselineOffset((-value * 10000 / 2) / m_textStyle.charStyle().fontSize()); |
||
162 | else if (controlWord == "up") |
||
163 | m_textStyle.charStyle().setBaselineOffset((value * 10000 / 2) / m_textStyle.charStyle().fontSize()); |
||
164 | else if (controlWord == "expnd") |
||
165 | m_textStyle.charStyle().setTracking((value * 10000 / 4) / m_textStyle.charStyle().fontSize()); |
||
166 | else if (controlWord == "expndtw") |
||
167 | m_textStyle.charStyle().setTracking((pixelsFromTwips(value) * 10000) / m_textStyle.charStyle().fontSize()); |
||
168 | else if ((controlWord == "s") && hasValue) |
||
169 | m_currentStyleHandleNumber = value; |
||
170 | // else |
||
171 | // qDebug() << "unhandled control word in StyleSheetDestination:" << controlWord; |
||
20448 | fschmid | 172 | } |
173 | |||
20461 | fschmid | 174 | void StyleSheetDestination::handlePlainText(const QByteArray &plainText) |
175 | { |
||
176 | if (plainText == ";") |
||
177 | m_output->insertStyleSheetTableEntry(m_currentStyleHandleNumber, m_textStyle); |
||
178 | else if (plainText.endsWith(";")) |
||
179 | { |
||
180 | // probably a style name with a terminating delimiter |
||
181 | int delimiterPosition = plainText.indexOf(";"); |
||
182 | if (delimiterPosition == (plainText.length() - 1)) |
||
183 | { |
||
184 | // It is at the end, chop it off |
||
185 | QString styleName = plainText.left(delimiterPosition); |
||
186 | m_textStyle.setName(styleName); |
||
187 | m_output->insertStyleSheetTableEntry(m_currentStyleHandleNumber, m_textStyle); |
||
188 | } |
||
189 | else |
||
190 | { |
||
191 | // we were not expecting a name with a delimiter other than at the end |
||
192 | //qDebug() << "Style name with embedded delimiter: " << plainText; |
||
193 | } |
||
194 | } |
||
195 | else |
||
196 | m_textStyle.setName(plainText); |
||
20448 | fschmid | 197 | } |
198 | |||
20461 | fschmid | 199 | void StyleSheetDestination::aboutToEndDestination() |
200 | { |
||
20448 | fschmid | 201 | // TODO |
20461 | fschmid | 202 | } |
203 | |||
204 | double StyleSheetDestination::pixelsFromTwips(const int twips) |
||
205 | { |
||
206 | return twips / 1440.0 * 72.0; |
||
207 | } |
||
20448 | fschmid | 208 | } |