From 74c0fe6dccdf66d3bd766861749fc7262db585ae Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Thu, 28 Apr 2022 19:01:30 +0200 Subject: [PATCH] Serialize font style correctly in Mac wxNativeFontInfo The style needs to be explicitly included in the serialized representation as it's not necessarily part of the native font description itself, but can be emulated using a transform matrix. Also fix the font family handling by recreating the native font info from the newly generated font. Closes #22373. --- src/osx/carbon/font.cpp | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp index fc0c5e78a6..742adfabba 100644 --- a/src/osx/carbon/font.cpp +++ b/src/osx/carbon/font.cpp @@ -384,6 +384,9 @@ void wxFontRefData::Alloc() } } + m_info = wxNativeFontInfo(); + m_info.InitFromFont(m_ctFont); + entryWithSize.font = m_ctFont; entryWithSize.cgFont = m_cgFont; entryWithSize.fontAttributes = m_ctFontAttributes; @@ -855,6 +858,13 @@ void wxNativeFontInfo::InitFromFont(CTFontRef font) wxCFRef desc(CTFontCopyFontDescriptor(font)); InitFromFontDescriptor( desc ); + + // in case the font itself does not exist as italic it might have + // been created with the emulation via a font transform + if ( !CGAffineTransformIsIdentity(CTFontGetMatrix(font)) ) + { + m_style = wxFONTSTYLE_ITALIC; + } } void wxNativeFontInfo::InitFromFontDescriptor(CTFontDescriptorRef desc) @@ -1122,8 +1132,18 @@ bool wxNativeFontInfo::FromString(const wxString& s) m_encoding = (wxFontEncoding)l; return true; } - else if ( version == 2 ) + else if ( version == 2 || version == 3 ) { + wxFontStyle style = wxFONTSTYLE_NORMAL; + + if ( version == 3 ) + { + token = tokenizer.GetNextToken(); + if ( !token.ToLong(&l) ) + return false; + style = (wxFontStyle)l; + } + token = tokenizer.GetNextToken(); if ( !token.ToLong(&l) ) return false; @@ -1154,6 +1174,7 @@ bool wxNativeFontInfo::FromString(const wxString& s) m_underlined = underlined; m_strikethrough = strikethrough; m_encoding = encoding; + m_style = style; return true; } } @@ -1167,6 +1188,9 @@ wxString wxNativeFontInfo::ToString() const // version 2 is a streamed property list of the font descriptor as recommended by Apple // prefixed by the attributes that are non-native to the native font ref like underline, strikethrough etc. + // version 3 adds to v2 the style attribute because this cannot always be expressed + // in the native font descriptor. In situations where there is no native italic font + // the slant-ness has to be emulated in the font's transform wxCFDictionaryRef attributes(CTFontDescriptorCopyAttributes(GetCTFontDescriptor())); if (attributes != NULL) @@ -1181,8 +1205,9 @@ wxString wxNativeFontInfo::ToString() const xml.Replace("\t",wxEmptyString,true); xml = xml.Mid(xml.Find("