Browse Source

inline tag

pull/1/head
gyroninja 5 years ago
parent
commit
9720e39231
  1. 10
      src/graphics/components/ComponentBuilder.cpp
  2. 3
      src/graphics/components/TextComponent.cpp
  3. 4
      src/graphics/elements/AElement.cpp
  4. 2
      src/graphics/elements/AElement.h
  5. 4
      src/graphics/elements/BLOCKQUOTEElement.cpp
  6. 2
      src/graphics/elements/BLOCKQUOTEElement.h
  7. 2
      src/graphics/elements/Element.h
  8. 4
      src/graphics/elements/H1Element.cpp
  9. 2
      src/graphics/elements/H1Element.h
  10. 4
      src/graphics/elements/H2Element.cpp
  11. 2
      src/graphics/elements/H2Element.h
  12. 4
      src/graphics/elements/H3Element.cpp
  13. 2
      src/graphics/elements/H3Element.h
  14. 4
      src/graphics/elements/LIElement.cpp
  15. 2
      src/graphics/elements/LIElement.h
  16. 4
      src/graphics/elements/PElement.cpp
  17. 2
      src/graphics/elements/PElement.h
  18. 4
      src/graphics/elements/SPANElement.cpp
  19. 2
      src/graphics/elements/SPANElement.h
  20. 34
      src/graphics/opengl/Window.cpp
  21. 10
      src/graphics/text/TextRasterizer.cpp
  22. 2
      src/graphics/text/TextRasterizer.h

10
src/graphics/components/ComponentBuilder.cpp

