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