~ubuntu-branches/ubuntu/precise/lilypond/precise

« back to all changes in this revision

Viewing changes to lily/font-metric.cc

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Bushnell, BSG
  • Date: 2006-12-19 10:18:12 UTC
  • mfrom: (3.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20061219101812-7awtjkp0i393wxty
Tags: 2.8.7-3
scripts/midi2ly.py: When setting DATADIR, find Lilypond python files
in the @TOPLEVEL_VERSION@ directory, not 'current'.  Patch thanks to
Chris Lamb (chris@chris-lamb.co.uk).  (Closes: #400550)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*   
2
 
  font-metric.cc --  implement Font_metric
3
 
  
 
1
/*
 
2
  font-metric.cc -- implement Font_metric
 
3
 
4
4
  source file of the GNU LilyPond music typesetter
5
 
  
6
 
  (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
 
 
8
 
    Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
9
 
 */
10
 
 
11
 
#include <math.h>
12
 
#include <ctype.h>
13
 
 
 
5
 
 
6
  (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
7
 
 
8
  Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
 
9
*/
 
10
 
 
11
#include "font-metric.hh"
 
12
 
 
13
#include <cmath>
 
14
#include <cctype>
 
15
using namespace std;
 
16
 
 
17
#include "dimensions.hh"
 
18
#include "modified-font-metric.hh"
 
19
#include "open-type-font.hh"
 
20
#include "stencil.hh"
14
21
#include "virtual-methods.hh"
15
22
#include "warn.hh"
16
 
#include "stencil.hh"
 
23
 
17
24
#include "ly-smobs.icc"
18
 
#include "font-metric.hh"
19
 
#include "string.hh"
20
 
 
21
 
Box
22
 
Font_metric::text_dimension (String text) const
23
 
{
24
 
  Interval ydims;
25
 
  Real w=0.0;
26
 
  
27
 
  for (int i = 0; i < text.length (); i++) 
 
25
 
 
26
Real
 
27
Font_metric::design_size () const
 
28
{
 
29
  return 1.0 * point_constant;
 
30
}
 
31
 
 
32
Stencil
 
33
Font_metric::find_by_name (string s) const
 
34
{
 
35
  replace_all (s, '-', 'M');
 
36
  int idx = name_to_index (s);
 
37
  Box b;
 
38
 
 
39
  SCM expr = SCM_EOL;
 
40
  if (idx >= 0)
28
41
    {
29
 
      
30
 
      switch (text[i]) 
31
 
        {
32
 
        case '\\':
33
 
  // accent marks use width of base letter
34
 
         if (i +1 < text.length ())
35
 
           {
36
 
             if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"' ||
37
 
                 text[i+1]=='^')
38
 
               {
39
 
                 i++;
40
 
                 break;
41
 
               }
42
 
             // for string width \\ is a \ and \_ is a _.
43
 
             if (text[i+1]=='\\' || text[i+1]=='_')        
44
 
               {
45
 
                 break;
46
 
               }
47
 
           }
48
 
          
49
 
          for (i++; (i < text.length ()) && !isspace (text[i]) 
50
 
                 && text[i]!='{' && text[i]!='}'; i++)
51
 
            ;
52
 
          // ugh.
53
 
          i--; // Compensate for the increment in the outer loop!
54
 
          break;
55
 
        case '{':  // Skip '{' and '}'
56
 
        case '}':
57
 
          break;
58
 
        
59
 
        default: 
60
 
          Box b = get_ascii_char ((unsigned char)text[i]);
61
 
          
62
 
          // Ugh, use the width of 'x' for unknown characters
63
 
          if (b[X_AXIS].length () == 0) 
64
 
            b = get_ascii_char ((unsigned char)'x');
65
 
          
66
 
          w += b[X_AXIS].length ();
67
 
          ydims.unite (b[Y_AXIS]);
68
 
          break;
69
 
        }
 
42
      expr = scm_list_3 (ly_symbol2scm ("named-glyph"),
 
43
                         self_scm (),
 
44
                         scm_makfrom0str (s.c_str ()));
 
45
      b = get_indexed_char (idx);
70
46
    }
71
 
  if (ydims.is_empty ())
72
 
    ydims = Interval (0,0);
73
 
 
74
 
  return Box (Interval (0, w), ydims);
75
 
}
76
 
 
77
 
 
78
 
 
79
 
Font_metric::~Font_metric ()
80
 
{
 
47
 
 
48
  Stencil q (b, expr);
 
49
  return q;
81
50
}
82
51
 
83
52
Font_metric::Font_metric ()
91
60
{
92
61
}
93
62
 
94
 
int
 
63
Font_metric::~Font_metric ()
 
64
{
 
65
}
 
66
 
 
67
size_t
95
68
Font_metric::count () const
96
69
{
97
70
  return 0;
98
71
}
99
72
 
100
 
Box 
101
 
Font_metric::get_ascii_char (int) const
 
73
Box
 
74
Font_metric::get_ascii_char (size_t) const
102
75
{
103
 
  return Box (Interval (0,0),Interval (0,0));
 
76
  return Box (Interval (0, 0), Interval (0, 0));
104
77
}
105
78
 
106
 
Box 
107
 
Font_metric::get_indexed_char (int k) const
 
79
Box
 
80
Font_metric::get_indexed_char (size_t k) const
108
81
{
109
82
  return get_ascii_char (k);
110
83
}
111
84
 
112
 
 
113
 
int
114
 
Font_metric::name_to_index (String) const
 
85
size_t
 
86
Font_metric::name_to_index (string) const
115
87
{
116
 
  return -1;
 
88
  return (size_t)-1;
117
89
}
118
90
 
119
91
Offset
120
 
Font_metric::get_indexed_wxwy (int )const
 
92
Font_metric::get_indexed_wxwy (size_t) const
121
93
{
122
 
  return Offset (0,0);
 
94
  return Offset (0, 0);
123
95
}
124
96
 
125
97
void
126
 
Font_metric::derived_mark ()const
 
98
Font_metric::derived_mark () const
127
99
{
128
 
  
129
100
}
130
101
 
131
102
SCM
132
103
Font_metric::mark_smob (SCM s)
133
104
{
134
 
  Font_metric * m = (Font_metric*) SCM_CELL_WORD_1 (s);
135
 
 
 
105
  Font_metric *m = (Font_metric *) SCM_CELL_WORD_1 (s);
136
106
  m->derived_mark ();
137
107
  return m->description_;
138
108
}
142
112
{
143
113
  Font_metric *m = unsmob_metrics (s);
144
114
  scm_puts ("#<", port);
145
 
  scm_puts (classname (m), port);
 
115
  scm_puts (m->class_name (), port);
146
116
  scm_puts (" ", port);
147
117
  scm_write (m->description_, port);
148
118
  scm_puts (">", port);
149
119
  return 1;
150
120
}
151
121
 
152
 
 
153
 
 
154
122
IMPLEMENT_SMOBS (Font_metric);
155
123
IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
156
124
IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
157
125
 
158
 
Stencil
159
 
Font_metric::find_by_name (String) const
160
 
{
161
 
  Stencil m ;
162
 
  return m;
163
 
}
164
 
 
165
 
LY_DEFINE (ly_find_glyph_by_name, "ly:find-glyph-by-name", 2 , 0, 0,
166
 
          (SCM font, SCM name),
167
 
          "This function retrieves a Stencil for the glyph named @var{name} "
168
 
           "in "
169
 
           "@var{font}.  "
170
 
           "The font must be available as an AFM file. If the glyph "
171
 
           "is not found, @code{#f} is returned. ")
172
 
{
173
 
  Font_metric *fm = unsmob_metrics (font);
174
 
  SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
175
 
  SCM_ASSERT_TYPE (gh_string_p (name), name, SCM_ARG2, __FUNCTION__, "string");
176
 
 
177
 
  Stencil m =  fm->find_by_name (ly_scm2string (name));
178
 
 
179
 
  /*
180
 
    TODO: make optional argument for default if not found.
181
 
   */
182
 
  return m.smobbed_copy ();
183
 
}
184
 
 
185
 
LY_DEFINE (ly_get_glyph, "ly:get-glyph", 2 , 0, 0,
186
 
          (SCM font, SCM index),
187
 
          "This function retrieves a Stencil for the glyph numbered @var{index} in "
188
 
"@var{font}. ")
189
 
{
190
 
  Font_metric *fm = unsmob_metrics (font);
191
 
  SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
192
 
  SCM_ASSERT_TYPE (gh_number_p (index), index, SCM_ARG2, __FUNCTION__, "number");
193
 
 
194
 
  return fm->get_ascii_char_stencil (gh_scm2int (index)).smobbed_copy ();
195
 
}
196
 
 
197
 
LY_DEFINE (ly_text_dimension,"ly:text-dimension", 2 , 0, 0,
198
 
          (SCM font, SCM text),
199
 
          "Given the font metric in @var{font} and the string @var{text}, compute "
200
 
"the extents of that text in that font. The return value is a pair of "
201
 
"number-pairs.")
202
 
{
203
 
  Box b;
204
 
  Font_metric *fm = unsmob_metrics (font);
205
 
  SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
206
 
  SCM_ASSERT_TYPE (gh_string_p (text), text, SCM_ARG2, __FUNCTION__, "string");
207
 
 
208
 
  b = fm->text_dimension (ly_scm2string (text));
209
 
  
210
 
  return gh_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[Y_AXIS]));
211
 
}
212
 
 
213
 
Stencil
214
 
Font_metric::get_ascii_char_stencil (int code)  const
215
 
{
216
 
  SCM at = scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code),
217
 
                       SCM_UNDEFINED);
