From 0f6858b69e4699b196e98937aa5509e37a504e30 Mon Sep 17 00:00:00 2001
From: Robert Roebling <robert@roebling.de>
Date: Wed, 13 Apr 2005 13:53:19 +0000
Subject: [PATCH]   Apply Mart R.'s patch for improved font width     handling
 and minor doc corrections.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@33572 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/font.tex | 93 +++++++++++++++++++++++++++---------------
 src/gtk/font.cpp       | 35 +++++++---------
 src/gtk1/font.cpp      | 35 +++++++---------
 src/unix/fontutil.cpp  | 43 +++++++++----------
 4 files changed, 109 insertions(+), 97 deletions(-)

diff --git a/docs/latex/wx/font.tex b/docs/latex/wx/font.tex
index d8475127d7..0767d3c045 100644
--- a/docs/latex/wx/font.tex
+++ b/docs/latex/wx/font.tex
@@ -36,6 +36,19 @@ enum wxFontFamily
 };
 \end{verbatim}
 
+The possible values for the \arg{weight} parameter are (the old names
+are for compatibility only):
+
+\begin{verbatim}
+enum wxFontWeight
+{
+    wxFONTWEIGHT_NORMAL = wxNORMAL,
+    wxFONTWEIGHT_LIGHT = wxLIGHT,
+    wxFONTWEIGHT_BOLD = wxBOLD,
+    wxFONTWEIGHT_MAX
+};
+\end{verbatim}
+
 The font flags which can be used during the font creation are:
 
 \begin{verbatim}
@@ -152,11 +165,11 @@ wxSWISS\_FONT}
 
 Default constructor.
 
-\func{}{wxFont}{\param{int}{ pointSize}, \param{wxFontFamily}{ family}, \param{int}{ style}, \param{int}{ weight},
+\func{}{wxFont}{\param{int}{ pointSize}, \param{wxFontFamily}{ family}, \param{int}{ style}, \param{wxFontWeight}{ weight},
  \param{const bool}{ underline = false}, \param{const wxString\& }{faceName = ""},
  \param{wxFontEncoding }{encoding = wxFONTENCODING\_DEFAULT}}
 
-\func{}{wxFont}{\param{int}{ pixelSize}, \param{int}{ family}, \param{int}{ style}, \param{int}{ weight},
+\func{}{wxFont}{\param{int}{ pixelSize}, \param{wxFontFamily}{ family}, \param{int}{ style}, \param{wxFontWeight}{ weight},
  \param{const bool}{ underline = false}, \param{const wxString\& }{faceName = ""},
  \param{wxFontEncoding }{encoding = wxFONTENCODING\_DEFAULT}}
 
@@ -175,17 +188,25 @@ the static \helpref{New}{wxfontnew} method must be used.}
 
 \twocolwidtha{5cm}
 \begin{twocollist}\itemsep=0pt
-\twocolitem{{\bf wxDEFAULT}}{Chooses a default font.}
-\twocolitem{{\bf wxDECORATIVE}}{A decorative font.}
-\twocolitem{{\bf wxROMAN}}{A formal, serif font.}
-\twocolitem{{\bf wxSCRIPT}}{A handwriting font.}
-\twocolitem{{\bf wxSWISS}}{A sans-serif font.}
-\twocolitem{{\bf wxMODERN}}{A fixed pitch font.}
+\twocolitem{{\bf wxFONTFAMILY\_DEFAULT}}{Chooses a default font.}
+\twocolitem{{\bf wxFONTFAMILY\_DECORATIVE}}{A decorative font.}
+\twocolitem{{\bf wxFONTFAMILY\_ROMAN}}{A formal, serif font.}
+\twocolitem{{\bf wxFONTFAMILY\_SCRIPT}}{A handwriting font.}
+\twocolitem{{\bf wxFONTFAMILY\_SWISS}}{A sans-serif font.}
+\twocolitem{{\bf wxFONTFAMILY\_MODERN}}{A fixed pitch font.}
+\twocolitem{{\bf wxFONTFAMILY\_TELETYPE}}{A teletype font.}
 \end{twocollist}}
 
