Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_LowLevelGraphicsPostScriptRenderer.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. // this will throw an assertion if you try to draw something that's not
  22. // possible in postscript
  23. #define WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS 0
  24. //==============================================================================
  25. #if JUCE_DEBUG && WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS
  26. #define notPossibleInPostscriptAssert jassertfalse
  27. #else
  28. #define notPossibleInPostscriptAssert
  29. #endif
  30. //==============================================================================
  31. LowLevelGraphicsPostScriptRenderer::LowLevelGraphicsPostScriptRenderer (OutputStream& resultingPostScript,
  32. const String& documentTitle,
  33. const int totalWidth_,
  34. const int totalHeight_)
  35. : out (resultingPostScript),
  36. totalWidth (totalWidth_),
  37. totalHeight (totalHeight_),
  38. needToClip (true)
  39. {
  40. stateStack.add (new SavedState());
  41. stateStack.getLast()->clip = Rectangle<int> (totalWidth_, totalHeight_);
  42. const float scale = jmin ((520.0f / totalWidth_), (750.0f / totalHeight));
  43. out << "%!PS-Adobe-3.0 EPSF-3.0"
  44. "\n%%BoundingBox: 0 0 600 824"
  45. "\n%%Pages: 0"
  46. "\n%%Creator: ROLI Ltd. JUCE"
  47. "\n%%Title: " << documentTitle <<
  48. "\n%%CreationDate: none"
  49. "\n%%LanguageLevel: 2"
  50. "\n%%EndComments"
  51. "\n%%BeginProlog"
  52. "\n%%BeginResource: JRes"
  53. "\n/bd {bind def} bind def"
  54. "\n/c {setrgbcolor} bd"
  55. "\n/m {moveto} bd"
  56. "\n/l {lineto} bd"
  57. "\n/rl {rlineto} bd"
  58. "\n/ct {curveto} bd"
  59. "\n/cp {closepath} bd"
  60. "\n/pr {3 index 3 index moveto 1 index 0 rlineto 0 1 index rlineto pop neg 0 rlineto pop pop closepath} bd"
  61. "\n/doclip {initclip newpath} bd"
  62. "\n/endclip {clip newpath} bd"
  63. "\n%%EndResource"
  64. "\n%%EndProlog"
  65. "\n%%BeginSetup"
  66. "\n%%EndSetup"
  67. "\n%%Page: 1 1"
  68. "\n%%BeginPageSetup"
  69. "\n%%EndPageSetup\n\n"
  70. << "40 800 translate\n"
  71. << scale << ' ' << scale << " scale\n\n";
  72. }
  73. LowLevelGraphicsPostScriptRenderer::~LowLevelGraphicsPostScriptRenderer()
  74. {
  75. }
  76. //==============================================================================
  77. bool LowLevelGraphicsPostScriptRenderer::isVectorDevice() const
  78. {
  79. return true;
  80. }
  81. void LowLevelGraphicsPostScriptRenderer::setOrigin (Point<int> o)
  82. {
  83. if (! o.isOrigin())
  84. {
  85. stateStack.getLast()->xOffset += o.x;
  86. stateStack.getLast()->yOffset += o.y;
  87. needToClip = true;
  88. }
  89. }
  90. void LowLevelGraphicsPostScriptRenderer::addTransform (const AffineTransform& /*transform*/)
  91. {
  92. //xxx
  93. jassertfalse;
  94. }
  95. float LowLevelGraphicsPostScriptRenderer::getPhysicalPixelScaleFactor() { return 1.0f; }
  96. bool LowLevelGraphicsPostScriptRenderer::clipToRectangle (const Rectangle<int>& r)
  97. {
  98. needToClip = true;
  99. return stateStack.getLast()->clip.clipTo (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
  100. }
  101. bool LowLevelGraphicsPostScriptRenderer::clipToRectangleList (const RectangleList<int>& clipRegion)
  102. {
  103. needToClip = true;
  104. return stateStack.getLast()->clip.clipTo (clipRegion);
  105. }
  106. void LowLevelGraphicsPostScriptRenderer::excludeClipRectangle (const Rectangle<int>& r)
  107. {
  108. needToClip = true;
  109. stateStack.getLast()->clip.subtract (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
  110. }
  111. void LowLevelGraphicsPostScriptRenderer::clipToPath (const Path& path, const AffineTransform& transform)
  112. {
  113. writeClip();
  114. Path p (path);
  115. p.applyTransform (transform.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset));
  116. writePath (p);
  117. out << "clip\n";
  118. }
  119. void LowLevelGraphicsPostScriptRenderer::clipToImageAlpha (const Image& /*sourceImage*/, const AffineTransform& /*transform*/)
  120. {
  121. needToClip = true;
  122. jassertfalse; // xxx
  123. }
  124. bool LowLevelGraphicsPostScriptRenderer::clipRegionIntersects (const Rectangle<int>& r)
  125. {
  126. return stateStack.getLast()->clip.intersectsRectangle (r.translated (stateStack.getLast()->xOffset, stateStack.getLast()->yOffset));
  127. }
  128. Rectangle<int> LowLevelGraphicsPostScriptRenderer::getClipBounds() const
  129. {
  130. return stateStack.getLast()->clip.getBounds().translated (-stateStack.getLast()->xOffset,
  131. -stateStack.getLast()->yOffset);
  132. }
  133. bool LowLevelGraphicsPostScriptRenderer::isClipEmpty() const
  134. {
  135. return stateStack.getLast()->clip.isEmpty();
  136. }
  137. //==============================================================================
  138. LowLevelGraphicsPostScriptRenderer::SavedState::SavedState()
  139. : xOffset (0),
  140. yOffset (0)
  141. {
  142. }
  143. LowLevelGraphicsPostScriptRenderer::SavedState::~SavedState()
  144. {
  145. }
  146. void LowLevelGraphicsPostScriptRenderer::saveState()
  147. {
  148. stateStack.add (new SavedState (*stateStack.getLast()));
  149. }
  150. void LowLevelGraphicsPostScriptRenderer::restoreState()
  151. {
  152. jassert (stateStack.size() > 0);
  153. if (stateStack.size() > 0)
  154. stateStack.removeLast();
  155. }
  156. void LowLevelGraphicsPostScriptRenderer::beginTransparencyLayer (float)
  157. {
  158. }
  159. void LowLevelGraphicsPostScriptRenderer::endTransparencyLayer()
  160. {
  161. }
  162. //==============================================================================
  163. void LowLevelGraphicsPostScriptRenderer::writeClip()
  164. {
  165. if (needToClip)
  166. {
  167. needToClip = false;
  168. out << "doclip ";
  169. int itemsOnLine = 0;
  170. for (auto& i : stateStack.getLast()->clip)
  171. {
  172. if (++itemsOnLine == 6)
  173. {
  174. itemsOnLine = 0;
  175. out << '\n';
  176. }
  177. out << i.getX() << ' ' << -i.getY() << ' '
  178. << i.getWidth() << ' ' << -i.getHeight() << " pr ";
  179. }
  180. out << "endclip\n";
  181. }
  182. }
  183. void LowLevelGraphicsPostScriptRenderer::writeColour (Colour colour)
  184. {
  185. Colour c (Colours::white.overlaidWith (colour));
  186. if (lastColour != c)
  187. {
  188. lastColour = c;
  189. out << String (c.getFloatRed(), 3) << ' '
  190. << String (c.getFloatGreen(), 3) << ' '
  191. << String (c.getFloatBlue(), 3) << " c\n";
  192. }
  193. }
  194. void LowLevelGraphicsPostScriptRenderer::writeXY (const float x, const float y) const
  195. {
  196. out << String (x, 2) << ' '
  197. << String (-y, 2) << ' ';
  198. }
  199. void LowLevelGraphicsPostScriptRenderer::writePath (const Path& path) const
  200. {
  201. out << "newpath ";
  202. float lastX = 0.0f;
  203. float lastY = 0.0f;
  204. int itemsOnLine = 0;
  205. Path::Iterator i (path);
  206. while (i.next())
  207. {
  208. if (++itemsOnLine == 4)
  209. {
  210. itemsOnLine = 0;
  211. out << '\n';
  212. }
  213. switch (i.elementType)
  214. {
  215. case Path::Iterator::startNewSubPath:
  216. writeXY (i.x1, i.y1);
  217. lastX = i.x1;
  218. lastY = i.y1;
  219. out << "m ";
  220. break;
  221. case Path::Iterator::lineTo:
  222. writeXY (i.x1, i.y1);
  223. lastX = i.x1;
  224. lastY = i.y1;
  225. out << "l ";
  226. break;
  227. case Path::Iterator::quadraticTo:
  228. {
  229. const float cp1x = lastX + (i.x1 - lastX) * 2.0f / 3.0f;
  230. const float cp1y = lastY + (i.y1 - lastY) * 2.0f / 3.0f;
  231. const float cp2x = cp1x + (i.x2 - lastX) / 3.0f;
  232. const float cp2y = cp1y + (i.y2 - lastY) / 3.0f;
  233. writeXY (cp1x, cp1y);
  234. writeXY (cp2x, cp2y);
  235. writeXY (i.x2, i.y2);
  236. out << "ct ";
  237. lastX = i.x2;
  238. lastY = i.y2;
  239. }
  240. break;
  241. case Path::Iterator::cubicTo:
  242. writeXY (i.x1, i.y1);
  243. writeXY (i.x2, i.y2);
  244. writeXY (i.x3, i.y3);
  245. out << "ct ";
  246. lastX = i.x3;
  247. lastY = i.y3;
  248. break;
  249. case Path::Iterator::closePath:
  250. out << "cp ";
  251. break;
  252. default:
  253. jassertfalse;
  254. break;
  255. }
  256. }
  257. out << '\n';
  258. }
  259. void LowLevelGraphicsPostScriptRenderer::writeTransform (const AffineTransform& trans) const
  260. {
  261. out << "[ "
  262. << trans.mat00 << ' '
  263. << trans.mat10 << ' '
  264. << trans.mat01 << ' '
  265. << trans.mat11 << ' '
  266. << trans.mat02 << ' '
  267. << trans.mat12 << " ] concat ";
  268. }
  269. //==============================================================================
  270. void LowLevelGraphicsPostScriptRenderer::setFill (const FillType& fillType)
  271. {
  272. stateStack.getLast()->fillType = fillType;
  273. }
  274. void LowLevelGraphicsPostScriptRenderer::setOpacity (float /*opacity*/)
  275. {
  276. }
  277. void LowLevelGraphicsPostScriptRenderer::setInterpolationQuality (Graphics::ResamplingQuality /*quality*/)
  278. {
  279. }
  280. //==============================================================================
  281. void LowLevelGraphicsPostScriptRenderer::fillRect (const Rectangle<int>& r, const bool /*replaceExistingContents*/)
  282. {
  283. fillRect (r.toFloat());
  284. }
  285. void LowLevelGraphicsPostScriptRenderer::fillRect (const Rectangle<float>& r)
  286. {
  287. if (stateStack.getLast()->fillType.isColour())
  288. {
  289. writeClip();
  290. writeColour (stateStack.getLast()->fillType.colour);
  291. Rectangle<float> r2 (r.translated ((float) stateStack.getLast()->xOffset,
  292. (float) stateStack.getLast()->yOffset));
  293. out << r2.getX() << ' ' << -r2.getBottom() << ' ' << r2.getWidth() << ' ' << r2.getHeight() << " rectfill\n";
  294. }
  295. else
  296. {
  297. Path p;
  298. p.addRectangle (r);
  299. fillPath (p, AffineTransform());
  300. }
  301. }
  302. void LowLevelGraphicsPostScriptRenderer::fillRectList (const RectangleList<float>& list)
  303. {
  304. fillPath (list.toPath(), AffineTransform());
  305. }
  306. //==============================================================================
  307. void LowLevelGraphicsPostScriptRenderer::fillPath (const Path& path, const AffineTransform& t)
  308. {
  309. if (stateStack.getLast()->fillType.isColour())
  310. {
  311. writeClip();
  312. Path p (path);
  313. p.applyTransform (t.translated ((float) stateStack.getLast()->xOffset,
  314. (float) stateStack.getLast()->yOffset));
  315. writePath (p);
  316. writeColour (stateStack.getLast()->fillType.colour);
  317. out << "fill\n";
  318. }
  319. else if (stateStack.getLast()->fillType.isGradient())
  320. {
  321. // this doesn't work correctly yet - it could be improved to handle solid gradients, but
  322. // postscript can't do semi-transparent ones.
  323. notPossibleInPostscriptAssert; // you can disable this warning by setting the WARN_ABOUT_NON_POSTSCRIPT_OPERATIONS flag at the top of this file
  324. writeClip();
  325. out << "gsave ";
  326. {
  327. Path p (path);
  328. p.applyTransform (t.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset));
  329. writePath (p);
  330. out << "clip\n";
  331. }
  332. const Rectangle<int> bounds (stateStack.getLast()->clip.getBounds());
  333. // ideally this would draw lots of lines or ellipses to approximate the gradient, but for the
  334. // time-being, this just fills it with the average colour..
  335. writeColour (stateStack.getLast()->fillType.gradient->getColourAtPosition (0.5f));
  336. out << bounds.getX() << ' ' << -bounds.getBottom() << ' ' << bounds.getWidth() << ' ' << bounds.getHeight() << " rectfill\n";
  337. out << "grestore\n";
  338. }
  339. }
  340. //==============================================================================
  341. void LowLevelGraphicsPostScriptRenderer::writeImage (const Image& im,
  342. const int sx, const int sy,
  343. const int maxW, const int maxH) const
  344. {
  345. out << "{<\n";
  346. const int w = jmin (maxW, im.getWidth());
  347. const int h = jmin (maxH, im.getHeight());
  348. int charsOnLine = 0;
  349. const Image::BitmapData srcData (im, 0, 0, w, h);
  350. Colour pixel;
  351. for (int y = h; --y >= 0;)
  352. {
  353. for (int x = 0; x < w; ++x)
  354. {
  355. const uint8* pixelData = srcData.getPixelPointer (x, y);
  356. if (x >= sx && y >= sy)
  357. {
  358. if (im.isARGB())
  359. {
  360. PixelARGB p (*(const PixelARGB*) pixelData);
  361. p.unpremultiply();
  362. pixel = Colours::white.overlaidWith (Colour (p));
  363. }
  364. else if (im.isRGB())
  365. {
  366. pixel = Colour (*((const PixelRGB*) pixelData));
  367. }
  368. else
  369. {
  370. pixel = Colour ((uint8) 0, (uint8) 0, (uint8) 0, *pixelData);
  371. }
  372. }
  373. else
  374. {
  375. pixel = Colours::transparentWhite;
  376. }
  377. const uint8 pixelValues[3] = { pixel.getRed(), pixel.getGreen(), pixel.getBlue() };
  378. out << String::toHexString (pixelValues, 3, 0);
  379. charsOnLine += 3;
  380. if (charsOnLine > 100)
  381. {
  382. out << '\n';
  383. charsOnLine = 0;
  384. }
  385. }
  386. }
  387. out << "\n>}\n";
  388. }
  389. void LowLevelGraphicsPostScriptRenderer::drawImage (const Image& sourceImage, const AffineTransform& transform)
  390. {
  391. const int w = sourceImage.getWidth();
  392. const int h = sourceImage.getHeight();
  393. writeClip();
  394. out << "gsave ";
  395. writeTransform (transform.translated ((float) stateStack.getLast()->xOffset, (float) stateStack.getLast()->yOffset)
  396. .scaled (1.0f, -1.0f));
  397. RectangleList<int> imageClip;
  398. sourceImage.createSolidAreaMask (imageClip, 0.5f);
  399. out << "newpath ";
  400. int itemsOnLine = 0;
  401. for (auto& i : imageClip)
  402. {
  403. if (++itemsOnLine == 6)
  404. {
  405. out << '\n';
  406. itemsOnLine = 0;
  407. }
  408. out << i.getX() << ' ' << i.getY() << ' ' << i.getWidth() << ' ' << i.getHeight() << " pr ";
  409. }
  410. out << " clip newpath\n";
  411. out << w << ' ' << h << " scale\n";
  412. out << w << ' ' << h << " 8 [" << w << " 0 0 -" << h << ' ' << (int) 0 << ' ' << h << " ]\n";
  413. writeImage (sourceImage, 0, 0, w, h);
  414. out << "false 3 colorimage grestore\n";
  415. needToClip = true;
  416. }
  417. //==============================================================================
  418. void LowLevelGraphicsPostScriptRenderer::drawLine (const Line <float>& line)
  419. {
  420. Path p;
  421. p.addLineSegment (line, 1.0f);
  422. fillPath (p, AffineTransform());
  423. }
  424. //==============================================================================
  425. void LowLevelGraphicsPostScriptRenderer::setFont (const Font& newFont)
  426. {
  427. stateStack.getLast()->font = newFont;
  428. }
  429. const Font& LowLevelGraphicsPostScriptRenderer::getFont()
  430. {
  431. return stateStack.getLast()->font;
  432. }
  433. void LowLevelGraphicsPostScriptRenderer::drawGlyph (int glyphNumber, const AffineTransform& transform)
  434. {
  435. Path p;
  436. Font& font = stateStack.getLast()->font;
  437. font.getTypeface()->getOutlineForGlyph (glyphNumber, p);
  438. fillPath (p, AffineTransform::scale (font.getHeight() * font.getHorizontalScale(), font.getHeight()).followedBy (transform));
  439. }
  440. } // namespace juce