HaikuDepot: CharacterStyle: SetBold() and SetItalic()

* Added convenience methods to derive the bold and italic
   font face from the currently set font. May not yield
   results depending on wether a specific face is available
   for the font.
 Ü * Changed test accordinly.
This commit is contained in:
Stephan Aßmus 2013-09-06 10:49:37 +02:00
parent e75fda0202
commit d94326b1c6
4 changed files with 99 additions and 3 deletions

View File

@ -82,6 +82,58 @@ CharacterStyle::SetFontSize(float size)
}
bool
CharacterStyle::SetBold(bool bold)
{
uint16 face = Font().Face();
if ((bold && (face & B_BOLD_FACE) != 0)
|| (!bold && (face & B_BOLD_FACE) == 0)) {
return true;
}
uint16 neededFace = face;
if (bold) {
if ((face & B_ITALIC_FACE) != 0)
neededFace = B_BOLD_FACE | B_ITALIC_FACE;
else
neededFace = B_BOLD_FACE;
} else {
if ((face & B_ITALIC_FACE) != 0)
neededFace = B_ITALIC_FACE;
else
neededFace = B_REGULAR_FACE;
}
return SetFont(_FindFontForFace(neededFace));
}
bool
CharacterStyle::SetItalic(bool italic)
{
uint16 face = Font().Face();
if ((italic && (face & B_ITALIC_FACE) != 0)
|| (!italic && (face & B_ITALIC_FACE) == 0)) {
return true;
}
uint16 neededFace = face;
if (italic) {
if ((face & B_BOLD_FACE) != 0)
neededFace = B_BOLD_FACE | B_ITALIC_FACE;
else
neededFace = B_ITALIC_FACE;
} else {
if ((face & B_BOLD_FACE) != 0)
neededFace = B_BOLD_FACE;
else
neededFace = B_REGULAR_FACE;
}
return SetFont(_FindFontForFace(neededFace));
}
bool
CharacterStyle::SetAscent(float ascent)
{
@ -286,3 +338,29 @@ CharacterStyle::Underline() const
}
// #pragma mark - private
BFont
CharacterStyle::_FindFontForFace(uint16 face) const
{
BFont font(Font());
font_family family;
font_style style;
font.GetFamilyAndStyle(&family, &style);
int32 styleCount = count_font_styles(family);
for (int32 i = 0; i < styleCount; i++) {
uint16 styleFace;
if (get_font_style(family, i, &style, &styleFace) == B_OK) {
if (styleFace == face) {
font.SetFamilyAndStyle(family, style);
return font;
}
}
}
return font;
}

View File

@ -21,6 +21,8 @@ public:
const BFont& Font() const;
bool SetFontSize(float size);
bool SetBold(bool bold);
bool SetItalic(bool italic);
bool SetAscent(float ascent);
float Ascent() const;
@ -58,6 +60,10 @@ public:
bool SetUnderline(uint8 underline);
uint8 Underline() const;
private:
BFont _FindFontForFace(uint16 face) const;
private:
CharacterStyleDataRef fStyleData;
};

View File

@ -277,3 +277,4 @@ CharacterStyleData::operator=(const CharacterStyleData& other)
{
return *this;
}

View File

@ -44,7 +44,13 @@ TextDocumentTest::ReadyToRun()
CharacterStyle regularStyle;
CharacterStyle boldStyle(regularStyle);
boldStyle.SetFont(BFont(be_bold_font));
boldStyle.SetBold(true);
CharacterStyle italicStyle(regularStyle);
italicStyle.SetItalic(true);
CharacterStyle italicAndBoldStyle(boldStyle);
italicAndBoldStyle.SetItalic(true);
CharacterStyle bigStyle(regularStyle);
bigStyle.SetFontSize(24);
@ -82,8 +88,13 @@ TextDocumentTest::ReadyToRun()
document->Append(paragraph);
paragraph = Paragraph(paragraphStyle);
paragraph.Append(TextSpan("The wrapping in this bullet item should "
"look visually pleasing. And why should it not?", regularStyle));
paragraph.Append(TextSpan("The wrapping in ", regularStyle));
paragraph.Append(TextSpan("this", italicStyle));
paragraph.Append(TextSpan(" bullet item should look visually "
"pleasing. And ", regularStyle));
paragraph.Append(TextSpan("why", italicAndBoldStyle));
paragraph.Append(TextSpan(" should it not?", regularStyle));
document->Append(paragraph);
documentView->SetTextDocument(document);