summaryrefslogtreecommitdiff
path: root/src/Aspect/Aspect_FontStyle.cxx
blob: 5586ca5a30d3a901446138c1997f04aecc4ff5c9 (plain)
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
//GG_040298	eliminer les variables statiques globales.

#define PRO5676 //GG_020197
//              Donner la possibillite d'exprimer la taille
//		par rapport a la hauteur totale du caractere ou par
//		rapport a la hauteur "ascent" situee au dessus de la 
//		ligne de base.
//		(CapsHeight flag)
//

//GG_160498 etude G1343 gerer les polices transformables pour 
//	    l'utilisation de MFT_FontManager.

// =====================================================================
// DCB_120598 study S3553. Platform specific code has been deleted
// ("#ifdef WNT") to be able to use on WNT the same font descriptor
// string like on UNIX.
// =====================================================================

// File		Aspect_FontStyle.cxx
// Created	Septembre 1993
// Author 	GG	

//-Copyright	MatraDatavision 1991,1992

//-Version	

//-Design	Declaration des variables specifiques aux Fontures de textes

//-Warning	Un style est definie, soit par son type predefini TOF_...
//		soit par sa description (Font string de type ADOBE) 

//-References	

//-Language	C++ 2.0

//-Declarations
#define MAXFIELDS 14
#define PITCHSIZE (0.00028 METER);

// for the class
#include <Aspect_FontStyle.ixx>
#include <Aspect_Units.hxx>
#include <stdio.h>

//-Aliases

//-Global data definitions

//	MyFontType	:	TypeOfFont from Aspect;
//	MyStyle		:	AsciiString from  TCollection
//	MyFontName	:	AsciiCString from TCollection 
//	MyFontSize	:	Real from Standard
//	MyFontSlant	:	Real from Standard
//	MyCapsHeight	:	Boolean from Standard

//-Constructors

//-Destructors

//-Methods, in order

Aspect_FontStyle::Aspect_FontStyle () {

	SetPredefinedStyle(Aspect_TOF_DEFAULT,0.003 METER,0.,Standard_False) ;
}

