1 /****************************************************************************
2     Copyright (C) 1987-2015 by Jeffery P. Hansen
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 ****************************************************************************/
18 
19 #ifndef __html_h
20 #define __html_h
21 
22 #include "fonts.h"
23 
24 
25 /*
26  * HTML unit types
27  */
28 typedef enum _hutype_t {
29 	HU_TEXT = 0,
30 #define HU_TEXT		HU_TEXT		/* Text string */
31 	HU_RULE = 1,
32 #define HU_RULE		HU_RULE		/* Horizontal rule */
33 	HU_BREAK = 2,
34 #define HU_BREAK	HU_BREAK	/* Line break */
35 	HU_NEWLINE = 3,
36 #define HU_NEWLINE	HU_NEWLINE	/* New line */
37 	HU_IMAGE = 4
38 #define HU_IMAGE	HU_IMAGE	/* In-line image */
39 } hutype_t;
40 
41 /*
42  * Types of html elements
43  */
44 #define HTML_TEXT	0			/* Block of text */
45 #define HTML_A		1			/* Hyperlink or tag */
46 #define HTML_B		2			/* Bold */
47 #define HTML_I		3			/* Italic */
48 #define HTML_H3		4			/* Heading 3 */
49 #define HTML_H2		5			/* Heading 2 */
50 #define HTML_H1		6			/* Heading 1 */
51 #define HTML_SMALL	7			/* Small text */
52 #define HTML_BIG	8			/* Big text */
53 #define HTML_TT		9			/* Typewriter text */
54 #define HTML_PRE	10			/* Preformated */
55 #define HTML_BR		11			/* Break */
56 #define HTML_HR		12			/* Horizontal rule */
57 #define HTML_FONT	13			/* Font change */
58 #define HTML_CRLF	14			/* Newline */
59 #define HTML_CHAR	15			/* Special character */
60 #define HTML_P		16			/* Paragraph */
61 
62 #define HTAG_MAX	5 			/* Maximum number of tags for an html element */
63 
64 typedef unsigned int htmlcode_t;
65 typedef struct HtmlUnit_str HtmlUnit;
66 typedef struct HtmlContext_str HtmlContext;
67 typedef struct HtmlFont_str HtmlFont;
68 typedef struct Html_str Html;
69 
70 /*
71  * HtmlFont - Font description
72  */
73 struct HtmlFont_str {
74   GateFont	gateFont;		/* base font part */
75   fontsize_t	points;			/* Current font size in points*/
76 };
77 
78 /*
79  * HtmlContext - Style information
80  *
81  * This object contains style information about a text string including the
82  * font and any associated hyperlinks or tags.  This object is used both
83  * to represent the current state as well as to mark a piece of text.
84  *
85  */
86 struct HtmlContext_str {
87   HtmlFont		hc_font;		/* Font */
88   //int			hc_pixel;		/* Pixel color */
89   GateColor		hc_pixelColor;
90 
91   char			*hc_link;		/* Associated hyperlink */
92   char			*hc_tag;		/* Associated tag */
93   int			hc_preformat;		/* Are we in preformating mode */
94 
95   HtmlContext		*hc_next;		/* Next item in stack */
96 
97   /*
98    *     These are informational members computed automatically
99    */
100   Html			*hc_html;		/* Parent html object */
101   XFontStruct		*hc_xFont;		/* The XFontStruct font definition */
102   int			hc_is16bit;		/* Is this a 16-bit font (e.g., Japanese) */
103   int			hc_spaceWidth;		/* Width of a space */
104   GateFontMetrics	hc_fontMetrics;		/* Metrics of the current font */
105 };
106 
107 /*
108  * HtmlUnit - Unit of formatted text.
109  *
110  * This object represents a block of text with the same html properties
111  * associated with it.
112  *
113  */
114 struct HtmlUnit_str {
115   hutype_t		hu_type;		/* Type of unit */
116   char			*hu_text;		/* Text in the unit */
117   int			hu_x,hu_y;		/* Position of unit (relative to block origin) */
118   HtmlContext		*hu_context;		/* Context of the unit */
119   int			hu_width;		/* Width of this unit */
120   Tk_Image		hu_image;		/* Image if this is an image unit */
121 
122   HtmlUnit		*hu_next;
123   HtmlUnit		*hu_prev;
124 };
125 
126 /*
127  * HtmlTagOpt - An option in an html tag
128  */
129 typedef struct {
130   char		*hto_label;
131   char		*hto_value;
132 } HtmlTagOpt;
133 
134 
135 /*
136  * HtmlTag - A parsed html tag
137  */
138 typedef struct {
139   char		*ht_name;			/* Name of this tag */
140   int		ht_numOptions;			/* Number of options in tag */
141   HtmlTagOpt	*ht_options;			/* Options */
142 } HtmlTag;
143 
144 /*
145  * Html - Block of html formatted text.
146  */
147 struct Html_str {
148   Locale	*h_locale;			/* Locale used to construct html */
149 
150   TargetDev_e	h_target;			/* Is this formatted for print or x11 display */
151 
152   int		h_reqWidth;			/* Requested width */
153   int		h_width;			/* Actual width */
154   int		h_height;			/* Actual height */
155   int		h_isVisible;			/* Are there any visible characters */
156 
157   int		h_zoom;				/* Zoom level used for formatting */
158 
159   int		h_dataLen;			/* Length of data */
160   char		*h_data;			/* Data for html formatting */
161 
162   HtmlContext	*h_context;			/* The context stack used in formatting */
163   HtmlContext	*h_contextPool;			/* Dequeued but possibly in-use context objects */
164 
165   HtmlUnit	*h_head;			/* Pointer to first html unit */
166   HtmlUnit	*h_tail;			/* Pointer to last html unit */
167 };
168 
169 Html *new_Html(TargetDev_e target);		/* Create a new html object */
170 void delete_Html(Html*);			/* Delete an html object */
171 void Html_addLine(Html*,const char *line);	/* Add a line to html object */
172 void Html_format(Html*);			/* Format the html object */
173 void Html_draw(Html*,int x,int y);		/* Draw at specified position */
174 int Html_isHit(Html*,int x,int y);		/* Is a hit at (x,y) inside formatted object? */
175 const char *Html_getLink(Html*,int x,int y);	/* Get hyperlink referenced at (x,y) */
176 
177 void Html_psPrint(Html *h,GPrint *P,int x,int y);
178 
179 int HtmlFont_isEqual(const HtmlFont*,const HtmlFont*);	/* Return non-zero if fonts are equal */
180 HtmlFont *HtmlFont_init(HtmlFont*,GateFont,fontsize_t points);
181 void HtmlFont_print(HtmlFont*,FILE*);
182 void HtmlContext_print(const HtmlContext * context, FILE * fp);
183 
184 int Hyperlink_selectAt(int x,int y);		/* Select a hyperlink at (x,y) in response to mouse down */
185 int Hyperlink_confirmAt(int x,int y);		/* Confirm a hyperlink at (x,y) in response to mouse up */
186 void Hyperlink_cancel();			/* Cancel any selected hyperlink */
187 int Hyperlink_isPending();
188 const char *Hyperlink_getAt(int x,int y);	/* Return the hyperlink at the specified location */
189 
190 #define HtmlContext_fontAscent(hc) (hc)->hc_fontMetrics.ascent
191 #define HtmlContext_fontDescent(hc) (hc)->hc_fontMetrics.descent
192 
193 
194 #endif
195