-\docparam{style}{One of {\bf wxNORMAL}, {\bf wxSLANT} and {\bf wxITALIC}.}
+\docparam{style}{One of {\bf wxFONTSTYLE\_NORMAL}, {\bf wxFONTSTYLE\_SLANT} and {\bf wxFONTSTYLE\_ITALIC}.}
+
+\docparam{weight}{Font weight, sometimes also referred to as font boldness. One of:
 
-\docparam{weight}{One of {\bf wxNORMAL}, {\bf wxLIGHT} and {\bf wxBOLD}.}
+\twocolwidtha{5cm}
+\begin{twocollist}\itemsep=0pt
+\twocolitem{{\bf wxFONTWEIGHT\_NORMAL}}{Normal font.}
+\twocolitem{{\bf wxFONTWEIGHT\_LIGHT}}{Light font.}
+\twocolitem{{\bf wxFONTWEIGHT\_BOLD}}{Bold font.}
+\end{twocollist}}
 
 \docparam{underline}{The value can be true or false. At present this has an effect on Windows and Motif 2.x only.}
 
@@ -197,7 +218,7 @@ a default typeface will be chosen based on the family.}
 \begin{twocollist}\itemsep=0pt
 \twocolitem{{\bf wxFONTENCODING\_SYSTEM}}{Default system encoding.}
 \twocolitem{{\bf wxFONTENCODING\_DEFAULT}}{Default application encoding: this
-is the encoding set by calls to 
+is the encoding set by calls to
 \helpref{SetDefaultEncoding}{wxfontsetdefaultencoding} and which may be set to,
 say, KOI8 to create all fonts by default with KOI8 encoding. Initially, the
 default application encoding is the same as default system encoding.}
@@ -239,7 +260,7 @@ application data structure, and there is a risk of double deletion.
 
 \constfunc{bool}{IsFixedWidth}{\void}
 
-Returns {\tt true} if the font is a fixed width (or monospaced) font, 
+Returns {\tt true} if the font is a fixed width (or monospaced) font,
 {\tt false} if it is a proportional one or font is invalid.
 
 
@@ -251,7 +272,7 @@ Returns the current application's default encoding.
 
 \wxheading{See also}
 
-\helpref{Font encoding overview}{wxfontencodingoverview}, 
+\helpref{Font encoding overview}{wxfontencodingoverview},
 \helpref{SetDefaultEncoding}{wxfontsetdefaultencoding}
 
 
@@ -269,9 +290,9 @@ typeface information.
 
 \membersection{wxFont::GetFamily}\label{wxfontgetfamily}
 
-\constfunc{int}{GetFamily}{\void}
+\constfunc{wxFontFamily}{GetFamily}{\void}
 
-Gets the font family. See \helpref{wxFont::wxFont}{wxfontctor} for a list of valid
+Gets the font family. See \helpref{wxFont::SetFamily}{wxfontsetfamily} for a list of valid
 family identifiers.
 
 \wxheading{See also}
@@ -327,7 +348,7 @@ Returns true if the font is underlined, false otherwise.
 
 \membersection{wxFont::GetWeight}\label{wxfontgetweight}
 
-\constfunc{int}{GetWeight}{\void}
+\constfunc{wxFontWeight}{GetWeight}{\void}
 
 Gets the font weight. See \helpref{wxFont::wxFont}{wxfontctor} for a list of valid
 weight identifiers.
@@ -339,19 +360,19 @@ weight identifiers.
 
 \membersection{wxFont::New}\label{wxfontnew}
 
