~ubuntu-branches/ubuntu/lucid/python-scipy/lucid

« back to all changes in this revision

Viewing changes to Lib/sandbox/xplt/src/play/mac/font.m

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * font.m
 
3
 * Font handling routines for Mac OS X.
 
4
 *
 
5
 * Copyright (c) 1999.  See accompanying LEGAL file for details.
 
6
 */
 
7
 
 
8
#include "playm.h"
 
9
 
 
10
static char* families[5][4] =
 
11
  {{"CourierNewPSMT",
 
12
    "CourierNewPS-BoldMT",
 
13
    "CourierNewPS-ItalicMT",
 
14
    "CourierNewPS-BoldItalicMT"},
 
15
   {"Times-Roman",
 
16
    "Times-Bold",
 
17
    "Times-Italic",
 
18
    "Times-BoldItalic"},
 
19
   {"ArialMT",
 
20
    "Arial-BoldMT",
 
21
    "Arial-ItalicMT",
 
22
    "Arial-BoldItalicMT"},
 
23
   {"Symbol",
 
24
    "Symbol",
 
25
    "Symbol",
 
26
    "Symbol"},
 
27
   {"CenturySchoolbook",
 
28
    "CenturySchoolbook-Bold",
 
29
    "CenturySchoolbook-Italic",
 
30
    "CenturySchoolbook-BoldItalic"}};
 
31
 
 
32
static void
 
33
m_font(p_scr* s, int fontid, CGFontRef* fontref, ATSFontMetrics** metric);
 
34
    
 
35
void
 
36
p_font(p_win *w, int fontid, int pixsize, int orient)
 
37
{ p_scr* s = w->s;
 
38
  CGContextRef cr = w->cr;
 
39
  if (p_signalling) {
 
40
    p_abort();
 
41
    return;
 
42
  }
 
43
  if (fontid != w->fontid)
 
44
  { m_font(s, fontid, &(w->fontref), NULL);
 
45
    w->fontid = fontid;
 
46
  }
 
47
  w->orient = orient;
 
48
  CGContextSetFont(cr, w->fontref);
 
49
  CGContextSetFontSize(cr, (float) pixsize);
 
50
}
 
51
 
 
52
int
 
53
p_txheight(p_scr *s, int fontid, int pixsize, int *baseline)
 
54
{ ATSFontMetrics* metric;
 
55
  int height = pixsize;
 
56
  int base = pixsize;
 
57
  if (!p_signalling) {
 
58
    float ascent, descent;
 
59
    m_font(s, fontid, NULL, &metric);
 
60
    ascent  = pixsize * (metric->ascent);
 
61
    descent = pixsize * (metric->descent);
 
62
    base = (int) ascent;
 
63
    height = (int) (ascent - descent);
 
64
  } else {
 
65
    p_abort();
 
66
  }
 
67
  if (baseline) *baseline = base;
 
68
  return height;
 
69
}
 
70
 
 
71
int
 
72
p_txwidth(p_scr *s, const char *text, int n, int fontid, int pixsize)
 
73
{ int width = pixsize;
 
74
  if (!p_signalling) {
 
75
    int i;
 
76
    CGPoint point;
 
77
    CGContextRef cr = s->scratch;
 
78
    CGFontRef font;
 
79
    m_font(s, fontid, &font, NULL);
 
80
    CGContextSetFont(cr, font);
 
81
    CGContextSetFontSize(cr, (float) pixsize);
 
82
    CGContextSetTextPosition(cr, 0.0, 0.0);
 
83
    for (i = 0; i < n; i++)
 
84
    { CGGlyph c = (unsigned short)(text[i]) - 29;
 
85
      CGContextShowGlyphs(cr, &c, 1);
 
86
    }
 
87
    point = CGContextGetTextPosition(cr);
 
88
    width = (int) (point.x);
 
89
  } else {
 
90
    p_abort();
 
91
  }
 
92
  return width;
 
93
}
 
94
 
 
95
static void 
 
96
m_font(p_scr* s, int fontid, CGFontRef* fontref, ATSFontMetrics** metric)
 
97
{ int index;
 
98
  int face = ((unsigned int)(fontid&0x1c))>>2;
 
99
  if (face >= 5) {
 
100
    if(fontref) *fontref = s->font;
 
101
    if(metric) *metric = &(s->metric);
 
102
    return;
 
103
  }
 
104
  else index = (fontid&P_ITALIC) + (fontid&P_BOLD);
 
105
  if (!(s->fonts[face][index]))
 
106
  { CFStringRef family = CFStringCreateWithCString(kCFAllocatorDefault,
 
107
                                                   families[face][index],
 
108
                                                   kCFStringEncodingMacRoman);
 
109
    ATSFontRef atsfont = ATSFontFindFromPostScriptName(family,
 
110
                                                       kATSOptionFlagsDefault);
 
111
    /* Some fonts are not found, although the family name is correct and
 
112
     * the font can be found with [NSFont fontWithName: size:] */
 
113
    ATSFontMetrics* newmetric = &(s->metrics[face][index]);
 
114
    ATSFontGetHorizontalMetrics(atsfont, kATSOptionFlagsDefault, newmetric);
 
115
    s->fonts[face][index] = CGFontCreateWithPlatformFont((void*)&atsfont);
 
116
    CFRelease(family);
 
117
    /* atsfont is an unsigned integer (UInt32); I assume it should not be
 
118
     * released. */
 
119
  }
 
120
  if(fontref) *fontref = s->fonts[face][index];
 
121
  if(metric) *metric = &(s->metrics[face][index]);
 
122
}