gfx/skia/trunk/src/pdf/SkPDFUtils.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2011 Google Inc.
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #include "SkData.h"
michael@0 11 #include "SkGeometry.h"
michael@0 12 #include "SkPaint.h"
michael@0 13 #include "SkPath.h"
michael@0 14 #include "SkPDFResourceDict.h"
michael@0 15 #include "SkPDFUtils.h"
michael@0 16 #include "SkStream.h"
michael@0 17 #include "SkString.h"
michael@0 18 #include "SkPDFTypes.h"
michael@0 19
michael@0 20 //static
michael@0 21 SkPDFArray* SkPDFUtils::RectToArray(const SkRect& rect) {
michael@0 22 SkPDFArray* result = new SkPDFArray();
michael@0 23 result->reserve(4);
michael@0 24 result->appendScalar(rect.fLeft);
michael@0 25 result->appendScalar(rect.fTop);
michael@0 26 result->appendScalar(rect.fRight);
michael@0 27 result->appendScalar(rect.fBottom);
michael@0 28 return result;
michael@0 29 }
michael@0 30
michael@0 31 // static
michael@0 32 SkPDFArray* SkPDFUtils::MatrixToArray(const SkMatrix& matrix) {
michael@0 33 SkScalar values[6];
michael@0 34 if (!matrix.asAffine(values)) {
michael@0 35 SkMatrix::SetAffineIdentity(values);
michael@0 36 }
michael@0 37
michael@0 38 SkPDFArray* result = new SkPDFArray;
michael@0 39 result->reserve(6);
michael@0 40 for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) {
michael@0 41 result->appendScalar(values[i]);
michael@0 42 }
michael@0 43 return result;
michael@0 44 }
michael@0 45
michael@0 46 // static
michael@0 47 void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) {
michael@0 48 SkScalar values[6];
michael@0 49 if (!matrix.asAffine(values)) {
michael@0 50 SkMatrix::SetAffineIdentity(values);
michael@0 51 }
michael@0 52 for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) {
michael@0 53 SkPDFScalar::Append(values[i], content);
michael@0 54 content->writeText(" ");
michael@0 55 }
michael@0 56 content->writeText("cm\n");
michael@0 57 }
michael@0 58
michael@0 59 // static
michael@0 60 void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) {
michael@0 61 SkPDFScalar::Append(x, content);
michael@0 62 content->writeText(" ");
michael@0 63 SkPDFScalar::Append(y, content);
michael@0 64 content->writeText(" m\n");
michael@0 65 }
michael@0 66
michael@0 67 // static
michael@0 68 void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) {
michael@0 69 SkPDFScalar::Append(x, content);
michael@0 70 content->writeText(" ");
michael@0 71 SkPDFScalar::Append(y, content);
michael@0 72 content->writeText(" l\n");
michael@0 73 }
michael@0 74
michael@0 75 // static
michael@0 76 void SkPDFUtils::AppendCubic(SkScalar ctl1X, SkScalar ctl1Y,
michael@0 77 SkScalar ctl2X, SkScalar ctl2Y,
michael@0 78 SkScalar dstX, SkScalar dstY, SkWStream* content) {
michael@0 79 SkString cmd("y\n");
michael@0 80 SkPDFScalar::Append(ctl1X, content);
michael@0 81 content->writeText(" ");
michael@0 82 SkPDFScalar::Append(ctl1Y, content);
michael@0 83 content->writeText(" ");
michael@0 84 if (ctl2X != dstX || ctl2Y != dstY) {
michael@0 85 cmd.set("c\n");
michael@0 86 SkPDFScalar::Append(ctl2X, content);
michael@0 87 content->writeText(" ");
michael@0 88 SkPDFScalar::Append(ctl2Y, content);
michael@0 89 content->writeText(" ");
michael@0 90 }
michael@0 91 SkPDFScalar::Append(dstX, content);
michael@0 92 content->writeText(" ");
michael@0 93 SkPDFScalar::Append(dstY, content);
michael@0 94 content->writeText(" ");
michael@0 95 content->writeText(cmd.c_str());
michael@0 96 }
michael@0 97
michael@0 98 // static
michael@0 99 void SkPDFUtils::AppendRectangle(const SkRect& rect, SkWStream* content) {
michael@0 100 // Skia has 0,0 at top left, pdf at bottom left. Do the right thing.
michael@0 101 SkScalar bottom = SkMinScalar(rect.fBottom, rect.fTop);
michael@0 102
michael@0 103 SkPDFScalar::Append(rect.fLeft, content);
michael@0 104 content->writeText(" ");
michael@0 105 SkPDFScalar::Append(bottom, content);
michael@0 106 content->writeText(" ");
michael@0 107 SkPDFScalar::Append(rect.width(), content);
michael@0 108 content->writeText(" ");
michael@0 109 SkPDFScalar::Append(rect.height(), content);
michael@0 110 content->writeText(" re\n");
michael@0 111 }
michael@0 112
michael@0 113 // static
michael@0 114 void SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle,
michael@0 115 SkWStream* content) {
michael@0 116 // Filling a path with no area results in a drawing in PDF renderers but
michael@0 117 // Chrome expects to be able to draw some such entities with no visible
michael@0 118 // result, so we detect those cases and discard the drawing for them.
michael@0 119 // Specifically: moveTo(X), lineTo(Y) and moveTo(X), lineTo(X), lineTo(Y).
michael@0 120 enum SkipFillState {
michael@0 121 kEmpty_SkipFillState = 0,
michael@0 122 kSingleLine_SkipFillState = 1,
michael@0 123 kNonSingleLine_SkipFillState = 2,
michael@0 124 };
michael@0 125 SkipFillState fillState = kEmpty_SkipFillState;
michael@0 126 if (paintStyle != SkPaint::kFill_Style) {
michael@0 127 fillState = kNonSingleLine_SkipFillState;
michael@0 128 }
michael@0 129 SkPoint lastMovePt = SkPoint::Make(0,0);
michael@0 130 SkDynamicMemoryWStream currentSegment;
michael@0 131 SkPoint args[4];
michael@0 132 SkPath::Iter iter(path, false);
michael@0 133 for (SkPath::Verb verb = iter.next(args);
michael@0 134 verb != SkPath::kDone_Verb;
michael@0 135 verb = iter.next(args)) {
michael@0 136 // args gets all the points, even the implicit first point.
michael@0 137 switch (verb) {
michael@0 138 case SkPath::kMove_Verb:
michael@0 139 MoveTo(args[0].fX, args[0].fY, &currentSegment);
michael@0 140 lastMovePt = args[0];
michael@0 141 fillState = kEmpty_SkipFillState;
michael@0 142 break;
michael@0 143 case SkPath::kLine_Verb:
michael@0 144 AppendLine(args[1].fX, args[1].fY, &currentSegment);
michael@0 145 if (fillState == kEmpty_SkipFillState) {
michael@0 146 if (args[0] != lastMovePt) {
michael@0 147 fillState = kSingleLine_SkipFillState;
michael@0 148 }
michael@0 149 } else if (fillState == kSingleLine_SkipFillState) {
michael@0 150 fillState = kNonSingleLine_SkipFillState;
michael@0 151 }
michael@0 152 break;
michael@0 153 case SkPath::kQuad_Verb: {
michael@0 154 SkPoint cubic[4];
michael@0 155 SkConvertQuadToCubic(args, cubic);
michael@0 156 AppendCubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY,
michael@0 157 cubic[3].fX, cubic[3].fY, &currentSegment);
michael@0 158 fillState = kNonSingleLine_SkipFillState;
michael@0 159 break;
michael@0 160 }
michael@0 161 case SkPath::kCubic_Verb:
michael@0 162 AppendCubic(args[1].fX, args[1].fY, args[2].fX, args[2].fY,
michael@0 163 args[3].fX, args[3].fY, &currentSegment);
michael@0 164 fillState = kNonSingleLine_SkipFillState;
michael@0 165 break;
michael@0 166 case SkPath::kClose_Verb:
michael@0 167 if (fillState != kSingleLine_SkipFillState) {
michael@0 168 ClosePath(&currentSegment);
michael@0 169 SkData* data = currentSegment.copyToData();
michael@0 170 content->write(data->data(), data->size());
michael@0 171 data->unref();
michael@0 172 }
michael@0 173 currentSegment.reset();
michael@0 174 break;
michael@0 175 default:
michael@0 176 SkASSERT(false);
michael@0 177 break;
michael@0 178 }
michael@0 179 }
michael@0 180 if (currentSegment.bytesWritten() > 0) {
michael@0 181 SkData* data = currentSegment.copyToData();
michael@0 182 content->write(data->data(), data->size());
michael@0 183 data->unref();
michael@0 184 }
michael@0 185 }
michael@0 186
michael@0 187 // static
michael@0 188 void SkPDFUtils::ClosePath(SkWStream* content) {
michael@0 189 content->writeText("h\n");
michael@0 190 }
michael@0 191
michael@0 192 // static
michael@0 193 void SkPDFUtils::PaintPath(SkPaint::Style style, SkPath::FillType fill,
michael@0 194 SkWStream* content) {
michael@0 195 if (style == SkPaint::kFill_Style) {
michael@0 196 content->writeText("f");
michael@0 197 } else if (style == SkPaint::kStrokeAndFill_Style) {
michael@0 198 content->writeText("B");
michael@0 199 } else if (style == SkPaint::kStroke_Style) {
michael@0 200 content->writeText("S");
michael@0 201 }
michael@0 202
michael@0 203 if (style != SkPaint::kStroke_Style) {
michael@0 204 NOT_IMPLEMENTED(fill == SkPath::kInverseEvenOdd_FillType, false);
michael@0 205 NOT_IMPLEMENTED(fill == SkPath::kInverseWinding_FillType, false);
michael@0 206 if (fill == SkPath::kEvenOdd_FillType) {
michael@0 207 content->writeText("*");
michael@0 208 }
michael@0 209 }
michael@0 210 content->writeText("\n");
michael@0 211 }
michael@0 212
michael@0 213 // static
michael@0 214 void SkPDFUtils::StrokePath(SkWStream* content) {
michael@0 215 SkPDFUtils::PaintPath(
michael@0 216 SkPaint::kStroke_Style, SkPath::kWinding_FillType, content);
michael@0 217 }
michael@0 218
michael@0 219 // static
michael@0 220 void SkPDFUtils::DrawFormXObject(int objectIndex, SkWStream* content) {
michael@0 221 content->writeText("/");
michael@0 222 content->writeText(SkPDFResourceDict::getResourceName(
michael@0 223 SkPDFResourceDict::kXObject_ResourceType,
michael@0 224 objectIndex).c_str());
michael@0 225 content->writeText(" Do\n");
michael@0 226 }
michael@0 227
michael@0 228 // static
michael@0 229 void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) {
michael@0 230 content->writeText("/");
michael@0 231 content->writeText(SkPDFResourceDict::getResourceName(
michael@0 232 SkPDFResourceDict::kExtGState_ResourceType,
michael@0 233 objectIndex).c_str());
michael@0 234 content->writeText(" gs\n");
michael@0 235 }
michael@0 236
michael@0 237 // static
michael@0 238 void SkPDFUtils::ApplyPattern(int objectIndex, SkWStream* content) {
michael@0 239 // Select Pattern color space (CS, cs) and set pattern object as current
michael@0 240 // color (SCN, scn)
michael@0 241 SkString resourceName = SkPDFResourceDict::getResourceName(
michael@0 242 SkPDFResourceDict::kPattern_ResourceType,
michael@0 243 objectIndex);
michael@0 244 content->writeText("/Pattern CS/Pattern cs/");
michael@0 245 content->writeText(resourceName.c_str());
michael@0 246 content->writeText(" SCN/");
michael@0 247 content->writeText(resourceName.c_str());
michael@0 248 content->writeText(" scn\n");
michael@0 249 }

mercurial