NetSurf
font_diskfont.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 - 2016 Chris Young <chris@unsatisfactorysoftware.co.uk>
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 "amiga/os3support.h"
20
21#include <stdlib.h>
22#include <string.h>
23#include <proto/diskfont.h>
24#include <proto/exec.h>
25#include <proto/graphics.h>
26#include <proto/utility.h>
27
28#include <graphics/rpattr.h>
29
30#include "utils/log.h"
31#include "utils/utf8.h"
32#include "utils/nsoption.h"
33
34#include "amiga/font.h"
35#include "amiga/font_diskfont.h"
36#include "amiga/gui.h"
37#include "amiga/plotters.h"
38#include "amiga/utf8.h"
39
40#define MAX_FONT_NAME_SIZE 33
41
43static struct TextFont *prev_font = NULL;
44static struct RastPort temp_rp;
45
46static struct TextFont *ami_font_bm_open(struct RastPort *rp, const plot_font_style_t *fstyle)
47{
48 struct TextFont *bmfont = NULL;
49 struct TextAttr tattr;
50 char *fontname;
51 char font[MAX_FONT_NAME_SIZE];
52
53 if((prev_fstyle != NULL) && (prev_font != NULL) &&
54 (fstyle->family == prev_fstyle->family) &&
55 (fstyle->size == prev_fstyle->size) &&
56 (fstyle->flags == prev_fstyle->flags) &&
57 (fstyle->weight == prev_fstyle->weight)) {
58 return prev_font;
59 }
60
61 if(rp == NULL) return NULL;
62
63 tattr.ta_Flags = 0;
64
65 switch(fstyle->family)
66 {
68 fontname = nsoption_charp(font_sans);
69 break;
71 fontname = nsoption_charp(font_serif);
72 break;
74 fontname = nsoption_charp(font_mono);
75 break;
77 fontname = nsoption_charp(font_cursive);
78 break;
80 fontname = nsoption_charp(font_fantasy);
81 break;
82 default:
83 return NULL;
84 break;
85 }
86
87 tattr.ta_Style = FS_NORMAL;
88
89 if (fstyle->flags & FONTF_OBLIQUE)
90 tattr.ta_Style = FSF_ITALIC;
91
92 if (fstyle->flags & FONTF_ITALIC)
93 tattr.ta_Style = FSF_ITALIC;
94
95 if (fstyle->weight >= 700)
96 tattr.ta_Style |= FSF_BOLD;
97
98 snprintf(font, MAX_FONT_NAME_SIZE, "%s.font", fontname);
99 tattr.ta_Name = font;
100 tattr.ta_YSize = fstyle->size / PLOT_STYLE_SCALE;
101 NSLOG(netsurf, INFO, "font: %s/%d", tattr.ta_Name, tattr.ta_YSize);
102
103 if(prev_font != NULL) {
104 CloseFont(prev_font);
105 prev_font = NULL;
106 }
107
108 if((bmfont = OpenDiskFont(&tattr))) {
109 SetRPAttrs(rp, RPTAG_Font, bmfont, TAG_DONE);
110 }
111
112 if(prev_fstyle != NULL) {
113 memcpy(prev_fstyle, fstyle, sizeof(plot_font_style_t));
114 prev_font = bmfont;
115 }
116
117 return bmfont;
118}
119
120static size_t ami_font_bm_convert_local_to_utf8_offset(const char *utf8string, size_t length, UWORD offset)
121{
122 size_t chr = 0;
123
124 for(UWORD i = 0; i < offset; i++) {
125 chr = utf8_next(utf8string, length, chr);
126 if(chr > length) return length;
127 }
128
129 return chr;
130}
131
132
134 const char *string, size_t length,
135 int *width)
136{
137 char *localtext = NULL;
138
139 *width = length;
140
141 struct TextFont *bmfont = ami_font_bm_open(&temp_rp, fstyle);
142 if(bmfont == NULL) return NSERROR_INVALID;
143
144 if(utf8_to_local_encoding(string, length, &localtext) != NSERROR_OK) {
145 return NSERROR_INVALID;
146 }
147
148 *width = TextLength(&temp_rp, localtext, (UWORD)strlen(localtext));
149 free(localtext);
150
151 return NSERROR_OK;
152}
153
154/**
155 * Find the position in a string where an x coordinate falls.
156 *
157 * \param fstyle style for this text
158 * \param string UTF-8 string to measure
159 * \param length length of string
160 * \param x x coordinate to search for
161 * \param char_offset updated to offset in string of actual_x, [0..length]
162 * \param actual_x updated to x coordinate of character closest to x
163 * \return true on success, false on error and error reported
164 */
165
167 const char *string, size_t length,
168 int x, size_t *char_offset, int *actual_x)
169{
170 struct TextExtent extent;
171 struct TextFont *bmfont;
172 char *localtext = NULL;
173 UWORD co = 0;
174
175 bmfont = ami_font_bm_open(&temp_rp, fstyle);
176 if(bmfont == NULL) return NSERROR_INVALID;
177
178 if(utf8_to_local_encoding(string, length, &localtext) != NSERROR_OK) {
179 return NSERROR_INVALID;
180 }
181
182 co = TextFit(&temp_rp, localtext, (UWORD)strlen(localtext),
183 &extent, NULL, 1, x, 32767);
184 *char_offset = ami_font_bm_convert_local_to_utf8_offset(string, length, co);
185 *actual_x = extent.te_Extent.MaxX;
186
187 free(localtext);
188
189 return NSERROR_OK;
190}
191
192
193/**
194 * Find where to split a string to make it fit a width.
195 *
196 * \param fstyle style for this text
197 * \param string UTF-8 string to measure
198 * \param length length of string
199 * \param x width available
200 * \param char_offset updated to offset in string of actual_x, [1..length]
201 * \param actual_x updated to x coordinate of character closest to x
202 * \return true on success, false on error and error reported
203 *
204 * On exit, char_offset indicates first character after split point.
205 *
206 * Note: char_offset of 0 should never be returned.
207 *
208 * Returns:
209 * char_offset giving split point closest to x, where actual_x <= x
210 * else
211 * char_offset giving split point closest to x, where actual_x > x
212 *
213 * Returning char_offset == length means no split possible
214 */
215
217 const char *string, size_t length,
218 int x, size_t *char_offset, int *actual_x)
219{
220 struct TextExtent extent;
221 UWORD co, offset;
222 char *charp;
223 char *localtext;
224
225 struct TextFont *bmfont = ami_font_bm_open(&temp_rp, fstyle);
226 if(bmfont == NULL) return NSERROR_INVALID;
227
228 if(utf8_to_local_encoding(string, length, &localtext) != NSERROR_OK) {
229 return NSERROR_INVALID;
230 }
231
232 offset = TextFit(&temp_rp, localtext, (UWORD)strlen(localtext),
233 &extent, NULL, 1, (UWORD)x, 32767);
234
235 co = offset;
236 charp = localtext + co;
237
238
239 while((*charp != ' ') && (co > 0)) {
240 charp--;
241 co--;
242 }
243
244 if(co == 0) {
245 co = offset;
246 charp = localtext + co;
247 while((*charp != ' ') && (co < strlen(localtext))) {
248 charp++;
249 co++;
250 }
251 }
252
253 if((co > 0) && (co < strlen(localtext))) {
254 *actual_x = TextLength(&temp_rp, localtext, co);
255 *char_offset = ami_font_bm_convert_local_to_utf8_offset(string, length, co);
256 } else {
257 *actual_x = x;
258 *char_offset = length;
259 }
260
261 free(localtext);
262
263 return NSERROR_OK;
264}
265
266static ULONG amiga_bm_nsfont_text(struct RastPort *rp, const char *string, ULONG length,
267 const plot_font_style_t *fstyle, ULONG dx, ULONG dy, bool aa)
268{
269 if(!string || string[0]=='\0') return 0;
270 if(!length) return 0;
271 if(rp == NULL) return 0;
272
273 struct TextFont *bmfont = ami_font_bm_open(rp, fstyle);
274 char *localtext = NULL;
275 if(bmfont == NULL) return 0;
276 if(utf8_to_local_encoding(string, length, &localtext) == NSERROR_OK) {
277 Move(rp, dx, dy);
278 Text(rp, localtext, (UWORD)strlen(localtext));
279 free(localtext);
280 }
281
282 return 0;
283}
284
290};
291
293{
294 /* Set up table */
296
297 /* Alloc space to hold currently open font - doesn't matter if this fails */
298 prev_fstyle = calloc(1, sizeof(plot_font_style_t));
299
300 /* Init temp RastPort */
301 InitRastPort(&temp_rp);
302}
303
305{
306 if(prev_font != NULL) {
307 CloseFont(prev_font);
308 prev_font = NULL;
309 }
310
311 if(prev_fstyle != NULL) {
312 free(prev_fstyle);
313 prev_fstyle = NULL;
314 }
315}
316
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_INVALID
Invalid data.
Definition: errors.h:49
@ NSERROR_OK
No error.
Definition: errors.h:30
static nserror amiga_bm_nsfont_width(const plot_font_style_t *fstyle, const char *string, size_t length, int *width)
void ami_font_diskfont_init(void)
static struct RastPort temp_rp
Definition: font_diskfont.c:44
static size_t ami_font_bm_convert_local_to_utf8_offset(const char *utf8string, size_t length, UWORD offset)
static struct TextFont * prev_font
Definition: font_diskfont.c:43
void ami_font_diskfont_fini(void)
static nserror amiga_bm_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.
#define MAX_FONT_NAME_SIZE
Definition: font_diskfont.c:40
static ULONG amiga_bm_nsfont_text(struct RastPort *rp, const char *string, ULONG length, const plot_font_style_t *fstyle, ULONG dx, ULONG dy, bool aa)
static struct TextFont * ami_font_bm_open(struct RastPort *rp, const plot_font_style_t *fstyle)
Definition: font_diskfont.c:46
const struct ami_font_functions ami_font_diskfont_table
static nserror amiga_bm_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.
static plot_font_style_t * prev_fstyle
Definition: font_diskfont.c:42
const struct ami_font_functions * ami_nsfont
Definition: font.h:58
nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
Definition: utf8.c:89
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Minimal compatibility header for AmigaOS 3.
@ FONTF_ITALIC
Definition: plot_style.h:103
@ FONTF_OBLIQUE
Definition: plot_style.h:104
@ 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
int width
Definition: gui.c:159
Interface to utility string handling.
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
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
int weight
Font weight: value in range [100,900] as per CSS.
Definition: plot_style.h:120
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:297
size_t utf8_next(const char *s, size_t l, size_t o)
Find next legal UTF-8 char in string.
Definition: utf8.c:129
UTF-8 manipulation functions (interface).