-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopennurbs_font.cpp
658 lines (559 loc) · 14.9 KB
/
opennurbs_font.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/* $NoKeywords: $ */
/*
//
// Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
*/
#include "opennurbs.h"
ON_OBJECT_IMPLEMENT( ON_Font, ON_Object, "4F0F51FB-35D0-4865-9998-6D2C6A99721D" );
ON_Font::ON_Font()
{
Defaults();
}
ON_Font::~ON_Font()
{
}
bool ON_Font::CreateFontFromFaceName(
const wchar_t* facename,
bool bBold,
bool bItalic
)
{
PurgeUserData();
Defaults();
if ( 0 == facename || 0 == facename[0] )
facename = L"Arial";
bool rc = SetFontFaceName(facename);
HeightOfI();
m_font_name = facename;
if ( bBold )
{
SetBold(true);
m_font_name += "L Bold";
}
if ( bItalic )
{
SetItalic(true);
m_font_name += "L Italic";
}
return rc;
}
void ON_Font::Defaults()
{
m_font_name.Empty();
m_font_weight = ON_Font::normal_weight;
m_font_italic = false;
m_font_underlined = false;
m_linefeed_ratio = m_default_linefeed_ratio;
m_font_index = -1;
memset(&m_font_id,0,sizeof(m_font_id));
memset( &m_facename, 0, sizeof( m_facename));
m_I_height = 0;
#if defined(ON_OS_WINDOWS_GDI)
memset(&m_logfont,0,sizeof(m_logfont));
m_logfont.lfHeight = normal_font_height;
m_logfont.lfCharSet = default_charset;
#endif
SetFontFaceName(L"Arial");
}
//////////////////////////////////////////////////////////////////////
//
// ON_Object overrides
ON_BOOL32 ON_Font::IsValid( ON_TextLog* text_log ) const
{
return ( m_font_name.Length() > 0
&& m_font_index >= 0
&& m_facename[0] > 32
&& m_facename[64] == 0
);
}
void ON_Font::Dump( ON_TextLog& dump ) const
{
const wchar_t* name = FontName();
if ( !name )
name = L"";
dump.Print("font index = %d\n",m_font_index);
dump.Print("font name = \"%ls\"\n",name);
dump.Print("font face name = \"%ls\"\n",m_facename);
dump.Print("font weight = \"%d\"\n",m_font_weight);
dump.Print("font is italic = \"%d\"\n",m_font_italic);
dump.Print("font is underlined = \"%d\"\n",m_font_underlined);
dump.Print("font linefeed ratio = \"%g\"\n", m_linefeed_ratio);
}
ON_BOOL32 ON_Font::Write(
ON_BinaryArchive& file // serialize definition to binary archive
) const
{
bool rc = file.Write3dmChunkVersion(1,2);
while(rc)
{
rc = file.WriteInt(m_font_index);
if (!rc) break;
rc = file.WriteString(m_font_name);
if (!rc) break;
{
// 18 October 2002 Dale Lear:
// Lowell, wchar_t has different sizes on different OSs.
// When writing a wchar_t string, you should use one
// of the WriteString functions. This function must continue
// to use WriteShort(64,...) so old files will remain valid.
unsigned short sh[64];
memset(sh,0,sizeof(sh));
int i;
for ( i = 0; i < 64 && i < face_name_size-1; i++ )
sh[i] = m_facename[i];
rc = file.WriteShort(64, sh);
if (!rc) break;
}
// 1.1 additions
rc = file.WriteInt( m_font_weight);
if (!rc) break;
rc = file.WriteInt( m_font_italic);
if (!rc) break;
rc = file.WriteDouble( m_linefeed_ratio);
if (!rc) break;
// 1.2 addition
rc = file.WriteUuid( m_font_id);
if (!rc) break;
// 1.3 addition
// rc = file.WriteInt( m_font_underlined);
// if (!rc) break;
break;
}
return rc;
}
ON_BOOL32 ON_Font::Read(
ON_BinaryArchive& file // restore definition from binary archive
)
{
Defaults();
m_font_index = -1;
int major_version = 0;
int minor_version = 0;
bool rc = file.Read3dmChunkVersion(&major_version,&minor_version);
if ( rc && major_version == 1 )
{
int i;
for(;;)
{
rc = file.ReadInt( &m_font_index );
if (!rc) break;
rc = file.ReadString( m_font_name );
if (!rc) break;
{
// 18 October 2002 Dale Lear:
// Lowell, wchar_t has different sizes on different OSs.
// When writing a wchar_t string, you should use one
// of the WriteString functions. This function must continue
// to use ReadShort(64,...) so old files will remain valid.
unsigned short sh[64];
rc = file.ReadShort(64, sh);
if (!rc) break;
wchar_t facename[65];
for ( i = 0; i < 64; i++ )
{
facename[i] = sh[i];
}
facename[64] = 0;
SetFontFaceName(facename);
}
if( minor_version >= 1 )
{
rc = file.ReadInt( &i );
if (!rc) break;
SetFontWeight(i);
rc = file.ReadInt( &i);
if (!rc) break;
SetIsItalic(i?true:false);
rc = file.ReadDouble( &m_linefeed_ratio );
if (!rc) break;
if ( minor_version >= 2 )
{
rc = file.ReadUuid( m_font_id );
if (!rc) break;
}
//if ( minor_version >= 3 )
//{
// rc = file.ReadInt( &i);
// if (!rc) break;
// SetUnderlined(i?true:false);
//}
}
break;
}
}
else
{
ON_ERROR("ON_Font::Read - get newer version of opennurbs");
rc = false;
}
return rc;
}
// Ratio of linefeed to character height
const double ON_Font::m_default_linefeed_ratio = 1.6;
// This must be an 'I' or 'H', but we have not tested 'H'.
// There are problems with any other upper case character.
// In particular, the standard 'M' does not work.
const int ON_Font::m_metrics_char = 'I';
//////////////////////////////////////////////////////////////////////
//
// Interface
void ON_Font::SetFontName( const wchar_t* s )
{
m_font_name = s;
}
void ON_Font::SetFontName( const char* s )
{
m_font_name = s;
}
void ON_Font::GetFontName( ON_wString& s ) const
{
s = m_font_name;
}
const wchar_t* ON_Font::FontName() const
{
const wchar_t* s = m_font_name;
return s;
}
#if defined(ON_OS_WINDOWS_GDI)
static
int CALLBACK ON__IsSymbolFontFaceNameHelper( ENUMLOGFONTEX*, NEWTEXTMETRICEX*, DWORD, LPARAM)
{
// If the fontname in the logfont structure has
// a corresponding symbol font on the system,
// set the lfCharSet member to SYMBOL_CHARSET,
// otherwise DEFAULT_CHARSET
// The input logfont structure may be modified.
return 7;
}
#endif
bool ON_Font::IsSymbolFontFaceName( const wchar_t* s)
{
bool rc = false;
#if defined(ON_OS_WINDOWS_GDI)
if( s && s[0])
{
HDC hdc = ::GetDC( NULL);
if( hdc)
{
LOGFONT logfont;
memset( &logfont, 0, sizeof( logfont));
int i;
for ( i = 0; i < LF_FACESIZE && s[i]; i++ )
{
logfont.lfFaceName[i] = s[i];
}
logfont.lfCharSet = ON_Font::symbol_charset;
if( 7 == ::EnumFontFamiliesEx( hdc, &logfont, (FONTENUMPROC)ON__IsSymbolFontFaceNameHelper, 0, 0))
{
rc = true;
}
::ReleaseDC( NULL, hdc);
}
}
#endif
return rc;
}
bool ON_Font::SetFontFaceName( const wchar_t* s )
{
int i;
memset( &m_facename, 0, sizeof(m_facename) );
if ( s)
{
for ( i = 0; i < face_name_size-1 && s[i]; i++ )
{
m_facename[i] = s[i];
}
}
#if defined(ON_OS_WINDOWS_GDI)
memset( &m_logfont.lfFaceName, 0, sizeof(m_logfont.lfFaceName) );
#endif
m_I_height = 0;
UpdateImplementationSettings();
return( m_facename[0] ? true : false);
}
bool ON_Font::SetFontFaceName( const char* s )
{
ON_wString wstr(s);
const wchar_t* w = wstr;
return SetFontFaceName(w);
}
double ON_Font::AscentRatio() const
{
return ((double)normal_font_height) / ((double)HeightOfI());
}
void ON_Font::GetFontFaceName( ON_wString& s ) const
{
s = m_facename;
}
const wchar_t* ON_Font::FontFaceName() const
{
const wchar_t* s = m_facename;
return s;
}
void ON_Font::SetFontIndex(int i)
{
m_font_index = i;
}
int ON_Font::FontIndex() const
{
return m_font_index;
}
double ON_Font::LinefeedRatio() const
{
return m_linefeed_ratio;
}
void ON_Font::SetLinefeedRatio( double d)
{
m_linefeed_ratio = d;
}
int ON_Font::FontWeight() const
{
return m_font_weight;
}
void ON_Font::SetFontWeight( int w)
{
if ( w != m_font_weight )
{
if ( w < 0 )
w = 0;
m_font_weight = w;
m_I_height = 0;
UpdateImplementationSettings();
}
}
bool ON_Font::IsItalic() const
{
return m_font_italic;
}
void ON_Font::SetIsItalic( bool b)
{
SetItalic( b);
}
void ON_Font::SetItalic( bool b)
{
if ( m_font_italic != b )
{
m_font_italic = b?true:false;
m_I_height = 0;
UpdateImplementationSettings();
}
}
bool ON_Font::IsBold() const
{
return (FontWeight() >= bold_weight);
}
void ON_Font::SetBold( bool bBold )
{
SetFontWeight( bBold ? bold_weight : normal_weight);
}
bool ON_Font::IsUnderlined() const
{
return m_font_underlined;
}
void ON_Font::SetUnderlined( bool b)
{
if ( m_font_underlined != b )
{
m_font_underlined = b?true:false;
UpdateImplementationSettings();
}
}
void ON_Font::UpdateImplementationSettings()
{
#if defined(ON_OS_WINDOWS_GDI)
BYTE b;
LONG w;
size_t cap0, cap1, cap, i;
w = m_font_weight;
if ( w < 0 )
w = 0;
if ( w != m_logfont.lfWeight )
{
m_logfont.lfWeight = w;
m_I_height = 0;
}
b = m_font_italic ? 1 : 0;
if ( b != m_logfont.lfItalic )
{
m_logfont.lfItalic = b;
m_I_height = 0;
}
b = m_font_underlined ? 1 : 0;
if ( b != m_logfont.lfUnderline )
{
m_logfont.lfUnderline = b;
m_I_height = 0;
}
b = 0;
cap0 = sizeof(m_facename)/sizeof(m_facename[0]);
cap1 = sizeof(m_logfont.lfFaceName)/sizeof(m_logfont.lfFaceName[0]);
cap = cap0 < cap1 ? cap0 : cap1;
for ( i = 0; i < cap; i++ )
{
if ( m_logfont.lfFaceName[i] != m_facename[i] )
{
m_logfont.lfFaceName[i] = m_facename[i];
b = 1;
}
}
if ( b )
{
for ( i = cap; i < cap1; i++ )
m_logfont.lfFaceName[i] = 0;
m_logfont.lfCharSet = ON_Font::IsSymbolFontFaceName( m_logfont.lfFaceName )
? ((unsigned char)ON_Font::symbol_charset)
: ((unsigned char)ON_Font::default_charset);
m_I_height = 0;
}
#endif
}
/*
Returns:
Height of the 'I' character when the font is drawn
with m_logfont.lfHeight = 256.
*/
int ON_Font::HeightOfI() const
{
if ( m_I_height <= 0 )
{
// Default is height of Arial 'I'. If we are running
// on Windows, then we calculate the actual height of
// an 'I' in the font.
// The ..ON_Font::normal_font_height/256 is here
// so this code will continue to work correctly
// if somebody changes ON_Font::normal_font_height.
int I_height = (166*ON_Font::normal_font_height)/256;
#if defined(ON_OS_WINDOWS_GDI)
if ( m_logfont.lfFaceName[0] )
{
// Get the height of an 'I'
HDC hdc = ::GetDC( NULL);
if (hdc)
{
LOGFONT logfont = m_logfont;
logfont.lfHeight = normal_font_height;
HFONT font = ::CreateFontIndirect( &logfont);
if ( font )
{
wchar_t str[2];
str[0] = ON_Font::m_metrics_char;
str[1] = 0;
HFONT oldfont = (HFONT)::SelectObject( hdc, font);
::SetBkMode( hdc, TRANSPARENT);
::BeginPath(hdc);
::ExtTextOut( hdc, 0, 0, 0, NULL, str, 1, NULL);
::EndPath( hdc);
int numPoints = ::GetPath( hdc, NULL, NULL, 0);
if( numPoints > 2)
{
// Allocate room for the points & point types
LPPOINT pPoints = (LPPOINT)onmalloc( numPoints * sizeof(*pPoints) );
LPBYTE pTypes = (LPBYTE)onmalloc( numPoints * sizeof(*pTypes) );
if ( pTypes && pPoints)
{
// Get the points and types from the current path
numPoints = ::GetPath( hdc, pPoints, pTypes, numPoints);
if( numPoints > 2)
{
int ymin = pPoints[0].y;
int ymax = ymin;
int k;
for( k = 1; k < numPoints; k++)
{
if( pPoints[k].y < ymin)
ymin = pPoints[k].y;
else if( pPoints[k].y > ymax)
ymax = pPoints[k].y;
}
I_height = ymax - ymin + 1;
}
}
onfree( pPoints);
onfree( pTypes);
}
::SelectObject( hdc, oldfont);
::DeleteObject( font);
}
}
::ReleaseDC( NULL, hdc);
}
#endif
const_cast<ON_Font*>(this)->m_I_height = I_height;
}
return m_I_height;
}
int ON_Font::HeightOfLinefeed() const
{
return ( (int)( ceil(m_linefeed_ratio*HeightOfI()) ) );
}
#if defined(ON_OS_WINDOWS_GDI)
#pragma message( " --- OpenNURBS including Windows LOGFONT support in ON_Font" )
bool ON_Font::SetLogFont( const LOGFONT& logfont )
{
if ( &m_logfont != &logfont )
{
memcpy(&m_logfont,&logfont,sizeof(m_logfont));
}
// synch persistent fields
m_font_weight = m_logfont.lfWeight;
m_font_italic = (m_logfont.lfItalic?true:false);
m_font_underlined = (m_logfont.lfUnderline?true:false);
memset(&m_facename[0],0,sizeof(m_facename));
int i;
for ( i = 0; i < face_name_size && i < LF_FACESIZE; i++ )
{
m_facename[i] = (wchar_t)m_logfont.lfFaceName[i];
}
m_facename[face_name_size-1] = 0;
m_I_height = 0;
return true;
}
const LOGFONT& ON_Font::LogFont() const
{
return m_logfont;
}
ON_Font::ON_Font( const LOGFONT& logfont )
{
Defaults();
SetLogFont(logfont);
}
ON_Font& ON_Font::operator=( const LOGFONT& logfont )
{
if ( &m_logfont == &logfont )
{
LOGFONT lf = logfont;
SetLogFont(lf);
}
else
{
SetLogFont(logfont);
}
return *this;
}
bool ON_Font::CompareFontCharacteristics( ON_Font& other_font, bool bCompareName) const
{
if( bCompareName && m_font_name.CompareNoCase( other_font.m_font_name))
return false;
if( m_font_weight != other_font.m_font_weight)
return false;
if( m_font_italic != other_font.m_font_italic)
return false;
if( m_font_underlined != other_font.m_font_underlined)
return false;
if( m_linefeed_ratio != other_font.m_linefeed_ratio)
return false;
if( _wcsicmp( m_facename, other_font.m_facename))
return false;
return true;
}
#endif