Created
February 13, 2022 18:26
-
-
Save piusayowale/9080871956fe5371ffb777dc0ad458bd 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}; | |
for (int index = 0; index < count; index++) { | |
int rand_index = std::rand() % 5; | |
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, YGJustifyCenter); | |
YGNodeStyleSetAlignItems(root, YGAlignCenter); | |
YGNodeStyleSetAlignContent(root, YGAlignCenter); | |
YGNodeStyleSetWidth(root, 400); | |
YGNodeStyleSetHeight(root, 400); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
addNode(root, config); | |
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirection::YGDirectionLTR); | |
SkBitmap bitmap{}; | |
bitmap.allocN32Pixels(400, 400); | |
SkCanvas canvas{ bitmap }; | |
SkRect rect1; | |
SkPaint paint1; | |
paint1.setColor(SkColors::kCyan); | |
rect1.fBottom = SkFloatToScalar(400.0f); | |
rect1.fTop = SkFloatToScalar(0.0f); | |
rect1.fLeft = SkFloatToScalar(0.0f); | |
rect1.fRight = SkFloatToScalar(400.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