Created
February 13, 2022 07:11
-
-
Save piusayowale/b9cce697d473140d155a70f2feda0aa1 to your computer and use it in GitHub Desktop.
Yoga with Skia
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> | |
int main() { | |
const YGConfigRef config = YGConfigNew(); | |
const YGNodeRef root = YGNodeNewWithConfig(config); | |
YGNodeStyleSetFlexDirection(root, YGFlexDirection::YGFlexDirectionRow); | |
YGNodeStyleSetDirection(root, YGDirection::YGDirectionLTR); | |
YGNodeStyleSetJustifyContent(root, YGJustifyFlexStart); | |
YGNodeStyleSetAlignItems(root, YGAlignFlexStart); | |
YGNodeStyleSetAlignContent(root, YGAlignFlexStart); | |
YGNodeStyleSetWidth(root, 400); | |
YGNodeStyleSetHeight(root, 400); | |
//YGNodeInsertChild(root, root_child0, 0); | |
const YGNodeRef child1 = YGNodeNewWithConfig(config); | |
YGNodeStyleSetAlignContent(child1, YGAlignCenter); | |
YGNodeStyleSetWidth(child1, 50); | |
YGNodeStyleSetHeight(child1, 50); | |
YGNodeInsertChild(root, child1, 0); | |
const YGNodeRef child2 = YGNodeNewWithConfig(config); | |
YGNodeStyleSetAlignContent(child2, YGAlignCenter); | |
YGNodeStyleSetWidth(child2, 50); | |
YGNodeStyleSetHeight(child2, 50); | |
YGNodeInsertChild(root, child2, 1); | |
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, 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); | |
SkRect rect; | |
SkPaint paint; | |
paint.setColor(SkColors::kGray); | |
rect.fTop = SkFloatToScalar(YGNodeLayoutGetTop(child1)); | |
rect.fBottom = SkFloatToScalar(YGNodeLayoutGetTop(child1) + YGNodeLayoutGetHeight(child1)); | |
rect.fLeft = SkFloatToScalar(YGNodeLayoutGetLeft(child1)); | |
rect.fRight = SkFloatToScalar(YGNodeLayoutGetLeft(child1) + YGNodeLayoutGetWidth(child1)); | |
canvas.drawRect(rect, paint); | |
SkRect rect2; | |
SkPaint paint2; | |
paint2.setColor(SkColors::kYellow); | |
rect2.fTop = SkFloatToScalar(YGNodeLayoutGetTop(child2)); | |
rect2.fBottom = SkFloatToScalar(YGNodeLayoutGetTop(child2) + YGNodeLayoutGetHeight(child2)); | |
rect2.fLeft = SkFloatToScalar(YGNodeLayoutGetLeft(child2)); | |
rect2.fRight = SkFloatToScalar(YGNodeLayoutGetLeft(child2) + YGNodeLayoutGetWidth(child2)); | |
canvas.drawRect(rect2, paint2); | |
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