]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxDC::GetFontMetrics() and implement it for wxMSW.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 27 Feb 2011 12:48:13 +0000 (12:48 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 27 Feb 2011 12:48:13 +0000 (12:48 +0000)
Add a new wxDC method allowing to retrieve the font characteristics not
available from GetTextExtent(), notably the internal leading (and also the
average font width).

Currently this is implemented for wxMSW only, the internal leading is always 0
in the other ports.

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

include/wx/dc.h
include/wx/msw/dc.h
interface/wx/dc.h
src/common/dcbase.cpp
src/msw/dc.cpp

index aa6b39e26065cab026d5a82d1a02f04939b04257..a06217ab45365a51eb772427f9c0bcc31d72a893 100644 (file)
@@ -114,6 +114,27 @@ enum wxMappingMode
     wxMM_POINTS
 };
 
+// Description of text characteristics.
+struct wxFontMetrics
+{
+    wxFontMetrics()
+    {
+        height =
+        ascent =
+        descent =
+        internalLeading =
+        externalLeading =
+        averageWidth = 0;
+    }
+
+    int height,             // Total character height.
+        ascent,             // Part of the height above the baseline.
+        descent,            // Part of the height below the baseline.
+        internalLeading,    // Intra-line spacing.
+        externalLeading,    // Inter-line spacing.
+        averageWidth;       // Average font width, a.k.a. "x-width".
+};
+
 #if WXWIN_COMPATIBILITY_2_8
 
 //-----------------------------------------------------------------------------
@@ -375,6 +396,18 @@ public:
 
     virtual wxCoord GetCharHeight() const = 0;
     virtual wxCoord GetCharWidth() const = 0;