-\func{static wxFont *}{New}{\param{int}{ pointSize}, \param{wxFontFamily}{ family}, \param{int}{ style}, \param{int}{ weight},
+\func{static wxFont *}{New}{\param{int}{ pointSize}, \param{wxFontFamily}{ family}, \param{int}{ style}, \param{wxFontWeight}{ weight},
  \param{const bool}{ underline = false}, \param{const wxString\& }{faceName = ""},
  \param{wxFontEncoding }{encoding = wxFONTENCODING\_DEFAULT}}
 
-\func{static wxFont *}{New}{\param{int}{ pointSize}, \param{wxFontFamily}{ family}, 
+\func{static wxFont *}{New}{\param{int}{ pointSize}, \param{wxFontFamily}{ family},
  \param{int}{ flags = \texttt{wxFONTFLAG\_DEFAULT}}, \param{const wxString\& }{faceName = ""},
  \param{wxFontEncoding }{encoding = wxFONTENCODING\_DEFAULT}}
 
-\func{static wxFont *}{New}{\param{const wxSize\&}{ pixelSize}, \param{wxFontFamily}{ family}, \param{int}{ style}, \param{int}{ weight},
+\func{static wxFont *}{New}{\param{const wxSize\&}{ pixelSize}, \param{wxFontFamily}{ family}, \param{int}{ style}, \param{wxFontWeight}{ weight},
  \param{const bool}{ underline = false}, \param{const wxString\& }{faceName = ""},
  \param{wxFontEncoding }{encoding = wxFONTENCODING\_DEFAULT}}
 
-\func{static wxFont *}{New}{\param{const wxSize\&}{ pixelSize}, \param{wxFontFamily}{ family}, 
+\func{static wxFont *}{New}{\param{const wxSize\&}{ pixelSize}, \param{wxFontFamily}{ family},
  \param{int}{ flags = \texttt{wxFONTFLAG\_DEFAULT}}, \param{const wxString\& }{faceName = ""},
  \param{wxFontEncoding }{encoding = wxFONTENCODING\_DEFAULT}}
 
@@ -377,7 +398,7 @@ Sets the default font encoding.
 
 \wxheading{See also}
 
-\helpref{Font encoding overview}{wxfontencodingoverview}, 
+\helpref{Font encoding overview}{wxfontencodingoverview},
 \helpref{GetDefaultEncoding}{wxfontgetdefaultencoding}
 
 
@@ -405,7 +426,7 @@ and then for a font belonging to the same family.
 
 \membersection{wxFont::SetFamily}\label{wxfontsetfamily}
 
-\func{void}{SetFamily}{\param{int}{ family}}
+\func{void}{SetFamily}{\param{wxFontFamily}{ family}}
 
 Sets the font family.
 
@@ -415,12 +436,13 @@ Sets the font family.
 
 \twocolwidtha{5cm}
 \begin{twocollist}\itemsep=0pt
-\twocolitem{{\bf wxDEFAULT}}{Chooses a default font.}
-\twocolitem{{\bf wxDECORATIVE}}{A decorative font.}
-\twocolitem{{\bf wxROMAN}}{A formal, serif font.}
-\twocolitem{{\bf wxSCRIPT}}{A handwriting font.}
-\twocolitem{{\bf wxSWISS}}{A sans-serif font.}
-\twocolitem{{\bf wxMODERN}}{A fixed pitch font.}
+\twocolitem{{\bf wxFONTFAMILY\_DEFAULT}}{Chooses a default font.}
+\twocolitem{{\bf wxFONTFAMILY\_DECORATIVE}}{A decorative font.}
+\twocolitem{{\bf wxFONTFAMILY\_ROMAN}}{A formal, serif font.}
+\twocolitem{{\bf wxFONTFAMILY\_SCRIPT}}{A handwriting font.}
+\twocolitem{{\bf wxFONTFAMILY\_SWISS}}{A sans-serif font.}
+\twocolitem{{\bf wxFONTFAMILY\_MODERN}}{A fixed pitch font.}
+\twocolitem{{\bf wxFONTFAMILY\_TELETYPE}}{A teletype font.}
 \end{twocollist}}
 
 \wxheading{See also}
@@ -433,7 +455,7 @@ Sets the font family.
 \func{void}{SetNativeFontInfo}{\param{const wxString\& }{info}}
 
 Creates the font corresponding to the given native font description string
-which must have been previously returned by 
+which must have been previously returned by
 \helpref{GetNativeFontInfoDesc}{wxfontgetnativefontinfodesc}. If the string is
 invalid, font is unchanged.
 
@@ -461,7 +483,7 @@ Sets the font style.
 
 \wxheading{Parameters}
 
-\docparam{style}{One of {\bf wxNORMAL}, {\bf wxSLANT} and {\bf wxITALIC}.}
+\docparam{style}{One of {\bf wxFONTSTYLE\_NORMAL}, {\bf wxFONTSTYLE\_SLANT} and {\bf wxFONTSTYLE\_ITALIC}.}
 
 \wxheading{See also}
 
@@ -485,13 +507,20 @@ Sets underlining.
 
 \membersection{wxFont::SetWeight}\label{wxfontsetweight}
 
-\func{void}{SetWeight}{\param{int}{ weight}}
+\func{void}{SetWeight}{\param{wxFontWeight}{ weight}}
 
 Sets the font weight.
 
 \wxheading{Parameters}
 
-\docparam{weight}{One of {\bf wxNORMAL}, {\bf wxLIGHT} and {\bf wxBOLD}.}
+\docparam{weight}{One of:
+
+\twocolwidtha{5cm}
+\begin{twocollist}\itemsep=0pt
+\twocolitem{{\bf wxFONTWEIGHT\_NORMAL}}{Normal font.}
+\twocolitem{{\bf wxFONTWEIGHT\_LIGHT}}{Light font.}
+\twocolitem{{\bf wxFONTWEIGHT\_BOLD}}{Bold font.}
+\end{twocollist}}
 
 \wxheading{See also}
 
diff --git a/src/gtk/font.cpp b/src/gtk/font.cpp
index 6201232e19..853e34a68c 100644
--- a/src/gtk/font.cpp
+++ b/src/gtk/font.cpp
@@ -240,7 +240,7 @@ void wxFontRefData::InitFromNative()
     int pango_size = pango_font_description_get_size( desc );
     if (pango_size == 0)
         pango_font_description_set_size( desc, 12 * PANGO_SCALE);
-        
+
     m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE;
 
     switch (pango_font_description_get_style( desc ))
@@ -256,26 +256,19 @@ void wxFontRefData::InitFromNative()
             break;
     }
 
-    switch (pango_font_description_get_weight( desc ))
+    PangoWeight pango_weight = pango_font_description_get_weight( desc );
+
+    if (pango_weight >= 600)
     {
-        case PANGO_WEIGHT_ULTRALIGHT:
-            m_weight = wxFONTWEIGHT_LIGHT;
-            break;
-        case PANGO_WEIGHT_LIGHT:
-            m_weight = wxFONTWEIGHT_LIGHT;
-            break;
-        case PANGO_WEIGHT_NORMAL:
-            m_weight = wxFONTWEIGHT_NORMAL;
-            break;
-        case PANGO_WEIGHT_BOLD:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
-        case PANGO_WEIGHT_ULTRABOLD:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
-        case PANGO_WEIGHT_HEAVY:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
+        m_weight = wxFONTWEIGHT_BOLD;
+    }
+    else if (pango_weight < 350)
+    {
+        m_weight = wxFONTWEIGHT_LIGHT;
+    }
+    else
+    {
+        m_weight = wxFONTWEIGHT_NORMAL;
     }
 
     if (m_faceName == wxT("monospace"))
@@ -960,7 +953,7 @@ GdkFont *wxFont::GetInternalFont( float scale ) const
                                                M_FONTDATA->m_faceName,
                                                M_FONTDATA->m_encoding,
                                                &xfontname);
-                // NB: wxFont::GetNativeFontInfo relies on this 
+                // NB: wxFont::GetNativeFontInfo relies on this
                 //     side-effect of GetInternalFont
                 if ( int_scale == 100 )
                     M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
diff --git a/src/gtk1/font.cpp b/src/gtk1/font.cpp
index 6201232e19..853e34a68c 100644
--- a/src/gtk1/font.cpp
+++ b/src/gtk1/font.cpp
@@ -240,7 +240,7 @@ void wxFontRefData::InitFromNative()
     int pango_size = pango_font_description_get_size( desc );
     if (pango_size == 0)
         pango_font_description_set_size( desc, 12 * PANGO_SCALE);
-        
+
     m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE;
 
     switch (pango_font_description_get_style( desc ))
@@ -256,26 +256,19 @@ void wxFontRefData::InitFromNative()
             break;
     }
 
