Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/types.h>
21 :
22 : #include <rtl/ustring.hxx>
23 :
24 : #include "outdev.h"
25 : #include "PhysicalFontFace.hxx"
26 : #include "PhysicalFontCollection.hxx"
27 :
28 : #include "PhysicalFontFamily.hxx"
29 :
30 0 : void PhysicalFontFamily::CalcType( sal_uLong& rType, FontWeight& rWeight, FontWidth& rWidth,
31 : FontFamily eFamily, const utl::FontNameAttr* pFontAttr )
32 : {
33 0 : if ( eFamily != FAMILY_DONTKNOW )
34 : {
35 0 : if ( eFamily == FAMILY_SWISS )
36 0 : rType |= IMPL_FONT_ATTR_SANSSERIF;
37 0 : else if ( eFamily == FAMILY_ROMAN )
38 0 : rType |= IMPL_FONT_ATTR_SERIF;
39 0 : else if ( eFamily == FAMILY_SCRIPT )
40 0 : rType |= IMPL_FONT_ATTR_SCRIPT;
41 0 : else if ( eFamily == FAMILY_MODERN )
42 0 : rType |= IMPL_FONT_ATTR_FIXED;
43 0 : else if ( eFamily == FAMILY_DECORATIVE )
44 0 : rType |= IMPL_FONT_ATTR_DECORATIVE;
45 : }
46 :
47 0 : if ( pFontAttr )
48 : {
49 0 : rType |= pFontAttr->Type;
50 :
51 0 : if ( ((rWeight == WEIGHT_DONTKNOW) || (rWeight == WEIGHT_NORMAL)) &&
52 0 : (pFontAttr->Weight != WEIGHT_DONTKNOW) )
53 0 : rWeight = pFontAttr->Weight;
54 0 : if ( ((rWidth == WIDTH_DONTKNOW) || (rWidth == WIDTH_NORMAL)) &&
55 0 : (pFontAttr->Width != WIDTH_DONTKNOW) )
56 0 : rWidth = pFontAttr->Width;
57 : }
58 0 : }
59 :
60 0 : static unsigned lcl_IsCJKFont( const OUString& rFontName )
61 : {
62 : // Test, if Fontname includes CJK characters --> In this case we
63 : // mention that it is a CJK font
64 0 : for(int i = 0; i < rFontName.getLength(); i++)
65 : {
66 0 : const sal_Unicode ch = rFontName[i];
67 : // japanese
68 0 : if ( ((ch >= 0x3040) && (ch <= 0x30FF)) ||
69 0 : ((ch >= 0x3190) && (ch <= 0x319F)) )
70 0 : return IMPL_FONT_ATTR_CJK|IMPL_FONT_ATTR_CJK_JP;
71 :
72 : // korean
73 0 : if ( ((ch >= 0xAC00) && (ch <= 0xD7AF)) ||
74 0 : ((ch >= 0x3130) && (ch <= 0x318F)) ||
75 0 : ((ch >= 0x1100) && (ch <= 0x11FF)) )
76 0 : return IMPL_FONT_ATTR_CJK|IMPL_FONT_ATTR_CJK_KR;
77 :
78 : // chinese
79 0 : if ( ((ch >= 0x3400) && (ch <= 0x9FFF)) )
80 0 : return IMPL_FONT_ATTR_CJK|IMPL_FONT_ATTR_CJK_TC|IMPL_FONT_ATTR_CJK_SC;
81 :
82 : // cjk
83 0 : if ( ((ch >= 0x3000) && (ch <= 0xD7AF)) ||
84 0 : ((ch >= 0xFF00) && (ch <= 0xFFEE)) )
85 0 : return IMPL_FONT_ATTR_CJK;
86 :
87 : }
88 :
89 0 : return 0;
90 : }
91 :
92 0 : PhysicalFontFamily::PhysicalFontFamily( const OUString& rSearchName )
93 : : mpFirst( NULL ),
94 : maSearchName( rSearchName ),
95 : mnTypeFaces( 0 ),
96 : mnMatchType( 0 ),
97 : meMatchWeight( WEIGHT_DONTKNOW ),
98 : meMatchWidth( WIDTH_DONTKNOW ),
99 : meFamily( FAMILY_DONTKNOW ),
100 : mePitch( PITCH_DONTKNOW ),
101 0 : mnMinQuality( -1 )
102 0 : {}
103 :
104 0 : PhysicalFontFamily::~PhysicalFontFamily()
105 : {
106 : // release all physical font faces
107 0 : while( mpFirst )
108 : {
109 0 : PhysicalFontFace* pFace = mpFirst;
110 0 : mpFirst = pFace->GetNextFace();
111 0 : delete pFace;
112 : }
113 0 : }
114 :
115 0 : bool PhysicalFontFamily::AddFontFace( PhysicalFontFace* pNewData )
116 : {
117 0 : pNewData->mpNext = NULL;
118 :
119 0 : if( !mpFirst )
120 : {
121 0 : maName = pNewData->GetFamilyName();
122 0 : maMapNames = pNewData->maMapNames;
123 0 : meFamily = pNewData->GetFamilyType();
124 0 : mePitch = pNewData->GetPitch();
125 0 : mnMinQuality = pNewData->mnQuality;
126 : }
127 : else
128 : {
129 0 : if( meFamily == FAMILY_DONTKNOW )
130 0 : meFamily = pNewData->GetFamilyType();
131 0 : if( mePitch == PITCH_DONTKNOW )
132 0 : mePitch = pNewData->GetPitch();
133 0 : if( mnMinQuality > pNewData->mnQuality )
134 0 : mnMinQuality = pNewData->mnQuality;
135 : }
136 :
137 : // set attributes for attribute based font matching
138 0 : if( pNewData->IsScalable() )
139 0 : mnTypeFaces |= FONT_FAMILY_SCALABLE;
140 :
141 0 : if( pNewData->IsSymbolFont() )
142 0 : mnTypeFaces |= FONT_FAMILY_SYMBOL;
143 : else
144 0 : mnTypeFaces |= FONT_FAMILY_NONESYMBOL;
145 :
146 0 : if( pNewData->GetWeight() != WEIGHT_DONTKNOW )
147 : {
148 0 : if( pNewData->GetWeight() >= WEIGHT_SEMIBOLD )
149 0 : mnTypeFaces |= FONT_FAMILY_BOLD;
150 0 : else if( pNewData->GetWeight() <= WEIGHT_SEMILIGHT )
151 0 : mnTypeFaces |= FONT_FAMILY_LIGHT;
152 : else
153 0 : mnTypeFaces |= FONT_FAMILY_NORMAL;
154 : }
155 :
156 0 : if( pNewData->GetSlant() == ITALIC_NONE )
157 0 : mnTypeFaces |= FONT_FAMILY_NONEITALIC;
158 0 : else if( (pNewData->GetSlant() == ITALIC_NORMAL)
159 0 : || (pNewData->GetSlant() == ITALIC_OBLIQUE) )
160 0 : mnTypeFaces |= FONT_FAMILY_ITALIC;
161 :
162 0 : if( (meMatchWeight == WEIGHT_DONTKNOW)
163 0 : || (meMatchWidth == WIDTH_DONTKNOW)
164 0 : || (mnMatchType == 0) )
165 : {
166 : // TODO: is it cheaper to calc matching attributes now or on demand?
167 : // calc matching attributes if other entries are already initialized
168 :
169 : // Do lazy, quite expensive, not needed in start-up!
170 : // const FontSubstConfiguration& rFontSubst = *FontSubstConfiguration::get();
171 : // InitMatchData( rFontSubst, maSearchName );
172 : // mbMatchData=true; // Somewhere else???
173 : }
174 :
175 : // reassign name (sharing saves memory)
176 0 : if( pNewData->GetFamilyName() == GetFamilyName() )
177 0 : pNewData->SetFamilyName( GetFamilyName() );
178 :
179 : // insert new physical font face into linked list
180 : // TODO: get rid of linear search?
181 : PhysicalFontFace* pData;
182 0 : PhysicalFontFace** ppHere = &mpFirst;
183 0 : for(; (pData=*ppHere) != NULL; ppHere=&pData->mpNext )
184 : {
185 0 : sal_Int32 eComp = pNewData->CompareWithSize( *pData );
186 0 : if( eComp > 0 )
187 0 : continue;
188 0 : if( eComp < 0 )
189 0 : break;
190 :
191 : // ignore duplicate if its quality is worse
192 0 : if( pNewData->mnQuality < pData->mnQuality )
193 0 : return false;
194 :
195 : // keep the device font if its quality is good enough
196 0 : if( (pNewData->mnQuality == pData->mnQuality)
197 0 : && (pData->mbDevice || !pNewData->mbDevice) )
198 0 : return false;
199 :
200 : // replace existing font face with a better one
201 0 : pNewData->mpNext = pData->mpNext;
202 0 : *ppHere = pNewData;
203 0 : delete pData;
204 0 : return true;
205 : }
206 :
207 : // insert into or append to list of physical font faces
208 0 : pNewData->mpNext = pData;
209 0 : *ppHere = pNewData;
210 0 : return true;
211 : }
212 :
213 : // get font attributes using the normalized font family name
214 0 : void PhysicalFontFamily::InitMatchData( const utl::FontSubstConfiguration& rFontSubst,
215 : const OUString& rSearchName )
216 : {
217 0 : OUString aShortName;
218 0 : OUString aMatchFamilyName(maMatchFamilyName);
219 : // get font attributes from the decorated font name
220 : rFontSubst.getMapName( rSearchName, aShortName, aMatchFamilyName,
221 0 : meMatchWeight, meMatchWidth, mnMatchType );
222 0 : maMatchFamilyName = aMatchFamilyName;
223 0 : const utl::FontNameAttr* pFontAttr = rFontSubst.getSubstInfo( rSearchName );
224 : // eventually use the stripped name
225 0 : if( !pFontAttr )
226 0 : if( aShortName != rSearchName )
227 0 : pFontAttr = rFontSubst.getSubstInfo( aShortName );
228 0 : CalcType( mnMatchType, meMatchWeight, meMatchWidth, meFamily, pFontAttr );
229 0 : mnMatchType |= lcl_IsCJKFont( maName );
230 0 : }
231 :
232 0 : PhysicalFontFace* PhysicalFontFamily::FindBestFontFace( const FontSelectPattern& rFSD ) const
233 : {
234 0 : if( !mpFirst )
235 0 : return NULL;
236 0 : if( !mpFirst->GetNextFace() )
237 0 : return mpFirst;
238 :
239 : // FontName+StyleName should map to FamilyName+StyleName
240 0 : const OUString& rSearchName = rFSD.maTargetName;
241 0 : OUString aTargetStyleName;
242 0 : const OUString* pTargetStyleName = NULL;
243 0 : if( (rSearchName.getLength() > maSearchName.getLength())
244 0 : && rSearchName.startsWith( maSearchName ) )
245 : {
246 0 : aTargetStyleName = rSearchName.copy(maSearchName.getLength() + 1);
247 0 : pTargetStyleName = &aTargetStyleName;
248 : }
249 :
250 : // TODO: linear search improve!
251 0 : PhysicalFontFace* pFontFace = mpFirst;
252 0 : PhysicalFontFace* pBestFontFace = pFontFace;
253 0 : FontMatchStatus aFontMatchStatus = {0,0,0, pTargetStyleName};
254 0 : for(; pFontFace; pFontFace = pFontFace->GetNextFace() )
255 0 : if( pFontFace->IsBetterMatch( rFSD, aFontMatchStatus ) )
256 0 : pBestFontFace = pFontFace;
257 :
258 0 : return pBestFontFace;
259 : }
260 :
261 : // update device font list with unique font faces, with uniqueness
262 : // meaning different font attributes, but not different fonts sizes
263 0 : void PhysicalFontFamily::UpdateDevFontList( ImplGetDevFontList& rDevFontList ) const
264 : {
265 0 : PhysicalFontFace* pPrevFace = NULL;
266 0 : for( PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
267 : {
268 0 : if( !pPrevFace || pFace->CompareIgnoreSize( *pPrevFace ) )
269 0 : rDevFontList.Add( pFace );
270 0 : pPrevFace = pFace;
271 : }
272 0 : }
273 :
274 0 : void PhysicalFontFamily::GetFontHeights( std::set<int>& rHeights ) const
275 : {
276 : // add all available font heights
277 0 : for( const PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
278 0 : rHeights.insert( pFace->GetHeight() );
279 0 : }
280 :
281 0 : void PhysicalFontFamily::UpdateCloneFontList( PhysicalFontCollection& rFontCollection,
282 : bool bScalable, bool bEmbeddable ) const
283 : {
284 0 : for( PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
285 : {
286 0 : if( bScalable && !pFace->IsScalable() )
287 0 : continue;
288 0 : if( bEmbeddable && !pFace->IsEmbeddable() && !pFace->IsSubsettable() )
289 0 : continue;
290 :
291 0 : PhysicalFontFace* pClonedFace = pFace->Clone();
292 0 : rFontCollection.Add( pClonedFace );
293 : }
294 0 : }
295 :
296 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|