0023024: Update headers of OCCT files
[occt.git] / src / Aspect / Aspect_FontStyle.cxx
1 // Created on: 1993-09-14
2 // Created by: GG       
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 //GG_040298     eliminer les variables statiques globales.
22
23 #define PRO5676 //GG_020197
24 //              Donner la possibillite d'exprimer la taille
25 //              par rapport a la hauteur totale du caractere ou par
26 //              rapport a la hauteur "ascent" situee au dessus de la 
27 //              ligne de base.
28 //              (CapsHeight flag)
29
30 //GG_160498 etude G1343 gerer les polices transformables pour 
31 //          l'utilisation de MFT_FontManager.
32
33 // =====================================================================
34 // DCB_120598 study S3553. Platform specific code has been deleted
35 // ("#ifdef WNT") to be able to use on WNT the same font descriptor
36 // string like on UNIX.
37 // =====================================================================
38
39
40
41 //-Version      
42
43 //-Design       Declaration des variables specifiques aux Fontures de textes
44
45 //-Warning      Un style est definie, soit par son type predefini TOF_...
46 //              soit par sa description (Font string de type ADOBE) 
47
48 //-References   
49
50 //-Language     C++ 2.0
51
52 //-Declarations
53 #define MAXFIELDS 14
54 #define PITCHSIZE (0.00028 METER);
55
56 // for the class
57 #include <Aspect_FontStyle.ixx>
58 #include <Aspect_Units.hxx>
59 #include <stdio.h>
60
61 //-Aliases
62
63 //-Global data definitions
64
65 //      MyFontType      :       TypeOfFont from Aspect;
66 //      MyStyle         :       AsciiString from  TCollection
67 //      MyFontName      :       AsciiCString from TCollection 
68 //      MyFontSize      :       Real from Standard
69 //      MyFontSlant     :       Real from Standard
70 //      MyCapsHeight    :       Boolean from Standard
71
72 //-Constructors
73
74 //-Destructors
75
76 //-Methods, in order
77
78 Aspect_FontStyle::Aspect_FontStyle () {
79
80         SetPredefinedStyle(Aspect_TOF_DEFAULT,0.003 METER,0.,Standard_False) ;
81 }
82
83 Aspect_FontStyle::Aspect_FontStyle (const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
84
85         SetPredefinedStyle(Type,Size,Slant,CapsHeight) ;
86 }
87
88 Aspect_FontStyle::Aspect_FontStyle (const Standard_CString style, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
89
90         MyFontType = Aspect_TOF_USERDEFINED ;
91         MyFontSize = Size;
92         MyFontSlant = Slant;
93         MyCapsHeight = CapsHeight;
94         MyStyle = style;
95         MyFontName = Normalize(style,MyFontSize) ;
96 }
97
98 Aspect_FontStyle::Aspect_FontStyle (const Standard_CString style) {
99
100         MyFontType = Aspect_TOF_USERDEFINED ;
101         MyFontSize = 0. ;
102         MyFontSlant = 0. ;
103         MyCapsHeight = Standard_False;
104         MyStyle = style;
105         MyFontName = Normalize(style,MyFontSize) ;
106 }
107
108 Aspect_FontStyle& Aspect_FontStyle::Assign (const Aspect_FontStyle& Other) {
109
110         MyFontType = Other.Style() ;
111         MyFontSize = Other.Size() ;
112         MyFontSlant = Other.Slant() ;
113         MyStyle = Other.Value();
114         MyFontName = Other.FullName() ;
115         MyCapsHeight = Other.CapsHeight() ;
116
117         return (*this);
118 }
119
120 void Aspect_FontStyle::SetValues ( const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
121
122         SetPredefinedStyle (Type,Size,Slant,CapsHeight);
123 }
124
125 void Aspect_FontStyle::SetValues ( const Standard_CString style, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
126         MyFontType = Aspect_TOF_USERDEFINED ;
127         MyFontSize = Size;
128         MyFontSlant = Slant;
129         MyCapsHeight = CapsHeight;
130         MyStyle = style;
131         MyFontName = Normalize(style,MyFontSize) ;
132 }
133
134 void Aspect_FontStyle::SetValues ( const Standard_CString style) {
135         MyFontType = Aspect_TOF_USERDEFINED ;
136         MyFontSize = 0. ;
137         MyFontSlant = 0. ;
138         MyCapsHeight = Standard_False;
139         MyStyle = style;
140         MyFontName = Normalize(style,MyFontSize) ;
141 }
142
143 // ============================================================================
144 //        ---Purpose: Sets the family of the font.
145 // ============================================================================
146
147 void Aspect_FontStyle::SetFamily(const Standard_CString aName) {
148     MyFontName = SetField(MyFontName,aName,2);
149 }
150
151 // ============================================================================
152 //        ---Purpose: Sets the weight of the font.
153 // ============================================================================
154
155 void Aspect_FontStyle::SetWeight(const Standard_CString aName) {
156     MyFontName = SetField(MyFontName,aName,3);
157 }
158
159 // ============================================================================
160 //        ---Purpose: Sets the Registry of the font.
161 // ============================================================================
162
163 void Aspect_FontStyle::SetRegistry(const Standard_CString aName) {
164     MyFontName = SetField(MyFontName,aName,13);
165 }
166
167 // ============================================================================
168 //        ---Purpose: Sets the Encoding of the font.
169 // ============================================================================
170
171 void Aspect_FontStyle::SetEncoding(const Standard_CString aName) {
172     MyFontName = SetField(MyFontName,aName,14);
173 }
174
175 Aspect_TypeOfFont Aspect_FontStyle::Style () const {
176
177         return (MyFontType);
178 }
179
180 Standard_Integer Aspect_FontStyle::Length () const {
181
182         return MyStyle.Length();
183 }
184
185 Standard_CString Aspect_FontStyle::Value () const
186 {
187   return MyStyle.ToCString();
188 }
189
190 Quantity_Length Aspect_FontStyle::Size () const {
191         return (MyFontSize);
192 }
193
194 Quantity_PlaneAngle Aspect_FontStyle::Slant () const {
195
196         return (MyFontSlant);
197 }
198
199 Standard_Boolean Aspect_FontStyle::CapsHeight () const {
200
201         return (MyCapsHeight);
202 }
203
204 // ============================================================================
205 //        ---Purpose: Returns the alias font name.
206 //        --          this is a shorter font name which identify the
207 //        --          main characteristics of the fonts.
208 //        ---Example: "helvetica-bold"
209 // ============================================================================
210
211 Standard_CString Aspect_FontStyle::AliasName() const
212 {
213 static TCollection_AsciiString aliasname;
214     aliasname.Clear();
215     TCollection_AsciiString family(Family());
216     if( family.Length() > 0 && family != "*" ) {
217       TCollection_AsciiString FAMILY = family; FAMILY.UpperCase();
218       family.SetValue(1,FAMILY.Value(1));
219       aliasname = family; 
220     }
221     TCollection_AsciiString weight(Weight()); 
222     if( weight.Length() > 0 && weight != "*" ) {
223       TCollection_AsciiString WEIGHT = weight; WEIGHT.UpperCase();
224       if( WEIGHT == "NORMAL" ) {
225         weight.Clear();
226       } else {
227         weight.SetValue(1,WEIGHT.Value(1));
228       }
229     } else weight.Clear();
230     TCollection_AsciiString sslant(SSlant());
231     if( sslant.Length() > 0 && sslant != "*" ) {
232       if( sslant == "r" ) {
233         sslant.Clear();
234       } else if( sslant == "i" ) {
235         weight += "Italic";
236         sslant.Clear();
237       } else if( sslant == "o" ) {
238         weight += "Oblique";
239         sslant.Clear();
240       }
241     } else sslant.Clear();
242     if( weight.Length() > 0 ) {
243       aliasname += '-'; aliasname += weight;
244     }
245     if( sslant.Length() > 0 ) {
246       aliasname += '-'; aliasname += sslant;
247     }
248     TCollection_AsciiString swidth(SWidth());
249     if( swidth.Length() > 0 && swidth != "*" ) {
250       TCollection_AsciiString SWIDTH = swidth; SWIDTH.UpperCase();
251       if( SWIDTH == "NORMAL" ) {
252       } else {
253         if( !weight.Length() ) aliasname += '-';
254         aliasname += '-'; aliasname += swidth;
255       }
256     }
257     return aliasname.ToCString();
258 }
259 // ============================================================================
260 //        ---Purpose: Returns the full normalized font name
261 // ============================================================================
262
263 Standard_CString Aspect_FontStyle::FullName () const {
264
265         return (MyFontName.ToCString());
266 }
267
268 // ============================================================================
269 //        ---Purpose: Returns the foundry of the font.
270 //        ---Example: "adobe"
271 // ============================================================================
272
273 Standard_CString Aspect_FontStyle::Foundry() const
274 {
275     return Field(MyFontName,1);
276 }
277
278 // ============================================================================
279 //        ---Purpose: Returns the family of the font.
280 //        ---Example: "helvetica"
281 // ============================================================================
282
283 Standard_CString Aspect_FontStyle::Family() const
284 {
285     return Field(MyFontName,2);
286 }
287
288 // ============================================================================
289 //        ---Purpose: Returns the weight of the font.
290 //        ---Example: "bold"
291 // ============================================================================
292
293 Standard_CString Aspect_FontStyle::Weight() const
294 {
295     return Field(MyFontName,3);
296 }
297
298 // ============================================================================
299 //        ---Purpose: Returns the char set registry of the font.
300 //        ---Example: "iso8859"
301 // ============================================================================
302
303 Standard_CString Aspect_FontStyle::Registry() const
304 {
305     return Field(MyFontName,13);
306 }
307
308 // ============================================================================
309 //        ---Purpose: Returns the char set encoding of the font.
310 //        ---Example: "1"
311 // ============================================================================
312
313 Standard_CString Aspect_FontStyle::Encoding() const
314 {
315     return Field(MyFontName,14);
316 }
317
318 // ============================================================================
319 //        ---Purpose: Returns the slant of the font.
320 //        ---Example: "i"
321 // ============================================================================
322
323 Standard_CString Aspect_FontStyle::SSlant() const
324 {
325     return Field(MyFontName,4);
326 }
327
328 // ============================================================================
329 //        ---Purpose: Returns the width name of the font.
330 //        ---Example: "normal"
331 // ============================================================================
332
333 Standard_CString Aspect_FontStyle::SWidth() const
334 {
335     return Field(MyFontName,5);
336 }
337
338 // ============================================================================
339 //        ---Purpose: Returns the style name of the font.
340 //        ---Example: "serif"
341 // ============================================================================
342
343 Standard_CString Aspect_FontStyle::SStyle() const
344 {
345     return Field(MyFontName,6);
346 }
347
348 // ============================================================================
349 //        ---Purpose: Returns the pixel size of the font.
350 //        ---Example: "14"
351 // ============================================================================
352
353 Standard_CString Aspect_FontStyle::SPixelSize() const
354 {
355     return Field(MyFontName,7);
356 }
357
358 // ============================================================================
359 //        ---Purpose: Returns the point size of the font.
360 //        ---Example: "140"
361 // ============================================================================
362
363 Standard_CString Aspect_FontStyle::SPointSize() const
364 {
365     return Field(MyFontName,8);
366 }
367 // ============================================================================
368 //        ---Purpose: Returns the resolution X of the font.
369 //        ---Example: "75"
370 // ============================================================================
371
372 Standard_CString Aspect_FontStyle::SResolutionX() const
373 {
374     return Field(MyFontName,9);
375 }
376
377 // ============================================================================
378 //        ---Purpose: Returns the resolution Y of the font.
379 //        ---Example: "75"
380 // ============================================================================
381
382 Standard_CString Aspect_FontStyle::SResolutionY() const
383 {
384     return Field(MyFontName,10);
385 }
386
387 // ============================================================================
388 //        ---Purpose: Returns the spacing of the font.
389 //        ---Example: "p"
390 // ============================================================================
391
392 Standard_CString Aspect_FontStyle::SSpacing() const
393 {
394     return Field(MyFontName,11);
395 }
396
397 // ============================================================================
398 //        ---Purpose: Returns the average width of the font.
399 //        ---Example: "90"
400 // ============================================================================
401
402 Standard_CString Aspect_FontStyle::SAverageWidth() const
403 {
404     return Field(MyFontName,12);
405 }
406
407 // ============================================================================
408 //        ---Purpose: Dumps the font attributes.
409 // ============================================================================
410
411 void Aspect_FontStyle::Dump() const
412 {
413     cout << ".Aspect_FontStyle::Dump() :" << endl;
414     cout << "-----------------" << endl << endl;
415     cout << "  style name is : '" << this->Value() << "'" << endl;
416     cout << "  Normalized font name is : '" << this->FullName() << "'" << endl;
417     cout << "  Alias font name is : '" << this->AliasName() << "'" << endl;
418     cout << "    Foundry is : '" << this->Foundry() << "'" << endl;
419     cout << "    Family is : '" << this->Family() << "'" << endl;
420     cout << "    Weight is : '" << this->Weight() << "'" << endl;
421     cout << "    Slant is : '" << this->SSlant() << "'" << endl;
422     cout << "    Width is : '" << this->SWidth() << "'" << endl;
423     cout << "    Style is : '" << this->SStyle() << "'" << endl;
424     cout << "    PixelSize is : '" << this->SPixelSize() << "'" << endl;
425     cout << "    PointSize is : '" << this->SPointSize() << "'" << endl;
426     cout << "    ResolutionX is : '" << this->SResolutionX() << "'" << endl;
427     cout << "    ResolutionY is : '" << this->SResolutionY() << "'" << endl;
428     cout << "    Spacing is : '" << this->SSpacing() << "'" << endl;
429     cout << "    AverageWidth is : '" << this->SAverageWidth() << "'" << endl;
430     cout << "    Registry is : '" << this->Registry() << "'" << endl;
431     cout << "    Encoding is : '" << this->Encoding() << "'" << endl << endl;
432 }
433
434 void Aspect_FontStyle::SetPredefinedStyle( const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
435
436         if( Size <= 0. )
437                 Aspect_FontStyleDefinitionError::Raise ("Bad font Size");
438
439         MyFontType = Type ;
440         MyFontSize = Size;
441         MyFontSlant = Slant ;
442         MyCapsHeight = CapsHeight;
443         Standard_CString pstyle = "" ;
444         switch ( Type ) {
445             case Aspect_TOF_USERDEFINED :
446                 Aspect_FontStyleDefinitionError::Raise ("Bad Font Type Style");
447             case Aspect_TOF_DEFAULT :
448                 pstyle = "Defaultfont" ;
449                 break ;
450             case Aspect_TOF_COURIER :
451                 pstyle = "Courier" ;
452                 break ;
453             case Aspect_TOF_HELVETICA :
454                 pstyle = "Helvetica" ;
455                 break ;
456             case Aspect_TOF_TIMES :
457                 pstyle = "Times" ;
458                 break ;
459         }
460         MyStyle = pstyle;
461         MyFontName = Normalize(pstyle,MyFontSize);
462 }
463
464 Standard_Boolean Aspect_FontStyle::IsEqual(const Aspect_FontStyle& Other) const
465 {
466   return (
467           (MyFontType == Other.MyFontType) &&
468           (MyFontName.Length() == Other.MyFontName.Length()) &&
469           (MyFontName == Other.MyFontName) &&
470           (MyFontSize == Other.MyFontSize) &&
471           (MyFontSlant == Other.MyFontSlant) &&
472           (MyCapsHeight == Other.MyCapsHeight)
473          );
474
475 }
476
477 Standard_Boolean Aspect_FontStyle::IsNotEqual(const Aspect_FontStyle& Other) const
478 {
479   return !IsEqual(Other);
480 }
481
482 // ============================================================================
483 //        ---Purpose: Returns a normalized XLFD descriptor from the font name
484 //        --         <aFontName>
485 // ============================================================================
486
487 Standard_CString Aspect_FontStyle::Normalize(const Standard_CString aFontName,Standard_Real& aSize)
488 {
489     Aspect_FontStyleDefinitionError_Raise_if( 
490                                 !aFontName ,"NULL font descriptor");
491
492 static TCollection_AsciiString afontstring;
493 static TCollection_AsciiString astar("*");
494 static TCollection_AsciiString azero("0");
495 TCollection_AsciiString afield,afontname(aFontName);
496 Standard_CString pfontname = NULL;
497 Standard_Boolean xlfd = Standard_False;
498 Standard_Integer i,len = afontname.Length();
499
500     afontstring = "";
501
502     if( len <= 0 ) return pfontname;
503
504     if( afontname.Value(1) != '-' ) afontname.Prepend("-adobe-");
505     for( i=1 ; i<=MAXFIELDS ; i++ ) {
506       Standard_CString pfield = Field(afontname,i);
507       switch (i) {
508         case 1:
509           if( pfield ) {
510             afield = pfield; afield.LowerCase();
511             {afontstring += '-'; afontstring += afield;}
512           } else {
513             afontstring += "-adobe";
514           }
515           break;
516         case 7:
517           if( pfield ) {
518             afield = pfield; afield.LowerCase();
519             if( afield == astar ) {
520               if( aSize > 0. ) afontstring += "-*";
521               else {afontstring += "-0"; xlfd = Standard_True;}
522             } else if( afield == azero ) { afontstring += "-0"; xlfd = Standard_True;}
523             else {afontstring += '-'; afontstring += afield;}
524           } else {
525             if( aSize > 0. ) afontstring += "-*";
526             else { afontstring += "-0"; xlfd = Standard_True;}
527           }
528           break;
529         case 8:
530         case 9:
531         case 10:
532           if( xlfd ) {
533             afontstring += "-0";
534           } else if( pfield ) {
535             afield = pfield; afield.LowerCase();
536             afontstring += '-'; afontstring += afield;
537           } else {
538             afontstring += "-*";
539           }
540           break;
541         case 13:
542           if( pfield ) {
543             afield = pfield; afield.LowerCase();
544             afontstring += '-'; afontstring += afield;
545           } else {
546             afontstring += "-*";
547           }
548           break;
549         default:
550           if( pfield ) {
551             afield = pfield;
552             afontstring += '-'; afontstring += afield;
553           } else {
554             afontstring += "-*";
555           }
556       }
557     }
558     pfontname = afontstring.ToCString();
559     if( aSize == 0. ) {
560       Standard_CString psize = Field(pfontname,7);
561       Standard_Integer isize;
562       if( sscanf(psize,"%d",&isize) == 1 ) {
563         aSize = isize*PITCHSIZE;
564       } else {
565         aSize = 2. MILLIMETER;
566       }
567     }
568
569     return pfontname;
570 }
571
572 // ============================================================================
573 //        ---Purpose: Returns the field at position <aRank>
574 //        --          from the font name <aFontName>.
575 // ============================================================================
576
577 Standard_CString Aspect_FontStyle::Field(const TCollection_AsciiString& aFontName,const Standard_Integer aRank)
578 {
579 static TCollection_AsciiString afieldstring;
580 Standard_Integer length = aFontName.Length();
581 Standard_CString pfield = NULL;
582
583     if( length > 0 && aRank >= 1 && aRank <= MAXFIELDS ) {
584       Standard_Integer start = 1,end = 0;
585       start = aFontName.Location(aRank,'-',1,length);
586       end = aFontName.Location(aRank+1,'-',1,length);
587       afieldstring = "";
588       if( start > 0 && end > 0 ) {
589         start++ ; 
590         end--;
591         if( end >= start ) afieldstring = aFontName.SubString(start,end);
592         else afieldstring = "";
593       } else if( start > 0 ) {
594         start++ ; 
595         end = length;
596         if( end >= start ) afieldstring = aFontName.SubString(start,end);
597         else afieldstring = "";
598       } else {
599         return NULL;
600       }
601     }
602     pfield = afieldstring.ToCString();
603
604     return pfield;
605 }
606
607 // ============================================================================
608 //        ---Purpose: Sets the field at position <aRank>
609 //        --          of the font <aFontName>
610 //        --          from the field <aField>.
611 // ============================================================================
612
613 Standard_CString Aspect_FontStyle::SetField(const TCollection_AsciiString& aFontName,const Standard_CString aField,const Standard_Integer aRank)
614 {
615 static TCollection_AsciiString afontstring;
616 Standard_Integer length = aFontName.Length();
617 Standard_CString pfontname = NULL;
618
619     if( length > 0 && aRank >= 1 && aRank <= MAXFIELDS ) {
620       Standard_Integer start = 1,end = 0;
621       start = aFontName.Location(aRank,'-',1,length);
622       end = aFontName.Location(aRank+1,'-',1,length);
623       TCollection_AsciiString lowername(""),uppername(""); 
624       if( start > 0 && end > 0 ) {
625         if( end > start ) {
626           if( start > 1 ) lowername = aFontName.SubString(1,start);
627           uppername = aFontName.SubString(end,length);
628         }
629       } else if( start > 1 ) {
630         lowername = aFontName.SubString(1,start);
631       } else {
632         return NULL;
633       }
634       afontstring = lowername;
635       afontstring += aField; afontstring += uppername;
636       pfontname = afontstring.ToCString();
637     }
638
639     return pfontname;
640 }