forked from lpxxn/docx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.cpp
333 lines (288 loc) · 7.2 KB
/
text.cpp
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
#include "text.h"
#include "./oxml/oxmltext.h"
#include "./oxml/oxmlshape.h"
#include "./parts/documentpart.h"
#include "./shape.h"
#include <QDomDocument>
#include <QDebug>
#include <math.h>
namespace Docx {
Paragraph::Paragraph(DocumentPart *part, const QDomElement &element)
: m_part(part)
{
m_dom = part->m_dom;
m_pEle = new QDomElement(element);
m_style = new CT_PPr(this);
// load exist run
QDomNodeList reles = m_pEle->elementsByTagName(QStringLiteral("w:r"));
if (!reles.isEmpty()) {
for (int i = 0; i < reles.count(); i++) {
QDomElement rele = reles.at(i).toElement();
Run *run = new Run(m_part, m_pEle, rele);
m_runs.append(run);
}
}
}
/*!
* \brief 添加文本块
* \param text
* \param style
* \return
*/
Run *Paragraph::addRun(const QString &text, const QString &style)
{
Run *run = new Run(m_part, m_pEle);
if (!text.isEmpty())
run->addText(text);
if (!style.isEmpty())
setStyle(style);
m_runs.append(run);
return run;
}
void Paragraph::addText(const QString &text)
{
addRun(text);
}
/*!
* \brief 所有字体
* \return
*/
QString Paragraph::text() const
{
QStringList str;
for (const Run *r : m_runs) {
str.append(r->text());
}
return str.join("");
}
/*!
* \brief 设置样式
* \param style
*/
void Paragraph::setStyle(const QString &style)
{
m_style->setStyle(style);
}
/*!
* \brief 对齐方式
* \param align
*/
void Paragraph::setAlignment(WD_PARAGRAPH_ALIGNMENT align)
{
m_style->setAlignment(align);
}
/*!
* \brief 在本段落前添加段落
* \param text
* \param style
* \return
*/
Paragraph *Paragraph::insertParagraphBefore(const QString &text, const QString &style)
{
QDomElement pEle = m_dom->createElement(QStringLiteral("w:p"));
Paragraph *p = new Paragraph(m_part, pEle);
p->addRun(text, style);
QDomNode parent = m_pEle->parentNode();
parent.insertBefore(pEle, *m_pEle);
return p;
}
Paragraph::~Paragraph()
{
qDeleteAll(m_runs);
delete m_pEle;
}
// End Paragraph
Run::Run(DocumentPart *part, QDomElement *parent)
: m_part(part), m_parent(parent)
{
m_dom = part->m_dom;
m_rEle = m_dom->createElement(QStringLiteral("w:r"));
m_parent->appendChild(m_rEle);
m_style = new CT_RPr(this);
}
Run::Run(DocumentPart *part, QDomElement *parent, const QDomElement &ele)
: m_part(part), m_parent(parent)
{
m_dom = part->m_dom;
m_rEle = QDomElement(ele);
m_style = new CT_RPr(this);
}
/*!
* \brief 添加 Tab
*/
void Run::addTab()
{
QDomElement tabEle = m_dom->createElement(QStringLiteral("w:tab"));
m_rEle.appendChild(tabEle);
}
void Run::addBreak(WD_BREAK breakType)
{
QDomElement brele = m_dom->createElement(QStringLiteral("w:br"));
QString eletype, eleclear;
switch (breakType) {
case WD_BREAK::LINE: break;
case WD_BREAK::PAGE: eletype = QStringLiteral("page"); break;
case WD_BREAK::COLUMN: eletype = QStringLiteral("column"); break;
case WD_BREAK::LINE_CLEAR_LEFT: eletype = QStringLiteral("textWrapping"); eleclear = QStringLiteral("left"); break;
case WD_BREAK::LINE_CLEAR_RIGHT: eletype = QStringLiteral("textWrapping"); eleclear = QStringLiteral("right"); break;
case WD_BREAK::LINE_CLEAR_ALL: eletype = QStringLiteral("textWrapping"); eleclear = QStringLiteral("all"); break;
default: break;
}
m_rEle.appendChild(brele);
if (!eletype.isEmpty())
brele.setAttribute(QStringLiteral("w:type"), eletype);
if (!eleclear.isEmpty())
brele.setAttribute(QStringLiteral("w:clear"), eleclear);
}
/*!
* \brief 添加文字
* \param text
*/
void Run::addText(const QString &text)
{
QDomElement tEle = m_dom->createElement(QStringLiteral("w:t"));
tEle.appendChild(m_dom->createTextNode(text));
m_rEle.appendChild(tEle);
//m_rEle
}
QString Run::text() const
{
return m_rEle.text();
}
/*!
* \brief 返回 InlineShape
* \param path
* \param width
* \param height
* \return
*/
InlineShape *Run::addPicture(const QString &path, const Length &width, const Length &height)
{
InlineShapes *ships = m_part->m_inlineshapes;
InlineShape *picture = ships->addPicture(path, this);
if(picture->width().emu() < 5280000)// 经测试A4默认页面内容的宽度为5280000
{
return picture;
}
else
{
//开始缩放
Length width1 = Length(5280000);
double hei = 5280000.0/picture->width().emu()*picture->height().emu();
Length height1 = Length(hei);
return scalePicture(picture, width1, height1);
}
}
InlineShape *Run::scalePicture(InlineShape *picture, const Length &width, const Length &height)
{
if (!width.isEmpty() || !height.isEmpty()) {
int lwidth = width.emu();
int lheight = height.emu();
int native_width = picture->width().emu();
int native_height = picture->height().emu();
qDebug()<<"width:"<<native_width<<"height:"<<native_height;
if (width.isEmpty()) {
float scaling_factor = float(lheight) / float(native_height);
lwidth = int(round(native_width * scaling_factor));
}
if (height.isEmpty()) {
float scaling_factor = float(lwidth) / float(native_width);
lheight = int(round(native_height * scaling_factor));
}
picture->setWidth(lwidth);
picture->setHeight(lheight);
}
return picture;
}
InlineShape *Run::addPicture(const QImage &img, const Length &width, const Length &height)
{
InlineShapes *ships = m_part->m_inlineshapes;
InlineShape *picture = ships->addPicture(img, this);
if(picture->width().emu() < 5280000) // 经测试A4默认页面内容的宽度为5280000
{
return picture;
}
else
{
//开始缩放
Length width1 = Length(5280000);
double hei = 5280000.0/picture->width().emu()*picture->height().emu();
Length height1 = Length(hei);
return scalePicture(picture, width1, height1);
}
}
/*!
* \brief 设置样式
* \param style
*/
void Run::setStyle(const QString &style)
{
m_style->setStyle(style);
}
Run::~Run()
{
delete m_style;
}
/*!
* \brief 全部大写
* \param isallcaps
*/
void Run::setAllcaps(bool isallcaps)
{
m_style->setAllcaps(isallcaps);
}
/*!
* \brief 小一号的大写
* \param smallcpas
*/
void Run::setSmallcaps(bool smallcpas)
{
m_style->setSmallcaps(smallcpas);
}
/*!
* \brief 加粗
* \param isbold
*/
void Run::setBold(bool isbold)
{
m_style->setBold(isbold);
}
/*!
* \brief 倾斜
* \param isItalic
*/
void Run::setItalic(bool isItalic)
{
m_style->setItalic(isItalic);
}
/*!
* \brief 双划线
* \param isDoubleStrike
*/
void Run::setDoubleStrike(bool isDoubleStrike)
{
m_style->setDoubleStrike(isDoubleStrike);
}
/*!
* \brief 阴影
* \param shadow
*/
void Run::setShadow(bool shadow)
{
m_style->setShadow(shadow);
}
/*!
* \brief 下划线
* \param underline
*/
void Run::setUnderLine(WD_UNDERLINE underline)
{
m_style->setUnderLine(underline);
}
void Run::addDrawing(CT_Inline *imline)
{
QDomElement drawing = m_dom->createElement(QStringLiteral("w:drawing"));
m_rEle.appendChild(drawing);
drawing.appendChild(imline->element());
}
}