Add a WriteClosedLoop() helper in cpdfsdk_appstream.cpp.
There are several instances of code that generate the same sequence of
drawing commands in cpdfsdk_appstream.cpp. Add a WriteClosedLoop()
function to consolidate these similar instances.
Note that this is not a pure refactor, as some instances that previously
generated stream content of the form "4 5 l f" now generate "4 5 l\nf"
instead.
Change-Id: I06f560146d3c71666371be2f270d44e2c8a6826e
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/96276
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index 74e56e4..d8e747e 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -38,6 +38,7 @@
#include "fpdfsdk/pwl/cpwl_edit_impl.h"
#include "fpdfsdk/pwl/cpwl_wnd.h"
#include "third_party/base/numerics/safe_conversions.h"
+#include "third_party/base/span.h"
namespace {
@@ -117,6 +118,14 @@
stream << point.x << " " << point.y << " " << kLineToOperator << "\n";
}
+void WriteClosedLoop(fxcrt::ostringstream& stream,
+ pdfium::span<const CFX_PointF> points) {
+ WriteMove(stream, points[0]);
+ for (const auto& point : points.subspan(1))
+ WriteLine(stream, point);
+ WriteLine(stream, points[0]);
+}
+
ByteString GetStrokeColorAppStream(const CFX_Color& color) {
fxcrt::ostringstream sColorStream;
switch (color.nColorType) {
@@ -273,11 +282,7 @@
{crBBox.left + fWidth / 2, crBBox.top},
{crBBox.right, crBBox.bottom + fHeight / 2},
{crBBox.left + fWidth / 2, crBBox.bottom}};
- csAP << points[0].x << " " << points[0].y << " " << kMoveToOperator << "\n";
- csAP << points[1].x << " " << points[1].y << " " << kLineToOperator << "\n";
- csAP << points[2].x << " " << points[2].y << " " << kLineToOperator << "\n";
- csAP << points[3].x << " " << points[3].y << " " << kLineToOperator << "\n";
- csAP << points[0].x << " " << points[0].y << " " << kLineToOperator << "\n";
+ WriteClosedLoop(csAP, points);
return ByteString(csAP);
}
@@ -289,11 +294,7 @@
{crBBox.right, crBBox.top},
{crBBox.right, crBBox.bottom},
{crBBox.left, crBBox.bottom}};
- csAP << points[0].x << " " << points[0].y << " " << kMoveToOperator << "\n";
- csAP << points[1].x << " " << points[1].y << " " << kLineToOperator << "\n";
- csAP << points[2].x << " " << points[2].y << " " << kLineToOperator << "\n";
- csAP << points[3].x << " " << points[3].y << " " << kLineToOperator << "\n";
- csAP << points[0].x << " " << points[0].y << " " << kLineToOperator << "\n";
+ WriteClosedLoop(csAP, points);
return ByteString(csAP);
}
@@ -978,16 +979,8 @@
{fLeft + fWidth / 2, fTop - fWidth / 2},
{fRight - fWidth / 2, fTop - fWidth / 2},
{fRight - fWidth / 2, fBottom + fWidth / 2}};
- sAppStream << points[0].x << " " << points[0].y << " "
- << kMoveToOperator << "\n";
- sAppStream << points[1].x << " " << points[1].x << " "
- << kLineToOperator << "\n";
- sAppStream << points[2].x << " " << points[2].x << " "
- << kLineToOperator << "\n";
- sAppStream << points[3].x << " " << points[3].x << " "
- << kLineToOperator << "\n";
- sAppStream << points[0].x << " " << points[0].y << " "
- << kLineToOperator << " " << kStrokeOperator << "\n";
+ WriteClosedLoop(sAppStream, points);
+ sAppStream << kStrokeOperator << "\n";
}
break;
case BorderStyle::kBeveled:
@@ -1079,15 +1072,9 @@
const CFX_PointF points[] = {{ptCenter.x - 3, ptCenter.y + 1.5f},
{ptCenter.x + 3, ptCenter.y + 1.5f},
{ptCenter.x, ptCenter.y - 1.5f}};
- sAppStream << " 0 " << kSetGrayOperator << "\n"
- << points[0].x << " " << points[0].y << " " << kMoveToOperator
- << "\n"
- << points[1].x << " " << points[1].y << " " << kLineToOperator
- << "\n"
- << points[2].x << " " << points[2].y << " " << kLineToOperator
- << "\n"
- << points[0].x << " " << points[0].y << " " << kLineToOperator
- << " " << kFillOperator << "\n";
+ sAppStream << " 0 " << kSetGrayOperator << "\n";
+ WriteClosedLoop(sAppStream, points);
+ sAppStream << kFillOperator << "\n";
}
return ByteString(sAppStream);