1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _ACCPORTIONS_HXX
28 #define _ACCPORTIONS_HXX
29 #include <SwPortionHandler.hxx>
30 #include <sal/types.h>
31 #include <rtl/ustrbuf.hxx>
32 #include <vector>
33 
34 class String;
35 class SwTxtNode;
36 struct SwSpecialPos;
37 class SwViewOption;
38 namespace com { namespace sun { namespace star {
39     namespace i18n { struct Boundary; }
40 } } }
41 
42 /**
43  * collect text portion data from the layout through SwPortionHandler interface
44  */
45 class SwAccessiblePortionData : public SwPortionHandler
46 {
47     // the node this portion is referring to
48     const SwTxtNode* pTxtNode;
49 
50     // variables used while collecting the data
51     rtl::OUStringBuffer aBuffer;
52     sal_Int32 nModelPosition;
53     sal_Bool bFinished;
54     const SwViewOption* pViewOptions;
55 
56     // the accessible string
57     rtl::OUString sAccessibleString;
58 
59     // positions array
60     // instances of Position_t must always include the minimum and
61     // maximum positions as first/last elements (to simplify the
62     // algorithms)
63     typedef std::vector<sal_Int32> Positions_t;
64 
65     Positions_t aLineBreaks;        /// position of line breaks
66     Positions_t aModelPositions;    /// position of portion breaks in the model
67     Positions_t aAccessiblePositions;   /// portion breaks in sAccessibleString
68 
69     typedef std::vector<sal_uInt8> PortionAttrs_t;
70     PortionAttrs_t aPortionAttrs;   /// additional portion attributes
71 
72     Positions_t* pSentences;    /// positions of sentence breaks
73 
74     size_t nBeforePortions;     /// # of portions before first model character
75     sal_Bool bLastIsSpecial;    /// set if last portion was 'Special()'
76 
77     /// returns the index of the first position whose value is smaller
78     /// or equal, and whose following value is equal or larger
79     size_t FindBreak( const Positions_t& rPositions, sal_Int32 nValue ) const;
80 
81     /// like FindBreak, but finds the last equal or larger position
82     size_t FindLastBreak( const Positions_t& rPositions, sal_Int32 nValue ) const;
83 
84     /// fill the boundary with the values from rPositions[nPos]
85     void FillBoundary(com::sun::star::i18n::Boundary& rBound,
86                       const Positions_t& rPositions,
87                       size_t nPos ) const;
88 
89     /// Access to portion attributes
90     sal_Bool IsPortionAttrSet( size_t nPortionNo, sal_uInt8 nAttr ) const;
91     sal_Bool IsSpecialPortion( size_t nPortionNo ) const;
92     sal_Bool IsReadOnlyPortion( size_t nPortionNo ) const;
93     sal_Bool IsGrayPortionType( sal_uInt16 nType ) const;
94 
95     // helper method for GetEditableRange(...):
96     void AdjustAndCheck( sal_Int32 nPos, size_t& nPortionNo,
97                          sal_uInt16& nCorePos, sal_Bool& bEdit ) const;
98 
99 public:
100     SwAccessiblePortionData( const SwTxtNode* pTxtNd,
101                              const SwViewOption* pViewOpt = NULL );
102     virtual ~SwAccessiblePortionData();
103 
104     // SwPortionHandler methods
105     virtual void Text(sal_uInt16 nLength, sal_uInt16 nType);
106     virtual void Special(sal_uInt16 nLength, const String& rText, sal_uInt16 nType);
107     virtual void LineBreak();
108     virtual void Skip(sal_uInt16 nLength);
109     virtual void Finish();
110 
111 
112     // access to the portion data
113 
114     /// get the text string, as presented by the layout
115     const rtl::OUString& GetAccessibleString() const;
116 
117     /// get the start & end positions of the sentence
118     void GetLineBoundary( com::sun::star::i18n::Boundary& rBound,
119                           sal_Int32 nPos ) const;
120 
121 	// get start and end position of the last line
122     void GetLastLineBoundary( com::sun::star::i18n::Boundary& rBound ) const;
123 
124     // --> OD 2008-05-30 #i89175#
125     sal_Int32 GetLineCount() const;
126     sal_Int32 GetLineNo( const sal_Int32 nPos ) const;
127     void GetBoundaryOfLine( const sal_Int32 nLineNo,
128                             com::sun::star::i18n::Boundary& rLineBound );
129     // <--
130 
131     /// get the position in the model string for a given
132     /// (accessibility) position
133     sal_uInt16 GetModelPosition( sal_Int32 nPos ) const;
134 
135     /// get the position in the accessibility string for a given model position
136     sal_Int32 GetAccessiblePosition( sal_uInt16 nPos ) const;
137 
138     /// fill a SwSpecialPos structure, suitable for calling
139     /// SwTxtFrm->GetCharRect
140     /// Returns the core position, and fills thr rpPos either with NULL or
141     /// with the &rPos, after putting the appropriate data into it.
142     sal_uInt16 FillSpecialPos( sal_Int32 nPos,
143                            SwSpecialPos& rPos,
144                            SwSpecialPos*& rpPos ) const;
145 
146 
147     // get boundaries of words/sentences. The data structures are
148     // created on-demand.
149     void GetSentenceBoundary( com::sun::star::i18n::Boundary& rBound,
150                               sal_Int32 nPos );
151 
152     // get (a) boundary for attribut change
153     void GetAttributeBoundary( com::sun::star::i18n::Boundary& rBound,
154                                sal_Int32 nPos ) const;
155 
156     /// Convert start and end positions into core positions.
157     /// @returns true if 'special' portions are included either completely
158     ///          or not at all. This can be used to test whether editing
159     ///          that range would be legal
160     sal_Bool GetEditableRange( sal_Int32 nStart, sal_Int32 nEnd,
161                                sal_uInt16& nCoreStart, sal_uInt16& nCoreEnd ) const;
162 
163     /// Determine whether this core position is valid for these portions.
164     /// (A paragraph may be split into several frames, e.g. at page
165     ///  boundaries. In this case, only part of a paragraph is represented
166     ///  through this object. This method determines whether one particular
167     ///  position is valid for this object or not.)
168     sal_Bool IsValidCorePosition( sal_uInt16 nPos ) const;
169     sal_uInt16 GetFirstValidCorePosition() const;
170     sal_uInt16 GetLastValidCorePosition() const;
171 };
172 
173 
174 #endif
175 
176