OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 |
| 7 #include "xfa/fxfa/parser/cxfa_measurement.h" |
| 8 |
| 9 #include "xfa/fgas/crt/fgas_system.h" |
| 10 |
| 11 void CXFA_Measurement::Set(const CFX_WideStringC& wsMeasure) { |
| 12 if (wsMeasure.IsEmpty()) { |
| 13 m_fValue = 0; |
| 14 m_eUnit = XFA_UNIT_Unknown; |
| 15 return; |
| 16 } |
| 17 int32_t iUsedLen = 0; |
| 18 int32_t iOffset = (wsMeasure.GetAt(0) == L'=') ? 1 : 0; |
| 19 FX_FLOAT fValue = FX_wcstof(wsMeasure.c_str() + iOffset, |
| 20 wsMeasure.GetLength() - iOffset, &iUsedLen); |
| 21 XFA_UNIT eUnit = GetUnit(wsMeasure.Mid(iOffset + iUsedLen)); |
| 22 Set(fValue, eUnit); |
| 23 } |
| 24 |
| 25 FX_BOOL CXFA_Measurement::ToString(CFX_WideString& wsMeasure) const { |
| 26 switch (GetUnit()) { |
| 27 case XFA_UNIT_Mm: |
| 28 wsMeasure.Format(L"%.8gmm", GetValue()); |
| 29 return TRUE; |
| 30 case XFA_UNIT_Pt: |
| 31 wsMeasure.Format(L"%.8gpt", GetValue()); |
| 32 return TRUE; |
| 33 case XFA_UNIT_In: |
| 34 wsMeasure.Format(L"%.8gin", GetValue()); |
| 35 return TRUE; |
| 36 case XFA_UNIT_Cm: |
| 37 wsMeasure.Format(L"%.8gcm", GetValue()); |
| 38 return TRUE; |
| 39 case XFA_UNIT_Mp: |
| 40 wsMeasure.Format(L"%.8gmp", GetValue()); |
| 41 return TRUE; |
| 42 case XFA_UNIT_Pc: |
| 43 wsMeasure.Format(L"%.8gpc", GetValue()); |
| 44 return TRUE; |
| 45 case XFA_UNIT_Em: |
| 46 wsMeasure.Format(L"%.8gem", GetValue()); |
| 47 return TRUE; |
| 48 case XFA_UNIT_Percent: |
| 49 wsMeasure.Format(L"%.8g%%", GetValue()); |
| 50 return TRUE; |
| 51 default: |
| 52 wsMeasure.Format(L"%.8g", GetValue()); |
| 53 return FALSE; |
| 54 } |
| 55 } |
| 56 |
| 57 FX_BOOL CXFA_Measurement::ToUnit(XFA_UNIT eUnit, FX_FLOAT& fValue) const { |
| 58 fValue = GetValue(); |
| 59 XFA_UNIT eFrom = GetUnit(); |
| 60 if (eFrom == eUnit) |
| 61 return TRUE; |
| 62 |
| 63 switch (eFrom) { |
| 64 case XFA_UNIT_Pt: |
| 65 break; |
| 66 case XFA_UNIT_Mm: |
| 67 fValue *= 72 / 2.54f / 10; |
| 68 break; |
| 69 case XFA_UNIT_In: |
| 70 fValue *= 72; |
| 71 break; |
| 72 case XFA_UNIT_Cm: |
| 73 fValue *= 72 / 2.54f; |
| 74 break; |
| 75 case XFA_UNIT_Mp: |
| 76 fValue *= 0.001f; |
| 77 break; |
| 78 case XFA_UNIT_Pc: |
| 79 fValue *= 12.0f; |
| 80 break; |
| 81 default: |
| 82 fValue = 0; |
| 83 return FALSE; |
| 84 } |
| 85 switch (eUnit) { |
| 86 case XFA_UNIT_Pt: |
| 87 return TRUE; |
| 88 case XFA_UNIT_Mm: |
| 89 fValue /= 72 / 2.54f / 10; |
| 90 return TRUE; |
| 91 case XFA_UNIT_In: |
| 92 fValue /= 72; |
| 93 return TRUE; |
| 94 case XFA_UNIT_Cm: |
| 95 fValue /= 72 / 2.54f; |
| 96 return TRUE; |
| 97 case XFA_UNIT_Mp: |
| 98 fValue /= 0.001f; |
| 99 return TRUE; |
| 100 case XFA_UNIT_Pc: |
| 101 fValue /= 12.0f; |
| 102 return TRUE; |
| 103 default: |
| 104 fValue = 0; |
| 105 return FALSE; |
| 106 } |
| 107 } |
| 108 |
| 109 XFA_UNIT CXFA_Measurement::GetUnit(const CFX_WideStringC& wsUnit) { |
| 110 if (wsUnit == FX_WSTRC(L"mm")) |
| 111 return XFA_UNIT_Mm; |
| 112 if (wsUnit == FX_WSTRC(L"pt")) |
| 113 return XFA_UNIT_Pt; |
| 114 if (wsUnit == FX_WSTRC(L"in")) |
| 115 return XFA_UNIT_In; |
| 116 if (wsUnit == FX_WSTRC(L"cm")) |
| 117 return XFA_UNIT_Cm; |
| 118 if (wsUnit == FX_WSTRC(L"pc")) |
| 119 return XFA_UNIT_Pc; |
| 120 if (wsUnit == FX_WSTRC(L"mp")) |
| 121 return XFA_UNIT_Mp; |
| 122 if (wsUnit == FX_WSTRC(L"em")) |
| 123 return XFA_UNIT_Em; |
| 124 if (wsUnit == FX_WSTRC(L"%")) |
| 125 return XFA_UNIT_Percent; |
| 126 return XFA_UNIT_Unknown; |
| 127 } |
OLD | NEW |