| OLD | NEW |
| 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/fde/css/fde_cssdatatable.h" | 7 #include "xfa/fde/css/fde_cssdatatable.h" |
| 8 | 8 |
| 9 #include "core/fxcrt/fx_ext.h" | 9 #include "core/fxcrt/fx_ext.h" |
| 10 #include "xfa/fgas/crt/fgas_codepage.h" | 10 #include "xfa/fgas/crt/fgas_codepage.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 uint8_t Hex2Dec(uint8_t hexHigh, uint8_t hexLow) { | 14 uint8_t Hex2Dec(uint8_t hexHigh, uint8_t hexLow) { |
| 15 return (FXSYS_toHexDigit(hexHigh) << 4) + FXSYS_toHexDigit(hexLow); | 15 return (FXSYS_toHexDigit(hexHigh) << 4) + FXSYS_toHexDigit(hexLow); |
| 16 } | 16 } |
| 17 | 17 |
| 18 } // namespace | 18 } // namespace |
| 19 | 19 |
| 20 FX_BOOL FDE_CSSLengthToFloat(const FDE_CSSLENGTH& len, | 20 bool FDE_CSSLengthToFloat(const FDE_CSSLENGTH& len, |
| 21 FX_FLOAT fPercentBase, | 21 FX_FLOAT fPercentBase, |
| 22 FX_FLOAT& fResult) { | 22 FX_FLOAT& fResult) { |
| 23 switch (len.GetUnit()) { | 23 switch (len.GetUnit()) { |
| 24 case FDE_CSSLENGTHUNIT_Point: | 24 case FDE_CSSLENGTHUNIT_Point: |
| 25 fResult = len.GetValue(); | 25 fResult = len.GetValue(); |
| 26 return TRUE; | 26 return true; |
| 27 case FDE_CSSLENGTHUNIT_Percent: | 27 case FDE_CSSLENGTHUNIT_Percent: |
| 28 fResult = len.GetValue() * fPercentBase; | 28 fResult = len.GetValue() * fPercentBase; |
| 29 return TRUE; | 29 return true; |
| 30 default: | 30 default: |
| 31 return FALSE; | 31 return false; |
| 32 } | 32 } |
| 33 } | 33 } |
| 34 CFX_FloatRect FDE_CSSBoundaryToRect(IFDE_CSSBoundaryStyle* pBoundStyle, | 34 CFX_FloatRect FDE_CSSBoundaryToRect(IFDE_CSSBoundaryStyle* pBoundStyle, |
| 35 FX_FLOAT fContainerWidth, | 35 FX_FLOAT fContainerWidth, |
| 36 FX_BOOL bPadding, | 36 bool bPadding, |
| 37 FX_BOOL bBorder, | 37 bool bBorder, |
| 38 FX_BOOL bMargin) { | 38 bool bMargin) { |
| 39 FX_FLOAT fResult; | 39 FX_FLOAT fResult; |
| 40 const FDE_CSSRECT* pRect; | 40 const FDE_CSSRECT* pRect; |
| 41 CFX_FloatRect rect(0, 0, 0, 0); | 41 CFX_FloatRect rect(0, 0, 0, 0); |
| 42 if (bPadding) { | 42 if (bPadding) { |
| 43 pRect = pBoundStyle->GetPaddingWidth(); | 43 pRect = pBoundStyle->GetPaddingWidth(); |
| 44 if (pRect) { | 44 if (pRect) { |
| 45 if (FDE_CSSLengthToFloat(pRect->left, fContainerWidth, fResult)) { | 45 if (FDE_CSSLengthToFloat(pRect->left, fContainerWidth, fResult)) { |
| 46 rect.left += fResult; | 46 rect.left += fResult; |
| 47 } | 47 } |
| 48 if (FDE_CSSLengthToFloat(pRect->top, fContainerWidth, fResult)) { | 48 if (FDE_CSSLengthToFloat(pRect->top, fContainerWidth, fResult)) { |
| (...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 return g_FDE_CSSColors + iMid; | 665 return g_FDE_CSSColors + iMid; |
| 666 } else if (dwHash > dwMid) { | 666 } else if (dwHash > dwMid) { |
| 667 iStart = iMid + 1; | 667 iStart = iMid + 1; |
| 668 } else { | 668 } else { |
| 669 iEnd = iMid - 1; | 669 iEnd = iMid - 1; |
| 670 } | 670 } |
| 671 } while (iStart <= iEnd); | 671 } while (iStart <= iEnd); |
| 672 return nullptr; | 672 return nullptr; |
| 673 } | 673 } |
| 674 | 674 |
| 675 FX_BOOL FDE_ParseCSSNumber(const FX_WCHAR* pszValue, | 675 bool FDE_ParseCSSNumber(const FX_WCHAR* pszValue, |
| 676 int32_t iValueLen, | 676 int32_t iValueLen, |
| 677 FX_FLOAT& fValue, | 677 FX_FLOAT& fValue, |
| 678 FDE_CSSPRIMITIVETYPE& eUnit) { | 678 FDE_CSSPRIMITIVETYPE& eUnit) { |
| 679 ASSERT(pszValue && iValueLen > 0); | 679 ASSERT(pszValue && iValueLen > 0); |
| 680 int32_t iUsedLen = 0; | 680 int32_t iUsedLen = 0; |
| 681 fValue = FXSYS_wcstof(pszValue, iValueLen, &iUsedLen); | 681 fValue = FXSYS_wcstof(pszValue, iValueLen, &iUsedLen); |
| 682 if (iUsedLen <= 0) | 682 if (iUsedLen <= 0) |
| 683 return FALSE; | 683 return false; |
| 684 | 684 |
| 685 iValueLen -= iUsedLen; | 685 iValueLen -= iUsedLen; |
| 686 pszValue += iUsedLen; | 686 pszValue += iUsedLen; |
| 687 eUnit = FDE_CSSPRIMITIVETYPE_Number; | 687 eUnit = FDE_CSSPRIMITIVETYPE_Number; |
| 688 if (iValueLen >= 1 && *pszValue == '%') { | 688 if (iValueLen >= 1 && *pszValue == '%') { |
| 689 eUnit = FDE_CSSPRIMITIVETYPE_Percent; | 689 eUnit = FDE_CSSPRIMITIVETYPE_Percent; |
| 690 } else if (iValueLen == 2) { | 690 } else if (iValueLen == 2) { |
| 691 FDE_LPCCSSLENGTHUNITTABLE pUnit = | 691 FDE_LPCCSSLENGTHUNITTABLE pUnit = |
| 692 FDE_GetCSSLengthUnitByName(CFX_WideStringC(pszValue, 2)); | 692 FDE_GetCSSLengthUnitByName(CFX_WideStringC(pszValue, 2)); |
| 693 if (pUnit) | 693 if (pUnit) |
| 694 eUnit = (FDE_CSSPRIMITIVETYPE)pUnit->wValue; | 694 eUnit = (FDE_CSSPRIMITIVETYPE)pUnit->wValue; |
| 695 } | 695 } |
| 696 return TRUE; | 696 return true; |
| 697 } | 697 } |
| 698 | 698 |
| 699 FX_BOOL FDE_ParseCSSString(const FX_WCHAR* pszValue, | 699 bool FDE_ParseCSSString(const FX_WCHAR* pszValue, |
| 700 int32_t iValueLen, | 700 int32_t iValueLen, |
| 701 int32_t& iOffset, | 701 int32_t& iOffset, |
| 702 int32_t& iLength) { | 702 int32_t& iLength) { |
| 703 ASSERT(pszValue && iValueLen > 0); | 703 ASSERT(pszValue && iValueLen > 0); |
| 704 iOffset = 0; | 704 iOffset = 0; |
| 705 iLength = iValueLen; | 705 iLength = iValueLen; |
| 706 if (iValueLen >= 2) { | 706 if (iValueLen >= 2) { |
| 707 FX_WCHAR first = pszValue[0], last = pszValue[iValueLen - 1]; | 707 FX_WCHAR first = pszValue[0], last = pszValue[iValueLen - 1]; |
| 708 if ((first == '\"' && last == '\"') || (first == '\'' && last == '\'')) { | 708 if ((first == '\"' && last == '\"') || (first == '\'' && last == '\'')) { |
| 709 iOffset = 1, iLength -= 2; | 709 iOffset = 1, iLength -= 2; |
| 710 } | 710 } |
| 711 } | 711 } |
| 712 return iValueLen > 0; | 712 return iValueLen > 0; |
| 713 } | 713 } |
| 714 | 714 |
| 715 FX_BOOL FDE_ParseCSSURI(const FX_WCHAR* pszValue, | 715 bool FDE_ParseCSSURI(const FX_WCHAR* pszValue, |
| 716 int32_t iValueLen, | 716 int32_t iValueLen, |
| 717 int32_t& iOffset, | 717 int32_t& iOffset, |
| 718 int32_t& iLength) { | 718 int32_t& iLength) { |
| 719 ASSERT(pszValue && iValueLen > 0); | 719 ASSERT(pszValue && iValueLen > 0); |
| 720 if (iValueLen < 6 || pszValue[iValueLen - 1] != ')' || | 720 if (iValueLen < 6 || pszValue[iValueLen - 1] != ')' || |
| 721 FXSYS_wcsnicmp(L"url(", pszValue, 4)) { | 721 FXSYS_wcsnicmp(L"url(", pszValue, 4)) { |
| 722 return FALSE; | 722 return false; |
| 723 } | 723 } |
| 724 if (FDE_ParseCSSString(pszValue + 4, iValueLen - 5, iOffset, iLength)) { | 724 if (FDE_ParseCSSString(pszValue + 4, iValueLen - 5, iOffset, iLength)) { |
| 725 iOffset += 4; | 725 iOffset += 4; |
| 726 return TRUE; | 726 return true; |
| 727 } | 727 } |
| 728 return FALSE; | 728 return false; |
| 729 } | 729 } |
| 730 | 730 |
| 731 FX_BOOL FDE_ParseCSSColor(const FX_WCHAR* pszValue, | 731 bool FDE_ParseCSSColor(const FX_WCHAR* pszValue, |
| 732 int32_t iValueLen, | 732 int32_t iValueLen, |
| 733 FX_ARGB& dwColor) { | 733 FX_ARGB& dwColor) { |
| 734 ASSERT(pszValue && iValueLen > 0); | 734 ASSERT(pszValue && iValueLen > 0); |
| 735 | 735 |
| 736 if (*pszValue == '#') { | 736 if (*pszValue == '#') { |
| 737 switch (iValueLen) { | 737 switch (iValueLen) { |
| 738 case 4: { | 738 case 4: { |
| 739 uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[1]); | 739 uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[1]); |
| 740 uint8_t green = Hex2Dec((uint8_t)pszValue[2], (uint8_t)pszValue[2]); | 740 uint8_t green = Hex2Dec((uint8_t)pszValue[2], (uint8_t)pszValue[2]); |
| 741 uint8_t blue = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[3]); | 741 uint8_t blue = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[3]); |
| 742 dwColor = ArgbEncode(255, red, green, blue); | 742 dwColor = ArgbEncode(255, red, green, blue); |
| 743 return TRUE; | 743 return true; |
| 744 } | 744 } |
| 745 case 7: { | 745 case 7: { |
| 746 uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[2]); | 746 uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[2]); |
| 747 uint8_t green = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[4]); | 747 uint8_t green = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[4]); |
| 748 uint8_t blue = Hex2Dec((uint8_t)pszValue[5], (uint8_t)pszValue[6]); | 748 uint8_t blue = Hex2Dec((uint8_t)pszValue[5], (uint8_t)pszValue[6]); |
| 749 dwColor = ArgbEncode(255, red, green, blue); | 749 dwColor = ArgbEncode(255, red, green, blue); |
| 750 return TRUE; | 750 return true; |
| 751 } | 751 } |
| 752 default: | 752 default: |
| 753 return FALSE; | 753 return false; |
| 754 } | 754 } |
| 755 } | 755 } |
| 756 | 756 |
| 757 if (iValueLen >= 10) { | 757 if (iValueLen >= 10) { |
| 758 if (pszValue[iValueLen - 1] != ')' || FXSYS_wcsnicmp(L"rgb(", pszValue, 4)) | 758 if (pszValue[iValueLen - 1] != ')' || FXSYS_wcsnicmp(L"rgb(", pszValue, 4)) |
| 759 return FALSE; | 759 return false; |
| 760 | 760 |
| 761 uint8_t rgb[3] = {0}; | 761 uint8_t rgb[3] = {0}; |
| 762 FX_FLOAT fValue; | 762 FX_FLOAT fValue; |
| 763 FDE_CSSPRIMITIVETYPE eType; | 763 FDE_CSSPRIMITIVETYPE eType; |
| 764 CFDE_CSSValueListParser list(pszValue + 4, iValueLen - 5, ','); | 764 CFDE_CSSValueListParser list(pszValue + 4, iValueLen - 5, ','); |
| 765 for (int32_t i = 0; i < 3; ++i) { | 765 for (int32_t i = 0; i < 3; ++i) { |
| 766 if (!list.NextValue(eType, pszValue, iValueLen)) | 766 if (!list.NextValue(eType, pszValue, iValueLen)) |
| 767 return FALSE; | 767 return false; |
| 768 if (eType != FDE_CSSPRIMITIVETYPE_Number) | 768 if (eType != FDE_CSSPRIMITIVETYPE_Number) |
| 769 return FALSE; | 769 return false; |
| 770 if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eType)) | 770 if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eType)) |
| 771 return FALSE; | 771 return false; |
| 772 | 772 |
| 773 rgb[i] = eType == FDE_CSSPRIMITIVETYPE_Percent | 773 rgb[i] = eType == FDE_CSSPRIMITIVETYPE_Percent |
| 774 ? FXSYS_round(fValue * 2.55f) | 774 ? FXSYS_round(fValue * 2.55f) |
| 775 : FXSYS_round(fValue); | 775 : FXSYS_round(fValue); |
| 776 } | 776 } |
| 777 dwColor = ArgbEncode(255, rgb[0], rgb[1], rgb[2]); | 777 dwColor = ArgbEncode(255, rgb[0], rgb[1], rgb[2]); |
| 778 return TRUE; | 778 return true; |
| 779 } | 779 } |
| 780 | 780 |
| 781 FDE_LPCCSSCOLORTABLE pColor = | 781 FDE_LPCCSSCOLORTABLE pColor = |
| 782 FDE_GetCSSColorByName(CFX_WideStringC(pszValue, iValueLen)); | 782 FDE_GetCSSColorByName(CFX_WideStringC(pszValue, iValueLen)); |
| 783 if (!pColor) | 783 if (!pColor) |
| 784 return FALSE; | 784 return false; |
| 785 | 785 |
| 786 dwColor = pColor->dwValue; | 786 dwColor = pColor->dwValue; |
| 787 return TRUE; | 787 return true; |
| 788 } | 788 } |
| 789 | 789 |
| 790 CFDE_CSSValueList::CFDE_CSSValueList(IFX_MemoryAllocator* pStaticStore, | 790 CFDE_CSSValueList::CFDE_CSSValueList(IFX_MemoryAllocator* pStaticStore, |
| 791 const CFDE_CSSValueArray& list) { | 791 const CFDE_CSSValueArray& list) { |
| 792 m_iCount = list.GetSize(); | 792 m_iCount = list.GetSize(); |
| 793 int32_t iByteCount = m_iCount * sizeof(IFDE_CSSValue*); | 793 int32_t iByteCount = m_iCount * sizeof(IFDE_CSSValue*); |
| 794 m_ppList = (IFDE_CSSValue**)pStaticStore->Alloc(iByteCount); | 794 m_ppList = (IFDE_CSSValue**)pStaticStore->Alloc(iByteCount); |
| 795 FXSYS_memcpy(m_ppList, list.GetData(), iByteCount); | 795 FXSYS_memcpy(m_ppList, list.GetData(), iByteCount); |
| 796 } | 796 } |
| 797 | 797 |
| 798 int32_t CFDE_CSSValueList::CountValues() const { | 798 int32_t CFDE_CSSValueList::CountValues() const { |
| 799 return m_iCount; | 799 return m_iCount; |
| 800 } | 800 } |
| 801 | 801 |
| 802 IFDE_CSSValue* CFDE_CSSValueList::GetValue(int32_t index) const { | 802 IFDE_CSSValue* CFDE_CSSValueList::GetValue(int32_t index) const { |
| 803 return m_ppList[index]; | 803 return m_ppList[index]; |
| 804 } | 804 } |
| 805 FX_BOOL CFDE_CSSValueListParser::NextValue(FDE_CSSPRIMITIVETYPE& eType, | 805 bool CFDE_CSSValueListParser::NextValue(FDE_CSSPRIMITIVETYPE& eType, |
| 806 const FX_WCHAR*& pStart, | 806 const FX_WCHAR*& pStart, |
| 807 int32_t& iLength) { | 807 int32_t& iLength) { |
| 808 while (m_pCur < m_pEnd && (*m_pCur <= ' ' || *m_pCur == m_Separator)) { | 808 while (m_pCur < m_pEnd && (*m_pCur <= ' ' || *m_pCur == m_Separator)) { |
| 809 ++m_pCur; | 809 ++m_pCur; |
| 810 } | 810 } |
| 811 if (m_pCur >= m_pEnd) { | 811 if (m_pCur >= m_pEnd) { |
| 812 return FALSE; | 812 return false; |
| 813 } | 813 } |
| 814 eType = FDE_CSSPRIMITIVETYPE_Unknown; | 814 eType = FDE_CSSPRIMITIVETYPE_Unknown; |
| 815 pStart = m_pCur; | 815 pStart = m_pCur; |
| 816 iLength = 0; | 816 iLength = 0; |
| 817 FX_WCHAR wch = *m_pCur; | 817 FX_WCHAR wch = *m_pCur; |
| 818 if (wch == '#') { | 818 if (wch == '#') { |
| 819 iLength = SkipTo(' '); | 819 iLength = SkipTo(' '); |
| 820 if (iLength == 4 || iLength == 7) { | 820 if (iLength == 4 || iLength == 7) { |
| 821 eType = FDE_CSSPRIMITIVETYPE_RGB; | 821 eType = FDE_CSSPRIMITIVETYPE_RGB; |
| 822 } | 822 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 846 iLength = SkipTo(')') - 4; | 846 iLength = SkipTo(')') - 4; |
| 847 m_pCur++; | 847 m_pCur++; |
| 848 } | 848 } |
| 849 eType = FDE_CSSPRIMITIVETYPE_URI; | 849 eType = FDE_CSSPRIMITIVETYPE_URI; |
| 850 } else if (FXSYS_wcsnicmp(L"rgb", m_pCur, 3) == 0) { | 850 } else if (FXSYS_wcsnicmp(L"rgb", m_pCur, 3) == 0) { |
| 851 iLength = SkipTo(')') + 1; | 851 iLength = SkipTo(')') + 1; |
| 852 m_pCur++; | 852 m_pCur++; |
| 853 eType = FDE_CSSPRIMITIVETYPE_RGB; | 853 eType = FDE_CSSPRIMITIVETYPE_RGB; |
| 854 } | 854 } |
| 855 } else { | 855 } else { |
| 856 iLength = SkipTo(m_Separator, TRUE, TRUE); | 856 iLength = SkipTo(m_Separator, true, true); |
| 857 eType = FDE_CSSPRIMITIVETYPE_String; | 857 eType = FDE_CSSPRIMITIVETYPE_String; |
| 858 } | 858 } |
| 859 return m_pCur <= m_pEnd && iLength > 0; | 859 return m_pCur <= m_pEnd && iLength > 0; |
| 860 } | 860 } |
| 861 int32_t CFDE_CSSValueListParser::SkipTo(FX_WCHAR wch, | 861 int32_t CFDE_CSSValueListParser::SkipTo(FX_WCHAR wch, |
| 862 FX_BOOL bWSSeparator, | 862 bool bWSSeparator, |
| 863 FX_BOOL bBrContinue) { | 863 bool bBrContinue) { |
| 864 const FX_WCHAR* pStart = m_pCur; | 864 const FX_WCHAR* pStart = m_pCur; |
| 865 if (!bBrContinue) { | 865 if (!bBrContinue) { |
| 866 if (bWSSeparator) { | 866 if (bWSSeparator) { |
| 867 while ((++m_pCur < m_pEnd) && (*m_pCur != wch) && (*m_pCur > ' ')) { | 867 while ((++m_pCur < m_pEnd) && (*m_pCur != wch) && (*m_pCur > ' ')) { |
| 868 continue; | 868 continue; |
| 869 } | 869 } |
| 870 } else { | 870 } else { |
| 871 while (++m_pCur < m_pEnd && *m_pCur != wch) { | 871 while (++m_pCur < m_pEnd && *m_pCur != wch) { |
| 872 continue; | 872 continue; |
| 873 } | 873 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 960 | 960 |
| 961 int32_t CFDE_CSSPrimitiveValue::CountArgs() const { | 961 int32_t CFDE_CSSPrimitiveValue::CountArgs() const { |
| 962 ASSERT(m_eType == FDE_CSSPRIMITIVETYPE_Function); | 962 ASSERT(m_eType == FDE_CSSPRIMITIVETYPE_Function); |
| 963 return m_pFunction->CountArgs(); | 963 return m_pFunction->CountArgs(); |
| 964 } | 964 } |
| 965 | 965 |
| 966 IFDE_CSSValue* CFDE_CSSPrimitiveValue::GetArgs(int32_t index) const { | 966 IFDE_CSSValue* CFDE_CSSPrimitiveValue::GetArgs(int32_t index) const { |
| 967 ASSERT(m_eType == FDE_CSSPRIMITIVETYPE_Function); | 967 ASSERT(m_eType == FDE_CSSPRIMITIVETYPE_Function); |
| 968 return m_pFunction->GetArgs(index); | 968 return m_pFunction->GetArgs(index); |
| 969 } | 969 } |
| OLD | NEW |