The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

543 lines
17KB

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