summaryrefslogtreecommitdiff
path: root/xfa/fxfa/parser/cxfa_measurement.cpp
blob: 053f9d1c471f005aebc9e0c71da433de21156e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "xfa/fxfa/parser/cxfa_measurement.h"

#include "core/fxcrt/fx_extension.h"

namespace {

constexpr float kPtToInch = 72;
constexpr float kPtToCm = kPtToInch / 2.54f;
constexpr float kPtToMm = kPtToCm / 10;
constexpr float kPtToMp = 0.001f;
constexpr float kPtToPc = 12;

}  // namespace

CXFA_Measurement::CXFA_Measurement(const WideStringView& wsMeasure) {
  SetString(wsMeasure);
}

CXFA_Measurement::CXFA_Measurement() {
  Set(-1, XFA_Unit::Unknown);
}

CXFA_Measurement::CXFA_Measurement(float fValue, XFA_Unit eUnit) {
  Set(fValue, eUnit);
}

void CXFA_Measurement::SetString(const WideStringView& wsMeasure) {
  if (wsMeasure.IsEmpty()) {
    m_fValue = 0;
    m_eUnit = XFA_Unit::Unknown;
    return;
  }
  int32_t iUsedLen = 0;
  int32_t iOffset = (wsMeasure[0] == L'=') ? 1 : 0;
  float fValue = FXSYS_wcstof(wsMeasure.unterminated_c_str() + iOffset,
                              wsMeasure.GetLength() - iOffset, &iUsedLen);
  XFA_Unit eUnit = GetUnitFromString(
      wsMeasure.Right(wsMeasure.GetLength() - (iOffset + iUsedLen)));
  Set(fValue, eUnit);
}

bool CXFA_Measurement::ToString(WideString* wsMeasure) const {
  switch (GetUnit()) {
    case XFA_Unit::Mm:
      wsMeasure->Format(L"%.8gmm", GetValue());
      return true;
    case XFA_Unit::Pt:
      wsMeasure->Format(L"%.8gpt", GetValue());
      return true;
    case XFA_Unit::In:
      wsMeasure->Format(L"%.8gin", GetValue());
      return true;
    case XFA_Unit::Cm:
      wsMeasure->Format(L"%.8gcm", GetValue());
      return true;
    case XFA_Unit::Mp:
      wsMeasure->Format(L"%.8gmp", GetValue());
      return true;
    case XFA_Unit::Pc:
      wsMeasure->Format(L"%.8gpc", GetValue());
      return true;
    case XFA_Unit::Em:
      wsMeasure->Format(L"%.8gem", GetValue());
      return true;
    case XFA_Unit::Percent:
      wsMeasure->Format(L"%.8g%%", GetValue());
      return true;
    default:
      wsMeasure->Format(L"%.8g", GetValue());
      return false;
  }
}

float CXFA_Measurement::ToUnit(XFA_Unit eUnit) const {
  float f;
  return ToUnitInternal(eUnit, &f) ? f : 0;
}

bool CXFA_Measurement::ToUnitInternal(XFA_Unit eUnit, float* fValue) const {
  *fValue = GetValue();
  XFA_Unit eFrom = GetUnit();
  if (eFrom == eUnit)
    return true;

  switch (eFrom) {
    case XFA_Unit::Pt:
      break;
    case XFA_Unit::Mm:
      *fValue *= kPtToMm;
      break;
    case XFA_Unit::In:
      *fValue *= kPtToInch;
      break;
    case XFA_Unit::Cm:
      *fValue *= kPtToCm;
      break;
    case XFA_Unit::Mp:
      *fValue *= kPtToMp;
      break;
    case XFA_Unit::Pc:
      *fValue *= kPtToPc;
      break;
    default:
      *fValue = 0;
      return false;
  }
  switch (eUnit) {
    case XFA_Unit::Pt:
      return true;
    case XFA_Unit::Mm:
      *fValue /= kPtToMm;
      return true;
    case XFA_Unit::In:
      *fValue /= kPtToInch;
      return true;
    case XFA_Unit::Cm:
      *fValue /= kPtToCm;
      return true;
    case XFA_Unit::Mp:
      *fValue /= kPtToMp;
      return true;
    case XFA_Unit::Pc:
      *fValue /= kPtToPc;
      return true;
    default:
      NOTREACHED();
      return false;
  }
}

// static
XFA_Unit CXFA_Measurement::GetUnitFromString(const WideStringView& wsUnit) {
  if (wsUnit == L"mm")
    return XFA_Unit::Mm;
  if (wsUnit == L"pt")
    return XFA_Unit::Pt;
  if (wsUnit == L"in")
    return XFA_Unit::In;
  if (wsUnit == L"cm")
    return XFA_Unit::Cm;
  if (wsUnit == L"pc")
    return XFA_Unit::Pc;
  if (wsUnit == L"mp")
    return XFA_Unit::Mp;
  if (wsUnit == L"em")
    return XFA_Unit::Em;
  if (wsUnit == L"%")
    return XFA_Unit::Percent;
  return XFA_Unit::Unknown;
}