-    switch (pango_font_description_get_weight( desc ))
+    PangoWeight pango_weight = pango_font_description_get_weight( desc );
+
+    if (pango_weight >= 600)
     {
-        case PANGO_WEIGHT_ULTRALIGHT:
-            m_weight = wxFONTWEIGHT_LIGHT;
-            break;
-        case PANGO_WEIGHT_LIGHT:
-            m_weight = wxFONTWEIGHT_LIGHT;
-            break;
-        case PANGO_WEIGHT_NORMAL:
-            m_weight = wxFONTWEIGHT_NORMAL;
-            break;
-        case PANGO_WEIGHT_BOLD:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
-        case PANGO_WEIGHT_ULTRABOLD:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
-        case PANGO_WEIGHT_HEAVY:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
+        m_weight = wxFONTWEIGHT_BOLD;
+    }
+    else if (pango_weight < 350)
+    {
+        m_weight = wxFONTWEIGHT_LIGHT;
+    }
+    else
+    {
+        m_weight = wxFONTWEIGHT_NORMAL;
     }
 
     if (m_faceName == wxT("monospace"))
@@ -960,7 +953,7 @@ GdkFont *wxFont::GetInternalFont( float scale ) const
                                                M_FONTDATA->m_faceName,
                                                M_FONTDATA->m_encoding,
                                                &xfontname);
