merge the formfield patch from ooo-build
[ooovba.git] / vcl / unx / source / gdi / xlfd_extd.cxx
blob30abc5a354414137212fb9cdb44d7be7a28df71b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xlfd_extd.cxx,v $
10 * $Revision: 1.32 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "xlfd_attr.hxx"
37 #include "xlfd_smpl.hxx"
38 #include "xlfd_extd.hxx"
39 #include <vcl/outfont.hxx>
40 #include <rtl/alloc.h>
42 #ifndef _RTL_TENCINFO_H_
43 #include <rtl/tencinfo.h>
44 #endif
46 #include <set>
48 // --------------------------------------------------------------------------
50 ImplX11FontData::ImplX11FontData( const ExtendedXlfd& rXlfd, int nHeight )
51 : ImplFontData( rXlfd, X11IFD_MAGIC ),
52 mrXlfd( rXlfd )
54 mnHeight = nHeight;
55 mnWidth = 0;
58 ImplFontEntry* ImplX11FontData::CreateFontInstance( ImplFontSelectData& rFSD ) const
60 ImplFontEntry* pEntry = new ImplFontEntry( rFSD );
61 return pEntry;
64 sal_IntPtr ImplX11FontData::GetFontId() const
66 return reinterpret_cast<sal_IntPtr>(&mrXlfd);
69 // --------------------------------------------------------------------------
71 // classes for Xlfd handling that contain more than a single encoding.
72 // Members that may vary through different encodings are stored in
73 // a mpEncodingInfo member. There are three different classes:
74 // true scalable fonts (truetype and type1) scalable bitmap fonts
75 // (the ugly ones) and bitmap fonts. The ExtendedXlfd stores all the members
76 // that are specific to a font outline
77 // ( e.g. adobe-times-roman-medium-r-normal- X -p- X )
78 // and specifies the interface.
80 // --------------------------------------------------------------------------
82 ExtendedXlfd::EncodingInfo&
83 ExtendedXlfd::EncodingInfo::operator= ( const Xlfd *pXlfd )
85 mcSpacing = pXlfd->mcSpacing;
86 mnResolutionX = pXlfd->mnResolutionX;
87 mnResolutionY = pXlfd->mnResolutionY;
88 mnAddstyle = pXlfd->mnAddstyle;
89 mnCharset = pXlfd->mnCharset;
90 mnEncoding = pXlfd->GetEncoding();
92 return *this;
95 // ------ base class --------------------------------------------------------
97 ExtendedXlfd::ExtendedXlfd( bool bScalable )
98 : mbScalable( bScalable ),
99 mnEncodings( 0 ),
100 mnEncCapacity( 0 ),
101 mpEncodingInfo( NULL )
103 mbOrientation = false;
104 mbDevice = false;
105 mbSubsettable = false;
106 mbEmbeddable = false;
108 meEmbeddedBitmap = EMBEDDEDBITMAP_DONTKNOW;
109 meAntiAlias = ANTIALIAS_DONTKNOW;
111 mnQuality = -1;
114 ExtendedXlfd::~ExtendedXlfd()
116 if ( mnEncodings != 0 )
117 rtl_freeMemory( mpEncodingInfo );
120 inline void*
121 Realloc( void *pPtr, sal_Size nSize )
123 return rtl_reallocateMemory( pPtr, nSize );
127 ExtendedXlfd::GetEncodingIdx( rtl_TextEncoding nEncoding ) const
129 for ( int i = 0; i < mnEncodings; i++ )
130 if ( nEncoding == mpEncodingInfo[i].mnEncoding )
131 return i;
132 return -1;
135 bool
136 ExtendedXlfd::HasEncoding( rtl_TextEncoding nEncoding ) const
138 return !(GetEncodingIdx( nEncoding ) < 0) ;
141 rtl_TextEncoding
142 ExtendedXlfd::GetEncoding( int i ) const
144 if ( i < mnEncodings && i >= 0 )
145 return mpEncodingInfo[i].mnEncoding;
147 return RTL_TEXTENCODING_DONTKNOW;
150 rtl_TextEncoding
151 ExtendedXlfd::GetEncoding() const
153 return mnEncodings == 1 ? mpEncodingInfo[0].mnEncoding : RTL_TEXTENCODING_DONTKNOW;
156 // query the most unicode / Ascii compatible font: either one of the fonts
157 // is utf16 encoded or there's a single byte font which is unicode
158 // compatible for the first 256 chars (latin1) or for at least 128
159 // chars (most latin-X encodings, cyrillic encodings)
160 rtl_TextEncoding
161 ExtendedXlfd::GetAsciiEncoding( int *pAsciiRange ) const
163 rtl_TextEncoding nBestEncoding = RTL_TEXTENCODING_DONTKNOW;
164 int nLargestRange = 0x0000;
166 for ( int i = 0; i < mnEncodings && nLargestRange < 0xffff; i++ )
168 rtl_TextEncoding nCurEncoding = mpEncodingInfo[i].mnEncoding;
169 switch ( nCurEncoding )
171 case RTL_TEXTENCODING_UNICODE:
172 nLargestRange = 0xffff;
173 nBestEncoding = nCurEncoding;
174 break;
176 case RTL_TEXTENCODING_ISO_8859_1:
177 case RTL_TEXTENCODING_MS_1252:
178 nLargestRange = 0x00ff;
179 nBestEncoding = nCurEncoding;
180 break;
182 case RTL_TEXTENCODING_ISO_8859_2:
183 case RTL_TEXTENCODING_ISO_8859_4:
184 case RTL_TEXTENCODING_ISO_8859_5:
185 case RTL_TEXTENCODING_ISO_8859_6:
186 case RTL_TEXTENCODING_ISO_8859_7:
187 case RTL_TEXTENCODING_ISO_8859_8:
188 case RTL_TEXTENCODING_ISO_8859_9:
189 case RTL_TEXTENCODING_ISO_8859_13:
190 case RTL_TEXTENCODING_ISO_8859_15:
191 case RTL_TEXTENCODING_MS_1251:
192 case RTL_TEXTENCODING_KOI8_R:
193 case RTL_TEXTENCODING_JIS_X_0201:
194 if ( nLargestRange < 0x0080 )
196 nLargestRange = 0x0080;
197 nBestEncoding = nCurEncoding;
199 break;
201 default:
202 if ( nLargestRange == 0x0000 )
204 nBestEncoding = nCurEncoding;
206 break;
210 if ( pAsciiRange != NULL )
211 *pAsciiRange = nLargestRange;
213 return nBestEncoding;
216 bool
217 ExtendedXlfd::AddEncoding( const Xlfd *pXlfd )
219 rtl_TextEncoding nEncoding = pXlfd->GetEncoding();
220 if ( HasEncoding(nEncoding) )
221 return false;
223 if ( mnEncodings == 0 )
225 // bootstrap
226 mnFoundry = pXlfd->mnFoundry;
227 mnFamily = pXlfd->mnFamily;
228 mnWeight = pXlfd->mnWeight;
229 mnSlant = pXlfd->mnSlant;
230 mnSetwidth = pXlfd->mnSetwidth;
231 mpFactory = pXlfd->mpFactory;
233 Attribute *pFamilyAttr = mpFactory->RetrieveFamily( mnFamily );
234 Attribute *pWeightAttr = mpFactory->RetrieveWeight( mnWeight );
235 Attribute *pWidthAttr = mpFactory->RetrieveSetwidth( mnSetwidth );
236 Attribute *pSlantAttr = mpFactory->RetrieveSlant( mnSlant );
238 meFamily = GetFamilyType();
239 meWeight = GetWeight();
240 meItalic = GetSlant();
241 meWidthType = GetWidthType();
242 mbSymbolFlag= (GetEncoding() == RTL_TEXTENCODING_SYMBOL);
243 mePitch = GetPitch();
245 maName = pFamilyAttr->GetAnnotation();
247 // the helvetica narrow hack
248 if ( ! pFamilyAttr->HasFeature(XLFD_FEATURE_NARROW)
249 && pWidthAttr->HasFeature(XLFD_FEATURE_NARROW) )
251 static const String aNarrow( RTL_CONSTASCII_USTRINGPARAM(" Narrow") );
252 maName += aNarrow;
255 // stylename = weight + slant + width
256 // XXX Fix me: there may be a space missing between them
257 if ( meWeight != WEIGHT_NORMAL )
258 maStyleName += pWeightAttr->GetAnnotation();
259 if ( meItalic != ITALIC_NONE )
260 maStyleName += pSlantAttr->GetAnnotation();
261 if ( (meWidthType != WIDTH_NORMAL)
262 && (! pWidthAttr->HasFeature(XLFD_FEATURE_NARROW)) )
263 maStyleName += pWidthAttr->GetAnnotation();
266 if( mnEncodings <= mnEncCapacity )
268 mnEncCapacity += mnEncodings + 4;
269 mpEncodingInfo = (EncodingInfo*)Realloc( mpEncodingInfo, mnEncCapacity * sizeof(EncodingInfo) );
272 mpEncodingInfo[ mnEncodings ] = pXlfd;
273 mnEncodings += 1;
274 return true;
277 void
278 ExtendedXlfd::ToString( ByteString &rString,
279 unsigned short /*nPixelSize*/, rtl_TextEncoding /*nEncoding*/ ) const
281 AppendAttribute( mpFactory->RetrieveFoundry(mnFoundry), rString );
282 AppendAttribute( mpFactory->RetrieveFamily(mnFamily), rString );
283 AppendAttribute( mpFactory->RetrieveWeight(mnWeight), rString );
284 AppendAttribute( mpFactory->RetrieveSlant(mnSlant), rString );
285 AppendAttribute( mpFactory->RetrieveSetwidth(mnSetwidth), rString );
288 void
289 ExtendedXlfd::ToString( ByteString &rString,
290 unsigned short /*nPixelSize*/, char* /*pMatricsString*/, rtl_TextEncoding /*nEncoding*/ ) const
292 AppendAttribute( mpFactory->RetrieveFoundry(mnFoundry), rString );
293 AppendAttribute( mpFactory->RetrieveFamily(mnFamily), rString );
294 AppendAttribute( mpFactory->RetrieveWeight(mnWeight), rString );
295 AppendAttribute( mpFactory->RetrieveSlant(mnSlant), rString );
296 AppendAttribute( mpFactory->RetrieveSetwidth(mnSetwidth), rString );
299 static FontPitch GetPitchFromX11Pitch( const char cSpacing )
301 switch ( cSpacing )
303 case 'c': // fall through
304 case 'm': return PITCH_FIXED;
305 case 'p': return PITCH_VARIABLE;
306 default: return PITCH_DONTKNOW;
310 // you must not call any of the ExtendedXlfd::GetXXX() functions if the
311 // ExtendedXlfd is really empty (i.e. mnEncodings is zero)
313 FontPitch ExtendedXlfd::GetPitch() const
315 if( mnEncodings > 1 )
316 return PITCH_VARIABLE;
317 if( mnEncodings == 1 )
318 return GetPitchFromX11Pitch( mpEncodingInfo[0].mcSpacing );
319 return PITCH_DONTKNOW;
322 FontPitch ExtendedXlfd::GetPitch( rtl_TextEncoding nEncoding ) const
324 for ( int nIdx = 0; nIdx < mnEncodings; nIdx++ )
325 if ( mpEncodingInfo[nIdx].mnEncoding == nEncoding )
326 return GetPitchFromX11Pitch( mpEncodingInfo[nIdx].mcSpacing );
327 return PITCH_DONTKNOW;
330 FontFamily ExtendedXlfd::GetFamilyType() const
332 Attribute *pFamilyAttr= mpFactory->RetrieveFamily(mnFamily);
333 return (FontFamily)pFamilyAttr->GetValue();
336 FontWeight ExtendedXlfd::GetWeight() const
338 Attribute *pWeightAttr = mpFactory->RetrieveWeight(mnWeight);
339 return (FontWeight)pWeightAttr->GetValue();
342 FontItalic ExtendedXlfd::GetSlant() const
344 Attribute *pSlantAttr = mpFactory->RetrieveSlant(mnSlant);
345 return (FontItalic)pSlantAttr->GetValue();
348 FontWidth ExtendedXlfd::GetWidthType() const
350 Attribute *pWidthAttr = mpFactory->RetrieveSetwidth(mnSetwidth);
351 return (FontWidth)pWidthAttr->GetValue();
354 class CodeRange
356 public:
357 CodeRange( int nMin, int nEnd ) : mnMin( nMin ), mnEnd( nEnd ) {}
359 sal_uInt32 GetMin() const { return mnMin; }
360 sal_uInt32 GetEnd() const { return mnEnd; }
362 bool operator<( const CodeRange& r ) const
363 { return (mnMin<r.mnMin) || ((mnMin==r.mnMin) && (mnEnd<r.mnEnd)); }
365 private:
366 sal_uInt32 mnMin, mnEnd;
370 int ExtendedXlfd::GetFontCodeRanges( sal_uInt32* pCodePairs ) const
372 bool bHasUnicode = false;
373 bool bHasUnknownEncoding = false;
375 // approximate unicode ranges from encodings
376 typedef std::set<CodeRange> RangeSet;
377 RangeSet aRangeSet;
379 for( unsigned short i = 0; i < mnEncodings; ++i )
381 // TODO: move encoding -> unicode range mapping to RTL
382 // NOTE: for now only some are VERY roughly approximated
383 const rtl_TextEncoding eEncoding = mpEncodingInfo[i].mnEncoding;
384 switch( mpEncodingInfo[i].mnEncoding )
386 case RTL_TEXTENCODING_SYMBOL: // postscript symbol encoding
387 aRangeSet.insert( CodeRange( 0x0020, 0x0100 ) ); // symbol aliasing
388 aRangeSet.insert( CodeRange( 0xF020, 0xF100 ) );
389 break;
391 case RTL_TEXTENCODING_ISO_8859_15:
392 aRangeSet.insert( CodeRange( 0x20AC, 0x20AD ) ); // Euro currency symbol
393 // fall through
394 case RTL_TEXTENCODING_APPLE_ROMAN:
395 case RTL_TEXTENCODING_ISO_8859_1:
396 case RTL_TEXTENCODING_MS_1252:
397 case RTL_TEXTENCODING_IBM_437:
398 case RTL_TEXTENCODING_IBM_852:
399 aRangeSet.insert( CodeRange( 0x0020, 0x0080 ) );
400 aRangeSet.insert( CodeRange( 0x00A0, 0x0100 ) );
401 break;
403 // Traditional, Simplified, Japanese
404 case RTL_TEXTENCODING_APPLE_CHINSIMP:
405 case RTL_TEXTENCODING_APPLE_CHINTRAD:
406 case RTL_TEXTENCODING_APPLE_JAPANESE:
407 case RTL_TEXTENCODING_SHIFT_JIS:
408 case RTL_TEXTENCODING_GB_2312:
409 case RTL_TEXTENCODING_GBT_12345:
410 case RTL_TEXTENCODING_GBK:
411 case RTL_TEXTENCODING_BIG5:
412 case RTL_TEXTENCODING_EUC_JP:
413 case RTL_TEXTENCODING_EUC_CN:
414 case RTL_TEXTENCODING_EUC_TW:
415 case RTL_TEXTENCODING_ISO_2022_JP:
416 case RTL_TEXTENCODING_ISO_2022_CN:
417 case RTL_TEXTENCODING_GB_18030:
418 case RTL_TEXTENCODING_BIG5_HKSCS:
419 case RTL_TEXTENCODING_JIS_X_0201:
420 case RTL_TEXTENCODING_JIS_X_0208:
421 case RTL_TEXTENCODING_JIS_X_0212:
422 case RTL_TEXTENCODING_MS_932:
423 case RTL_TEXTENCODING_MS_936:
424 case RTL_TEXTENCODING_MS_950:
425 aRangeSet.insert( CodeRange( 0x3000, 0xA000 ) );
426 aRangeSet.insert( CodeRange( 0xF900, 0xFB00 ) );
427 break;
429 // Korean
430 case RTL_TEXTENCODING_APPLE_KOREAN:
431 case RTL_TEXTENCODING_MS_949:
432 case RTL_TEXTENCODING_MS_1361:
433 case RTL_TEXTENCODING_EUC_KR:
434 case RTL_TEXTENCODING_ISO_2022_KR:
435 aRangeSet.insert( CodeRange( 0x1100, 0x1200 ) );
436 aRangeSet.insert( CodeRange( 0x3130, 0x3190 ) );
437 aRangeSet.insert( CodeRange( 0xAC00, 0xD7A4 ) );
438 break;
440 // unknown encoding
441 case RTL_TEXTENCODING_DONTKNOW:
442 bHasUnknownEncoding = true;
443 break;
445 // Unicode
446 case RTL_TEXTENCODING_UNICODE:
447 case RTL_TEXTENCODING_UTF7:
448 case RTL_TEXTENCODING_UTF8:
449 bHasUnicode = true;
450 break;
452 // misc 8bit encodings
453 default:
454 if( !rtl_isOctetTextEncoding( eEncoding ) )
455 bHasUnknownEncoding = true;
456 else
458 // use the unicode converter to get the coverage of an 8bit encoding
459 rtl_TextToUnicodeConverter aConverter = rtl_createTextToUnicodeConverter( eEncoding );
460 rtl_UnicodeToTextContext aCvtContext = rtl_createTextToUnicodeContext( aConverter );
461 if( !aConverter || !aCvtContext )
462 bHasUnknownEncoding = true;
463 else
465 sal_Char cCharsInp[ 0x100 ];
466 for( int j = 0x20; j < 0x080; ++j )
467 cCharsInp[ j-0x20 ] = j;
468 for( int j = 0xA0; j < 0x100; ++j )
469 cCharsInp[ j-0x40 ] = j;
471 sal_Unicode cCharsOut[ 0x100 ];
472 sal_uInt32 nCvtInfo;
473 sal_Size nSrcCvtBytes;
474 int nOutLen = rtl_convertTextToUnicode(
475 aConverter, aCvtContext,
476 cCharsInp, 0xC0,
477 cCharsOut, sizeof(cCharsOut)/sizeof(*cCharsOut),
478 RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE
479 | RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_IGNORE,
480 &nCvtInfo, &nSrcCvtBytes );
482 for( int j = 0; j < nOutLen; ++j )
483 aRangeSet.insert( CodeRange( cCharsOut[j], cCharsOut[j]+1 ) );
485 rtl_destroyTextToUnicodeConverter( aCvtContext );
486 rtl_destroyTextToUnicodeConverter( aConverter );
489 break;
493 // unicode encoded fonts usually do not cover the entire unicode range
494 // => only use them to determine coverage when no other encodings are available
495 if( aRangeSet.empty() && (bHasUnicode || bHasUnknownEncoding) )
497 if( pCodePairs )
499 pCodePairs[0] = 0x0020;
500 pCodePairs[1] = 0xD800;
501 pCodePairs[2] = 0xE000;
502 pCodePairs[3] = 0xFFFE;
504 return 2;
507 if( aRangeSet.empty() )
508 return 0;
510 // sort and merge the code pairs
511 sal_uInt32* pDst = pCodePairs;
512 RangeSet::const_iterator it = aRangeSet.begin();
513 for( sal_uInt32 nEnd = 0; it != aRangeSet.end(); ++it )
515 // check overlap with to previous range
516 const CodeRange& rSrc = *it;
517 if( nEnd < rSrc.GetMin() )
519 nEnd = rSrc.GetEnd();
520 if( pCodePairs )
522 pDst[0] = rSrc.GetMin();
523 pDst[1] = rSrc.GetEnd();
525 pDst += 2;
527 else
529 // merge overlapping ranges
530 if( nEnd < rSrc.GetEnd() )
532 nEnd = rSrc.GetEnd();
533 if( pCodePairs )
534 pDst[-1] = nEnd;
539 int nRangeCount = (pDst - pCodePairs) / 2;
540 return nRangeCount;
543 // ------ class to handle scalable bitmap fonts ------------------------------
545 ScalableBitmapXlfd::ScalableBitmapXlfd()
546 : ExtendedXlfd( true )
549 ScalableBitmapXlfd::~ScalableBitmapXlfd()
552 void
553 ScalableBitmapXlfd::ToString( ByteString &rString,
554 unsigned short nPixelSize, rtl_TextEncoding nEncoding ) const
556 int nIdx = GetEncodingIdx( nEncoding );
557 if ( nIdx < 0 )
558 return;
560 ExtendedXlfd::ToString( rString, nPixelSize, nEncoding );
561 EncodingInfo& rInfo = mpEncodingInfo[ nIdx ];
563 AppendAttribute( mpFactory->RetrieveAddstyle(rInfo.mnAddstyle), rString );
565 rString += '-';
566 rString += ByteString::CreateFromInt32( nPixelSize );
567 rString += "-0-";
568 rString += ByteString::CreateFromInt32( rInfo.mnResolutionX );
569 rString += '-';
570 rString += ByteString::CreateFromInt32( rInfo.mnResolutionY );
571 rString += '-';
572 rString += static_cast< char >(rInfo.mcSpacing);
573 rString += "-0";
575 AppendAttribute( mpFactory->RetrieveCharset(rInfo.mnCharset), rString );
578 void
579 ScalableBitmapXlfd::ToString( ByteString &rString,
580 unsigned short nPixelSize, char *pMatricsString, rtl_TextEncoding nEncoding ) const
582 int nIdx = GetEncodingIdx( nEncoding );
583 if ( nIdx < 0 )
584 return;
586 ExtendedXlfd::ToString( rString, nPixelSize, nEncoding );
587 EncodingInfo& rInfo = mpEncodingInfo[ nIdx ];
589 AppendAttribute( mpFactory->RetrieveAddstyle(rInfo.mnAddstyle), rString );
591 rString += "-*-";
592 char pTmp[256];
593 snprintf( pTmp, sizeof(pTmp), pMatricsString, nPixelSize, nPixelSize );
594 rString += pTmp;
595 rString += "-*-*-";
596 rString += static_cast< char >(rInfo.mcSpacing);
597 rString += "-*";
599 AppendAttribute( mpFactory->RetrieveCharset(rInfo.mnCharset), rString );
602 ImplFontData* ScalableBitmapXlfd::GetImplFontData() const
604 ImplX11FontData* pFontData = new ImplX11FontData( *this, 0 );
605 pFontData->mnQuality= 0;
606 return pFontData;
609 // ------ class to handle true bitmap fonts ----------------------------------
611 void
612 BitmapXlfd::ToString( ByteString &rString,
613 unsigned short nPixelSize, char *pMatricsString, rtl_TextEncoding nEncoding ) const
615 int nIdx = GetEncodingIdx( nEncoding );
616 if ( nIdx < 0 )
617 return;
619 ExtendedXlfd::ToString( rString, nPixelSize, nEncoding );
620 EncodingInfo& rInfo = mpEncodingInfo[ nIdx ];
622 AppendAttribute( mpFactory->RetrieveAddstyle(rInfo.mnAddstyle), rString );
624 rString += "-*-";
625 char pTmp[256];
626 snprintf( pTmp, sizeof(pTmp), pMatricsString, nPixelSize, nPixelSize );
627 rString += pTmp;
628 rString += "-*-*-";
629 rString += static_cast< char >(rInfo.mcSpacing);
630 rString += "-*";
632 AppendAttribute( mpFactory->RetrieveCharset(rInfo.mnCharset), rString );
635 BitmapXlfd::BitmapXlfd( )
636 : ExtendedXlfd( false )
639 BitmapXlfd::~BitmapXlfd( )
642 bool
643 BitmapXlfd::AddEncoding( const Xlfd *pXlfd )
645 if ( mnEncodings == 0 )
647 mnPixelSize = pXlfd->mnPixelSize;
648 mnPointSize = pXlfd->mnPointSize;
649 mnAverageWidth = pXlfd->mnAverageWidth;
652 return ExtendedXlfd::AddEncoding( pXlfd );
655 void
656 BitmapXlfd::ToString( ByteString &rString,
657 unsigned short nPixelSize, rtl_TextEncoding nEncoding ) const
659 int nIdx = GetEncodingIdx( nEncoding );
660 if ( nIdx < 0 )
661 return;
663 ExtendedXlfd::ToString( rString, nPixelSize, nEncoding );
664 EncodingInfo& rInfo = mpEncodingInfo[ nIdx ];
665 AppendAttribute( mpFactory->RetrieveAddstyle(rInfo.mnAddstyle), rString );
666 rString += '-';
667 rString += ByteString::CreateFromInt32( mnPixelSize );
668 rString += "-*-*-*-";
669 rString += static_cast< char >(rInfo.mcSpacing);
670 rString += "-*";
672 AppendAttribute( mpFactory->RetrieveCharset(rInfo.mnCharset), rString );
675 ImplFontData* BitmapXlfd::GetImplFontData() const
677 ImplX11FontData* pFontData = new ImplX11FontData( *this, mnPixelSize );
678 pFontData->mnQuality= 100;
679 return pFontData;
682 // ------ class to handle true scalable fonts --------------------------------
684 ScalableXlfd::ScalableXlfd()
685 : ExtendedXlfd( true )
688 ScalableXlfd::~ScalableXlfd()
691 void
692 ScalableXlfd::ToString( ByteString &rString,
693 unsigned short nPixelSize, rtl_TextEncoding nEncoding ) const
695 int nIdx = GetEncodingIdx( nEncoding );
696 if ( nIdx < 0 )
697 return;
699 ExtendedXlfd::ToString( rString, nPixelSize, nEncoding);
701 EncodingInfo& rInfo = mpEncodingInfo[ nIdx ];
702 AppendAttribute( mpFactory->RetrieveAddstyle(rInfo.mnAddstyle), rString );
704 rString += '-';
705 rString += ByteString::CreateFromInt32( nPixelSize );
706 rString += "-0-0-0-";
707 rString += static_cast< char >(rInfo.mcSpacing);
708 rString += "-0";
710 AppendAttribute( mpFactory->RetrieveCharset(rInfo.mnCharset), rString );
713 void
714 ScalableXlfd::ToString( ByteString &rString,
715 unsigned short nPixelSize, char* pMatricsString, rtl_TextEncoding nEncoding ) const
717 int nIdx = GetEncodingIdx( nEncoding );
718 if ( nIdx < 0 )
719 return;
721 ExtendedXlfd::ToString( rString, nPixelSize, nEncoding);
723 EncodingInfo& rInfo = mpEncodingInfo[ nIdx ];
724 AppendAttribute( mpFactory->RetrieveAddstyle(rInfo.mnAddstyle), rString );
726 rString += "-*-";
727 char pTmp[256];
728 snprintf( pTmp, sizeof(pTmp), pMatricsString, nPixelSize, nPixelSize );
729 rString += pTmp;
730 rString += "-*-*-";
731 rString += static_cast< char >(rInfo.mcSpacing);
732 rString += "-*";
734 AppendAttribute( mpFactory->RetrieveCharset(rInfo.mnCharset), rString );
737 ImplFontData* ScalableXlfd::GetImplFontData() const
739 ImplX11FontData* pFontData = new ImplX11FontData( *this, 0 );
740 pFontData->mnQuality= 200;
741 return pFontData;
744 /* ------- virtual fonts for user interface ------------------------------- */
746 VirtualXlfd::ExtEncodingInfo&
747 VirtualXlfd::ExtEncodingInfo::operator= ( const Xlfd *pXlfd )
749 mnFoundry = pXlfd->mnFoundry;
750 mnFamily = pXlfd->mnFamily;
751 mnWeight = pXlfd->mnWeight;
752 mnSlant = pXlfd->mnSlant;
753 mnSetwidth = pXlfd->mnSetwidth;
755 return *this;
758 VirtualXlfd::VirtualXlfd()
759 : ExtendedXlfd( true ),
760 mnExtCapacity(0),
761 mpExtEncodingInfo(NULL)
763 mnFoundry = 0;
764 mnFamily = 0;
765 mnWeight = 0;
766 mnSlant = 0;
767 mnSetwidth = 0;
770 VirtualXlfd::~VirtualXlfd()
772 if ( mpExtEncodingInfo != NULL )
773 rtl_freeMemory( mpExtEncodingInfo );
777 VirtualXlfd::GetFontQuality (unsigned short nFamily)
779 Attribute *pFamily = mpFactory->RetrieveFamily(nFamily);
780 int nQuality = 0;
782 if (pFamily->HasFeature(XLFD_FEATURE_HQ))
783 nQuality += 16;
784 if (pFamily->HasFeature(XLFD_FEATURE_MQ))
785 nQuality += 8;
786 if (pFamily->HasFeature(XLFD_FEATURE_LQ))
787 nQuality += 4;
788 return nQuality;
791 bool
792 VirtualXlfd::AddEncoding( const Xlfd *pXlfd )
794 // add new font
795 bool bRC = ExtendedXlfd::AddEncoding( pXlfd );
797 int nIdx;
798 if( bRC )
800 // new encoding => append the new pXlfd
801 nIdx = mnEncodings - 1;
802 if( nIdx >= mnExtCapacity )
804 mnExtCapacity = mnEncCapacity;
805 mpExtEncodingInfo = (ExtEncodingInfo*)Realloc( mpExtEncodingInfo,
806 mnExtCapacity * sizeof(ExtEncodingInfo) );
809 else
811 // existing encoding => check if the new pXlfd is better
812 rtl_TextEncoding nEncoding = pXlfd->GetEncoding();
813 nIdx = GetEncodingIdx( nEncoding );
815 int nOldQuality = GetFontQuality( mpExtEncodingInfo[nIdx].mnFamily );
816 int nNewQuality = GetFontQuality( pXlfd->mnFamily );
817 if( nOldQuality >= nNewQuality )
818 return false;
821 mpExtEncodingInfo[ nIdx ] = pXlfd;
822 return true;
825 void
826 VirtualXlfd::FilterInterfaceFont (const Xlfd *pXlfd)
828 Attribute *pAttr;
829 AttributeProvider *pFactory = pXlfd->mpFactory;
831 if (! pXlfd->Fonttype() == TYPE_SCALABLE)
832 return;
833 pAttr = pFactory->RetrieveFamily(pXlfd->mnFamily);
834 if (! pAttr->HasFeature(XLFD_FEATURE_INTERFACE_FONT))
835 return;
836 pAttr = pFactory->RetrieveSlant(pXlfd->mnSlant);
837 if (! (FontItalic)pAttr->GetValue() == ITALIC_NONE)
838 return;
839 pAttr = pFactory->RetrieveSetwidth(pXlfd->mnSetwidth);
840 if (pAttr->HasFeature(XLFD_FEATURE_NARROW))
841 return;
842 pAttr = pFactory->RetrieveWeight(pXlfd->mnWeight);
843 FontWeight eWeight = (FontWeight)pAttr->GetValue();
844 if ((eWeight != WEIGHT_NORMAL) && (eWeight != WEIGHT_MEDIUM))
845 return;
847 AddEncoding (pXlfd);
850 void
851 VirtualXlfd::ToString( ByteString &rString, unsigned short nPixelSize,
852 rtl_TextEncoding nEncoding ) const
854 int nIdx = GetEncodingIdx( nEncoding );
855 if ( nIdx < 0 )
856 return;
858 ExtEncodingInfo &rExtInfo = mpExtEncodingInfo[ nIdx ];
860 AppendAttribute( mpFactory->RetrieveFoundry(rExtInfo.mnFoundry), rString );
861 AppendAttribute( mpFactory->RetrieveFamily(rExtInfo.mnFamily), rString );
862 AppendAttribute( mpFactory->RetrieveWeight(rExtInfo.mnWeight), rString );
863 AppendAttribute( mpFactory->RetrieveSlant(rExtInfo.mnSlant), rString );
864 AppendAttribute( mpFactory->RetrieveSetwidth(rExtInfo.mnSetwidth), rString );
866 EncodingInfo& rInfo = mpEncodingInfo[ nIdx ];
867 AppendAttribute( mpFactory->RetrieveAddstyle(rInfo.mnAddstyle), rString );
869 rString += '-';
870 rString += ByteString::CreateFromInt32( nPixelSize );
871 rString += "-0-0-0-";
872 rString += static_cast< char >(rInfo.mcSpacing);
873 rString += "-0";
875 AppendAttribute( mpFactory->RetrieveCharset(rInfo.mnCharset), rString );
878 void
879 VirtualXlfd::ToString( ByteString &rString, unsigned short nPixelSize,
880 char* pMatricsString, rtl_TextEncoding nEncoding ) const
882 int nIdx = GetEncodingIdx( nEncoding );
883 if ( nIdx < 0 )
884 return;
886 ExtEncodingInfo &rExtInfo = mpExtEncodingInfo[ nIdx ];
888 AppendAttribute( mpFactory->RetrieveFoundry(rExtInfo.mnFoundry), rString );
889 AppendAttribute( mpFactory->RetrieveFamily(rExtInfo.mnFamily), rString );
890 AppendAttribute( mpFactory->RetrieveWeight(rExtInfo.mnWeight), rString );
891 AppendAttribute( mpFactory->RetrieveSlant(rExtInfo.mnSlant), rString );
892 AppendAttribute( mpFactory->RetrieveSetwidth(rExtInfo.mnSetwidth), rString );
894 EncodingInfo& rInfo = mpEncodingInfo[ nIdx ];
895 AppendAttribute( mpFactory->RetrieveAddstyle(rInfo.mnAddstyle), rString );
897 rString += "-*-";
898 char pTmp[256];
899 snprintf( pTmp, sizeof(pTmp), pMatricsString, nPixelSize, nPixelSize );
900 rString += pTmp;
901 rString += "-*-*-";
902 rString += static_cast< char >(rInfo.mcSpacing);
903 rString += "-*";
905 AppendAttribute( mpFactory->RetrieveCharset(rInfo.mnCharset), rString );
908 ImplFontData* VirtualXlfd::GetImplFontData() const
910 ImplX11FontData* pFontData = new ImplX11FontData( *this, 0 );
912 // family name
913 static const String aFontName( RTL_CONSTASCII_USTRINGPARAM("Interface User") );
914 pFontData->maName = aFontName;
915 // pFontData->maStyleName = aStyleName;
916 pFontData->meFamily = FAMILY_SWISS;
917 pFontData->meWeight = WEIGHT_NORMAL;
918 pFontData->meItalic = ITALIC_NONE;
919 pFontData->meWidthType = WIDTH_NORMAL;
920 pFontData->mePitch = PITCH_VARIABLE;
922 pFontData->mbSymbolFlag = false;
923 pFontData->mbOrientation= false;
924 pFontData->mbDevice = true;
925 pFontData->mnQuality = 100;
927 return pFontData;
930 // ------ font list -------------------------------------------------------
932 XlfdStorage::XlfdStorage()
934 maXlfdList.reserve( 256 );
937 void
938 XlfdStorage::Dispose()
940 XlfdList::const_iterator it = maXlfdList.begin();
941 for(; it != maXlfdList.end(); ++it )
942 delete *it;
943 maXlfdList.clear();
946 void
947 XlfdStorage::Reset()
949 maXlfdList.clear();
952 void
953 XlfdStorage::Add( const ExtendedXlfd* pXlfd )
955 if ( pXlfd != NULL )
956 maXlfdList.push_back( pXlfd );
959 void
960 XlfdStorage::Add( const XlfdStorage* pXlfd )
962 if ( !pXlfd || pXlfd->maXlfdList.empty() )
963 return;
965 maXlfdList.reserve( maXlfdList.size() + pXlfd->maXlfdList.size() );
966 XlfdList::const_iterator it = pXlfd->maXlfdList.begin();
967 for(; it != pXlfd->maXlfdList.end(); ++it )
968 maXlfdList.push_back( *it );
971 void XlfdStorage::AnnounceFonts( ImplDevFontList* pList ) const
973 XlfdList::const_iterator it = maXlfdList.begin();
974 for(; it != maXlfdList.end(); ++it )
976 const ExtendedXlfd* pXlfd = *it;
977 ImplFontData* pFontData = pXlfd->GetImplFontData();
978 pList->Add( pFontData );
982 // ------ bitmap font list --------------------------------------------------
984 void
985 BitmapXlfdStorage::AddBitmapFont( const Xlfd *pXlfd )
987 if ( pXlfd == NULL )
988 return;
990 int nPixelSize = pXlfd->mnPixelSize;
991 XlfdList::const_iterator it = maXlfdList.begin();
992 for(; it != maXlfdList.end(); ++it )
994 BitmapXlfd* pBitmapXlfd = (BitmapXlfd*)*it;
995 if( nPixelSize == pBitmapXlfd->GetPixelSize() )
997 // we need to add an encoding to an existing bitmap font
998 pBitmapXlfd->AddEncoding( pXlfd );
999 return;
1003 // we have a new bitmap font
1004 BitmapXlfd* pBitmapXlfd = new BitmapXlfd;
1005 pBitmapXlfd->AddEncoding( pXlfd );
1006 Add( pBitmapXlfd );