Michael R Sweet
Copyright © 2018-2020 by Michael R Sweet
Free memory associated with a stylesheet.
void hcCSSDelete(hc_css_t *css);
css | Stylesheet |
---|
Import CSS definitions from a URL, file, or string.
int hcCSSImport(hc_css_t *css, hc_file_t *file);
css | Stylesheet |
---|---|
file | File |
1 on success, 0 on error
Import the default HTML stylesheet.
int hcCSSImportDefault(hc_css_t *css);
css | Stylesheet |
---|
1 on success, 0 on error
Allocate a new stylesheet.
hc_css_t *hcCSSNew(hc_pool_t *pool);
pool | Memory pool |
---|
Stylesheet
Set the base media settings.
int hcCSSSetMedia(hc_css_t *css, const char *type, int color_bits, int monochrome_bits, float width, float height);
css | Stylesheet |
---|---|
type | Media type ("print', etc.) |
color_bits | Bits of color supported |
monochrome_bits | Bits of grayscale supported |
width | Device width |
height | Device height |
1 on success, 0 on failure
Make a copy of a dictionary.
hc_dict_t *hcDictCopy(const hc_dict_t *dict);
dict | Dictionary to copy |
---|
New dictionary
Delete a dictionary.
void hcDictDelete(hc_dict_t *dict);
dict | Dictionary |
---|
Return the number of key/value pairs in a dictionary.
size_t hcDictGetCount(const hc_dict_t *dict);
dict | Dictionary |
---|
Number of key/value pairs
Return the key and value for the specified pair.
const char *hcDictGetIndexKeyValue(const hc_dict_t *dict, size_t idx, const char **key);
dict | Dictionary |
---|---|
idx | Index (0-based) |
key | Key or NULL if idx is invalid. |
Value or NULL
if idx
is invalid.
Get the value for a key in a dictionary.
const char *hcDictGetKeyValue(const hc_dict_t *dict, const char *key);
dict | Dictionary |
---|---|
key | Key string |
Value or NULL
if not found.
Create a new dictionary.
hc_dict_t *hcDictNew(hc_pool_t *pool);
pool | Memory pool |
---|
New dictionary
Remove a key/value pair from a dictionary.
void hcDictRemoveKey(hc_dict_t *dict, const char *key);
dict | Dictionary |
---|---|
key | Key string |
Set a key/value pair in a dictionary.
void hcDictSetKeyValue(hc_dict_t *dict, const char *key, const char *value);
dict | Dictionary |
---|---|
key | Key string |
value | Value string |
Close a file and free any memory associated with it.
void hcFileDelete(hc_file_t *file);
file | File |
---|
Get a character from a file.
int hcFileGetc(hc_file_t *file);
file | File |
---|
Character or EOF
Create a new file buffer stream.
hc_file_t *hcFileNewBuffer(hc_pool_t *pool, const void *buffer, size_t bytes);
pool | Memory pool |
---|---|
buffer | Buffer |
bytes | Size of buffer |
File
Create a new file string stream.
hc_file_t *hcFileNewString(hc_pool_t *pool, const char *s);
pool | Memory pool |
---|---|
s | String |
File
Create a new file URL stream.
hc_file_t *hcFileNewURL(hc_pool_t *pool, const char *url, const char *baseurl);
pool | Memory pool |
---|---|
url | URL or filename |
baseurl | Base URL or NULL |
File
Read bytes from a file.
size_t hcFileRead(hc_file_t *file, void *buffer, size_t bytes);
file | File |
---|---|
buffer | Buffer |
bytes | Number of bytes to read |
Number of bytes read
Randomly access data within a file.
size_t hcFileSeek(hc_file_t *file, size_t offset);
file | File |
---|---|
offset | Offset within file |
New file offset or 0 on error
Return a character to a file.
void hcFileUngetc(hc_file_t *f, int ch);
f | File |
---|---|
ch | Character |
Add a font to a memory pool cache.
void hcFontAddCached(hc_pool_t *pool, hc_font_t *font, const char *url);
pool | Memory pool for cache |
---|---|
font | Font to add |
url | URL for font |
Compute the extents of a string when rendered using the given font object, size, style, etc.
int hcFontComputeExtents(const hc_font_t *font, float size, const char *s, hc_rect_t *extents);
font | Font object |
---|---|
size | Font size |
s | String |
extents | Extents of the string |
1 on success, 0 on failure
Free all memory used for a font family object.
void hcFontDelete(hc_font_t *font);
font | Font object |
---|
Find a font...
hc_font_t *hcFontFindCached(hc_pool_t *pool, const char *family, hc_font_stretch_t stretch, hc_font_style_t style, hc_font_variant_t variant, hc_font_weight_t weight);
pool | Memory pool for cache |
---|---|
family | Family name |
stretch | Stretching of font |
style | Style of font |
variant | Variant of font |
weight | Weight of font |
Matching font or NULL
Get a cached font from a pool.
hc_font_t *hcFontGetCached(hc_pool_t *pool, size_t idx);
pool | Memory pool |
---|---|
idx | Font number (0-based) |
Font
Return the number of cached fonts.
size_t hcFontGetCachedCount(hc_pool_t *pool);
pool | Memory pool |
---|
Number of cached fonts
Get the copyright text for a font.
const char *hcFontGetCopyright(hc_font_t *font);
font | Font object |
---|
Copyright text
Get the family name of a font.
const char *hcFontGetFamily(hc_font_t *font);
font | Font object |
---|
Family name
size_t hcFontGetNumFonts(hc_font_t *font);
font | Font object |
---|
Get the number of fonts in this collection.
Get the PostScript name of a font.
const char *hcFontGetPostScriptName(hc_font_t *font);
font | Font object |
---|
PostScript name
Get the font style.
hc_font_style_t hcFontGetStyle(hc_font_t *font);
font | Font object |
---|
Style
Get the version number of a font.
const char *hcFontGetVersion(hc_font_t *font);
font | Font object |
---|
Version number
Get the weight of a font.
hc_font_weight_t hcFontGetWeight(hc_font_t *font);
font | Font object |
---|
Weight
Create a new font object for the named font family.
hc_font_t *hcFontNew(hc_pool_t *pool, hc_file_t *file, size_t idx);
pool | Memory pool |
---|---|
file | File |
idx | Font number in collection (0-based) |
New font object
Free the memory used by a HTML document.
void hcHTMLDelete(hc_html_t *html);
html | HTML document |
---|
Find the first node in a document that matches the given element and/or ID string.
hc_node_t *hcHTMLFindNode(hc_html_t *html, hc_node_t *current, hc_element_t element, const char *id);
html | HTML document |
---|---|
current | Current node or NULL |
element | Element or HC_ELEMENT_WILDCARD for any |
id | ID string or NULL for any |
First matching node or NULL
Get the stylesheet for a HTML document.
hc_css_t *hcHTMLGetCSS(hc_html_t *html);
html | HTML document |
---|
Stylesheet
Get a HTML document's DOCTYPE value, if any.
const char *hcHTMLGetDOCTYPE(hc_html_t *html);
html | HTML document |
---|
DOCTYPE value
Get the root node for a document.
hc_node_t *hcHTMLGetRootNode(hc_html_t *html);
html | HTML document |
---|
Root node or NULL
if none
Load a HTML file into a document.
int hcHTMLImport(hc_html_t *html, hc_file_t *file);
html | HTML document |
---|---|
file | File to import |
1 on success, 0 on failure
Create a new HTML document.
hc_html_t *hcHTMLNew(hc_pool_t *pool, hc_css_t *css);
pool | Memory pool |
---|---|
css | Base stylesheet |
HTML document
Create a new root node.
hc_node_t *hcHTMLNewRootNode(hc_html_t *html, const char *doctype);
html | HTML document |
---|---|
doctype | DOCTYPE value |
New root node
Delete an image object.
void hcImageDelete(hc_image_t *image);
image | Image object |
---|
Get the MIME media type for the image.
const char *hcImageGetFormat(hc_image_t *image);
image | Image object |
---|
MIME media type
Get the height of an image.
int hcImageGetHeight(hc_image_t *image);
image | Image object |
---|
Height in pixels
Get the natural size of an image.
hc_size_t hcImageGetSize(hc_image_t *image);
image | Image object |
---|
CSS dimensions
Get the width of an image.
int hcImageGetWidth(hc_image_t *image);
image | Image object |
---|
Width in pixels
Create a new image object.
hc_image_t *hcImageNew(hc_pool_t *pool, hc_file_t *file);
pool | Memory pool |
---|---|
file | File |
Image object
Get the number of attributes for an element.
size_t hcNodeAttrGetCount(hc_node_t *node);
node | Element node |
---|
Number of attributes
Get the name and value of a specified attribute.
const char *hcNodeAttrGetIndexNameValue(hc_node_t *node, size_t idx, const char **name);
node | Element node |
---|---|
idx | Attribute index (0-based) |
name | Attribute name |
Attribute value or NULL
Get the value of an element attribute.
const char *hcNodeAttrGetNameValue(hc_node_t *node, const char *name);
node | Element node |
---|---|
name | Attribute name |
Value or NULL
if not present
Delete an element attribute.
void hcNodeAttrRemove(hc_node_t *node, const char *name);
node | Element node |
---|---|
name | Attribute name |
Add an element attribute.
void hcNodeAttrSetNameValue(hc_node_t *node, const char *name, const char *value);
node | Element node |
---|---|
name | Attribute name |
value | Attribute value |
Compute the box properties for the given HTML node.
int hcNodeComputeCSSBox(hc_node_t *node, hc_compute_t compute, hc_box_t *box);
node | HTML node |
---|---|
compute | Pseudo-class, if any |
box | Box properties |
1 on success, 0 on failure
Compute the content: value for the given HTML node.
char *hcNodeComputeCSSContent(hc_node_t *node, hc_compute_t compute);
node | HTML node |
---|---|
compute | Pseudo-class, if any |
Content string or NULL
if none/error
The returned string must be freed using free()
.
Compute the display property for the given HTML node.
hc_display_t hcNodeComputeCSSDisplay(hc_node_t *node, hc_compute_t compute);
node | HTML node |
---|---|
compute | Pseudo-class, if any |
1 on success, 0 on failure
Compute the media properties for the given HTML node.
int hcNodeComputeCSSMedia(hc_node_t *node, hc_compute_t compute, hc_media_t *media);
node | HTML node |
---|---|
compute | Pseudo-class, if any |
media | Media properties |
1 on success, 0 on failure
Compute the properties for the given node.
const hc_dict_t *hcNodeComputeCSSProperties(hc_node_t *node, hc_compute_t compute);
node | HTML node |
---|---|
compute | Pseudo-class, if any |
Properties or NULL
on error
The dictionary is stored in the stylesheet cache and must not be
deleted using the hcDictDelete
function.
Compute the table properties for the given HTML node.
int hcNodeComputeCSSTable(hc_node_t *node, hc_compute_t compute, hc_table_t *table);
node | HTML node |
---|---|
compute | Pseudo-class, if any |
table | Table properties |
1 on success, 0 on failure
Compute the text properties for the given HTML node.
int hcNodeComputeCSSText(hc_node_t *node, hc_compute_t compute, hc_text_t *text);
node | HTML node |
---|---|
compute | Pseudo-class, if any |
text | Text properties |
1 on success, 0 on failure
Delete a HTML node from a document.
void hcNodeDelete(hc_html_t *html, hc_node_t *node);
html | HTML document |
---|---|
node | HTML node |
Get a HTML node's comment value, if any.
const char *hcNodeGetComment(hc_node_t *node);
node | HTML node |
---|
Comment value
Get a HTML node's element/type.
hc_element_t hcNodeGetElement(hc_node_t *node);
node | HTML node |
---|
Node element/type
Get a HTML node's first child node, if any.
hc_node_t *hcNodeGetFirstChildNode(hc_node_t *node);
node | HTML node |
---|
First child node or NULL
if none
Get a HTML node's last child node, if any.
hc_node_t *hcNodeGetLastChildNode(hc_node_t *node);
node | HTML node |
---|
Last child node or NULL
if none
Get a HTML node's next sibling node, if any.
hc_node_t *hcNodeGetNextSiblingNode(hc_node_t *node);
node | HTML node |
---|
Next sibling node or NULL
if none
Get a HTML node's parent node, if any.
hc_node_t *hcNodeGetParentNode(hc_node_t *node);
node | HTML node |
---|
Parent node or NULL
if none
Get a HTML node's previous sibling node, if any.
hc_node_t *hcNodeGetPrevSiblingNode(hc_node_t *node);
node | HTML node |
---|
Previous sibling node or NULL
if none
Get a HTML node's string value, if any.
const char *hcNodeGetString(hc_node_t *node);
node | HTML node |
---|
String value
Create a new HTML comment node.
hc_node_t *hcNodeNewComment(hc_node_t *parent, const char *c);
parent | Parent node |
---|---|
c | Comment value |
New HTML comment node
Create a new HTML element node.
hc_node_t *hcNodeNewElement(hc_node_t *parent, hc_element_t element);
parent | Parent node |
---|---|
element | HTML element |
New HTML element node
Create a new HTML string node.
hc_node_t *hcNodeNewString(hc_node_t *parent, const char *s);
parent | Parent node |
---|---|
s | String value |
New HTML string node
Free the memory used by a pool.
void hcPoolDelete(hc_pool_t *pool);
pool | Memory pool |
---|
Return the last error message recorded.
const char *hcPoolGetLastError(hc_pool_t *pool);
pool | Memory pool |
---|
Last error message or NULL
Find or copy a string.
const char *hcPoolGetString(hc_pool_t *pool, const char *s);
pool | Memory pool |
---|---|
s | String to find/copy |
New string pointer
This function finds or makes a copy of the passed string that will be freed when the corresponding memory pool is deleted. Since the memory pool only maintains a single copy of any string, copied strings are immutable.
Get a file corresponding to a URL.
const char *hcPoolGetURL(hc_pool_t *pool, const char *url, const char *baseurl);
pool | Memory pool |
---|---|
url | URL |
baseurl | Base URL, if any |
Filename or NULL
on error
Create a new memory pool.
hc_pool_t *hcPoolNew(void);
New memory pool
Set the error reporting callback.
void hcPoolSetErrorCallback(hc_pool_t *pool, hc_error_cb_t cb, void *ctx);
pool | Memory pool |
---|---|
cb | Error callback or NULL for the default |
ctx | Context pointer for callback |
The default error callback writes the message to stderr
.
The error callback returns 1 to continue processing or 0 to stop immediately.
Set the URL callback.
void hcPoolSetURLCallback(hc_pool_t *pool, hc_url_cb_t cb, void *ctx);
pool | Memory pool |
---|---|
cb | URL callback or NULL for the default |
ctx | Context pointer for callback |
The default URL callback supports local files (only).
The URL callback returns a local pathname (copied to the specified buffer)
or NULL
if the URL cannot be loaded/found.
Finalize the SHA3-256 hash, putting the result in "hash".
void hcSHA3Final(hc_sha3_t *ctx, unsigned char *hash, size_t hashlen);
ctx | Hash context |
---|---|
hash | Hash buffer |
hashlen | Bytes to copy from hash |
Initialize a SHA3-256 hashing context.
void hcSHA3Init(hc_sha3_t *ctx);
ctx | Hash context |
---|
Update the SHA3-256 hashing context with the given data.
void hcSHA3Update(hc_sha3_t *ctx, const void *data, size_t datalen);
ctx | Hash context |
---|---|
data | Data to hash |
datalen | Number of bytes of data |
Types...
typedef enum hc_background_attachment_t;
CSS border properties
typedef struct hc_border_props_s hc_border_props_t;
CSS border-xxx-radius properties
typedef struct hc_border_radius_s hc_border_radius_t;
All CSS border properties
typedef struct hc_border_s hc_border_t;
Box shadow values
typedef struct hc_box_shadow_s hc_box_shadow_t;
CSS box properties
typedef struct hc_box_s hc_box_t;
sRGBA color
typedef struct hc_color_s hc_color_t;
What to compute
typedef enum hc_compute_t;
CSS data
typedef struct _hc_css_s hc_css_t;
Key/value string dictionary
typedef struct _hc_dict_s hc_dict_t;
HTML document
typedef struct _hc_html_s hc_html_t;
Image object
typedef struct _hc_image_s hc_image_t;
HTML element enum
typedef typedef struct _hc_node_s hc_node_t;
High-level file/stream
typedef struct _hc_file_s hc_file_t;
Font stretch
typedef enum hc_font_stretch_t;
Font style
typedef enum hc_font_style_t;
Font variant
typedef enum hc_font_variant_t;
Font weight
typedef typedef struct _hc_font_s hc_font_t;
CSS media properties
typedef struct hc_media_s hc_media_t;
Point/coordinate
typedef struct hc_point_s hc_point_t;
Memory allocation pool
typedef struct _hc_pool_s hc_pool_t;
Rectangle
typedef struct hc_rect_s hc_rect_t;
SHA3 hashing context
typedef struct hc_sha3_s hc_sha3_t;
Point/coordinate
typedef struct hc_size_s hc_size_t;
CSS table properties
typedef struct hc_table_s hc_table_t;
CSS text properties
typedef struct hc_text_s hc_text_t;
Functions...
typedef char *(*hc_url_cb_t)(void *ctx, const char *url, char *buffer, size_t bufsize);
CSS border properties
struct hc_border_props_s {
hc_color_t color;
hc_border_style_t style;
float width;
};
color | Border color |
---|---|
style | Border style |
width | Border width |
CSS border-xxx-radius properties
struct hc_border_radius_s {
hc_size_t bottom_left;
hc_size_t bottom_right;
hc_size_t top_left;
hc_size_t top_right;
};
bottom_left | Bottom-left border radius |
---|---|
bottom_right | Bottom-right border radius |
top_left | Top-left border radius |
top_right | Top-right border radius |
All CSS border properties
struct hc_border_s {
hc_border_props_t bottom;
hc_border_props_t left;
hc_border_props_t right;
hc_border_props_t top;
};
bottom |
---|
left |
right |
top |
CSS box properties
struct hc_box_s {
hc_background_attachment_t background_attachment;
hc_background_box_t background_clip;
hc_color_t background_color;
const char *background_image;
hc_background_box_t background_origin;
hc_point_t background_position;
hc_background_repeat_t background_repeat;
hc_size_t background_size;
hc_border_t border;
const char *border_image;
hc_bool_t border_image_fill;
hc_rect_t border_image_outset;
hc_border_image_repeat_t border_image_repeat[2];
hc_rect_t border_image_slice;
hc_rect_t border_image_width;
hc_border_radius_t border_radius;
hc_size_t border_spacing;
hc_rect_t bounds;
hc_box_shadow_t box_shadow;
hc_break_t break_after;
hc_break_t break_before;
hc_break_t break_inside;
hc_rect_t clip;
hc_float_t float_value;
const char *list_style_image;
hc_list_style_position_t list_style_position;
hc_list_style_type_t list_style_type;
hc_rect_t margin;
hc_size_t max_size;
hc_size_t min_size;
int orphans;
hc_overflow_t overflow;
hc_rect_t padding;
hc_size_t size;
int widows;
int z_index;
};
background_attachment | |
---|---|
background_clip | |
background_color | |
background_image | |
background_origin | |
background_position | |
background_repeat | |
background_size | |
border | |
border_image | |
border_image_fill | |
border_image_outset | |
border_image_repeat[2] | |
border_image_slice | |
border_image_width | |
border_radius | |
border_spacing | |
bounds | Computed bounds |
box_shadow | |
break_after | |
break_before | |
break_inside | |
clip | Clip bounds |
float_value | |
list_style_image | |
list_style_position | |
list_style_type | |
margin | |
max_size | |
min_size | |
orphans | |
overflow | |
padding | |
size | Computed size |
widows | |
z_index |
Box shadow values
struct hc_box_shadow_s {
float blur_radius;
hc_color_t color;
float horizontal_offset;
hc_bool_t inset;
float spread_distance;
float vertical_offset;
};
blur_radius |
---|
color |
horizontal_offset |
inset |
spread_distance |
vertical_offset |
sRGBA color
struct hc_color_s {
float alpha;
float blue;
float green;
float red;
};
alpha | Alpha, 0.0 (transparent) to 1.0 (opaque) |
---|---|
blue | Blue, 0.0 to 1.0 |
green | Green, 0.0 to 1.0 |
red | Red, 0.0 to 1.0 |
CSS media properties
struct hc_media_s {
int color_bits;
hc_rect_t margin;
int monochrome_bits;
hc_size_t size;
const char *type;
};
color_bits | Color bits |
---|---|
margin | Margins |
monochrome_bits | Grayscale bits |
size | Dimensions |
type | "print", "screen", etc. |
Point/coordinate
struct hc_point_s {
float left;
float top;
};
left | Horizontal position |
---|---|
top | Vertical position |
Rectangle
struct hc_rect_s {
float bottom;
float left;
float right;
float top;
};
bottom | Bottom offset |
---|---|
left | Left offset |
right | Right offset |
top | Top offset |
SHA3 hashing context
struct hc_sha3_s {
unsigned char used, block, state[200];
};
state[200] | SHA3 state |
---|
Point/coordinate
struct hc_size_s {
float height;
float width;
};
height | Height |
---|---|
width | Width |
CSS table properties
struct hc_table_s {
hc_border_collapse_t border_collapse;
hc_caption_side_t caption_side;
hc_empty_cells_t empty_cells;
hc_table_layout_t table_layout;
};
border_collapse |
---|
caption_side |
empty_cells |
table_layout |
CSS text properties
struct hc_text_s {
hc_color_t color;
hc_direction_t direction;
hc_font_t *font;
const char *font_family;
float font_size;
float font_size_adjust;
hc_font_stretch_t font_stretch;
hc_font_style_t font_style;
hc_font_variant_t font_variant;
hc_font_weight_t font_weight;
float letter_spacing;
float line_height;
const char *quotes[4];
hc_text_align_t text_align;
hc_text_decoration_t text_decoration;
float text_indent;
hc_text_transform_t text_transform;
hc_unicode_bidi_t unicode_bidi;
hc_white_space_t white_space;
float word_spacing;
};
color | |
---|---|
direction | |
font | Loaded font |
font_family | |
font_size | |
font_size_adjust | |
font_stretch | |
font_style | |
font_variant | |
font_weight | |
letter_spacing | |
line_height | |
quotes[4] | |
text_align | |
text_decoration | |
text_indent | |
text_transform | |
unicode_bidi | |
white_space | |
word_spacing |
Functions...
extern const char *const hcElements[HC_ELEMENT_MAX];