From fc2cdf8657534467fd807e216d50650b0e959868 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Thu, 19 May 2016 18:07:11 -0700 Subject: fgas/ code cleanup. This CL shuffles code around in the fgas/ headers, removes unused functions and adds anonymous namepaces for static methods and data. Review-Url: https://codereview.chromium.org/1992033002 --- xfa/fde/css/fde_cssdatatable.cpp | 73 ++++++++++++++++++------------- xfa/fde/css/fde_cssdatatable_unittest.cpp | 60 +++++++++++++++++++++++++ xfa/fde/css/fde_csssyntax.cpp | 2 +- 3 files changed, 103 insertions(+), 32 deletions(-) create mode 100644 xfa/fde/css/fde_cssdatatable_unittest.cpp (limited to 'xfa/fde') diff --git a/xfa/fde/css/fde_cssdatatable.cpp b/xfa/fde/css/fde_cssdatatable.cpp index 3b090a2699..db4302b9fc 100644 --- a/xfa/fde/css/fde_cssdatatable.cpp +++ b/xfa/fde/css/fde_cssdatatable.cpp @@ -11,6 +11,14 @@ #include "xfa/fgas/crt/fgas_codepage.h" #include "xfa/fgas/crt/fgas_system.h" +namespace { + +uint8_t Hex2Dec(uint8_t hexHigh, uint8_t hexLow) { + return (FXSYS_toHexDigit(hexHigh) << 4) + FXSYS_toHexDigit(hexLow); +} + +} // namespace + FX_BOOL FDE_CSSLengthToFloat(const FDE_CSSLENGTH& len, FX_FLOAT fPercentBase, FX_FLOAT& fResult) { @@ -666,16 +674,17 @@ FDE_LPCCSSCOLORTABLE FDE_GetCSSColorByName(const CFX_WideStringC& wsName) { } while (iStart <= iEnd); return NULL; } + FX_BOOL FDE_ParseCSSNumber(const FX_WCHAR* pszValue, int32_t iValueLen, FX_FLOAT& fValue, FDE_CSSPRIMITIVETYPE& eUnit) { - ASSERT(pszValue != NULL && iValueLen > 0); + ASSERT(pszValue && iValueLen > 0); int32_t iUsedLen = 0; fValue = FX_wcstof(pszValue, iValueLen, &iUsedLen); - if (iUsedLen <= 0) { + if (iUsedLen <= 0) return FALSE; - } + iValueLen -= iUsedLen; pszValue += iUsedLen; eUnit = FDE_CSSPRIMITIVETYPE_Number; @@ -684,9 +693,8 @@ FX_BOOL FDE_ParseCSSNumber(const FX_WCHAR* pszValue, } else if (iValueLen == 2) { FDE_LPCCSSLENGTHUNITTABLE pUnit = FDE_GetCSSLengthUnitByName(CFX_WideStringC(pszValue, 2)); - if (pUnit != NULL) { + if (pUnit) eUnit = (FDE_CSSPRIMITIVETYPE)pUnit->wValue; - } } return TRUE; } @@ -726,57 +734,60 @@ FX_BOOL FDE_ParseCSSURI(const FX_WCHAR* pszValue, FX_BOOL FDE_ParseCSSColor(const FX_WCHAR* pszValue, int32_t iValueLen, FX_ARGB& dwColor) { - ASSERT(pszValue != NULL && iValueLen > 0); + ASSERT(pszValue && iValueLen > 0); + if (*pszValue == '#') { switch (iValueLen) { case 4: { - uint8_t red = FX_Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[1]); - uint8_t green = FX_Hex2Dec((uint8_t)pszValue[2], (uint8_t)pszValue[2]); - uint8_t blue = FX_Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[3]); + uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[1]); + uint8_t green = Hex2Dec((uint8_t)pszValue[2], (uint8_t)pszValue[2]); + uint8_t blue = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[3]); dwColor = ArgbEncode(255, red, green, blue); - } return TRUE; + } case 7: { - uint8_t red = FX_Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[2]); - uint8_t green = FX_Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[4]); - uint8_t blue = FX_Hex2Dec((uint8_t)pszValue[5], (uint8_t)pszValue[6]); + uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[2]); + uint8_t green = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[4]); + uint8_t blue = Hex2Dec((uint8_t)pszValue[5], (uint8_t)pszValue[6]); dwColor = ArgbEncode(255, red, green, blue); - } return TRUE; + } + default: + return FALSE; } - } else if (iValueLen >= 10) { - if (pszValue[iValueLen - 1] != ')' || FX_wcsnicmp(L"rgb(", pszValue, 4)) { + } + + if (iValueLen >= 10) { + if (pszValue[iValueLen - 1] != ')' || FX_wcsnicmp(L"rgb(", pszValue, 4)) return FALSE; - } + uint8_t rgb[3] = {0}; FX_FLOAT fValue; FDE_CSSPRIMITIVETYPE eType; CFDE_CSSValueListParser list(pszValue + 4, iValueLen - 5, ','); for (int32_t i = 0; i < 3; ++i) { - if (!list.NextValue(eType, pszValue, iValueLen)) { + if (!list.NextValue(eType, pszValue, iValueLen)) return FALSE; - } - if (eType != FDE_CSSPRIMITIVETYPE_Number) { + if (eType != FDE_CSSPRIMITIVETYPE_Number) return FALSE; - } - if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eType)) { + if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eType)) return FALSE; - } + rgb[i] = eType == FDE_CSSPRIMITIVETYPE_Percent ? FXSYS_round(fValue * 2.55f) : FXSYS_round(fValue); } dwColor = ArgbEncode(255, rgb[0], rgb[1], rgb[2]); return TRUE; - } else { - FDE_LPCCSSCOLORTABLE pColor = - FDE_GetCSSColorByName(CFX_WideStringC(pszValue, iValueLen)); - if (pColor != NULL) { - dwColor = pColor->dwValue; - return TRUE; - } } - return FALSE; + + FDE_LPCCSSCOLORTABLE pColor = + FDE_GetCSSColorByName(CFX_WideStringC(pszValue, iValueLen)); + if (!pColor) + return FALSE; + + dwColor = pColor->dwValue; + return TRUE; } CFDE_CSSValueList::CFDE_CSSValueList(IFX_MemoryAllocator* pStaticStore, diff --git a/xfa/fde/css/fde_cssdatatable_unittest.cpp b/xfa/fde/css/fde_cssdatatable_unittest.cpp new file mode 100644 index 0000000000..d602bff046 --- /dev/null +++ b/xfa/fde/css/fde_cssdatatable_unittest.cpp @@ -0,0 +1,60 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "xfa/fde/css/fde_cssdatatable.h" + +#include "testing/gtest/include/gtest/gtest.h" + +TEST(FDE_ParseCSSColor, HexEncodingParsing) { + FX_ARGB color; + + // Length value invalid. + EXPECT_FALSE(FDE_ParseCSSColor(L"#000", 3, color)); + EXPECT_FALSE(FDE_ParseCSSColor(L"#000000", 5, color)); + EXPECT_FALSE(FDE_ParseCSSColor(L"#000000", 8, color)); + + // Invalid characters + EXPECT_TRUE(FDE_ParseCSSColor(L"#zxytlm", 7, color)); + EXPECT_EQ(0, FXARGB_R(color)); + EXPECT_EQ(0, FXARGB_G(color)); + EXPECT_EQ(0, FXARGB_B(color)); + + EXPECT_TRUE(FDE_ParseCSSColor(L"#000", 4, color)); + EXPECT_EQ(0, FXARGB_R(color)); + EXPECT_EQ(0, FXARGB_G(color)); + EXPECT_EQ(0, FXARGB_B(color)); + + EXPECT_TRUE(FDE_ParseCSSColor(L"#FFF", 4, color)); + EXPECT_EQ(255, FXARGB_R(color)); + EXPECT_EQ(255, FXARGB_G(color)); + EXPECT_EQ(255, FXARGB_B(color)); + + EXPECT_TRUE(FDE_ParseCSSColor(L"#F0F0F0", 7, color)); + EXPECT_EQ(240, FXARGB_R(color)); + EXPECT_EQ(240, FXARGB_G(color)); + EXPECT_EQ(240, FXARGB_B(color)); + + // Upper and lower case characters. + EXPECT_TRUE(FDE_ParseCSSColor(L"#1b2F3c", 7, color)); + EXPECT_EQ(27, FXARGB_R(color)); + EXPECT_EQ(47, FXARGB_G(color)); + EXPECT_EQ(60, FXARGB_B(color)); +} + +TEST(FDE_ParseCSSColor, RGBEncodingParsing) { + FX_ARGB color; + + // Invalid input for rgb() syntax. + EXPECT_FALSE(FDE_ParseCSSColor(L"blahblahblah", 11, color)); + + EXPECT_TRUE(FDE_ParseCSSColor(L"rgb(0, 0, 0)", 12, color)); + EXPECT_EQ(0, FXARGB_R(color)); + EXPECT_EQ(0, FXARGB_G(color)); + EXPECT_EQ(0, FXARGB_B(color)); + + EXPECT_TRUE(FDE_ParseCSSColor(L"rgb(128,255,48)", 15, color)); + EXPECT_EQ(128, FXARGB_R(color)); + EXPECT_EQ(255, FXARGB_G(color)); + EXPECT_EQ(48, FXARGB_B(color)); +} diff --git a/xfa/fde/css/fde_csssyntax.cpp b/xfa/fde/css/fde_csssyntax.cpp index 26ddaf727a..f948b58e71 100644 --- a/xfa/fde/css/fde_csssyntax.cpp +++ b/xfa/fde/css/fde_csssyntax.cpp @@ -334,7 +334,7 @@ FDE_CSSSYNTAXSTATUS CFDE_CSSSyntaxParser::DoSyntaxParse() { DisableCharset(); if (m_iTextDatLen > 0) { if (m_pStream != NULL) { - uint16_t wCodePage = FX_GetCodePageFormStringW( + uint16_t wCodePage = FX_GetCodePageFromStringW( m_TextData.GetBuffer(), m_iTextDatLen); if (wCodePage < 0xFFFF && m_pStream->GetCodePage() != wCodePage) { -- cgit v1.2.3