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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
// this will throw an assertion if you try to draw something that's not
// possible in postscript
#define WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS 0
//==============================================================================
#if JUCE_DEBUG && WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS
#define notPossibleInPostscriptAssert jassertfalse
#else
#define notPossibleInPostscriptAssert
#endif
//==============================================================================
LowLevelGraphicsPostScriptRenderer::LowLevelGraphicsPostScriptRenderer (OutputStream& resultingPostScript,
const String& documentTitle,
const int totalWidth_,
const int totalHeight_)
: out (resultingPostScript),
totalWidth (totalWidth_),
totalHeight (totalHeight_),
needToClip (true)
{
stateStack.add (new SavedState());
stateStack.getLast()->clip = Rectangle<int> (totalWidth_, totalHeight_);
const float scale = jmin ((520.0f / (float) totalWidth_), (750.0f / (float) totalHeight));
out << "%!PS-Adobe-3.0 EPSF-3.0"
"\n%%BoundingBox: 0 0 600 824"
"\n%%Pages: 0"
"\n%%Creator: Raw Material Software Limited - JUCE"
"\n%%Title: " << documentTitle <<
"\n%%CreationDate: none"
"\n%%LanguageLevel: 2"
"\n%%EndComments"
"\n%%BeginProlog"
"\n%%BeginResource: JRes"
"\n/bd {bind def} bind def"
"\n/c {setrgbcolor} bd"
"\n/m {moveto} bd"
"\n/l {lineto} bd"
"\n/rl {rlineto} bd"
"\n/ct {curveto} bd"
"\n/cp {closepath} bd"
"\n/pr {3 index 3 index moveto 1 index 0 rlineto 0 1 index rlineto pop neg 0 rlineto pop pop closepath} bd"
"\n/doclip {initclip newpath} bd"
"\n/endclip {clip newpath} bd"
"\n%%EndResource"
"\n%%EndProlog"
"\n%%BeginSetup"
"\n%%EndSetup"
"\n%%Page: 1 1"
"\n%%BeginPageSetup"
"\n%%EndPageSetup\n\n"
<< "40 800 translate\n"
<< scale << ' ' << scale << " scale\n\n";
}
//==============================================================================
bool LowLevelGraphicsPostScriptRenderer::isVectorDevice() const
{
return true;
}
void LowLevelGraphicsPostScriptRenderer::setOrigin (Point<int> o)
{
if (! o.isOrigin())
{
stateStack.getLast()->xOffset += o.x;
stateStack.getLast()->yOffset += o.y;
needToClip = true;
}
}
void LowLevelGraphicsPostScriptRenderer::addTransform (const AffineTransform& /*transform*/)
{
//xxx
jassertfalse;
}
float LowLevelGraphicsPostScriptRenderer::getPhysicalPixelScaleFactor() { return 1.0f; }
bool LowLevelGraphicsPostScriptRenderer::clipToRectangle (const Rectangle<int>& r)
{
needToClip = true;
return stateStack.getLast()->clip.clipTo (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
}
bool LowLevelGraphicsPostScriptRenderer::clipToRectangleList (const RectangleList<int>& clipRegion)
{
needToClip = true;
return stateStack.getLast()->clip.clipTo (clipRegion);
}
void LowLevelGraphicsPostScriptRenderer::excludeClipRectangle (const Rectangle<int>& r)
{
needToClip = true;
stateStack.getLast()->clip.subtract (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
}
void LowLevelGraphicsPostScriptRenderer::clipToPath (const Path& path, const AffineTransform& transform)
{
writeClip();
Path p (path);
p.applyTransform (transform.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset));
writePath (p);
out << "clip\n";
}
void LowLevelGraphicsPostScriptRenderer::clipToImageAlpha (const Image& /*sourceImage*/, const AffineTransform& /*transform*/)
{
needToClip = true;
jassertfalse; // xxx
}
bool LowLevelGraphicsPostScriptRenderer::clipRegionIntersects (const Rectangle<int>& r)
{
return stateStack.getLast()->clip.intersectsRectangle (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
}
Rectangle<int> LowLevelGraphicsPostScriptRenderer::getClipBounds() const
{
return stateStack.getLast()->clip.getBounds().translated (-stateStack.getLast()->xOffset,
-stateStack.getLast()->yOffset);
}
bool LowLevelGraphicsPostScriptRenderer::isClipEmpty() const
{
return stateStack.getLast()->clip.isEmpty();
}
//==============================================================================
LowLevelGraphicsPostScriptRenderer::SavedState::SavedState()
: xOffset (0),
yOffset (0)
{
}
void LowLevelGraphicsPostScriptRenderer::saveState()
{
stateStack.add (new SavedState (*stateStack.getLast()));
}
void LowLevelGraphicsPostScriptRenderer::restoreState()
{
jassert (stateStack.size() > 0);
if (stateStack.size() > 0)
stateStack.removeLast();
}
void LowLevelGraphicsPostScriptRenderer::beginTransparencyLayer (float)
{
}
void LowLevelGraphicsPostScriptRenderer::endTransparencyLayer()
{
}
//==============================================================================
void LowLevelGraphicsPostScriptRenderer::writeClip()
{
if (needToClip)
{
needToClip = false;
out << "doclip ";
int itemsOnLine = 0;
for (auto& i : stateStack.getLast()->clip)
{
if (++itemsOnLine == 6)
{
itemsOnLine = 0;
out << '\n';
}
out << i.getX() << ' ' << -i.getY() << ' '
<< i.getWidth() << ' ' << -i.getHeight() << " pr ";
}
out << "endclip\n";
}
}
void LowLevelGraphicsPostScriptRenderer::writeColour (Colour colour)
{
Colour c (Colours::white.overlaidWith (colour));
if (lastColour != c)
{
lastColour = c;
out << String (c.getFloatRed(), 3) << ' '
<< String (c.getFloatGreen(), 3) << ' '
<< String (c.getFloatBlue(), 3) << " c\n";
}
}
void LowLevelGraphicsPostScriptRenderer::writeXY (const float x, const float y) const
{
out << String (x, 2) << ' '
<< String (-y, 2) << ' ';
}
void LowLevelGraphicsPostScriptRenderer::writePath (const Path& path) const
{
out << "newpath ";
float lastX = 0.0f;
float lastY = 0.0f;
int itemsOnLine = 0;
Path::Iterator i (path);
while (i.next())
{
if (++itemsOnLine == 4)
{
itemsOnLine = 0;
out << '\n';
}
switch (i.elementType)
{
case Path::Iterator::startNewSubPath:
writeXY (i.x1, i.y1);
lastX = i.x1;
lastY = i.y1;
out << "m ";
break;
case Path::Iterator::lineTo:
writeXY (i.x1, i.y1);
lastX = i.x1;
lastY = i.y1;
out << "l ";
break;
case Path::Iterator::quadraticTo:
{
const float cp1x = lastX + (i.x1 - lastX) * 2.0f / 3.0f;
const float cp1y = lastY + (i.y1 - lastY) * 2.0f / 3.0f;
const float cp2x = cp1x + (i.x2 - lastX) / 3.0f;
const float cp2y = cp1y + (i.y2 - lastY) / 3.0f;
writeXY (cp1x, cp1y);
writeXY (cp2x, cp2y);
writeXY (i.x2, i.y2);
out << "ct ";
lastX = i.x2;
lastY = i.y2;
}
break;
case Path::Iterator::cubicTo:
writeXY (i.x1, i.y1);
writeXY (i.x2, i.y2);
writeXY (i.x3, i.y3);
out << "ct ";
lastX = i.x3;
lastY = i.y3;
break;
case Path::Iterator::closePath:
out << "cp ";
break;
default:
jassertfalse;
break;
}
}
out << '\n';
}
void LowLevelGraphicsPostScriptRenderer::writeTransform (const AffineTransform& trans) const
{
out << "[ "
<< trans.mat00 << ' '
<< trans.mat10 << ' '
<< trans.mat01 << ' '
<< trans.mat11 << ' '
<< trans.mat02 << ' '
<< trans.mat12 << " ] concat ";
}
//==============================================================================
void LowLevelGraphicsPostScriptRenderer::setFill (const FillType& fillType)
{
stateStack.getLast()->fillType = fillType;
}
void LowLevelGraphicsPostScriptRenderer::setOpacity (float /*opacity*/)
{
}
void LowLevelGraphicsPostScriptRenderer::setInterpolationQuality (Graphics::ResamplingQuality /*quality*/)
{
}
//==============================================================================
void LowLevelGraphicsPostScriptRenderer::fillRect (const Rectangle<int>& r, const bool /*replaceExistingContents*/)
{
fillRect (r.toFloat());
}
void LowLevelGraphicsPostScriptRenderer::fillRect (const Rectangle<float>& r)
{
if (stateStack.getLast()->fillType.isColour())
{
writeClip();
writeColour (stateStack.getLast()->fillType.colour);
auto r2 = r.translated ((float) stateStack.getLast()->xOffset,
(float) stateStack.getLast()->yOffset);
out << r2.getX() << ' ' << -r2.getBottom() << ' ' << r2.getWidth() << ' ' << r2.getHeight() << " rectfill\n";
}
else
{
Path p;
p.addRectangle (r);
fillPath (p, AffineTransform());
}
}
void LowLevelGraphicsPostScriptRenderer::fillRectList (const RectangleList<float>& list)
{
fillPath (list.toPath(), AffineTransform());
}
//==============================================================================
void LowLevelGraphicsPostScriptRenderer::fillPath (const Path& path, const AffineTransform& t)
{
if (stateStack.getLast()->fillType.isColour())
{
writeClip();
Path p (path);
p.applyTransform (t.translated ((float) stateStack.getLast()->xOffset,
(float) stateStack.getLast()->yOffset));
writePath (p);
writeColour (stateStack.getLast()->fillType.colour);
out << "fill\n";
}
else if (stateStack.getLast()->fillType.isGradient())
{
// this doesn't work correctly yet - it could be improved to handle solid gradients, but
// postscript can't do semi-transparent ones.
notPossibleInPostscriptAssert; // you can disable this warning by setting the WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS flag at the top of this file
writeClip();
out << "gsave ";
{
Path p (path);
p.applyTransform (t.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset));
writePath (p);
out << "clip\n";
}
auto bounds = stateStack.getLast()->clip.getBounds();
// ideally this would draw lots of lines or ellipses to approximate the gradient, but for the
// time-being, this just fills it with the average colour..
writeColour (stateStack.getLast()->fillType.gradient->getColourAtPosition (0.5f));
out << bounds.getX() << ' ' << -bounds.getBottom() << ' ' << bounds.getWidth() << ' ' << bounds.getHeight() << " rectfill\n";
out << "grestore\n";
}
}
//==============================================================================
void LowLevelGraphicsPostScriptRenderer::writeImage (const Image& im,
const int sx, const int sy,
const int maxW, const int maxH) const
{
out << "{<\n";
const int w = jmin (maxW, im.getWidth());
const int h = jmin (maxH, im.getHeight());
int charsOnLine = 0;
const Image::BitmapData srcData (im, 0, 0, w, h);
Colour pixel;
for (int y = h; --y >= 0;)
{
for (int x = 0; x < w; ++x)
{
const uint8* pixelData = srcData.getPixelPointer (x, y);
if (x >= sx && y >= sy)
{
if (im.isARGB())
{
PixelARGB p (*(const PixelARGB*) pixelData);
p.unpremultiply();
pixel = Colours::white.overlaidWith (Colour (p));
}
else if (im.isRGB())
{
pixel = Colour (*((const PixelRGB*) pixelData));
}
else
{
pixel = Colour ((uint8) 0, (uint8) 0, (uint8) 0, *pixelData);
}
}
else
{
pixel = Colours::transparentWhite;
}
const uint8 pixelValues[3] = { pixel.getRed(), pixel.getGreen(), pixel.getBlue() };
out << String::toHexString (pixelValues, 3, 0);
charsOnLine += 3;
if (charsOnLine > 100)
{
out << '\n';
charsOnLine = 0;
}
}
}
out << "\n>}\n";
}
void LowLevelGraphicsPostScriptRenderer::drawImage (const Image& sourceImage, const AffineTransform& transform)
{
const int w = sourceImage.getWidth();
const int h = sourceImage.getHeight();
writeClip();
out << "gsave ";
writeTransform (transform.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset)
.scaled (1.0f, -1.0f));
RectangleList<int> imageClip;
sourceImage.createSolidAreaMask (imageClip, 0.5f);
out << "newpath ";
int itemsOnLine = 0;
for (auto& i : imageClip)
{
if (++itemsOnLine == 6)
{
out << '\n';
itemsOnLine = 0;
}
out << i.getX() << ' ' << i.getY() << ' ' << i.getWidth() << ' ' << i.getHeight() << " pr ";
}
out << " clip newpath\n";
out << w << ' ' << h << " scale\n";
out << w << ' ' << h << " 8 [" << w << " 0 0 -" << h << ' ' << (int) 0 << ' ' << h << " ]\n";
writeImage (sourceImage, 0, 0, w, h);
out << "false 3 colorimage grestore\n";
needToClip = true;
}
//==============================================================================
void LowLevelGraphicsPostScriptRenderer::drawLine (const Line <float>& line)
{
Path p;
p.addLineSegment (line, 1.0f);
fillPath (p, AffineTransform());
}
//==============================================================================
void LowLevelGraphicsPostScriptRenderer::setFont (const Font& newFont)
{
stateStack.getLast()->font = newFont;
}
const Font& LowLevelGraphicsPostScriptRenderer::getFont()
{
return stateStack.getLast()->font;
}
void LowLevelGraphicsPostScriptRenderer::drawGlyph (int glyphNumber, const AffineTransform& transform)
{
Path p;
Font& font = stateStack.getLast()->font;
font.getTypefacePtr()->getOutlineForGlyph (glyphNumber, p);
fillPath (p, AffineTransform::scale (font.getHeight() * font.getHorizontalScale(), font.getHeight()).followedBy (transform));
}
} // namespace juce
|