summaryrefslogtreecommitdiff
path: root/core/fpdfdoc/cpdf_defaultappearance.cpp
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2018-03-28 20:00:35 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-03-28 20:00:35 +0000
commit53a8093c6ef694ec520fe0b087fbac86af97f5e8 (patch)
tree519a805d66eed415e310eb67a05212c6d217025d /core/fpdfdoc/cpdf_defaultappearance.cpp
parent971a674ef17526ad37ce55ba90110830b94889d0 (diff)
downloadpdfium-53a8093c6ef694ec520fe0b087fbac86af97f5e8.tar.xz
Use CPDF_DefaultAppearance instead of custom parsing
This CL moves code over to using CPDF_DefaultAppearance instead of calling the CPDF_SimpleParser directly. This means the code for finding a specific tag start can move into CPDF_DefaultAppearance directly. Change-Id: I1dc64e54aedd03d059b963121d466f3eb75c17db Reviewed-on: https://pdfium-review.googlesource.com/28410 Reviewed-by: Henrique Nakashima <hnakashima@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fpdfdoc/cpdf_defaultappearance.cpp')
-rw-r--r--core/fpdfdoc/cpdf_defaultappearance.cpp65
1 files changed, 57 insertions, 8 deletions
diff --git a/core/fpdfdoc/cpdf_defaultappearance.cpp b/core/fpdfdoc/cpdf_defaultappearance.cpp
index cae553a7c2..8878dc0093 100644
--- a/core/fpdfdoc/cpdf_defaultappearance.cpp
+++ b/core/fpdfdoc/cpdf_defaultappearance.cpp
@@ -7,17 +7,58 @@
#include "core/fpdfdoc/cpdf_defaultappearance.h"
#include <algorithm>
+#include <vector>
#include "core/fpdfapi/parser/cpdf_simple_parser.h"
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
#include "core/fxge/cfx_color.h"
+namespace {
+
+// Find the token and its |nParams| parameters from the start of data,
+// and move the current position to the start of those parameters.
+bool FindTagParamFromStart(CPDF_SimpleParser* parser,
+ const ByteStringView& token,
+ int nParams) {
+ nParams++;
+
+ std::vector<uint32_t> pBuf(nParams);
+ int buf_index = 0;
+ int buf_count = 0;
+
+ parser->SetCurPos(0);
+ while (1) {
+ pBuf[buf_index++] = parser->GetCurPos();
+ if (buf_index == nParams)
+ buf_index = 0;
+
+ buf_count++;
+ if (buf_count > nParams)
+ buf_count = nParams;
+
+ ByteStringView word = parser->GetWord();
+ if (word.IsEmpty())
+ return false;
+
+ if (word == token) {
+ if (buf_count < nParams)
+ continue;
+
+ parser->SetCurPos(pBuf[buf_index]);
+ return true;
+ }
+ }
+ return false;
+}
+
+} // namespace
+
bool CPDF_DefaultAppearance::HasFont() {
if (m_csDA.IsEmpty())
return false;
CPDF_SimpleParser syntax(m_csDA.AsStringView());
- return syntax.FindTagParamFromStart("Tf", 2);
+ return FindTagParamFromStart(&syntax, "Tf", 2);
}
ByteString CPDF_DefaultAppearance::GetFont(float* fFontSize) {
@@ -27,7 +68,7 @@ ByteString CPDF_DefaultAppearance::GetFont(float* fFontSize) {
ByteString csFontNameTag;
CPDF_SimpleParser syntax(m_csDA.AsStringView());
- if (syntax.FindTagParamFromStart("Tf", 2)) {
+ if (FindTagParamFromStart(&syntax, "Tf", 2)) {
csFontNameTag = ByteString(syntax.GetWord());
csFontNameTag.Delete(0, 1);
*fFontSize = FX_atof(syntax.GetWord());
@@ -40,11 +81,11 @@ bool CPDF_DefaultAppearance::HasColor() {
return false;
CPDF_SimpleParser syntax(m_csDA.AsStringView());
- if (syntax.FindTagParamFromStart("g", 1))
+ if (FindTagParamFromStart(&syntax, "g", 1))
return true;
- if (syntax.FindTagParamFromStart("rg", 3))
+ if (FindTagParamFromStart(&syntax, "rg", 3))
return true;
- return syntax.FindTagParamFromStart("k", 4);
+ return FindTagParamFromStart(&syntax, "k", 4);
}
void CPDF_DefaultAppearance::GetColor(int& iColorType, float fc[4]) {
@@ -56,19 +97,19 @@ void CPDF_DefaultAppearance::GetColor(int& iColorType, float fc[4]) {
return;
CPDF_SimpleParser syntax(m_csDA.AsStringView());
- if (syntax.FindTagParamFromStart("g", 1)) {
+ if (FindTagParamFromStart(&syntax, "g", 1)) {
iColorType = CFX_Color::kGray;
fc[0] = FX_atof(syntax.GetWord());
return;
}
- if (syntax.FindTagParamFromStart("rg", 3)) {
+ if (FindTagParamFromStart(&syntax, "rg", 3)) {
iColorType = CFX_Color::kRGB;
fc[0] = FX_atof(syntax.GetWord());
fc[1] = FX_atof(syntax.GetWord());
fc[2] = FX_atof(syntax.GetWord());
return;
}
- if (syntax.FindTagParamFromStart("k", 4)) {
+ if (FindTagParamFromStart(&syntax, "k", 4)) {
iColorType = CFX_Color::kCMYK;
fc[0] = FX_atof(syntax.GetWord());
fc[1] = FX_atof(syntax.GetWord());
@@ -104,4 +145,12 @@ void CPDF_DefaultAppearance::GetColor(FX_ARGB& color, int& iColorType) {
static_cast<int>(g * 255 + 0.5f),
static_cast<int>(b * 255 + 0.5f));
}
+ NOTREACHED();
+}
+
+bool CPDF_DefaultAppearance::FindTagParamFromStartForTesting(
+ CPDF_SimpleParser* parser,
+ const ByteStringView& token,
+ int nParams) {
+ return FindTagParamFromStart(parser, token, nParams);
}