| 1 |
/** |
| 2 |
* SPDX-FileCopyrightText: (C) 2010 Dominik Seichter <[email protected]> |
| 3 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <podofo/private/PdfDeclarationsPrivate.h> |
| 7 |
#include "colorchanger.h" |
| 8 |
|
| 9 |
#include <iostream> |
| 10 |
#include <sstream> |
| 11 |
#include <cstdlib> |
| 12 |
#include <iomanip> |
| 13 |
|
| 14 |
#include "graphicsstack.h" |
| 15 |
#include "iconverter.h" |
| 16 |
|
| 17 |
using namespace std; |
| 18 |
using namespace PoDoFo; |
| 19 |
|
| 20 |
static const ColorChanger::KWInfo kwInfo[] = { |
| 21 |
{ ColorChanger::eKeyword_GraphicsStack_Push, "q", 0, "Save state" }, |
| 22 |
{ ColorChanger::eKeyword_GraphicsStack_Pop, "Q", 0, "Restore state" }, |
| 23 |
|
| 24 |
{ ColorChanger::eKeyword_SelectGray_Stroking, "G", 1, "Select gray stroking color" }, |
| 25 |
{ ColorChanger::eKeyword_SelectRGB_Stroking, "RG", 3, "Select RGB stroking color" }, |
| 26 |
{ ColorChanger::eKeyword_SelectCMYK_Stroking, "K", 4, "Select CMYK stroking color" }, |
| 27 |
|
| 28 |
{ ColorChanger::eKeyword_SelectGray_NonStroking, "g", 1, "Select gray non-stroking color" }, |
| 29 |
{ ColorChanger::eKeyword_SelectRGB_NonStroking, "rg", 3, "Select RGB non-stroking color" }, |
| 30 |
{ ColorChanger::eKeyword_SelectCMYK_NonStroking, "k", 4, "Select CMYK non-stroking color" }, |
| 31 |
|
| 32 |
{ ColorChanger::eKeyword_SelectColorSpace_Stroking, "CS", 1, "Select colorspace non-stroking color" }, |
| 33 |
{ ColorChanger::eKeyword_SelectColorSpace_NonStroking, "cs", 1, "Select colorspace non-stroking color" }, |
| 34 |
|
| 35 |
{ ColorChanger::eKeyword_SelectColor_Stroking, "SC", 1, "Select depending on current colorspace" }, |
| 36 |
{ ColorChanger::eKeyword_SelectColor_NonStroking, "sc", 1, "Select depending on current colorspace" }, |
| 37 |
{ ColorChanger::eKeyword_SelectColor_Stroking2, "SCN", 1, "Select depending on current colorspace (extended)" }, |
| 38 |
{ ColorChanger::eKeyword_SelectColor_NonStroking2, "scn", 1, "Select depending on current colorspace (extended)" }, |
| 39 |
|
| 40 |
// Sentinel |
| 41 |
{ ColorChanger::eKeyword_Undefined, "\0", 0, NULL } |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
// PDF Commands, which modify colors according to PDFReference 1.7 |
| 46 |
// CS - select colorspace stroking (May need lookup in Colorspace key of resource directory) |
| 47 |
// cs - select colorspace non-stroking (May need lookup in Colorspace key of resource directory) |
| 48 |
// SC - select stroking color depending on colorspace |
| 49 |
// SCN - select stroking color for colorspaces including Separation, DeviceN, ICCBased |
| 50 |
// sc - select non-stroking color depending on colorspace |
| 51 |
// scn - select non-stroking color for colorspaces including Separation, DeviceN, ICCBased |
| 52 |
// G - select gray colorspace and gray stroking color |
| 53 |
// g - select gray colorspace and gray non stroking color |
| 54 |
// RG - select RGB colorspace and RGB stroking color |
| 55 |
// rg - select RGB colorspace and RGB non stroking color |
| 56 |
// K - select CMYK colorspace and CMYK stroking color |
| 57 |
// k - select CMYK colorspace and CMYK non stroking color |
| 58 |
|
| 59 |
// TODO: Allow to set default color and colorspace when starting a page |
| 60 |
|
| 61 |
// ColorSpaces and their default colors |
| 62 |
// DeviceColorSpaces |
| 63 |
// DeviceGray 0.0 |
| 64 |
// DeviceRGB 0.0 |
| 65 |
// DeviceCMYK 0.0 0.0 0.0 1.0 |
| 66 |
// CIE Based ColorSpaces |
| 67 |
// CalGray 0.0 |
| 68 |
// CalRGB 0.0 |
| 69 |
// Lab - all values 0.0 or closest according to range |
| 70 |
// ICCBased - all values 0.0 or closest according to range |
| 71 |
// Special ColorSpaces |
| 72 |
// Pattern - the value that causes nothing to be painted |
| 73 |
// Indexed 0 |
| 74 |
// Separation - all values 1.0 |
| 75 |
// DeviceN - all values 1.0 |
| 76 |
|
| 77 |
// GraphicsState entries and their default values |
| 78 |
// ColorSpace - DeviceGray |
| 79 |
// color stroking - black (see ColorSpace default values) |
| 80 |
// color non stroking - black (see ColorSpace default values) |
| 81 |
// Operations |
| 82 |
// q Push |
| 83 |
// Q Pop |
| 84 |
|
| 85 |
ColorChanger::ColorChanger(IConverter* convert, const string_view& sInput, const string_view& sOutput) |
| 86 |
: m_converter(convert), m_input(sInput), m_output(sOutput) |
| 87 |
{ |
| 88 |
if (!m_converter) |
| 89 |
{ |
| 90 |
PODOFO_RAISE_ERROR(PdfErrorCode::InvalidHandle); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void ColorChanger::start() |
| 95 |
{ |
| 96 |
PdfMemDocument input; |
| 97 |
input.LoadFromBuffer(m_input); |
| 98 |
for (unsigned i = 0; i < input.GetPages().GetCount(); i++) |
| 99 |
{ |
| 100 |
cout << "Processing page " << std::setw(6) << (i + 1) << "..." << endl << flush; |
| 101 |
|
| 102 |
auto& page = input.GetPages().GetPageAt(i);; |
| 103 |
|
| 104 |
m_converter->StartPage(page, i); |
| 105 |
this->ReplaceColorsInPage(page); |
| 106 |
m_converter->EndPage(page, i); |
| 107 |
} |
| 108 |
|
| 109 |
// Go through all XObjects |
| 110 |
PdfIndirectObjectList::iterator it = input.GetObjects().begin(); |
| 111 |
while (it != input.GetObjects().end()) |
| 112 |
{ |
| 113 |
if ((*it)->IsDictionary() && (*it)->GetDictionary().HasKey("Type")) |
| 114 |
{ |
| 115 |
if (PdfName("XObject") == (*it)->GetDictionary().GetKey("Type")->GetName() |
| 116 |
&& (*it)->GetDictionary().HasKey("Subtype") |
| 117 |
&& PdfName("Image") != (*it)->GetDictionary().GetKey("Subtype")->GetName()) |
| 118 |
{ |
| 119 |
cout << "Processing XObject " << (*it)->GetReference().ObjectNumber() << " " |
| 120 |
<< (*it)->GetReference().GenerationNumber() << endl; |
| 121 |
|
| 122 |
unique_ptr<PdfXObjectForm> xobj; |
| 123 |
(void)PdfXObject::TryCreateFromObject(**it, xobj); |
| 124 |
m_converter->StartXObject(*xobj); |
| 125 |
this->ReplaceColorsInPage(*xobj); |
| 126 |
m_converter->EndXObject(*xobj); |
| 127 |
} |
| 128 |
} |
| 129 |
it++; |
| 130 |
} |
| 131 |
|
| 132 |
input.Save(m_output); |
| 133 |
} |
| 134 |
|
| 135 |
void ColorChanger::ReplaceColorsInPage(PdfCanvas& page) |
| 136 |
{ |
| 137 |
PdfPostScriptTokenType t; |
| 138 |
string_view keyword; |
| 139 |
PdfVariant var; |
| 140 |
|
| 141 |
GraphicsStack graphicsStack; |
| 142 |
PdfPostScriptTokenizer tokenizer; |
| 143 |
vector<PdfVariant> args; |
| 144 |
|
| 145 |
PdfCanvasInputDevice input(page); |
| 146 |
|
| 147 |
charbuff buffer; |
| 148 |
BufferStreamDevice device(buffer); |
| 149 |
|
| 150 |
while (tokenizer.TryReadNext(input, t, keyword, var)) |
| 151 |
{ |
| 152 |
if (t == PdfPostScriptTokenType::Variant) |
| 153 |
{ |
| 154 |
// arguments come before operators, but we want to group them up before |
| 155 |
// their operator. |
| 156 |
args.push_back(var); |
| 157 |
} |
| 158 |
////else if (t == PdfPostScriptTokenType::ImageData) |
| 159 |
////{ |
| 160 |
//// // Handle inline images (Internally using PdfData) |
| 161 |
//// args.push_back(var); |
| 162 |
////} |
| 163 |
else if (t == PdfPostScriptTokenType::Keyword) |
| 164 |
{ |
| 165 |
const KWInfo* info = FindKeyWordByName(keyword.data()); |
| 166 |
PdfColor color, newColor; |
| 167 |
int nNumArgs = info->nNumArguments; |
| 168 |
PdfColorSpaceType colorSpace; |
| 169 |
|
| 170 |
if (info->nNumArguments > 0 && args.size() != static_cast<size_t>(info->nNumArguments)) |
| 171 |
{ |
| 172 |
ostringstream oss; |
| 173 |
oss << "Expected " << info->nNumArguments << " argument(s) for keyword '" << keyword << "', but " << args.size() << " given instead."; |
| 174 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidContentStream, oss.str().c_str()); |
| 175 |
} |
| 176 |
|
| 177 |
switch (info->eKeywordType) |
| 178 |
{ |
| 179 |
case eKeyword_GraphicsStack_Push: |
| 180 |
graphicsStack.Push(); |
| 181 |
break; |
| 182 |
case eKeyword_GraphicsStack_Pop: |
| 183 |
graphicsStack.Pop(); |
| 184 |
break; |
| 185 |
|
| 186 |
case eKeyword_SelectColorSpace_Stroking: |
| 187 |
colorSpace = this->GetColorSpaceForName(args.back().GetName(), page); |
| 188 |
colorSpace = PoDoFo::ConvertTo<PdfColorSpaceType>(args.back().GetName()); |
| 189 |
args.pop_back(); |
| 190 |
graphicsStack.SetStrokingColorSpace(colorSpace); |
| 191 |
break; |
| 192 |
|
| 193 |
case eKeyword_SelectColorSpace_NonStroking: |
| 194 |
colorSpace = PoDoFo::ConvertTo<PdfColorSpaceType>(args.back().GetName()); |
| 195 |
args.pop_back(); |
| 196 |
graphicsStack.SetNonStrokingColorSpace(colorSpace); |
| 197 |
break; |
| 198 |
|
| 199 |
case eKeyword_SelectGray_Stroking: |
| 200 |
case eKeyword_SelectRGB_Stroking: |
| 201 |
case eKeyword_SelectCMYK_Stroking: |
| 202 |
case eKeyword_SelectGray_NonStroking: |
| 203 |
case eKeyword_SelectRGB_NonStroking: |
| 204 |
case eKeyword_SelectCMYK_NonStroking: |
| 205 |
|
| 206 |
keyword = |
| 207 |
this->ProcessColor(info->eKeywordType, nNumArgs, args, graphicsStack); |
| 208 |
|
| 209 |
break; |
| 210 |
|
| 211 |
case eKeyword_SelectColor_Stroking: |
| 212 |
case eKeyword_SelectColor_Stroking2: |
| 213 |
{ |
| 214 |
/* |
| 215 |
PdfError::LogMessage( LogSeverity::Information, "SCN called for colorspace: %s\n", |
| 216 |
PdfColor::GetNameForColorSpace( |
| 217 |
graphicsStack.GetStrokingColorSpace() ).GetName().c_str() ); |
| 218 |
*/ |
| 219 |
int nTmpArgs; |
| 220 |
EKeywordType eTempKeyword; |
| 221 |
|
| 222 |
switch (graphicsStack.GetStrokingColorSpace()) |
| 223 |
{ |
| 224 |
case PdfColorSpaceType::DeviceGray: |
| 225 |
nTmpArgs = 1; |
| 226 |
eTempKeyword = eKeyword_SelectGray_Stroking; |
| 227 |
break; |
| 228 |
case PdfColorSpaceType::DeviceRGB: |
| 229 |
nTmpArgs = 3; |
| 230 |
eTempKeyword = eKeyword_SelectRGB_Stroking; |
| 231 |
break; |
| 232 |
case PdfColorSpaceType::DeviceCMYK: |
| 233 |
nTmpArgs = 4; |
| 234 |
eTempKeyword = eKeyword_SelectCMYK_Stroking; |
| 235 |
break; |
| 236 |
|
| 237 |
case PdfColorSpaceType::Separation: |
| 238 |
{ |
| 239 |
PoDoFo::LogMessage(PdfLogSeverity::Error, "Separation color space not supported.\n"); |
| 240 |
PODOFO_RAISE_ERROR(PdfErrorCode::CannotConvertColor); |
| 241 |
break; |
| 242 |
} |
| 243 |
case PdfColorSpaceType::Lab: |
| 244 |
{ |
| 245 |
PoDoFo::LogMessage(PdfLogSeverity::Error, "CieLab color space not supported.\n"); |
| 246 |
PODOFO_RAISE_ERROR(PdfErrorCode::CannotConvertColor); |
| 247 |
break; |
| 248 |
} |
| 249 |
case PdfColorSpaceType::Indexed: |
| 250 |
{ |
| 251 |
PoDoFo::LogMessage(PdfLogSeverity::Error, "Indexed color space not supported.\n"); |
| 252 |
PODOFO_RAISE_ERROR(PdfErrorCode::CannotConvertColor); |
| 253 |
break; |
| 254 |
} |
| 255 |
case PdfColorSpaceType::Unknown: |
| 256 |
|
| 257 |
default: |
| 258 |
{ |
| 259 |
PODOFO_RAISE_ERROR(PdfErrorCode::CannotConvertColor); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
keyword = |
| 264 |
this->ProcessColor(eTempKeyword, nTmpArgs, args, graphicsStack); |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
case eKeyword_SelectColor_NonStroking: |
| 269 |
case eKeyword_SelectColor_NonStroking2: |
| 270 |
{ |
| 271 |
/* |
| 272 |
PdfError::LogMessage( LogSeverity::Information, |
| 273 |
"scn called for colorspace: %s\n", |
| 274 |
PdfColor::GetNameForColorSpace( |
| 275 |
graphicsStack.GetNonStrokingColorSpace() ).GetName().c_str() );*/ |
| 276 |
|
| 277 |
int nTmpArgs; |
| 278 |
EKeywordType eTempKeyword; |
| 279 |
|
| 280 |
switch (graphicsStack.GetNonStrokingColorSpace()) |
| 281 |
{ |
| 282 |
case PdfColorSpaceType::DeviceGray: |
| 283 |
nTmpArgs = 1; |
| 284 |
eTempKeyword = eKeyword_SelectGray_NonStroking; |
| 285 |
break; |
| 286 |
case PdfColorSpaceType::DeviceRGB: |
| 287 |
nTmpArgs = 3; |
| 288 |
eTempKeyword = eKeyword_SelectRGB_NonStroking; |
| 289 |
break; |
| 290 |
case PdfColorSpaceType::DeviceCMYK: |
| 291 |
nTmpArgs = 4; |
| 292 |
eTempKeyword = eKeyword_SelectCMYK_NonStroking; |
| 293 |
break; |
| 294 |
|
| 295 |
case PdfColorSpaceType::Separation: |
| 296 |
case PdfColorSpaceType::Lab: |
| 297 |
case PdfColorSpaceType::Indexed: |
| 298 |
case PdfColorSpaceType::Unknown: |
| 299 |
|
| 300 |
default: |
| 301 |
{ |
| 302 |
PoDoFo::LogMessage(PdfLogSeverity::Error, "Unknown color space {} type.\n", |
| 303 |
PoDoFo::ToString(graphicsStack.GetNonStrokingColorSpace())); |
| 304 |
PODOFO_RAISE_ERROR(PdfErrorCode::CannotConvertColor); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
keyword = |
| 309 |
this->ProcessColor(eTempKeyword, nTmpArgs, args, graphicsStack); |
| 310 |
break; |
| 311 |
} |
| 312 |
case eKeyword_Undefined: |
| 313 |
//PdfError::LogMessage( LogSeverity::Error, "Unknown keyword type.\n" ); |
| 314 |
break; |
| 315 |
default: |
| 316 |
break; |
| 317 |
} |
| 318 |
|
| 319 |
WriteArgumentsAndKeyword(args, keyword, device); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// Write arguments if there are any left |
| 324 |
WriteArgumentsAndKeyword(args, { }, device); |
| 325 |
// Set new contents stream |
| 326 |
page.GetOrCreateContentsStream(PdfStreamAppendFlags::None). |
| 327 |
SetData(buffer); |
| 328 |
} |
| 329 |
|
| 330 |
void ColorChanger::WriteArgumentsAndKeyword(vector<PdfVariant>& args, const string_view& keyword, OutputStreamDevice& device) |
| 331 |
{ |
| 332 |
charbuff buffer; |
| 333 |
vector<PdfVariant>::const_iterator it = args.begin(); |
| 334 |
while (it != args.end()) |
| 335 |
{ |
| 336 |
(*it).Write(device, PdfWriteFlags::None, { }, buffer); |
| 337 |
it++; |
| 338 |
} |
| 339 |
|
| 340 |
args.clear(); |
| 341 |
|
| 342 |
if (!keyword.empty()) |
| 343 |
{ |
| 344 |
device.Write(" ", 1); |
| 345 |
device.Write(keyword); |
| 346 |
device.Write("\n", 1); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
const ColorChanger::KWInfo* ColorChanger::FindKeyWordByName(const string_view& keyword) |
| 351 |
{ |
| 352 |
PODOFO_RAISE_LOGIC_IF(keyword.empty(), "Keyword cannot be NULL."); |
| 353 |
|
| 354 |
const KWInfo* pInfo = &(kwInfo[0]); |
| 355 |
while (pInfo->eKeywordType != eKeyword_Undefined) |
| 356 |
{ |
| 357 |
if (strcmp(pInfo->pszText, keyword.data()) == 0) |
| 358 |
{ |
| 359 |
return pInfo; |
| 360 |
} |
| 361 |
|
| 362 |
pInfo++; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
return pInfo; |
| 367 |
} |
| 368 |
|
| 369 |
void ColorChanger::PutColorOnStack(const PdfColor& rColor, vector<PdfVariant>& args) |
| 370 |
{ |
| 371 |
switch (rColor.GetColorSpace()) |
| 372 |
{ |
| 373 |
case PdfColorSpaceType::DeviceGray: |
| 374 |
args.push_back(rColor.GetGrayScale()); |
| 375 |
break; |
| 376 |
|
| 377 |
case PdfColorSpaceType::DeviceRGB: |
| 378 |
args.push_back(rColor.GetRed()); |
| 379 |
args.push_back(rColor.GetGreen()); |
| 380 |
args.push_back(rColor.GetBlue()); |
| 381 |
break; |
| 382 |
|
| 383 |
case PdfColorSpaceType::DeviceCMYK: |
| 384 |
args.push_back(rColor.GetCyan()); |
| 385 |
args.push_back(rColor.GetMagenta()); |
| 386 |
args.push_back(rColor.GetYellow()); |
| 387 |
args.push_back(rColor.GetBlack()); |
| 388 |
break; |
| 389 |
|
| 390 |
case PdfColorSpaceType::Separation: |
| 391 |
case PdfColorSpaceType::Lab: |
| 392 |
case PdfColorSpaceType::Indexed: |
| 393 |
case PdfColorSpaceType::Unknown: |
| 394 |
|
| 395 |
default: |
| 396 |
{ |
| 397 |
PODOFO_RAISE_ERROR(PdfErrorCode::CannotConvertColor); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
PdfColor ColorChanger::GetColorFromStack(int nArgs, vector<PdfVariant>& args) |
| 403 |
{ |
| 404 |
PdfColor color; |
| 405 |
|
| 406 |
double gray = -1.0; |
| 407 |
double red = -1.0, green = -1.0, blue = -1.0; |
| 408 |
double cyan = -1.0, magenta = -1.0, yellow = -1.0, black = -1.0; |
| 409 |
switch (nArgs) |
| 410 |
{ |
| 411 |
case 1: |
| 412 |
gray = args.back().GetReal(); |
| 413 |
args.pop_back(); |
| 414 |
color = PdfColor(gray); |
| 415 |
break; |
| 416 |
case 3: |
| 417 |
blue = args.back().GetReal(); |
| 418 |
args.pop_back(); |
| 419 |
green = args.back().GetReal(); |
| 420 |
args.pop_back(); |
| 421 |
red = args.back().GetReal(); |
| 422 |
args.pop_back(); |
| 423 |
color = PdfColor(red, green, blue); |
| 424 |
break; |
| 425 |
case 4: |
| 426 |
black = args.back().GetReal(); |
| 427 |
args.pop_back(); |
| 428 |
yellow = args.back().GetReal(); |
| 429 |
args.pop_back(); |
| 430 |
magenta = args.back().GetReal(); |
| 431 |
args.pop_back(); |
| 432 |
cyan = args.back().GetReal(); |
| 433 |
args.pop_back(); |
| 434 |
color = PdfColor(cyan, magenta, yellow, black); |
| 435 |
break; |
| 436 |
} |
| 437 |
|
| 438 |
return color; |
| 439 |
} |
| 440 |
|
| 441 |
const char* ColorChanger::ProcessColor(EKeywordType eKeywordType, int nNumArgs, vector<PdfVariant>& args, GraphicsStack& rGraphicsStack) |
| 442 |
{ |
| 443 |
PdfColor newColor; |
| 444 |
bool bStroking = false; |
| 445 |
PdfColor color = this->GetColorFromStack(nNumArgs, args); |
| 446 |
|
| 447 |
switch (eKeywordType) |
| 448 |
{ |
| 449 |
|
| 450 |
case eKeyword_SelectGray_Stroking: |
| 451 |
bStroking = true; |
| 452 |
rGraphicsStack.SetStrokingColorSpace(PdfColorSpaceType::DeviceGray); |
| 453 |
newColor = m_converter->SetStrokingColorGray(color); |
| 454 |
break; |
| 455 |
|
| 456 |
case eKeyword_SelectRGB_Stroking: |
| 457 |
bStroking = true; |
| 458 |
rGraphicsStack.SetStrokingColorSpace(PdfColorSpaceType::DeviceRGB); |
| 459 |
newColor = m_converter->SetStrokingColorRGB(color); |
| 460 |
break; |
| 461 |
|
| 462 |
case eKeyword_SelectCMYK_Stroking: |
| 463 |
bStroking = true; |
| 464 |
rGraphicsStack.SetStrokingColorSpace(PdfColorSpaceType::DeviceCMYK); |
| 465 |
newColor = m_converter->SetStrokingColorCMYK(color); |
| 466 |
break; |
| 467 |
|
| 468 |
case eKeyword_SelectGray_NonStroking: |
| 469 |
rGraphicsStack.SetNonStrokingColorSpace(PdfColorSpaceType::DeviceGray); |
| 470 |
newColor = m_converter->SetNonStrokingColorGray(color); |
| 471 |
break; |
| 472 |
|
| 473 |
case eKeyword_SelectRGB_NonStroking: |
| 474 |
rGraphicsStack.SetNonStrokingColorSpace(PdfColorSpaceType::DeviceRGB); |
| 475 |
newColor = m_converter->SetNonStrokingColorRGB(color); |
| 476 |
break; |
| 477 |
|
| 478 |
case eKeyword_SelectCMYK_NonStroking: |
| 479 |
rGraphicsStack.SetNonStrokingColorSpace(PdfColorSpaceType::DeviceCMYK); |
| 480 |
newColor = m_converter->SetNonStrokingColorCMYK(color); |
| 481 |
break; |
| 482 |
|
| 483 |
case eKeyword_GraphicsStack_Push: |
| 484 |
case eKeyword_GraphicsStack_Pop: |
| 485 |
case eKeyword_SelectColorSpace_Stroking: |
| 486 |
case eKeyword_SelectColorSpace_NonStroking: |
| 487 |
case eKeyword_SelectColor_Stroking: |
| 488 |
case eKeyword_SelectColor_Stroking2: |
| 489 |
case eKeyword_SelectColor_NonStroking: |
| 490 |
case eKeyword_SelectColor_NonStroking2: |
| 491 |
case eKeyword_Undefined: |
| 492 |
|
| 493 |
default: |
| 494 |
{ |
| 495 |
PODOFO_RAISE_ERROR(PdfErrorCode::CannotConvertColor); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
this->PutColorOnStack(newColor, args); |
| 501 |
if (bStroking) |
| 502 |
{ |
| 503 |
rGraphicsStack.SetStrokingColor(newColor); |
| 504 |
} |
| 505 |
else |
| 506 |
{ |
| 507 |
rGraphicsStack.SetNonStrokingColor(newColor); |
| 508 |
} |
| 509 |
|
| 510 |
return this->GetKeywordForColor(newColor, bStroking); |
| 511 |
} |
| 512 |
|
| 513 |
const char* ColorChanger::GetKeywordForColor(const PdfColor& rColor, bool bIsStroking) |
| 514 |
{ |
| 515 |
const char* pszKeyword = NULL; |
| 516 |
|
| 517 |
switch (rColor.GetColorSpace()) |
| 518 |
{ |
| 519 |
case PdfColorSpaceType::DeviceGray: |
| 520 |
pszKeyword = (bIsStroking ? "G" : "g"); |
| 521 |
break; |
| 522 |
|
| 523 |
case PdfColorSpaceType::DeviceRGB: |
| 524 |
pszKeyword = (bIsStroking ? "RG" : "rg"); |
| 525 |
break; |
| 526 |
|
| 527 |
case PdfColorSpaceType::DeviceCMYK: |
| 528 |
pszKeyword = (bIsStroking ? "K" : "k"); |
| 529 |
break; |
| 530 |
|
| 531 |
case PdfColorSpaceType::Separation: |
| 532 |
case PdfColorSpaceType::Lab: |
| 533 |
case PdfColorSpaceType::Indexed: |
| 534 |
case PdfColorSpaceType::Unknown: |
| 535 |
|
| 536 |
default: |
| 537 |
{ |
| 538 |
PODOFO_RAISE_ERROR(PdfErrorCode::CannotConvertColor); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
return pszKeyword; |
| 543 |
} |
| 544 |
|
| 545 |
PdfColorSpaceType ColorChanger::GetColorSpaceForName(const PdfName& name, PdfCanvas& page) |
| 546 |
{ |
| 547 |
PdfColorSpaceType colorSpace = PoDoFo::ConvertTo<PdfColorSpaceType>(name); |
| 548 |
|
| 549 |
if (colorSpace == PdfColorSpaceType::Unknown) |
| 550 |
{ |
| 551 |
// See if we can find it in the resource dictionary of the current page |
| 552 |
auto resources = page.GetResources(); |
| 553 |
if (resources != NULL |
| 554 |
&& resources->GetDictionary().HasKey("ColorSpace")) |
| 555 |
{ |
| 556 |
auto colorSpaces = resources->GetDictionary().FindKey("ColorSpace"); |
| 557 |
if (colorSpaces != NULL |
| 558 |
&& colorSpaces->GetDictionary().HasKey(name)) |
| 559 |
{ |
| 560 |
auto cs = colorSpaces->GetDictionary().FindKey(name); |
| 561 |
if (!cs) |
| 562 |
{ |
| 563 |
PODOFO_RAISE_ERROR(PdfErrorCode::ObjectNotFound); |
| 564 |
} |
| 565 |
else if (cs->IsName()) |
| 566 |
{ |
| 567 |
return this->GetColorSpaceForName(cs->GetName(), page); |
| 568 |
} |
| 569 |
else if (cs->IsArray()) |
| 570 |
{ |
| 571 |
return this->GetColorSpaceForArray(cs->GetArray(), page); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
return colorSpace; |
| 578 |
} |
| 579 |
|
| 580 |
PdfColorSpaceType ColorChanger::GetColorSpaceForArray(const PdfArray&, PdfCanvas&) |
| 581 |
{ |
| 582 |
PdfColorSpaceType colorSpace = PdfColorSpaceType::Unknown; |
| 583 |
|
| 584 |
// CIE Based: [name dictionary] |
| 585 |
// CalGray |
| 586 |
// CalRGB |
| 587 |
// CalLab |
| 588 |
// ICCBased [name stream] |
| 589 |
// Special: |
| 590 |
// Pattern |
| 591 |
// Indexed [/Indexed base hival lookup] |
| 592 |
// Separation [/Separation name alternateSpace tintTransform] |
| 593 |
// DeviceN [/DeviceN names alternateSpace tintTransform] or |
| 594 |
// [/DeviceN names alternateSpace tintTransform attributes] |
| 595 |
// |
| 596 |
|
| 597 |
return colorSpace; |
| 598 |
} |