NetSurf
plot_style.h
Go to the documentation of this file.
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 */
48typedef 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 */
65typedef 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 */
71
72
73/**
74 * Plot style for stroke/fill plotters
75 */
76typedef 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 */
83
84
85/**
86 * Generic font family type
87 */
88typedef enum {
94 PLOT_FONT_FAMILY_COUNT /**< Number of generic families */
96
97
98/**
99 * Font plot flags
100 */
101typedef enum {
107
108/**
109 * Font style for plotting
110 */
111typedef 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 */
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/**
168 * Obtain the luminance of a colour according to ITU BT.601
169 *
170 * ITU BT.601 formula is
171 * Y = 0.299 R + 0.587 G + 0.114 B
172 * actual values are
173 * Y = 76/255 R + 150/255 G + 29/255 B
174 * Y = 0.298 R + 0.588 G + 0.113 B
175 *
176 * @note if additional performance is required this could be altered to
177 * Y = 0.375 R + 0.5 G + 0.125 B
178 * with
179 * Y = (R << 1 + R + G << 2 + B) >> 3
180 */
181#define colour_lightness(c0) \
182 ((((c0 & 0x0000ff) * 77) >> 8) + \
183 (((c0 & 0x00ff00) * 151) >> 16) + \
184 (((c0 & 0xff0000) * 30) >> 24))
185
186/* Choose either black or white, depending on which is nearest to the
187 * percieved lightness of the supplied colour, c0. */
188#define colour_to_bw_nearest(c0) \
189 ((colour_lightness(c0) > (0xff / 2)) ? 0xffffff : 0x000000)
190
191/* Choose either black or white, depending on which is furthest from the
192 * percieved lightness of the supplied colour, c0. */
193#define colour_to_bw_furthest(c0) \
194 ((colour_lightness(c0) > (0xff / 2)) ? 0x000000 : 0xffffff)
195
196/* Mix two colours according to the proportion given by p, where 0 <= p <= 255
197 * p = 0 gives result ==> c1, p = 255 gives result ==> c0 */
198#define mix_colour(c0, c1, p) \
199 ((((((c1 & 0xff00ff) * (255 - p)) + \
200 ((c0 & 0xff00ff) * ( p)) ) >> 8) & 0xff00ff) | \
201 (((((c1 & 0x00ff00) * (255 - p)) + \
202 ((c0 & 0x00ff00) * ( p)) ) >> 8) & 0x00ff00))
203
204/* Get the red channel from a colour */
205#define red_from_colour(c) \
206 ((c ) & 0xff)
207
208/* Get the green channel from a colour */
209#define green_from_colour(c) \
210 ((c >> 8) & 0xff)
211
212/* Get the blue channel from a colour */
213#define blue_from_colour(c) \
214 ((c >> 16) & 0xff)
215
216/* Swap red and blue channels in a colour */
217#define colour_rb_swap(c) \
218 (((0x000000ff & c) << 16) | \
219 ((0x0000ff00 & c) ) | \
220 ((0x00ff0000 & c) >> 16))
221
222/** Colour components */
228};
229
230/**
231 * Engorge a particular colour channel.
232 *
233 * \param[in] col The colour to engorge a component of.
234 * \param[in] dark Whether col is a dark colour.
235 * \param[in] comp Colour component to engorge.
236 */
238 colour col,
239 bool dark,
240 enum plot_colour_component comp)
241{
242 static const colour msk[PLOT_COLOUR_COMPONENT_ALPHA] = {
243 [PLOT_COLOUR_COMPONENT_RED] = 0x0000ff,
244 [PLOT_COLOUR_COMPONENT_GREEN] = 0x00ff00,
245 [PLOT_COLOUR_COMPONENT_BLUE] = 0xff0000,
246 };
247 colour d = dark ? darken_colour(col) : double_darken_colour(col);
248 colour l = dark ? double_lighten_colour(col) : lighten_colour(col);
249
250 assert(comp < PLOT_COLOUR_COMPONENT_ALPHA);
251
252 return (msk[comp] & l) | (~msk[comp] & d);
253}
254
255
256/* global fill styles */
260
261
262/* Box model debug outline styles for content, padding and margin edges */
263extern plot_style_t const * const plot_style_content_edge;
264extern plot_style_t const * const plot_style_padding_edge;
265extern plot_style_t const * const plot_style_margin_edge;
266
267
268/* Broken object replacement styles */
269extern plot_style_t const * const plot_style_broken_object;
271
272
273/* other styles */
282
283
284/* Default font style */
285extern plot_font_style_t const * const plot_style_font;
286
287
288#endif
plot_style_t * plot_style_fill_white
Definition: plot_style.c:32
plot_style_t const *const plot_style_broken_object
Definition: plot_style.c:79
plot_style_t const *const plot_style_margin_edge
Definition: plot_style.c:68
plot_font_style_t const *const plot_style_font
Definition: plot_style.c:165
plot_style_t const *const plot_style_content_edge
Definition: plot_style.c:52
plot_colour_component
Colour components.
Definition: plot_style.h:223
@ PLOT_COLOUR_COMPONENT_RED
Definition: plot_style.h:224
@ PLOT_COLOUR_COMPONENT_GREEN
Definition: plot_style.h:225
@ PLOT_COLOUR_COMPONENT_BLUE
Definition: plot_style.h:226
@ PLOT_COLOUR_COMPONENT_ALPHA
Definition: plot_style.h:227
plot_style_t * plot_style_fill_wbasec
Definition: plot_style.c:109
struct plot_style_s plot_style_t
Plot style for stroke/fill plotters.
#define lighten_colour(c1)
Definition: plot_style.h:150
plot_style_t * plot_style_stroke_darkwbasec
Definition: plot_style.c:146
plot_style_t * plot_style_caret
Definition: plot_style.c:98
int32_t plot_style_fixed
Definition: plot_style.h:48
#define darken_colour(c1)
Definition: plot_style.h:133
plot_operation_type_t
Type of plot operation.
Definition: plot_style.h:65
@ PLOT_OP_TYPE_NONE
No operation.
Definition: plot_style.h:66
@ PLOT_OP_TYPE_DASH
Dashed plot.
Definition: plot_style.h:69
@ PLOT_OP_TYPE_DOT
Dotted plot.
Definition: plot_style.h:68
@ PLOT_OP_TYPE_SOLID
Solid colour.
Definition: plot_style.h:67
plot_style_t * plot_style_fill_red
Definition: plot_style.c:44
plot_style_t const *const plot_style_padding_edge
Definition: plot_style.c:60
plot_font_flags_t
Font plot flags.
Definition: plot_style.h:101
@ FONTF_ITALIC
Definition: plot_style.h:103
@ FONTF_SMALLCAPS
Definition: plot_style.h:105
@ FONTF_OBLIQUE
Definition: plot_style.h:104
@ FONTF_NONE
Definition: plot_style.h:102
plot_style_t * plot_style_fill_wblobc
Definition: plot_style.c:131
#define double_lighten_colour(c1)
Definition: plot_style.h:156
plot_font_generic_family_t
Generic font family type.
Definition: plot_style.h:88
@ PLOT_FONT_FAMILY_CURSIVE
Definition: plot_style.h:92
@ PLOT_FONT_FAMILY_COUNT
Number of generic families.
Definition: plot_style.h:94
@ PLOT_FONT_FAMILY_SANS_SERIF
Definition: plot_style.h:89
@ PLOT_FONT_FAMILY_FANTASY
Definition: plot_style.h:93
@ PLOT_FONT_FAMILY_MONOSPACE
Definition: plot_style.h:91
@ PLOT_FONT_FAMILY_SERIF
Definition: plot_style.h:90
plot_font_style_t const *const plot_fstyle_broken_object
Definition: plot_style.c:90
plot_style_t * plot_style_fill_black
Definition: plot_style.c:38
static colour colour_engorge_component(colour col, bool dark, enum plot_colour_component comp)
Engorge a particular colour channel.
Definition: plot_style.h:237
plot_style_t * plot_style_stroke_lightwbasec
Definition: plot_style.c:153
struct plot_font_style plot_font_style_t
Font style for plotting.
plot_style_t * plot_style_fill_lightwbasec
Definition: plot_style.c:123
#define double_darken_colour(c1)
Definition: plot_style.h:138
plot_style_t * plot_style_stroke_wblobc
Definition: plot_style.c:139
plot_style_t * plot_style_fill_darkwbasec
Definition: plot_style.c:116
Font style for plotting.
Definition: plot_style.h:111
plot_font_generic_family_t family
Generic family to plot with.
Definition: plot_style.h:118
colour foreground
Colour of text.
Definition: plot_style.h:123
lwc_string *const * families
Array of pointers to font families.
Definition: plot_style.h:117
plot_font_flags_t flags
Font flags.
Definition: plot_style.h:121
plot_style_fixed size
Font size, in pt.
Definition: plot_style.h:119
colour background
Background colour to blend to, if appropriate.
Definition: plot_style.h:122
int weight
Font weight: value in range [100,900] as per CSS.
Definition: plot_style.h:120
Plot style for stroke/fill plotters.
Definition: plot_style.h:76
colour fill_colour
Colour of fill.
Definition: plot_style.h:81
plot_style_fixed stroke_width
Width of stroke, in pixels.
Definition: plot_style.h:78
plot_operation_type_t fill_type
Fill plot type.
Definition: plot_style.h:80
colour stroke_colour
Colour of stroke.
Definition: plot_style.h:79
plot_operation_type_t stroke_type
Stroke plot type.
Definition: plot_style.h:77
NetSurf types.
uint32_t colour
Colour type: XBGR.
Definition: types.h:35