Browse Source

Start rendering the DOM tree

pull/1/head
gyroninja 5 years ago
parent
commit
292812ff81
  1. BIN
      DejaVuSerif-Bold.ttf
  2. BIN
      DejaVuSerif.ttf
  3. 4
      Makefile
  4. 2
      cr
  5. 54
      src/graphics/opengl/Window.cpp
  6. 17
      src/graphics/opengl/Window.h
  7. 18
      src/graphics/opengl/components/BoxComponent.cpp
  8. 12
      src/graphics/opengl/components/BoxComponent.h
  9. 4
      src/graphics/opengl/components/Component.cpp
  10. 10
      src/graphics/opengl/components/Component.h
  11. 20
      src/graphics/opengl/components/TextComponent.cpp
  12. 12
      src/graphics/opengl/components/TextComponent.h
  13. 23
      src/graphics/text/TextRasterizer.cpp
  14. 6
      src/graphics/text/TextRasterizer.h
  15. 5
      src/html/HTMLParser.cpp
  16. 2
      src/html/HTMLParser.h
  17. 24
      src/html/TagNode.cpp
  18. 9
      src/html/TagNode.h
  19. 8
      src/html/elements/H1Element.cpp
  20. 13
      src/html/elements/H1Element.h
  21. 8
      src/html/elements/H2Element.cpp
  22. 13
      src/html/elements/H2Element.h
  23. 8
      src/html/elements/H3Element.cpp
  24. 13
      src/html/elements/H3Element.h
  25. 8
      src/html/elements/LIElement.cpp
  26. 13
      src/html/elements/LIElement.h
  27. 8
      src/html/elements/PElement.cpp
  28. 13
      src/html/elements/PElement.h
  29. 11
      src/main.cpp

BIN
DejaVuSerif-Bold.ttf

Binary file not shown.

BIN
DejaVuSerif.ttf

Binary file not shown.

4
Makefile

@ -1,8 +1,8 @@ @@ -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

2
cr

@ -1,2 +1,2 @@ @@ -1,2 +1,2 @@
#!/bin/bash
make && ./netrunner http://gyroninja.net/a.html;
make -j8 && ./netrunner http://motherfuckingwebsite.com/

54
src/graphics/opengl/Window.cpp

@ -1,6 +1,9 @@ @@ -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 <cmath>
#include <iostream>
Window::~Window() {
@ -19,9 +22,8 @@ bool Window::init() { @@ -19,9 +22,8 @@ bool Window::init() {
}
initGL();
boxes.push_back(std::make_unique<Box>(0.0f, 1.0f, 1.0f, -64, windowWidth, windowHeight));
boxes.push_back(std::make_unique<Box>(-1024, 0.0f, 1024, 1024, windowWidth, windowHeight));
texts.push_back(std::make_unique<Text>("Hello, World!", 32, 32, windowWidth, windowHeight));
boxComponents.push_back(std::make_unique<BoxComponent>(0.0f, 1.0f, 1.0f, -64, windowWidth, windowHeight));
boxComponents.push_back(std::make_unique<BoxComponent>(-512, 0.0f, 512, 512, windowWidth, windowHeight));
return true;
}
@ -52,11 +54,12 @@ bool Window::initGLFW() { @@ -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> &box : thiz->boxes) {
box->resize(width, height);
for (const std::unique_ptr<BoxComponent> &boxComponent : thiz->boxComponents) {
boxComponent->resize(width, height);
}
for (const std::unique_ptr<Text> &text : thiz->texts) {
text->resize(sx, sy);
for (const std::unique_ptr<Component> &component : thiz->components) {
TextComponent *textComponent = dynamic_cast<TextComponent*>(component.get());
textComponent->resize(sx, sy);
}
});
glfwMakeContextCurrent(window);
@ -80,8 +83,6 @@ bool Window::initGL() { @@ -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 @@ -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> &box : boxes) {
box->render();
for (const std::unique_ptr<BoxComponent> &boxComponent : boxComponents) {
boxComponent->render();
}
glUseProgram(fontProgram);
for (const std::unique_ptr<Text> &text : texts) {
text->render();
for (const std::unique_ptr<Component> &component : components) {
TextComponent *textComponent = dynamic_cast<TextComponent*>(component.get());
textComponent->render();
}
glfwPollEvents();
glfwSwapBuffers(window);
}
void Window::setDOM(std::shared_ptr<Node> rootNode) {
domRootNode = rootNode;
domDirty = true;
}
void Window::drawNode(std::shared_ptr<Node> node) {
if (node->nodeType == NodeType::TAG) {
TagNode *tagNode = dynamic_cast<TagNode*>(node.get());
std::unique_ptr<Component> 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<Node> child : node->children) {
drawNode(child);
}
}

17
src/graphics/opengl/Window.h

@ -1,8 +1,10 @@ @@ -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 <GL/glew.h>
#include <GLFW/glfw3.h>
#include <memory>
@ -20,12 +22,17 @@ public: @@ -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<Node> rootNode);
void drawNode(std::shared_ptr<Node> rootNode);
GLFWwindow *window;
int windowWidth;
int windowHeight;
std::vector<std::unique_ptr<Box>> boxes;
std::vector<std::unique_ptr<Text>> texts;
std::shared_ptr<Node> domRootNode;
bool domDirty = false;
std::vector<std::unique_ptr<BoxComponent>> boxComponents;
std::vector<std::unique_ptr<Component>> components;
int y = 950;
};
#endif

