• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Kevin Ollivier <kevino@theolliviers.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "fontprops.h"
28 
29 #include <ApplicationServices/ApplicationServices.h>
30 
31 #include <wx/defs.h>
32 #include <wx/gdicmn.h>
33 
34 #ifdef BUILDING_ON_TIGER
35 void (*wkGetFontMetrics)(CGFontRef, int* ascent, int* descent, int* lineGap, unsigned* unitsPerEm);
36 #endif
37 
38 const float smallCapsFontSizeMultiplier = 0.7f;
39 const float contextDPI = 72.0f;
scaleEmToUnits(float x,unsigned unitsPerEm)40 static inline float scaleEmToUnits(float x, unsigned unitsPerEm) { return x * (contextDPI / (contextDPI * unitsPerEm)); }
41 
wxFontProperties(wxFont * font)42 wxFontProperties::wxFontProperties(wxFont* font):
43 m_ascent(0), m_descent(0), m_lineGap(0), m_lineSpacing(0), m_xHeight(0)
44 {
45     CGFontRef cgFont;
46 
47 #ifdef wxOSX_USE_CORE_TEXT && wxOSX_USE_CORE_TEXT
48     cgFont = CTFontCopyGraphicsFont((CTFontRef)font->OSXGetCTFont(), NULL);
49 #else
50     ATSFontRef fontRef;
51 
52     fontRef = FMGetATSFontRefFromFont(font->MacGetATSUFontID());
53 
54     if (fontRef)
55         cgFont = CGFontCreateWithPlatformFont((void*)&fontRef);
56 #endif
57 
58     if (cgFont) {
59         int iAscent;
60         int iDescent;
61         int iLineGap;
62         unsigned unitsPerEm;
63 #ifdef BUILDING_ON_TIGER
64         wkGetFontMetrics(cgFont, &iAscent, &iDescent, &iLineGap, &unitsPerEm);
65 #else
66         iAscent = CGFontGetAscent(cgFont);
67         iDescent = CGFontGetDescent(cgFont);
68         iLineGap = CGFontGetLeading(cgFont);
69         unitsPerEm = CGFontGetUnitsPerEm(cgFont);
70 #endif
71         float pointSize = font->GetPointSize();
72         float fAscent = scaleEmToUnits(iAscent, unitsPerEm) * pointSize;
73         float fDescent = -scaleEmToUnits(iDescent, unitsPerEm) * pointSize;
74         float fLineGap = scaleEmToUnits(iLineGap, unitsPerEm) * pointSize;
75 
76         m_ascent = lroundf(fAscent);
77         m_descent = lroundf(fDescent);
78         m_lineGap = lroundf(fLineGap);
79         wxCoord xHeight = 0;
80         GetTextExtent(*font, wxT("x"), NULL, &xHeight, NULL, NULL);
81         m_xHeight = lroundf(xHeight);
82         m_lineSpacing = m_ascent + m_descent + m_lineGap;
83 
84     }
85 
86     if (cgFont)
87         CGFontRelease(cgFont);
88 
89 }
90 
GetTextExtent(const wxFont & font,const wxString & str,wxCoord * width,wxCoord * height,wxCoord * descent,wxCoord * externalLeading)91 void GetTextExtent( const wxFont& font, const wxString& str, wxCoord *width, wxCoord *height,
92                             wxCoord *descent, wxCoord *externalLeading )
93 {
94     ATSUStyle* ATSUIStyle;
95 
96     if ( font.Ok() )
97     {
98         OSStatus status ;
99 
100         status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , (ATSUStyle*) &ATSUIStyle ) ;
101 
102         wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") ) ;
103 
104         // we need the scale here ...
105 
106         Fixed atsuSize = IntToFixed( int( /*m_scaleY*/ 1 * font.GetPointSize()) ) ;
107         //RGBColor atsuColor = MAC_WXCOLORREF( m_textForegroundColor.GetPixel() ) ;
108         ATSUAttributeTag atsuTags[] =
109         {
110                 kATSUSizeTag //,
111         //        kATSUColorTag ,
112         } ;
113         ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
114         {
115                 sizeof( Fixed ) //,
116         //        sizeof( RGBColor ) ,
117         } ;
118         ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
119         {
120                 &atsuSize //,
121         //        &atsuColor ,
122         } ;
123 
124         status = ::ATSUSetAttributes(
125             (ATSUStyle)ATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
126             atsuTags, atsuSizes, atsuValues);
127 
128         wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ) ;
129     }
130 
131     wxCHECK_RET( ATSUIStyle != NULL, wxT("GetTextExtent - no valid font set") ) ;
132 
133     OSStatus status = noErr ;
134 
135     ATSUTextLayout atsuLayout ;
136     UniCharCount chars = str.length() ;
137     UniChar* ubuf = NULL ;
138 
139 #if SIZEOF_WCHAR_T == 4
140     wxMBConvUTF16 converter ;
141 #if wxUSE_UNICODE
142     size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
143     ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
144     converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
145 #else
146     const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
147     size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
148     ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
149     converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
150 #endif
151     chars = unicharlen / 2 ;
152 #else
153 #if wxUSE_UNICODE
154     ubuf = (UniChar*) str.wc_str() ;
155 #else
156     wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
157     chars = wxWcslen( wchar.data() ) ;
158     ubuf = (UniChar*) wchar.data() ;
159 #endif
160 #endif
161 
162     status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
163         &chars , (ATSUStyle*) &ATSUIStyle , &atsuLayout ) ;
164 
165     wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
166 
167     ATSUTextMeasurement textBefore, textAfter ;
168     ATSUTextMeasurement textAscent, textDescent ;
169 
170     status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
171         &textBefore , &textAfter, &textAscent , &textDescent );
172 
173     if ( height )
174         *height = FixedToInt(textAscent + textDescent) ;
175     if ( descent )
176         *descent = FixedToInt(textDescent) ;
177     if ( externalLeading )
178         *externalLeading = 0 ;
179     if ( width )
180         *width = FixedToInt(textAfter - textBefore) ;
181 
182 #if SIZEOF_WCHAR_T == 4
183     free( ubuf ) ;
184 #endif
185 
186     ::ATSUDisposeTextLayout(atsuLayout);
187     ::ATSUDisposeStyle((ATSUStyle)ATSUIStyle);
188 }
189