2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
6 #include "CharacterStyle.h"
9 CharacterStyle::CharacterStyle()
11 fStyleData(new CharacterStyleData(), true)
16 CharacterStyle::CharacterStyle(const CharacterStyle
& other
)
18 fStyleData(other
.fStyleData
)
24 CharacterStyle::operator=(const CharacterStyle
& other
)
29 fStyleData
= other
.fStyleData
;
35 CharacterStyle::operator==(const CharacterStyle
& other
) const
40 if (fStyleData
== other
.fStyleData
)
43 if (fStyleData
.Get() != NULL
&& other
.fStyleData
.Get() != NULL
)
44 return *fStyleData
.Get() == *other
.fStyleData
.Get();
51 CharacterStyle::operator!=(const CharacterStyle
& other
) const
53 return !(*this == other
);
58 CharacterStyle::SetFont(const BFont
& font
)
60 CharacterStyleDataRef data
= fStyleData
->SetFont(font
);
61 if (data
== fStyleData
)
62 return data
->Font() == font
;
70 CharacterStyle::Font() const
72 return fStyleData
->Font();
77 CharacterStyle::SetFontSize(float size
)
86 CharacterStyle::FontSize() const
93 CharacterStyle::SetBold(bool bold
)
95 uint16 face
= Font().Face();
96 if ((bold
&& (face
& B_BOLD_FACE
) != 0)
97 || (!bold
&& (face
& B_BOLD_FACE
) == 0)) {
101 uint16 neededFace
= face
;
103 if ((face
& B_ITALIC_FACE
) != 0)
104 neededFace
= B_BOLD_FACE
| B_ITALIC_FACE
;
106 neededFace
= B_BOLD_FACE
;
108 if ((face
& B_ITALIC_FACE
) != 0)
109 neededFace
= B_ITALIC_FACE
;
111 neededFace
= B_REGULAR_FACE
;
114 return SetFont(_FindFontForFace(neededFace
));
119 CharacterStyle::IsBold() const
121 return (Font().Face() & B_BOLD_FACE
) != 0;
126 CharacterStyle::SetItalic(bool italic
)
128 uint16 face
= Font().Face();
129 if ((italic
&& (face
& B_ITALIC_FACE
) != 0)
130 || (!italic
&& (face
& B_ITALIC_FACE
) == 0)) {
134 uint16 neededFace
= face
;
136 if ((face
& B_BOLD_FACE
) != 0)
137 neededFace
= B_BOLD_FACE
| B_ITALIC_FACE
;
139 neededFace
= B_ITALIC_FACE
;
141 if ((face
& B_BOLD_FACE
) != 0)
142 neededFace
= B_BOLD_FACE
;
144 neededFace
= B_REGULAR_FACE
;
147 return SetFont(_FindFontForFace(neededFace
));
152 CharacterStyle::IsItalic() const
154 return (Font().Face() & B_ITALIC_FACE
) != 0;
159 CharacterStyle::SetAscent(float ascent
)
161 CharacterStyleDataRef data
= fStyleData
->SetAscent(ascent
);
162 if (data
== fStyleData
)
163 return data
->Ascent() == ascent
;
171 CharacterStyle::Ascent() const
173 return fStyleData
->Ascent();
178 CharacterStyle::SetDescent(float descent
)
180 CharacterStyleDataRef data
= fStyleData
->SetDescent(descent
);
181 if (data
== fStyleData
)
182 return data
->Descent() == descent
;
190 CharacterStyle::Descent() const
192 return fStyleData
->Descent();
197 CharacterStyle::SetWidth(float width
)
199 CharacterStyleDataRef data
= fStyleData
->SetWidth(width
);
200 if (data
== fStyleData
)
201 return data
->Width() == width
;
209 CharacterStyle::Width() const
211 return fStyleData
->Width();
216 CharacterStyle::SetGlyphSpacing(float spacing
)
218 CharacterStyleDataRef data
= fStyleData
->SetGlyphSpacing(spacing
);
219 if (data
== fStyleData
)
220 return data
->GlyphSpacing() == spacing
;
228 CharacterStyle::GlyphSpacing() const
230 return fStyleData
->GlyphSpacing();
235 CharacterStyle::SetForegroundColor(uint8 r
, uint8 g
, uint8 b
, uint8 a
)
237 return SetForegroundColor((rgb_color
){ r
, g
, b
, a
});
242 CharacterStyle::SetForegroundColor(color_which which
)
244 CharacterStyleDataRef data
= fStyleData
->SetForegroundColor(which
);
245 if (data
== fStyleData
)
246 return data
->WhichForegroundColor() == which
;
254 CharacterStyle::SetForegroundColor(rgb_color color
)
256 CharacterStyleDataRef data
= fStyleData
->SetForegroundColor(color
);
257 if (data
== fStyleData
)
258 return data
->ForegroundColor() == color
;
266 CharacterStyle::ForegroundColor() const
268 return fStyleData
->ForegroundColor();
273 CharacterStyle::WhichForegroundColor() const
275 return fStyleData
->WhichForegroundColor();
280 CharacterStyle::SetBackgroundColor(uint8 r
, uint8 g
, uint8 b
, uint8 a
)
282 return SetBackgroundColor((rgb_color
){ r
, g
, b
, a
});
287 CharacterStyle::SetBackgroundColor(color_which which
)
289 CharacterStyleDataRef data
= fStyleData
->SetBackgroundColor(which
);
290 if (data
== fStyleData
)
291 return data
->WhichBackgroundColor() == which
;
299 CharacterStyle::SetBackgroundColor(rgb_color color
)
301 CharacterStyleDataRef data
= fStyleData
->SetBackgroundColor(color
);
302 if (data
== fStyleData
)
303 return data
->BackgroundColor() == color
;
311 CharacterStyle::BackgroundColor() const
313 return fStyleData
->BackgroundColor();
318 CharacterStyle::WhichBackgroundColor() const
320 return fStyleData
->WhichBackgroundColor();
325 CharacterStyle::SetStrikeOutColor(color_which which
)
327 CharacterStyleDataRef data
= fStyleData
->SetStrikeOutColor(which
);
328 if (data
== fStyleData
)
329 return data
->WhichStrikeOutColor() == which
;
337 CharacterStyle::SetStrikeOutColor(rgb_color color
)
339 CharacterStyleDataRef data
= fStyleData
->SetStrikeOutColor(color
);
340 if (data
== fStyleData
)
341 return data
->StrikeOutColor() == color
;
349 CharacterStyle::StrikeOutColor() const
351 return fStyleData
->StrikeOutColor();
356 CharacterStyle::WhichStrikeOutColor() const
358 return fStyleData
->WhichStrikeOutColor();
363 CharacterStyle::SetUnderlineColor(color_which which
)
365 CharacterStyleDataRef data
= fStyleData
->SetUnderlineColor(which
);
366 if (data
== fStyleData
)
367 return data
->WhichUnderlineColor() == which
;
375 CharacterStyle::SetUnderlineColor(rgb_color color
)
377 CharacterStyleDataRef data
= fStyleData
->SetUnderlineColor(color
);
378 if (data
== fStyleData
)
379 return data
->UnderlineColor() == color
;
387 CharacterStyle::UnderlineColor() const
389 return fStyleData
->UnderlineColor();
394 CharacterStyle::WhichUnderlineColor() const
396 return fStyleData
->WhichUnderlineColor();
401 CharacterStyle::SetStrikeOut(uint8 strikeOut
)
403 CharacterStyleDataRef data
= fStyleData
->SetStrikeOut(strikeOut
);
404 if (data
== fStyleData
)
405 return data
->StrikeOut() == strikeOut
;
413 CharacterStyle::StrikeOut() const
415 return fStyleData
->StrikeOut();
420 CharacterStyle::SetUnderline(uint8 underline
)
422 CharacterStyleDataRef data
= fStyleData
->SetUnderline(underline
);
423 if (data
== fStyleData
)
424 return data
->Underline() == underline
;
432 CharacterStyle::Underline() const
434 return fStyleData
->Underline();
438 // #pragma mark - private
442 CharacterStyle::_FindFontForFace(uint16 face
) const
448 font
.GetFamilyAndStyle(&family
, &style
);
450 int32 styleCount
= count_font_styles(family
);
451 for (int32 i
= 0; i
< styleCount
; i
++) {
453 if (get_font_style(family
, i
, &style
, &styleFace
) == B_OK
) {
454 if (styleFace
== face
) {
455 font
.SetFamilyAndStyle(family
, style
);