@ -34,10 +34,16 @@ std::shared_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> n @@ -34,10 +34,16 @@ std::shared_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> n
std::unordered_map<std::string, std::shared_ptr<Element>>::const_iterator elementPair = elementMap.find(tag);
if (elementPair != elementMap.end()) {
std::shared_ptr<Element> element = elementPair->second;
if (element->isInline) {
if (!element->isInline) {
component = element->renderer(node, 0, y, windowWidth, windowHeight);
}
else {
y += parentComponent->height;
component = element->renderer(node, parentComponent->parent->width, y, windowWidth, windowHeight);
if (component) {
component->isInline = true;
}
}
component = element->renderer(node, y, windowWidth, windowHeight);
}
if (!component) {

3
src/graphics/components/TextComponent.cpp

@ -62,7 +62,7 @@ TextComponent::~TextComponent() { @@ -62,7 +62,7 @@ TextComponent::~TextComponent() {
void TextComponent::rasterize(const int rawX, const int rawY, const int windowWidth, const int windowHeight) {
const std::unique_ptr<TextRasterizer> textRasterizer = std::make_unique<TextRasterizer>("DejaVuSerif.ttf", fontSize, 72, bold);
unsigned int glyphCount;
glyphs = textRasterizer->rasterize(text, rawX, rawY, windowWidth, windowHeight, height, glyphCount);
glyphs = textRasterizer->rasterize(text, rawX, rawY, windowWidth, windowHeight, width, height, glyphCount);
if (glyphs == nullptr) {
return;
}
@ -136,6 +136,7 @@ void TextComponent::render() { @@ -136,6 +136,7 @@ void TextComponent::render() {
}
void TextComponent::resize(const int rawX, const int rawY, const int windowWidth, const int windowHeight) {
x = rawX;
y = rawY;
rasterize(rawX, rawY, windowWidth, windowHeight);
verticesDirty = true;

4
src/graphics/elements/AElement.cpp

@ -4,10 +4,10 @@ AElement::AElement() { @@ -4,10 +4,10 @@ AElement::AElement() {
isInline = true;
}
std::unique_ptr<Component> AElement::renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight) {
std::unique_ptr<Component> AElement::renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
return std::make_unique<TextComponent>(textNode->text, 0, y, 12, false, 0x00FFFF, windowWidth, windowHeight);
return std::make_unique<TextComponent>(textNode->text, x, y, 12, false, 0x00FFFF, windowWidth, windowHeight);
}
return nullptr;
}

2
src/graphics/elements/AElement.h

@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
class AElement : public Element {
public:
AElement();
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

4
src/graphics/elements/BLOCKQUOTEElement.cpp

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#include "BLOCKQUOTEElement.h"
std::unique_ptr<Component> BLOCKQUOTEElement::renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight) {
std::unique_ptr<Component> BLOCKQUOTEElement::renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
return std::make_unique<TextComponent>(textNode->text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
return std::make_unique<TextComponent>(textNode->text, x, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}
return nullptr;
}

2
src/graphics/elements/BLOCKQUOTEElement.h

@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
class BLOCKQUOTEElement : public Element {
public:
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

2
src/graphics/elements/Element.h

@ -10,7 +10,7 @@ class Element { @@ -10,7 +10,7 @@ class Element {
public:
bool isInline = false;
virtual ~Element();
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

4
src/graphics/elements/H1Element.cpp

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#include "H1Element.h"
std::unique_ptr<Component> H1Element::renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight) {
std::unique_ptr<Component> H1Element::renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
return std::make_unique<TextComponent>(textNode->text, 0, y, 24, true, 0x000000FF, windowWidth, windowHeight);
return std::make_unique<TextComponent>(textNode->text, x, y, 24, true, 0x000000FF, windowWidth, windowHeight);
}
return nullptr;
}

2
src/graphics/elements/H1Element.h

@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
class H1Element : public Element {
public:
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

4
src/graphics/elements/H2Element.cpp

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#include "H2Element.h"
std::unique_ptr<Component> H2Element::renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight) {
std::unique_ptr<Component> H2Element::renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
return std::make_unique<TextComponent>(textNode->text, 0, y, 18, true, 0x000000FF, windowWidth, windowHeight);
return std::make_unique<TextComponent>(textNode->text, x, y, 18, true, 0x000000FF, windowWidth, windowHeight);
}
return nullptr;
}

2
src/graphics/elements/H2Element.h

@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
class H2Element : public Element {
public:
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

4
src/graphics/elements/H3Element.cpp

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#include "H3Element.h"
std::unique_ptr<Component> H3Element::renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight) {
std::unique_ptr<Component> H3Element::renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
return std::make_unique<TextComponent>(textNode->text, 0, y, 14, true, 0x000000FF, windowWidth, windowHeight); // Should be 14.04pt
return std::make_unique<TextComponent>(textNode->text, x, y, 14, true, 0x000000FF, windowWidth, windowHeight); // Should be 14.04pt
}
return nullptr;
}

2
src/graphics/elements/H3Element.h

@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
class H3Element : public Element {
public:
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

4
src/graphics/elements/LIElement.cpp

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#include "LIElement.h"
std::unique_ptr<Component> LIElement::renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight) {
std::unique_ptr<Component> LIElement::renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
return std::make_unique<TextComponent>("" + textNode->text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
return std::make_unique<TextComponent>("" + textNode->text, x, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}
return nullptr;
}

2
src/graphics/elements/LIElement.h

@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
class LIElement : public Element {
public:
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

4
src/graphics/elements/PElement.cpp

@ -2,10 +2,10 @@ @@ -2,10 +2,10 @@
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
std::unique_ptr<Component> PElement::renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight) {
std::unique_ptr<Component> PElement::renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
return std::make_unique<TextComponent>(textNode->text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
return std::make_unique<TextComponent>(textNode->text, x, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}
return nullptr;
}

2
src/graphics/elements/PElement.h

@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
class PElement : public Element {
public:
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

4
src/graphics/elements/SPANElement.cpp

@ -4,11 +4,11 @@ SPANElement::SPANElement() { @@ -4,11 +4,11 @@ SPANElement::SPANElement() {
isInline = true;
}
std::unique_ptr<Component> SPANElement::renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight) {
std::unique_ptr<Component> SPANElement::renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
if (node->parent->children.size() == 1) {
std::unique_ptr<Component> component = std::make_unique<TextComponent>(textNode->text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
std::unique_ptr<Component> component = std::make_unique<TextComponent>(textNode->text, x, y, 12, false, 0x000000FF, windowWidth, windowHeight);
return component;
}
}

2
src/graphics/elements/SPANElement.h

@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
class SPANElement : public Element {
public:
SPANElement();
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int y, const int windowWidth, const int windowHeight);
virtual std::unique_ptr<Component> renderer(const std::shared_ptr<Node> node, const int x, const int y, const int windowWidth, const int windowHeight);
};
#endif

34
src/graphics/opengl/Window.cpp

@ -59,7 +59,7 @@ bool Window::initGLFW() { @@ -59,7 +59,7 @@ bool Window::initGLFW() {
boxComponent->resize(width, height);
}
thiz->resizeComponentTree(thiz->rootComponent, width, height);
// thiz->printComponentTree(thiz->rootComponent, 0);
thiz->printComponentTree(thiz->rootComponent, 0);
});
glfwSetScrollCallback(window, [](GLFWwindow *win, double xOffset, double yOffset) {
Window *thiz = reinterpret_cast<Window*>(glfwGetWindowUserPointer(win));
@ -158,7 +158,7 @@ void Window::render() { @@ -158,7 +158,7 @@ void Window::render() {
createComponentTree(domRootNode, rootComponent);
const std::clock_t end = clock();
std::cout << "Parsed dom in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
// printComponentTree(rootComponent, 0);
// printComponentTree(rootComponent, 0);
domDirty = false;
}
@ -191,7 +191,13 @@ void Window::createComponentTree(const std::shared_ptr<Node> node, const std::sh @@ -191,7 +191,13 @@ void Window::createComponentTree(const std::shared_ptr<Node> node, const std::sh
parentComponent->children.push_back(component);
}
for (std::shared_ptr<Component> parent = component->parent; parent != nullptr; parent = parent->parent) {
parent->height += component->height;
parent->width += component->width;
if (!component->isInline) {
parent->height += component->height;
}
else {
parent->height += component->height - parent->height;
}
}
for (std::shared_ptr<Node> child : node->children) {
createComponentTree(child, component);
@ -204,10 +210,10 @@ void Window::printComponentTree(const std::shared_ptr<Component> &component, int @@ -204,10 +210,10 @@ void Window::printComponentTree(const std::shared_ptr<Component> &component, int
}
TextComponent *textComponent = dynamic_cast<TextComponent*>(component.get());
if (textComponent) {
std::cout << "Y: " << std::fixed << textComponent->y << " HEIGHT: " << textComponent->height << " TEXT: " << textComponent->text << std::endl;
std::cout << std::fixed << "X: " << textComponent->x << " Y: " << textComponent->y << " WIDTH: " << textComponent->width << " HEIGHT: " << textComponent->height << " TEXT: " << textComponent->text << std::endl;
}
else {
std::cout << "Y: " << std::fixed << component->y << " HEIGHT: " << component->height << std::endl;
std::cout << std::fixed << "X: " << component->x << " Y: " << component->y << " WIDTH: " << component->width << " HEIGHT: " << component->height << std::endl;
}
for (std::shared_ptr<Component> child : component->children) {
printComponentTree(child, depth + 1);
@ -230,15 +236,29 @@ void Window::renderComponents(std::shared_ptr<Component> component) { @@ -230,15 +236,29 @@ void Window::renderComponents(std::shared_ptr<Component> component) {
void Window::resizeComponentTree(const std::shared_ptr<Component> &component, const int width, const int height) {
TextComponent *textComponent = dynamic_cast<TextComponent*>(component.get());
if (textComponent) {
textComponent->resize(0, component->parent->y - component->parent->height, width, height);
if (!textComponent->isInline) {
textComponent->resize(component->parent->x, component->parent->y - component->parent->height, width, height);
}
else {
textComponent->resize(component->parent->x + component->parent->parent->width, component->parent->parent->y, width, height);
}
for (std::shared_ptr<Component> parent = component->parent; parent != nullptr; parent = parent->parent) {
parent->height += component->height;
parent->width += component->width;
if (!component->isInline) {
parent->height += component->height;
}
else {
parent->height += component->height - parent->height;
}
}
}
else {
if (component->parent) {
component->x = 0;
component->y = component->parent->y - component->parent->height;
}
component->width = 0;
component->height = 0;
}
for (std::shared_ptr<Component> child : component->children) {

10
src/graphics/text/TextRasterizer.cpp

@ -36,7 +36,7 @@ TextRasterizer::~TextRasterizer() { @@ -36,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, const int windowWidth, const int windowHeight, float &height, unsigned int &glyphCount) const {
std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text, const int x, const int y, const int windowWidth, const int windowHeight, float &width, 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));
@ -54,6 +54,8 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text @@ -54,6 +54,8 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text
std::unique_ptr<Glyph[]> glyphs = std::make_unique<Glyph[]>(glyphCount);
int maxX = 0;
int cx = x;
int cy = y;
@ -101,13 +103,19 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text @@ -101,13 +103,19 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text
glyphs[i].y1 -= std::ceil(1.2f * fontSize);
cx -= cx;
cy -= std::ceil(1.2f * fontSize);
maxX = windowWidth;
}
cx += xa;
cy += ya;
if (cx > maxX) {
maxX = cx;
}
}
cy -= std::ceil(1.2f * fontSize);
width = maxX - x;
height = y - cy;
return glyphs;

2
src/graphics/text/TextRasterizer.h

@ -26,7 +26,7 @@ private: @@ -26,7 +26,7 @@ private:
public:
TextRasterizer(const std::string &fontPath, const int size, const unsigned int resolution, const bool bold);
~TextRasterizer();
std::unique_ptr<const Glyph[]> rasterize(const std::string &text, const int x, const int y, const int windowWidth, const int windowHeight, float &height, unsigned int &glyphCount) const;
std::unique_ptr<const Glyph[]> rasterize(const std::string &text, const int x, const int y, const int windowWidth, const int windowHeight, float &width, float &height, unsigned int &glyphCount) const;
bool isUnicodeBMP(const FT_Face &face) const;
FT_Library lib;
hb_font_t *font;

Loading…
Cancel
Save