-                // NB: wxFont::GetNativeFontInfo relies on this 
+                // NB: wxFont::GetNativeFontInfo relies on this
                 //     side-effect of GetInternalFont
                 if ( int_scale == 100 )
                     M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
diff --git a/src/unix/fontutil.cpp b/src/unix/fontutil.cpp
index 6599fa5e71..082ce17f8b 100644
--- a/src/unix/fontutil.cpp
+++ b/src/unix/fontutil.cpp
@@ -29,6 +29,7 @@
 #endif
 
 #ifndef WX_PRECOMP
+    #include "wx/font.h" // wxFont enums
     #include "wx/encinfo.h"
 #endif // PCH
 
@@ -99,31 +100,27 @@ wxFontStyle wxNativeFontInfo::GetStyle() const
 
 wxFontWeight wxNativeFontInfo::GetWeight() const
 {
-    wxFontWeight m_weight = wxFONTWEIGHT_NORMAL;
+#if 0
+    // We seem to currently initialize only by string.
+    // In that case PANGO_FONT_MASK_WEIGHT is always set.
+    if (!(pango_font_description_get_set_fields(description) & PANGO_FONT_MASK_WEIGHT))
+        return wxFONTWEIGHT_NORMAL;
+#endif
 
-    switch (pango_font_description_get_weight( description ))
-    {
-        case PANGO_WEIGHT_ULTRALIGHT:
-            m_weight = wxFONTWEIGHT_LIGHT;
-            break;
-        case PANGO_WEIGHT_LIGHT:
-            m_weight = wxFONTWEIGHT_LIGHT;
-            break;
-        case PANGO_WEIGHT_NORMAL:
-            m_weight = wxFONTWEIGHT_NORMAL;
-            break;
-        case PANGO_WEIGHT_BOLD:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
-        case PANGO_WEIGHT_ULTRABOLD:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
-        case PANGO_WEIGHT_HEAVY:
-            m_weight = wxFONTWEIGHT_BOLD;
-            break;
-    }
+    PangoWeight pango_weight = pango_font_description_get_weight( description );
+
+    // Until the API can be changed the following ranges of weight values are used:
+    // wxFONTWEIGHT_LIGHT:  100 .. 349 - range of 250
+    // wxFONTWEIGHT_NORMAL: 350 .. 599 - range of 250
+    // wxFONTWEIGHT_BOLD:   600 .. 900 - range of 301 (600 is "semibold" already)
+
+    if (pango_weight >= 600)
+        return wxFONTWEIGHT_BOLD;
 
-    return m_weight;
+    if (pango_weight < 350)
+        return wxFONTWEIGHT_LIGHT;
+
+    return wxFONTWEIGHT_NORMAL;
 }
 
 bool wxNativeFontInfo::GetUnderlined() const
-- 
2.50.0

