]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxFont::SetSymbolicSize() and SetSymbolicSizeRelativeTo().
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 27 Feb 2011 12:47:05 +0000 (12:47 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 27 Feb 2011 12:47:05 +0000 (12:47 +0000)
These methods allow to set the font size using CSS-like absolute size
specifications.

Notice that the factors used here are incompatible with (but better than) the
ones used in wxBuildFontSizes() in src/html/winpars.cpp. In the future it
would be nice to reuse the new wxFont functions in wxHTML code.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67052 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
include/wx/font.h
interface/wx/font.h
src/common/fontcmn.cpp

index dd27fe19eed1f0bed723601c383e859ef24c2154..7cc55df2478f2061dbc000030bf1cc7f5855f27f 100644 (file)
@@ -468,6 +468,7 @@ All (GUI):
 - Added support for saving as GIF and animated GIF (troelsk).
 - Fix wxWrapSizer minimal size calculation (Catalin Raceanu).
 - Added wxFont::Underlined() and MakeUnderlined() methods.
+- Added wxFont::SetSymbolicSize() and related methods.
 
 GTK:
 
index 3109409104d9ab4c1ceac02adb20eb0fb912355b..43968049b38eeb2779a50fc9138c78745d2f554c 100644 (file)
@@ -66,6 +66,18 @@ enum wxFontWeight
     wxFONTWEIGHT_MAX
 };
 
+// Symbolic font sizes as defined in CSS specification.
+enum wxFontSymbolicSize
+{
+    wxFONTSIZE_XX_SMALL = -3,
+    wxFONTSIZE_X_SMALL,
+    wxFONTSIZE_SMALL,
+    wxFONTSIZE_MEDIUM,
+    wxFONTSIZE_LARGE,
+    wxFONTSIZE_X_LARGE,
+    wxFONTSIZE_XX_LARGE
+};
+
 // the font flag bits for the new font ctor accepting one combined flags word
 enum wxFontFlag
 {
@@ -240,6 +252,19 @@ public:
     bool SetNativeFontInfo(const wxString& info);
     bool SetNativeFontInfoUserDesc(const wxString& info);
 
+    // Symbolic font sizes support: set the font size to "large" or "very
+    // small" either absolutely (i.e. compared to the default font size) or
+    // relatively to the given font size.
+    void SetSymbolicSize(wxFontSymbolicSize size);
+    void SetSymbolicSizeRelativeTo(wxFontSymbolicSize size, int base)
+    {
+        SetPointSize(AdjustToSymbolicSize(size, base));
+    }
+
+    // Adjust the base size in points according to symbolic size.
+    static int AdjustToSymbolicSize(wxFontSymbolicSize size, int base);
+
+
     // translate the fonts into human-readable string (i.e. GetStyleString()
     // will return "wxITALIC" for an italic font, ...)
     wxString GetFamilyString() const;
index 47afe9019a317294b6b3d39618f2dc96c02c09b1..3d4d3b1fdc1bf0b6f6ecb0a5de0d8f095feb0273 100644 (file)
@@ -70,6 +70,27 @@ enum wxFontWeight
     wxFONTWEIGHT_MAX
 };
 
+/**
+    Symbolic font sizes.
+
+    The elements of this enum correspond to CSS absolute size specifications,
+    see http://www.w3.org/TR/CSS21/fonts.html#font-size-props
+
+    @see wxFont::SetSymbolicSize()
+
+    @since 2.9.2
+ */
+enum wxFontSymbolicSize
+{
+    wxFONTSIZE_XX_SMALL = -3,   //!< Extra small.
+    wxFONTSIZE_X_SMALL,         //!< Very small.
+    wxFONTSIZE_SMALL,           //!< Small.
+    wxFONTSIZE_MEDIUM,          //!< Normal.
+    wxFONTSIZE_LARGE,           //!< Large.
+    wxFONTSIZE_X_LARGE,         //!< Very large.
+    wxFONTSIZE_XX_LARGE         //!< Extra large.
+};
+
 /**
     The font flag bits for the new font ctor accepting one combined flags word.
 */
@@ -791,6 +812,28 @@ public:
     */
     virtual void SetStyle(wxFontStyle style);
 
+    /**
+        Sets the font size using a predefined symbolic size name.
+
+        This function allows to change font size to be (very) large or small
+        compared to the standard font size.
+
+        @see SetSymbolicSizeRelativeTo().
+
+        @since 2.9.2
+     */
+    void SetSymbolicSize(wxFontSymbolicSize size);
+
+    /**
+        Sets the font size compared to the base font size.
+
+        This is the same as SetSymbolicSize() except that it uses the given
+        font size as the normal font size instead of the standard font size.
+
+        @since 2.9.2
+     */
+    void SetSymbolicSizeRelativeTo(wxFontSymbolicSize size, int base);
+
     /**
         Sets underlining.
 
index 2125ea97e9a203b0467ddcb2fcd4e0fefeed58f6..036066d29f703ff212bb01a280b3e3953a6ef5fc 100644 (file)
@@ -491,6 +491,29 @@ bool wxFontBase::SetFaceName(const wxString& facename)
     return true;
 }
 
+void wxFontBase::SetSymbolicSize(wxFontSymbolicSize size)
+{
+    SetSymbolicSizeRelativeTo(size, wxNORMAL_FONT->GetPointSize());
+}
+
+/* static */
+int wxFontBase::AdjustToSymbolicSize(wxFontSymbolicSize size, int base)
+{
+    // Using a fixed factor (1.2, from CSS2) is a bad idea as explained at
+    // http://www.w3.org/TR/CSS21/fonts.html#font-size-props so use the values
+    // from http://style.cleverchimp.com/font_size_intervals/altintervals.html
+    // instead.
+    static const float factors[] = { 0.60f, 0.75f, 0.89f, 1.f, 1.2f, 1.5f, 2.f };
+
+    wxCOMPILE_TIME_ASSERT
+    (
+        WXSIZEOF(factors) == wxFONTSIZE_XX_LARGE - wxFONTSIZE_XX_SMALL + 1,
+        WrongFontSizeFactorsSize
+    );
+
+    return factors[size - wxFONTSIZE_XX_SMALL]*base;
+}
+
 wxFont& wxFont::MakeBold()
 {
     SetWeight(wxFONTWEIGHT_BOLD);