[haiku-commits] haiku: hrev46026 - src/apps/haiku-depot/textview

  • From: superstippi@xxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Fri, 6 Sep 2013 10:52:28 +0200 (CEST)

hrev46026 adds 1 changeset to branch 'master'
old head: e75fda02029e7283338487d7c7b1575b6447a270
new head: d94326b1c63bb13fd1cfdcf4e0fb5f66ed67951e
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=d94326b+%5Ee75fda0

----------------------------------------------------------------------------

d94326b: 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.

                                      [ Stephan Aßmus <superstippi@xxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev46026
Commit:      d94326b1c63bb13fd1cfdcf4e0fb5f66ed67951e
URL:         http://cgit.haiku-os.org/haiku/commit/?id=d94326b
Author:      Stephan Aßmus <superstippi@xxxxxx>
Date:        Fri Sep  6 08:49:37 2013 UTC

----------------------------------------------------------------------------

4 files changed, 99 insertions(+), 3 deletions(-)
src/apps/haiku-depot/textview/CharacterStyle.cpp | 78 ++++++++++++++++++++
src/apps/haiku-depot/textview/CharacterStyle.h   |  6 ++
.../haiku-depot/textview/CharacterStyleData.cpp  |  1 +
.../haiku-depot/textview/TextDocumentTest.cpp    | 17 ++++-

----------------------------------------------------------------------------

diff --git a/src/apps/haiku-depot/textview/CharacterStyle.cpp 
b/src/apps/haiku-depot/textview/CharacterStyle.cpp
index 3e1c7ea..697186b 100644
--- a/src/apps/haiku-depot/textview/CharacterStyle.cpp
+++ b/src/apps/haiku-depot/textview/CharacterStyle.cpp
@@ -83,6 +83,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)
 {
        CharacterStyleDataRef data = fStyleData->SetAscent(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 7e6615b..b3729b0 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 50ed806..1103205 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 433f0fa..dcdd9b0 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);


Other related posts:

  • » [haiku-commits] haiku: hrev46026 - src/apps/haiku-depot/textview - superstippi