DISTRHO Plugin Framework
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.

NanoVG.hpp 29KB

7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
8 years ago
8 years ago
7 years ago
7 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_NANO_WIDGET_HPP_INCLUDED
  17. #define DGL_NANO_WIDGET_HPP_INCLUDED
  18. #include "Color.hpp"
  19. #include "OpenGL.hpp"
  20. #include "Widget.hpp"
  21. #ifndef DGL_NO_SHARED_RESOURCES
  22. # define NANOVG_DEJAVU_SANS_TTF "__dpf_dejavusans_ttf__"
  23. #endif
  24. struct NVGcontext;
  25. struct NVGpaint;
  26. START_NAMESPACE_DGL
  27. // -----------------------------------------------------------------------
  28. // Forward class names
  29. class BlendishWidget;
  30. class NanoVG;
  31. // -----------------------------------------------------------------------
  32. // NanoImage
  33. /**
  34. NanoVG Image class.
  35. This implements NanoVG images as a C++ class where deletion is handled automatically.
  36. Images need to be created within a NanoVG or NanoWidget class.
  37. */
  38. class NanoImage
  39. {
  40. private:
  41. struct Handle {
  42. NVGcontext* context;
  43. int imageId;
  44. Handle() noexcept
  45. : context(nullptr),
  46. imageId(0) {}
  47. Handle(NVGcontext* c, int id) noexcept
  48. : context(c),
  49. imageId(id) {}
  50. };
  51. public:
  52. /**
  53. Constructor for an invalid/null image.
  54. */
  55. NanoImage();
  56. /**
  57. Constructor.
  58. */
  59. NanoImage(const Handle& handle);
  60. /**
  61. Destructor.
  62. */
  63. ~NanoImage();
  64. /**
  65. Create a new image without recreating the C++ class.
  66. */
  67. NanoImage& operator=(const Handle& handle);
  68. /**
  69. Wherever this image is valid.
  70. */
  71. bool isValid() const noexcept;
  72. /**
  73. Get size.
  74. */
  75. Size<uint> getSize() const noexcept;
  76. /**
  77. Get the OpenGL texture handle.
  78. */
  79. GLuint getTextureHandle() const;
  80. private:
  81. Handle fHandle;
  82. Size<uint> fSize;
  83. friend class NanoVG;
  84. /** @internal */
  85. void _updateSize();
  86. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NanoImage)
  87. };
  88. // -----------------------------------------------------------------------
  89. // NanoVG
  90. /**
  91. NanoVG class.
  92. This class exposes the NanoVG drawing API.
  93. All calls should be wrapped in beginFrame() and endFrame().
  94. @section State Handling
  95. NanoVG contains state which represents how paths will be rendered.
  96. The state contains transform, fill and stroke styles, text and font styles, and scissor clipping.
  97. @section Render styles
  98. Fill and stroke render style can be either a solid color or a paint which is a gradient or a pattern.
  99. Solid color is simply defined as a color value, different kinds of paints can be created
  100. using linearGradient(), boxGradient(), radialGradient() and imagePattern().
  101. Current render style can be saved and restored using save() and restore().
  102. @section Transforms
  103. The paths, gradients, patterns and scissor region are transformed by an transformation
  104. matrix at the time when they are passed to the API.
  105. The current transformation matrix is a affine matrix:
  106. [sx kx tx]
  107. [ky sy ty]
  108. [ 0 0 1]
  109. Where: sx,sy define scaling, kx,ky skewing, and tx,ty translation.
  110. The last row is assumed to be 0,0,1 and is not stored.
  111. Apart from resetTransform(), each transformation function first creates
  112. specific transformation matrix and pre-multiplies the current transformation by it.
  113. Current coordinate system (transformation) can be saved and restored using save() and restore().
  114. @section Images
  115. NanoVG allows you to load jpg, png, psd, tga, pic and gif files to be used for rendering.
  116. In addition you can upload your own image. The image loading is provided by stb_image.
  117. @section Paints
  118. NanoVG supports four types of paints: linear gradient, box gradient, radial gradient and image pattern.
  119. These can be used as paints for strokes and fills.
  120. @section Scissoring
  121. Scissoring allows you to clip the rendering into a rectangle. This is useful for various
  122. user interface cases like rendering a text edit or a timeline.
  123. @section Paths
  124. Drawing a new shape starts with beginPath(), it clears all the currently defined paths.
  125. Then you define one or more paths and sub-paths which describe the shape. The are functions
  126. to draw common shapes like rectangles and circles, and lower level step-by-step functions,
  127. which allow to define a path curve by curve.
  128. NanoVG uses even-odd fill rule to draw the shapes. Solid shapes should have counter clockwise
  129. winding and holes should have counter clockwise order. To specify winding of a path you can
  130. call pathWinding(). This is useful especially for the common shapes, which are drawn CCW.
  131. Finally you can fill the path using current fill style by calling fill(), and stroke it
  132. with current stroke style by calling stroke().
  133. The curve segments and sub-paths are transformed by the current transform.
  134. @section Text
  135. NanoVG allows you to load .ttf files and use the font to render text.
  136. The appearance of the text can be defined by setting the current text style
  137. and by specifying the fill color. Common text and font settings such as
  138. font size, letter spacing and text align are supported. Font blur allows you
  139. to create simple text effects such as drop shadows.
  140. At render time the font face can be set based on the font handles or name.
  141. Font measure functions return values in local space, the calculations are
  142. carried in the same resolution as the final rendering. This is done because
  143. the text glyph positions are snapped to the nearest pixels sharp rendering.
  144. The local space means that values are not rotated or scale as per the current
  145. transformation. For example if you set font size to 12, which would mean that
  146. line height is 16, then regardless of the current scaling and rotation, the
  147. returned line height is always 16. Some measures may vary because of the scaling
  148. since aforementioned pixel snapping.
  149. While this may sound a little odd, the setup allows you to always render the
  150. same way regardless of scaling. i.e. following works regardless of scaling:
  151. @code
  152. const char* txt = "Text me up.";
  153. vg.textBounds(x,y, txt, NULL, bounds);
  154. vg.beginPath();
  155. vg.roundedRect(bounds[0], bounds[1], bounds[2]-bounds[0], bounds[3]-bounds[1]);
  156. vg.fill();
  157. @endcode
  158. Note: currently only solid color fill is supported for text.
  159. */
  160. class NanoVG
  161. {
  162. public:
  163. enum CreateFlags {
  164. /**
  165. Flag indicating if geometry based anti-aliasing is used (may not be needed when using MSAA).
  166. */
  167. CREATE_ANTIALIAS = 1 << 0,
  168. /**
  169. Flag indicating if strokes should be drawn using stencil buffer. The rendering will be a little
  170. slower, but path overlaps (i.e. self-intersecting or sharp turns) will be drawn just once.
  171. */
  172. CREATE_STENCIL_STROKES = 1 << 1,
  173. /**
  174. Flag indicating that additional debug checks are done.
  175. */
  176. CREATE_DEBUG = 1 << 2,
  177. };
  178. enum ImageFlags {
  179. IMAGE_GENERATE_MIPMAPS = 1 << 0, // Generate mipmaps during creation of the image.
  180. IMAGE_REPEAT_X = 1 << 1, // Repeat image in X direction.
  181. IMAGE_REPEAT_Y = 1 << 2, // Repeat image in Y direction.
  182. IMAGE_FLIP_Y = 1 << 3, // Flips (inverses) image in Y direction when rendered.
  183. IMAGE_PREMULTIPLIED = 1 << 4 // Image data has premultiplied alpha.
  184. };
  185. enum Align {
  186. // Horizontal align
  187. ALIGN_LEFT = 1 << 0, // Align horizontally to left (default).
  188. ALIGN_CENTER = 1 << 1, // Align horizontally to center.
  189. ALIGN_RIGHT = 1 << 2, // Align horizontally to right.
  190. // Vertical align
  191. ALIGN_TOP = 1 << 3, // Align vertically to top.
  192. ALIGN_MIDDLE = 1 << 4, // Align vertically to middle.
  193. ALIGN_BOTTOM = 1 << 5, // Align vertically to bottom.
  194. ALIGN_BASELINE = 1 << 6 // Align vertically to baseline (default).
  195. };
  196. enum LineCap {
  197. BUTT,
  198. ROUND,
  199. SQUARE,
  200. BEVEL,
  201. MITER
  202. };
  203. enum Solidity {
  204. SOLID = 1, // CCW
  205. HOLE = 2 // CW
  206. };
  207. enum Winding {
  208. CCW = 1, // Winding for solid shapes
  209. CW = 2 // Winding for holes
  210. };
  211. struct Paint {
  212. float xform[6];
  213. float extent[2];
  214. float radius;
  215. float feather;
  216. Color innerColor;
  217. Color outerColor;
  218. int imageId;
  219. Paint() noexcept;
  220. /**
  221. @internal
  222. */
  223. Paint(const NVGpaint&) noexcept;
  224. operator NVGpaint() const noexcept;
  225. };
  226. struct GlyphPosition {
  227. const char* str; // Position of the glyph in the input string.
  228. float x; // The x-coordinate of the logical glyph position.
  229. float minx, maxx; // The bounds of the glyph shape.
  230. };
  231. struct TextRow {
  232. const char* start; // Pointer to the input text where the row starts.
  233. const char* end; // Pointer to the input text where the row ends (one past the last character).
  234. const char* next; // Pointer to the beginning of the next row.
  235. float width; // Logical width of the row.
  236. float minx, maxx; // Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending.
  237. };
  238. typedef int FontId;
  239. /**
  240. Constructor.
  241. @see CreateFlags
  242. */
  243. NanoVG(int flags = CREATE_ANTIALIAS);
  244. /**
  245. Constructor reusing a NanoVG context, used for subwidgets.
  246. */
  247. NanoVG(NanoWidget* groupWidget);
  248. /**
  249. Destructor.
  250. */
  251. virtual ~NanoVG();
  252. /**
  253. Get the NanoVG context.
  254. You should not need this under normal circumstances.
  255. */
  256. NVGcontext* getContext() const noexcept
  257. {
  258. return fContext;
  259. }
  260. /**
  261. Begin drawing a new frame.
  262. */
  263. void beginFrame(const uint width, const uint height, const float scaleFactor = 1.0f);
  264. /**
  265. Begin drawing a new frame inside a widget.
  266. */
  267. void beginFrame(Widget* const widget);
  268. /**
  269. Cancels drawing the current frame.
  270. */
  271. void cancelFrame();
  272. /**
  273. Ends drawing flushing remaining render state.
  274. */
  275. void endFrame();
  276. /* --------------------------------------------------------------------
  277. * State Handling */
  278. /**
  279. Pushes and saves the current render state into a state stack.
  280. A matching restore() must be used to restore the state.
  281. */
  282. void save();
  283. /**
  284. Pops and restores current render state.
  285. */
  286. void restore();
  287. /**
  288. Resets current render state to default values. Does not affect the render state stack.
  289. */
  290. void reset();
  291. /* --------------------------------------------------------------------
  292. * Render styles */
  293. /**
  294. Sets current stroke style to a solid color.
  295. */
  296. void strokeColor(const Color& color);
  297. /**
  298. Sets current stroke style to a solid color, made from red, green, blue and alpha numeric values.
  299. Values must be in [0..255] range.
  300. */
  301. void strokeColor(const int red, const int green, const int blue, const int alpha = 255);
  302. /**
  303. Sets current stroke style to a solid color, made from red, green, blue and alpha numeric values.
  304. Values must in [0..1] range.
  305. */
  306. void strokeColor(const float red, const float green, const float blue, const float alpha = 1.0f);
  307. /**
  308. Sets current stroke style to a paint, which can be a one of the gradients or a pattern.
  309. */
  310. void strokePaint(const Paint& paint);
  311. /**
  312. Sets current fill style to a solid color.
  313. */
  314. void fillColor(const Color& color);
  315. /**
  316. Sets current fill style to a solid color, made from red, green, blue and alpha numeric values.
  317. Values must be in [0..255] range.
  318. */
  319. void fillColor(const int red, const int green, const int blue, const int alpha = 255);
  320. /**
  321. Sets current fill style to a solid color, made from red, green, blue and alpha numeric values.
  322. Values must in [0..1] range.
  323. */
  324. void fillColor(const float red, const float green, const float blue, const float alpha = 1.0f);
  325. /**
  326. Sets current fill style to a paint, which can be a one of the gradients or a pattern.
  327. */
  328. void fillPaint(const Paint& paint);
  329. /**
  330. Sets the miter limit of the stroke style.
  331. Miter limit controls when a sharp corner is beveled.
  332. */
  333. void miterLimit(float limit);
  334. /**
  335. Sets the stroke width of the stroke style.
  336. */
  337. void strokeWidth(float size);
  338. /**
  339. Sets how the end of the line (cap) is drawn,
  340. Can be one of: BUTT, ROUND, SQUARE.
  341. */
  342. void lineCap(LineCap cap = BUTT);
  343. /**
  344. Sets how sharp path corners are drawn.
  345. Can be one of MITER, ROUND, BEVEL.
  346. */
  347. void lineJoin(LineCap join = MITER);
  348. /**
  349. Sets the transparency applied to all rendered shapes.
  350. Already transparent paths will get proportionally more transparent as well.
  351. */
  352. void globalAlpha(float alpha);
  353. /* --------------------------------------------------------------------
  354. * Transforms */
  355. /**
  356. Resets current transform to a identity matrix.
  357. */
  358. void resetTransform();
  359. /**
  360. Pre-multiplies current coordinate system by specified matrix.
  361. The parameters are interpreted as matrix as follows:
  362. [a c e]
  363. [b d f]
  364. [0 0 1]
  365. */
  366. void transform(float a, float b, float c, float d, float e, float f);
  367. /**
  368. Translates current coordinate system.
  369. */
  370. void translate(float x, float y);
  371. /**
  372. Rotates current coordinate system. Angle is specified in radians.
  373. */
  374. void rotate(float angle);
  375. /**
  376. Skews the current coordinate system along X axis. Angle is specified in radians.
  377. */
  378. void skewX(float angle);
  379. /**
  380. Skews the current coordinate system along Y axis. Angle is specified in radians.
  381. */
  382. void skewY(float angle);
  383. /**
  384. Scales the current coordinate system.
  385. */
  386. void scale(float x, float y);
  387. /**
  388. Stores the top part (a-f) of the current transformation matrix in to the specified buffer.
  389. [a c e]
  390. [b d f]
  391. [0 0 1]
  392. */
  393. void currentTransform(float xform[6]);
  394. /**
  395. The following functions can be used to make calculations on 2x3 transformation matrices.
  396. A 2x3 matrix is represented as float[6]. */
  397. /**
  398. Sets the transform to identity matrix.
  399. */
  400. static void transformIdentity(float dst[6]);
  401. /**
  402. Sets the transform to translation matrix
  403. */
  404. static void transformTranslate(float dst[6], float tx, float ty);
  405. /**
  406. Sets the transform to scale matrix.
  407. */
  408. static void transformScale(float dst[6], float sx, float sy);
  409. /**
  410. Sets the transform to rotate matrix. Angle is specified in radians.
  411. */
  412. static void transformRotate(float dst[6], float a);
  413. /**
  414. Sets the transform to skew-x matrix. Angle is specified in radians.
  415. */
  416. static void transformSkewX(float dst[6], float a);
  417. /**
  418. Sets the transform to skew-y matrix. Angle is specified in radians.
  419. */
  420. static void transformSkewY(float dst[6], float a);
  421. /**
  422. Sets the transform to the result of multiplication of two transforms, of A = A*B.
  423. */
  424. static void transformMultiply(float dst[6], const float src[6]);
  425. /**
  426. Sets the transform to the result of multiplication of two transforms, of A = B*A.
  427. */
  428. static void transformPremultiply(float dst[6], const float src[6]);
  429. /**
  430. Sets the destination to inverse of specified transform.
  431. Returns 1 if the inverse could be calculated, else 0.
  432. */
  433. static int transformInverse(float dst[6], const float src[6]);
  434. /**
  435. Transform a point by given transform.
  436. */
  437. static void transformPoint(float& dstx, float& dsty, const float xform[6], float srcx, float srcy);
  438. /**
  439. Convert degrees to radians.
  440. */
  441. static float degToRad(float deg);
  442. /**
  443. Convert radians to degrees.
  444. */
  445. static float radToDeg(float rad);
  446. /* --------------------------------------------------------------------
  447. * Images */
  448. /**
  449. Creates image by loading it from the disk from specified file name.
  450. */
  451. NanoImage::Handle createImageFromFile(const char* filename, ImageFlags imageFlags);
  452. /**
  453. Creates image by loading it from the disk from specified file name.
  454. Overloaded function for convenience.
  455. @see ImageFlags
  456. */
  457. NanoImage::Handle createImageFromFile(const char* filename, int imageFlags);
  458. /**
  459. Creates image by loading it from the specified chunk of memory.
  460. */
  461. NanoImage::Handle createImageFromMemory(uchar* data, uint dataSize, ImageFlags imageFlags);
  462. /**
  463. Creates image by loading it from the specified chunk of memory.
  464. Overloaded function for convenience.
  465. @see ImageFlags
  466. */
  467. NanoImage::Handle createImageFromMemory(uchar* data, uint dataSize, int imageFlags);
  468. /**
  469. Creates image from specified image data.
  470. */
  471. NanoImage::Handle createImageFromRGBA(uint w, uint h, const uchar* data, ImageFlags imageFlags);
  472. /**
  473. Creates image from specified image data.
  474. Overloaded function for convenience.
  475. @see ImageFlags
  476. */
  477. NanoImage::Handle createImageFromRGBA(uint w, uint h, const uchar* data, int imageFlags);
  478. /**
  479. Creates image from an OpenGL texture handle.
  480. */
  481. NanoImage::Handle createImageFromTextureHandle(GLuint textureId, uint w, uint h, ImageFlags imageFlags, bool deleteTexture = false);
  482. /**
  483. Creates image from an OpenGL texture handle.
  484. Overloaded function for convenience.
  485. @see ImageFlags
  486. */
  487. NanoImage::Handle createImageFromTextureHandle(GLuint textureId, uint w, uint h, int imageFlags, bool deleteTexture = false);
  488. /* --------------------------------------------------------------------
  489. * Paints */
  490. /**
  491. Creates and returns a linear gradient. Parameters (sx,sy)-(ex,ey) specify the start and end coordinates
  492. of the linear gradient, icol specifies the start color and ocol the end color.
  493. The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
  494. */
  495. Paint linearGradient(float sx, float sy, float ex, float ey, const Color& icol, const Color& ocol);
  496. /**
  497. Creates and returns a box gradient. Box gradient is a feathered rounded rectangle, it is useful for rendering
  498. drop shadows or highlights for boxes. Parameters (x,y) define the top-left corner of the rectangle,
  499. (w,h) define the size of the rectangle, r defines the corner radius, and f feather. Feather defines how blurry
  500. the border of the rectangle is. Parameter icol specifies the inner color and ocol the outer color of the gradient.
  501. The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
  502. */
  503. Paint boxGradient(float x, float y, float w, float h, float r, float f, const Color& icol, const Color& ocol);
  504. /**
  505. Creates and returns a radial gradient. Parameters (cx,cy) specify the center, inr and outr specify
  506. the inner and outer radius of the gradient, icol specifies the start color and ocol the end color.
  507. The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
  508. */
  509. Paint radialGradient(float cx, float cy, float inr, float outr, const Color& icol, const Color& ocol);
  510. /**
  511. Creates and returns an image pattern. Parameters (ox,oy) specify the left-top location of the image pattern,
  512. (ex,ey) the size of one image, angle rotation around the top-left corner, image is handle to the image to render.
  513. The gradient is transformed by the current transform when it is passed to fillPaint() or strokePaint().
  514. */
  515. Paint imagePattern(float ox, float oy, float ex, float ey, float angle, const NanoImage& image, float alpha);
  516. /* --------------------------------------------------------------------
  517. * Scissoring */
  518. /**
  519. Sets the current scissor rectangle.
  520. The scissor rectangle is transformed by the current transform.
  521. */
  522. void scissor(float x, float y, float w, float h);
  523. /**
  524. Intersects current scissor rectangle with the specified rectangle.
  525. The scissor rectangle is transformed by the current transform.
  526. Note: in case the rotation of previous scissor rect differs from
  527. the current one, the intersection will be done between the specified
  528. rectangle and the previous scissor rectangle transformed in the current
  529. transform space. The resulting shape is always rectangle.
  530. */
  531. void intersectScissor(float x, float y, float w, float h);
  532. /**
  533. Reset and disables scissoring.
  534. */
  535. void resetScissor();
  536. /* --------------------------------------------------------------------
  537. * Paths */
  538. /**
  539. Clears the current path and sub-paths.
  540. */
  541. void beginPath();
  542. /**
  543. Starts new sub-path with specified point as first point.
  544. */
  545. void moveTo(float x, float y);
  546. /**
  547. Adds line segment from the last point in the path to the specified point.
  548. */
  549. void lineTo(float x, float y);
  550. /**
  551. Adds cubic bezier segment from last point in the path via two control points to the specified point.
  552. */
  553. void bezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y);
  554. /**
  555. Adds quadratic bezier segment from last point in the path via a control point to the specified point.
  556. */
  557. void quadTo(float cx, float cy, float x, float y);
  558. /**
  559. Adds an arc segment at the corner defined by the last path point, and two specified points.
  560. */
  561. void arcTo(float x1, float y1, float x2, float y2, float radius);
  562. /**
  563. Closes current sub-path with a line segment.
  564. */
  565. void closePath();
  566. /**
  567. Sets the current sub-path winding.
  568. */
  569. void pathWinding(Winding dir);
  570. /**
  571. Creates new circle arc shaped sub-path. The arc center is at cx,cy, the arc radius is r,
  572. and the arc is drawn from angle a0 to a1, and swept in direction dir (NVG_CCW or NVG_CW).
  573. Angles are specified in radians.
  574. */
  575. void arc(float cx, float cy, float r, float a0, float a1, Winding dir);
  576. /**
  577. Creates new rectangle shaped sub-path.
  578. */
  579. void rect(float x, float y, float w, float h);
  580. /**
  581. Creates new rounded rectangle shaped sub-path.
  582. */
  583. void roundedRect(float x, float y, float w, float h, float r);
  584. /**
  585. Creates new ellipse shaped sub-path.
  586. */
  587. void ellipse(float cx, float cy, float rx, float ry);
  588. /**
  589. Creates new circle shaped sub-path.
  590. */
  591. void circle(float cx, float cy, float r);
  592. /**
  593. Fills the current path with current fill style.
  594. */
  595. void fill();
  596. /**
  597. Fills the current path with current stroke style.
  598. */
  599. void stroke();
  600. /* --------------------------------------------------------------------
  601. * Text */
  602. /**
  603. Creates font by loading it from the disk from specified file name.
  604. Returns handle to the font.
  605. */
  606. FontId createFontFromFile(const char* name, const char* filename);
  607. /**
  608. Creates font by loading it from the specified memory chunk.
  609. Returns handle to the font.
  610. */
  611. FontId createFontFromMemory(const char* name, const uchar* data, uint dataSize, bool freeData);
  612. /**
  613. Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
  614. */
  615. FontId findFont(const char* name);
  616. /**
  617. Sets the font size of current text style.
  618. */
  619. void fontSize(float size);
  620. /**
  621. Sets the blur of current text style.
  622. */
  623. void fontBlur(float blur);
  624. /**
  625. Sets the letter spacing of current text style.
  626. */
  627. void textLetterSpacing(float spacing);
  628. /**
  629. Sets the proportional line height of current text style. The line height is specified as multiple of font size.
  630. */
  631. void textLineHeight(float lineHeight);
  632. /**
  633. Sets the text align of current text style.
  634. */
  635. void textAlign(Align align);
  636. /**
  637. Sets the text align of current text style.
  638. Overloaded function for convenience.
  639. @see Align
  640. */
  641. void textAlign(int align);
  642. /**
  643. Sets the font face based on specified id of current text style.
  644. */
  645. void fontFaceId(FontId font);
  646. /**
  647. Sets the font face based on specified name of current text style.
  648. */
  649. void fontFace(const char* font);
  650. /**
  651. Draws text string at specified location. If end is specified only the sub-string up to the end is drawn.
  652. */
  653. float text(float x, float y, const char* string, const char* end);
  654. /**
  655. Draws multi-line text string at specified location wrapped at the specified width.
  656. If end is specified only the sub-string up to the end is drawn.
  657. White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
  658. Words longer than the max width are slit at nearest character (i.e. no hyphenation).
  659. */
  660. void textBox(float x, float y, float breakRowWidth, const char* string, const char* end = nullptr);
  661. /**
  662. Measures the specified text string. The bounds value are [xmin,ymin, xmax,ymax].
  663. Returns the horizontal advance of the measured text (i.e. where the next character should drawn).
  664. Measured values are returned in local coordinate space.
  665. */
  666. float textBounds(float x, float y, const char* string, const char* end, Rectangle<float>& bounds);
  667. /**
  668. Measures the specified multi-text string. Parameter bounds should be a pointer to float[4],
  669. if the bounding box of the text should be returned. The bounds value are [xmin,ymin, xmax,ymax]
  670. Measured values are returned in local coordinate space.
  671. */
  672. void textBoxBounds(float x, float y, float breakRowWidth, const char* string, const char* end, float bounds[4]);
  673. /**
  674. Calculates the glyph x positions of the specified text. If end is specified only the sub-string will be used.
  675. Measured values are returned in local coordinate space.
  676. */
  677. int textGlyphPositions(float x, float y, const char* string, const char* end, GlyphPosition& positions, int maxPositions);
  678. /**
  679. Returns the vertical metrics based on the current text style.
  680. Measured values are returned in local coordinate space.
  681. */
  682. void textMetrics(float* ascender, float* descender, float* lineh);
  683. /**
  684. Breaks the specified text into lines. If end is specified only the sub-string will be used.
  685. White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
  686. Words longer than the max width are slit at nearest character (i.e. no hyphenation).
  687. */
  688. int textBreakLines(const char* string, const char* end, float breakRowWidth, TextRow& rows, int maxRows);
  689. #ifndef DGL_NO_SHARED_RESOURCES
  690. /**
  691. Load DPF's internal shared resources for this NanoVG class.
  692. */
  693. virtual void loadSharedResources();
  694. #endif
  695. private:
  696. NVGcontext* const fContext;
  697. bool fInFrame;
  698. bool fIsSubWidget;
  699. friend class BlendishWidget;
  700. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NanoVG)
  701. };
  702. // -----------------------------------------------------------------------
  703. // NanoWidget
  704. /**
  705. NanoVG Widget class.
  706. This class implements the NanoVG drawing API inside a DGL Widget.
  707. The drawing function onDisplay() is implemented internally but a
  708. new onNanoDisplay() needs to be overridden instead.
  709. */
  710. class NanoWidget : public Widget,
  711. public NanoVG
  712. {
  713. public:
  714. /**
  715. Constructor.
  716. @see CreateFlags
  717. */
  718. explicit NanoWidget(Window& parent, int flags = CREATE_ANTIALIAS);
  719. /**
  720. Constructor for a subwidget.
  721. */
  722. explicit NanoWidget(Widget* groupWidget, int flags = CREATE_ANTIALIAS);
  723. /**
  724. Constructor for a subwidget, reusing a NanoVG context.
  725. */
  726. explicit NanoWidget(NanoWidget* groupWidget);
  727. /**
  728. Destructor.
  729. */
  730. virtual ~NanoWidget();
  731. protected:
  732. /**
  733. New virtual onDisplay function.
  734. @see onDisplay
  735. */
  736. virtual void onNanoDisplay() = 0;
  737. private:
  738. struct PrivateData;
  739. PrivateData* const nData;
  740. /**
  741. Widget display function.
  742. Implemented internally to wrap begin/endFrame() automatically.
  743. */
  744. void onDisplay() override;
  745. // these should not be used
  746. void beginFrame(uint,uint) {}
  747. void beginFrame(uint,uint,float) {}
  748. void beginFrame(Widget*) {}
  749. void cancelFrame() {}
  750. void endFrame() {}
  751. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NanoWidget)
  752. };
  753. // -----------------------------------------------------------------------
  754. END_NAMESPACE_DGL
  755. #endif // DGL_NANO_WIDGET_HPP_INCLUDED