+
+    // The derived classes should really override DoGetFontMetrics() to return
+    // the correct values in the future but for now provide a default
+    // implementation in terms of DoGetTextExtent() to avoid breaking the
+    // compilation of all other ports as wxMSW is the only one to implement it.
+    virtual void DoGetFontMetrics(int *height,
+                                  int *ascent,
+                                  int *descent,
+                                  int *internalLeading,
+                                  int *externalLeading,
+                                  int *averageWidth) const;
+
     virtual void DoGetTextExtent(const wxString& string,
                                  wxCoord *x, wxCoord *y,
                                  wxCoord *descent = NULL,
@@ -840,6 +873,15 @@ public:
     wxCoord GetCharWidth() const
         { return m_pimpl->GetCharWidth(); }
 
+    wxFontMetrics GetFontMetrics() const
+    {
+        wxFontMetrics fm;
+        m_pimpl->DoGetFontMetrics(&fm.height, &fm.ascent, &fm.descent,
+                                  &fm.internalLeading, &fm.externalLeading,
+                                  &fm.averageWidth);
+        return fm;
+    }
+
     void GetTextExtent(const wxString& string,
                        wxCoord *x, wxCoord *y,
                        wxCoord *descent = NULL,
index e8205c4a218e5ca329fd9a0d4231248c55e12862..b99da69a1dc1835b6d7207f5d815ed1b387cc274 100644 (file)
@@ -165,6 +165,12 @@ protected:
     void RealizeScaleAndOrigin();
 
 public:
+    virtual void DoGetFontMetrics(int *height,
+                                  int *ascent,
+                                  int *descent,
+                                  int *internalLeading,
+                                  int *externalLeading,
+                                  int *averageWidth) const;
     virtual void DoGetTextExtent(const wxString& string,
                                  wxCoord *x, wxCoord *y,
                                  wxCoord *descent = NULL,
index 582474020bbbe57a30d8d1337d1c0aea286be86a..3c0b009072a99a422a63760608e1cf13b99ea1ea 100644 (file)
@@ -79,6 +79,28 @@ enum wxMappingMode
     wxMM_POINTS
 };
 
+/**
+    Simple collection of various font metrics.
+
+    This object is returned by wxDC::GetFontMetrics().
+
+    @since 2.9.2
+
+    @library{wxcore}
+    @category{dc,gdi}
+ */
+struct wxFontMetrics
+{
+    /// Constructor initializes all fields to 0.
+    wxFontMetrics();
+
+    int height,             ///< Total character height.
+        ascent,             ///< Part of the height above the baseline.
+        descent,            ///< Part of the height below the baseline.
+        internalLeading,    ///< Intra-line spacing.
+        externalLeading,    ///< Inter-line spacing.
+        averageWidth;       ///< Average font width, a.k.a. "x-width".
+};
 
 
 /**
@@ -773,6 +795,21 @@ public:
     */
     wxCoord GetCharWidth() const;
 
+    /**
+        Returns the various font characteristics.
+
+        This method allows to retrieve some of the font characteristics not
+        returned by GetTextExtent(), notably internal leading and average
+        character width.
+
+        Currently this method returns correct results only under wxMSW, in the
+        other ports the internal leading will always be 0 and the average
+        character width will be computed as the width of the character 'x'.
+
+        @since 2.9.2
+     */
+    wxFontMetrics GetFontMetrics() const;
+
     /**
         Gets the dimensions of the string using the currently selected font.
         @a string is the text string to measure, @e heightLine, if non @NULL,
index dd745d02fb67a6efd13b2842de9ba2e9038683c2..3dea79e650d8a122531f55ac3bbfcf7cc2f4ea5f 100644 (file)
@@ -1134,6 +1134,27 @@ void wxDCImpl::InheritAttributes(wxWindow *win)
     SetLayoutDirection(win->GetLayoutDirection());
 }
 
+void wxDCImpl::DoGetFontMetrics(int *height,
+                                int *ascent,
+                                int *descent,
+                                int *internalLeading,
+                                int *externalLeading,
+                                int *averageWidth) const
+{
+    // Average width is typically the same as width of 'x'.
+    wxCoord h, d;
+    DoGetTextExtent("x", averageWidth, &h, &d, externalLeading);
+
+    if ( height )
+        *height = h;
+    if ( ascent )
+        *ascent = h - d;
+    if ( descent )
+        *descent = d;
+    if ( internalLeading )
+        *internalLeading = 0;
+}
+
 //-----------------------------------------------------------------------------
 // wxDC
 //-----------------------------------------------------------------------------
index b5f8cd1298e7da88509d2b37a0a9cb1185ed3024..81fdb5558717be44d6f1e809ecfdf816f3dae0b9 100644 (file)
@@ -1722,6 +1722,31 @@ wxCoord wxMSWDCImpl::GetCharWidth() const
     return lpTextMetric.tmAveCharWidth;
 }
 
+void wxMSWDCImpl::DoGetFontMetrics(int *height,
+                                   int *ascent,
+                                   int *descent,
+                                   int *internalLeading,
+                                   int *externalLeading,
+                                   int *averageWidth) const
+{
+    TEXTMETRIC tm;
+
+    GetTextMetrics(GetHdc(), &tm);
+
+    if ( height )
+        *height = tm.tmHeight;
+    if ( ascent )
+        *ascent = tm.tmAscent;
+    if ( descent )
+        *descent = tm.tmDescent;
+    if ( internalLeading )
+        *internalLeading = tm.tmInternalLeading;
+    if ( externalLeading )
+        *externalLeading = tm.tmExternalLeading;
+    if ( averageWidth )
+        *averageWidth = tm.tmAveCharWidth;
+}
+
 void wxMSWDCImpl::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
                            wxCoord *descent, wxCoord *externalLeading,
                            const wxFont *font) const
@@ -1791,13 +1816,7 @@ void wxMSWDCImpl::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y
 
     if ( descent || externalLeading )
     {
-        TEXTMETRIC tm;
-        ::GetTextMetrics(GetHdc(), &tm);
-
-        if (descent)
-            *descent = tm.tmDescent;
-        if (externalLeading)
-            *externalLeading = tm.tmExternalLeading;
+        DoGetFontMetrics(NULL, NULL, descent, NULL, externalLeading, NULL);
     }
 
     if ( hfontOld )