1efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5efeef26fSAndrew Rist * distributed with this work for additional information
6efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at
10efeef26fSAndrew Rist *
11efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12efeef26fSAndrew Rist *
13efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist * software distributed under the License is distributed on an
15efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist * KIND, either express or implied. See the License for the
17efeef26fSAndrew Rist * specific language governing permissions and limitations
18efeef26fSAndrew Rist * under the License.
19efeef26fSAndrew Rist *
20efeef26fSAndrew Rist *************************************************************/
21efeef26fSAndrew Rist
22efeef26fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <string.h> // fuer strchr()
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <com/sun/star/i18n/UnicodeType.hdl>
30cdf0e10cSrcweir #include <com/sun/star/i18n/WordType.hdl>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include <unotools/charclass.hxx>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #include <hintids.hxx>
35cdf0e10cSrcweir #include <doc.hxx>
36cdf0e10cSrcweir #include <IDocumentUndoRedo.hxx>
37cdf0e10cSrcweir #include <docary.hxx>
38cdf0e10cSrcweir #include <mvsave.hxx> // Strukturen zum Sichern beim Move/Delete
39cdf0e10cSrcweir #include <ndtxt.hxx>
40cdf0e10cSrcweir #include <txatbase.hxx>
41cdf0e10cSrcweir #include <rubylist.hxx>
42cdf0e10cSrcweir #include <pam.hxx>
43cdf0e10cSrcweir #include <swundo.hxx> // fuer die UndoIds
44cdf0e10cSrcweir #include <breakit.hxx>
45cdf0e10cSrcweir #include <crsskip.hxx>
46cdf0e10cSrcweir
47cdf0e10cSrcweir SV_IMPL_PTRARR( SwRubyList, SwRubyListEntryPtr )
48cdf0e10cSrcweir
49cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
50cdf0e10cSrcweir
51cdf0e10cSrcweir
52cdf0e10cSrcweir /*
53cdf0e10cSrcweir * Members in the list:
54cdf0e10cSrcweir * - String - the orig text
55cdf0e10cSrcweir * - SwFmtRuby - the ruby attribut
56cdf0e10cSrcweir *
57cdf0e10cSrcweir *
58cdf0e10cSrcweir */
FillRubyList(const SwPaM & rPam,SwRubyList & rList,sal_uInt16 nMode)59cdf0e10cSrcweir sal_uInt16 SwDoc::FillRubyList( const SwPaM& rPam, SwRubyList& rList,
60cdf0e10cSrcweir sal_uInt16 nMode )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir const SwPaM *_pStartCrsr = (SwPaM*)rPam.GetNext(),
63cdf0e10cSrcweir *__pStartCrsr = _pStartCrsr;
64cdf0e10cSrcweir sal_Bool bCheckEmpty = &rPam != _pStartCrsr;
65cdf0e10cSrcweir do {
66cdf0e10cSrcweir const SwPosition* pStt = _pStartCrsr->Start(),
67cdf0e10cSrcweir * pEnd = pStt == _pStartCrsr->GetPoint()
68cdf0e10cSrcweir ? _pStartCrsr->GetMark()
69cdf0e10cSrcweir : _pStartCrsr->GetPoint();
70cdf0e10cSrcweir if( !bCheckEmpty || ( pStt != pEnd && *pStt != *pEnd ))
71cdf0e10cSrcweir {
72cdf0e10cSrcweir SwPaM aPam( *pStt );
73cdf0e10cSrcweir do {
74cdf0e10cSrcweir SwRubyListEntry* pNew = new SwRubyListEntry;
75cdf0e10cSrcweir if( pEnd != pStt )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir aPam.SetMark();
78cdf0e10cSrcweir *aPam.GetMark() = *pEnd;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir if( _SelectNextRubyChars( aPam, *pNew, nMode ))
81cdf0e10cSrcweir {
82cdf0e10cSrcweir rList.Insert( pNew, rList.Count() );
83cdf0e10cSrcweir aPam.DeleteMark();
84cdf0e10cSrcweir }
85cdf0e10cSrcweir else
86cdf0e10cSrcweir {
87cdf0e10cSrcweir delete pNew;
88cdf0e10cSrcweir if( *aPam.GetPoint() < *pEnd )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir // goto next paragraph
91cdf0e10cSrcweir aPam.DeleteMark();
92cdf0e10cSrcweir aPam.Move( fnMoveForward, fnGoNode );
93cdf0e10cSrcweir }
94cdf0e10cSrcweir else
95cdf0e10cSrcweir break;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir } while( 30 > rList.Count() && *aPam.GetPoint() < *pEnd );
98cdf0e10cSrcweir }
99cdf0e10cSrcweir } while( 30 > rList.Count() &&
100cdf0e10cSrcweir (_pStartCrsr=(SwPaM *)_pStartCrsr->GetNext()) != __pStartCrsr );
101cdf0e10cSrcweir
102cdf0e10cSrcweir return rList.Count();
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
SetRubyList(const SwPaM & rPam,const SwRubyList & rList,sal_uInt16 nMode)105cdf0e10cSrcweir sal_uInt16 SwDoc::SetRubyList( const SwPaM& rPam, const SwRubyList& rList,
106cdf0e10cSrcweir sal_uInt16 nMode )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir GetIDocumentUndoRedo().StartUndo( UNDO_SETRUBYATTR, NULL );
109cdf0e10cSrcweir SvUShortsSort aDelArr;
110cdf0e10cSrcweir aDelArr.Insert( RES_TXTATR_CJK_RUBY );
111cdf0e10cSrcweir
112cdf0e10cSrcweir sal_uInt16 nListEntry = 0;
113cdf0e10cSrcweir
114cdf0e10cSrcweir const SwPaM *_pStartCrsr = (SwPaM*)rPam.GetNext(),
115cdf0e10cSrcweir *__pStartCrsr = _pStartCrsr;
116cdf0e10cSrcweir sal_Bool bCheckEmpty = &rPam != _pStartCrsr;
117cdf0e10cSrcweir do {
118cdf0e10cSrcweir const SwPosition* pStt = _pStartCrsr->Start(),
119cdf0e10cSrcweir * pEnd = pStt == _pStartCrsr->GetPoint()
120cdf0e10cSrcweir ? _pStartCrsr->GetMark()
121cdf0e10cSrcweir : _pStartCrsr->GetPoint();
122cdf0e10cSrcweir if( !bCheckEmpty || ( pStt != pEnd && *pStt != *pEnd ))
123cdf0e10cSrcweir {
124cdf0e10cSrcweir
125cdf0e10cSrcweir SwPaM aPam( *pStt );
126cdf0e10cSrcweir do {
127cdf0e10cSrcweir SwRubyListEntry aCheckEntry;
128cdf0e10cSrcweir if( pEnd != pStt )
129cdf0e10cSrcweir {
130cdf0e10cSrcweir aPam.SetMark();
131cdf0e10cSrcweir *aPam.GetMark() = *pEnd;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir if( _SelectNextRubyChars( aPam, aCheckEntry, nMode ))
134cdf0e10cSrcweir {
135cdf0e10cSrcweir const SwRubyListEntry* pEntry = rList[ nListEntry++ ];
136cdf0e10cSrcweir if( aCheckEntry.GetRubyAttr() != pEntry->GetRubyAttr() )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir // set/reset the attribut
139cdf0e10cSrcweir if( pEntry->GetRubyAttr().GetText().Len() )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir InsertPoolItem( aPam, pEntry->GetRubyAttr(), 0 );
142cdf0e10cSrcweir }
143cdf0e10cSrcweir else
144cdf0e10cSrcweir {
145cdf0e10cSrcweir ResetAttrs( aPam, sal_True, &aDelArr );
146cdf0e10cSrcweir }
147cdf0e10cSrcweir }
148cdf0e10cSrcweir
149cdf0e10cSrcweir if( aCheckEntry.GetText() != pEntry->GetText() &&
150cdf0e10cSrcweir pEntry->GetText().Len() )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir // text is changed, so replace the original
153cdf0e10cSrcweir ReplaceRange( aPam, pEntry->GetText(), false );
154cdf0e10cSrcweir }
155cdf0e10cSrcweir aPam.DeleteMark();
156cdf0e10cSrcweir }
157cdf0e10cSrcweir else
158cdf0e10cSrcweir {
159cdf0e10cSrcweir if( *aPam.GetPoint() < *pEnd )
160cdf0e10cSrcweir {
161cdf0e10cSrcweir // goto next paragraph
162cdf0e10cSrcweir aPam.DeleteMark();
163cdf0e10cSrcweir aPam.Move( fnMoveForward, fnGoNode );
164cdf0e10cSrcweir }
165cdf0e10cSrcweir else
166cdf0e10cSrcweir {
167cdf0e10cSrcweir const SwRubyListEntry* pEntry = rList[ nListEntry++ ];
168cdf0e10cSrcweir
169cdf0e10cSrcweir // set/reset the attribut
170cdf0e10cSrcweir if( pEntry->GetRubyAttr().GetText().Len() &&
171cdf0e10cSrcweir pEntry->GetText().Len() )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir InsertString( aPam, pEntry->GetText() );
174cdf0e10cSrcweir aPam.SetMark();
175cdf0e10cSrcweir aPam.GetMark()->nContent -= pEntry->GetText().Len();
176*69a74367SOliver-Rainer Wittmann InsertPoolItem(
177*69a74367SOliver-Rainer Wittmann aPam, pEntry->GetRubyAttr(), nsSetAttrMode::SETATTR_DONTEXPAND );
178cdf0e10cSrcweir }
179cdf0e10cSrcweir else
180cdf0e10cSrcweir break;
181cdf0e10cSrcweir aPam.DeleteMark();
182cdf0e10cSrcweir }
183cdf0e10cSrcweir }
184cdf0e10cSrcweir } while( nListEntry < rList.Count() && *aPam.GetPoint() < *pEnd );
185cdf0e10cSrcweir }
186cdf0e10cSrcweir } while( 30 > rList.Count() &&
187cdf0e10cSrcweir (_pStartCrsr=(SwPaM *)_pStartCrsr->GetNext()) != __pStartCrsr );
188cdf0e10cSrcweir
189cdf0e10cSrcweir GetIDocumentUndoRedo().EndUndo( UNDO_SETRUBYATTR, NULL );
190cdf0e10cSrcweir
191cdf0e10cSrcweir return nListEntry;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
_SelectNextRubyChars(SwPaM & rPam,SwRubyListEntry & rEntry,sal_uInt16)194cdf0e10cSrcweir sal_Bool SwDoc::_SelectNextRubyChars( SwPaM& rPam, SwRubyListEntry& rEntry, sal_uInt16 )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir // Point must be the startposition, Mark is optional the end position
197cdf0e10cSrcweir SwPosition* pPos = rPam.GetPoint();
198cdf0e10cSrcweir const SwTxtNode* pTNd = pPos->nNode.GetNode().GetTxtNode();
199cdf0e10cSrcweir const String* pTxt = &pTNd->GetTxt();
200cdf0e10cSrcweir xub_StrLen nStart = pPos->nContent.GetIndex(), nEnd = pTxt->Len();
201cdf0e10cSrcweir
202cdf0e10cSrcweir sal_Bool bHasMark = rPam.HasMark();
203cdf0e10cSrcweir if( bHasMark )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir // in the same node?
206cdf0e10cSrcweir if( rPam.GetMark()->nNode == pPos->nNode )
207cdf0e10cSrcweir {
208cdf0e10cSrcweir // then use that end
209cdf0e10cSrcweir xub_StrLen nTEnd = rPam.GetMark()->nContent.GetIndex();
210cdf0e10cSrcweir if( nTEnd < nEnd )
211cdf0e10cSrcweir nEnd = nTEnd;
212cdf0e10cSrcweir }
213cdf0e10cSrcweir rPam.DeleteMark();
214cdf0e10cSrcweir }
215cdf0e10cSrcweir
216cdf0e10cSrcweir // ----- search the start
217cdf0e10cSrcweir // --- look where a ruby attribut starts
218cdf0e10cSrcweir sal_uInt16 nHtIdx = USHRT_MAX;
219cdf0e10cSrcweir const SwpHints* pHts = pTNd->GetpSwpHints();
220cdf0e10cSrcweir const SwTxtAttr* pAttr = 0;
221cdf0e10cSrcweir if( pHts )
222cdf0e10cSrcweir {
223cdf0e10cSrcweir const SwTxtAttr* pHt;
224cdf0e10cSrcweir for( nHtIdx = 0; nHtIdx < pHts->Count(); ++nHtIdx )
225cdf0e10cSrcweir if( RES_TXTATR_CJK_RUBY == ( pHt = (*pHts)[ nHtIdx ])->Which() &&
226cdf0e10cSrcweir *pHt->GetAnyEnd() > nStart )
227cdf0e10cSrcweir {
228cdf0e10cSrcweir if( *pHt->GetStart() < nEnd )
229cdf0e10cSrcweir {
230cdf0e10cSrcweir pAttr = pHt;
231cdf0e10cSrcweir if( !bHasMark && nStart > *pAttr->GetStart() )
232cdf0e10cSrcweir {
233cdf0e10cSrcweir nStart = *pAttr->GetStart();
234cdf0e10cSrcweir pPos->nContent = nStart;
235cdf0e10cSrcweir }
236cdf0e10cSrcweir }
237cdf0e10cSrcweir break;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir }
240cdf0e10cSrcweir
241cdf0e10cSrcweir if( !bHasMark && nStart && ( !pAttr || nStart != *pAttr->GetStart()) )
242cdf0e10cSrcweir {
243cdf0e10cSrcweir // skip to the word begin!
244cdf0e10cSrcweir long nWordStt = pBreakIt->GetBreakIter()->getWordBoundary(
245cdf0e10cSrcweir *pTxt, nStart,
246cdf0e10cSrcweir pBreakIt->GetLocale( pTNd->GetLang( nStart )),
247cdf0e10cSrcweir WordType::ANYWORD_IGNOREWHITESPACES,
248cdf0e10cSrcweir sal_True ).startPos;
249cdf0e10cSrcweir if( nWordStt < nStart && -1 != nWordStt )
250cdf0e10cSrcweir {
251cdf0e10cSrcweir nStart = (xub_StrLen)nWordStt;
252cdf0e10cSrcweir pPos->nContent = nStart;
253cdf0e10cSrcweir }
254cdf0e10cSrcweir }
255cdf0e10cSrcweir
256cdf0e10cSrcweir sal_Bool bAlphaNum = sal_False;
257cdf0e10cSrcweir long nWordEnd = nEnd;
258cdf0e10cSrcweir CharClass& rCC = GetAppCharClass();
259cdf0e10cSrcweir while( nStart < nEnd )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir if( pAttr && nStart == *pAttr->GetStart() )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir pPos->nContent = nStart;
264cdf0e10cSrcweir if( !rPam.HasMark() )
265cdf0e10cSrcweir {
266cdf0e10cSrcweir rPam.SetMark();
267cdf0e10cSrcweir pPos->nContent = *pAttr->GetAnyEnd();
268cdf0e10cSrcweir if( pPos->nContent.GetIndex() > nEnd )
269cdf0e10cSrcweir pPos->nContent = nEnd;
270cdf0e10cSrcweir rEntry.SetRubyAttr( pAttr->GetRuby() );
271cdf0e10cSrcweir }
272cdf0e10cSrcweir break;
273cdf0e10cSrcweir }
274cdf0e10cSrcweir
275cdf0e10cSrcweir sal_Int32 nChType = rCC.getType( *pTxt, nStart );
276cdf0e10cSrcweir sal_Bool bIgnoreChar = sal_False, bIsAlphaNum = sal_False, bChkNxtWrd = sal_False;
277cdf0e10cSrcweir switch( nChType )
278cdf0e10cSrcweir {
279cdf0e10cSrcweir case UnicodeType::UPPERCASE_LETTER:
280cdf0e10cSrcweir case UnicodeType::LOWERCASE_LETTER:
281cdf0e10cSrcweir case UnicodeType::TITLECASE_LETTER:
282cdf0e10cSrcweir case UnicodeType::DECIMAL_DIGIT_NUMBER:
283cdf0e10cSrcweir bChkNxtWrd = bIsAlphaNum = sal_True;
284cdf0e10cSrcweir break;
285cdf0e10cSrcweir
286cdf0e10cSrcweir case UnicodeType::SPACE_SEPARATOR:
287cdf0e10cSrcweir case UnicodeType::CONTROL:
288cdf0e10cSrcweir /*??*/ case UnicodeType::PRIVATE_USE:
289cdf0e10cSrcweir case UnicodeType::START_PUNCTUATION:
290cdf0e10cSrcweir case UnicodeType::END_PUNCTUATION:
291cdf0e10cSrcweir bIgnoreChar = sal_True;
292cdf0e10cSrcweir break;
293cdf0e10cSrcweir
294cdf0e10cSrcweir
295cdf0e10cSrcweir case UnicodeType::OTHER_LETTER:
296cdf0e10cSrcweir bChkNxtWrd = sal_True;
297cdf0e10cSrcweir // no break!
298cdf0e10cSrcweir // case UnicodeType::UNASSIGNED:
299cdf0e10cSrcweir // case UnicodeType::MODIFIER_LETTER:
300cdf0e10cSrcweir // case UnicodeType::NON_SPACING_MARK:
301cdf0e10cSrcweir // case UnicodeType::ENCLOSING_MARK:
302cdf0e10cSrcweir // case UnicodeType::COMBINING_SPACING_MARK:
303cdf0e10cSrcweir // case UnicodeType::LETTER_NUMBER:
304cdf0e10cSrcweir // case UnicodeType::OTHER_NUMBER:
305cdf0e10cSrcweir // case UnicodeType::LINE_SEPARATOR:
306cdf0e10cSrcweir // case UnicodeType::PARAGRAPH_SEPARATOR:
307cdf0e10cSrcweir // case UnicodeType::FORMAT:
308cdf0e10cSrcweir // case UnicodeType::SURROGATE:
309cdf0e10cSrcweir // case UnicodeType::DASH_PUNCTUATION:
310cdf0e10cSrcweir // case UnicodeType::CONNECTOR_PUNCTUATION:
311cdf0e10cSrcweir ///*?? */case UnicodeType::OTHER_PUNCTUATION:
312cdf0e10cSrcweir //--> char '!' is to ignore!
313cdf0e10cSrcweir // case UnicodeType::MATH_SYMBOL:
314cdf0e10cSrcweir // case UnicodeType::CURRENCY_SYMBOL:
315cdf0e10cSrcweir // case UnicodeType::MODIFIER_SYMBOL:
316cdf0e10cSrcweir // case UnicodeType::OTHER_SYMBOL:
317cdf0e10cSrcweir // case UnicodeType::INITIAL_PUNCTUATION:
318cdf0e10cSrcweir // case UnicodeType::FINAL_PUNCTUATION:
319cdf0e10cSrcweir default:
320cdf0e10cSrcweir bIsAlphaNum = sal_False;
321cdf0e10cSrcweir break;
322cdf0e10cSrcweir }
323cdf0e10cSrcweir
324cdf0e10cSrcweir if( rPam.HasMark() )
325cdf0e10cSrcweir {
326cdf0e10cSrcweir if( bIgnoreChar || bIsAlphaNum != bAlphaNum || nStart >= nWordEnd )
327cdf0e10cSrcweir break;
328cdf0e10cSrcweir }
329cdf0e10cSrcweir else if( !bIgnoreChar )
330cdf0e10cSrcweir {
331cdf0e10cSrcweir rPam.SetMark();
332cdf0e10cSrcweir bAlphaNum = bIsAlphaNum;
333cdf0e10cSrcweir if( bChkNxtWrd && pBreakIt->GetBreakIter().is() )
334cdf0e10cSrcweir {
335cdf0e10cSrcweir // search the end of this word
336cdf0e10cSrcweir nWordEnd = pBreakIt->GetBreakIter()->getWordBoundary(
337cdf0e10cSrcweir *pTxt, nStart,
338cdf0e10cSrcweir pBreakIt->GetLocale( pTNd->GetLang( nStart )),
339cdf0e10cSrcweir WordType::ANYWORD_IGNOREWHITESPACES,
340cdf0e10cSrcweir sal_True ).endPos;
341cdf0e10cSrcweir if( 0 > nWordEnd || nWordEnd > nEnd || nWordEnd == nStart )
342cdf0e10cSrcweir nWordEnd = nEnd;
343cdf0e10cSrcweir }
344cdf0e10cSrcweir }
345cdf0e10cSrcweir pTNd->GoNext( &pPos->nContent, CRSR_SKIP_CHARS );
346cdf0e10cSrcweir nStart = pPos->nContent.GetIndex();
347cdf0e10cSrcweir }
348cdf0e10cSrcweir
349cdf0e10cSrcweir nStart = rPam.GetMark()->nContent.GetIndex();
350cdf0e10cSrcweir rEntry.SetText( pTxt->Copy( nStart,
351cdf0e10cSrcweir rPam.GetPoint()->nContent.GetIndex() - nStart ));
352cdf0e10cSrcweir return rPam.HasMark();
353cdf0e10cSrcweir }
354cdf0e10cSrcweir
~SwRubyListEntry()355cdf0e10cSrcweir SwRubyListEntry::~SwRubyListEntry()
356cdf0e10cSrcweir {
357cdf0e10cSrcweir }
358