1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #ifndef INCLUDED_VCL_TXTATTR_HXX
21 #define INCLUDED_VCL_TXTATTR_HXX
22
23 #include <tools/color.hxx>
24 #include <tools/debug.hxx>
25 #include <tools/fontenum.hxx>
26 #include <vcl/dllapi.h>
27 #include <memory>
28
29 namespace vcl { class Font; }
30
31 #define TEXTATTR_FONTCOLOR 1
32 #define TEXTATTR_FONTWEIGHT 3
33
34 #define TEXTATTR_USER_START 1000 //start id for user defined text attributes
35 #define TEXTATTR_PROTECTED 4
36
37
38 class VCL_DLLPUBLIC TextAttrib
39 {
40 private:
41 sal_uInt16 const mnWhich;
42
43 protected:
TextAttrib(sal_uInt16 nWhich)44 TextAttrib( sal_uInt16 nWhich ) : mnWhich(nWhich) {}
45 TextAttrib( const TextAttrib& ) = default;
46
47 public:
48
49 virtual ~TextAttrib();
50
Which() const51 sal_uInt16 Which() const { return mnWhich; }
52 virtual void SetFont( vcl::Font& rFont ) const = 0;
53 virtual std::unique_ptr<TextAttrib> Clone() const = 0;
54
55 virtual bool operator==( const TextAttrib& rAttr ) const = 0;
operator !=(const TextAttrib & rAttr) const56 bool operator!=( const TextAttrib& rAttr ) const
57 { return !(*this == rAttr ); }
58 };
59
60
61 class VCL_DLLPUBLIC TextAttribFontColor final : public TextAttrib
62 {
63 private:
64 Color const maColor;
65
66 public:
67 TextAttribFontColor( const Color& rColor );
68
GetColor() const69 const Color& GetColor() const { return maColor; }
70
71 virtual void SetFont( vcl::Font& rFont ) const override;
72 virtual std::unique_ptr<TextAttrib> Clone() const override;
73 virtual bool operator==( const TextAttrib& rAttr ) const override;
74
75 };
76
77 class VCL_DLLPUBLIC TextAttribFontWeight final : public TextAttrib
78 {
79 private:
80 FontWeight const meWeight;
81
82 public:
83 TextAttribFontWeight( FontWeight eWeight );
84
85 virtual void SetFont( vcl::Font& rFont ) const override;
86 virtual std::unique_ptr<TextAttrib> Clone() const override;
87 virtual bool operator==( const TextAttrib& rAttr ) const override;
88
getFontWeight() const89 FontWeight getFontWeight() const { return meWeight; }
90 };
91
92 class TextAttribProtect final : public TextAttrib
93 {
94 public:
95 TextAttribProtect();
96
97 virtual void SetFont( vcl::Font& rFont ) const override;
98 virtual std::unique_ptr<TextAttrib> Clone() const override;
99 virtual bool operator==( const TextAttrib& rAttr ) const override;
100
101 };
102
103
104 class TextCharAttrib
105 {
106 private:
107 std::unique_ptr<TextAttrib>
108 mpAttr;
109 sal_Int32 mnStart;
110 sal_Int32 mnEnd;
111
112 public:
113 TextCharAttrib( const TextAttrib& rAttr, sal_Int32 nStart, sal_Int32 nEnd );
114 TextCharAttrib( const TextCharAttrib& rTextCharAttrib );
115
GetAttr() const116 const TextAttrib& GetAttr() const { return *mpAttr; }
117
Which() const118 sal_uInt16 Which() const { return mpAttr->Which(); }
119
GetStart() const120 sal_Int32 GetStart() const { return mnStart; }
SetStart(sal_Int32 n)121 void SetStart(sal_Int32 n) { mnStart = n; }
122
GetEnd() const123 sal_Int32 GetEnd() const { return mnEnd; }
SetEnd(sal_Int32 n)124 void SetEnd(sal_Int32 n) { mnEnd = n; }
125
126 inline sal_Int32 GetLen() const;
127
128 inline void MoveForward( sal_Int32 nDiff );
129 inline void MoveBackward( sal_Int32 nDiff );
130
131 inline void Expand( sal_Int32 nDiff );
132 inline void Collaps( sal_Int32 nDiff );
133
134 inline bool IsIn( sal_Int32 nIndex );
135 inline bool IsInside( sal_Int32 nIndex );
136 inline bool IsEmpty() const;
137
138 };
139
GetLen() const140 inline sal_Int32 TextCharAttrib::GetLen() const
141 {
142 DBG_ASSERT( mnEnd >= mnStart, "TextCharAttrib: nEnd < nStart!" );
143 return mnEnd-mnStart;
144 }
145
MoveForward(sal_Int32 nDiff)146 inline void TextCharAttrib::MoveForward( sal_Int32 nDiff )
147 {
148 DBG_ASSERT( nDiff <= SAL_MAX_INT32-mnEnd, "TextCharAttrib: MoveForward?!" );
149 mnStart = mnStart + nDiff;
150 mnEnd = mnEnd + nDiff;
151 }
152
MoveBackward(sal_Int32 nDiff)153 inline void TextCharAttrib::MoveBackward( sal_Int32 nDiff )
154 {
155 DBG_ASSERT( mnStart >= nDiff, "TextCharAttrib: MoveBackward?!" );
156 mnStart = mnStart - nDiff;
157 mnEnd = mnEnd - nDiff;
158 }
159
Expand(sal_Int32 nDiff)160 inline void TextCharAttrib::Expand( sal_Int32 nDiff )
161 {
162 DBG_ASSERT( nDiff <= SAL_MAX_INT32-mnEnd, "TextCharAttrib: Expand?!" );
163 mnEnd = mnEnd + nDiff;
164 }
165
Collaps(sal_Int32 nDiff)166 inline void TextCharAttrib::Collaps( sal_Int32 nDiff )
167 {
168 DBG_ASSERT( mnEnd-mnStart >= nDiff, "TextCharAttrib: Collaps?!" );
169 mnEnd = mnEnd - nDiff;
170 }
171
IsIn(sal_Int32 nIndex)172 inline bool TextCharAttrib::IsIn( sal_Int32 nIndex )
173 {
174 return ( ( mnStart <= nIndex ) && ( mnEnd >= nIndex ) );
175 }
176
IsInside(sal_Int32 nIndex)177 inline bool TextCharAttrib::IsInside( sal_Int32 nIndex )
178 {
179 return ( ( mnStart < nIndex ) && ( mnEnd > nIndex ) );
180 }
181
IsEmpty() const182 inline bool TextCharAttrib::IsEmpty() const
183 {
184 return mnStart == mnEnd;
185 }
186
187 #endif // INCLUDED_VCL_TXTATTR_HXX
188
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
190