NetSurf
font.c
Go to the documentation of this file.
1/*
2 * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
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 *
22 * HTML internal font handling implementation.
23 */
24
25#include "utils/nsoption.h"
26#include "netsurf/plot_style.h"
27#include "css/utils.h"
28
29#include "html/font.h"
30
31/**
32 * Map a generic CSS font family to a generic plot font family
33 *
34 * \param css Generic CSS font family
35 * \return Plot font family
36 */
38plot_font_generic_family(enum css_font_family_e css)
39{
41
42 switch (css) {
43 case CSS_FONT_FAMILY_SERIF:
45 break;
46 case CSS_FONT_FAMILY_MONOSPACE:
48 break;
49 case CSS_FONT_FAMILY_CURSIVE:
51 break;
52 case CSS_FONT_FAMILY_FANTASY:
54 break;
55 case CSS_FONT_FAMILY_SANS_SERIF:
56 default:
58 break;
59 }
60
61 return plot;
62}
63
64/**
65 * Map a CSS font weight to a plot weight value
66 *
67 * \param css CSS font weight
68 * \return Plot weight
69 */
70static int plot_font_weight(enum css_font_weight_e css)
71{
72 int weight;
73
74 switch (css) {
75 case CSS_FONT_WEIGHT_100:
76 weight = 100;
77 break;
78 case CSS_FONT_WEIGHT_200:
79 weight = 200;
80 break;
81 case CSS_FONT_WEIGHT_300:
82 weight = 300;
83 break;
84 case CSS_FONT_WEIGHT_400:
85 case CSS_FONT_WEIGHT_NORMAL:
86 default:
87 weight = 400;
88 break;
89 case CSS_FONT_WEIGHT_500:
90 weight = 500;
91 break;
92 case CSS_FONT_WEIGHT_600:
93 weight = 600;
94 break;
95 case CSS_FONT_WEIGHT_700:
96 case CSS_FONT_WEIGHT_BOLD:
97 weight = 700;
98 break;
99 case CSS_FONT_WEIGHT_800:
100 weight = 800;
101 break;
102 case CSS_FONT_WEIGHT_900:
103 weight = 900;
104 break;
105 }
106
107 return weight;
108}
109
110/**
111 * Map a CSS font style and font variant to plot font flags
112 *
113 * \param style CSS font style
114 * \param variant CSS font variant
115 * \return Computed plot flags
116 */
117static plot_font_flags_t plot_font_flags(enum css_font_style_e style,
118 enum css_font_variant_e variant)
119{
121
122 if (style == CSS_FONT_STYLE_ITALIC)
123 flags |= FONTF_ITALIC;
124 else if (style == CSS_FONT_STYLE_OBLIQUE)
125 flags |= FONTF_OBLIQUE;
126
127 if (variant == CSS_FONT_VARIANT_SMALL_CAPS)
128 flags |= FONTF_SMALLCAPS;
129
130 return flags;
131}
132
133
134/* exported function documented in html/font.h */
136 const css_unit_ctx *unit_len_ctx,
137 const css_computed_style *css,
138 plot_font_style_t *fstyle)
139{
140 lwc_string **families;
141 css_fixed length = 0;
142 css_unit unit = CSS_UNIT_PX;
143 css_color col;
144
146 css_computed_font_family(css, &families));
147 fstyle->families = families;
148
149 css_computed_font_size(css, &length, &unit);
150 fstyle->size = FIXTOINT(FMUL(css_unit_font_size_len2pt(css,
151 unit_len_ctx, length, unit),
152 INTTOFIX(PLOT_STYLE_SCALE)));
153
154 /* Clamp font size to configured minimum */
155 if (fstyle->size < (nsoption_int(font_min_size) * PLOT_STYLE_SCALE) / 10)
156 fstyle->size = (nsoption_int(font_min_size) * PLOT_STYLE_SCALE) / 10;
157
158 fstyle->weight = plot_font_weight(css_computed_font_weight(css));
159 fstyle->flags = plot_font_flags(css_computed_font_style(css),
160 css_computed_font_variant(css));
161
162 css_computed_color(css, &col);
163 fstyle->foreground = nscss_color_to_ns(col);
164 fstyle->background = 0;
165}
static int plot_font_weight(enum css_font_weight_e css)
Map a CSS font weight to a plot weight value.
Definition: font.c:70
void font_plot_style_from_css(const css_unit_ctx *unit_len_ctx, const css_computed_style *css, plot_font_style_t *fstyle)
Populate a font style using data from a computed CSS style.
Definition: font.c:135
static plot_font_flags_t plot_font_flags(enum css_font_style_e style, enum css_font_variant_e variant)
Map a CSS font style and font variant to plot font flags.
Definition: font.c:117
static plot_font_generic_family_t plot_font_generic_family(enum css_font_family_e css)
Map a generic CSS font family to a generic plot font family.
Definition: font.c:38
Internal font handling interfaces.
#define nscss_color_to_ns(c)
Convert a CSS color to a NetSurf colour primitive.
Definition: css.h:35
plotter style interfaces, generic styles and style colour helpers.
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_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_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
#define PLOT_STYLE_SCALE
Scaling factor for plot styles.
Definition: plot_style.h:45
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
Option reading and saving interface.
#define nsoption_int(OPTION)
Get the value of an integer option.
Definition: nsoption.h:313