Aspect_FontStyle::Aspect_FontStyle (const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {

	SetPredefinedStyle(Type,Size,Slant,CapsHeight) ;
}

Aspect_FontStyle::Aspect_FontStyle (const Standard_CString style, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {

	MyFontType = Aspect_TOF_USERDEFINED ;
	MyFontSize = Size;
	MyFontSlant = Slant;
	MyCapsHeight = CapsHeight;
	MyStyle = style;
	MyFontName = Normalize(style,MyFontSize) ;
}

Aspect_FontStyle::Aspect_FontStyle (const Standard_CString style) {

	MyFontType = Aspect_TOF_USERDEFINED ;
	MyFontSize = 0. ;
	MyFontSlant = 0. ;
	MyCapsHeight = Standard_False;
	MyStyle = style;
	MyFontName = Normalize(style,MyFontSize) ;
}

Aspect_FontStyle& Aspect_FontStyle::Assign (const Aspect_FontStyle& Other) {

	MyFontType = Other.Style() ;
	MyFontSize = Other.Size() ;
	MyFontSlant = Other.Slant() ;
	MyStyle = Other.Value();
	MyFontName = Other.FullName() ;
	MyCapsHeight = Other.CapsHeight() ;

	return (*this);
}

void Aspect_FontStyle::SetValues ( const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {

	SetPredefinedStyle (Type,Size,Slant,CapsHeight);
}

void Aspect_FontStyle::SetValues ( const Standard_CString style, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
	MyFontType = Aspect_TOF_USERDEFINED ;
	MyFontSize = Size;
	MyFontSlant = Slant;
	MyCapsHeight = CapsHeight;
	MyStyle = style;
	MyFontName = Normalize(style,MyFontSize) ;
}

void Aspect_FontStyle::SetValues ( const Standard_CString style) {
	MyFontType = Aspect_TOF_USERDEFINED ;
	MyFontSize = 0. ;
	MyFontSlant = 0. ;
	MyCapsHeight = Standard_False;
	MyStyle = style;
	MyFontName = Normalize(style,MyFontSize) ;
}

// ============================================================================
//        ---Purpose: Sets the family of the font.
// ============================================================================

void Aspect_FontStyle::SetFamily(const Standard_CString aName) {
    MyFontName = SetField(MyFontName,aName,2);
}

// ============================================================================
//        ---Purpose: Sets the weight of the font.
// ============================================================================

void Aspect_FontStyle::SetWeight(const Standard_CString aName) {
    MyFontName = SetField(MyFontName,aName,3);
}

// ============================================================================
//        ---Purpose: Sets the Registry of the font.
// ============================================================================

void Aspect_FontStyle::SetRegistry(const Standard_CString aName) {
    MyFontName = SetField(MyFontName,aName,13);
}

// ============================================================================
//        ---Purpose: Sets the Encoding of the font.
// ============================================================================

void Aspect_FontStyle::SetEncoding(const Standard_CString aName) {
    MyFontName = SetField(MyFontName,aName,14);
}

Aspect_TypeOfFont Aspect_FontStyle::Style () const {

	return (MyFontType);
}

Standard_Integer Aspect_FontStyle::Length () const {

	return MyStyle.Length();
}

Standard_CString Aspect_FontStyle::Value () const
{
  return MyStyle.ToCString();
}

Quantity_Length Aspect_FontStyle::Size () const {
	return (MyFontSize);
}

Quantity_PlaneAngle Aspect_FontStyle::Slant () const {

	return (MyFontSlant);
}

Standard_Boolean Aspect_FontStyle::CapsHeight () const {

	return (MyCapsHeight);
}

// ============================================================================
//        ---Purpose: Returns the alias font name.
//        --          this is a shorter font name which identify the
//        --          main characteristics of the fonts.
//        ---Example: "helvetica-bold"
// ============================================================================

Standard_CString Aspect_FontStyle::AliasName() const
{
static TCollection_AsciiString aliasname;
    aliasname.Clear();
    TCollection_AsciiString family(Family());
    if( family.Length() > 0 && family != "*" ) {
      TCollection_AsciiString FAMILY = family; FAMILY.UpperCase();
      family.SetValue(1,FAMILY.Value(1));
      aliasname = family; 
    }
    TCollection_AsciiString weight(Weight()); 
    if( weight.Length() > 0 && weight != "*" ) {
      TCollection_AsciiString WEIGHT = weight; WEIGHT.UpperCase();
      if( WEIGHT == "NORMAL" ) {
	weight.Clear();
      } else {
        weight.SetValue(1,WEIGHT.Value(1));
      }
    } else weight.Clear();
    TCollection_AsciiString sslant(SSlant());
    if( sslant.Length() > 0 && sslant != "*" ) {
      if( sslant == "r" ) {
	sslant.Clear();
      } else if( sslant == "i" ) {
        weight += "Italic";
	sslant.Clear();
      } else if( sslant == "o" ) {
        weight += "Oblique";
	sslant.Clear();
      }
    } else sslant.Clear();
    if( weight.Length() > 0 ) {
      aliasname += '-'; aliasname += weight;
    }
    if( sslant.Length() > 0 ) {
      aliasname += '-'; aliasname += sslant;
    }
    TCollection_AsciiString swidth(SWidth());
    if( swidth.Length() > 0 && swidth != "*" ) {
      TCollection_AsciiString SWIDTH = swidth; SWIDTH.UpperCase();
      if( SWIDTH == "NORMAL" ) {
      } else {
	if( !weight.Length() ) aliasname += '-';
        aliasname += '-'; aliasname += swidth;
      }
    }
    return aliasname.ToCString();
}
// ============================================================================
//        ---Purpose: Returns the full normalized font name
// ============================================================================

Standard_CString Aspect_FontStyle::FullName () const {

	return (MyFontName.ToCString());
}

// ============================================================================
//        ---Purpose: Returns the foundry of the font.
//        ---Example: "adobe"
// ============================================================================

Standard_CString Aspect_FontStyle::Foundry() const
{
    return Field(MyFontName,1);
}

// ============================================================================
//        ---Purpose: Returns the family of the font.
//        ---Example: "helvetica"
// ============================================================================

Standard_CString Aspect_FontStyle::Family() const
{
    return Field(MyFontName,2);
}

// ============================================================================
//        ---Purpose: Returns the weight of the font.
//        ---Example: "bold"
// ============================================================================

Standard_CString Aspect_FontStyle::Weight() const
{
    return Field(MyFontName,3);
}

// ============================================================================
//        ---Purpose: Returns the char set registry of the font.
//        ---Example: "iso8859"
// ============================================================================

Standard_CString Aspect_FontStyle::Registry() const
{
    return Field(MyFontName,13);
}

// ============================================================================
//        ---Purpose: Returns the char set encoding of the font.
//        ---Example: "1"
// ============================================================================

Standard_CString Aspect_FontStyle::Encoding() const
{
    return Field(MyFontName,14);
}

// ============================================================================
//        ---Purpose: Returns the slant of the font.
//        ---Example: "i"
// ============================================================================

Standard_CString Aspect_FontStyle::SSlant() const
{
    return Field(MyFontName,4);
}

// ============================================================================
//        ---Purpose: Returns the width name of the font.
//        ---Example: "normal"
// ============================================================================

Standard_CString Aspect_FontStyle::SWidth() const
{
    return Field(MyFontName,5);
}

// ============================================================================
//        ---Purpose: Returns the style name of the font.
//        ---Example: "serif"
// ============================================================================

Standard_CString Aspect_FontStyle::SStyle() const
{
    return Field(MyFontName,6);
}

// ============================================================================
//        ---Purpose: Returns the pixel size of the font.
//        ---Example: "14"
// ============================================================================

Standard_CString Aspect_FontStyle::SPixelSize() const
{
    return Field(MyFontName,7);
}

// ============================================================================
//        ---Purpose: Returns the point size of the font.
//        ---Example: "140"
// ============================================================================

Standard_CString Aspect_FontStyle::SPointSize() const
{
    return Field(MyFontName,8);
}
// ============================================================================
//        ---Purpose: Returns the resolution X of the font.
//        ---Example: "75"
// ============================================================================

Standard_CString Aspect_FontStyle::SResolutionX() const
{
    return Field(MyFontName,9);
}

// ============================================================================
//        ---Purpose: Returns the resolution Y of the font.
//        ---Example: "75"
// ============================================================================

Standard_CString Aspect_FontStyle::SResolutionY() const
{
    return Field(MyFontName,10);
}

// ============================================================================
//        ---Purpose: Returns the spacing of the font.
//        ---Example: "p"
// ============================================================================

Standard_CString Aspect_FontStyle::SSpacing() const
{
    return Field(MyFontName,11);
}

// ============================================================================
//        ---Purpose: Returns the average width of the font.
//        ---Example: "90"
// ============================================================================

Standard_CString Aspect_FontStyle::SAverageWidth() const
{
    return Field(MyFontName,12);
}

// ============================================================================
//        ---Purpose: Dumps the font attributes.
// ============================================================================

void Aspect_FontStyle::Dump() const
{
    cout << ".Aspect_FontStyle::Dump() :" << endl;
    cout << "-----------------" << endl << endl;
    cout << "  style name is : '" << this->Value() << "'" << endl;
    cout << "  Normalized font name is : '" << this->FullName() << "'" << endl;
    cout << "  Alias font name is : '" << this->AliasName() << "'" << endl;
    cout << "    Foundry is : '" << this->Foundry() << "'" << endl;
    cout << "    Family is : '" << this->Family() << "'" << endl;
    cout << "    Weight is : '" << this->Weight() << "'" << endl;
    cout << "    Slant is : '" << this->SSlant() << "'" << endl;
    cout << "    Width is : '" << this->SWidth() << "'" << endl;
    cout << "    Style is : '" << this->SStyle() << "'" << endl;
    cout << "    PixelSize is : '" << this->SPixelSize() << "'" << endl;
    cout << "    PointSize is : '" << this->SPointSize() << "'" << endl;
    cout << "    ResolutionX is : '" << this->SResolutionX() << "'" << endl;
    cout << "    ResolutionY is : '" << this->SResolutionY() << "'" << endl;
    cout << "    Spacing is : '" << this->SSpacing() << "'" << endl;
    cout << "    AverageWidth is : '" << this->SAverageWidth() << "'" << endl;
    cout << "    Registry is : '" << this->Registry() << "'" << endl;
    cout << "    Encoding is : '" << this->Encoding() << "'" << endl << endl;
}

void Aspect_FontStyle::SetPredefinedStyle( const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {

	if( Size <= 0. )
		Aspect_FontStyleDefinitionError::Raise ("Bad font Size");

	MyFontType = Type ;
	MyFontSize = Size;
	MyFontSlant = Slant ;
	MyCapsHeight = CapsHeight;
	Standard_CString pstyle = "" ;
	switch ( Type ) {
	    case Aspect_TOF_USERDEFINED :
		Aspect_FontStyleDefinitionError::Raise ("Bad Font Type Style");
	    case Aspect_TOF_DEFAULT :
		pstyle = "Defaultfont" ;
		break ;
	    case Aspect_TOF_COURIER :
		pstyle = "Courier" ;
		break ;
	    case Aspect_TOF_HELVETICA :
		pstyle = "Helvetica" ;
		break ;
	    case Aspect_TOF_TIMES :
		pstyle = "Times" ;
		break ;
	}
	MyStyle = pstyle;
	MyFontName = Normalize(pstyle,MyFontSize);
}

Standard_Boolean Aspect_FontStyle::IsEqual(const Aspect_FontStyle& Other) const
{
  return (
	  (MyFontType == Other.MyFontType) &&
	  (MyFontName.Length() == Other.MyFontName.Length()) &&
	  (MyFontName == Other.MyFontName) &&
	  (MyFontSize == Other.MyFontSize) &&
	  (MyFontSlant == Other.MyFontSlant) &&
	  (MyCapsHeight == Other.MyCapsHeight)
	 );

}

Standard_Boolean Aspect_FontStyle::IsNotEqual(const Aspect_FontStyle& Other) const
{
  return !IsEqual(Other);
}

// ============================================================================
//        ---Purpose: Returns a normalized XLFD descriptor from the font name
//        --         <aFontName>
// ============================================================================

Standard_CString Aspect_FontStyle::Normalize(const Standard_CString aFontName,Standard_Real& aSize)
{
    Aspect_FontStyleDefinitionError_Raise_if( 
				!aFontName ,"NULL font descriptor");

static TCollection_AsciiString afontstring;
static TCollection_AsciiString astar("*");
static TCollection_AsciiString azero("0");
TCollection_AsciiString afield,afontname(aFontName);
Standard_CString pfontname = NULL;
Standard_Boolean xlfd = Standard_False;
Standard_Integer i,len = afontname.Length();

    afontstring = "";

    if( len <= 0 ) return pfontname;

    if( afontname.Value(1) != '-' ) afontname.Prepend("-adobe-");
    for( i=1 ; i<=MAXFIELDS ; i++ ) {
      Standard_CString pfield = Field(afontname,i);
      switch (i) {
        case 1:
	  if( pfield ) {
            afield = pfield; afield.LowerCase();
	    {afontstring += '-'; afontstring += afield;}
	  } else {
            afontstring += "-adobe";
	  }
	  break;
        case 7:
	  if( pfield ) {
            afield = pfield; afield.LowerCase();
	    if( afield == astar ) {
	      if( aSize > 0. ) afontstring += "-*";
	      else {afontstring += "-0"; xlfd = Standard_True;}
	    } else if( afield == azero ) { afontstring += "-0"; xlfd = Standard_True;}
	    else {afontstring += '-'; afontstring += afield;}
	  } else {
            if( aSize > 0. ) afontstring += "-*";
	    else { afontstring += "-0"; xlfd = Standard_True;}
	  }
	  break;
        case 8:
        case 9:
        case 10:
	  if( xlfd ) {
	    afontstring += "-0";
	  } else if( pfield ) {
            afield = pfield; afield.LowerCase();
	    afontstring += '-'; afontstring += afield;
	  } else {
            afontstring += "-*";
	  }
	  break;
        case 13:
	  if( pfield ) {
            afield = pfield; afield.LowerCase();
	    afontstring += '-'; afontstring += afield;
	  } else {
            afontstring += "-*";
	  }
	  break;
	default:
	  if( pfield ) {
            afield = pfield;
	    afontstring += '-'; afontstring += afield;
          } else {
            afontstring += "-*";
          }
      }
    }
    pfontname = afontstring.ToCString();
    if( aSize == 0. ) {
      Standard_CString psize = Field(pfontname,7);
      Standard_Integer isize;
      if( sscanf(psize,"%d",&isize) == 1 ) {
        aSize = isize*PITCHSIZE;
      } else {
        aSize = 2. MILLIMETER;
      }
    }

    return pfontname;
}

// ============================================================================
//        ---Purpose: Returns the field at position <aRank>
//        --          from the font name <aFontName>.
// ============================================================================

Standard_CString Aspect_FontStyle::Field(const TCollection_AsciiString& aFontName,const Standard_Integer aRank)
{
static TCollection_AsciiString afieldstring;
Standard_Integer length = aFontName.Length();
Standard_CString pfield = NULL;

    if( length > 0 && aRank >= 1 && aRank <= MAXFIELDS ) {
      Standard_Integer start = 1,end = 0;
      start = aFontName.Location(aRank,'-',1,length);
      end = aFontName.Location(aRank+1,'-',1,length);
      afieldstring = "";
      if( start > 0 && end > 0 ) {
        start++ ; 
	end--;
        if( end >= start ) afieldstring = aFontName.SubString(start,end);
        else afieldstring = "";
      } else if( start > 0 ) {
        start++ ; 
	end = length;
        if( end >= start ) afieldstring = aFontName.SubString(start,end);
        else afieldstring = "";
      } else {
        return NULL;
      }
    }
    pfield = afieldstring.ToCString();

    return pfield;
}

// ============================================================================
//        ---Purpose: Sets the field at position <aRank>
//	  --	      of the font <aFontName>
//        --          from the field <aField>.
// ============================================================================

Standard_CString Aspect_FontStyle::SetField(const TCollection_AsciiString& aFontName,const Standard_CString aField,const Standard_Integer aRank)
{
static TCollection_AsciiString afontstring;
Standard_Integer length = aFontName.Length();
Standard_CString pfontname = NULL;

    if( length > 0 && aRank >= 1 && aRank <= MAXFIELDS ) {
      Standard_Integer start = 1,end = 0;
      start = aFontName.Location(aRank,'-',1,length);
      end = aFontName.Location(aRank+1,'-',1,length);
      TCollection_AsciiString lowername(""),uppername(""); 
      if( start > 0 && end > 0 ) {
        if( end > start ) {
	  if( start > 1 ) lowername = aFontName.SubString(1,start);
	  uppername = aFontName.SubString(end,length);
	}
      } else if( start > 1 ) {
	lowername = aFontName.SubString(1,start);
      } else {
        return NULL;
      }
      afontstring = lowername;
      afontstring += aField; afontstring += uppername;
      pfontname = afontstring.ToCString();
    }

    return pfontname;
}