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 <tools/fontenum.hxx>
23 :
24 : #include "outfont.hxx"
25 :
26 : #include "PhysicalFontFace.hxx"
27 :
28 0 : PhysicalFontFace::PhysicalFontFace( const ImplDevFontAttributes& rDFA, int nMagic )
29 : : ImplDevFontAttributes( rDFA )
30 : , mnWidth(0)
31 : , mnHeight(0)
32 : , mnMagic( nMagic )
33 0 : , mpNext( NULL )
34 : {
35 : // StarSymbol is a unicode font, but it still deserves the symbol flag
36 0 : if( !IsSymbolFont() )
37 0 : if ( IsStarSymbol( GetFamilyName() ) )
38 0 : SetSymbolFlag( true );
39 0 : }
40 :
41 0 : sal_Int32 PhysicalFontFace::CompareIgnoreSize( const PhysicalFontFace& rOther ) const
42 : {
43 : // compare their width, weight, italic, style name and family name
44 0 : if( GetWidthType() < rOther.GetWidthType() )
45 0 : return -1;
46 0 : else if( GetWidthType() > rOther.GetWidthType() )
47 0 : return 1;
48 :
49 0 : if( GetWeight() < rOther.GetWeight() )
50 0 : return -1;
51 0 : else if( GetWeight() > rOther.GetWeight() )
52 0 : return 1;
53 :
54 0 : if( GetSlant() < rOther.GetSlant() )
55 0 : return -1;
56 0 : else if( GetSlant() > rOther.GetSlant() )
57 0 : return 1;
58 :
59 0 : sal_Int32 nRet = GetFamilyName().compareTo( rOther.GetFamilyName() );
60 :
61 0 : if (nRet == 0)
62 : {
63 0 : nRet = GetStyleName().compareTo( rOther.GetStyleName() );
64 : }
65 :
66 0 : return nRet;
67 : }
68 :
69 0 : sal_Int32 PhysicalFontFace::CompareWithSize( const PhysicalFontFace& rOther ) const
70 : {
71 0 : sal_Int32 nCompare = CompareIgnoreSize( rOther );
72 0 : if (nCompare != 0)
73 0 : return nCompare;
74 :
75 0 : if( mnHeight < rOther.mnHeight )
76 0 : return -1;
77 0 : else if( mnHeight > rOther.mnHeight )
78 0 : return 1;
79 :
80 0 : if( mnWidth < rOther.mnWidth )
81 0 : return -1;
82 0 : else if( mnWidth > rOther.mnWidth )
83 0 : return 1;
84 :
85 0 : return 0;
86 : }
87 :
88 0 : bool PhysicalFontFace::IsBetterMatch( const FontSelectPattern& rFSD, FontMatchStatus& rStatus ) const
89 : {
90 0 : int nMatch = 0;
91 :
92 0 : const OUString& rFontName = rFSD.maTargetName;
93 0 : if( rFontName.equalsIgnoreAsciiCase( GetFamilyName() ) )
94 0 : nMatch += 240000;
95 :
96 0 : if( rStatus.mpTargetStyleName
97 0 : && GetStyleName().equalsIgnoreAsciiCase( *rStatus.mpTargetStyleName ) )
98 0 : nMatch += 120000;
99 :
100 0 : if( (rFSD.GetPitch() != PITCH_DONTKNOW) && (rFSD.GetPitch() == GetPitch()) )
101 0 : nMatch += 20000;
102 :
103 : // prefer NORMAL font width
104 : // TODO: change when the upper layers can tell their width preference
105 0 : if( GetWidthType() == WIDTH_NORMAL )
106 0 : nMatch += 400;
107 0 : else if( (GetWidthType() == WIDTH_SEMI_EXPANDED) || (GetWidthType() == WIDTH_SEMI_CONDENSED) )
108 0 : nMatch += 300;
109 :
110 0 : if( rFSD.GetWeight() != WEIGHT_DONTKNOW )
111 : {
112 : // if not bold or requiring emboldening prefer light fonts to bold fonts
113 0 : FontWeight ePatternWeight = rFSD.mbEmbolden ? WEIGHT_NORMAL : rFSD.GetWeight();
114 :
115 0 : int nReqWeight = (int)ePatternWeight;
116 0 : if ( ePatternWeight > WEIGHT_MEDIUM )
117 0 : nReqWeight += 100;
118 :
119 0 : int nGivenWeight = (int)GetWeight();
120 0 : if( GetWeight() > WEIGHT_MEDIUM )
121 0 : nGivenWeight += 100;
122 :
123 0 : int nWeightDiff = nReqWeight - nGivenWeight;
124 :
125 0 : if ( nWeightDiff == 0 )
126 0 : nMatch += 1000;
127 0 : else if ( nWeightDiff == +1 || nWeightDiff == -1 )
128 0 : nMatch += 700;
129 0 : else if ( nWeightDiff < +50 && nWeightDiff > -50)
130 0 : nMatch += 200;
131 : }
132 : else // requested weight == WEIGHT_DONTKNOW
133 : {
134 : // prefer NORMAL font weight
135 : // TODO: change when the upper layers can tell their weight preference
136 0 : if( GetWeight() == WEIGHT_NORMAL )
137 0 : nMatch += 450;
138 0 : else if( GetWeight() == WEIGHT_MEDIUM )
139 0 : nMatch += 350;
140 0 : else if( (GetWeight() == WEIGHT_SEMILIGHT) || (GetWeight() == WEIGHT_SEMIBOLD) )
141 0 : nMatch += 200;
142 0 : else if( GetWeight() == WEIGHT_LIGHT )
143 0 : nMatch += 150;
144 : }
145 :
146 : // if requiring custom matrix to fake italic, prefer upright font
147 0 : FontItalic ePatternItalic = rFSD.maItalicMatrix != ItalicMatrix() ? ITALIC_NONE : rFSD.GetSlant();
148 :
149 0 : if ( ePatternItalic == ITALIC_NONE )
150 : {
151 0 : if( GetSlant() == ITALIC_NONE )
152 0 : nMatch += 900;
153 : }
154 : else
155 : {
156 0 : if( ePatternItalic == GetSlant() )
157 0 : nMatch += 900;
158 0 : else if( GetSlant() != ITALIC_NONE )
159 0 : nMatch += 600;
160 : }
161 :
162 0 : if( mbDevice )
163 0 : nMatch += 1;
164 :
165 0 : int nHeightMatch = 0;
166 0 : int nWidthMatch = 0;
167 :
168 0 : if( IsScalable() )
169 : {
170 0 : if( rFSD.mnOrientation != 0 )
171 0 : nMatch += 80;
172 0 : else if( rFSD.mnWidth != 0 )
173 0 : nMatch += 25;
174 : else
175 0 : nMatch += 5;
176 : }
177 : else
178 : {
179 0 : if( rFSD.mnHeight == mnHeight )
180 : {
181 0 : nMatch += 20;
182 0 : if( rFSD.mnWidth == mnWidth )
183 0 : nMatch += 10;
184 : }
185 : else
186 : {
187 : // for non-scalable fonts the size difference is very important
188 : // prefer the smaller font face because of clipping/overlapping issues
189 0 : int nHeightDiff = (rFSD.mnHeight - mnHeight) * 1000;
190 0 : nHeightMatch = (nHeightDiff >= 0) ? -nHeightDiff : 100+nHeightDiff;
191 0 : if( rFSD.mnHeight )
192 0 : nHeightMatch /= rFSD.mnHeight;
193 :
194 0 : if( (rFSD.mnWidth != 0) && (mnWidth != 0) && (rFSD.mnWidth != mnWidth) )
195 : {
196 0 : int nWidthDiff = (rFSD.mnWidth - mnWidth) * 100;
197 0 : nWidthMatch = (nWidthDiff >= 0) ? -nWidthDiff : +nWidthDiff;
198 : }
199 : }
200 : }
201 :
202 0 : if( rStatus.mnFaceMatch > nMatch )
203 0 : return false;
204 0 : else if( rStatus.mnFaceMatch < nMatch )
205 : {
206 0 : rStatus.mnFaceMatch = nMatch;
207 0 : rStatus.mnHeightMatch = nHeightMatch;
208 0 : rStatus.mnWidthMatch = nWidthMatch;
209 0 : return true;
210 : }
211 :
212 : // when two fonts are still competing prefer the
213 : // one with the best matching height
214 0 : if( rStatus.mnHeightMatch > nHeightMatch )
215 0 : return false;
216 0 : else if( rStatus.mnHeightMatch < nHeightMatch )
217 : {
218 0 : rStatus.mnHeightMatch = nHeightMatch;
219 0 : rStatus.mnWidthMatch = nWidthMatch;
220 0 : return true;
221 : }
222 :
223 0 : if( rStatus.mnWidthMatch > nWidthMatch )
224 0 : return false;
225 :
226 0 : rStatus.mnWidthMatch = nWidthMatch;
227 0 : return true;
228 : }
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|