218
 
  at = fontify_atom (this, at);
 
126
SCM
 
127
Font_metric::font_file_name () const
 
128
{
 
129
  return scm_car (description_);
 
130
}
 
131
 
 
132
string
 
133
Font_metric::font_name () const
 
134
{
 
135
  string s ("unknown");
 
136
  return s;
 
137
}
 
138
 
 
139
size_t
 
140
Font_metric::index_to_ascii (size_t i) const
 
141
{
 
142
  return i;
 
143
}
 
144
 
 
145
size_t
 
146
Font_metric::index_to_charcode (size_t i) const
 
147
{
 
148
  return index_to_ascii (i);
 
149
}
 
150
 
 
151
Stencil
 
152
Font_metric::get_ascii_char_stencil (size_t code) const
 
153
{
 
154
  SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (),
 
155
                       scm_from_unsigned (code));
219
156
  Box b = get_ascii_char (code);
220
157
  return Stencil (b, at);
221
158
}
222
159
 
223
160
Stencil
224
 
Font_metric::get_indexed_char_stencil (int code)  const
 
161
Font_metric::get_indexed_char_stencil (size_t code) const
225
162
{
226
 
  SCM at = scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code),
227
 
                       SCM_UNDEFINED);
228
 
  at = fontify_atom (this, at);
 
163
  size_t idx = index_to_ascii (code);
 
164
  SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (),
 
165
                       scm_from_unsigned (idx));
229
166
  Box b = get_indexed_char (code);
230
167
  return Stencil (b, at);
231
168
}
 
169
 
 
170
Offset
 
171
Font_metric::attachment_point (string) const
 
172
{
 
173
  return Offset (0, 0);
 
174
}
 
175
 
 
176
SCM
 
177
Font_metric::sub_fonts () const
 
178
{
 
179
  return SCM_EOL;
 
180
}
 
181
 
 
182
Stencil
 
183
Font_metric::text_stencil (string str) const
 
184
{
 
185
  SCM lst = scm_list_3 (ly_symbol2scm ("text"),
 
186
                        this->self_scm (),
 
187
                        scm_makfrom0str (str.c_str ()));
 
188
 
 
189
  Box b = text_dimension (str);
 
190
  return Stencil (b, lst);
 
191
}
 
192
 
 
193
Box
 
194
Font_metric::text_dimension (string) const
 
195
{
 
196
  return Box (Interval (0, 0), Interval (0, 0));
 
197
}
 
198