-/* Auto-creates a fontset built around the font in font_object,
- by creating an attributed string with characters from each
- script, then requesting the NS text system to fix attributes
- in range. */
-void nsfont_make_fontset_for_font (Lisp_Object name, Lisp_Object font_object)
-{
- Lisp_Object script, famAndReg;
- struct nsfont_info *font_info =
- (struct nsfont_info *)XFONT_OBJECT (font_object);
-
- /* NS text system (and char buf) init */
- static NSTextStorage *store;
- static NSLayoutManager *layout;
- static NSRange range;
- static NSMutableDictionary *attribs;
- static Lisp_Object *scripts;
- static int nscripts;
- static int *scriptsNchars;
- static BOOL firstTime = YES;
- Lisp_Object regString = build_string ("iso10646-1");
- int i, j;
-
- if (firstTime == YES)
- {
- nscripts = XINT (Flength (Vscript_representative_chars));
- scriptsNchars = (int *) malloc (nscripts * sizeof (int));
- unsigned char *buf = malloc (4*nscripts*sizeof (char));
- Lisp_Object scriptsChars = Vscript_representative_chars;
- unsigned char *tpos = buf;
-
- scripts = (Lisp_Object *) malloc (nscripts * sizeof (Lisp_Object));
-
- for (i =0; i<nscripts; i++)
- {
- Lisp_Object sChars = XCAR (scriptsChars);
- Lisp_Object chars = XCDR (sChars);
- unsigned int ch, c =0;
- scripts[i] = XCAR (sChars);
-
- while (CONSP (chars))
- {
- ch = XUINT (XCAR (chars));
- chars = XCDR (chars);
- CHAR_STRING_ADVANCE (ch, tpos);
- c++;
- }
- scriptsNchars[i] = c;
-
- scriptsChars = XCDR (scriptsChars);
- }
- *tpos = '\0';
-
- store = [[NSTextStorage alloc] init];
- layout = [[NSLayoutManager alloc] init];
- [store addLayoutManager: layout];
- [layout release];
-
- [store beginEditing];
- [[store mutableString] appendString:
- [NSString stringWithUTF8String: buf]];
- [store endEditing];
-
- free (buf);
- range = NSMakeRange (0, [store length]);
-
- attribs = [[NSMutableDictionary alloc] init];
- firstTime = NO;
- }
-
- /* set the fonts */
- [store beginEditing];
- [store removeAttribute: NSFontAttributeName range: range];
- [attribs setObject: font_info->nsfont forKey: NSFontAttributeName];
- [store addAttributes: attribs range: range];
- [store endEditing];
-
- /* read them out */
- {
- NSMutableDictionary *map =
- [NSMutableDictionary dictionaryWithCapacity: nscripts * 4];
- NSEnumerator *fonts;
- NSFont *cfont = nil, *tfont;
- NSNumber *n;
- int idx = 0;
- int max;
- for (i =0; i<nscripts; i++)
- {
- [map removeAllObjects];
- for (j =0; j<scriptsNchars[i]; j++)
- {
- cfont = [store attribute: NSFontAttributeName atIndex: idx++
- effectiveRange: NULL];
- n = [map objectForKey: cfont];
- if (n == nil)
- n = [NSNumber numberWithInt: 1];
- else
- n = [NSNumber numberWithInt: [n intValue] + 1];
- [map setObject: n forKey: cfont];
- }
-
- /* majority rules */
- max = 0;
- fonts = [map keyEnumerator];
- while (tfont = [fonts nextObject])
- {
- n = [map objectForKey: tfont];
- if ([n intValue] > max)
- {
- cfont = tfont;
- max = [n intValue];
- }
- }
-
- if (cfont != nil)
- {
- char *family = strdup([[cfont familyName] UTF8String]);
- Lisp_Object famAndReg;
-
- nsfont_escape_name (family);
- famAndReg = Fcons (build_string (family), regString);
-
- if (NSFONT_TRACE)
- fprintf (stderr, "%s fontset: use '%s' for script '%s'\n",
- font_info->name, family,
- SDATA (SYMBOL_NAME (scripts[i])));
-
- Fset_fontset_font (name, scripts[i], famAndReg, Qnil, Qnil);
- free (family);
- }
- else
- {
- fprintf (stderr, "%s fontset: none found for script '%s'\n",
- font_info->name, SDATA (SYMBOL_NAME (scripts[i])));
- }
- } /* for i = script */
- }
-}
-
-