Skip to content

Commit 82b9f57

Browse files
committed
Merge branch 'release/1.1.0'
2 parents b337e44 + 2c49652 commit 82b9f57

File tree

8 files changed

+51
-40
lines changed

8 files changed

+51
-40
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.0)
22
cmake_policy(SET CMP0063 NEW)
3-
project(font-chef LANGUAGES C CXX VERSION 1.0.2)
3+
project(font-chef LANGUAGES C CXX VERSION 1.1.0)
44
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake-modules)
55

66
set(CMAKE_C_STANDARD 99)

include/font-chef/character-mapping.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ FONT_CHEF_EXPORT struct fc_rect fc_text_bounds(struct fc_character_mapping const
8484
* @param line_height The space between the topmost pixel in the line to the bottomost pixel in the line (this includes characters in the line itself)
8585
* @param space_width The width of a space character
8686
* @param alignment Which aligment should lines follow
87+
* @return The line count in the text
8788
* @sa ::fc_render_wrapped
8889
* @sa fc::render_result::wrap
8990
* @sa fc_get_space_metrics
9091
*/
91-
FONT_CHEF_EXPORT extern void fc_wrap(
92+
FONT_CHEF_EXPORT extern uint32_t fc_wrap(
9293
struct fc_character_mapping mapping[],
9394
size_t count,
9495
float line_width,

include/font-chef/font.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ struct fc_pixels {
4343
struct fc_size dimensions;
4444
};
4545

46+
/**
47+
* @brief A structure holding the result of a call to `fc_render` or `fc_render_wrapped`
48+
*/
49+
struct fc_render_result {
50+
/**
51+
* @brief How many lines were produced
52+
*/
53+
uint32_t line_count;
54+
55+
/**
56+
* @brief How many glphs were produced
57+
*/
58+
uint32_t glyph_count;
59+
};
60+
4661

4762
/**
4863
* @struct fc_font
@@ -191,9 +206,10 @@ FONT_CHEF_EXPORT extern void fc_cook(struct fc_font * font);
191206
* @param byte_count How many bytes are there in the character array
192207
* @param mapping An array of `fc_character_mapping` values that
193208
* must be at least `byte_count` long.
194-
* @return the actual number of glyphs to be rendered.
209+
* @return how many glyphs and lines were produced
210+
* @sa ::fc_render_result
195211
*/
196-
FONT_CHEF_EXPORT extern int fc_render(
212+
FONT_CHEF_EXPORT extern struct fc_render_result fc_render(
197213
struct fc_font const * font,
198214
unsigned char const * text,
199215
size_t byte_count,
@@ -216,11 +232,12 @@ FONT_CHEF_EXPORT extern int fc_render(
216232
* @param alignment Which aligment should lines follow
217233
* @param mapping An array of `fc_character_mapping` values that
218234
* must be at least `byte_count` long.
235+
* @return how many glyphs and lines were produced
219236
* @sa ::fc_render
220237
* @sa ::fc_wrap
221238
* @sa fc::render_result::wrap
222239
*/
223-
FONT_CHEF_EXPORT int fc_render_wrapped(
240+
FONT_CHEF_EXPORT struct fc_render_result fc_render_wrapped(
224241
struct fc_font const * font,
225242
unsigned char const * text,
226243
size_t byte_count,

include/font-chef/font.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ namespace fc {
209209
result.font = data;
210210
std::vector<fc_character_mapping> & mapping = result.mapping;
211211
if (mapping.size() < text.length()) mapping.resize(text.length());
212-
size_t count = fc_render(data, (uint8_t *) text.data(), text.size(), mapping.data());
213-
mapping.resize(count);
212+
struct fc_render_result r = fc_render(data, (uint8_t *) text.data(), text.size(), mapping.data());
213+
mapping.resize(r.glyph_count);
214+
result.line_count = r.line_count;
214215
return result;
215216
}
216217
};

include/font-chef/render-result.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "font-chef/character-mapping.h"
55
#include "font-chef/font.h"
66
#include <vector>
7-
#include <cstdio>
87

98

109
/**
@@ -33,6 +32,11 @@ namespace fc {
3332
*/
3433
std::vector<::fc_character_mapping> mapping;
3534

35+
/**
36+
* @brief How many lines were produced
37+
*/
38+
uint32_t line_count;
39+
3640
/**
3741
* @brief A pointer to the ::fc_font used to generate this mapping
3842
*/
@@ -49,16 +53,15 @@ namespace fc {
4953
* @sa fc::font::render
5054
*/
5155
render_result(fc_font * font = nullptr, std::vector<fc_character_mapping> && mapping = {}) //NOLINT
52-
: mapping(mapping), font(font) {
53-
::printf("construct render result");
56+
: mapping(mapping), font(font), line_count(0) {
5457
};
5558

5659
/**
5760
* @brief Move constructor of fc::render_result.
5861
* @param other A rvalue (moveable) ref to a fc::render_result instance
5962
*/
6063
render_result(render_result && other) noexcept
61-
: mapping(std::move(other.mapping)), font(other.font) {
64+
: mapping(std::move(other.mapping)), font(other.font), line_count(other.line_count) {
6265
other.font = nullptr;
6366
};
6467

@@ -71,6 +74,7 @@ namespace fc {
7174
if (this == &other) return *this;
7275
this->mapping = std::move(other.mapping);
7376
this->font = other.font;
77+
this->line_count = other.line_count;
7478
other.font = nullptr;
7579
return *this;
7680
}
@@ -111,7 +115,7 @@ namespace fc {
111115
wrap(float line_width, float line_height_multiplier = 1.0f, fc_alignment alignment = fc_align_left) & {
112116
if (!font) return *this;
113117
fc_size space_metrics = fc_get_space_metrics(font);
114-
fc_wrap(
118+
this->line_count = fc_wrap(
115119
mapping.data(),
116120
mapping.size(),
117121
line_width,

src/examples/c/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ int main(int argc, char const ** argv) {
2020

2121
struct fc_character_mapping mapping[256];
2222
const char text[] = "Hello, handsome! I mean... world!";
23-
int txt_glyph_count = fc_render_wrapped(
23+
struct fc_render_result result = fc_render_wrapped(
2424
font,
2525
(uint8_t *) text,
2626
strlen(text),
2727
state.bounds.right * 0.66f,
2828
1.0f,
2929
fc_align_left, mapping
3030
);
31-
fc_move(mapping, txt_glyph_count, 0.0f, state.bounds.bottom / 2 - 20);
31+
fc_move(mapping, result.glyph_count, 0.0f, state.bounds.bottom / 2 - 20);
3232

3333
while (update(&state)) {
34-
for (int i = 0; i < txt_glyph_count; i++) {
34+
for (int i = 0; i < result.glyph_count; i++) {
3535
render(font_texture, mapping[i].source, mapping[i].target);
3636
}
3737
}

src/font-chef/font.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include <font-chef/character-mapping.h>
77

88

9-
#define fc_fabs(x) ((float) fabs((double) x))
10-
119
struct fc_font * fc_construct(
1210
uint8_t const * font_data,
1311
struct fc_font_size font_size,
@@ -104,7 +102,7 @@ void fc_cook(struct fc_font * font) {
104102
free(pixels_1bpp);
105103
}
106104

107-
int fc_render(
105+
struct fc_render_result fc_render(
108106
struct fc_font const * font,
109107
unsigned char const * text,
110108
size_t byte_count,
@@ -154,11 +152,6 @@ int fc_render(
154152

155153
m->codepoint = decode.codepoint;
156154

157-
/* stores the top offset to add it later to the mappings, to make sure
158-
* that rendering starts at 0 */
159-
// if ((dst->top < top_offset) || target_index == 0)
160-
// top_offset = dst->top;
161-
162155
/* checks to see if there is kerning to add to the next character, and
163156
* sets it to be used in the next iteration */
164157
kern = 0;
@@ -171,18 +164,13 @@ int fc_render(
171164
}
172165
}
173166

174-
/* characters are rendered around a baseline, which was set to 0 when
175-
* fc_cook was called. the following loop goes through all glyph
176-
* mappings and adjusts them to remove the "empty" space on top caused
177-
* by that. */
178-
// top_offset = fc_fabs(top_offset);
179-
// for (size_t i = 0; i < target_index; i++) {
180-
// mapping[i].target.top += top_offset;
181-
// mapping[i].target.bottom += top_offset;
182-
// }
183-
184167
/* end of the loop, target_index will be the amount of decoded glyphs */
185-
return (int) target_index;
168+
struct fc_render_result result = {
169+
.line_count = 1,
170+
.glyph_count = (uint32_t) target_index
171+
};
172+
173+
return result;
186174
}
187175

188176
struct fc_font_size fc_get_font_size(struct fc_font const * font) {
@@ -229,7 +217,7 @@ void fc_destruct(struct fc_font * font) {
229217
free(font);
230218
}
231219

232-
int fc_render_wrapped(
220+
struct fc_render_result fc_render_wrapped(
233221
struct fc_font const * font,
234222
unsigned char const * text,
235223
size_t byte_count,
@@ -238,10 +226,10 @@ int fc_render_wrapped(
238226
enum fc_alignment alignment,
239227
struct fc_character_mapping * mapping
240228
) {
241-
int rendered = fc_render(font, text, byte_count, mapping);
229+
struct fc_render_result result = fc_render(font, text, byte_count, mapping);
242230
struct fc_size space_metrics = fc_get_space_metrics(font);
243-
fc_wrap(mapping, rendered, (float) line_width, font->metrics.line_height * line_height_multiplier, space_metrics.width, alignment);
244-
return rendered;
231+
result.line_count = fc_wrap(mapping, result.glyph_count, (float) line_width, font->metrics.line_height * line_height_multiplier, space_metrics.width, alignment);
232+
return result;
245233
}
246234

247235
struct fc_size fc_get_space_metrics(struct fc_font const * font) {

src/font-chef/render-result.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ float fc_text_segment_width(struct fc_text_segment const * segment, struct fc_ch
2929
return mapping[segment->last].target.right - mapping[segment->first].target.left;
3030
}
3131

32-
void fc_wrap(struct fc_character_mapping mapping[], size_t glyph_count, float line_width, float line_height, float space_width, enum fc_alignment aligment) {
32+
uint32_t fc_wrap(struct fc_character_mapping mapping[], size_t glyph_count, float line_width, float line_height, float space_width, enum fc_alignment aligment) {
3333
struct fc_text_segment * words = calloc(sizeof(*words), glyph_count);
3434
struct fc_text_segment * lines = calloc(sizeof(*lines), glyph_count);
3535
size_t word_count = 0;
@@ -67,7 +67,6 @@ void fc_wrap(struct fc_character_mapping mapping[], size_t glyph_count, float li
6767
current_word->last = i;
6868
word_count += 1;
6969
}
70-
7170
}
7271

7372
/* identify lines */
@@ -123,6 +122,7 @@ void fc_wrap(struct fc_character_mapping mapping[], size_t glyph_count, float li
123122
}
124123
free(words);
125124
free(lines);
125+
return line_count;
126126
}
127127

128128
void fc_move(struct fc_character_mapping * mapping, size_t count, float left, float baseline) {

0 commit comments

Comments
 (0)