merge the formfield patch from ooo-build
[ooovba.git] / vcl / unx / source / gdi / xlfd_smpl.cxx
blob1f17f441343ba24a79086a6e68707ab8c5b42a43
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_smpl.cxx,v $
10 * $Revision: 1.10 $
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"
39 // --------------------------------------------------------------------------
42 // broken down structure equivalent to a Xlfd string
45 // --------------------------------------------------------------------------
47 Xlfd::Xlfd()
51 // XlfdCompare abi has to be qsort(3) compatible, the sorting result must
52 // guarantee that fonts with SameFontoutline() are successive
53 // XlfdCompare relies on vFrom->mpFactory eq vTo->mpFactory. Since comparing
54 // Xlfd's is done by comparing attributes there is no way around this.
55 extern "C" int
56 XlfdCompare( const void *vFrom, const void *vTo )
58 const Xlfd *pFrom = (Xlfd*)vFrom;
59 const Xlfd *pTo = (Xlfd*)vTo;
61 // Compare outline description
62 if ( pFrom->mnFoundry != pTo->mnFoundry )
63 return (int)pFrom->mnFoundry - (int)pTo->mnFoundry;
64 if ( pFrom->mnFamily != pTo->mnFamily )
65 return (int)pFrom->mnFamily - (int)pTo->mnFamily;
66 if ( pFrom->mnWeight != pTo->mnWeight )
67 return (int)pFrom->mnWeight - (int)pTo->mnWeight;
68 if ( pFrom->mnSlant != pTo->mnSlant )
69 return (int)pFrom->mnSlant - (int)pTo->mnSlant;
70 if ( pFrom->mnSetwidth != pTo->mnSetwidth )
71 return (int)pFrom->mnSetwidth - (int)pTo->mnSetwidth;
73 // Addstyle name is futile tricky. it may contain encoding information
74 // (like "ansi_1251") which Compares equal, or it may contain style
75 // information (like "serif") which Compares unequal, anyway if the font
76 // is "interface user" or "interface system" then compare equal anyway to
77 // build fontsets as large as possible
78 if ( pFrom->mnAddstyle == pTo->mnAddstyle )
79 return 0;
81 AttributeProvider *pFactory = pFrom->mpFactory;
82 Attribute *pFamily = pFactory->RetrieveFamily( pFrom->mnFamily );
83 if ( pFamily->HasFeature(XLFD_FEATURE_APPLICATION_FONT) )
84 return 0;
86 Attribute *pFromAddStyle = pFactory->RetrieveAddstyle( pFrom->mnAddstyle );
87 Attribute *pToAddStyle = pFactory->RetrieveAddstyle( pTo->mnAddstyle );
89 // if both addstyles denote encodings or if one denotes an
90 // encoding and the other denotes a style which really
91 // duplicates weight and slant information
93 int nFromCompare = (pFromAddStyle->GetValue() != RTL_TEXTENCODING_DONTKNOW)
94 || (pFromAddStyle->HasFeature(XLFD_FEATURE_REDUNDANTSTYLE)) ?
95 -1 : pFrom->mnAddstyle;
96 int nToCompare = (pToAddStyle->GetValue() != RTL_TEXTENCODING_DONTKNOW)
97 || (pToAddStyle->HasFeature(XLFD_FEATURE_REDUNDANTSTYLE)) ?
98 -1 : pTo->mnAddstyle;
100 return nFromCompare - nToCompare;
103 // check whether two fonts are identical as appearance is concerned
104 // this does not Compare the actual scaling of two fonts
105 Bool
106 Xlfd::SameFontoutline( const Xlfd* pComparedTo ) const
108 void* pThis = (void*)this;
109 return XlfdCompare( (void*)pThis, (void*)pComparedTo ) == 0 ;
112 unsigned short
113 Xlfd::GetEncoding() const
115 Attribute *pAddstyle = mpFactory->RetrieveAddstyle( mnAddstyle );
116 if ( pAddstyle->GetValue() != RTL_TEXTENCODING_DONTKNOW )
117 return pAddstyle->GetValue();
119 Attribute *pEncoding = mpFactory->RetrieveCharset( mnCharset );
120 return pEncoding->GetValue();
123 XlfdFonttype
124 Xlfd::Fonttype() const
126 if ( (mnAverageWidth == 0) && (mnPixelSize == 0) && (mnPointSize == 0) )
128 return (mnResolutionX == 0)
129 && (mnResolutionY == 0) ? eTypeScalable : eTypeScalableBitmap;
132 return eTypeBitmap;
135 void
136 Advance( const char** pFrom, const char** pTo )
138 const char *pTmp = *pTo;
140 for( ; (*pTmp != '\0') && (*pTmp++ != '-'); )
142 *pFrom = *pTo;
143 *pTo = pTmp;
146 Bool
147 Xlfd::IsConformant (const char* pXlfd) const
149 // X FontNameRegistry prefix "-"
150 if (*pXlfd++ != '-')
151 return False;
153 // All Xlfd FontName fields are defined
154 int nNumFields = 1;
155 while (*pXlfd != '\0')
157 if (*pXlfd++ == '-')
158 nNumFields++;
160 // enough entries ?
161 if (nNumFields != 14)
162 return False;
163 // and the last one is not empty as well ?
164 if (*(pXlfd - 1) == '-')
165 return False;
167 return True;
170 // this is the real workhorse function. Since this is called for every font
171 // in the fontpath it has to be as fast a possible
172 Bool
173 Xlfd::FromString( const char* pXlfdstring, AttributeProvider *pFactory )
175 if (!IsConformant(pXlfdstring))
176 return False;
178 const char* pFrom = pXlfdstring + 1;
179 const char* pTo = pFrom;
180 mpFactory = pFactory;
182 Advance( &pFrom, &pTo ); //-foundry-*
183 mnFoundry = mpFactory->InsertFoundry( pFrom, pTo - pFrom - 1 );
185 Advance( &pFrom, &pTo ); // -*-family-*
186 mnFamily = mpFactory->InsertFamily( pFrom, pTo - pFrom - 1 );
188 Advance( &pFrom, &pTo ); // -*-*-weight-*
189 mnWeight = mpFactory->InsertWeight( pFrom, pTo - pFrom - 1 );
191 Advance( &pFrom, &pTo ); //-*-*-*-slant-*
192 mnSlant = mpFactory->InsertSlant( pFrom, pTo - pFrom - 1 );
194 Advance( &pFrom, &pTo ); //-*-*-*-*-setwidth-*
195 mnSetwidth = mpFactory->InsertSetwidth( pFrom, pTo - pFrom - 1 );
197 Advance( &pFrom, &pTo ); //-*-*-*-*-*-Addstyle-*
198 mnAddstyle = mpFactory->InsertAddstyle( pFrom, pTo - pFrom - 1 );
200 Advance( &pFrom, &pTo ); //-*-*-*-*-*-*-height-*
201 mnPixelSize = atoi( pFrom );
203 Advance( &pFrom, &pTo ); //-*-*-*-*-*-*-*-pt height-*
204 mnPointSize = atoi( pFrom );
206 Advance( &pFrom, &pTo ); //-*-*-*-*-*-*-*-*-x resolution-*
207 mnResolutionX = atoi( pFrom );
209 Advance( &pFrom, &pTo ); //-*-*-*-*-*-*-*-*-*-y resolution-*
210 mnResolutionY = atoi( pFrom );
212 Advance( &pFrom, &pTo ); //-*-*-*-*-*-*-*-*-*-*-spacing-*
213 mcSpacing = pFrom == pTo ? '\0' : *pFrom;
215 Advance( &pFrom, &pTo ); //-*-*-*-*-*-*-*-*-*-*-*-average-*
216 mnAverageWidth = atoi( pFrom );
218 Advance( &pFrom, &pTo ); //-*-*-*-*-*-*-*-*-*-*-*-*-registry-encoding
219 const char* pTmp = pFrom;
220 Advance( &pTmp, &pTo );
221 mnCharset = mpFactory->InsertCharset( pFrom, pTo - pFrom );
223 // sanity check whether we have really found a valid XLFD, if not
224 // throw away the whole font, since we have no idea what parts of
225 // the XLFD contains the error.
226 if ( !(pTo > pFrom) )
227 return False;
229 // a non-empty family name is essential, since otherwise the font
230 // would match the "default font" #52299#
231 Attribute* pFamily = mpFactory->RetrieveFamily( mnFamily );
232 const char* pFamilyName = pFamily->GetName();
233 if ( pFamilyName[0] == '\0' )
234 return False;
236 // well done
237 return True;
240 #if OSL_DEBUG_LEVEL > 1
241 // pure debug for now: this is only to inspect/pretty print a Xlfd struct
242 const char*
243 Xlfd::ToString( ByteString &rString ) const
245 AppendAttribute( mpFactory->RetrieveFoundry(mnFoundry), rString );
246 AppendAttribute( mpFactory->RetrieveFamily(mnFamily), rString );
247 AppendAttribute( mpFactory->RetrieveWeight(mnWeight), rString );
248 AppendAttribute( mpFactory->RetrieveSlant(mnSlant), rString );
249 AppendAttribute( mpFactory->RetrieveSetwidth(mnSetwidth), rString );
250 AppendAttribute( mpFactory->RetrieveAddstyle(mnAddstyle), rString );
252 rString.Append("-"); rString.Append( ByteString::CreateFromInt32( mnPixelSize ) );
253 rString.Append("-"); rString.Append( ByteString::CreateFromInt32( mnPointSize ) );
254 rString.Append("-"); rString.Append( ByteString::CreateFromInt32( mnResolutionX ) );
255 rString.Append("-"); rString.Append( ByteString::CreateFromInt32( mnResolutionY ) );
256 rString.Append("-"); rString.Append( (char)mcSpacing );
257 rString.Append("-"); rString.Append( ByteString::CreateFromInt32( mnAverageWidth ) );
259 AppendAttribute( mpFactory->RetrieveCharset(mnCharset), rString );
261 return rString.GetBuffer() ;
264 void
265 Xlfd::Dump() const
267 ByteString aString;
268 fprintf(stderr, "Xlfd: %s\n", ToString(aString) );
270 #endif