Created
February 13, 2022 19:20
-
-
Save piusayowale/a2eaa865c5c1ca163fe716f0b5bd8e4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include <string> | |
#include <algorithm> | |
#include <yoga/Yoga.h> | |
#include <yoga/YGNode.h> | |
#include <yoga/YGLayout.h> | |
#include <yoga/YGStyle.h> | |
#include <skia/core/SkCanvas.h> | |
#include <skia/core/SkColor.h> | |
#include <skia/core/SkFont.h> | |
#include <skia/encode/SkPngEncoder.h> | |
#include <skia/encode/SkEncoder.h> | |
#include <skia/core/SkBitmap.h> | |
#include <skia/core/SkSurface.h> | |
#include <skia/core/SkStream.h> | |
#include <skia/core/SkImage.h> | |
#include <fstream> | |
#include <cstdlib> | |
#include <ctime> | |
void addNode(const YGNodeRef root, const YGConfigRef config) { | |
const YGNodeRef child1 = YGNodeNewWithConfig(config); | |
int index = YGNodeGetChildCount(root); | |
YGNodeStyleSetWidth(child1, 100); | |
YGNodeStyleSetHeight(child1, 100); | |
YGNodeInsertChild(root, child1, index); | |
return ; | |
} | |
void iterateOverNodes(const YGNodeRef root, SkCanvas * canvas) { | |
int count = YGNodeGetChildCount(root); | |
SkColor4f color[] = { SkColors::kBlue, SkColors::kGreen, SkColors::kGray , SkColors::kMagenta, SkColors::kBlack, | |
SkColors::kYellow, SkColors::kMagenta, SkColors::kWhite, SkColors::kRed, SkColors::kDkGray | |
}; | |
for (int index = 0; index < count; index++) { | |
int rand_index = std::rand() % 10; | |
std::cout << "Random value is " << rand_index << "\n"; | |
YGNodeRef node = YGNodeGetChild(root, index); | |
SkRect rect; | |
SkPaint paint; | |
rect.setXYWH(YGNodeLayoutGetLeft(node), YGNodeLayoutGetTop(node), YGNodeLayoutGetWidth(node), YGNodeLayoutGetHeight(node)); | |
std::cout << YGNodeLayoutGetHeight(node) << "\n"; | |
std::cout << YGNodeLayoutGetWidth(node) << "\n"; | |
std::cout << YGNodeLayoutGetTop(node) << "\n"; | |
std::cout << YGNodeLayoutGetBottom(node) << "\n"; | |
paint.setColor(color[rand_index]); | |
canvas->drawRect(rect, paint); | |
} | |
} | |
int main() { | |
std::srand(std::time(0)); | |
const YGConfigRef config = YGConfigNew(); | |
const YGNodeRef root = YGNodeNewWithConfig(config); | |
YGNodeStyleSetFlexDirection(root, YGFlexDirection::YGFlexDirectionRow); | |
YGNodeStyleSetDirection(root, YGDirection::YGDirectionLTR); | |
YGNodeStyleSetJustifyContent(root, YGJustifyFlexStart); | |
YGNodeStyleSetAlignContent(root, YGAlignFlexStart); | |
YGNodeStyleSetFlexWrap(root, YGWrap::YGWrapWrap); | |
YGNodeStyleSetPadding(root, YGEdge::YGEdgeAll, 2.0f); | |
YGNodeStyleSetWidth(root, 404); | |
YGNodeStyleSetHeight(root, 404); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirection::YGDirectionLTR); | |
SkBitmap bitmap{}; | |
bitmap.allocN32Pixels(404, 404); | |
SkCanvas canvas{ bitmap }; | |
SkRect rect1; | |
SkPaint paint1; | |
paint1.setColor(SkColors::kCyan); | |
rect1.fBottom = SkFloatToScalar(404.0f); | |
rect1.fTop = SkFloatToScalar(0.0f); | |
rect1.fLeft = SkFloatToScalar(0.0f); | |
rect1.fRight = SkFloatToScalar(404.0f); | |
canvas.drawRect(rect1, paint1); | |
iterateOverNodes(root, &canvas); | |
auto data = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kPNG, 80); | |
std::ofstream file("img.png", std::ios_base::out | std::ios_base::binary); | |
file.write((const char*)data.get()->bytes(), data->size()); | |
file.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment