1 /* 2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 #include "CharacterStyleData.h" 7 8 #include <new> 9 10 11 CharacterStyleData::CharacterStyleData() 12 : 13 fFont(), 14 15 fAscent(-1.0f), 16 fDescent(-1.0f), 17 fWidth(-1.0f), 18 19 fGlyphSpacing(0.0f), 20 21 fWhichFgColor(B_PANEL_TEXT_COLOR), 22 fWhichBgColor(B_PANEL_BACKGROUND_COLOR), 23 fWhichStrikeOutColor(fWhichFgColor), 24 fWhichUnderlineColor(fWhichFgColor), 25 26 fFgColor(ui_color(fWhichFgColor)), 27 fBgColor(ui_color(fWhichBgColor)), 28 fStrikeOutColor(fFgColor), 29 fUnderlineColor(fFgColor), 30 31 fStrikeOutStyle(STRIKE_OUT_NONE), 32 fUnderlineStyle(UNDERLINE_NONE) 33 { 34 } 35 36 37 CharacterStyleData::CharacterStyleData(const CharacterStyleData& other) 38 : 39 fFont(other.fFont), 40 41 fAscent(other.fAscent), 42 fDescent(other.fDescent), 43 fWidth(other.fWidth), 44 45 fGlyphSpacing(other.fGlyphSpacing), 46 47 fWhichFgColor(other.fWhichFgColor), 48 fWhichBgColor(other.fWhichBgColor), 49 fWhichStrikeOutColor(other.fWhichStrikeOutColor), 50 fWhichUnderlineColor(other.fWhichUnderlineColor), 51 52 fFgColor(other.fFgColor), 53 fBgColor(other.fBgColor), 54 fStrikeOutColor(other.fStrikeOutColor), 55 fUnderlineColor(other.fUnderlineColor), 56 57 fStrikeOutStyle(other.fStrikeOutStyle), 58 fUnderlineStyle(other.fUnderlineStyle) 59 { 60 } 61 62 63 bool 64 CharacterStyleData::operator==(const CharacterStyleData& other) const 65 { 66 if (this == &other) 67 return true; 68 69 return fFont == other.fFont 70 && fAscent == other.fAscent 71 && fDescent == other.fDescent 72 && fWidth == other.fWidth 73 74 && fGlyphSpacing == other.fGlyphSpacing 75 76 && fWhichFgColor == other.fWhichFgColor 77 && fWhichBgColor == other.fWhichBgColor 78 && fWhichStrikeOutColor == other.fWhichStrikeOutColor 79 && fWhichUnderlineColor == other.fWhichUnderlineColor 80 81 && fFgColor == other.fFgColor 82 && fBgColor == other.fBgColor 83 && fStrikeOutColor == other.fStrikeOutColor 84 && fUnderlineColor == other.fUnderlineColor 85 86 && fStrikeOutStyle == other.fStrikeOutStyle 87 && fUnderlineStyle == other.fUnderlineStyle; 88 } 89 90 91 bool 92 CharacterStyleData::operator!=(const CharacterStyleData& other) const 93 { 94 return !(*this == other); 95 } 96 97 98 CharacterStyleDataRef 99 CharacterStyleData::SetFont(const BFont& font) 100 { 101 if (fFont == font) 102 return CharacterStyleDataRef(this); 103 104 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 105 if (ret == NULL) 106 return CharacterStyleDataRef(this); 107 108 ret->fFont = font; 109 return CharacterStyleDataRef(ret, true); 110 } 111 112 113 CharacterStyleDataRef 114 CharacterStyleData::SetAscent(float ascent) 115 { 116 if (fAscent == ascent) 117 return CharacterStyleDataRef(this); 118 119 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 120 if (ret == NULL) 121 return CharacterStyleDataRef(this); 122 123 ret->fAscent = ascent; 124 return CharacterStyleDataRef(ret, true); 125 } 126 127 128 float 129 CharacterStyleData::Ascent() const 130 { 131 if (fAscent >= 0.0f) 132 return fAscent; 133 134 font_height fontHeight; 135 fFont.GetHeight(&fontHeight); 136 return fontHeight.ascent; 137 } 138 139 140 CharacterStyleDataRef 141 CharacterStyleData::SetDescent(float descent) 142 { 143 if (fDescent == descent) 144 return CharacterStyleDataRef(this); 145 146 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 147 if (ret == NULL) 148 return CharacterStyleDataRef(this); 149 150 ret->fDescent = descent; 151 return CharacterStyleDataRef(ret, true); 152 } 153 154 155 float 156 CharacterStyleData::Descent() const 157 { 158 if (fDescent >= 0.0f) 159 return fDescent; 160 161 font_height fontHeight; 162 fFont.GetHeight(&fontHeight); 163 return fontHeight.descent; 164 } 165 166 167 CharacterStyleDataRef 168 CharacterStyleData::SetWidth(float width) 169 { 170 if (fWidth == width) 171 return CharacterStyleDataRef(this); 172 173 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 174 if (ret == NULL) 175 return CharacterStyleDataRef(this); 176 177 ret->fWidth = width; 178 return CharacterStyleDataRef(ret, true); 179 } 180 181 182 CharacterStyleDataRef 183 CharacterStyleData::SetGlyphSpacing(float glyphSpacing) 184 { 185 if (fGlyphSpacing == glyphSpacing) 186 return CharacterStyleDataRef(this); 187 188 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 189 if (ret == NULL) 190 return CharacterStyleDataRef(this); 191 192 ret->fGlyphSpacing = glyphSpacing; 193 return CharacterStyleDataRef(ret, true); 194 } 195 196 197 CharacterStyleDataRef 198 CharacterStyleData::SetForegroundColor(color_which which) 199 { 200 if (fWhichFgColor == which) 201 return CharacterStyleDataRef(this); 202 203 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 204 if (ret == NULL) 205 return CharacterStyleDataRef(this); 206 207 ret->fWhichFgColor = which; 208 return CharacterStyleDataRef(ret, true); 209 } 210 211 212 CharacterStyleDataRef 213 CharacterStyleData::SetForegroundColor(rgb_color color) 214 { 215 if (fFgColor == color) 216 return CharacterStyleDataRef(this); 217 218 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 219 if (ret == NULL) 220 return CharacterStyleDataRef(this); 221 222 ret->fFgColor = color; 223 ret->fWhichFgColor = B_NO_COLOR; 224 return CharacterStyleDataRef(ret, true); 225 } 226 227 228 CharacterStyleDataRef 229 CharacterStyleData::SetBackgroundColor(color_which which) 230 { 231 if (fWhichBgColor == which) 232 return CharacterStyleDataRef(this); 233 234 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 235 if (ret == NULL) 236 return CharacterStyleDataRef(this); 237 238 ret->fWhichBgColor = which; 239 return CharacterStyleDataRef(ret, true); 240 } 241 242 243 CharacterStyleDataRef 244 CharacterStyleData::SetBackgroundColor(rgb_color color) 245 { 246 if (fBgColor == color) 247 return CharacterStyleDataRef(this); 248 249 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 250 if (ret == NULL) 251 return CharacterStyleDataRef(this); 252 253 ret->fBgColor = color; 254 ret->fWhichBgColor = B_NO_COLOR; 255 return CharacterStyleDataRef(ret, true); 256 } 257 258 259 CharacterStyleDataRef 260 CharacterStyleData::SetStrikeOutColor(color_which which) 261 { 262 if (fWhichStrikeOutColor == which) 263 return CharacterStyleDataRef(this); 264 265 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 266 if (ret == NULL) 267 return CharacterStyleDataRef(this); 268 269 ret->fWhichStrikeOutColor = which; 270 return CharacterStyleDataRef(ret, true); 271 } 272 273 274 CharacterStyleDataRef 275 CharacterStyleData::SetStrikeOutColor(rgb_color color) 276 { 277 if (fStrikeOutColor == color) 278 return CharacterStyleDataRef(this); 279 280 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 281 if (ret == NULL) 282 return CharacterStyleDataRef(this); 283 284 ret->fStrikeOutColor = color; 285 ret->fWhichStrikeOutColor = B_NO_COLOR; 286 return CharacterStyleDataRef(ret, true); 287 } 288 289 290 CharacterStyleDataRef 291 CharacterStyleData::SetUnderlineColor(color_which which) 292 { 293 if (fWhichUnderlineColor == which) 294 return CharacterStyleDataRef(this); 295 296 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 297 if (ret == NULL) 298 return CharacterStyleDataRef(this); 299 300 ret->fWhichUnderlineColor = which; 301 return CharacterStyleDataRef(ret, true); 302 } 303 304 305 CharacterStyleDataRef 306 CharacterStyleData::SetUnderlineColor(rgb_color color) 307 { 308 if (fUnderlineColor == color) 309 return CharacterStyleDataRef(this); 310 311 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 312 if (ret == NULL) 313 return CharacterStyleDataRef(this); 314 315 ret->fUnderlineColor = color; 316 ret->fWhichUnderlineColor = B_NO_COLOR; 317 return CharacterStyleDataRef(ret, true); 318 } 319 320 321 CharacterStyleDataRef 322 CharacterStyleData::SetStrikeOut(uint8 strikeOut) 323 { 324 if (fStrikeOutStyle == strikeOut) 325 return CharacterStyleDataRef(this); 326 327 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 328 if (ret == NULL) 329 return CharacterStyleDataRef(this); 330 331 ret->fStrikeOutStyle = strikeOut; 332 return CharacterStyleDataRef(ret, true); 333 } 334 335 336 CharacterStyleDataRef 337 CharacterStyleData::SetUnderline(uint8 underline) 338 { 339 if (fUnderlineStyle == underline) 340 return CharacterStyleDataRef(this); 341 342 CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this); 343 if (ret == NULL) 344 return CharacterStyleDataRef(this); 345 346 ret->fUnderlineStyle = underline; 347 return CharacterStyleDataRef(ret, true); 348 } 349 350 351 // #pragma mark - private 352 353 354 CharacterStyleData& 355 CharacterStyleData::operator=(const CharacterStyleData& other) 356 { 357 return *this; 358 } 359 360