summaryrefslogtreecommitdiff
path: root/core/fpdfdoc/cpdf_defaultappearance.cpp
blob: daf93414bf751f3ebdeb39804b40cfda24d7feba (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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 "core/fpdfdoc/cpdf_defaultappearance.h"

#include <algorithm>

#include "core/fpdfapi/parser/cpdf_simple_parser.h"
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
#include "core/fpdfdoc/cpdf_formcontrol.h"

bool CPDF_DefaultAppearance::HasFont() {
  if (m_csDA.IsEmpty())
    return false;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  return syntax.FindTagParamFromStart("Tf", 2);
}

CFX_ByteString CPDF_DefaultAppearance::GetFontString() {
  CFX_ByteString csFont;
  if (m_csDA.IsEmpty())
    return csFont;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  if (syntax.FindTagParamFromStart("Tf", 2)) {
    csFont += syntax.GetWord();
    csFont += " ";
    csFont += syntax.GetWord();
    csFont += " ";
    csFont += syntax.GetWord();
  }
  return csFont;
}

void CPDF_DefaultAppearance::GetFont(CFX_ByteString& csFontNameTag,
                                     FX_FLOAT& fFontSize) {
  csFontNameTag = "";
  fFontSize = 0;
  if (m_csDA.IsEmpty())
    return;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  if (syntax.FindTagParamFromStart("Tf", 2)) {
    csFontNameTag = CFX_ByteString(syntax.GetWord());
    csFontNameTag.Delete(0, 1);
    fFontSize = FX_atof(syntax.GetWord());
  }
  csFontNameTag = PDF_NameDecode(csFontNameTag);
}

bool CPDF_DefaultAppearance::HasColor(PaintOperation nOperation) {
  if (m_csDA.IsEmpty())
    return false;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "G" : "g"), 1)) {
    return true;
  }
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "RG" : "rg"), 3)) {
    return true;
  }
  return syntax.FindTagParamFromStart(
      (nOperation == PaintOperation::STROKE ? "K" : "k"), 4);
}

CFX_ByteString CPDF_DefaultAppearance::GetColorString(
    PaintOperation nOperation) {
  CFX_ByteString csColor;
  if (m_csDA.IsEmpty())
    return csColor;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "G" : "g"), 1)) {
    csColor += syntax.GetWord();
    csColor += " ";
    csColor += syntax.GetWord();
    return csColor;
  }
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "RG" : "rg"), 3)) {
    csColor += syntax.GetWord();
    csColor += " ";
    csColor += syntax.GetWord();
    csColor += " ";
    csColor += syntax.GetWord();
    csColor += " ";
    csColor += syntax.GetWord();
    return csColor;
  }
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "K" : "k"), 4)) {
    csColor += syntax.GetWord();
    csColor += " ";
    csColor += syntax.GetWord();
    csColor += " ";
    csColor += syntax.GetWord();
    csColor += " ";
    csColor += syntax.GetWord();
    csColor += " ";
    csColor += syntax.GetWord();
  }
  return csColor;
}

void CPDF_DefaultAppearance::GetColor(int& iColorType,
                                      FX_FLOAT fc[4],
                                      PaintOperation nOperation) {
  iColorType = COLORTYPE_TRANSPARENT;
  for (int c = 0; c < 4; c++)
    fc[c] = 0;

  if (m_csDA.IsEmpty())
    return;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "G" : "g"), 1)) {
    iColorType = COLORTYPE_GRAY;
    fc[0] = FX_atof(syntax.GetWord());
    return;
  }
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "RG" : "rg"), 3)) {
    iColorType = COLORTYPE_RGB;
    fc[0] = FX_atof(syntax.GetWord());
    fc[1] = FX_atof(syntax.GetWord());
    fc[2] = FX_atof(syntax.GetWord());
    return;
  }
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "K" : "k"), 4)) {
    iColorType = COLORTYPE_CMYK;
    fc[0] = FX_atof(syntax.GetWord());
    fc[1] = FX_atof(syntax.GetWord());
    fc[2] = FX_atof(syntax.GetWord());
    fc[3] = FX_atof(syntax.GetWord());
  }
}

void CPDF_DefaultAppearance::GetColor(FX_ARGB& color,
                                      int& iColorType,
                                      PaintOperation nOperation) {
  color = 0;
  iColorType = COLORTYPE_TRANSPARENT;
  if (m_csDA.IsEmpty())
    return;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "G" : "g"), 1)) {
    iColorType = COLORTYPE_GRAY;
    FX_FLOAT g = FX_atof(syntax.GetWord()) * 255 + 0.5f;
    color = ArgbEncode(255, (int)g, (int)g, (int)g);
    return;
  }
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "RG" : "rg"), 3)) {
    iColorType = COLORTYPE_RGB;
    FX_FLOAT r = FX_atof(syntax.GetWord()) * 255 + 0.5f;
    FX_FLOAT g = FX_atof(syntax.GetWord()) * 255 + 0.5f;
    FX_FLOAT b = FX_atof(syntax.GetWord()) * 255 + 0.5f;
    color = ArgbEncode(255, (int)r, (int)g, (int)b);
    return;
  }
  if (syntax.FindTagParamFromStart(
          (nOperation == PaintOperation::STROKE ? "K" : "k"), 4)) {
    iColorType = COLORTYPE_CMYK;
    FX_FLOAT c = FX_atof(syntax.GetWord());
    FX_FLOAT m = FX_atof(syntax.GetWord());
    FX_FLOAT y = FX_atof(syntax.GetWord());
    FX_FLOAT k = FX_atof(syntax.GetWord());
    FX_FLOAT r = 1.0f - std::min(1.0f, c + k);
    FX_FLOAT g = 1.0f - std::min(1.0f, m + k);
    FX_FLOAT b = 1.0f - std::min(1.0f, y + k);
    color = ArgbEncode(255, (int)(r * 255 + 0.5f), (int)(g * 255 + 0.5f),
                       (int)(b * 255 + 0.5f));
  }
}

bool CPDF_DefaultAppearance::HasTextMatrix() {
  if (m_csDA.IsEmpty())
    return false;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  return syntax.FindTagParamFromStart("Tm", 6);
}

CFX_ByteString CPDF_DefaultAppearance::GetTextMatrixString() {
  CFX_ByteString csTM;
  if (m_csDA.IsEmpty())
    return csTM;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  if (syntax.FindTagParamFromStart("Tm", 6)) {
    for (int i = 0; i < 6; i++) {
      csTM += syntax.GetWord();
      csTM += " ";
    }
    csTM += syntax.GetWord();
  }
  return csTM;
}

CFX_Matrix CPDF_DefaultAppearance::GetTextMatrix() {
  CFX_Matrix tm;
  if (m_csDA.IsEmpty())
    return tm;

  CPDF_SimpleParser syntax(m_csDA.AsStringC());
  if (syntax.FindTagParamFromStart("Tm", 6)) {
    FX_FLOAT f[6];
    for (int i = 0; i < 6; i++)
      f[i] = FX_atof(syntax.GetWord());
    tm.Set(f[0], f[1], f[2], f[3], f[4], f[5]);
  }
  return tm;
}