18
src/graphics/opengl/Box.cpp → src/graphics/opengl/components/BoxComponent.cpp

@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
#include "Box.h"
#include "../../../anime.h"
#include "BoxComponent.h"
#include "../../../../anime.h"
#include <cmath>
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 @@ -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() { @@ -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) { @@ -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 @@ -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;
}

12
src/graphics/opengl/Box.h → src/graphics/opengl/components/BoxComponent.h

@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
#ifndef BOX_H
#define BOX_H
#ifndef BOXCOMPONENT_H
#define BOXCOMPONENT_H
#include <GL/glew.h>
#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: @@ -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 ;

4
src/graphics/opengl/components/Component.cpp

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
#include "Component.h"
Component::~Component() {
}

10
src/graphics/opengl/components/Component.h

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
#ifndef COMPONENT_H
#define COMPONENT_H
class Component {
public:
virtual ~Component();
float height;
};
#endif

20
src/graphics/opengl/Text.cpp → src/graphics/opengl/components/TextComponent.cpp

@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
#include "Text.h"
#include "../text/TextRasterizer.h"
#include "TextComponent.h"
#include "../../text/TextRasterizer.h"
#include <memory>
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> textRasterizer = std::make_unique<TextRasterizer>("DejaVuSerif.ttf", 50, 72);
const std::unique_ptr<TextRasterizer> textRasterizer = std::make_unique<TextRasterizer>("DejaVuSerif.ttf", fontSize, 72, bold);
unsigned int glyphCount;
std::unique_ptr<const Glyph[]> glyphs = textRasterizer->rasterize(text, x, y, glyphCount);
std::unique_ptr<const Glyph[]> 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 @@ -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() { @@ -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() { @@ -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) { @@ -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;
}

12
src/graphics/opengl/Text.h → src/graphics/opengl/components/TextComponent.h

@ -1,17 +1,17 @@ @@ -1,17 +1,17 @@
#ifndef TEXT_H
#define TEXT_H
#ifndef TEXTCOMPONENT_H
#define TEXTCOMPONENT_H
#include <GL/glew.h>
#include "Component.h"
#include <array>
#include <memory>
#include <vector>
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: @@ -24,8 +24,8 @@ private:
std::vector<GLuint> elementBufferObjects;
std::vector<GLuint> 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;

23
src/graphics/text/TextRasterizer.cpp

@ -3,11 +3,12 @@ @@ -3,11 +3,12 @@
#include <limits>
#include <iostream>
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<FT_Face>();
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 @@ -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() { @@ -35,7 +36,7 @@ TextRasterizer::~TextRasterizer() {
FT_Done_FreeType(lib);
}
std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text, const int x, const int y, unsigned int &glyphCount) const {
std::unique_ptr<const Glyph[]> 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<const Glyph[]> TextRasterizer::rasterize(const std::string &text @@ -91,8 +92,20 @@ std::unique_ptr<const Glyph[]> 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;
}

6
src/graphics/text/TextRasterizer.h

@ -21,10 +21,12 @@ struct Glyph { @@ -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<const Glyph[]> rasterize(const std::string &text, const int x, const int y, unsigned int &glyphCount) const;
std::unique_ptr<const Glyph[]> 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;

5
src/html/HTMLParser.cpp

@ -29,7 +29,7 @@ void printNode(const std::shared_ptr<Node> node, const int indent) { @@ -29,7 +29,7 @@ void printNode(const std::shared_ptr<Node> node, const int indent) {
}
}
void HTMLParser::parse(const std::string &html) const {
std::shared_ptr<Node> HTMLParser::parse(const std::string &html) const {
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
std::shared_ptr<Node> currentNode = rootNode;
std::vector<int> starts;
@ -91,7 +91,8 @@ void HTMLParser::parse(const std::string &html) const { @@ -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 {

2
src/html/HTMLParser.h

@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
class HTMLParser {
public:
void parse(const std::string &html) const;
std::shared_ptr<Node> parse(const std::string &html) const;
void parseTag(const std::string &element, TagNode* tagNode) const;
};

24
src/html/TagNode.cpp

@ -1,5 +1,27 @@ @@ -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) {
}
}
std::unique_ptr<Component> 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;
}

9
src/html/TagNode.h

@ -2,11 +2,20 @@ @@ -2,11 +2,20 @@
#define TAGNODE_H
#include "Node.h"
#include "../graphics/opengl/components/Component.h"
#include <map>
struct Element {
std::string tag;
std::function<std::unique_ptr<Component>(const Node &node, int y, int windowWidth, int windowHeight)> render;
};
class TagNode : public Node {
private:
static const Element elements[];
public:
TagNode();
std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
std::string tag;
std::map<std::string, std::string> properties;
};

8
src/html/elements/H1Element.cpp

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
#include "H1Element.h"
#include "../TextNode.h"
#include <iostream>
std::unique_ptr<Component> H1Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.children[0].get());
return std::make_unique<TextComponent>(textNode->text, 0, y, 24, true, windowWidth, windowHeight);
}

13
src/html/elements/H1Element.h

@ -0,0 +1,13 @@ @@ -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<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif

8
src/html/elements/H2Element.cpp

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
#include "H2Element.h"
#include "../TextNode.h"
#include <iostream>
std::unique_ptr<Component> H2Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.children[0].get());
return std::make_unique<TextComponent>(textNode->text, 0, y, 18, true, windowWidth, windowHeight);
}

