1 // Created on: 1993-09-14
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 //GG_040298 eliminer les variables statiques globales.
19 #define PRO5676 //GG_020197
20 // Donner la possibillite d'exprimer la taille
21 // par rapport a la hauteur totale du caractere ou par
22 // rapport a la hauteur "ascent" situee au dessus de la
26 //GG_160498 etude G1343 gerer les polices transformables pour
27 // l'utilisation de MFT_FontManager.
29 // =====================================================================
30 // DCB_120598 study S3553. Platform specific code has been deleted
31 // ("#ifdef WNT") to be able to use on WNT the same font descriptor
32 // string like on UNIX.
33 // =====================================================================
39 //-Design Declaration des variables specifiques aux Fontures de textes
41 //-Warning Un style est definie, soit par son type predefini TOF_...
42 // soit par sa description (Font string de type ADOBE)
50 #define PITCHSIZE (0.00028 METER);
53 #include <Aspect_FontStyle.ixx>
54 #include <Aspect_Units.hxx>
59 //-Global data definitions
61 // MyFontType : TypeOfFont from Aspect;
62 // MyStyle : AsciiString from TCollection
63 // MyFontName : AsciiCString from TCollection
64 // MyFontSize : Real from Standard
65 // MyFontSlant : Real from Standard
66 // MyCapsHeight : Boolean from Standard
74 Aspect_FontStyle::Aspect_FontStyle () {
76 SetPredefinedStyle(Aspect_TOF_DEFAULT,0.003 METER,0.,Standard_False) ;
79 Aspect_FontStyle::Aspect_FontStyle (const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
81 SetPredefinedStyle(Type,Size,Slant,CapsHeight) ;
84 Aspect_FontStyle::Aspect_FontStyle (const Standard_CString style, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
86 MyFontType = Aspect_TOF_USERDEFINED ;
89 MyCapsHeight = CapsHeight;
91 MyFontName = Normalize(style,MyFontSize) ;
94 Aspect_FontStyle::Aspect_FontStyle (const Standard_CString style) {
96 MyFontType = Aspect_TOF_USERDEFINED ;
99 MyCapsHeight = Standard_False;
101 MyFontName = Normalize(style,MyFontSize) ;
104 Aspect_FontStyle& Aspect_FontStyle::Assign (const Aspect_FontStyle& Other) {
106 MyFontType = Other.Style() ;
107 MyFontSize = Other.Size() ;
108 MyFontSlant = Other.Slant() ;
109 MyStyle = Other.Value();
110 MyFontName = Other.FullName() ;
111 MyCapsHeight = Other.CapsHeight() ;
116 void Aspect_FontStyle::SetValues ( const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
118 SetPredefinedStyle (Type,Size,Slant,CapsHeight);
121 void Aspect_FontStyle::SetValues ( const Standard_CString style, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
122 MyFontType = Aspect_TOF_USERDEFINED ;
125 MyCapsHeight = CapsHeight;
127 MyFontName = Normalize(style,MyFontSize) ;
130 void Aspect_FontStyle::SetValues ( const Standard_CString style) {
131 MyFontType = Aspect_TOF_USERDEFINED ;
134 MyCapsHeight = Standard_False;
136 MyFontName = Normalize(style,MyFontSize) ;
139 // ============================================================================
140 // ---Purpose: Sets the family of the font.
141 // ============================================================================
143 void Aspect_FontStyle::SetFamily(const Standard_CString aName) {
144 MyFontName = SetField(MyFontName,aName,2);
147 // ============================================================================
148 // ---Purpose: Sets the weight of the font.
149 // ============================================================================
151 void Aspect_FontStyle::SetWeight(const Standard_CString aName) {
152 MyFontName = SetField(MyFontName,aName,3);
155 // ============================================================================
156 // ---Purpose: Sets the Registry of the font.
157 // ============================================================================
159 void Aspect_FontStyle::SetRegistry(const Standard_CString aName) {
160 MyFontName = SetField(MyFontName,aName,13);
163 // ============================================================================
164 // ---Purpose: Sets the Encoding of the font.
165 // ============================================================================
167 void Aspect_FontStyle::SetEncoding(const Standard_CString aName) {
168 MyFontName = SetField(MyFontName,aName,14);
171 Aspect_TypeOfFont Aspect_FontStyle::Style () const {
176 Standard_Integer Aspect_FontStyle::Length () const {
178 return MyStyle.Length();
181 Standard_CString Aspect_FontStyle::Value () const
183 return MyStyle.ToCString();
186 Quantity_Length Aspect_FontStyle::Size () const {
190 Quantity_PlaneAngle Aspect_FontStyle::Slant () const {
192 return (MyFontSlant);
195 Standard_Boolean Aspect_FontStyle::CapsHeight () const {
197 return (MyCapsHeight);
200 // ============================================================================
201 // ---Purpose: Returns the alias font name.
202 // -- this is a shorter font name which identify the
203 // -- main characteristics of the fonts.
204 // ---Example: "helvetica-bold"
205 // ============================================================================
207 Standard_CString Aspect_FontStyle::AliasName() const
209 static TCollection_AsciiString aliasname;
211 TCollection_AsciiString family(Family());
212 if( family.Length() > 0 && family != "*" ) {
213 TCollection_AsciiString FAMILY = family; FAMILY.UpperCase();
214 family.SetValue(1,FAMILY.Value(1));
217 TCollection_AsciiString weight(Weight());
218 if( weight.Length() > 0 && weight != "*" ) {
219 TCollection_AsciiString WEIGHT = weight; WEIGHT.UpperCase();
220 if( WEIGHT == "NORMAL" ) {
223 weight.SetValue(1,WEIGHT.Value(1));
225 } else weight.Clear();
226 TCollection_AsciiString sslant(SSlant());
227 if( sslant.Length() > 0 && sslant != "*" ) {
228 if( sslant == "r" ) {
230 } else if( sslant == "i" ) {
233 } else if( sslant == "o" ) {
237 } else sslant.Clear();
238 if( weight.Length() > 0 ) {
239 aliasname += '-'; aliasname += weight;
241 if( sslant.Length() > 0 ) {
242 aliasname += '-'; aliasname += sslant;
244 TCollection_AsciiString swidth(SWidth());
245 if( swidth.Length() > 0 && swidth != "*" ) {
246 TCollection_AsciiString SWIDTH = swidth; SWIDTH.UpperCase();
247 if( SWIDTH == "NORMAL" ) {
249 if( !weight.Length() ) aliasname += '-';
250 aliasname += '-'; aliasname += swidth;
253 return aliasname.ToCString();
255 // ============================================================================
256 // ---Purpose: Returns the full normalized font name
257 // ============================================================================
259 Standard_CString Aspect_FontStyle::FullName () const {
261 return (MyFontName.ToCString());
264 // ============================================================================
265 // ---Purpose: Returns the foundry of the font.
266 // ---Example: "adobe"
267 // ============================================================================
269 Standard_CString Aspect_FontStyle::Foundry() const
271 return Field(MyFontName,1);
274 // ============================================================================
275 // ---Purpose: Returns the family of the font.
276 // ---Example: "helvetica"
277 // ============================================================================
279 Standard_CString Aspect_FontStyle::Family() const
281 return Field(MyFontName,2);
284 // ============================================================================
285 // ---Purpose: Returns the weight of the font.
286 // ---Example: "bold"
287 // ============================================================================
289 Standard_CString Aspect_FontStyle::Weight() const
291 return Field(MyFontName,3);
294 // ============================================================================
295 // ---Purpose: Returns the char set registry of the font.
296 // ---Example: "iso8859"
297 // ============================================================================
299 Standard_CString Aspect_FontStyle::Registry() const
301 return Field(MyFontName,13);
304 // ============================================================================
305 // ---Purpose: Returns the char set encoding of the font.
307 // ============================================================================
309 Standard_CString Aspect_FontStyle::Encoding() const
311 return Field(MyFontName,14);
314 // ============================================================================
315 // ---Purpose: Returns the slant of the font.
317 // ============================================================================
319 Standard_CString Aspect_FontStyle::SSlant() const
321 return Field(MyFontName,4);
324 // ============================================================================
325 // ---Purpose: Returns the width name of the font.
326 // ---Example: "normal"
327 // ============================================================================
329 Standard_CString Aspect_FontStyle::SWidth() const
331 return Field(MyFontName,5);
334 // ============================================================================
335 // ---Purpose: Returns the style name of the font.
336 // ---Example: "serif"
337 // ============================================================================
339 Standard_CString Aspect_FontStyle::SStyle() const
341 return Field(MyFontName,6);
344 // ============================================================================
345 // ---Purpose: Returns the pixel size of the font.
347 // ============================================================================
349 Standard_CString Aspect_FontStyle::SPixelSize() const
351 return Field(MyFontName,7);
354 // ============================================================================
355 // ---Purpose: Returns the point size of the font.
357 // ============================================================================
359 Standard_CString Aspect_FontStyle::SPointSize() const
361 return Field(MyFontName,8);
363 // ============================================================================
364 // ---Purpose: Returns the resolution X of the font.
366 // ============================================================================
368 Standard_CString Aspect_FontStyle::SResolutionX() const
370 return Field(MyFontName,9);
373 // ============================================================================
374 // ---Purpose: Returns the resolution Y of the font.
376 // ============================================================================
378 Standard_CString Aspect_FontStyle::SResolutionY() const
380 return Field(MyFontName,10);
383 // ============================================================================
384 // ---Purpose: Returns the spacing of the font.
386 // ============================================================================
388 Standard_CString Aspect_FontStyle::SSpacing() const
390 return Field(MyFontName,11);
393 // ============================================================================
394 // ---Purpose: Returns the average width of the font.
396 // ============================================================================
398 Standard_CString Aspect_FontStyle::SAverageWidth() const
400 return Field(MyFontName,12);
403 // ============================================================================
404 // ---Purpose: Dumps the font attributes.
405 // ============================================================================
407 void Aspect_FontStyle::Dump() const
409 cout << ".Aspect_FontStyle::Dump() :" << endl;
410 cout << "-----------------" << endl << endl;
411 cout << " style name is : '" << this->Value() << "'" << endl;
412 cout << " Normalized font name is : '" << this->FullName() << "'" << endl;
413 cout << " Alias font name is : '" << this->AliasName() << "'" << endl;
414 cout << " Foundry is : '" << this->Foundry() << "'" << endl;
415 cout << " Family is : '" << this->Family() << "'" << endl;
416 cout << " Weight is : '" << this->Weight() << "'" << endl;
417 cout << " Slant is : '" << this->SSlant() << "'" << endl;
418 cout << " Width is : '" << this->SWidth() << "'" << endl;
419 cout << " Style is : '" << this->SStyle() << "'" << endl;
420 cout << " PixelSize is : '" << this->SPixelSize() << "'" << endl;
421 cout << " PointSize is : '" << this->SPointSize() << "'" << endl;
422 cout << " ResolutionX is : '" << this->SResolutionX() << "'" << endl;
423 cout << " ResolutionY is : '" << this->SResolutionY() << "'" << endl;
424 cout << " Spacing is : '" << this->SSpacing() << "'" << endl;
425 cout << " AverageWidth is : '" << this->SAverageWidth() << "'" << endl;
426 cout << " Registry is : '" << this->Registry() << "'" << endl;
427 cout << " Encoding is : '" << this->Encoding() << "'" << endl << endl;
430 void Aspect_FontStyle::SetPredefinedStyle( const Aspect_TypeOfFont Type, const Quantity_Length Size, const Quantity_PlaneAngle Slant, const Standard_Boolean CapsHeight) {
433 Aspect_FontStyleDefinitionError::Raise ("Bad font Size");
437 MyFontSlant = Slant ;
438 MyCapsHeight = CapsHeight;
439 Standard_CString pstyle = "" ;
441 case Aspect_TOF_USERDEFINED :
442 Aspect_FontStyleDefinitionError::Raise ("Bad Font Type Style");
443 case Aspect_TOF_DEFAULT :
444 pstyle = "Defaultfont" ;
446 case Aspect_TOF_COURIER :
449 case Aspect_TOF_HELVETICA :
450 pstyle = "Helvetica" ;
452 case Aspect_TOF_TIMES :
457 MyFontName = Normalize(pstyle,MyFontSize);
460 Standard_Boolean Aspect_FontStyle::IsEqual(const Aspect_FontStyle& Other) const
463 (MyFontType == Other.MyFontType) &&
464 (MyFontName.Length() == Other.MyFontName.Length()) &&
465 (MyFontName == Other.MyFontName) &&
466 (MyFontSize == Other.MyFontSize) &&
467 (MyFontSlant == Other.MyFontSlant) &&
468 (MyCapsHeight == Other.MyCapsHeight)
473 Standard_Boolean Aspect_FontStyle::IsNotEqual(const Aspect_FontStyle& Other) const
475 return !IsEqual(Other);
478 // ============================================================================
479 // ---Purpose: Returns a normalized XLFD descriptor from the font name
481 // ============================================================================
483 Standard_CString Aspect_FontStyle::Normalize(const Standard_CString aFontName,Standard_Real& aSize)
485 Aspect_FontStyleDefinitionError_Raise_if(
486 !aFontName ,"NULL font descriptor");
488 static TCollection_AsciiString afontstring;
489 static TCollection_AsciiString astar("*");
490 static TCollection_AsciiString azero("0");
491 TCollection_AsciiString afield,afontname(aFontName);
492 Standard_CString pfontname = NULL;
493 Standard_Boolean xlfd = Standard_False;
494 Standard_Integer i,len = afontname.Length();
498 if( len <= 0 ) return pfontname;
500 if( afontname.Value(1) != '-' ) afontname.Prepend("-adobe-");
501 for( i=1 ; i<=MAXFIELDS ; i++ ) {
502 Standard_CString pfield = Field(afontname,i);
506 afield = pfield; afield.LowerCase();
507 {afontstring += '-'; afontstring += afield;}
509 afontstring += "-adobe";
514 afield = pfield; afield.LowerCase();
515 if( afield == astar ) {
516 if( aSize > 0. ) afontstring += "-*";
517 else {afontstring += "-0"; xlfd = Standard_True;}
518 } else if( afield == azero ) { afontstring += "-0"; xlfd = Standard_True;}
519 else {afontstring += '-'; afontstring += afield;}
521 if( aSize > 0. ) afontstring += "-*";
522 else { afontstring += "-0"; xlfd = Standard_True;}
530 } else if( pfield ) {
531 afield = pfield; afield.LowerCase();
532 afontstring += '-'; afontstring += afield;
539 afield = pfield; afield.LowerCase();
540 afontstring += '-'; afontstring += afield;
548 afontstring += '-'; afontstring += afield;
554 pfontname = afontstring.ToCString();
556 Standard_CString psize = Field(pfontname,7);
557 Standard_Integer isize;
558 if( sscanf(psize,"%d",&isize) == 1 ) {
559 aSize = isize*PITCHSIZE;
561 aSize = 2. MILLIMETER;
568 // ============================================================================
569 // ---Purpose: Returns the field at position <aRank>
570 // -- from the font name <aFontName>.
571 // ============================================================================
573 Standard_CString Aspect_FontStyle::Field(const TCollection_AsciiString& aFontName,const Standard_Integer aRank)
575 static TCollection_AsciiString afieldstring;
576 Standard_Integer length = aFontName.Length();
577 Standard_CString pfield = NULL;
579 if( length > 0 && aRank >= 1 && aRank <= MAXFIELDS ) {
580 Standard_Integer start = 1,end = 0;
581 start = aFontName.Location(aRank,'-',1,length);
582 end = aFontName.Location(aRank+1,'-',1,length);
584 if( start > 0 && end > 0 ) {
587 if( end >= start ) afieldstring = aFontName.SubString(start,end);
588 else afieldstring = "";
589 } else if( start > 0 ) {
592 if( end >= start ) afieldstring = aFontName.SubString(start,end);
593 else afieldstring = "";
598 pfield = afieldstring.ToCString();
603 // ============================================================================
604 // ---Purpose: Sets the field at position <aRank>
605 // -- of the font <aFontName>
606 // -- from the field <aField>.
607 // ============================================================================
609 Standard_CString Aspect_FontStyle::SetField(const TCollection_AsciiString& aFontName,const Standard_CString aField,const Standard_Integer aRank)
611 static TCollection_AsciiString afontstring;
612 Standard_Integer length = aFontName.Length();
613 Standard_CString pfontname = NULL;
615 if( length > 0 && aRank >= 1 && aRank <= MAXFIELDS ) {
616 Standard_Integer start = 1,end = 0;
617 start = aFontName.Location(aRank,'-',1,length);
618 end = aFontName.Location(aRank+1,'-',1,length);
619 TCollection_AsciiString lowername(""),uppername("");
620 if( start > 0 && end > 0 ) {
622 if( start > 1 ) lowername = aFontName.SubString(1,start);
623 uppername = aFontName.SubString(end,length);
625 } else if( start > 1 ) {
626 lowername = aFontName.SubString(1,start);
630 afontstring = lowername;
631 afontstring += aField; afontstring += uppername;
632 pfontname = afontstring.ToCString();