From d94326b1c63bb13fd1cfdcf4e0fb5f66ed67951e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Fri, 6 Sep 2013 10:49:37 +0200 Subject: [PATCH] HaikuDepot: CharacterStyle: SetBold() and SetItalic() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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. --- .../haiku-depot/textview/CharacterStyle.cpp | 78 +++++++++++++++++++ .../haiku-depot/textview/CharacterStyle.h | 6 ++ .../textview/CharacterStyleData.cpp | 1 + .../haiku-depot/textview/TextDocumentTest.cpp | 17 +++- 4 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/apps/haiku-depot/textview/CharacterStyle.cpp b/src/apps/haiku-depot/textview/CharacterStyle.cpp index 3e1c7ea32c..697186b827 100644 --- a/src/apps/haiku-depot/textview/CharacterStyle.cpp +++ b/src/apps/haiku-depot/textview/CharacterStyle.cpp @@ -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; +} + diff --git a/src/apps/haiku-depot/textview/CharacterStyle.h b/src/apps/haiku-depot/textview/CharacterStyle.h index 7e6615bf02..b3729b0a27 100644 --- a/src/apps/haiku-depot/textview/CharacterStyle.h +++ b/src/apps/haiku-depot/textview/CharacterStyle.h @@ -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; }; diff --git a/src/apps/haiku-depot/textview/CharacterStyleData.cpp b/src/apps/haiku-depot/textview/CharacterStyleData.cpp index 50ed806f04..1103205ec8 100644 --- a/src/apps/haiku-depot/textview/CharacterStyleData.cpp +++ b/src/apps/haiku-depot/textview/CharacterStyleData.cpp @@ -277,3 +277,4 @@ CharacterStyleData::operator=(const CharacterStyleData& other) { return *this; } + diff --git a/src/apps/haiku-depot/textview/TextDocumentTest.cpp b/src/apps/haiku-depot/textview/TextDocumentTest.cpp index 433f0fadfb..dcdd9b0e65 100644 --- a/src/apps/haiku-depot/textview/TextDocumentTest.cpp +++ b/src/apps/haiku-depot/textview/TextDocumentTest.cpp @@ -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);