vfs: check userland buffers before reading them.
[haiku.git] / src / apps / haikudepot / textview / CharacterStyle.cpp
blob85261bba6454542199dc8abb7ee41ed36d5b0544
1 /*
2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include "CharacterStyle.h"
9 CharacterStyle::CharacterStyle()
11 fStyleData(new CharacterStyleData(), true)
16 CharacterStyle::CharacterStyle(const CharacterStyle& other)
18 fStyleData(other.fStyleData)
23 CharacterStyle&
24 CharacterStyle::operator=(const CharacterStyle& other)
26 if (this == &other)
27 return *this;
29 fStyleData = other.fStyleData;
30 return *this;
34 bool
35 CharacterStyle::operator==(const CharacterStyle& other) const
37 if (this == &other)
38 return true;
40 if (fStyleData == other.fStyleData)
41 return true;
43 if (fStyleData.Get() != NULL && other.fStyleData.Get() != NULL)
44 return *fStyleData.Get() == *other.fStyleData.Get();
46 return false;
50 bool
51 CharacterStyle::operator!=(const CharacterStyle& other) const
53 return !(*this == other);
57 bool
58 CharacterStyle::SetFont(const BFont& font)
60 CharacterStyleDataRef data = fStyleData->SetFont(font);
61 if (data == fStyleData)
62 return data->Font() == font;
64 fStyleData = data;
65 return true;
69 const BFont&
70 CharacterStyle::Font() const
72 return fStyleData->Font();
76 bool
77 CharacterStyle::SetFontSize(float size)
79 BFont font(Font());
80 font.SetSize(size);
81 return SetFont(font);
85 float
86 CharacterStyle::FontSize() const
88 return Font().Size();
92 bool
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)) {
98 return true;
101 uint16 neededFace = face;
102 if (bold) {
103 if ((face & B_ITALIC_FACE) != 0)
104 neededFace = B_BOLD_FACE | B_ITALIC_FACE;
105 else
106 neededFace = B_BOLD_FACE;
107 } else {
108 if ((face & B_ITALIC_FACE) != 0)
109 neededFace = B_ITALIC_FACE;
110 else
111 neededFace = B_REGULAR_FACE;
114 return SetFont(_FindFontForFace(neededFace));
118 bool
119 CharacterStyle::IsBold() const
121 return (Font().Face() & B_BOLD_FACE) != 0;
125 bool
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)) {
131 return true;
134 uint16 neededFace = face;
135 if (italic) {
136 if ((face & B_BOLD_FACE) != 0)
137 neededFace = B_BOLD_FACE | B_ITALIC_FACE;
138 else
139 neededFace = B_ITALIC_FACE;
140 } else {
141 if ((face & B_BOLD_FACE) != 0)
142 neededFace = B_BOLD_FACE;
143 else
144 neededFace = B_REGULAR_FACE;
147 return SetFont(_FindFontForFace(neededFace));
151 bool
152 CharacterStyle::IsItalic() const
154 return (Font().Face() & B_ITALIC_FACE) != 0;
158 bool
159 CharacterStyle::SetAscent(float ascent)
161 CharacterStyleDataRef data = fStyleData->SetAscent(ascent);
162 if (data == fStyleData)
163 return data->Ascent() == ascent;
165 fStyleData = data;
166 return true;
170 float
171 CharacterStyle::Ascent() const
173 return fStyleData->Ascent();
177 bool
178 CharacterStyle::SetDescent(float descent)
180 CharacterStyleDataRef data = fStyleData->SetDescent(descent);
181 if (data == fStyleData)
182 return data->Descent() == descent;
184 fStyleData = data;
185 return true;
189 float
190 CharacterStyle::Descent() const
192 return fStyleData->Descent();
196 bool
197 CharacterStyle::SetWidth(float width)
199 CharacterStyleDataRef data = fStyleData->SetWidth(width);
200 if (data == fStyleData)
201 return data->Width() == width;
203 fStyleData = data;
204 return true;
208 float
209 CharacterStyle::Width() const
211 return fStyleData->Width();
215 bool
216 CharacterStyle::SetGlyphSpacing(float spacing)
218 CharacterStyleDataRef data = fStyleData->SetGlyphSpacing(spacing);
219 if (data == fStyleData)
220 return data->GlyphSpacing() == spacing;
222 fStyleData = data;
223 return true;
227 float
228 CharacterStyle::GlyphSpacing() const
230 return fStyleData->GlyphSpacing();
234 bool
235 CharacterStyle::SetForegroundColor(uint8 r, uint8 g, uint8 b, uint8 a)
237 return SetForegroundColor((rgb_color){ r, g, b, a });
241 bool
242 CharacterStyle::SetForegroundColor(color_which which)
244 CharacterStyleDataRef data = fStyleData->SetForegroundColor(which);
245 if (data == fStyleData)
246 return data->WhichForegroundColor() == which;
248 fStyleData = data;
249 return true;
253 bool
254 CharacterStyle::SetForegroundColor(rgb_color color)
256 CharacterStyleDataRef data = fStyleData->SetForegroundColor(color);
257 if (data == fStyleData)
258 return data->ForegroundColor() == color;
260 fStyleData = data;
261 return true;
265 rgb_color
266 CharacterStyle::ForegroundColor() const
268 return fStyleData->ForegroundColor();
272 color_which
273 CharacterStyle::WhichForegroundColor() const
275 return fStyleData->WhichForegroundColor();
279 bool
280 CharacterStyle::SetBackgroundColor(uint8 r, uint8 g, uint8 b, uint8 a)
282 return SetBackgroundColor((rgb_color){ r, g, b, a });
286 bool
287 CharacterStyle::SetBackgroundColor(color_which which)
289 CharacterStyleDataRef data = fStyleData->SetBackgroundColor(which);
290 if (data == fStyleData)
291 return data->WhichBackgroundColor() == which;
293 fStyleData = data;
294 return true;
298 bool
299 CharacterStyle::SetBackgroundColor(rgb_color color)
301 CharacterStyleDataRef data = fStyleData->SetBackgroundColor(color);
302 if (data == fStyleData)
303 return data->BackgroundColor() == color;
305 fStyleData = data;
306 return true;
310 rgb_color
311 CharacterStyle::BackgroundColor() const
313 return fStyleData->BackgroundColor();
317 color_which
318 CharacterStyle::WhichBackgroundColor() const
320 return fStyleData->WhichBackgroundColor();
324 bool
325 CharacterStyle::SetStrikeOutColor(color_which which)
327 CharacterStyleDataRef data = fStyleData->SetStrikeOutColor(which);
328 if (data == fStyleData)
329 return data->WhichStrikeOutColor() == which;
331 fStyleData = data;
332 return true;
336 bool
337 CharacterStyle::SetStrikeOutColor(rgb_color color)
339 CharacterStyleDataRef data = fStyleData->SetStrikeOutColor(color);
340 if (data == fStyleData)
341 return data->StrikeOutColor() == color;
343 fStyleData = data;
344 return true;
348 rgb_color
349 CharacterStyle::StrikeOutColor() const
351 return fStyleData->StrikeOutColor();
355 color_which
356 CharacterStyle::WhichStrikeOutColor() const
358 return fStyleData->WhichStrikeOutColor();
362 bool
363 CharacterStyle::SetUnderlineColor(color_which which)
365 CharacterStyleDataRef data = fStyleData->SetUnderlineColor(which);
366 if (data == fStyleData)
367 return data->WhichUnderlineColor() == which;
369 fStyleData = data;
370 return true;
374 bool
375 CharacterStyle::SetUnderlineColor(rgb_color color)
377 CharacterStyleDataRef data = fStyleData->SetUnderlineColor(color);
378 if (data == fStyleData)
379 return data->UnderlineColor() == color;
381 fStyleData = data;
382 return true;
386 rgb_color
387 CharacterStyle::UnderlineColor() const
389 return fStyleData->UnderlineColor();
393 color_which
394 CharacterStyle::WhichUnderlineColor() const
396 return fStyleData->WhichUnderlineColor();
400 bool
401 CharacterStyle::SetStrikeOut(uint8 strikeOut)
403 CharacterStyleDataRef data = fStyleData->SetStrikeOut(strikeOut);
404 if (data == fStyleData)
405 return data->StrikeOut() == strikeOut;
407 fStyleData = data;
408 return true;
412 uint8
413 CharacterStyle::StrikeOut() const
415 return fStyleData->StrikeOut();
419 bool
420 CharacterStyle::SetUnderline(uint8 underline)
422 CharacterStyleDataRef data = fStyleData->SetUnderline(underline);
423 if (data == fStyleData)
424 return data->Underline() == underline;
426 fStyleData = data;
427 return true;
431 uint8
432 CharacterStyle::Underline() const
434 return fStyleData->Underline();
438 // #pragma mark - private
441 BFont
442 CharacterStyle::_FindFontForFace(uint16 face) const
444 BFont font(Font());
446 font_family family;
447 font_style style;
448 font.GetFamilyAndStyle(&family, &style);
450 int32 styleCount = count_font_styles(family);
451 for (int32 i = 0; i < styleCount; i++) {
452 uint16 styleFace;
453 if (get_font_style(family, i, &style, &styleFace) == B_OK) {
454 if (styleFace == face) {
455 font.SetFamilyAndStyle(family, style);
456 return font;
461 return font;