1 /*
2  * This file is part of LibCSS.
3  * Licensed under the MIT License,
4  *                http://www.opensource.org/licenses/mit-license.php
5  * Copyright 2011 Things Made Out Of Other Things Ltd.
6  * Written by James Montgomerie <jamie@th.ingsmadeoutofotherthin.gs>
7  */
8 
9 #include <string.h>
10 
11 #include "select/font_face.h"
12 
font_faces_srcs_destroy(css_font_face * font_face)13 static void font_faces_srcs_destroy(css_font_face *font_face)
14 {
15 	uint32_t i;
16 	css_font_face_src *srcs = font_face->srcs;
17 
18 	for (i = 0; i < font_face->n_srcs; ++i) {
19 		if (srcs[i].location != NULL) {
20 			lwc_string_unref(srcs[i].location);
21 		}
22 	}
23 
24 	free(srcs);
25 	font_face->srcs = NULL;
26 }
27 
28 static const css_font_face default_font_face = {
29 	NULL,
30 	NULL,
31 	0,
32 	{ (CSS_FONT_WEIGHT_NORMAL << 2) | CSS_FONT_STYLE_NORMAL }
33 };
34 
35 /**
36  * Create a font-face
37  *
38  * \param result  Pointer to location to receive result
39  * \return CSS_OK on success,
40  *         CSS_NOMEM on memory exhaustion,
41  *         CSS_BADPARM on bad parameters.
42  */
css__font_face_create(css_font_face ** result)43 css_error css__font_face_create(css_font_face **result)
44 {
45 	css_font_face *f;
46 
47 	if (result == NULL)
48 		return CSS_BADPARM;
49 
50 	f = malloc(sizeof(css_font_face));
51 	if (f == NULL)
52 		return CSS_NOMEM;
53 
54 	memcpy(f, &default_font_face, sizeof(css_font_face));
55 
56 	*result = f;
57 
58 	return CSS_OK;
59 }
60 
61 /**
62  * Destroy a font-face
63  *
64  * \param font_face  Font-face to destroy
65  * \return CSS_OK on success, appropriate error otherwise
66  */
css__font_face_destroy(css_font_face * font_face)67 css_error css__font_face_destroy(css_font_face *font_face)
68 {
69 	if (font_face == NULL)
70 		return CSS_BADPARM;
71 
72 	if (font_face->font_family != NULL)
73 		lwc_string_unref(font_face->font_family);
74 
75 	if (font_face->srcs != NULL)
76 		font_faces_srcs_destroy(font_face);
77 
78 	free(font_face);
79 
80 	return CSS_OK;
81 }
82 
83 
84 /**
85  * Set a font-face's font-family name
86  *
87  * \param font_face    The font-face
88  * \param font_family  Font-family name
89  * \param result       Pointer to location to receive result
90  * \return CSS_OK on success,
91  *         CSS_BADPARM on bad parameters.
92  */
css__font_face_set_font_family(css_font_face * font_face,lwc_string * font_family)93 css_error css__font_face_set_font_family(css_font_face *font_face,
94 		lwc_string *font_family)
95 {
96 	if (font_face == NULL || font_family == NULL)
97 		return CSS_BADPARM;
98 
99 	if (font_face->font_family != NULL)
100 		lwc_string_unref(font_face->font_family);
101 
102 	font_face->font_family = lwc_string_ref(font_family);
103 
104 	return CSS_OK;
105 }
106 
107 /**
108  * Get a font-face's font-family name
109  *
110  * \param font_face  The font-face
111  * \param result     Pointer to location to receive result
112  * \return CSS_OK on success,
113  *         CSS_BADPARM on bad parameters.
114  */
css_font_face_get_font_family(const css_font_face * font_face,lwc_string ** font_family)115 css_error css_font_face_get_font_family(const css_font_face *font_face,
116 		lwc_string **font_family)
117 {
118 	if (font_face == NULL || font_family == NULL)
119 		return CSS_BADPARM;
120 
121 	*font_family = font_face->font_family;
122 
123 	return CSS_OK;
124 }
125 
126 /**
127  * Get the style of font for a font-face.
128  *
129  * \param src  The font-face
130  * \return The style, as a css_font_style_e
131  */
css_font_face_font_style(const css_font_face * font_face)132 uint8_t css_font_face_font_style(const css_font_face *font_face)
133 {
134 	return font_face->bits[0] & 0x3;
135 }
136 
137 /**
138  * Get the weight of font for a font-face.
139  *
140  * \param src  The font-face
141  * \return The style, as a css_font_weight_e
142  */
css_font_face_font_weight(const css_font_face * font_face)143 uint8_t css_font_face_font_weight(const css_font_face *font_face)
144 {
145 	return (font_face->bits[0] >> 2) & 0xf;
146 }
147 
148 /**
149  * Get the number of potential src locations for a font-face
150  *
151  * \param font_face  The font-face
152  * \param count      Pointer to location to receive result
153  * \return CSS_OK on success,
154  *         CSS_BADPARM on bad parameters.
155  */
css_font_face_count_srcs(const css_font_face * font_face,uint32_t * count)156 css_error css_font_face_count_srcs(const css_font_face *font_face,
157 		uint32_t *count)
158 {
159 	if (font_face == NULL || count == NULL)
160 		return CSS_BADPARM;
161 
162 	*count = font_face->n_srcs;
163 	return CSS_OK;
164 }
165 
166 /**
167  * Get a specific src location from a font-face
168  *
169  * \param font_face  The font-face
170  * \param index	     The index for the wanted src.
171  * \param count      Pointer to location to receive result
172  * \return CSS_OK on success,
173  *         CSS_BADPARM on bad parameters.
174  */
css_font_face_get_src(const css_font_face * font_face,uint32_t index,const css_font_face_src ** src)175 css_error css_font_face_get_src(const css_font_face *font_face,
176 		uint32_t index, const css_font_face_src **src)
177 {
178 	if (font_face == NULL || src == NULL || index >= font_face->n_srcs)
179 		return CSS_BADPARM;
180 
181 	*src = &(font_face->srcs[index]);
182 
183 	return CSS_OK;
184 }
185 
186 /**
187  * Get the location for a font-face src.
188  *
189  * \param font_face  The font-face
190  * \param count      Pointer to location to receive result
191  * \return CSS_OK on success,
192  *         CSS_BADPARM on bad parameters.
193  *
194  * \note  The type of location (local or URL) can be gathered from
195  *        css_font_face_src_location_type, and the format of font (if specified)
196  *        from css_font_face_src_format.
197  */
css_font_face_src_get_location(const css_font_face_src * src,lwc_string ** location)198 css_error css_font_face_src_get_location(const css_font_face_src *src,
199 		lwc_string **location)
200 {
201 	if (src == NULL || location == NULL)
202 		return CSS_BADPARM;
203 
204 	*location = src->location;
205 
206 	return CSS_OK;
207 }
208 
209 /**
210  * Get the location type for a font-face src.
211  *
212  * \param src  The font-face src
213  * \return The location type
214  */
css_font_face_src_location_type(const css_font_face_src * src)215 css_font_face_location_type css_font_face_src_location_type(
216 		const css_font_face_src *src)
217 {
218 	return src->bits[0] & 0x3;
219 }
220 
221 /**
222  * Get the format of font for a font-face src.
223  *
224  * \param src  The font-face src
225  * \return The format, if specified
226  */
css_font_face_src_format(const css_font_face_src * src)227 css_font_face_format css_font_face_src_format(const css_font_face_src *src)
228 {
229 	return (src->bits[0] >> 2) & 0x1f;
230 }
231 
232 /**
233  * Set a font-faces array of srcs.
234  *
235  * \param font_face  The font-face
236  * \param srcs	     The array of css_font_face_srcs
237  * \param n_srcs     The count of css_font_face_srcs in the array
238  * \return The format, if specified
239  */
css__font_face_set_srcs(css_font_face * font_face,css_font_face_src * srcs,uint32_t n_srcs)240 css_error css__font_face_set_srcs(css_font_face *font_face,
241 		css_font_face_src *srcs, uint32_t n_srcs)
242 {
243 	if (font_face->srcs != NULL)
244 		font_faces_srcs_destroy(font_face);
245 
246 	font_face->srcs = srcs;
247 	font_face->n_srcs = n_srcs;
248 
249 	return CSS_OK;
250 }
251 
252 
253