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 25KB

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