NetSurf
font.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 Ole Loots <ole@monochrom.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#include <assert.h>
20#include <stdbool.h>
21#include <stdlib.h>
22
23#include "netsurf/inttypes.h"
24#include "utils/utf8.h"
25#include "utils/log.h"
26#include "utils/nsoption.h"
27#include "netsurf/layout.h"
28#include "netsurf/mouse.h"
29#include "netsurf/plotters.h"
30
31#include "atari/gui.h"
32#include "atari/plot/fontplot.h"
33#include "atari/plot/plot.h"
34#include "atari/findfile.h"
35#include "atari/font.h"
36
38
39
40/**
41 * Find the position in a string where an x coordinate falls.
42 *
43 * \param[in] fstyle style for this text
44 * \param[in] string UTF-8 string to measure
45 * \param[in] length length of string, in bytes
46 * \param[in] x coordinate to search for
47 * \param[out] char_offset updated to offset in string of actual_x, [0..length]
48 * \param[out] actual_x updated to x coordinate of character closest to x
49 * \return NSERROR_OK and char_offset and actual_x updated or appropriate error code on faliure
50 */
51static nserror
53 const char *string,
54 size_t length,
55 int x,
56 size_t *char_offset,
57 int *actual_x)
58{
59 float scale = plot_get_scale();
60
61 if (scale != 1.0) {
62 plot_font_style_t newstyle = *fstyle;
63 newstyle.size = (int)((float)fstyle->size*scale);
64 fplotter->pixel_pos(fplotter, &newstyle, string, length, x,
65 char_offset, actual_x);
66 } else {
67 fplotter->pixel_pos(fplotter, fstyle, string, length, x,
68 char_offset, actual_x);
69 }
70
71 return NSERROR_OK;
72}
73
74
75/**
76 * Find where to split a string to make it fit a width.
77 *
78 * \param[in] fstyle style for this text
79 * \param[in] string UTF-8 string to measure
80 * \param[in] length length of string, in bytes
81 * \param[in] x width available
82 * \param[out] char_offset updated to offset in string of actual_x, [1..length]
83 * \param[out] actual_x updated to x coordinate of character closest to x
84 * \return NSERROR_OK or appropriate error code on faliure
85 *
86 * On exit, char_offset indicates first character after split point.
87 *
88 * \note char_offset of 0 must never be returned.
89 *
90 * Returns:
91 * char_offset giving split point closest to x, where actual_x <= x
92 * else
93 * char_offset giving split point closest to x, where actual_x > x
94 *
95 * Returning char_offset == length means no split possible
96 */
97static nserror
99 const char *string,
100 size_t length,
101 int x,
102 size_t *char_offset,
103 int *actual_x)
104{
105 float scale = plot_get_scale();
106
107 if (scale != 1.0) {
108 plot_font_style_t newstyle = *fstyle;
109 newstyle.size = (int)((float)fstyle->size*scale);
110 fplotter->str_split(fplotter, &newstyle, string, length, x,
111 char_offset, actual_x);
112 } else {
113 fplotter->str_split(fplotter, fstyle, string, length, x,
114 char_offset, actual_x);
115 }
116
117 return NSERROR_OK;
118}
119
120
121/**
122 * Measure the width of a string.
123 *
124 * \param[in] fstyle plot style for this text
125 * \param[in] str UTF-8 string to measure
126 * \param[in] length length of string, in bytes
127 * \param[out] width updated to width of string[0..length)
128 * \return NSERROR_OK and width updated or appropriate error code on faliure
129 */
130static nserror
132 const char *str,
133 size_t length,
134 int *width)
135{
136 float scale = plot_get_scale();
137
138 if (scale != 1.0) {
139 plot_font_style_t newstyle = *fstyle;
140 newstyle.size = (int)((float)fstyle->size*scale);
141 fplotter->str_width(fplotter, &newstyle, str, length, width);
142 } else {
143 fplotter->str_width(fplotter, fstyle, str, length, width);
144 }
145
146 return NSERROR_OK;
147}
148
149
152 .position = atari_font_position,
153 .split = atari_font_split,
154};
155
float plot_get_scale(void)
Definition: plot.c:1864
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
static nserror atari_font_split(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x)
Find where to split a string to make it fit a width.
Definition: font.c:98
static struct gui_layout_table layout_table
Definition: font.c:150
static nserror atari_font_position(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x)
Find the position in a string where an x coordinate falls.
Definition: font.c:52
FONT_PLOTTER fplotter
Definition: plot.c:165
struct gui_layout_table * atari_layout_table
Definition: font.c:156
static nserror atari_font_width(const plot_font_style_t *fstyle, const char *str, size_t length, int *width)
Measure the width of a string.
Definition: font.c:131
Interface to platform-specific layout operation table.
Core mouse and pointer states.
Target independent plotting interface.
Netsurf additional integer type formatting macros.
int width
Definition: gui.c:160
nserror(* width)(const struct plot_font_style *fstyle, const char *string, size_t length, int *width)
Measure the width of a string.
Definition: layout.h:49
Font style for plotting.
Definition: plot_style.h:111
plot_style_fixed size
Font size, in pt.
Definition: plot_style.h:119
_fpmf_str_width str_width
Definition: fontplot.h:56
_fpmf_str_split str_split
Definition: fontplot.h:57
_fpmf_pixel_pos pixel_pos
Definition: fontplot.h:58
Option reading and saving interface.
UTF-8 manipulation functions (interface).