NetSurf
layout.c
Go to the documentation of this file.
1/*
2 * Copyright 2011 Daniel Silverstone <dsilvers@digital-scurf.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 * monkey implementation of font layout.
22 */
23
24#include <stddef.h>
25
26#include "utils/utf8.h"
27#include "netsurf/plot_style.h"
28#include "netsurf/layout.h"
29
30#include "monkey/layout.h"
31
33 const char *string, size_t length,
34 int *width)
35{
36 *width = (fstyle->size * utf8_bounded_length(string, length)) / PLOT_STYLE_SCALE;
37 return NSERROR_OK;
38}
39
40/**
41 * Find the position in a string where an x coordinate falls.
42 *
43 * \param fstyle style for this text
44 * \param string UTF-8 string to measure
45 * \param length length of string
46 * \param x x coordinate to search for
47 * \param char_offset updated to offset in string of actual_x, [0..length]
48 * \param actual_x updated to x coordinate of character closest to x
49 * \return true on success, false on error and error reported
50 */
51
53 const char *string, size_t length,
54 int x, size_t *char_offset, int *actual_x)
55{
56 *char_offset = x / (fstyle->size / PLOT_STYLE_SCALE);
57 if (*char_offset > length)
58 *char_offset = length;
59 *actual_x = *char_offset * (fstyle->size / PLOT_STYLE_SCALE);
60 return NSERROR_OK;
61}
62
63
64/**
65 * Find where to split a string to make it fit a width.
66 *
67 * \param fstyle style for this text
68 * \param string UTF-8 string to measure
69 * \param length length of string, in bytes
70 * \param x width available
71 * \param char_offset updated to offset in string of actual_x, [1..length]
72 * \param actual_x updated to x coordinate of character closest to x
73 * \return true on success, false on error and error reported
74 *
75 * On exit, char_offset indicates first character after split point.
76 *
77 * Note: char_offset of 0 should never be returned.
78 *
79 * Returns:
80 * char_offset giving split point closest to x, where actual_x <= x
81 * else
82 * char_offset giving split point closest to x, where actual_x > x
83 *
84 * Returning char_offset == length means no split possible
85 */
86
88 const char *string, size_t length,
89 int x, size_t *char_offset, int *actual_x)
90{
91 int c_off = *char_offset = x / (fstyle->size / PLOT_STYLE_SCALE);
92 if (*char_offset > length) {
93 *char_offset = length;
94 } else {
95 while (*char_offset > 0) {
96 if (string[*char_offset] == ' ')
97 break;
98 (*char_offset)--;
99 }
100 if (*char_offset == 0) {
101 *char_offset = c_off;
102 while (*char_offset < length && string[*char_offset] != ' ') {
103 (*char_offset)++;
104 }
105 }
106 }
107 *actual_x = *char_offset * (fstyle->size / PLOT_STYLE_SCALE);
108 return NSERROR_OK;
109}
110
113 .position = nsfont_position_in_string,
114 .split = nsfont_split,
115};
116
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
struct gui_layout_table * monkey_layout_table
Definition: layout.c:117
static nserror nsfont_position_in_string(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: layout.c:52
static nserror nsfont_width(const plot_font_style_t *fstyle, const char *string, size_t length, int *width)
Definition: layout.c:32
static struct gui_layout_table layout_table
Definition: layout.c:111
static nserror nsfont_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: layout.c:87
Interface to platform-specific layout operation table.
plotter style interfaces, generic styles and style colour helpers.
#define PLOT_STYLE_SCALE
Scaling factor for plot styles.
Definition: plot_style.h:45
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
size_t utf8_bounded_length(const char *s, size_t l)
Calculated the length (in characters) of a bounded UTF-8 string.
Definition: utf8.c:80
UTF-8 manipulation functions (interface).