From ca164bb4ca8980aadcb3077eb1c9da66ea75b906 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 15 Sep 2018 13:30:45 +0200 Subject: [PATCH] Support fractional font sizes and numeric weights in XRC Change the code to handle them, the XRC sample to test them, the schema to accept them and the documentation to describe them. --- docs/doxygen/overviews/xrc_format.h | 14 +++++++---- misc/schema/xrc_schema.rnc | 4 ++-- samples/xrc/rc/controls.xrc | 24 ++++++++++++++----- src/xrc/xmlres.cpp | 36 +++++++++++++++++++++-------- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/docs/doxygen/overviews/xrc_format.h b/docs/doxygen/overviews/xrc_format.h index 02270bebcc..d8702ef93d 100644 --- a/docs/doxygen/overviews/xrc_format.h +++ b/docs/doxygen/overviews/xrc_format.h @@ -403,14 +403,20 @@ and can be one of the following "sub-properties": @beginTable @hdr3col{property, type, description} -@row3col{size, unsigned integer, +@row3col{size, float, Pixel size of the font (default: wxNORMAL_FONT's size or @c sysfont's size if the @c sysfont property is used or the current size of the font - of the enclosing control if the @c inherit property is used.} + of the enclosing control if the @c inherit property is used. Note that + versions of wxWidgets until 3.1.2 only supported integer values for the + font size.} @row3col{style, enum, One of "normal", "italic" or "slant" (default: normal).} -@row3col{weight, enum, - One of "normal", "bold" or "light" (default: normal).} +@row3col{weight, enum or integer, + One of "thin", "extralight", "light", "normal", "medium", "semibold", + "bold", "extrabold", "heavy", "extraheavy", corresponding to the similarly + named elements of ::wxFontWeight enum, or a numeric value between 1 and + 1000 (default: normal). Note that versions of wxWidgets until 3.1.2 only + supported "light", "normal" and "bold" values for weight.} @row3col{family, enum, One of "default", "roman", "script", "decorative", "swiss", "modern" or "teletype" (default: default).} diff --git a/misc/schema/xrc_schema.rnc b/misc/schema/xrc_schema.rnc index 9d5491e48f..26e9efa7e7 100644 --- a/misc/schema/xrc_schema.rnc +++ b/misc/schema/xrc_schema.rnc @@ -454,11 +454,11 @@ t_bitmap = t_url?, )? t_font = ( - [xrc:p="o"] element size {_, t_integer }* & + [xrc:p="o"] element size {_, t_float }* & [xrc:p="o"] element style {_, ("normal" | "italic" | "slant") }* & [xrc:p="o"] element weight {_, ("normal" | "thin" | "extralight" | "light" | "medium" | "semibold" | "bold" | "extrabold" | - "heavy" | "extraheavy") }* & + "heavy" | "extraheavy" | t_integer) }* & [xrc:p="o"] element family {_, ("roman" | "script" | "decorative" | "swiss" | "modern" | "teletype") }* & [xrc:p="o"] element underlined {_, t_bool }* & diff --git a/samples/xrc/rc/controls.xrc b/samples/xrc/rc/controls.xrc index 2ee51c1212..e93a7ce0e8 100644 --- a/samples/xrc/rc/controls.xrc +++ b/samples/xrc/rc/controls.xrc @@ -347,7 +347,6 @@ wxGROW|wxALL 5 - 0 @@ -1096,6 +1095,23 @@ lay them out using wxSizers, absolute positioning, everything you like! + + wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxALL + 5 + + + + + + wxALIGN_LEFT|wxALIGN_CENTRE_VERTICAL + 5 + + + + 12.5 + + + wxALIGN_RIGHT|wxALIGN_CENTRE_VERTICAL|wxALL 5 @@ -1127,7 +1143,7 @@ lay them out using wxSizers, absolute positioning, everything you like! 1 - bold + 700 @@ -1494,7 +1510,6 @@ lay them out using wxSizers, absolute positioning, everything you like! wxGROW|wxALL - 5 #00ff00 @@ -1510,7 +1525,6 @@ lay them out using wxSizers, absolute positioning, everything you like! wxGROW|wxALL - 5 Here goes a message @@ -1528,7 +1542,6 @@ lay them out using wxSizers, absolute positioning, everything you like! wxGROW|wxALL - 5 @@ -1545,7 +1558,6 @@ lay them out using wxSizers, absolute positioning, everything you like! wxGROW|wxALL - 5 diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index b196ad873c..2bf281e37b 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -2275,10 +2275,10 @@ wxFont wxXmlResourceHandlerImpl::GetFont(const wxString& param, wxWindow* parent // font attributes: // size - int isize = -1; + float pointSize = -1.0f; bool hasSize = HasParam(wxT("size")); if (hasSize) - isize = GetLong(wxT("size"), -1); + pointSize = GetFloat(wxT("size"), -1.0f); // style wxFontStyle istyle = wxFONTSTYLE_NORMAL; @@ -2301,12 +2301,23 @@ wxFont wxXmlResourceHandlerImpl::GetFont(const wxString& param, wxWindow* parent } // weight - wxFontWeight iweight = wxFONTWEIGHT_NORMAL; + long iweight = wxFONTWEIGHT_NORMAL; bool hasWeight = HasParam(wxT("weight")); if (hasWeight) { wxString weight = GetParamValue(wxT("weight")); - if (weight == wxT("thin")) + if (weight.ToLong(&iweight)) + { + if (iweight <= wxFONTWEIGHT_INVALID || iweight > wxFONTWEIGHT_MAX) + { + ReportParamError + ( + param, + wxString::Format("invalid font weight value \"%d\"", iweight) + ); + } + } + else if (weight == wxT("thin")) iweight = wxFONTWEIGHT_THIN; else if (weight == wxT("extralight")) iweight = wxFONTWEIGHT_EXTRALIGHT; @@ -2433,9 +2444,9 @@ wxFont wxXmlResourceHandlerImpl::GetFont(const wxString& param, wxWindow* parent if (font.IsOk()) { - if (hasSize && isize != -1) + if (pointSize > 0) { - font.SetPointSize(isize); + font.SetFractionalPointSize(pointSize); if (HasParam(wxT("relativesize"))) { ReportParamError @@ -2452,7 +2463,7 @@ wxFont wxXmlResourceHandlerImpl::GetFont(const wxString& param, wxWindow* parent if (hasStyle) font.SetStyle(istyle); if (hasWeight) - font.SetWeight(iweight); + font.SetNumericWeight(iweight); if (hasUnderlined) font.SetUnderlined(underlined); if (hasFamily) @@ -2464,9 +2475,14 @@ wxFont wxXmlResourceHandlerImpl::GetFont(const wxString& param, wxWindow* parent } else // not based on system font { - font = wxFont(isize == -1 ? wxNORMAL_FONT->GetPointSize() : isize, - ifamily, istyle, iweight, - underlined, facename, enc); + font = wxFontInfo(pointSize) + .FaceName(facename) + .Family(ifamily) + .Style(istyle) + .Weight(iweight) + .Underlined(underlined) + .Encoding(enc) + ; } m_handler->m_node = oldnode;