iPlug2 - C++ Audio Plug-in Framework
IGraphicsCanvas.h
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #pragma once
12 
13 #include <emscripten/val.h>
14 #include <emscripten/bind.h>
15 
16 #include "IPlugPlatform.h"
17 
18 #include "IGraphics.h"
19 
20 using namespace emscripten;
21 
22 BEGIN_IPLUG_NAMESPACE
23 BEGIN_IGRAPHICS_NAMESPACE
24 
26 std::string CanvasColor(const IColor& color, float alpha = 1.0);
27 
30 class IGraphicsCanvas : public IGraphics
31 {
32 private:
33  class Bitmap;
34  struct Font;
35 
36 public:
37  IGraphicsCanvas(IGEditorDelegate& dlg, int w, int h, int fps, float scale);
38  ~IGraphicsCanvas();
39 
40  const char* GetDrawingAPIStr() override { return "Canvas2D"; }
41 
42  void DrawBitmap(const IBitmap& bitmap, const IRECT& bounds, int srcX, int srcY, const IBlend* pBlend) override;
43 
44  void DrawResize() override {};
45 
46  void PathClear() override;
47  void PathClose() override;
48  void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override;
49  void PathMoveTo(float x, float y) override;
50  void PathLineTo(float x, float y) override;
51  void PathCubicBezierTo(float c1x, float c1y, float c2x, float c2y, float x2, float y2) override;
52  void PathQuadraticBezierTo(float cx, float cy, float x2, float y2) override;
53  void PathStroke(const IPattern& pattern, float thickness, const IStrokeOptions& options, const IBlend* pBlend) override;
54  void PathFill(const IPattern& pattern, const IFillOptions& options, const IBlend* pBlend) override;
55 
56  IColor GetPoint(int x, int y) override { return COLOR_BLACK; } // TODO:
57  void* GetDrawContext() override { return nullptr; }
58 
59  bool BitmapExtSupported(const char* ext) override;
60 
61 protected:
62  APIBitmap* LoadAPIBitmap(const char* fileNameOrResID, int scale, EResourceLocation location, const char* ext) override;
63  APIBitmap* LoadAPIBitmap(const char* name, const void* pData, int dataSize, int scale) override;
64  APIBitmap* CreateAPIBitmap(int width, int height, int scale, double drawScale, bool cacheable = false) override;
65 
66  bool LoadAPIFont(const char* fontID, const PlatformFontPtr& font) override;
67 
68  bool AssetsLoaded() override;
69 
70  int AlphaChannel() const override { return 3; }
71  bool FlippedBitmap() const override { return false; }
72 
73  void GetLayerBitmapData(const ILayerPtr& layer, RawBitmapData& data) override;
74  void ApplyShadowMask(ILayerPtr& layer, RawBitmapData& mask, const IShadow& shadow) override;
75 
76  float DoMeasureText(const IText& text, const char* str, IRECT& bounds) const override;
77  void DoDrawText(const IText& text, const char* str, const IRECT& bounds, const IBlend* pBlend) override;
78 
79 private:
80  void PrepareAndMeasureText(const IText& text, const char* str, IRECT& r, double& x, double & y) const;
81 
82  val GetContext() const
83  {
84  val canvas = mLayers.empty() ? val::global("document").call<val>("getElementById", std::string("canvas")) : *(mLayers.top()->GetAPIBitmap()->GetBitmap());
85 
86  return canvas.call<val>("getContext", std::string("2d"));
87  }
88 
89  void GetFontMetrics(const char* font, const char* style, double& ascenderRatio, double& EMRatio);
90  bool CompareFontMetrics(const char* style, const char* font1, const char* font2);
91  bool FontExists(const char* font, const char* style);
92 
93  double XTranslate() { return mLayers.empty() ? 0 : -mLayers.top()->Bounds().L; }
94  double YTranslate() { return mLayers.empty() ? 0 : -mLayers.top()->Bounds().T; }
95 
96  void PathTransformSetMatrix(const IMatrix& m) override;
97  void SetClipRegion(const IRECT& r) override;
98 
99  void SetCanvasSourcePattern(val& context, const IPattern& pattern, const IBlend* pBlend = nullptr);
100  void SetCanvasBlendMode(val& context, const IBlend* pBlend);
101 
102  std::vector<val> mLoadingFonts;
103 
104  static StaticStorage<Font> sFontCache;
105 };
106 
107 END_IPLUG_NAMESPACE
108 END_IGRAPHICS_NAMESPACE
109 
IColor GetPoint(int x, int y) override
Get the color at an X, Y location in the graphics context.
Used to manage a rectangular area, independent of draw class/platform.
Used to manage composite/blend operations, independent of draw class/platform.
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
Used to manage fill behaviour for path based drawing back ends.
Include to get consistently named preprocessor macros for different platforms and logging functionali...
int AlphaChannel() const override
Used to manage color data, independent of draw class/platform.
Used to manage stroke behaviour for path based drawing back ends.
The lowest level base class of an IGraphics context.
An editor delegate base class for a SOMETHING that uses IGraphics for it&#39;s UI.
IText is used to manage font and text/text entry style for a piece of text on the UI...
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
Used to specify properties of a drop-shadow to a layer.
bool FlippedBitmap() const override
const char * GetDrawingAPIStr() override
void * GetDrawContext() override
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
IGraphics draw class HTML5 canvas.
std::unique_ptr< ILayer > ILayerPtr
ILayerPtr is a managed pointer for transferring the ownership of layers.
Used to store pattern information for gradients.
void DrawResize() override
Used to store transformation matrices.