iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
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
20using namespace emscripten;
21
22BEGIN_IPLUG_NAMESPACE
23BEGIN_IGRAPHICS_NAMESPACE
24
26std::string CanvasColor(const IColor& color, float alpha = 1.0);
27
31{
32private:
33 class Bitmap;
34 struct Font;
35
36public:
37 IGraphicsCanvas(IGEditorDelegate& dlg, int w, int h, int fps, float scale);
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
61protected:
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, float 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
79private:
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
107END_IPLUG_NAMESPACE
108END_IGRAPHICS_NAMESPACE
109
Include to get consistently named preprocessor macros for different platforms and logging functionali...
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
An editor delegate base class for a SOMETHING that uses IGraphics for it's UI.
IGraphics draw class HTML5 canvas.
void PathQuadraticBezierTo(float cx, float cy, float x2, float y2) override
Add a quadratic bezier to the current path from the current point to the specified location.
bool LoadAPIFont(const char *fontID, const PlatformFontPtr &font) override
Drawing API method to load a font from a PlatformFontPtr, called internally.
bool BitmapExtSupported(const char *ext) override
Checks a file extension and reports whether this drawing API supports loading that extension.
float DoMeasureText(const IText &text, const char *str, IRECT &bounds) const override
void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override
Add an arc to the current path.
APIBitmap * LoadAPIBitmap(const char *fileNameOrResID, int scale, EResourceLocation location, const char *ext) override
Drawing API method to load a bitmap, called internally.
void PathClear() override
Clear the stack of path drawing commands.
void DrawResize() override
void PathCubicBezierTo(float c1x, float c1y, float c2x, float c2y, float x2, float y2) override
Add a cubic bezier to the current path from the current point to the specified location.
const char * GetDrawingAPIStr() override
int AlphaChannel() const override
void PathClose() override
Close the path that is being specified.
void PathMoveTo(float x, float y) override
Move the current point in the current path.
void PathFill(const IPattern &pattern, const IFillOptions &options, const IBlend *pBlend) override
Fill the current current path.
bool AssetsLoaded() override
Specialized in IGraphicsCanvas drawing backend.
void * GetDrawContext() override
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
void PathLineTo(float x, float y) override
Add a line to the current path from the current point to the specified location.
IColor GetPoint(int x, int y) override
Get the color at an X, Y location in the graphics context.
void PathStroke(const IPattern &pattern, float thickness, const IStrokeOptions &options, const IBlend *pBlend) override
Stroke the current current path.
APIBitmap * CreateAPIBitmap(int width, int height, float scale, double drawScale, bool cacheable=false) override
Creates a new API bitmap, either in memory or as a GPU texture.
void GetLayerBitmapData(const ILayerPtr &layer, RawBitmapData &data) override
Get the contents of a layer as Raw RGBA bitmap data NOTE: you should only call this within IControl::...
bool FlippedBitmap() const override
void DoDrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend) override
void ApplyShadowMask(ILayerPtr &layer, RawBitmapData &mask, const IShadow &shadow) override
Implemented by a graphics backend to apply a calculated shadow mask to a layer, according to the shad...
void DrawBitmap(const IBitmap &bitmap, const IRECT &bounds, int srcX, int srcY, const IBlend *pBlend) override
Draw a bitmap (raster) image to the graphics context.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
std::unique_ptr< ILayer > ILayerPtr
ILayerPtr is a managed pointer for transferring the ownership of layers.
Used to manage stroke behaviour for path based drawing back ends.
Used to manage composite/blend operations, independent of draw class/platform.
Used to manage color data, independent of draw class/platform.
Used to manage fill behaviour.
Used to store transformation matrices.
Used to store pattern information for gradients.
Used to manage a rectangular area, independent of draw class/platform.
Used to specify properties of a drop-shadow to a layer.
IText is used to manage font and text/text entry style for a piece of text on the UI,...