diff --git a/DejaVuSerif-Bold.ttf b/DejaVuSerif-Bold.ttf new file mode 100644 index 0000000..3bb755f Binary files /dev/null and b/DejaVuSerif-Bold.ttf differ diff --git a/DejaVuSerif.ttf b/DejaVuSerif.ttf index 64bd24c..0b803d2 100644 Binary files a/DejaVuSerif.ttf and b/DejaVuSerif.ttf differ diff --git a/Makefile b/Makefile index 51ec059..809279e 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ CXX = g++ -CXXFLAGS = -O3 +CXXFLAGS = -O3 -flto=8 INCPATH = -I /usr/include/freetype2 -I /usr/include/harfbuzz LINK = g++ -LDFLAGS = -O3 -flto +LDFLAGS = -O3 -flto=8 LIBS = -lglfw -lGL -lGLEW -lfreetype -lharfbuzz SRCDIR = src OBJDIR = gen diff --git a/cr b/cr index b8dc326..ba49c32 100755 --- a/cr +++ b/cr @@ -1,2 +1,2 @@ #!/bin/bash -make && ./netrunner http://gyroninja.net/a.html; +make -j8 && ./netrunner http://motherfuckingwebsite.com/ diff --git a/src/graphics/opengl/Window.cpp b/src/graphics/opengl/Window.cpp index fe7edfa..662d7a7 100644 --- a/src/graphics/opengl/Window.cpp +++ b/src/graphics/opengl/Window.cpp @@ -1,6 +1,9 @@ #include "Window.h" #include "shaders/gen/FontShader.h" #include "shaders/gen/TextureShader.h" +#include "../../html/TagNode.h" +#include "../../html/TextNode.h" +#include #include Window::~Window() { @@ -19,9 +22,8 @@ bool Window::init() { } initGL(); - boxes.push_back(std::make_unique(0.0f, 1.0f, 1.0f, -64, windowWidth, windowHeight)); - boxes.push_back(std::make_unique(-1024, 0.0f, 1024, 1024, windowWidth, windowHeight)); - texts.push_back(std::make_unique("Hello, World!", 32, 32, windowWidth, windowHeight)); + boxComponents.push_back(std::make_unique(0.0f, 1.0f, 1.0f, -64, windowWidth, windowHeight)); + boxComponents.push_back(std::make_unique(-512, 0.0f, 512, 512, windowWidth, windowHeight)); return true; } @@ -52,11 +54,12 @@ bool Window::initGLFW() { const float sy = (float) height / thiz->windowHeight; thiz->windowWidth = width; thiz->windowHeight = height; - for (const std::unique_ptr &box : thiz->boxes) { - box->resize(width, height); + for (const std::unique_ptr &boxComponent : thiz->boxComponents) { + boxComponent->resize(width, height); } - for (const std::unique_ptr &text : thiz->texts) { - text->resize(sx, sy); + for (const std::unique_ptr &component : thiz->components) { + TextComponent *textComponent = dynamic_cast(component.get()); + textComponent->resize(sx, sy); } }); glfwMakeContextCurrent(window); @@ -80,8 +83,6 @@ bool Window::initGL() { std::cout << "Renderer: " << renderer << std::endl; std::cout << "Version: " << version << std::endl; - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LESS); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glClearColor(0.8f, 0.8f, 0.8f, 0.8f); @@ -134,16 +135,41 @@ const GLuint Window::compileProgram(const GLuint vertexShader, const GLuint frag return program; } -void Window::render() const { +void Window::render() { + if (domDirty) { + drawNode(domRootNode); + domDirty = false; + } + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(textureProgram); - for (const std::unique_ptr &box : boxes) { - box->render(); + for (const std::unique_ptr &boxComponent : boxComponents) { + boxComponent->render(); } glUseProgram(fontProgram); - for (const std::unique_ptr &text : texts) { - text->render(); + for (const std::unique_ptr &component : components) { + TextComponent *textComponent = dynamic_cast(component.get()); + textComponent->render(); } glfwPollEvents(); glfwSwapBuffers(window); } + +void Window::setDOM(std::shared_ptr rootNode) { + domRootNode = rootNode; + domDirty = true; +} + +void Window::drawNode(std::shared_ptr node) { + if (node->nodeType == NodeType::TAG) { + TagNode *tagNode = dynamic_cast(node.get()); + std::unique_ptr component = tagNode->render(*tagNode, y, windowWidth, windowHeight); + if (component) { + components.push_back(tagNode->render(*tagNode, y, windowWidth, windowHeight)); + y -= component->height; + } + } + for (std::shared_ptr child : node->children) { + drawNode(child); + } +} diff --git a/src/graphics/opengl/Window.h b/src/graphics/opengl/Window.h index 5ea2154..00ead5e 100644 --- a/src/graphics/opengl/Window.h +++ b/src/graphics/opengl/Window.h @@ -1,8 +1,10 @@ #ifndef WINDOW_H #define WINDOW_H -#include "Box.h" -#include "Text.h" +#include "components/BoxComponent.h" +#include "components/TextComponent.h" +#include "components/Component.h" +#include "../../html/Node.h" #include #include #include @@ -20,12 +22,17 @@ public: bool initGL(); const GLuint compileShader(const GLenum shaderType, const char *shaderSource) const; const GLuint compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const; - void render() const; + void render(); + void setDOM(std::shared_ptr rootNode); + void drawNode(std::shared_ptr rootNode); GLFWwindow *window; int windowWidth; int windowHeight; - std::vector> boxes; - std::vector> texts; + std::shared_ptr domRootNode; + bool domDirty = false; + std::vector> boxComponents; + std::vector> components; + int y = 950; }; #endif diff --git a/src/graphics/opengl/Box.cpp b/src/graphics/opengl/components/BoxComponent.cpp similarity index 85% rename from src/graphics/opengl/Box.cpp rename to src/graphics/opengl/components/BoxComponent.cpp index 428b32f..e2817eb 100644 --- a/src/graphics/opengl/Box.cpp +++ b/src/graphics/opengl/components/BoxComponent.cpp @@ -1,14 +1,14 @@ -#include "Box.h" -#include "../../../anime.h" +#include "BoxComponent.h" +#include "../../../../anime.h" #include -Box::Box(const float x, const float y, const float width, const float height, const int windowWidth, const int windowHeight) { +BoxComponent::BoxComponent(const float x, const float y, const float width, const float height, const int windowWidth, const int windowHeight) { this->x = x; this->y = y; this->width = width; this->height = height; - if (width == 1024) { + if (width == 512) { for (int y = 0; y < 1024; y++) { for (int x = 0; x < 1024; x++) { for (int i = 0; i < 4; i++) { @@ -69,14 +69,14 @@ Box::Box(const float x, const float y, const float width, const float height, co glGenerateMipmap(GL_TEXTURE_2D); } -Box::~Box() { +BoxComponent::~BoxComponent() { glDeleteVertexArrays(1, &vertexArrayObject); glDeleteBuffers(1, &vertexBufferObject); glDeleteBuffers(1, &elementBufferObject); glDeleteTextures(1, &texture); } -void Box::render() { +void BoxComponent::render() { if (verticesDirty) { glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); @@ -87,7 +87,7 @@ void Box::render() { glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } -void Box::resize(const int windowWidth, const int windowHeight) { +void BoxComponent::resize(const int windowWidth, const int windowHeight) { float vx = x; float vy = y; float vWidth = width; @@ -107,7 +107,7 @@ void Box::resize(const int windowWidth, const int windowHeight) { verticesDirty = true; } -void Box::pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const { +void BoxComponent::pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const { if (x < 0) { x += windowWidth; } @@ -124,7 +124,7 @@ void Box::pointToViewport(float &x, float &y, const int windowWidth, const int w y = (y * 2) - 1; } -void Box::distanceToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const { +void BoxComponent::distanceToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const { if (std::abs(x) > 1) { x /= windowWidth; } diff --git a/src/graphics/opengl/Box.h b/src/graphics/opengl/components/BoxComponent.h similarity index 75% rename from src/graphics/opengl/Box.h rename to src/graphics/opengl/components/BoxComponent.h index d122a27..4b1c3f9 100644 --- a/src/graphics/opengl/Box.h +++ b/src/graphics/opengl/components/BoxComponent.h @@ -1,14 +1,14 @@ -#ifndef BOX_H -#define BOX_H +#ifndef BOXCOMPONENT_H +#define BOXCOMPONENT_H #include +#include "Component.h" -class Box { +class BoxComponent : public Component { private: float x; float y; float width; - float height; float vertices[20] = { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, @@ -26,8 +26,8 @@ private: GLuint elementBufferObject = 0; GLuint texture = 0; public: - Box(const float x, const float y, const float width, const float height, const int windowWidth, const int windowHeight); - ~Box(); + BoxComponent(const float x, const float y, const float width, const float height, const int windowWidth, const int windowHeight); + ~BoxComponent(); void render(); void resize(const int width, const int height); void pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const ; diff --git a/src/graphics/opengl/components/Component.cpp b/src/graphics/opengl/components/Component.cpp new file mode 100644 index 0000000..5ed8969 --- /dev/null +++ b/src/graphics/opengl/components/Component.cpp @@ -0,0 +1,4 @@ +#include "Component.h" + +Component::~Component() { +} diff --git a/src/graphics/opengl/components/Component.h b/src/graphics/opengl/components/Component.h new file mode 100644 index 0000000..5376402 --- /dev/null +++ b/src/graphics/opengl/components/Component.h @@ -0,0 +1,10 @@ +#ifndef COMPONENT_H +#define COMPONENT_H + +class Component { +public: + virtual ~Component(); + float height; +}; + +#endif \ No newline at end of file diff --git a/src/graphics/opengl/Text.cpp b/src/graphics/opengl/components/TextComponent.cpp similarity index 87% rename from src/graphics/opengl/Text.cpp rename to src/graphics/opengl/components/TextComponent.cpp index 2256b3f..c2b09f9 100644 --- a/src/graphics/opengl/Text.cpp +++ b/src/graphics/opengl/components/TextComponent.cpp @@ -1,14 +1,14 @@ -#include "Text.h" -#include "../text/TextRasterizer.h" +#include "TextComponent.h" +#include "../../text/TextRasterizer.h" #include -Text::Text(const std::string &text, const int x, const int y, const int windowWidth, const int windowHeight) { +TextComponent::TextComponent(const std::string &text, const int x, const int y, const int fontSize, const bool bold, const int windowWidth, const int windowHeight) { this->x = x; this->y = y; - const std::unique_ptr textRasterizer = std::make_unique("DejaVuSerif.ttf", 50, 72); + const std::unique_ptr textRasterizer = std::make_unique("DejaVuSerif.ttf", fontSize, 72, bold); unsigned int glyphCount; - std::unique_ptr glyphs = textRasterizer->rasterize(text, x, y, glyphCount); + std::unique_ptr glyphs = textRasterizer->rasterize(text, x, y, height, glyphCount); if (glyphs == nullptr) { return; } @@ -75,7 +75,7 @@ Text::Text(const std::string &text, const int x, const int y, const int windowWi } } -Text::~Text() { +TextComponent::~TextComponent() { for (int i = 0; i < vertexArrayObjects.size(); i++) { glDeleteVertexArrays(1, &vertexArrayObjects[i]); glDeleteBuffers(1, &vertexBufferObjects[i]); @@ -84,7 +84,7 @@ Text::~Text() { } } -void Text::render() { +void TextComponent::render() { if (verticesDirty) { for (int i = 0; i < vertexArrayObjects.size(); i++) { glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjects[i]); @@ -92,14 +92,14 @@ void Text::render() { } verticesDirty = false; } - for (int i = 0; i < vertexArrayObjects.size(); i++) { + for (int i = vertexArrayObjects.size() - 1; i >= 0; i--) { glBindVertexArray(vertexArrayObjects[i]); glBindTexture(GL_TEXTURE_2D, textures[i]); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } } -void Text::resize(const float sx, const float sy) { +void TextComponent::resize(const float sx, const float sy) { for (int i = 0; i < glyphVertices.size(); i++) { glyphVertices[i][(0 * 5) + 0] = ((glyphVertices[i][(0 * 5) + 0] + 1) / sx) - 1; glyphVertices[i][(0 * 5) + 1] = ((glyphVertices[i][(0 * 5) + 1] + 1) / sy) - 1; @@ -113,7 +113,7 @@ void Text::resize(const float sx, const float sy) { verticesDirty = true; } -void Text::pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const { +void TextComponent::pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const { x = ((x / windowWidth) * 2) - 1; y = ((y / windowHeight) * 2) - 1; } diff --git a/src/graphics/opengl/Text.h b/src/graphics/opengl/components/TextComponent.h similarity index 69% rename from src/graphics/opengl/Text.h rename to src/graphics/opengl/components/TextComponent.h index 88703de..bc6b235 100644 --- a/src/graphics/opengl/Text.h +++ b/src/graphics/opengl/components/TextComponent.h @@ -1,17 +1,17 @@ -#ifndef TEXT_H -#define TEXT_H +#ifndef TEXTCOMPONENT_H +#define TEXTCOMPONENT_H #include +#include "Component.h" #include #include #include -class Text { +class TextComponent : public Component { private: float x; float y; float width; - float height; bool verticesDirty = false; const unsigned int indices[6] = { 0, 1, 2, @@ -24,8 +24,8 @@ private: std::vector elementBufferObjects; std::vector textures; public: - Text(const std::string &text, const int x, const int y, const int windowWidth, const int windowHeight); - ~Text(); + TextComponent(const std::string &text, const int x, const int y, const int fontSize, const bool bold,const int windowWidth, const int windowHeight); + ~TextComponent(); void render(); void resize(const float sx, const float sy); void pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const; diff --git a/src/graphics/text/TextRasterizer.cpp b/src/graphics/text/TextRasterizer.cpp index 3cdbe30..074fc14 100644 --- a/src/graphics/text/TextRasterizer.cpp +++ b/src/graphics/text/TextRasterizer.cpp @@ -3,11 +3,12 @@ #include #include -TextRasterizer::TextRasterizer(const std::string &fontPath, int size, int resolution) { +TextRasterizer::TextRasterizer(const std::string &fontPath, const int fontSize, const int resolution, const bool bold) { + this->fontSize = fontSize; FT_Init_FreeType(&lib); face = std::make_unique(); - if (FT_New_Face(lib, fontPath.c_str(), 0, face.get())) { + if (FT_New_Face(lib, bold ? (fontPath.substr(0, fontPath.length() - 4) + "-Bold.ttf").c_str() : fontPath.c_str(), 0, face.get())) { std::cout << "Could not open font" << std::endl; return; } @@ -15,7 +16,7 @@ TextRasterizer::TextRasterizer(const std::string &fontPath, int size, int resolu std::cout << "Font is not Unicode BMP" << std::endl; return; } - if (FT_Set_Char_Size(*face, 0, size * 64, resolution, resolution)) { + if (FT_Set_Char_Size(*face, 0, fontSize * 64, resolution, resolution)) { std::cout << "Could not set font size" << std::endl; return; } @@ -35,7 +36,7 @@ TextRasterizer::~TextRasterizer() { FT_Done_FreeType(lib); } -std::unique_ptr TextRasterizer::rasterize(const std::string &text, const int x, const int y, unsigned int &glyphCount) const { +std::unique_ptr TextRasterizer::rasterize(const std::string &text, const int x, const int y, float &height, unsigned int &glyphCount) const { hb_buffer_reset(buffer); hb_buffer_set_direction(buffer, HB_DIRECTION_LTR); hb_buffer_set_language(buffer, hb_language_from_string("en", 2)); @@ -91,8 +92,20 @@ std::unique_ptr TextRasterizer::rasterize(const std::string &text glyphs[i].t1 = (float) ftBitmap.rows / glyphs[i].textureHeight; cx += xa; - cx += ya; + cy += ya; + + if (glyphs[i].x1 >= 1920) { + glyphs[i].x0 -= cx; + glyphs[i].y0 -= std::ceil(1.2f * fontSize); // 1.2 scalar from https://developer.mozilla.org/en-US/docs/Web/CSS/line-height + glyphs[i].x1 -= cx; + glyphs[i].y1 -= std::ceil(1.2f * fontSize); + cx -= cx; + cy -= std::ceil(1.2f * fontSize); + } } + cy -= std::ceil(1.2f * fontSize); // 1.2 scalar from https://developer.mozilla.org/en-US/docs/Web/CSS/line-height + + height = y - cy; return glyphs; } diff --git a/src/graphics/text/TextRasterizer.h b/src/graphics/text/TextRasterizer.h index a8284ba..37e7e0d 100644 --- a/src/graphics/text/TextRasterizer.h +++ b/src/graphics/text/TextRasterizer.h @@ -21,10 +21,12 @@ struct Glyph { }; class TextRasterizer { +private: + int fontSize; public: - TextRasterizer(const std::string &fontPath, int size, int resolution); + TextRasterizer(const std::string &fontPath, const int size, const int resolution, const bool bold); ~TextRasterizer(); - std::unique_ptr rasterize(const std::string &text, const int x, const int y, unsigned int &glyphCount) const; + std::unique_ptr rasterize(const std::string &text, const int x, const int y, float &height, unsigned int &glyphCount) const; const bool isUnicodeBMP(const FT_Face &face) const; FT_Library lib; hb_font_t *font; diff --git a/src/html/HTMLParser.cpp b/src/html/HTMLParser.cpp index 53d2ae8..8d0b841 100644 --- a/src/html/HTMLParser.cpp +++ b/src/html/HTMLParser.cpp @@ -29,7 +29,7 @@ void printNode(const std::shared_ptr node, const int indent) { } } -void HTMLParser::parse(const std::string &html) const { +std::shared_ptr HTMLParser::parse(const std::string &html) const { std::shared_ptr rootNode = std::make_shared(NodeType::ROOT); std::shared_ptr currentNode = rootNode; std::vector starts; @@ -91,7 +91,8 @@ void HTMLParser::parse(const std::string &html) const { } } - printNode(rootNode, 0); +// printNode(rootNode, 0); + return rootNode; } void HTMLParser::parseTag(const std::string &element, TagNode* tagNode) const { diff --git a/src/html/HTMLParser.h b/src/html/HTMLParser.h index a09f726..2de159e 100644 --- a/src/html/HTMLParser.h +++ b/src/html/HTMLParser.h @@ -7,7 +7,7 @@ class HTMLParser { public: - void parse(const std::string &html) const; + std::shared_ptr parse(const std::string &html) const; void parseTag(const std::string &element, TagNode* tagNode) const; }; diff --git a/src/html/TagNode.cpp b/src/html/TagNode.cpp index 2c0ffac..0d89c70 100644 --- a/src/html/TagNode.cpp +++ b/src/html/TagNode.cpp @@ -1,5 +1,27 @@ #include "TagNode.h" +#include "elements/H1Element.h" +#include "elements/H2Element.h" +#include "elements/H3Element.h" +#include "elements/LIElement.h" +#include "elements/PElement.h" + +const Element TagNode::elements[] = { + {"h1", &H1Element::render}, + {"h2", &H2Element::render}, + {"h3", &H3Element::render}, + {"li", &LIElement::render}, + {"p", &PElement::render} +}; TagNode::TagNode() : Node(NodeType::TAG) { -} \ No newline at end of file +} + +std::unique_ptr TagNode::render(const Node &node, int y, int windowWidth, int windowHeight) { + for (Element element : elements) { + if (tag == element.tag) { + return element.render(node, y, windowWidth, windowHeight); + } + } + return nullptr; +} diff --git a/src/html/TagNode.h b/src/html/TagNode.h index 314d588..fbc774b 100644 --- a/src/html/TagNode.h +++ b/src/html/TagNode.h @@ -2,11 +2,20 @@ #define TAGNODE_H #include "Node.h" +#include "../graphics/opengl/components/Component.h" #include +struct Element { + std::string tag; + std::function(const Node &node, int y, int windowWidth, int windowHeight)> render; +}; + class TagNode : public Node { +private: + static const Element elements[]; public: TagNode(); + std::unique_ptr render(const Node &node, int y, int windowWidth, int windowHeight); std::string tag; std::map properties; }; diff --git a/src/html/elements/H1Element.cpp b/src/html/elements/H1Element.cpp new file mode 100644 index 0000000..04be4f1 --- /dev/null +++ b/src/html/elements/H1Element.cpp @@ -0,0 +1,8 @@ +#include "H1Element.h" +#include "../TextNode.h" +#include + +std::unique_ptr H1Element::render(const Node &node, int y, int windowWidth, int windowHeight) { + TextNode *textNode = dynamic_cast(node.children[0].get()); + return std::make_unique(textNode->text, 0, y, 24, true, windowWidth, windowHeight); +} diff --git a/src/html/elements/H1Element.h b/src/html/elements/H1Element.h new file mode 100644 index 0000000..5980b75 --- /dev/null +++ b/src/html/elements/H1Element.h @@ -0,0 +1,13 @@ +#ifndef H1ELEMENT_H +#define H1ELEMENT_H + +#include "../../graphics/opengl/components/Component.h" +#include "../../graphics/opengl/components/TextComponent.h" +#include "../Node.h" + +class H1Element { +public: + static std::unique_ptr render(const Node &node, int y, int windowWidth, int windowHeight); +}; + +#endif \ No newline at end of file diff --git a/src/html/elements/H2Element.cpp b/src/html/elements/H2Element.cpp new file mode 100644 index 0000000..3ba9e15 --- /dev/null +++ b/src/html/elements/H2Element.cpp @@ -0,0 +1,8 @@ +#include "H2Element.h" +#include "../TextNode.h" +#include + +std::unique_ptr H2Element::render(const Node &node, int y, int windowWidth, int windowHeight) { + TextNode *textNode = dynamic_cast(node.children[0].get()); + return std::make_unique(textNode->text, 0, y, 18, true, windowWidth, windowHeight); +} diff --git a/src/html/elements/H2Element.h b/src/html/elements/H2Element.h new file mode 100644 index 0000000..234c1e2 --- /dev/null +++ b/src/html/elements/H2Element.h @@ -0,0 +1,13 @@ +#ifndef H2ELEMENT_H +#define H2ELEMENT_H + +#include "../../graphics/opengl/components/Component.h" +#include "../../graphics/opengl/components/TextComponent.h" +#include "../Node.h" + +class H2Element { +public: + static std::unique_ptr render(const Node &node, int y, int windowWidth, int windowHeight); +}; + +#endif \ No newline at end of file diff --git a/src/html/elements/H3Element.cpp b/src/html/elements/H3Element.cpp new file mode 100644 index 0000000..bbed914 --- /dev/null +++ b/src/html/elements/H3Element.cpp @@ -0,0 +1,8 @@ +#include "H3Element.h" +#include "../TextNode.h" +#include + +std::unique_ptr H3Element::render(const Node &node, int y, int windowWidth, int windowHeight) { + TextNode *textNode = dynamic_cast(node.children[0].get()); + return std::make_unique(textNode->text, 0, y, 14, true, windowWidth, windowHeight); // Should be 14.04pt +} diff --git a/src/html/elements/H3Element.h b/src/html/elements/H3Element.h new file mode 100644 index 0000000..a264421 --- /dev/null +++ b/src/html/elements/H3Element.h @@ -0,0 +1,13 @@ +#ifndef H3ELEMENT_H +#define H3ELEMENT_H + +#include "../../graphics/opengl/components/Component.h" +#include "../../graphics/opengl/components/TextComponent.h" +#include "../Node.h" + +class H3Element { +public: + static std::unique_ptr render(const Node &node, int y, int windowWidth, int windowHeight); +}; + +#endif \ No newline at end of file diff --git a/src/html/elements/LIElement.cpp b/src/html/elements/LIElement.cpp new file mode 100644 index 0000000..42afcd8 --- /dev/null +++ b/src/html/elements/LIElement.cpp @@ -0,0 +1,8 @@ +#include "LIElement.h" +#include "../TextNode.h" +#include + +std::unique_ptr LIElement::render(const Node &node, int y, int windowWidth, int windowHeight) { + TextNode *textNode = dynamic_cast(node.children[0].get()); + return std::make_unique("• " + textNode->text, 0, y, 12, false, windowWidth, windowHeight); +} diff --git a/src/html/elements/LIElement.h b/src/html/elements/LIElement.h new file mode 100644 index 0000000..01de6b2 --- /dev/null +++ b/src/html/elements/LIElement.h @@ -0,0 +1,13 @@ +#ifndef LIELEMENT_H +#define LIELEMENT_H + +#include "../../graphics/opengl/components/Component.h" +#include "../../graphics/opengl/components/TextComponent.h" +#include "../Node.h" + +class LIElement { +public: + static std::unique_ptr render(const Node &node, int y, int windowWidth, int windowHeight); +}; + +#endif \ No newline at end of file diff --git a/src/html/elements/PElement.cpp b/src/html/elements/PElement.cpp new file mode 100644 index 0000000..3249a6f --- /dev/null +++ b/src/html/elements/PElement.cpp @@ -0,0 +1,8 @@ +#include "PElement.h" +#include "../TextNode.h" +#include + +std::unique_ptr PElement::render(const Node &node, int y, int windowWidth, int windowHeight) { + TextNode *textNode = dynamic_cast(node.children[0].get()); + return std::make_unique(textNode->text, 0, y, 12, false, windowWidth, windowHeight); +} diff --git a/src/html/elements/PElement.h b/src/html/elements/PElement.h new file mode 100644 index 0000000..11946a1 --- /dev/null +++ b/src/html/elements/PElement.h @@ -0,0 +1,13 @@ +#ifndef PELEMENT_H +#define PELEMENT_H + +#include "../../graphics/opengl/components/Component.h" +#include "../../graphics/opengl/components/TextComponent.h" +#include "../Node.h" + +class PElement { +public: + static std::unique_ptr render(const Node &node, int y, int windowWidth, int windowHeight); +}; + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4089c6b..0901cdb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,9 +4,12 @@ #include "html/TextNode.h" #include "networking/HTTPRequest.h" #include "networking/HTTPResponse.h" +#include #include #include +const std::unique_ptr window = std::make_unique(); + const std::string getDocumentFromURL(const std::string &url) { int slashes = 0; for (int i = 0; i < url.length(); i++) { @@ -36,12 +39,15 @@ const std::string getHostFromURL(const std::string &url) { void handleRequest(const HTTPResponse &response) { if (response.statusCode == 200) { const std::unique_ptr parser = std::make_unique(); - parser->parse(response.body); + std::clock_t begin = clock(); + std::shared_ptr rootNode = parser->parse(response.body); + std::clock_t end = clock(); + std::cout << "Parsed document in: " << std::fixed << (((double) (end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl; + window->setDOM(rootNode); } else if (response.statusCode == 301) { const std::string location = response.properties.at("Location"); std::cout << "Redirect To: " << location << std::endl; - std::cout << getHostFromURL(location) << "!" << getDocumentFromURL(location) << std::endl; const std::unique_ptr request = std::make_unique(getHostFromURL(location), getDocumentFromURL(location)); request->sendRequest(handleRequest); return; @@ -58,7 +64,6 @@ int main(int argc, char *argv[]) { } const std::unique_ptr request = std::make_unique(getHostFromURL(argv[1]), getDocumentFromURL(argv[1])); request->sendRequest(handleRequest); - const std::unique_ptr window = std::make_unique(); window->init(); while (!glfwWindowShouldClose(window->window)) { window->render();