diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:50:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:50:49 +0000 |
commit | c853ffb5b2f75f5a889ed2e3ef89b818a736e87a (patch) | |
tree | 7d13a0883bb7936b84d6ecdd7bc332b41ed04bee /src/3rdparty/libcroco/docs/examples | |
parent | Initial commit. (diff) | |
download | inkscape-c853ffb5b2f75f5a889ed2e3ef89b818a736e87a.tar.xz inkscape-c853ffb5b2f75f5a889ed2e3ef89b818a736e87a.zip |
Adding upstream version 1.3+ds.upstream/1.3+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/3rdparty/libcroco/docs/examples')
7 files changed, 903 insertions, 0 deletions
diff --git a/src/3rdparty/libcroco/docs/examples/Makefile.am b/src/3rdparty/libcroco/docs/examples/Makefile.am new file mode 100644 index 0000000..e960594 --- /dev/null +++ b/src/3rdparty/libcroco/docs/examples/Makefile.am @@ -0,0 +1 @@ +EXTRA_DIST=cssom-example-1.c sac-example-1.c sac-example-2.c diff --git a/src/3rdparty/libcroco/docs/examples/cssom-example-1.c b/src/3rdparty/libcroco/docs/examples/cssom-example-1.c new file mode 100644 index 0000000..85079ce --- /dev/null +++ b/src/3rdparty/libcroco/docs/examples/cssom-example-1.c @@ -0,0 +1,109 @@ +/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset:8 -*- */ + +/** + *This is an example that shows how to use + *the CSSOM api of the libcroco CSS2 parsing library. + *It just parses the CSS document given in argument and + *it dumps it (serializes) it on the screen. + * + *To compile it using gcc, type + * + *gcc -g -Wall `croco-0.6-config --cflags` `croco-0.6-config --libs` -o cssom-example-1 cssom-example-1.c + * + *Prior to that, you must have compiled and installed libcroco, of course. + * + *@author Dodji Seketeli + */ + +#include <string.h> +#include <libcroco/libcroco.h> + +/** + *Displays the usage of this program. + *@param prg_name the name of the program. + */ +void +display_usage (char *prg_name) +{ + printf ("\nusage: %s [options] <css file to parse>\n\n", + prg_name) ; + printf ("\twhere options are:\n") ; + printf ("\t--help|h\tdisplays this help\n") ; + printf ("\n") ; +} + + +/** + *Main entry point. + *This function illustrates a way to use libcroco. + *In this example, we will use a CSS Object Model parser + *to parse a cascading style sheet and dump what we have parsed on stdout. + * + *The goal of the object model parser is to parse a css and build + *an abstract tree out of it. One can then walk the tree to perform + *whatever action he wants. In our case, the function used to + *dump the tree on stdout will walk the tree and dump each one of its + *components (a.k.a. css statements). + */ +int +main (int argc, char **argv) +{ + short i = 0 ; + enum CRStatus status = CR_OK ; + CRStyleSheet *stylesheet = NULL ; + + /*first parse command line arguments*/ + for (i = 1 ; i < argc ; i++) + { + if (argv[i][0] != '-') + break ; + if (!strcmp (argv[i],"--help") + || !strcmp (argv[i], "-h")) + { + display_usage (argv[0]) ; + return 0 ; + } + else + { + display_usage (argv[0]) ; + return 0 ; + } + } + if (i >= argc) + { + display_usage (argv[0]) ; + return 0; + } + + /***************************************************** + *Enough plumbering... now, the real libcroco stuffs. + ***************************************************/ + + /* + *What we want here is to simply parse + *a CSS document using the cssom api. + */ + status = cr_om_parser_simply_parse_file ((const guchar*)argv[i] /*sheet*/, + CR_ASCII /*the encoding*/, + &stylesheet) ; + if (status == CR_OK && stylesheet) + { + /* + *everything went well, + *so dump the stylesheet on stdout. + */ + cr_stylesheet_dump (stylesheet, stdout) ; + } + + if (stylesheet) + { + /* + *free the the memory used to hold the css + *object model + */ + cr_stylesheet_destroy (stylesheet) ; + stylesheet = NULL ; + } + + return 0 ; +} diff --git a/src/3rdparty/libcroco/docs/examples/sac-example-1.c b/src/3rdparty/libcroco/docs/examples/sac-example-1.c new file mode 100644 index 0000000..074fc07 --- /dev/null +++ b/src/3rdparty/libcroco/docs/examples/sac-example-1.c @@ -0,0 +1,198 @@ +/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset:8 -*- */ + +/** + *This test example shows how to use the SAC api. + * + *This features a simple parser that says "hey" when + *it encounters the beginning of a CSS ruleset, and "woohoo" + *at the end of a CSS ruleset. + * + *To compile this file, type: + * + *gcc -g -Wall -o sac-example-1 `croco-0.6-config --cflags` `croco-0.6-config --libs` sac-example-1.c + * + *Make sure you have compiled and installed libcroco prior to trying to + *compile this file :) + * + *Once you have compiled it, type: + * + *./sac-example-1 <a-path-to-a-css-file> + * + *to try it on a CSS file of your choice. + * + *Initial Author: Dodji Seketeli <Dodji 47 seketeli dot org> + */ + +#include <string.h> +#include <libcroco/libcroco.h> + +/** + *This is a callback function that will + *be called at the beginning of each css ruleset. + *@param a_handler a pointer to the current sac + *document handler + *@param a_selector a pointer to the selector. + *of the current ruleset. + */ +static void +start_selector_cb (CRDocHandler *a_handler, + CRSelector *a_selector) +{ + printf ("==========================================\n") ; + printf ("Hey, this is the beginning of a ruleset\n") ; +} + +/** + *This is a callback function that will be called at the end + *of the each css ruleset. + */ +static void +end_selector_cb (CRDocHandler *a_handler, + CRSelector *a_selector) +{ + printf ("Woohoo, this is the end of a ruleset\n") ; + printf ("======================================\n\n") ; +} + +/** + *Displays some information about how to use this program. + *@param a_prog_name the name of the current program. + */ +void +display_usage (unsigned char *a_prog_name) +{ + unsigned char *prog_name = a_prog_name ; + + if (!prog_name) + { + prog_name = (unsigned char*)"sac-example-1" ; + } + + printf ("usage: %s [--help] | <css file name>\n", prog_name) ; +} + +int +main (int argc, char **argv) +{ + unsigned short i = 0 ; + unsigned char * file_path = NULL ; + CRParser * parser = NULL ; + CRDocHandler *sac_handler = NULL ; + + if (argc <= 1) + { + display_usage ((unsigned char*)argv[0]) ; + return -1 ; + } + + /* + *Let's parse the + *command line arguments of this + *program in this loop. + */ + for (i=1 ; i < argc ;i++) + { + if (*argv[i] != '-') + break ; + + if (!strcmp (argv[i], "--help") + || !strcmp (argv[i], "-h")) + { + display_usage ((unsigned char*)argv[0]) ; + return -1; + } + else + { + /* + *no other option is + *available now, so this is + *a bit redundant... + */ + display_usage ((unsigned char*)argv[0]) ; + } + } + + if (i > argc) + { + /* + *no file name has been given + *in parameter, go out. + */ + return -1 ; + } + + /**************************************** + *Now, the real libcroco related stuffs... + ****************************************/ + + file_path = (unsigned char*)argv[i] ; + + /* + *Instantiate the libcroco parser. + */ + parser = cr_parser_new_from_file (file_path, + CR_ASCII) ; + if (!parser) + { + /* + *Damned, something bad happened ... + */ + return -1; + } + + /* + *Instantiates the SAC document handler. + */ + sac_handler = cr_doc_handler_new () ; + if (!sac_handler) + { + /* + *Argh, something bad happened here :-\ + *Let's release the resources we allocated + *and let's get out. + */ + + cr_parser_destroy (parser) ; + return -1; + } + + /****************** + *Sets some of the sac document handlers. + ****************/ + + /* + *This sac handler callback function will get called by the parser + *each time it encounters the beginning of a ruleset. + */ + sac_handler->start_selector = start_selector_cb ; + + /* + *This sac handler callback function will get called by the parser + *each time it encounters the end of a ruleset. + */ + sac_handler->end_selector = end_selector_cb ; + + /* + *Let's register our sac handler into the parser. + */ + cr_parser_set_sac_handler (parser, sac_handler) ; + + /* + *Now, let's do the parsing !!! + */ + cr_parser_parse (parser) ; + + /******************************************************* + *End of the parsing. A lot of sentences beginning with "Hey" + *may have been printed on the screen... + ***********************************************/ + + /* + *Time to free the resources we allocated. + */ + cr_parser_destroy (parser) ; + + cr_doc_handler_unref (sac_handler) ; + + return 0 ; +} diff --git a/src/3rdparty/libcroco/docs/examples/sac-example-2.c b/src/3rdparty/libcroco/docs/examples/sac-example-2.c new file mode 100644 index 0000000..63b002d --- /dev/null +++ b/src/3rdparty/libcroco/docs/examples/sac-example-2.c @@ -0,0 +1,349 @@ +/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset:8 -*- */ +/** + *This test example shows how to use the SAC api. + * + *This features a simple parser that prints the selector list + *and the properties list of each CSS ruleset found. + *It also prints the number of properties found in each CSS ruleset. + * + *At the end of the parsing, it a prints the number of rulesets found + *in the CSS document. + * + *To to this, several handler callbacks have been registered. + *We have also created a data structure that we call a parsing context. + *This parsing context stores the data necessary to achieve the calculation + *done during the parsing (count the number of rulesets and the number + *of properties per ruleset) + *The parsing context itself is stored in the "app_data" field of the sac handler. + *This field is there to provide applications with a + *place to store the custom data they want to be able to get/set during the parsing. + * + *To compile this file, type: + * + *gcc -g -Wall -o sac-example-2 `croco-0.6-config --cflags` `croco-0.6-config --libs` sac-example-2.c + * + *Make sure you have compiled and installed libcroco prior to trying to + *compile this file :) + * + *Once you have compiled it, type: + * + *./sac-example-2 <a-path-to-a-css-file> + * + *to try it on a CSS file of your choice. + * + *Initial Author: Dodji Seketeli <Dodji 47 seketeli dot org> + */ +#include <string.h> +#include <stdlib.h> +#include <libcroco/libcroco.h> + +/** + *A Context that will hold the + *variables necessary to count the number + *of rulesets and the number of properties + *per ruleset. + */ +struct MyFooContext +{ + /**the total number of rulesets in the stylesheet.*/ + int nb_rulesets ; + + /**the number of property per ruleset.*/ + int nb_props_per_ruleset ; +} ; + +/** + *This callback is called only once, at the beginning of + *the CSS Document. + *So here, we will allocate a custom parsing context + *where we will store data necessary for us to + *count the number of rulesets and properties found in + *the CSS document. + *@param a_handler the sac handler. + */ +static void +start_document_cb (CRDocHandler *a_handler) +{ + struct MyFooContext * parsing_context = NULL ; + + /* + *Allocate the parsing context. + *Our custom parsing context. + */ + parsing_context = (struct MyFooContext*) malloc + (sizeof (struct MyFooContext)) ; + if (!parsing_context) + { + /* + *the system ran out of memory. Advertise it and + *stop the program. + */ + fprintf (stderr, "program ran out of memory") ; + exit (-1) ; + } + + /* + *Initialize the newly allocated custom parsing context + *to zero. + */ + memset (parsing_context, 0, sizeof (struct MyFooContext)) ; + + /* + *Store the parsing context in the document handler. + *The app_data field CRDocHandler is especially there + *for that: give applications a place to store some data + *during the parsing. + */ + a_handler->app_data = parsing_context ; +} + +/** + *This callback function will + *be called at the beginning of each css ruleset. + *@param a_handler a pointer to the current sac + *document handler + *@param a_selector a pointer to the selector. + *of the current ruleset. + */ +static void +start_selector_cb (CRDocHandler *a_handler, + CRSelector *a_selector) +{ + struct MyFooContext *context = NULL ; + + context = (struct MyFooContext*) a_handler->app_data ; + if (!context) + return ; + context->nb_props_per_ruleset = 0 ; + + cr_selector_dump (a_selector, stdout) ; + printf (" {\n") ; + +} + +/** + *This callback function is called when + *the parser encounters a CSS property. + *@param a_handler the SAC handler. + *@param a_name string that contains the + *name of the property. + *@param a_value the value of the property. + */ +static void +property_cb (CRDocHandler *a_handler, + CRString *a_name, + CRTerm *a_value, + gboolean a_important) +{ + struct MyFooContext *context = NULL ; + + context = (struct MyFooContext *)a_handler->app_data ; + + if (!context || !a_name) + return ; + context->nb_props_per_ruleset ++ ; + + printf ("%s : ", cr_string_peek_raw_str (a_name)) ; + cr_term_dump (a_value, stdout) ; + printf ("\n") ; +} + + +/** + *This is a callback function that will be called at the end + *of the each css ruleset. + */ +static void +end_selector_cb (CRDocHandler *a_handler, + CRSelector *a_selector) +{ + struct MyFooContext *context = NULL ; + + context = (struct MyFooContext*)a_handler->app_data ; + if (!context) + return ; + context->nb_rulesets ++ ; + printf ("\n}\n") ; + printf ("**Number of properties in this ruleset: %d\n\n\n", + context->nb_props_per_ruleset) ; +} + +/** + *This callback is called only once at the end + *of the CSS document. + *@param a_handler the SAC handler. + */ +static void +end_document_cb (CRDocHandler *a_handler) +{ + struct MyFooContext *context = NULL ; + + context = (struct MyFooContext*) a_handler->app_data ; + if (!context) + return ; + + printf ("\nTotal number of rulesets: %d\n", + context->nb_rulesets) ; + + free (context) ; + a_handler->app_data = NULL ; +} + +/** + *Displays some information about how to use this program. + *@param a_prog_name the name of the current program. + */ +void +display_usage (unsigned char *a_prog_name) +{ + unsigned char *prog_name = a_prog_name ; + + if (!prog_name) + { + prog_name = (unsigned char*) "sac-example-1" ; + } + + printf ("usage: %s [--help] | <css file name>\n", prog_name) ; +} + +int +main (int argc, char **argv) +{ + unsigned short i = 0 ; + unsigned char * file_path = NULL ; + CRParser * parser = NULL ; + CRDocHandler *sac_handler = NULL ; + + if (argc <= 1) + { + display_usage ((unsigned char*)argv[0]) ; + return -1 ; + } + + /* + *Let's parse the + *command line arguments of this + *program in this loop. + */ + for (i=1 ; i < argc ;i++) + { + if (*argv[i] != '-') + break ; + + if (!strcmp (argv[i], "--help") + || !strcmp (argv[i], "-h")) + { + display_usage ((unsigned char*)argv[0]) ; + return -1; + } + else + { + /* + *no other option is + *available now, so this is + *a bit redundant... + */ + /*display_usage ((unsigned char*)argv[0]) ;*/ + } + } + + if (i > argc) + { + /* + *no file name has been given + *in parameter, go out. + */ + return -1; + } + + /**************************************** + *Now, the real libcroco related stuffs... + ****************************************/ + + file_path = (unsigned char*)argv[i] ; + + /* + *Instantiate the libcroco parser. + */ + parser = cr_parser_new_from_file (file_path, + CR_ASCII) ; + if (!parser) + { + /* + *Damned, something bad happened ... + */ + return -1 ; + } + + /* + *Instantiates the SAC document handler. + */ + sac_handler = cr_doc_handler_new () ; + if (!sac_handler) + { + /* + *Argh, something bad happened here :-\ + *Let's release the resources we allocated + *and let's get out. + */ + + cr_parser_destroy (parser) ; + return -1 ; + } + + /**************************************** + *Set some of the sac document handlers. + ****************************************/ + + /* + *This callback function will be called by the parser + *only once, at the beginning of the CSS Document. + */ + sac_handler->start_document = start_document_cb ; + + /* + *This callback function will be called by the parser + *only once, at the end of the CSS Document. + */ + sac_handler->end_document =end_document_cb ; + + /* + *This callback function will be called by the parser + *each time it encounters the beginning of a ruleset. + */ + sac_handler->start_selector = start_selector_cb ; + + /* + *This callback function will be called by the parser + *each time it encounters the beginning of a ruleset. + */ + sac_handler->property = property_cb ; + + /* + *This sac handler callback function will be called by the parser + *each time it encounters the end of a ruleset. + */ + sac_handler->end_selector = end_selector_cb ; + + /* + *Let's register our sac handler into the parser. + */ + cr_parser_set_sac_handler (parser, sac_handler) ; + + /* + *Now, let's do the parsing !!! + */ + cr_parser_parse (parser) ; + + /******************************************************* + *End of the parsing. A Couple of CSS rulesets must have + *been printed on the screen... + ***********************************************/ + + /* + *Time to free the resources we allocated. + */ + cr_parser_destroy (parser) ; + + return 0 ; +} diff --git a/src/3rdparty/libcroco/docs/examples/selection-example-1.c b/src/3rdparty/libcroco/docs/examples/selection-example-1.c new file mode 100644 index 0000000..3215a99 --- /dev/null +++ b/src/3rdparty/libcroco/docs/examples/selection-example-1.c @@ -0,0 +1,191 @@ +/** + * This example looks up a node from a document with an + * xpath expression, then reports all properties that apply + * from a given stylesheet. + * + * To compile it using gcc, type + * + * gcc -g -Wall `croco-0.6-config --cflags` `croco-0.6-config --libs` -o selection-example-1 selection-example-1.c + * + * Initial author: Stefan Seefeld. + */ + +#include <libcroco/libcroco.h> +#include <libcroco/cr-libxml-node-iface.h> +#include <libxml/tree.h> +#include <libxml/xpath.h> + +void +usage_and_exit (char *progname) +{ + fprintf (stderr, + "Usage: %s <xml doc> <stylesheet> <xpath>\n", + progname); + exit(-1); +} + +struct workspace +{ + xmlDoc *document; + xmlXPathContext *xpath; + xmlXPathObject *result; + CRStyleSheet *stylesheet; + CRCascade *cascade; + CRSelEng *selector; +}; + +/** + *construct workspace members in order... + * return 0 on success and -1 on error + */ +int +init (struct workspace *ws, char **args) +{ + enum CRStatus status = CR_OK; + + ws->document = 0; + ws->xpath = 0; + ws->result = 0; + ws->stylesheet = 0; + ws->cascade = 0; + ws->selector = 0; + + + ws->document = xmlParseFile(args[0]); + if (!ws->document) + { + fprintf(stderr, "could not parse the document %s", args[0]); + return -1; + } + ws->xpath = xmlXPathNewContext(ws->document); + if (!ws->xpath) + { + fprintf(stderr, "Error: unable to create new XPath context\n"); + return -1; + } + ws->result = xmlXPathEvalExpression((xmlChar *)args[2], ws->xpath); + if (!ws->result) + { + fprintf(stderr, "Error: unable to evaluate xpath expression\n"); + return -1; + } + if (ws->result->type != XPATH_NODESET || !ws->result->nodesetval) + { + fprintf(stderr, "Error: xpath does not evaluate to a node set\n"); + return -1; + } + + status = cr_om_parser_simply_parse_file((const guchar*)args[1] /*sheet*/, + CR_ASCII /*the encoding*/, + &ws->stylesheet); + if (status != CR_OK || !ws->stylesheet) + { + fprintf(stderr, "could not parse the stylesheet %s", args[1]); + return -1; + } + ws->cascade = cr_cascade_new(ws->stylesheet, 0, 0); + ws->selector = cr_sel_eng_new(&cr_libxml_node_iface); + return 1 ; +} + +/* ...and destruct in reverse order*/ +void +fini(struct workspace *ws) +{ + if (ws->selector) + { + cr_sel_eng_destroy(ws->selector); + ws->selector = NULL ; + } + if (ws->cascade) + { + cr_cascade_destroy(ws->cascade); + ws->cascade = NULL ; + } + + if (ws->result) + { + xmlXPathFreeObject(ws->result); + ws->result = NULL ; + } + if (ws->xpath) + { + xmlXPathFreeContext(ws->xpath); + ws->xpath = NULL ; + } + if (ws->document) + { + xmlFreeDoc(ws->document); + ws->document = NULL ; + } + xmlCleanupParser () ; +} + +void +print_properties_real (CRPropList *proplist) +{ + CRDeclaration *decl = NULL ; + CRPropList *cur_pair = NULL ; + + for (cur_pair = proplist ; cur_pair ; + cur_pair= cr_prop_list_get_next (cur_pair)) { + gchar *str = NULL ; + decl = NULL ; + + cr_prop_list_get_decl (cur_pair, &decl) ; + if (decl) { + str = cr_declaration_to_string (decl, 0) ; + if (str) { + printf ("%s\n", str) ; + g_free (str); + str = NULL ; + } + } + } +} + +void +print_properties (struct workspace *ws) +{ + enum CRStatus status; + CRPropList *prop_list = NULL; + xmlNode *node = ws->result->nodesetval->nodeTab[0]; + + status = cr_sel_eng_get_matched_properties_from_cascade + (ws->selector, ws->cascade, + node, &prop_list); + + if (status != CR_OK) + fprintf(stderr, "Error retrieving properties\n"); + else + { + xmlChar *prop = NULL ; + prop = xmlGetNodePath(node) ; + if (prop) { + printf("properties for node %s :\n", prop); + xmlFree (prop) ; + prop = NULL ; + } + print_properties_real (prop_list) ; + } + cr_prop_list_destroy (prop_list) ; +} + +int +main(int argc, char **argv) +{ + struct workspace ws; + if (argc != 4) usage_and_exit(argv[0]); + if (!init(&ws, argv + 1)) { + fini(&ws); + return -1 ; + } + + if (ws.result->nodesetval->nodeNr == 0) + printf("no matching nodes found\n"); + else + print_properties(&ws); + + fini(&ws); + return 0; +} diff --git a/src/3rdparty/libcroco/docs/examples/selection-example-1.css b/src/3rdparty/libcroco/docs/examples/selection-example-1.css new file mode 100644 index 0000000..044e053 --- /dev/null +++ b/src/3rdparty/libcroco/docs/examples/selection-example-1.css @@ -0,0 +1,41 @@ +report +{ + color : red; +} + +report > entry +{ + color : blue; +} + +note +{ + color : note-color; + frame : solid; +} + +note[id] +{ + color : note-id-color; + frame : solid; +} + +note[id=bar] +{ + margin : 5px; + color: note-id-bar-color ; +} + +note[id=foo] +{ + color : note-id-foo-color; +} + +entry[type=notice] note[id=bar] +{ + padding : 5px; +} + +note + notea { + color: notea-color ; +} diff --git a/src/3rdparty/libcroco/docs/examples/selection-example-1.xml b/src/3rdparty/libcroco/docs/examples/selection-example-1.xml new file mode 100644 index 0000000..4c3f558 --- /dev/null +++ b/src/3rdparty/libcroco/docs/examples/selection-example-1.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" ?> +<report> + <entry type="notice"> + <note id="foo"/> + <notea/> + <note id="bar"/> + </entry> + <entry> + <notea/> + <note/> + <note id="foo"/> + <note id="bar"/> + </entry> +</report> |