1 /*
2  * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
3  *
4  * This file is part of NetSurf, http://www.netsurf-browser.org/
5  *
6  * NetSurf is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * NetSurf is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /**
20  * \file
21  * plotter style interfaces, generic styles and style colour helpers.
22  */
23 
24 #ifndef NETSURF_PLOT_STYLE_H
25 #define NETSURF_PLOT_STYLE_H
26 
27 #include <stdint.h>
28 #include <stdint.h>
29 #include <libwapcaplet/libwapcaplet.h>
30 #include "netsurf/types.h"
31 
32 /** light grey widget base colour */
33 #define WIDGET_BASEC 0xd9d9d9
34 
35 /** black blob colour */
36 #define WIDGET_BLOBC 0x000000
37 
38 /** Transparent colour value. */
39 #define NS_TRANSPARENT 0x01000000
40 
41 /** 22:10 fixed point */
42 #define PLOT_STYLE_RADIX (10)
43 
44 /** Scaling factor for plot styles */
45 #define PLOT_STYLE_SCALE (1 << PLOT_STYLE_RADIX)
46 
47 /* type for fixed point numbers */
48 typedef int32_t plot_style_fixed;
49 
50 /* Convert an int to fixed point */
51 #define plot_style_int_to_fixed(v) ((v) << PLOT_STYLE_RADIX)
52 
53 /* Convert fixed point to int */
54 #define plot_style_fixed_to_int(v) ((v) >> PLOT_STYLE_RADIX)
55 
56 /* Convert fixed point to float */
57 #define plot_style_fixed_to_float(v) (((float)v) / PLOT_STYLE_SCALE)
58 
59 /* Convert fixed point to double */
60 #define plot_style_fixed_to_double(v) (((double)v) / PLOT_STYLE_SCALE)
61 
62 /**
63  * Type of plot operation
64  */
65 typedef enum {
66 	PLOT_OP_TYPE_NONE = 0, /**< No operation */
67 	PLOT_OP_TYPE_SOLID, /**< Solid colour */
68 	PLOT_OP_TYPE_DOT, /**< Dotted plot */
69 	PLOT_OP_TYPE_DASH, /**< Dashed plot */
70 } plot_operation_type_t;
71 
72 
73 /**
74  * Plot style for stroke/fill plotters
75  */
76 typedef struct plot_style_s {
77 	plot_operation_type_t stroke_type; /**< Stroke plot type */
78 	plot_style_fixed stroke_width; /**< Width of stroke, in pixels */
79 	colour stroke_colour; /**< Colour of stroke */
80 	plot_operation_type_t fill_type; /**< Fill plot type */
81 	colour fill_colour; /**< Colour of fill */
82 } plot_style_t;
83 
84 
85 /**
86  * Generic font family type
87  */
88 typedef enum {
89 	PLOT_FONT_FAMILY_SANS_SERIF = 0,
90 	PLOT_FONT_FAMILY_SERIF,
91 	PLOT_FONT_FAMILY_MONOSPACE,
92 	PLOT_FONT_FAMILY_CURSIVE,
93 	PLOT_FONT_FAMILY_FANTASY,
94 	PLOT_FONT_FAMILY_COUNT /**< Number of generic families */
95 } plot_font_generic_family_t;
96 
97 
98 /**
99  * Font plot flags
100  */
101 typedef enum {
102 	FONTF_NONE = 0,
103 	FONTF_ITALIC = 1,
104 	FONTF_OBLIQUE = 2,
105 	FONTF_SMALLCAPS = 4,
106 } plot_font_flags_t;
107 
108 /**
109  * Font style for plotting
110  */
111 typedef struct plot_font_style {
112 	/**
113 	 * Array of pointers to font families.
114 	 *
115 	 * May be NULL.  Array is NULL terminated.
116 	 */
117 	lwc_string * const * families;
118 	plot_font_generic_family_t family; /**< Generic family to plot with */
119 	plot_style_fixed size; /**< Font size, in pt */
120 	int weight; /**< Font weight: value in range [100,900] as per CSS */
121 	plot_font_flags_t flags; /**< Font flags */
122 	colour background; /**< Background colour to blend to, if appropriate */
123 	colour foreground; /**< Colour of text */
124 } plot_font_style_t;
125 
126 
127 /* Darken a colour by taking seven eighths of each channel's intensity */
128 #define half_darken_colour(c1)				 		\
129 	((((7 * (c1 & 0xff00ff)) >> 3) & 0xff00ff) |			\
130 	 (((7 * (c1 & 0x00ff00)) >> 3) & 0x00ff00))
131 
132 /* Darken a colour by taking three quarters of each channel's intensity */
133 #define darken_colour(c1)				 		\
134 	((((3 * (c1 & 0xff00ff)) >> 2) & 0xff00ff) |			\
135 	 (((3 * (c1 & 0x00ff00)) >> 2) & 0x00ff00))
136 
137 /* Darken a colour by taking nine sixteenths of each channel's intensity */
138 #define double_darken_colour(c1)					\
139 	((((9 * (c1 & 0xff00ff)) >> 4) & 0xff00ff) |			\
140 	 (((9 * (c1 & 0x00ff00)) >> 4) & 0x00ff00))
141 
142 /* Lighten a colour by taking seven eighths of each channel's intensity
143  * and adding a full one eighth intensity */
144 #define half_lighten_colour(c1)						\
145 	(((((7 * (c1 & 0xff00ff)) >> 3) + 0x200020) & 0xff00ff) |	\
146 	 ((((7 * (c1 & 0x00ff00)) >> 3) + 0x002000) & 0x00ff00))
147 
148 /* Lighten a colour by taking 12/16ths of each channel's intensity
149  * and adding a full 4/16ths intensity */
150 #define lighten_colour(c1)						\
151 	(((((3 * (c1 & 0xff00ff)) >> 2) + 0x400040) & 0xff00ff) |	\
152 	 ((((3 * (c1 & 0x00ff00)) >> 2) + 0x004000) & 0x00ff00))
153 
154 /* Lighten a colour by taking 9/16ths of each channel's intensity
155  * and adding a full 7/16ths intensity */
156 #define double_lighten_colour(c1)					\
157 	(((((9 * (c1 & 0xff00ff)) >> 4) + 0x700070) & 0xff00ff) |	\
158 	 ((((9 * (c1 & 0x00ff00)) >> 4) + 0x007000) & 0x00ff00))
159 
160 /* Blend two colours by taking half the intensity of each channel in the first
161  * colour and adding them to half the intensity of each channel in the second
162  * colour */
163 #define blend_colour(c0, c1)						\
164 	(((((c0 & 0xff00ff) + (c1 & 0xff00ff)) >> 1) & 0xff00ff) |	\
165 	 ((((c0 & 0x00ff00) + (c1 & 0x00ff00)) >> 1) & 0x00ff00))
166 
167 /* Get the percieved lightness of the supplied colour, c0. */
168 #define colour_lightness(c0)						\
169 	((((c0 & 0x0000ff) *  77) >>  8) +				\
170 	 (((c0 & 0x00ff00) * 151) >> 16) +				\
171 	 (((c0 & 0xff0000) *  28) >> 24))
172 
173 /* Choose either black or white, depending on which is nearest to the
174  * percieved lightness of the supplied colour, c0. */
175 #define colour_to_bw_nearest(c0)					\
176 	((colour_lightness(c0) > (0xff / 2)) ? 0xffffff : 0x000000)
177 
178 /* Choose either black or white, depending on which is furthest from the
179  * percieved lightness of the supplied colour, c0. */
180 #define colour_to_bw_furthest(c0)					\
181 	((colour_lightness(c0) > (0xff / 2)) ? 0x000000 : 0xffffff)
182 
183 /* Mix two colours according to the proportion given by p, where 0 <= p <= 255
184  * p = 0 gives result ==> c1,  p = 255 gives result ==> c0 */
185 #define mix_colour(c0, c1, p)						\
186 	((((((c1 & 0xff00ff) * (255 - p)) +				\
187 	    ((c0 & 0xff00ff) * (      p))   ) >> 8) & 0xff00ff) |	\
188 	 (((((c1 & 0x00ff00) * (255 - p)) +				\
189 	    ((c0 & 0x00ff00) * (      p))   ) >> 8) & 0x00ff00))
190 
191 /* get a bitmap pixel (image/bitmap.h) into a plot colour */
192 #define pixel_to_colour(b)						\
193 	b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)
194 
195 /* Get the red channel from a colour */
196 #define red_from_colour(c)						\
197 	((c      ) & 0xff)
198 
199 /* Get the green channel from a colour */
200 #define green_from_colour(c)						\
201 	((c >>  8) & 0xff)
202 
203 /* Get the blue channel from a colour */
204 #define blue_from_colour(c)						\
205 	((c >> 16) & 0xff)
206 
207 /* Swap red and blue channels in a colour */
208 #define colour_rb_swap(c)						\
209 	(((0x000000ff & c) << 16) |					\
210 	 ((0x0000ff00 & c)      ) |					\
211 	 ((0x00ff0000 & c) >> 16))
212 
213 /** Colour components */
214 enum plot_colour_component {
215 	PLOT_COLOUR_COMPONENT_RED,
216 	PLOT_COLOUR_COMPONENT_GREEN,
217 	PLOT_COLOUR_COMPONENT_BLUE,
218 	PLOT_COLOUR_COMPONENT_ALPHA,
219 };
220 
221 /**
222  * Engorge a particular colour channel.
223  *
224  * \param[in] col   The colour to engorge a component of.
225  * \param[in] dark  Whether col is a dark colour.
226  * \param[in] comp  Colour component to engorge.
227  */
colour_engorge_component(colour col,bool dark,enum plot_colour_component comp)228 static inline colour colour_engorge_component(
229 		colour col,
230 		bool dark,
231 		enum plot_colour_component comp)
232 {
233 	static const colour msk[PLOT_COLOUR_COMPONENT_ALPHA] = {
234 		[PLOT_COLOUR_COMPONENT_RED]   = 0x0000ff,
235 		[PLOT_COLOUR_COMPONENT_GREEN] = 0x00ff00,
236 		[PLOT_COLOUR_COMPONENT_BLUE]  = 0xff0000,
237 	};
238 	colour d = dark ? darken_colour(col) : double_darken_colour(col);
239 	colour l = dark ? double_lighten_colour(col) : lighten_colour(col);
240 
241 	assert(comp < PLOT_COLOUR_COMPONENT_ALPHA);
242 
243 	return (msk[comp] & l) | (~msk[comp] & d);
244 }
245 
246 
247 /* global fill styles */
248 extern plot_style_t *plot_style_fill_white;
249 extern plot_style_t *plot_style_fill_red;
250 extern plot_style_t *plot_style_fill_black;
251 
252 
253 /* Box model debug outline styles for content, padding and margin edges */
254 extern plot_style_t const * const plot_style_content_edge;
255 extern plot_style_t const * const plot_style_padding_edge;
256 extern plot_style_t const * const plot_style_margin_edge;
257 
258 
259 /* Broken object replacement styles */
260 extern plot_style_t const * const plot_style_broken_object;
261 extern plot_font_style_t const * const plot_fstyle_broken_object;
262 
263 
264 /* other styles */
265 extern plot_style_t *plot_style_caret;
266 extern plot_style_t *plot_style_fill_wbasec;
267 extern plot_style_t *plot_style_fill_darkwbasec;
268 extern plot_style_t *plot_style_fill_lightwbasec;
269 extern plot_style_t *plot_style_fill_wblobc;
270 extern plot_style_t *plot_style_stroke_wblobc;
271 extern plot_style_t *plot_style_stroke_darkwbasec;
272 extern plot_style_t *plot_style_stroke_lightwbasec;
273 
274 
275 /* Default font style */
276 extern plot_font_style_t const * const plot_style_font;
277 
278 
279 #endif
280