13
src/html/elements/H2Element.h

@ -0,0 +1,13 @@ @@ -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<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif

8
src/html/elements/H3Element.cpp

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
#include "H3Element.h"
#include "../TextNode.h"
#include <iostream>
std::unique_ptr<Component> H3Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.children[0].get());
return std::make_unique<TextComponent>(textNode->text, 0, y, 14, true, windowWidth, windowHeight); // Should be 14.04pt
}

13
src/html/elements/H3Element.h

@ -0,0 +1,13 @@ @@ -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<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif

8
src/html/elements/LIElement.cpp

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
#include "LIElement.h"
#include "../TextNode.h"
#include <iostream>
std::unique_ptr<Component> LIElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.children[0].get());
return std::make_unique<TextComponent>("" + textNode->text, 0, y, 12, false, windowWidth, windowHeight);
}

13
src/html/elements/LIElement.h

@ -0,0 +1,13 @@ @@ -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<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif

8
src/html/elements/PElement.cpp

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
#include "PElement.h"
#include "../TextNode.h"
#include <iostream>
std::unique_ptr<Component> PElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.children[0].get());
return std::make_unique<TextComponent>(textNode->text, 0, y, 12, false, windowWidth, windowHeight);
}

13
src/html/elements/PElement.h

@ -0,0 +1,13 @@ @@ -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<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif

11
src/main.cpp

@ -4,9 +4,12 @@ @@ -4,9 +4,12 @@
#include "html/TextNode.h"
#include "networking/HTTPRequest.h"
#include "networking/HTTPResponse.h"
#include <ctime>
#include <iostream>
#include <memory>
const std::unique_ptr<Window> window = std::make_unique<Window>();
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) { @@ -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<HTMLParser> parser = std::make_unique<HTMLParser>();
parser->parse(response.body);
std::clock_t begin = clock();
std::shared_ptr<Node> 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<HTTPRequest> request = std::make_unique<HTTPRequest>(getHostFromURL(location), getDocumentFromURL(location));
request->sendRequest(handleRequest);
return;
@ -58,7 +64,6 @@ int main(int argc, char *argv[]) { @@ -58,7 +64,6 @@ int main(int argc, char *argv[]) {
}
const std::unique_ptr<HTTPRequest> request = std::make_unique<HTTPRequest>(getHostFromURL(argv[1]), getDocumentFromURL(argv[1]));
request->sendRequest(handleRequest);
const std::unique_ptr<Window> window = std::make_unique<Window>();
window->init();
while (!glfwWindowShouldClose(window->window)) {
window->render();

Loading…
Cancel
Save