summaryrefslogtreecommitdiff
path: root/xfa/fxfa/parser/cxfa_measurement.cpp
blob: 288ed0cd31f3e46eb1807892fc76ca90e771ce73 (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
// 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);
}

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

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;
}