Fixes for the 'xamarin' profile.
[mono.git] / bockbuild / MacSDK / patches / pango-system-font-single.patch
1 diff --git a/configure.ac b/configure.ac
2 index 42c04ef..1260b8a 100644
3 --- a/configure.ac
4 +++ b/configure.ac
5 @@ -185,7 +185,7 @@ changequote(,)dnl
6  if test "x$GCC" = "xyes"; then
7    case " $CFLAGS " in
8    *[\ \        ]-Wall[\ \      ]*) ;;
9 -  *) CFLAGS="$CFLAGS -Wall" ;;
10 +  *) CFLAGS="$CFLAGS -Wall -ObjC" ;;
11    esac
12  fi
13  changequote([,])dnl
14 diff --git a/pango/pangocairo-coretext.h b/pango/pangocairo-coretext.h
15 index 31faf9b..f562587 100644
16 --- a/pango/pangocairo-coretext.h
17 +++ b/pango/pangocairo-coretext.h
18 @@ -43,6 +43,19 @@ struct _PangoCairoCoreTextFontMap
19    gdouble dpi;
20  };
21
22 +struct _PangoCairoCoreTextFont
23 +{
24 +  PangoCoreTextFont font;
25 +  PangoCairoFontPrivate cf_priv;
26 +
27 +  int abs_size;
28 +};
29 +
30 +struct _PangoCairoCoreTextFontClass
31 +{
32 +  PangoCoreTextFontClass parent_class;
33 +};
34 +
35  GType pango_cairo_core_text_font_map_get_type (void) G_GNUC_CONST;
36
37  PangoCoreTextFont *
38 diff --git a/pango/pangocairo-coretextfont.c b/pango/pangocairo-coretextfont.c
39 index 0f2a9ef..463b6e2 100644
40 --- a/pango/pangocairo-coretextfont.c
41 +++ b/pango/pangocairo-coretextfont.c
42 @@ -23,6 +23,7 @@
43
44  #include "config.h"
45
46 +#import <AppKit/AppKit.h>
47  #include <Carbon/Carbon.h>
48
49  #include "pango-impl-utils.h"
50 @@ -32,21 +33,6 @@
51  #include "pangocairo-coretext.h"
52  #include "pangocairo-coretextfont.h"
53
54 -struct _PangoCairoCoreTextFont
55 -{
56 -  PangoCoreTextFont font;
57 -  PangoCairoFontPrivate cf_priv;
58 -
59 -  int abs_size;
60 -};
61 -
62 -struct _PangoCairoCoreTextFontClass
63 -{
64 -  PangoCoreTextFontClass parent_class;
65 -};
66 -
67 -
68 -
69  static cairo_font_face_t *pango_cairo_core_text_font_create_font_face           (PangoCairoFont *font);
70  static PangoFontMetrics  *pango_cairo_core_text_font_create_base_metrics_for_context (PangoCairoFont *font,
71                                                                                        PangoContext    *context);
72 @@ -204,6 +190,75 @@ pango_cairo_core_text_font_init (PangoCairoCoreTextFont *cafont G_GNUC_UNUSED)
73  {
74  }
75
76 +static gchar *
77 +gchar_from_cf_string (CFStringRef str)
78 +{
79 +  CFIndex len;
80 +  gchar *buffer;
81 +
82 +  /* GetLength returns the number of UTF-16 pairs, so this number
83 +   * times 2 should definitely give us enough space for UTF8.
84 +   * We add one for the terminating zero.
85 +   */
86 +  len = CFStringGetLength (str) * 2 + 1;
87 +  buffer = g_new0 (char, len);
88 +  CFStringGetCString (str, buffer, len, kCFStringEncodingUTF8);
89 +
90 +  return buffer;
91 +}
92 +
93 +static gchar *
94 +ct_font_descriptor_get_family_name (CTFontDescriptorRef desc,
95 +                                    gboolean            may_fail)
96 +{
97 +  CFStringRef cf_str;
98 +  char *buffer;
99 +
100 +  cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontFamilyNameAttribute);
101 +  if (!cf_str)
102 +    {
103 +      int i;
104 +
105 +      /* No font family name is set, try to retrieve font name and deduce
106 +       * the family name from that instead.
107 +       */
108 +      cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontNameAttribute);
109 +      if (!cf_str)
110 +        {
111 +          if (may_fail)
112 +            return NULL;
113 +
114 +          /* This font is likely broken, return a default family name ... */
115 +          return g_strdup ("Sans");
116 +        }
117 +
118 +      buffer = gchar_from_cf_string (cf_str);
119 +      CFRelease (cf_str);
120 +
121 +      for (i = 0; i < strlen (buffer); i++)
122 +        if (buffer[i] == '-')
123 +          break;
124 +
125 +      if (i < strlen (buffer))
126 +        {
127 +          char *ret;
128 +
129 +          ret = g_strndup (buffer, i);
130 +          g_free (buffer);
131 +
132 +          return ret;
133 +        }
134 +      else
135 +        return buffer;
136 +    }
137 +  /* else */
138 +
139 +  buffer = gchar_from_cf_string (cf_str);
140 +  CFRelease (cf_str);
141 +
142 +  return buffer;
143 +}
144 +
145  PangoCoreTextFont *
146  _pango_cairo_core_text_font_new (PangoCairoCoreTextFontMap  *cafontmap,
147                                   PangoCoreTextFontKey       *key)
148 diff --git a/pango/pangocairo-coretextfontmap.c b/pango/pangocairo-coretextfontmap.c
149 index ba5dcec..6cb3809 100644
150 --- a/pango/pangocairo-coretextfontmap.c
151 +++ b/pango/pangocairo-coretextfontmap.c
152 @@ -26,6 +26,9 @@
153  #include "pangocairo.h"
154  #include "pangocairo-private.h"
155  #include "pangocairo-coretext.h"
156 +#include "pangocairo-coretextfont.h"
157 +
158 +#import <AppKit/Appkit.h>
159
160  typedef struct _PangoCairoCoreTextFontMapClass PangoCairoCoreTextFontMapClass;
161
162 @@ -161,6 +164,106 @@ pango_cairo_core_text_font_map_context_key_equal (PangoCoreTextFontMap *fontmap
163    return cairo_font_options_equal (key_a, key_b);
164  }
165
166 +static int
167 +get_scaled_size (PangoCoreTextFontMap       *fontmap,
168 +                 PangoContext               *context,
169 +                 const PangoFontDescription *desc)
170 +{
171 +  double size = pango_font_description_get_size (desc);
172 +
173 +  if (!pango_font_description_get_size_is_absolute (desc))
174 +    {
175 +      double dpi = pango_cairo_core_text_font_map_get_resolution_core_text (fontmap, context);
176 +      size = size * dpi / 72.;
177 +    }
178 +
179 +  return .5 + pango_matrix_get_font_scale_factor (pango_context_get_matrix (context)) * size;
180 +}
181 +
182 +static PangoCoreTextFont *
183 +pango_cairo_core_text_font_map_load_font (PangoCoreTextFontMap       *fontmap,
184 +                                          PangoContext               *context,
185 +                                          const PangoFontDescription *description)
186 +{
187 +  char *descname = pango_font_description_get_family (description);
188 +
189 +  if (strcmp (descname, "-apple-system-font") == 0)
190 +    {
191 +      PangoCoreTextFont *cfont;
192 +      NSFont *sysfont;
193 +      CTFontDescriptorRef ctfontdescriptor;
194 +      cairo_matrix_t font_matrix;
195 +      PangoCoreTextFontsetKey fontset_key;
196 +      PangoCoreTextFontKey key;
197 +      PangoLanguage *language;
198 +      PangoFontDescription *tmp_desc;
199 +      gboolean synthetic_italic = FALSE;
200 +      double abs_size = get_scaled_size (fontmap, context, description);
201 +      double size = pango_units_to_double (abs_size) / pango_matrix_get_font_scale_factor (pango_context_get_matrix (context));
202 +
203 +      sysfont = [NSFont systemFontOfSize: size];
204 +      ctfontdescriptor = (CTFontDescriptorRef)[sysfont fontDescriptor];
205 +
206 +      PangoCairoCoreTextFont *cafont = g_object_new (PANGO_TYPE_CAIRO_CORE_TEXT_FONT, NULL);
207 +      cfont = PANGO_CORE_TEXT_FONT (cafont);
208 +      cafont->abs_size = abs_size;
209 +      _pango_core_text_font_set_ctfont (cfont, CFRetain (sysfont));
210 +
211 +      tmp_desc = pango_font_description_copy_static (description);
212 +
213 +      _pango_core_text_fontset_key_init (&fontset_key,
214 +                                         fontmap,
215 +                                         context,
216 +                                         tmp_desc,
217 +                                         pango_context_get_language (context));
218 +      _pango_core_text_font_key_init (&key,
219 +                                      fontmap,
220 +                                      &fontset_key,
221 +                                      ctfontdescriptor,
222 +                                      synthetic_italic);
223 +
224 +      if (pango_core_text_font_key_get_synthetic_italic (&key))
225 +        synthetic_italic = TRUE;
226 +
227 +      if (synthetic_italic)
228 +        {
229 +          cairo_matrix_init (&font_matrix,
230 +                             1, 0,
231 +                             -0.25, 1,
232 +                             0, 0);
233 +        }
234 +      else
235 +        {
236 +          cairo_matrix_init_identity (&font_matrix);
237 +        }
238 +
239 +      cairo_matrix_scale (&font_matrix, size, size);
240 +
241 +      _pango_cairo_font_private_initialize (&cafont->cf_priv,
242 +                                            (PangoCairoFont *)cafont,
243 +                                            pango_core_text_font_key_get_gravity (&key),
244 +                                            pango_core_text_font_key_get_context_key (&key),
245 +                                            pango_core_text_font_key_get_matrix (&key),
246 +                                            &font_matrix);
247 +
248 +      PangoCoreTextFont *tmp_font = _pango_core_text_font_map_lookup_font (fontmap, &key);
249 +      if (tmp_font)
250 +        {
251 +          g_object_ref (tmp_font);
252 +          return tmp_font;
253 +        }
254 +      else
255 +        {
256 +          g_object_ref (cfont);
257 +          _pango_core_text_font_map_add (PANGO_CORE_TEXT_FONT_MAP (fontmap), &key, cfont);
258 +
259 +          return cfont;
260 +        }
261 +    }
262 +
263 +  return PANGO_FONT_MAP_CLASS (pango_cairo_core_text_font_map_parent_class)->load_font (fontmap, context, description);
264 +}
265 +
266  static void
267  pango_cairo_core_text_font_map_class_init (PangoCairoCoreTextFontMapClass *class)
268  {
269 @@ -170,6 +273,7 @@ pango_cairo_core_text_font_map_class_init (PangoCairoCoreTextFontMapClass *class
270
271    object_class->finalize = pango_cairo_core_text_font_map_finalize;
272
273 +  fontmap_class->load_font = pango_cairo_core_text_font_map_load_font;
274    fontmap_class->get_serial = pango_cairo_core_text_font_map_get_serial;
275    fontmap_class->changed = pango_cairo_core_text_font_map_changed;
276
277 diff --git a/pango/pangocoretext-fontmap.c b/pango/pangocoretext-fontmap.c
278 index bcbb173..4234bf1 100644
279 --- a/pango/pangocoretext-fontmap.c
280 +++ b/pango/pangocoretext-fontmap.c
281 @@ -28,6 +28,7 @@
282  #include "pango-impl-utils.h"
283  #include "modules.h"
284
285 +#import <AppKit/Appkit.h>
286  #include <Carbon/Carbon.h>
287
288  typedef struct _FontHashKey      FontHashKey;
289 @@ -826,28 +827,12 @@ get_scaled_size (PangoCoreTextFontMap       *fontmap,
290    return .5 + pango_matrix_get_font_scale_factor (pango_context_get_matrix (context)) * size;
291  }
292
293 -
294 -/*
295 - * PangoCoreTextFontsetKey
296 - */
297 -struct _PangoCoreTextFontsetKey
298 -{
299 -  PangoCoreTextFontMap *fontmap;
300 -  PangoLanguage *language;
301 -  PangoFontDescription *desc;
302 -  PangoMatrix matrix;
303 -  int pixelsize;
304 -  double resolution;
305 -  PangoGravity gravity;
306 -  gpointer context_key;
307 -};
308 -
309 -static void
310 -pango_core_text_fontset_key_init (PangoCoreTextFontsetKey    *key,
311 -                                  PangoCoreTextFontMap       *fontmap,
312 -                                  PangoContext               *context,
313 -                                  const PangoFontDescription *desc,
314 -                                  PangoLanguage              *language)
315 +void
316 +_pango_core_text_fontset_key_init (PangoCoreTextFontsetKey    *key,
317 +                                   PangoCoreTextFontMap       *fontmap,
318 +                                   PangoContext               *context,
319 +                                   const PangoFontDescription *desc,
320 +                                   PangoLanguage              *language)
321  {
322    if (!language && context)
323      language = pango_context_get_language (context);
324 @@ -960,27 +945,12 @@ pango_core_text_fontset_key_get_context_key (const PangoCoreTextFontsetKey *key)
325    return key->context_key;
326  }
327
328 -/*
329 - * PangoCoreTextFontKey
330 - */
331 -struct _PangoCoreTextFontKey
332 -{
333 -  PangoCoreTextFontMap *fontmap;
334 -  CTFontDescriptorRef ctfontdescriptor;
335 -  PangoMatrix matrix;
336 -  PangoGravity gravity;
337 -  int pixelsize;
338 -  double resolution;
339 -  gboolean synthetic_italic;
340 -  gpointer context_key;
341 -};
342 -
343 -static void
344 -pango_core_text_font_key_init (PangoCoreTextFontKey    *key,
345 -                               PangoCoreTextFontMap    *ctfontmap,
346 -                               PangoCoreTextFontsetKey *fontset_key,
347 -                               CTFontDescriptorRef      ctdescriptor,
348 -                               gboolean                 synthetic_italic)
349 +void
350 +_pango_core_text_font_key_init (PangoCoreTextFontKey    *key,
351 +                                PangoCoreTextFontMap    *ctfontmap,
352 +                                PangoCoreTextFontsetKey *fontset_key,
353 +                                CTFontDescriptorRef      ctdescriptor,
354 +                                gboolean                 synthetic_italic)
355  {
356    key->fontmap = ctfontmap;
357    key->ctfontdescriptor = ctdescriptor;
358 @@ -1104,14 +1074,24 @@ pango_core_text_font_key_get_ctfontdescriptor (const PangoCoreTextFontKey *key)
359    return key->ctfontdescriptor;
360  }
361
362 +PangoCoreTextFont *
363 +_pango_core_text_font_map_lookup_font (PangoCoreTextFontMap *fontmap,
364 +                                       PangoCoreTextFontKey *key)
365 +{
366 +  return g_hash_table_lookup (fontmap->font_hash, key);
367 +}
368
369 -
370 -static void
371 -pango_core_text_font_map_add (PangoCoreTextFontMap *ctfontmap,
372 -                              PangoCoreTextFontKey *key,
373 -                              PangoCoreTextFont    *ctfont)
374 +void
375 +_pango_core_text_font_map_add (PangoCoreTextFontMap *ctfontmap,
376 +                               PangoCoreTextFontKey *key,
377 +                               PangoCoreTextFont    *ctfont)
378  {
379    PangoCoreTextFontKey *key_copy;
380 +  PangoCoreTextFont *tmp;
381 +
382 +  tmp = g_hash_table_lookup (ctfontmap->font_hash, key);
383 +  if (tmp)
384 +    return;
385
386    _pango_core_text_font_set_font_map (ctfont, ctfontmap);
387
388 @@ -1130,8 +1110,8 @@ pango_core_text_font_map_new_font (PangoCoreTextFontMap    *fontmap,
389    PangoCoreTextFont *font;
390    PangoCoreTextFontKey key;
391
392 -  pango_core_text_font_key_init (&key, fontmap, fontset_key, ctfontdescriptor,
393 -                                 synthetic_italic);
394 +  _pango_core_text_font_key_init (&key, fontmap, fontset_key, ctfontdescriptor,
395 +                                  synthetic_italic);
396
397    font = g_hash_table_lookup (fontmap->font_hash, &key);
398    if (font)
399 @@ -1144,7 +1124,7 @@ pango_core_text_font_map_new_font (PangoCoreTextFontMap    *fontmap,
400    if (!font)
401      return NULL;
402
403 -  pango_core_text_font_map_add (fontmap, &key, font);
404 +  _pango_core_text_font_map_add (fontmap, &key, font);
405
406    return font;
407  }
408 @@ -1295,8 +1275,8 @@ pango_core_text_font_map_load_fontset (PangoFontMap               *fontmap,
409    PangoCoreTextFontMap *ctfontmap = PANGO_CORE_TEXT_FONT_MAP (fontmap);
410    static gboolean warned_full_fallback = FALSE; /* MT-safe */
411
412 -  pango_core_text_fontset_key_init (&key, ctfontmap,
413 -                                    context, desc, language);
414 +  _pango_core_text_fontset_key_init (&key, ctfontmap,
415 +                                     context, desc, language);
416
417    fontset = g_hash_table_lookup (ctfontmap->fontset_hash, &key);
418
419 @@ -1320,8 +1300,8 @@ pango_core_text_font_map_load_fontset (PangoFontMap               *fontmap,
420            tmp_desc = pango_font_description_copy_static (desc);
421            pango_font_description_set_family_static (tmp_desc, "Sans");
422
423 -          pango_core_text_fontset_key_init (&key, ctfontmap, context, tmp_desc,
424 -                                            language);
425 +          _pango_core_text_fontset_key_init (&key, ctfontmap, context, tmp_desc,
426 +                                             language);
427
428            fontset = g_hash_table_lookup (ctfontmap->fontset_hash, &key);
429            if (G_LIKELY (fontset))
430 @@ -1450,6 +1430,53 @@ pango_core_text_font_map_init (PangoCoreTextFontMap *ctfontmap)
431        CFRelease (dict);
432      }
433
434 +  if (true)
435 +    {
436 +      NSFont *sysfont = [NSFont systemFontOfSize: 0.0];
437 +      NSString *name = [[NSFont fontWithName: [[sysfont fontDescriptor] postscriptName] size: 0.0] familyName];
438 +
439 +      NSArray *fontfaces = [[NSFontManager sharedFontManager] availableMembersOfFontFamily: name];
440 +      int num_faces = [fontfaces count];
441 +      CFRelease (name);
442 +
443 +      for (int faceindex = 0; faceindex < num_faces; faceindex++)
444 +        {
445 +          SInt64 font_traits;
446 +          char *buffer;
447 +          char *family_name;
448 +          CFNumberRef number;
449 +          CFDictionaryRef dict;
450 +          CTFontDescriptorRef desc = [sysfont fontDescriptor];
451 +
452 +          buffer = ct_font_descriptor_get_family_name (desc, TRUE);
453 +          if (!buffer)
454 +            continue;
455 +
456 +          family_name = g_utf8_casefold (buffer, -1);
457 +          family = g_hash_table_lookup (ctfontmap->families, family_name);
458 +          if (!family)
459 +            {
460 +              family = g_object_new (PANGO_TYPE_CORE_TEXT_FAMILY, NULL);
461 +              g_hash_table_insert (ctfontmap->families, g_strdup ("-apple-system-font"), family);
462 +              family->family_name = g_strdup (buffer);
463 +            }
464 +
465 +          g_free (buffer);
466 +          g_free (family_name);
467 +
468 +          dict = CTFontDescriptorCopyAttribute (desc, kCTFontTraitsAttribute);
469 +          number = (CFNumberRef)CFDictionaryGetValue (dict, kCTFontSymbolicTrait);
470 +
471 +          if (CFNumberGetValue (number, kCFNumberSInt64Type, &font_traits))
472 +            {
473 +              if ((font_traits & kCTFontMonoSpaceTrait) == kCTFontMonoSpaceTrait)
474 +                family->is_monospace = TRUE;
475 +            }
476 +
477 +          CFRelease (dict);
478 +        }
479 +    }
480 +
481    /* Insert aliases */
482    family = g_object_new (PANGO_TYPE_CORE_TEXT_FAMILY, NULL);
483    family->family_name = g_strdup ("Sans");
484 diff --git a/pango/pangocoretext-private.h b/pango/pangocoretext-private.h
485 index 8076eae..5c6ce41 100644
486 --- a/pango/pangocoretext-private.h
487 +++ b/pango/pangocoretext-private.h
488 @@ -81,6 +81,30 @@ struct _PangoCoreTextFontMapClass
489                                            PangoContext              *context);
490  };
491
492 +struct _PangoCoreTextFontKey
493 +{
494 +  PangoCoreTextFontMap *fontmap;
495 +  CTFontDescriptorRef ctfontdescriptor;
496 +  PangoMatrix matrix;
497 +  PangoGravity gravity;
498 +  int pixelsize;
499 +  double resolution;
500 +  gboolean synthetic_italic;
501 +  gpointer context_key;
502 +};
503 +
504 +struct _PangoCoreTextFontsetKey
505 +{
506 +  PangoCoreTextFontMap *fontmap;
507 +  PangoLanguage *language;
508 +  PangoFontDescription *desc;
509 +  PangoMatrix matrix;
510 +  int pixelsize;
511 +  double resolution;
512 +  PangoGravity gravity;
513 +  gpointer context_key;
514 +};
515 +
516
517  GType                 pango_core_text_font_map_get_type          (void) G_GNUC_CONST;
518
519 @@ -97,6 +121,24 @@ void                  _pango_core_text_font_set_font_key         (PangoCoreTextF
520  void                  _pango_core_text_font_set_ctfont           (PangoCoreTextFont    *font,
521                                                                    CTFontRef         font_ref);
522
523 +void                  _pango_core_text_fontset_key_init          (PangoCoreTextFontsetKey    *key,
524 +                                                                  PangoCoreTextFontMap       *fontmap,
525 +                                                                  PangoContext               *context,
526 +                                                                  const PangoFontDescription *desc,
527 +                                                                  PangoLanguage              *language);
528 +void                  _pango_core_text_font_key_init             (PangoCoreTextFontKey       *key,
529 +                                                                  PangoCoreTextFontMap       *ctfontmap,
530 +                                                                  PangoCoreTextFontsetKey    *fontset_key,
531 +                                                                  CTFontDescriptorRef         ctdescriptor,
532 +                                                                  gboolean                    synthetic_italic);
533 +
534 +void                  _pango_core_text_font_map_add              (PangoCoreTextFontMap       *ctfontmap,
535 +                                                                  PangoCoreTextFontKey       *key,
536 +                                                                  PangoCoreTextFont          *ctfont);
537 +
538 +PangoCoreTextFont    *_pango_core_text_font_map_lookup_font      (PangoCoreTextFontMap       *fontmap,
539 +                                                                  PangoCoreTextFontKey       *key);
540 +
541  PangoFontDescription *_pango_core_text_font_description_from_ct_font_descriptor (CTFontDescriptorRef desc);
542
543  int                   pango_core_text_font_key_get_absolute_size    (const PangoCoreTextFontKey *key);