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.

NanoVG.hpp 29KB

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