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
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/* This is the Scribus Short Words plugin main mechanism.
This code is based on the Scribus-Vlna plug in rewritten for
international use.
2004 Petr Vanek <petr@yarpen.cz>
with contributors.
This program is free software - see LICENSE file in the distribution
or documentation
*/
#include <QRegExp>
#include "shortwords.h"
#include "parse.h"
#include "version.h"
#include "configuration.h"
#include "scribus.h"
#include "page.h"
#include "pageitem.h"
#include "selection.h"
#include "langmgr.h"
SWParse::SWParse()
{
modify = 0;
}
void SWParse::parseItem(PageItem *aFrame)
{
// the content of the frame - text itself
QString content = QString();
int changes = 0;
// list of the short words
QStringList shorts;
// text with special space
QString unbreak;
// the regexp
QRegExp rx(" ");
// cfg
SWConfig *cfg = new SWConfig();
// just textframes processed
if (!aFrame->asTextFrame())
return;
// an ugly hack to get the language code from the item language property
if (lang.isNull() || lang.isEmpty())
{
lang = aFrame->itemText.charStyle(0).language();
if (lang.isNull() || lang.isEmpty())
qDebug("SWParse::parseItem - variable lang is still empty. No changes are made.");
}
QString langCode;
// if (aFrame->doc()->scMW()->Sprachen.contains(lang))
langCode = cfg->getLangCodeFromHyph(LanguageManager::instance()->getHyphFilename(lang,false));
// apply spaces after shorts
shorts = cfg->getShortWords(langCode);
if (shorts.count()==0)
return; // no changes
// get text from frame
int i;
for (i=0; i < aFrame->itemText.length() && ! aFrame->frameDisplays(i); ++i)
;
for (; i < aFrame->itemText.length() && aFrame->frameDisplays(i); ++i)
content += aFrame->itemText.text(i,1);
changes = content.count(SpecialChars::NBSPACE);
// for every config string, replace its spaces by nbsp's.
for (QStringList::Iterator it = shorts.begin(); it != shorts.end(); ++it)
{
unbreak = (*it);
// replace ' ' from cfg with '~' in the replacement string
unbreak = unbreak.replace(SPACE, SpecialChars::NBSPACE);
/*
Regexp used to find the config string (*it) in content.
Cheat sheet:
- \b is a "word boundary"; it matches at a *position*
not a *character*
- \W is a "non-word character"; it matches every character
that is neither a letter, nor a number, nor '_';
for example, it matches all kind of whitespace
(including carriage return) and punctuation
Example occurrences when (*it) == "Mr ":
- "Mr Bla etc." : there's one of the word boundaries
of the word "Mr" before the pattern, and one of the
word boundaries of the word "Bla" after.
Example occurrences when (*it) == " !":
- "ugly hack ! No." : there's a word boundary before,
and a whitespace is matched by \W after.
- "» !" : '«' is matched by \W before, newline is
matched by \W after.
*/
rx.setPattern("(\\b|\\W)" + rx.escape(*it) + "(\\b|\\W)");
/*
QString::replace works on the whole string in one pass.
On every occurrence of our regexp, \1 and \2 are replaced
by what has been matched (captured characters) in,
respectively, the first and second capturing parentheses.
*/
content.replace(rx, "\\1" + unbreak + "\\2");
}
// return text into frame
for (i=0; i < aFrame->itemText.length() && ! aFrame->frameDisplays(i); ++i)
;
for (; i < aFrame->itemText.length() && aFrame->frameDisplays(i); ++i)
aFrame->itemText.replaceChar(i, content.at(i));
if (content.count(SpecialChars::NBSPACE) > changes)
++modify;
delete(cfg);
} // end of method
void SWParse::parseSelection(ScribusDoc* doc)
{
uint docSelectionCount = doc->m_Selection->count();
if (docSelectionCount == 0)
return;
doc->scMW()->mainWindowProgressBar->setMaximum(docSelectionCount);
for (uint i=0; i < docSelectionCount; ++i)
{
doc->scMW()->mainWindowProgressBar->setValue(i);
parseItem(doc->m_Selection->itemAt(i));
} // for items
doc->scMW()->mainWindowProgressBar->setValue(docSelectionCount);
}
void SWParse::parsePage(ScribusDoc* doc)
{
parsePage(doc, doc->currentPageNumber());
}
void SWParse::parsePage(ScribusDoc* doc, int page)
{
uint cnt = 0;
uint docItemsCount=doc->Items->count();
if (docItemsCount == 0)
return;
for (uint a = 0; a < docItemsCount; ++a)
{
PageItem* b = doc->Items->at(a);
if (b->OwnPage == page)
++cnt;
}
doc->scMW()->mainWindowProgressBar->setMaximum(cnt);
doc->view()->GotoPage(page);
uint i = 0;
for (uint a = 0; a < docItemsCount; ++a)
{
PageItem* b = doc->Items->at(a);
if (b->OwnPage == page)
{
doc->scMW()->mainWindowProgressBar->setValue(++i);
parseItem(b);
}
}
doc->scMW()->mainWindowProgressBar->setValue(cnt);
}
void SWParse::parseAll(ScribusDoc* doc)
{
for (int i=0; i < doc->Pages->count(); ++i)
parsePage(doc, i);
}
|