feat: add row/col tags

This commit is contained in:
2024-10-13 18:29:58 +01:00
parent 29de0aa47d
commit ebe34ecebb
10 changed files with 123 additions and 37 deletions

10
src/layout/layout.hxx Normal file
View File

@@ -0,0 +1,10 @@
#ifndef __CLEF_LAYOUT_LAYOUT_HXX__
#define __CLEF_LAYOUT_LAYOUT_HXX__
namespace Clef {
enum class Orientation { Horizontal, Vertical };
}
#endif

View File

@@ -5,8 +5,8 @@
#include "include/core/SkRect.h" #include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h" #include "include/core/SkRefCnt.h"
#include "include/core/SkTypeface.h" #include "include/core/SkTypeface.h"
#include "layout/layout.hxx"
#include "rapidxml.hpp" #include "rapidxml.hpp"
#include "rendering.hxx"
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
@@ -19,16 +19,24 @@ static std::uniform_int_distribution<std::mt19937::result_type>
static std::random_device dev; static std::random_device dev;
static std::mt19937 rng(dev()); static std::mt19937 rng(dev());
inline const auto NODE_TYPE_BOX = "box";
inline const auto NODE_TYPE_ROW = "row";
inline const auto NODE_TYPE_COL = "col";
inline const auto NODE_TYPE_TEXT = "text";
uint64_t __generate_random_node_id() { return random_id(rng); }; uint64_t __generate_random_node_id() { return random_id(rng); };
Clef::ContainerNode::ContainerNode() { Clef::BoxNode::BoxNode() : orientation(Orientation::Vertical) {
prev_sibiling = nullptr; prev_sibiling = nullptr;
next_sibiling = nullptr; next_sibiling = nullptr;
parent = nullptr; parent = nullptr;
type = Clef::LayoutTree::NodeType::Container; type = Clef::LayoutTree::NodeType::Box;
} }
Clef::LayoutBound Clef::ContainerNode::measure() { Clef::LayoutBound Clef::BoxNode::measure() {
switch (orientation) {
case Orientation::Vertical: {
float max_child_width = -1; float max_child_width = -1;
float total_height = 0; float total_height = 0;
for (const auto &child : children) { for (const auto &child : children) {
@@ -39,6 +47,21 @@ Clef::LayoutBound Clef::ContainerNode::measure() {
total_height += bound.height; total_height += bound.height;
} }
return {0, 0, max_child_width, total_height}; return {0, 0, max_child_width, total_height};
}
case Orientation::Horizontal: {
float max_child_height = -1;
float total_width = 0;
for (const auto &child : children) {
const auto bound = child->measure();
if (bound.height > max_child_height) {
max_child_height = bound.height;
}
total_width += bound.width;
}
return {0, 0, total_width, max_child_height};
}
}
} }
Clef::TextNode::TextNode(const char *content, SkFont font) Clef::TextNode::TextNode(const char *content, SkFont font)
@@ -61,13 +84,17 @@ Clef::LayoutTree::LayoutTree(Clef::LayoutTree::Node *root) : root(root) {}
Clef::LayoutTree::Node * Clef::LayoutTree::Node *
Clef::LayoutTree::Node::from_xml_node(const RenderingContext &ctx, Clef::LayoutTree::Node::from_xml_node(const RenderingContext &ctx,
rapidxml::xml_node<char> *node) { rapidxml::xml_node<char> *node) {
std::cout << "encountered node:" << node->name() << std::endl; if (std::strncmp(NODE_TYPE_ROW, node->name(), 3) == 0 ||
std::strncmp(NODE_TYPE_COL, node->name(), 3) == 0) {
if (std::strncmp("box", node->name(), 3) == 0) { auto container_node = new BoxNode();
auto container_node = new ContainerNode();
container_node->id = __generate_random_node_id(); container_node->id = __generate_random_node_id();
std::cout << "assigned id " << container_node->id << std::endl; if (std::strncmp(NODE_TYPE_ROW, node->name(), 3) == 0) {
container_node->orientation = Orientation::Horizontal;
} else {
container_node->orientation = Orientation::Vertical;
}
auto current_node = node->first_node(); auto current_node = node->first_node();
Node *last_node = nullptr; Node *last_node = nullptr;
@@ -80,8 +107,6 @@ Clef::LayoutTree::Node::from_xml_node(const RenderingContext &ctx,
if (last_node) { if (last_node) {
n->prev_sibiling = last_node; n->prev_sibiling = last_node;
last_node->next_sibiling = n; last_node->next_sibiling = n;
std::cout << n << std::endl;
std::cout << last_node << std::endl;
} }
n->parent = container_node; n->parent = container_node;
@@ -94,7 +119,7 @@ Clef::LayoutTree::Node::from_xml_node(const RenderingContext &ctx,
return container_node; return container_node;
} }
if (std::strncmp("text", node->name(), 4) == 0) { if (std::strncmp(NODE_TYPE_TEXT, node->name(), 4) == 0) {
const auto content = node->value(); const auto content = node->value();
if (!content) { if (!content) {
return nullptr; return nullptr;
@@ -120,10 +145,9 @@ Clef::LayoutTree::from_xml(const RenderingContext &ctx,
const rapidxml::xml_document<> &doc) { const rapidxml::xml_document<> &doc) {
Clef::LayoutTree::Node *root; Clef::LayoutTree::Node *root;
auto xml_root = doc.first_node("box"); auto xml_root = doc.first_node();
if (!xml_root) { if (!xml_root) {
throw std::runtime_error( throw std::runtime_error("provided layout file appears to be empty.");
"cml layout must start with a container node.");
} }
root = Node::from_xml_node(ctx, xml_root); root = Node::from_xml_node(ctx, xml_root);

View File

@@ -3,6 +3,7 @@
#include "include/core/SkColorSpace.h" #include "include/core/SkColorSpace.h"
#include "include/core/SkFont.h" #include "include/core/SkFont.h"
#include "layout/layout.hxx"
#include "layout_bound.hxx" #include "layout_bound.hxx"
#include "rapidxml.hpp" #include "rapidxml.hpp"
#include "rendering_context.hxx" #include "rendering_context.hxx"
@@ -13,7 +14,7 @@
namespace Clef { namespace Clef {
struct LayoutTree { struct LayoutTree {
enum class NodeType { Text, Container }; enum class NodeType { Text, Box };
struct Node { struct Node {
uint64_t id; uint64_t id;
@@ -37,12 +38,13 @@ struct LayoutTree {
LayoutTree(Node *root); LayoutTree(Node *root);
}; };
struct ContainerNode : LayoutTree::Node { struct BoxNode : LayoutTree::Node {
Orientation orientation;
std::vector<Node *> children; std::vector<Node *> children;
Clef::LayoutBound measure() override; Clef::LayoutBound measure() override;
ContainerNode(); BoxNode();
}; };
struct TextNode : LayoutTree::Node { struct TextNode : LayoutTree::Node {

View File

@@ -6,13 +6,10 @@
#include "include/core/SkRect.h" #include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h" #include "include/core/SkRefCnt.h"
#include "include/ports/SkFontMgr_directory.h" #include "include/ports/SkFontMgr_directory.h"
#include "layout/layout.hxx"
#include "layout/layout_tree.hxx" #include "layout/layout_tree.hxx"
#include <iostream>
#include <utility> #include <utility>
inline const auto NODE_TYPE_BOX = "box";
inline const auto NODE_TYPE_TEXT = "text";
Clef::LayoutBound __calculate_node_position(Clef::RenderingContext &ctx, Clef::LayoutBound __calculate_node_position(Clef::RenderingContext &ctx,
Clef::LayoutTree::Node *node) { Clef::LayoutTree::Node *node) {
auto bounds = node->measure(); auto bounds = node->measure();
@@ -26,8 +23,21 @@ Clef::LayoutBound __calculate_node_position(Clef::RenderingContext &ctx,
return {0, 0, bounds.width, bounds.height}; return {0, 0, bounds.width, bounds.height};
} }
const auto parent = node->parent;
const auto prev_bound = entry->second; const auto prev_bound = entry->second;
return {prev_bound.x, prev_bound.y + prev_bound.height, bounds.width, if (parent->type == Clef::LayoutTree::NodeType::Box) {
const auto parent_box = static_cast<Clef::BoxNode *>(parent);
switch (parent_box->orientation) {
case Clef::Orientation::Vertical:
return {prev_bound.x, prev_bound.y + prev_bound.height,
bounds.width, bounds.height};
case Clef::Orientation::Horizontal:
return {prev_bound.x + prev_bound.width, prev_bound.y,
bounds.width, bounds.height};
}
}
return {prev_bound.x + prev_bound.width, prev_bound.y, bounds.width,
bounds.height}; bounds.height};
} }
@@ -55,8 +65,8 @@ void clef_render_node(Clef::RenderingContext &ctx,
break; break;
} }
case Clef::LayoutTree::NodeType::Container: { case Clef::LayoutTree::NodeType::Box: {
auto cn = static_cast<Clef::ContainerNode *>(node); const auto box = static_cast<Clef::BoxNode *>(node);
const auto pos = __calculate_node_position(ctx, node); const auto pos = __calculate_node_position(ctx, node);
SkRect rect{pos.x, pos.y, pos.x + pos.width, pos.y + pos.height}; SkRect rect{pos.x, pos.y, pos.x + pos.width, pos.y + pos.height};
SkPaint paint; SkPaint paint;
@@ -64,7 +74,7 @@ void clef_render_node(Clef::RenderingContext &ctx,
ctx.canvas->drawRect(rect, paint); ctx.canvas->drawRect(rect, paint);
ctx.layout_map.insert({node->id, pos}); ctx.layout_map.insert({node->id, pos});
for (const auto &child : cn->children) { for (const auto &child : box->children) {
clef_render_node(ctx, child); clef_render_node(ctx, child);
} }
break; break;

View File

@@ -41,8 +41,5 @@ void Clef::update_skia_surface(Clef::RenderingContext &ctx, int width,
ctx.gr_direct_context.get(), ctx.render_target, ctx.gr_direct_context.get(), ctx.render_target,
kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
SkColorSpace::MakeSRGB(), nullptr); SkColorSpace::MakeSRGB(), nullptr);
std::cout << "sk surface" << ctx.sk_surface << std::endl;
ctx.canvas = ctx.sk_surface->getCanvas(); ctx.canvas = ctx.sk_surface->getCanvas();
std::cout << " kasdjkjsak" << ctx.sk_surface->getCanvas() << std::endl;
std::cout << "new canvas address " << ctx.canvas << std::endl;
} }

1
test_cml/code.cml Normal file
View File

@@ -0,0 +1 @@
<bundle src="/app.bundle" />

9
test_cml/header.cml Normal file
View File

@@ -0,0 +1,9 @@
<meta>
<fragment>
<name>header</header>
<attr type="string" name="content" />
</fragment>
</meta>
<text level="1" style="bold italic">{content}</text>

View File

@@ -1,4 +1,16 @@
<import src="https://google.com/header.cml" />
<box> <box>
<text level="1">hello from clef</text> <text level="1">hello from clef</text>
<text>this is rendered from hello.cml</text> <text>this is rendered from hello.cml</text>
</box> </box>
<header content="my content" />
<row id="kasjksajdkj">
<text level="1">hello from clef</text>
<text>this is rendered from hello.cml</text>
</row>
<bundle src="/app.bundle" />

17
test_cml/permission.cml Normal file
View File

@@ -0,0 +1,17 @@
<meta>
<title>asdklasdk</title>
<description>askdjksajdk</description>
<!-- permission system -->
<permissions>
<filesystem description="aksdjsadjksjdk" />
<camera description="asldklsadk" />
<location description="" type="precise|approximate" />
<gpu />
</permissions>
</meta>
<text id="label" />
<bundle src="./app.wasm" />

4
test_cml/test.cml Normal file
View File

@@ -0,0 +1,4 @@
<col>
<text>hello from clef</text>
<text>this is rendered from test.cml</text>
</col>