Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: xfa/src/fdp/src/css/fde_cssstyleselector.cpp

Issue 1730753002: Simplify CFDE_CSSStyleSelector::SortRulesTo(). (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « xfa/src/fdp/src/css/fde_cssstyleselector.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "xfa/src/fdp/src/css/fde_csscache.h" 7 #include "xfa/src/fdp/src/css/fde_csscache.h"
8 #include "xfa/src/fdp/src/css/fde_cssdeclaration.h" 8 #include "xfa/src/fdp/src/css/fde_cssdeclaration.h"
9 #include "xfa/src/fdp/src/css/fde_cssstyleselector.h" 9 #include "xfa/src/fdp/src/css/fde_cssstyleselector.h"
10 #include "xfa/src/foxitlib.h" 10 #include "xfa/src/foxitlib.h"
11
11 int32_t CFDE_CSSCounterStyle::FindIndex(const FX_WCHAR* pszIdentifier) { 12 int32_t CFDE_CSSCounterStyle::FindIndex(const FX_WCHAR* pszIdentifier) {
12 int32_t iCount = m_arrCounterData.GetSize(); 13 int32_t iCount = m_arrCounterData.GetSize();
13 for (int32_t i = 0; i < iCount; i++) { 14 for (int32_t i = 0; i < iCount; i++) {
14 if (FXSYS_wcscmp(pszIdentifier, m_arrCounterData.ElementAt(i).m_pszIdent) == 15 if (FXSYS_wcscmp(pszIdentifier, m_arrCounterData.ElementAt(i).m_pszIdent) ==
15 0) { 16 0) {
16 return i; 17 return i;
17 } 18 }
18 } 19 }
19 return -1; 20 return -1;
20 } 21 }
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 IFDE_CSSDeclaration* pDecl) { 182 IFDE_CSSDeclaration* pDecl) {
182 void* pKey = (void*)(uintptr_t)dwKey; 183 void* pKey = (void*)(uintptr_t)dwKey;
183 FDE_CSSRULEDATA* pData = NewRuleData(pSel, pDecl); 184 FDE_CSSRULEDATA* pData = NewRuleData(pSel, pDecl);
184 FDE_CSSRULEDATA* pList = NULL; 185 FDE_CSSRULEDATA* pList = NULL;
185 if (!map.Lookup(pKey, (void*&)pList)) { 186 if (!map.Lookup(pKey, (void*&)pList)) {
186 map.SetAt(pKey, pData); 187 map.SetAt(pKey, pData);
187 } else if (AddRuleTo(pList, pData)) { 188 } else if (AddRuleTo(pList, pData)) {
188 map.SetAt(pKey, pList); 189 map.SetAt(pKey, pList);
189 } 190 }
190 } 191 }
191 inline FX_BOOL CFDE_CSSRuleCollection::AddRuleTo(FDE_CSSRULEDATA*& pList, 192
192 FDE_CSSRULEDATA* pData) { 193 FX_BOOL CFDE_CSSRuleCollection::AddRuleTo(FDE_CSSRULEDATA*& pList,
193 if (pList == NULL) { 194 FDE_CSSRULEDATA* pData) {
194 pList = pData; 195 if (pList) {
195 return TRUE;
196 } else {
197 pData->pNext = pList->pNext; 196 pData->pNext = pList->pNext;
198 pList->pNext = pData; 197 pList->pNext = pData;
199 return FALSE; 198 return FALSE;
200 } 199 }
200
201 pList = pData;
202 return TRUE;
201 } 203 }
202 inline FDE_CSSRULEDATA* CFDE_CSSRuleCollection::NewRuleData( 204
205 FDE_CSSRULEDATA* CFDE_CSSRuleCollection::NewRuleData(
203 IFDE_CSSSelector* pSel, 206 IFDE_CSSSelector* pSel,
204 IFDE_CSSDeclaration* pDecl) { 207 IFDE_CSSDeclaration* pDecl) {
205 return FDE_NewWith(m_pStaticStore) 208 return FDE_NewWith(m_pStaticStore)
206 FDE_CSSRULEDATA(pSel, pDecl, ++m_iSelectors); 209 FDE_CSSRULEDATA(pSel, pDecl, ++m_iSelectors);
207 } 210 }
211
208 IFDE_CSSStyleSelector* IFDE_CSSStyleSelector::Create() { 212 IFDE_CSSStyleSelector* IFDE_CSSStyleSelector::Create() {
209 return new CFDE_CSSStyleSelector; 213 return new CFDE_CSSStyleSelector;
210 } 214 }
211 CFDE_CSSStyleSelector::CFDE_CSSStyleSelector() 215 CFDE_CSSStyleSelector::CFDE_CSSStyleSelector()
212 : m_pFontMgr(NULL), 216 : m_pFontMgr(NULL),
213 m_fDefFontSize(12.0f), 217 m_fDefFontSize(12.0f),
214 m_pRuleDataStore(NULL), 218 m_pRuleDataStore(NULL),
215 m_pInlineStyleStore(NULL), 219 m_pInlineStyleStore(NULL),
216 m_pFixedStyleStore(NULL), 220 m_pFixedStyleStore(NULL),
217 m_pAccelerator(NULL) { 221 m_pAccelerator(NULL) {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 } 338 }
335 int32_t iCount = pCache->CountHashClass(); 339 int32_t iCount = pCache->CountHashClass();
336 for (int32_t i = 0; i < iCount; i++) { 340 for (int32_t i = 0; i < iCount; i++) {
337 pCache->SetClassIndex(i); 341 pCache->SetClassIndex(i);
338 MatchRules(pCache, rules.GetClassRuleData(pCache->HashClass()), 342 MatchRules(pCache, rules.GetClassRuleData(pCache->HashClass()),
339 ePersudoType); 343 ePersudoType);
340 } 344 }
341 } else { 345 } else {
342 MatchRules(pCache, rules.GetPersudoRuleData(), ePersudoType); 346 MatchRules(pCache, rules.GetPersudoRuleData(), ePersudoType);
343 } 347 }
344 if (m_MatchedRules.GetSize() > 0) { 348 if (!m_MatchedRules.empty()) {
Tom Sepez 2016/02/23 23:34:02 Do we still need this if? begin() and end() on an
Lei Zhang 2016/02/24 00:06:35 Nope.
345 SortRulesTo(matchedDecls); 349 std::sort(m_MatchedRules.begin(), m_MatchedRules.end(),
346 m_MatchedRules.RemoveAt(0, m_MatchedRules.GetSize()); 350 [](const FDE_CSSRULEDATA* p1, const FDE_CSSRULEDATA* p2) {
351 return p1->dwPriority < p2->dwPriority;
352 });
353 for (const auto& rule : m_MatchedRules)
354 matchedDecls.Add(rule->pDeclaration);
355 m_MatchedRules.clear();
347 } 356 }
348 } 357 }
349 return matchedDecls.GetSize(); 358 return matchedDecls.GetSize();
350 } 359 }
351 inline void CFDE_CSSStyleSelector::MatchRules(FDE_CSSTAGCACHE* pCache, 360
352 FDE_CSSRULEDATA* pList, 361 void CFDE_CSSStyleSelector::MatchRules(FDE_CSSTAGCACHE* pCache,
353 FDE_CSSPERSUDO ePersudoType) { 362 FDE_CSSRULEDATA* pList,
354 while (pList != NULL) { 363 FDE_CSSPERSUDO ePersudoType) {
355 if (MatchSelector(pCache, pList->pSelector, ePersudoType)) { 364 while (pList) {
356 m_MatchedRules.Add(pList); 365 if (MatchSelector(pCache, pList->pSelector, ePersudoType))
357 } 366 m_MatchedRules.push_back(pList);
358 pList = pList->pNext; 367 pList = pList->pNext;
359 } 368 }
360 } 369 }
370
361 FX_BOOL CFDE_CSSStyleSelector::MatchSelector(FDE_CSSTAGCACHE* pCache, 371 FX_BOOL CFDE_CSSStyleSelector::MatchSelector(FDE_CSSTAGCACHE* pCache,
362 IFDE_CSSSelector* pSel, 372 IFDE_CSSSelector* pSel,
363 FDE_CSSPERSUDO ePersudoType) { 373 FDE_CSSPERSUDO ePersudoType) {
364 FX_DWORD dwHash; 374 FX_DWORD dwHash;
365 while (pSel != NULL && pCache != NULL) { 375 while (pSel != NULL && pCache != NULL) {
366 switch (pSel->GetType()) { 376 switch (pSel->GetType()) {
367 case FDE_CSSSELECTORTYPE_Descendant: 377 case FDE_CSSSELECTORTYPE_Descendant:
368 dwHash = pSel->GetNameHash(); 378 dwHash = pSel->GetNameHash();
369 while ((pCache = pCache->GetParent()) != NULL) { 379 while ((pCache = pCache->GetParent()) != NULL) {
370 if (dwHash != FDE_CSSUNIVERSALHASH && dwHash != pCache->HashTag()) { 380 if (dwHash != FDE_CSSUNIVERSALHASH && dwHash != pCache->HashTag()) {
(...skipping 29 matching lines...) Expand all
400 } 410 }
401 break; 411 break;
402 default: 412 default:
403 FXSYS_assert(FALSE); 413 FXSYS_assert(FALSE);
404 break; 414 break;
405 } 415 }
406 pSel = pSel->GetNextSelector(); 416 pSel = pSel->GetNextSelector();
407 } 417 }
408 return pSel == NULL && pCache != NULL; 418 return pSel == NULL && pCache != NULL;
409 } 419 }
410 void CFDE_CSSStyleSelector::SortRulesTo(CFDE_CSSDeclarationArray& matchDecls) { 420
411 for (int32_t j = m_MatchedRules.GetUpperBound(); j >= 0; --j) {
412 FDE_CSSRULEDATA*& pMin = m_MatchedRules.ElementAt(j);
413 FX_DWORD dwMin = pMin->dwPriority;
414 for (int32_t i = j - 1; i >= 0; --i) {
415 FDE_CSSRULEDATA*& pCur = m_MatchedRules.ElementAt(i);
416 if (dwMin > pCur->dwPriority) {
417 dwMin = pCur->dwPriority;
418 FDE_CSSRULEDATA* p = pMin;
419 pMin = pCur;
420 pCur = p;
421 }
422 }
423 matchDecls.Add(pMin->pDeclaration);
424 }
425 }
426 void CFDE_CSSStyleSelector::ComputeStyle( 421 void CFDE_CSSStyleSelector::ComputeStyle(
427 IFDE_CSSTagProvider* pTag, 422 IFDE_CSSTagProvider* pTag,
428 const IFDE_CSSDeclaration** ppDeclArray, 423 const IFDE_CSSDeclaration** ppDeclArray,
429 int32_t iDeclCount, 424 int32_t iDeclCount,
430 IFDE_CSSComputedStyle* pDestStyle) { 425 IFDE_CSSComputedStyle* pDestStyle) {
431 FXSYS_assert(iDeclCount >= 0); 426 FXSYS_assert(iDeclCount >= 0);
432 FXSYS_assert(pDestStyle); 427 FXSYS_assert(pDestStyle);
433 FX_POSITION pos = pTag->GetFirstAttribute(); 428 FX_POSITION pos = pTag->GetFirstAttribute();
434 if (pos != NULL) { 429 if (pos != NULL) {
435 if (m_pInlineStyleStore == NULL) { 430 if (m_pInlineStyleStore == NULL) {
(...skipping 1353 matching lines...) Expand 10 before | Expand all | Expand 10 after
1789 return FDE_CSSTEXTTRANSFORM_LowerCase; 1784 return FDE_CSSTEXTTRANSFORM_LowerCase;
1790 default: 1785 default:
1791 return FDE_CSSTEXTTRANSFORM_None; 1786 return FDE_CSSTEXTTRANSFORM_None;
1792 } 1787 }
1793 } 1788 }
1794 FDE_CSSFONTVARIANT CFDE_CSSStyleSelector::ToFontVariant( 1789 FDE_CSSFONTVARIANT CFDE_CSSStyleSelector::ToFontVariant(
1795 FDE_CSSPROPERTYVALUE eValue) { 1790 FDE_CSSPROPERTYVALUE eValue) {
1796 return eValue == FDE_CSSPROPERTYVALUE_SmallCaps ? FDE_CSSFONTVARIANT_SmallCaps 1791 return eValue == FDE_CSSPROPERTYVALUE_SmallCaps ? FDE_CSSFONTVARIANT_SmallCaps
1797 : FDE_CSSFONTVARIANT_Normal; 1792 : FDE_CSSFONTVARIANT_Normal;
1798 } 1793 }
OLDNEW
« no previous file with comments | « xfa/src/fdp/src/css/fde_cssstyleselector.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698