#define DEMO_TYPE_WINDOW demo_window_get_type ()
G_DECLARE_FINAL_TYPE (DemoWindow, demo_window, DEMO, WINDOW, AdwApplicationWindow)
struct _DemoWindow
{
AdwApplicationWindow parent_instance;
};
G_DEFINE_FINAL_TYPE (DemoWindow, demo_window, ADW_TYPE_APPLICATION_WINDOW)
enum {
PROP_0,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
static void
demo_window_finalize (GObject *object)
{
DemoWindow *self = (DemoWindow *)object;
G_OBJECT_CLASS (demo_window_parent_class)->finalize (object);
}
static void
demo_window_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
DemoWindow *self = DEMO_WINDOW (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
demo_window_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
DemoWindow *self = DEMO_WINDOW (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
demo_window_class_init (DemoWindowClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = demo_window_finalize;
object_class->get_property = demo_window_get_property;
object_class->set_property = demo_window_set_property;
}
static void
demo_window_init (DemoWindow *self)
{
TextFrame *frame;
TextDisplay *display;
const gchar *test;
GtkWidget *header_bar;
GtkWidget *vbox;
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
adw_application_window_set_content (ADW_APPLICATION_WINDOW (self), vbox);
// Example rich text document (uses html subset)
test = "There was an Old Man with a beard\nWho said, "It is just as I feared!
> Two Owls and a Hen,
> Four Larks and a Wren,
Have all built their nests in my beard!"
";
frame = format_parse_html (test);
header_bar = adw_header_bar_new ();
display = text_display_new (frame);
gtk_box_append (GTK_BOX (vbox), header_bar);
gtk_box_append (GTK_BOX (vbox), GTK_WIDGET (display));
}
static void
demo_activate (GApplication *app)
{
GtkWindow *window;
g_assert (G_IS_APPLICATION (app));
// Add CSS Stylesheet
GtkCssProvider *css_provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (css_provider, "/com/mattjakeman/TextEngine/Demo/style.css");
GdkDisplay *display = gdk_display_get_default ();
gtk_style_context_add_provider_for_display (display, GTK_STYLE_PROVIDER (css_provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
// Get the current window or create one if necessary.
window = gtk_application_get_active_window (GTK_APPLICATION (app));
if (window == NULL)
window = g_object_new (DEMO_TYPE_WINDOW,
"application", app,
"default-width", 500,
"default-height", 500,
NULL);
// Ask the window manager/compositor to present the window.
gtk_window_present (window);
}
int
main (int argc, char **argv)
{
AdwApplication *app;
int ret;
app = adw_application_new ("com.mattjakeman.TextEngine.Demo", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (demo_activate), NULL);
ret = g_application_run (G_APPLICATION (app), argc, argv);
g_clear_object (&app);
return ret;
}
text-engine-0.1.1/demo/meson.build 0000664 0000000 0000000 00000000521 14220124322 0017014 0 ustar 00root root 0000000 0000000 demo_sources = [
'demo.c'
]
demo_deps = [
text_engine_dep,
dependency('libadwaita-1')
]
gnome = import('gnome')
demo_sources += gnome.compile_resources('resources',
'resources.gresource.xml',
source_dir: '.',
c_name: 'resources'
)
executable('text-engine-demo', demo_sources,
dependencies: demo_deps,
install: true,
)
text-engine-0.1.1/demo/resources.gresource.xml 0000664 0000000 0000000 00000000244 14220124322 0021405 0 ustar 00root root 0000000 0000000
style.css
text-engine-0.1.1/demo/style.css 0000664 0000000 0000000 00000000000 14220124322 0016514 0 ustar 00root root 0000000 0000000 text-engine-0.1.1/meson.build 0000664 0000000 0000000 00000000742 14220124322 0016075 0 ustar 00root root 0000000 0000000 project('text-engine', 'c',
version: '0.1.0',
meson_version: '>= 0.50.0',
default_options: [ 'warning_level=2',
'c_std=gnu11',
],
)
config_h = configuration_data()
config_h.set_quoted('PACKAGE_VERSION', meson.project_version())
configure_file(
output: 'text-engine-config.h',
configuration: config_h,
)
add_project_arguments([
'-I' + meson.build_root(),
], language: 'c')
subdir('src')
subdir('demo')
subdir('test')
text-engine-0.1.1/src/ 0000775 0000000 0000000 00000000000 14220124322 0014517 5 ustar 00root root 0000000 0000000 text-engine-0.1.1/src/format/ 0000775 0000000 0000000 00000000000 14220124322 0016007 5 ustar 00root root 0000000 0000000 text-engine-0.1.1/src/format/import-html.c 0000664 0000000 0000000 00000006355 14220124322 0020440 0 ustar 00root root 0000000 0000000 /* import-html.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "import.h"
#include
#include
#include "../model/paragraph.h"
#include "../model/block.h"
#include "../model/run.h"
static void
build_text_frame_recursive (xmlNode *nodes,
TextFrame *frame,
TextParagraph **current)
{
g_return_if_fail (TEXT_IS_FRAME (frame));
g_return_if_fail (current);
xmlNode *cur_node = NULL;
if (nodes == NULL)
return;
for (cur_node = nodes; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_ELEMENT_NODE)
{
if (g_str_equal (cur_node->name, "p") ||
g_str_equal (cur_node->name, "br"))
{
*current = text_paragraph_new ();
text_frame_append_block (frame, TEXT_BLOCK (*current));
}
else
{
// Catch-all for not-yet implemented elements
g_print ("Ignored element %s\n", cur_node->name);
}
}
else if (cur_node->type == XML_TEXT_NODE)
{
// Append text as new run
const gchar *content = (gchar *)cur_node->content;
text_paragraph_append_run (*current, text_run_new (content));
}
build_text_frame_recursive (cur_node->children, frame, current);
}
}
/**
* build_text_frame:
* @nodes: Array of #xmlNode structures to parse
* @frame: An initialised #TextFrame to populate with data
*
* Recursively builds a new #TextFrame and populates it with data
* from the provided normalised HTML tree.
*/
static void
build_text_frame (xmlNode *nodes,
TextFrame *frame)
{
TextParagraph *current = NULL;
build_text_frame_recursive (nodes, frame, ¤t);
}
TextFrame *
format_parse_html (const gchar *html)
{
htmlDocPtr doc;
xmlNode *root;
TextFrame *frame;
g_print ("%s\n", html);
doc = htmlParseDoc ((const guchar *)html, "utf-8");
if (doc == NULL)
{
g_critical ("Could not parse HTML document.");
return NULL;
}
root = xmlDocGetRootElement (doc);
if (root == NULL)
{
g_warning ("Empty HTML document.");
xmlFreeDoc (doc);
xmlCleanupParser ();
return NULL;
}
frame = text_frame_new ();
g_print ("Root Node discovered: %s\n", root->name);
build_text_frame (root, frame);
xmlFreeDoc (doc);
xmlCleanupParser ();
return frame;
}
text-engine-0.1.1/src/format/import.h 0000664 0000000 0000000 00000001646 14220124322 0017501 0 ustar 00root root 0000000 0000000 /* import.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include "../model/frame.h"
G_BEGIN_DECLS
TextFrame *format_parse_html (const gchar *html);
G_END_DECLS
text-engine-0.1.1/src/format/meson.build 0000664 0000000 0000000 00000000232 14220124322 0020146 0 ustar 00root root 0000000 0000000 text_engine_sources += files([
'import-html.c',
])
format_headers = [
'import.h',
]
install_headers(format_headers, subdir : header_dir / 'format')
text-engine-0.1.1/src/layout/ 0000775 0000000 0000000 00000000000 14220124322 0016034 5 ustar 00root root 0000000 0000000 text-engine-0.1.1/src/layout/README.md 0000664 0000000 0000000 00000001362 14220124322 0017315 0 ustar 00root root 0000000 0000000 # Layout
This is the default layout engine. It is a layout policy used to transform
the semantic text model into a format that can be displayed. Custom layouts
can be implemented (not now, in the future) by subclassing `TextLayout`
and overriding select virtual methods.
The 'layout manager' takes in a 'semantic tree' and produces a 'layout tree',
which is passed on the toolkit-specific drawing widget. It is responsible for
caching the layout tree in between redraws and minimising the recomputation
needed.
The architecture of the layout tree is loosely inspired by Matt Brubeck's
[excellent series](https://limpet.net/mbrubeck/2014/09/08/toy-layout-engine-5-boxes.html)
on building a browser engine, itself a simplification of modern web engines.
text-engine-0.1.1/src/layout/layout-box.c 0000664 0000000 0000000 00000013744 14220124322 0020314 0 ustar 00root root 0000000 0000000 /* layout-box.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "layout-box.h"
#include "../model/paragraph.h"
#include "../tree/node.h"
typedef struct
{
TextItem *item;
PangoLayout *layout;
TextDimensions bbox;
} TextLayoutBoxPrivate;
G_DEFINE_FINAL_TYPE_WITH_PRIVATE (TextLayoutBox, text_layout_box, TEXT_TYPE_NODE)
enum {
PROP_0,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/**
* text_layout_box_new:
*
* Create a new #TextLayoutBox.
*
* Returns: (transfer full): a newly created #TextLayoutBox
*/
TextLayoutBox *
text_layout_box_new (void)
{
return g_object_new (TEXT_TYPE_LAYOUT_BOX, NULL);
}
static void
text_layout_box_finalize (GObject *object)
{
TextLayoutBox *self = (TextLayoutBox *)object;
TextLayoutBoxPrivate *priv = text_layout_box_get_instance_private (self);
// Dispose of children
for (TextNode *child = text_node_get_first_child (TEXT_NODE (self));
child != NULL;
child = text_node_get_next (TEXT_NODE (self)))
{
g_clear_object (&child);
}
G_OBJECT_CLASS (text_layout_box_parent_class)->finalize (object);
}
static void
text_layout_box_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextLayoutBox *self = TEXT_LAYOUT_BOX (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_layout_box_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextLayoutBox *self = TEXT_LAYOUT_BOX (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
void
text_layout_box_layout (TextLayoutBox *self,
PangoContext *context,
int width)
{
g_return_if_fail (TEXT_IS_LAYOUT_BOX (self));
TextLayoutBoxPrivate *priv = text_layout_box_get_instance_private (self);
int height = 0;
g_debug ("Starting for %s\n", g_type_name_from_instance (self));
g_debug ("Has item: %d\n", priv->item != NULL);
g_debug ("Has paragraph: %d\n", TEXT_IS_PARAGRAPH (priv->item));
if (priv->item && TEXT_IS_PARAGRAPH (priv->item))
{
GString *str = g_string_new ("");
for (TextNode *run = text_node_get_first_child (TEXT_NODE (priv->item));
run != NULL;
run = text_node_get_next (run))
{
const gchar *run_text;
g_object_get (run, "text", &run_text, NULL);
g_string_append (str, run_text);
g_debug (" - Counting run\n");
}
gchar *text = g_string_free (str, FALSE);
g_debug (" - String %s\n", text);
if (!priv->layout)
priv->layout = pango_layout_new (context);
pango_layout_set_text (priv->layout, text, -1);
pango_layout_set_wrap (priv->layout, PANGO_WRAP_WORD_CHAR);
pango_layout_set_width (priv->layout, PANGO_SCALE * width);
pango_layout_get_pixel_size (priv->layout, NULL, &height);
g_debug (" - Height %d\n", height);
g_free (text);
}
// Account for children (should we force elements to choose between
// children and text? seems like a sensible simplification)
for (TextNode *node = text_node_get_first_child (TEXT_NODE (self));
node != NULL;
node = text_node_get_next (node))
{
TextLayoutBox *child_box = TEXT_LAYOUT_BOX (node);
g_debug (" - Found child\n");
// We can assume bbox already exists by now, as the layout() method
// has been called already in the layout manager.
TextLayoutBoxPrivate *priv = text_layout_box_get_instance_private (child_box);
height += priv->bbox.height;
g_debug (" - Child height %d\n", height);
}
priv->bbox.x = 0;
priv->bbox.y = 0;
priv->bbox.width = width;
priv->bbox.height = height;
}
void
text_layout_box_set_item (TextLayoutBox *self,
TextItem *item)
{
TextLayoutBoxPrivate *priv = text_layout_box_get_instance_private (self);
priv->item = item;
g_debug ("Set item to non null: %d\n", priv->item != NULL);
}
TextItem *
text_layout_box_get_item (TextLayoutBox *self,
TextItem *item)
{
TextLayoutBoxPrivate *priv = text_layout_box_get_instance_private (self);
return priv->item;
}
const TextDimensions *
text_layout_box_get_bbox (TextLayoutBox *self)
{
TextLayoutBoxPrivate *priv = text_layout_box_get_instance_private (self);
return &priv->bbox;
}
PangoLayout *
text_layout_box_get_pango_layout (TextLayoutBox *self)
{
TextLayoutBoxPrivate *priv = text_layout_box_get_instance_private (self);
return priv->layout;
}
static void
text_layout_box_class_init (TextLayoutBoxClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_layout_box_finalize;
object_class->get_property = text_layout_box_get_property;
object_class->set_property = text_layout_box_set_property;
}
static void
text_layout_box_init (TextLayoutBox *self)
{
}
text-engine-0.1.1/src/layout/layout-box.h 0000664 0000000 0000000 00000003264 14220124322 0020315 0 ustar 00root root 0000000 0000000 /* layout-box.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include
#include "../tree/node.h"
#include "../model/item.h"
#include "types.h"
G_BEGIN_DECLS
#define TEXT_TYPE_LAYOUT_BOX (text_layout_box_get_type())
G_DECLARE_DERIVABLE_TYPE (TextLayoutBox, text_layout_box, TEXT, LAYOUT_BOX, TextNode)
struct _TextLayoutBoxClass
{
TextNodeClass parent_class;
};
TextLayoutBox *text_layout_box_new (void);
void
text_layout_box_set_item (TextLayoutBox *self,
TextItem *item);
TextItem *
text_layout_box_get_item (TextLayoutBox *self,
TextItem *item);
void
text_layout_box_layout (TextLayoutBox *self,
PangoContext *context,
int width);
const TextDimensions *
text_layout_box_get_bbox (TextLayoutBox *self);
PangoLayout *
text_layout_box_get_pango_layout (TextLayoutBox *self);
G_END_DECLS
text-engine-0.1.1/src/layout/layout.c 0000664 0000000 0000000 00000010440 14220124322 0017514 0 ustar 00root root 0000000 0000000 /* layout.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "layout.h"
struct _TextLayout
{
GObject parent_instance;
};
G_DEFINE_FINAL_TYPE (TextLayout, text_layout, G_TYPE_OBJECT)
enum {
PROP_0,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
TextLayout *
text_layout_new (void)
{
return g_object_new (TEXT_TYPE_LAYOUT, NULL);
}
static void
text_layout_finalize (GObject *object)
{
TextLayout *self = (TextLayout *)object;
G_OBJECT_CLASS (text_layout_parent_class)->finalize (object);
}
static void
text_layout_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextLayout *self = TEXT_LAYOUT (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_layout_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextLayout *self = TEXT_LAYOUT (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
do_layout_recursive (TextLayout *self,
TextLayoutBox *parent,
PangoContext *context,
TextItem *item,
int width)
{
g_return_if_fail (TEXT_IS_LAYOUT (self));
g_return_if_fail (TEXT_IS_LAYOUT_BOX (parent));
g_return_if_fail (TEXT_IS_ITEM (item));
// Append children (in future, recursively)
for (TextNode *node = text_node_get_first_child (TEXT_NODE (item));
node != NULL;
node = text_node_get_next (node))
{
g_assert (TEXT_IS_ITEM (node));
g_debug ("Counting child %s\n", g_type_name_from_instance (node));
// Let's treat paragraphs opaquely for now. In the future, we need
// to manually consider each text run in order for inline equations
// and images.
if (TEXT_IS_PARAGRAPH (node))
{
TextLayoutBox *box = text_layout_box_new ();
text_layout_box_set_item (box, TEXT_ITEM (node));
text_node_append_child (TEXT_NODE (parent), TEXT_NODE (box));
g_debug ("Added child %s\n", g_type_name_from_instance (node));
// TODO: This function should be properly recursive in the future,
// so avoid calling it here. Below should be the only time it is
// called (i.e. post-order traversal).
text_layout_box_layout (box, context, width);
}
}
text_layout_box_layout (parent, context, width);
g_debug ("Layout for %s\n", g_type_name_from_instance (parent));
}
TextLayoutBox *
text_layout_build_layout_tree (TextLayout *self,
PangoContext *context,
TextFrame *frame,
int width)
{
g_return_if_fail (TEXT_IS_LAYOUT (self));
g_return_if_fail (TEXT_IS_FRAME (frame));
TextLayoutBox *root = text_layout_box_new ();
do_layout_recursive (self, root, context, TEXT_ITEM (frame), width);
return root;
}
static void
text_layout_class_init (TextLayoutClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_layout_finalize;
object_class->get_property = text_layout_get_property;
object_class->set_property = text_layout_set_property;
}
static void
text_layout_init (TextLayout *self)
{
}
text-engine-0.1.1/src/layout/layout.h 0000664 0000000 0000000 00000002544 14220124322 0017527 0 ustar 00root root 0000000 0000000 /* layout.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include
#include
#include "layout-box.h"
#include "../model/frame.h"
#include "../model/paragraph.h"
G_BEGIN_DECLS
#define TEXT_TYPE_LAYOUT (text_layout_get_type())
G_DECLARE_FINAL_TYPE (TextLayout, text_layout, TEXT, LAYOUT, GObject)
TextLayout *text_layout_new (void);
TextLayoutBox *
text_layout_build_layout_tree (TextLayout *self,
PangoContext *context,
TextFrame *frame,
int width);
G_END_DECLS
text-engine-0.1.1/src/layout/meson.build 0000664 0000000 0000000 00000000304 14220124322 0020173 0 ustar 00root root 0000000 0000000 text_engine_sources += files([
'layout.c',
'layout-box.c'
])
layout_headers = [
'layout.h',
'layout-box.h',
'types.h'
]
install_headers(layout_headers, subdir : header_dir / 'layout')
text-engine-0.1.1/src/layout/types.h 0000664 0000000 0000000 00000002010 14220124322 0017342 0 ustar 00root root 0000000 0000000 /* types.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
/* TODO: Make these GBoxed for introspection support! */
#include
typedef struct
{
gdouble x;
gdouble y;
gdouble width;
gdouble height;
// TODO: Also consider padding/margin/border?
} TextDimensions;
text-engine-0.1.1/src/meson.build 0000664 0000000 0000000 00000003250 14220124322 0016661 0 ustar 00root root 0000000 0000000 api_version = '0.1'
header_dir = 'text-engine'
text_engine_sources = [
'text-engine.c',
]
text_engine_headers = [
'text-engine.h',
]
# Utility
subdir('format')
# Trees
subdir('tree')
subdir('model')
subdir('layout')
subdir('ui')
version_split = meson.project_version().split('.')
MAJOR_VERSION = version_split[0]
MINOR_VERSION = version_split[1]
MICRO_VERSION = version_split[2]
version_conf = configuration_data()
version_conf.set('VERSION', meson.project_version())
version_conf.set('MAJOR_VERSION', MAJOR_VERSION)
version_conf.set('MINOR_VERSION', MINOR_VERSION)
version_conf.set('MICRO_VERSION', MICRO_VERSION)
configure_file(
input: 'text-engine-version.h.in',
output: 'text-engine-version.h',
configuration: version_conf,
install: true,
install_dir: join_paths(get_option('includedir'), header_dir)
)
text_engine_deps = [
dependency('gio-2.0', version: '>= 2.50'),
dependency('json-glib-1.0'),
dependency('libxml-2.0'),
dependency('gtk4')
]
text_engine_lib = shared_library('text-engine-' + api_version,
text_engine_sources,
dependencies: text_engine_deps,
install: true,
)
install_headers(text_engine_headers, subdir: header_dir)
text_engine_dep = declare_dependency(
link_with: text_engine_lib,
include_directories: include_directories('.'),
dependencies: text_engine_deps,
# sources: resources
)
pkg = import('pkgconfig')
pkg.generate(
description: 'A shared library for ...',
libraries: text_engine_lib,
name: 'text-engine',
filebase: 'text-engine-' + api_version,
version: meson.project_version(),
subdirs: 'text-engine',
requires: 'glib-2.0',
install_dir: join_paths(get_option('libdir'), 'pkgconfig')
)
text-engine-0.1.1/src/model/ 0000775 0000000 0000000 00000000000 14220124322 0015617 5 ustar 00root root 0000000 0000000 text-engine-0.1.1/src/model/README.md 0000664 0000000 0000000 00000000216 14220124322 0017075 0 ustar 00root root 0000000 0000000 # Model
This is the text engine document model. It is a platform agnostic representation
of a rich text document using a tree-like structure.
text-engine-0.1.1/src/model/block.c 0000664 0000000 0000000 00000004405 14220124322 0017060 0 ustar 00root root 0000000 0000000 /* block.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "block.h"
typedef struct
{
int _padding;
} TextBlockPrivate;
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (TextBlock, text_block, TEXT_TYPE_ITEM)
enum {
PROP_0,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
static void
text_block_finalize (GObject *object)
{
TextBlock *self = (TextBlock *)object;
TextBlockPrivate *priv = text_block_get_instance_private (self);
G_OBJECT_CLASS (text_block_parent_class)->finalize (object);
}
static void
text_block_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextBlock *self = TEXT_BLOCK (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_block_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextBlock *self = TEXT_BLOCK (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_block_class_init (TextBlockClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_block_finalize;
object_class->get_property = text_block_get_property;
object_class->set_property = text_block_set_property;
}
static void
text_block_init (TextBlock *self)
{
}
text-engine-0.1.1/src/model/block.h 0000664 0000000 0000000 00000002046 14220124322 0017064 0 ustar 00root root 0000000 0000000 /* block.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include "item.h"
G_BEGIN_DECLS
#define TEXT_TYPE_BLOCK (text_block_get_type())
G_DECLARE_DERIVABLE_TYPE (TextBlock, text_block, TEXT, BLOCK, TextItem)
struct _TextBlockClass
{
TextNodeClass parent_class;
};
G_END_DECLS
text-engine-0.1.1/src/model/frame.c 0000664 0000000 0000000 00000005731 14220124322 0017063 0 ustar 00root root 0000000 0000000 /* frame.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "frame.h"
typedef struct
{
int _padding;
} TextFramePrivate;
G_DEFINE_FINAL_TYPE_WITH_PRIVATE (TextFrame, text_frame, TEXT_TYPE_BLOCK)
enum {
PROP_0,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/**
* text_frame_new:
*
* Create a new #TextFrame.
*
* Returns: (transfer full): a newly created #TextFrame
*/
TextFrame *
text_frame_new (void)
{
return g_object_new (TEXT_TYPE_FRAME, NULL);
}
static void
text_frame_finalize (GObject *object)
{
TextFrame *self = (TextFrame *)object;
TextFramePrivate *priv = text_frame_get_instance_private (self);
G_OBJECT_CLASS (text_frame_parent_class)->finalize (object);
}
static void
text_frame_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextFrame *self = TEXT_FRAME (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_frame_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextFrame *self = TEXT_FRAME (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
void
text_frame_append_block (TextFrame *self,
TextBlock *block)
{
g_return_if_fail (TEXT_IS_FRAME (self));
g_return_if_fail (TEXT_IS_BLOCK (block));
text_node_append_child (TEXT_NODE (self), TEXT_NODE (block));
}
void
text_frame_prepend_block (TextFrame *self,
TextBlock *block)
{
g_return_if_fail (TEXT_IS_FRAME (self));
g_return_if_fail (TEXT_IS_BLOCK (block));
text_node_prepend_child (TEXT_NODE (self), TEXT_NODE (block));
}
static void
text_frame_class_init (TextFrameClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_frame_finalize;
object_class->get_property = text_frame_get_property;
object_class->set_property = text_frame_set_property;
}
static void
text_frame_init (TextFrame *self)
{
}
text-engine-0.1.1/src/model/frame.h 0000664 0000000 0000000 00000002372 14220124322 0017066 0 ustar 00root root 0000000 0000000 /* frame.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include "item.h"
#include "block.h"
G_BEGIN_DECLS
#define TEXT_TYPE_FRAME (text_frame_get_type())
G_DECLARE_DERIVABLE_TYPE (TextFrame, text_frame, TEXT, FRAME, TextBlock)
struct _TextFrameClass
{
TextBlockClass parent_class;
};
TextFrame *text_frame_new (void);
void text_frame_append_block (TextFrame *self, TextBlock *block);
void text_frame_prepend_block (TextFrame *self, TextBlock *block);
G_END_DECLS
text-engine-0.1.1/src/model/item.c 0000664 0000000 0000000 00000004452 14220124322 0016726 0 ustar 00root root 0000000 0000000 /* item.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "item.h"
G_DEFINE_TYPE (TextItem, text_item, TEXT_TYPE_NODE)
enum {
PROP_0,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/**
* text_item_new:
*
* Create a new #TextItem.
*
* Returns: (transfer full): a newly created #TextItem
*/
TextItem *
text_item_new (void)
{
return g_object_new (TEXT_TYPE_ITEM, NULL);
}
static void
text_item_finalize (GObject *object)
{
TextItem *self = (TextItem *)object;
G_OBJECT_CLASS (text_item_parent_class)->finalize (object);
}
static void
text_item_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextItem *self = TEXT_ITEM (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_item_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextItem *self = TEXT_ITEM (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_item_class_init (TextItemClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_item_finalize;
object_class->get_property = text_item_get_property;
object_class->set_property = text_item_set_property;
}
static void
text_item_init (TextItem *self)
{
}
text-engine-0.1.1/src/model/item.h 0000664 0000000 0000000 00000002107 14220124322 0016726 0 ustar 00root root 0000000 0000000 /* item.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include "../tree/node.h"
G_BEGIN_DECLS
#define TEXT_TYPE_ITEM (text_item_get_type())
G_DECLARE_DERIVABLE_TYPE (TextItem, text_item, TEXT, ITEM, TextNode)
struct _TextItemClass
{
TextNodeClass parent_class;
};
TextItem *text_item_new (void);
G_END_DECLS
text-engine-0.1.1/src/model/meson.build 0000664 0000000 0000000 00000000370 14220124322 0017761 0 ustar 00root root 0000000 0000000 text_engine_sources += files([
'item.c',
'run.c',
'block.c',
'frame.c',
'paragraph.c'
])
model_headers = [
'item.h',
'run.h',
'block.h',
'frame.h',
'paragraph.h'
]
install_headers(model_headers, subdir : header_dir / 'model')
text-engine-0.1.1/src/model/paragraph.c 0000664 0000000 0000000 00000005225 14220124322 0017734 0 ustar 00root root 0000000 0000000 /* paragraph.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "paragraph.h"
struct _TextParagraph
{
TextBlock parent_instance;
};
G_DEFINE_FINAL_TYPE (TextParagraph, text_paragraph, TEXT_TYPE_BLOCK)
enum {
PROP_0,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
TextParagraph *
text_paragraph_new (void)
{
return g_object_new (TEXT_TYPE_PARAGRAPH, NULL);
}
static void
text_paragraph_finalize (GObject *object)
{
TextParagraph *self = (TextParagraph *)object;
G_OBJECT_CLASS (text_paragraph_parent_class)->finalize (object);
}
static void
text_paragraph_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextParagraph *self = TEXT_PARAGRAPH (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_paragraph_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextParagraph *self = TEXT_PARAGRAPH (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
void
text_paragraph_append_run (TextParagraph *self,
TextRun *run)
{
g_return_if_fail (TEXT_IS_PARAGRAPH (self));
g_return_if_fail (TEXT_IS_RUN (run));
text_node_append_child (TEXT_NODE (self), TEXT_NODE (run));
}
static void
text_paragraph_class_init (TextParagraphClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_paragraph_finalize;
object_class->get_property = text_paragraph_get_property;
object_class->set_property = text_paragraph_set_property;
}
static void
text_paragraph_init (TextParagraph *self)
{
}
text-engine-0.1.1/src/model/paragraph.h 0000664 0000000 0000000 00000002242 14220124322 0017735 0 ustar 00root root 0000000 0000000 /* paragraph.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include "item.h"
#include "block.h"
#include "run.h"
G_BEGIN_DECLS
#define TEXT_TYPE_PARAGRAPH (text_paragraph_get_type())
G_DECLARE_FINAL_TYPE (TextParagraph, text_paragraph, TEXT, PARAGRAPH, TextBlock)
TextParagraph *text_paragraph_new (void);
void text_paragraph_append_run (TextParagraph *para, TextRun *run);
G_END_DECLS
text-engine-0.1.1/src/model/run.c 0000664 0000000 0000000 00000005535 14220124322 0016577 0 ustar 00root root 0000000 0000000 /* run.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "run.h"
struct _TextRun
{
TextItem parent_instance;
gchar *text;
};
G_DEFINE_FINAL_TYPE (TextRun, text_run, TEXT_TYPE_ITEM)
enum {
PROP_0,
PROP_TEXT,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
TextRun *
text_run_new (const gchar *text)
{
return g_object_new (TEXT_TYPE_RUN,
"text", text,
NULL);
}
static void
text_run_finalize (GObject *object)
{
TextRun *self = (TextRun *)object;
G_OBJECT_CLASS (text_run_parent_class)->finalize (object);
}
static void
text_run_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextRun *self = TEXT_RUN (object);
switch (prop_id)
{
case PROP_TEXT:
g_value_set_string (value, self->text);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_run_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextRun *self = TEXT_RUN (object);
switch (prop_id)
{
case PROP_TEXT:
if (self->text)
g_free (self->text);
self->text = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_run_class_init (TextRunClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_run_finalize;
object_class->get_property = text_run_get_property;
object_class->set_property = text_run_set_property;
properties [PROP_TEXT]
= g_param_spec_string ("text",
"Text",
"Text",
NULL,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT);
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
text_run_init (TextRun *self)
{
}
text-engine-0.1.1/src/model/run.h 0000664 0000000 0000000 00000002005 14220124322 0016571 0 ustar 00root root 0000000 0000000 /* run.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include "item.h"
G_BEGIN_DECLS
#define TEXT_TYPE_RUN (text_run_get_type())
G_DECLARE_FINAL_TYPE (TextRun, text_run, TEXT, RUN, TextItem)
TextRun *text_run_new (const gchar *text);
G_END_DECLS
text-engine-0.1.1/src/text-engine-version.h.in 0000664 0000000 0000000 00000005563 14220124322 0021220 0 ustar 00root root 0000000 0000000 /* text-engine-version.h.in
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#if !defined(TEXT_ENGINE_INSIDE) && !defined(TEXT_ENGINE_COMPILATION)
# error "Only can be included directly."
#endif
/**
* SECTION:text_engineversion
* @short_description: text-engine version checking
*
* text-engine provides macros to check the version of the library
* at compile-time
*/
/**
* TEXT_ENGINE_MAJOR_VERSION:
*
* text-engine major version component (e.g. 1 if %TEXT_ENGINE_VERSION is 1.2.3)
*/
#define TEXT_ENGINE_MAJOR_VERSION (@MAJOR_VERSION@)
/**
* TEXT_ENGINE_MINOR_VERSION:
*
* text-engine minor version component (e.g. 2 if %TEXT_ENGINE_VERSION is 1.2.3)
*/
#define TEXT_ENGINE_MINOR_VERSION (@MINOR_VERSION@)
/**
* TEXT_ENGINE_MICRO_VERSION:
*
* text-engine micro version component (e.g. 3 if %TEXT_ENGINE_VERSION is 1.2.3)
*/
#define TEXT_ENGINE_MICRO_VERSION (@MICRO_VERSION@)
/**
* TEXT_ENGINE_VERSION
*
* text-engine version.
*/
#define TEXT_ENGINE_VERSION (@VERSION@)
/**
* TEXT_ENGINE_VERSION_S:
*
* text-engine version, encoded as a string, useful for printing and
* concatenation.
*/
#define TEXT_ENGINE_VERSION_S "@VERSION@"
#define TEXT_ENGINE_ENCODE_VERSION(major,minor,micro) \
((major) << 24 | (minor) << 16 | (micro) << 8)
/**
* TEXT_ENGINE_VERSION_HEX:
*
* text-engine version, encoded as an hexadecimal number, useful for
* integer comparisons.
*/
#define TEXT_ENGINE_VERSION_HEX \
(TEXT_ENGINE_ENCODE_VERSION (TEXT_ENGINE_MAJOR_VERSION, TEXT_ENGINE_MINOR_VERSION, TEXT_ENGINE_MICRO_VERSION))
/**
* TEXT_ENGINE_CHECK_VERSION:
* @major: required major version
* @minor: required minor version
* @micro: required micro version
*
* Compile-time version checking. Evaluates to %TRUE if the version
* of text-engine is greater than the required one.
*/
#define TEXT_ENGINE_CHECK_VERSION(major,minor,micro) \
(TEXT_ENGINE_MAJOR_VERSION > (major) || \
(TEXT_ENGINE_MAJOR_VERSION == (major) && TEXT_ENGINE_MINOR_VERSION > (minor)) || \
(TEXT_ENGINE_MAJOR_VERSION == (major) && TEXT_ENGINE_MINOR_VERSION == (minor) && \
TEXT_ENGINE_MICRO_VERSION >= (micro)))
text-engine-0.1.1/src/text-engine.c 0000664 0000000 0000000 00000001471 14220124322 0017115 0 ustar 00root root 0000000 0000000 /* text-engine.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "text-engine.h"
text-engine-0.1.1/src/text-engine.h 0000664 0000000 0000000 00000001663 14220124322 0017125 0 ustar 00root root 0000000 0000000 /* text-engine.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
G_BEGIN_DECLS
#define TEXT_ENGINE_INSIDE
# include "text-engine-version.h"
#undef TEXT_ENGINE_INSIDE
G_END_DECLS
text-engine-0.1.1/src/tree/ 0000775 0000000 0000000 00000000000 14220124322 0015456 5 ustar 00root root 0000000 0000000 text-engine-0.1.1/src/tree/README.md 0000664 0000000 0000000 00000000223 14220124322 0016732 0 ustar 00root root 0000000 0000000 # Tree
A flexible tree/graph implementation. Used by the model
and layout stages to create and update intermediary
trees in the rendering process.
text-engine-0.1.1/src/tree/meson.build 0000664 0000000 0000000 00000000213 14220124322 0017614 0 ustar 00root root 0000000 0000000 text_engine_sources += files([
'node.c',
])
tree_headers = [
'node.h',
]
install_headers(tree_headers, subdir : header_dir / 'tree')
text-engine-0.1.1/src/tree/node.c 0000664 0000000 0000000 00000016027 14220124322 0016555 0 ustar 00root root 0000000 0000000 /* node.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "node.h"
typedef struct
{
TextNode *parent;
TextNode *prev;
TextNode *next;
TextNode *first_child;
TextNode *last_child;
int n_children;
} TextNodePrivate;
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (TextNode, text_node, G_TYPE_OBJECT)
enum {
PROP_0,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/**
* text_node_new:
*
* Create a new #TextNode.
*
* Returns: (transfer full): a newly created #TextNode
*/
TextNode *
text_node_new (void)
{
return g_object_new (TEXT_TYPE_NODE, NULL);
}
static void
text_node_finalize (GObject *object)
{
TextNode *self = (TextNode *)object;
TextNodePrivate *priv = text_node_get_instance_private (self);
G_OBJECT_CLASS (text_node_parent_class)->finalize (object);
}
static void
text_node_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextNode *self = TEXT_NODE (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_node_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextNode *self = TEXT_NODE (object);
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
TextNode *
text_node_get_first_child (TextNode *self)
{
TextNodePrivate *priv = text_node_get_instance_private (self);
return priv->first_child;
}
TextNode *
text_node_get_last_child (TextNode *self)
{
TextNodePrivate *priv = text_node_get_instance_private (self);
return priv->last_child;
}
TextNode *
text_node_get_previous (TextNode *self)
{
TextNodePrivate *priv = text_node_get_instance_private (self);
return priv->prev;
}
TextNode *
text_node_get_next (TextNode *self)
{
TextNodePrivate *priv = text_node_get_instance_private (self);
return priv->next;
}
TextNode *
text_node_get_parent (TextNode *self)
{
TextNodePrivate *priv = text_node_get_instance_private (self);
return priv->parent;
}
static void
_insert_between (TextNode *node,
TextNode *before,
TextNode *after)
{
TextNodePrivate *node_priv, *before_priv, *after_priv;
g_assert (node != NULL);
g_assert (before != NULL);
g_assert (after != NULL);
node_priv = text_node_get_instance_private (node);
before_priv = text_node_get_instance_private (before);
after_priv = text_node_get_instance_private (after);
node_priv->prev = before;
before_priv->next = node;
node_priv->next = after;
after_priv->prev = node;
}
static int
_get_index_of (TextNode *self,
TextNode *child)
{
TextNode *iter;
int index;
index = 0;
for (iter = text_node_get_first_child (self);
iter != NULL;
iter = text_node_get_next (self))
{
if (iter == child)
return index;
index++;
}
return -1;
}
void
text_node_insert_child (TextNode *self,
TextNode *child,
int index)
{
int cmp_index;
TextNode *before, *after, *iter;
TextNodePrivate *priv, *child_priv, *before_priv, *after_priv;
priv = text_node_get_instance_private (self);
child_priv = text_node_get_instance_private (child);
g_assert (index >= 0 && index <= priv->n_children);
g_object_ref_sink (child);
// No children
if (priv->n_children == 0)
{
priv->first_child = child;
priv->last_child = child;
priv->n_children = 1;
return;
}
// Prepend
if (index == 0)
{
after = priv->first_child;
after_priv = text_node_get_instance_private (after);
after_priv->prev = child;
child_priv->next = after;
priv->first_child = child;
priv->n_children++;
return;
}
// Append
if (index == priv->n_children)
{
before = priv->last_child;
before_priv = text_node_get_instance_private (before);
before_priv->next = child;
child_priv->prev = before;
priv->last_child = child;
priv->n_children++;
return;
}
// Insert (After)
cmp_index = 0;
iter = text_node_get_first_child (self);
while (cmp_index++ < index) {
iter = text_node_get_next (iter);
g_assert (iter != NULL);
}
_insert_between (child, iter, text_node_get_next (iter));
}
void
text_node_prepend_child (TextNode *self,
TextNode *child)
{
text_node_insert_child (self, child, 0);
}
void
text_node_append_child (TextNode *self,
TextNode *child)
{
TextNodePrivate *priv = text_node_get_instance_private (self);
text_node_insert_child (self, child, priv->n_children);
}
void
text_node_insert_child_before (TextNode *self,
TextNode *child,
TextNode *compare)
{
int index;
index = _get_index_of (self, compare);
if (index == -1)
{
g_critical ("Provided compare node is not a child of this text node.");
return;
}
if ((index - 1) == 0)
{
g_object_ref_sink (child);
text_node_prepend_child (self, child);
return;
}
text_node_insert_child (self, child, index-1);
}
void
text_node_insert_child_after (TextNode *self,
TextNode *child,
TextNode *compare)
{
int index;
index = _get_index_of (self, compare);
if (index == -1)
{
g_critical ("Provided compare node is not a child of this text node.");
return;
}
text_node_insert_child (self, child, index);
}
static void
text_node_class_init (TextNodeClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_node_finalize;
object_class->get_property = text_node_get_property;
object_class->set_property = text_node_set_property;
}
static void
text_node_init (TextNode *self)
{
TextNodePrivate *priv = text_node_get_instance_private (self);
priv->first_child = NULL;
priv->last_child = NULL;
priv->n_children = 0;
}
text-engine-0.1.1/src/tree/node.h 0000664 0000000 0000000 00000003353 14220124322 0016560 0 ustar 00root root 0000000 0000000 /* node.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
G_BEGIN_DECLS
#define TEXT_TYPE_NODE (text_node_get_type())
G_DECLARE_DERIVABLE_TYPE (TextNode, text_node, TEXT, NODE, GObject)
struct _TextNodeClass
{
GObjectClass parent_class;
};
// Implementors Only
TextNode *text_node_get_parent (TextNode *self);
TextNode *text_node_get_next (TextNode *self);
TextNode *text_node_get_previous (TextNode *self);
TextNode *text_node_get_first_child (TextNode *self);
TextNode *text_node_get_last_child (TextNode *self);
void text_node_insert_child (TextNode *self, TextNode *child, int index);
void text_node_prepend_child (TextNode *self, TextNode *child);
void text_node_append_child (TextNode *self, TextNode *child);
void text_node_insert_child_before (TextNode *self, TextNode *child, TextNode *compare);
void text_node_insert_child_after (TextNode *self, TextNode *child, TextNode *compare);
G_END_DECLS
text-engine-0.1.1/src/ui/ 0000775 0000000 0000000 00000000000 14220124322 0015134 5 ustar 00root root 0000000 0000000 text-engine-0.1.1/src/ui/display.c 0000664 0000000 0000000 00000016636 14220124322 0016761 0 ustar 00root root 0000000 0000000 /* display.c
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "display.h"
#include "../model/paragraph.h"
#include "../layout/layout.h"
struct _TextDisplay
{
GtkWidget parent_instance;
TextFrame *frame;
TextLayout *layout;
TextLayoutBox *layout_tree;
};
G_DEFINE_FINAL_TYPE (TextDisplay, text_display, GTK_TYPE_WIDGET)
enum {
PROP_0,
PROP_FRAME,
N_PROPS
};
static GParamSpec *properties [N_PROPS];
/**
* text_display_new:
* @frame: The #TextFrame to display or %NULL
*
* Creates a new #TextDisplay widget which displays the rich text
* document stored inside @frame.
*
* Returns: A new #TextDisplay widget
*/
TextDisplay *
text_display_new (TextFrame *frame)
{
return g_object_new (TEXT_TYPE_DISPLAY,
"frame", frame,
NULL);
}
static void
text_display_finalize (GObject *object)
{
TextDisplay *self = (TextDisplay *)object;
G_OBJECT_CLASS (text_display_parent_class)->finalize (object);
}
static void
text_display_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TextDisplay *self = TEXT_DISPLAY (object);
switch (prop_id)
{
case PROP_FRAME:
g_value_set_object (value, self->frame);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
text_display_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
TextDisplay *self = TEXT_DISPLAY (object);
switch (prop_id)
{
case PROP_FRAME:
g_clear_object (&self->layout_tree);
self->frame = g_value_get_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
layout_snapshot_recursive (GtkWidget *widget,
TextLayoutBox *layout_box,
GtkSnapshot *snapshot,
GdkRGBA *fg_color,
int *delta_height)
{
int offset = 0;
for (TextNode *node = text_node_get_first_child (TEXT_NODE (layout_box));
node != NULL;
node = text_node_get_next (node))
{
g_assert (TEXT_IS_LAYOUT_BOX (node));
int delta_height;
layout_snapshot_recursive (widget, node, snapshot, fg_color, &delta_height);
offset += delta_height;
}
PangoLayout *layout = text_layout_box_get_pango_layout (layout_box);
const TextDimensions *bbox = text_layout_box_get_bbox (layout_box);
if (layout)
{
gtk_snapshot_save (snapshot);
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (0, offset));
gtk_snapshot_append_layout (snapshot, layout, fg_color);
gtk_snapshot_restore (snapshot);
offset = bbox->height;
}
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (0, bbox->height));
*delta_height = bbox->height;
}
static void
text_display_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
{
g_return_if_fail (TEXT_IS_DISPLAY (widget));
TextDisplay *self = TEXT_DISPLAY (widget);
if (!self->frame)
return;
if (!self->layout_tree)
return;
// TODO: Don't recreate this each time - do in size allocate instead?
g_clear_object (&self->layout_tree);
self->layout_tree = text_layout_build_layout_tree (self->layout,
gtk_widget_get_pango_context (GTK_WIDGET (self)),
self->frame,
gtk_widget_get_width (GTK_WIDGET (self)));
GdkRGBA fg_color;
gtk_style_context_get_color (gtk_widget_get_style_context (widget), &fg_color);
// Display the layout tree
int delta_height;
layout_snapshot_recursive (widget, self->layout_tree, snapshot, &fg_color, &delta_height);
}
static GtkSizeRequestMode
text_display_get_request_mode (GtkWidget* widget)
{
return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
}
static void
text_display_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline)
{
if (orientation == GTK_ORIENTATION_VERTICAL)
{
TextDisplay *self = TEXT_DISPLAY (widget);
PangoContext *context = gtk_widget_get_pango_context (widget);
g_clear_object (&self->layout_tree);
self->layout_tree = text_layout_build_layout_tree (self->layout,
context,
self->frame,
for_size);
*minimum = *natural = text_layout_box_get_bbox (self->layout_tree)->height;
g_debug ("Height: %d\n", *minimum);
}
else if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
GTK_WIDGET_CLASS (text_display_parent_class)->measure (widget,
orientation,
for_size,
minimum,
natural,
minimum_baseline,
natural_baseline);
}
}
static void
text_display_class_init (TextDisplayClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = text_display_finalize;
object_class->get_property = text_display_get_property;
object_class->set_property = text_display_set_property;
properties [PROP_FRAME]
= g_param_spec_object ("frame",
"Frame",
"Frame",
TEXT_TYPE_FRAME,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT);
g_object_class_install_properties (object_class, N_PROPS, properties);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
widget_class->snapshot = text_display_snapshot;
widget_class->get_request_mode = text_display_get_request_mode;
widget_class->measure = text_display_measure;
}
static void
text_display_init (TextDisplay *self)
{
self->layout = text_layout_new ();
}
text-engine-0.1.1/src/ui/display.h 0000664 0000000 0000000 00000002053 14220124322 0016752 0 ustar 00root root 0000000 0000000 /* display.h
*
* Copyright 2022 Matthew Jakeman
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see .
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#pragma once
#include
#include "../model/frame.h"
G_BEGIN_DECLS
#define TEXT_TYPE_DISPLAY (text_display_get_type())
G_DECLARE_FINAL_TYPE (TextDisplay, text_display, TEXT, DISPLAY, GtkWidget)
TextDisplay *text_display_new (TextFrame *frame);
G_END_DECLS
text-engine-0.1.1/src/ui/meson.build 0000664 0000000 0000000 00000000213 14220124322 0017272 0 ustar 00root root 0000000 0000000 text_engine_sources += files([
'display.c',
])
ui_headers = [
'display.h',
]
install_headers(ui_headers, subdir : header_dir / 'ui')
text-engine-0.1.1/test/ 0000775 0000000 0000000 00000000000 14220124322 0014707 5 ustar 00root root 0000000 0000000 text-engine-0.1.1/test/delta.c 0000664 0000000 0000000 00000002151 14220124322 0016143 0 ustar 00root root 0000000 0000000 #include
#include
typedef struct {
int a;
} DeltaFixture;
static void
delta_fixture_set_up (DeltaFixture *fixture,
gconstpointer user_data)
{
fixture->a = 4;
}
static void
delta_fixture_tear_down (DeltaFixture *fixture,
gconstpointer user_data)
{
fixture->a = 0;
}
static void
test_delta_test1 (DeltaFixture *fixture,
gconstpointer user_data)
{
g_assert_true (fixture->a == 4);
}
static void
test_delta_test2 (DeltaFixture *fixture,
gconstpointer user_data)
{
g_assert_true (fixture->a != 5);
}
int
main (int argc, char *argv[])
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
// Define the tests.
g_test_add ("/text-engine/delta/test1", DeltaFixture, "some-user-data",
delta_fixture_set_up, test_delta_test1,
delta_fixture_tear_down);
g_test_add ("/text-engine/delta/test2", DeltaFixture, "some-user-data",
delta_fixture_set_up, test_delta_test2,
delta_fixture_tear_down);
return g_test_run ();
}
text-engine-0.1.1/test/meson.build 0000664 0000000 0000000 00000000433 14220124322 0017051 0 ustar 00root root 0000000 0000000 deps = [
dependency('glib-2.0'),
text_engine_dep
]
test(
'delta',
executable('delta', 'delta.c', dependencies: deps),
env: [
'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()),
'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()),
],
protocol: 'tap',
)