Skip to content

Commit 9219179

Browse files
committed
Separate sync and async font funcs
1 parent 32b77ea commit 9219179

10 files changed

Lines changed: 174 additions & 76 deletions

File tree

call.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ static void commandVersion() {
1313
// exit
1414

1515
static void commandExit(pipe_buffer *target) {
16-
static int exitCount = 0;
17-
if (exitCount++ == 0) {
16+
if (!is_sync_pipe_buffer(target)) {
1817
top_signal_disconnect();
1918
}
2019
io_stop(target);
@@ -148,8 +147,13 @@ static struct {
148147

149148
_Static_assert(sizeof font == 12, "wrong font align");
150149

151-
static void setFont(void *family) {
150+
static void setFontElem(void *family) {
152151
font_elem_add(font.id, font.height, family, font.style, font.variant, font.weight, font.stretch);
152+
g_free(family);
153+
}
154+
155+
static void setFontMetric(void *family) {
156+
font_metric_add(font.id, font.height, family, font.style, font.variant, font.weight, font.stretch);
153157
int16_t lineheight, baseline, ascent, descent;
154158
get_font_metrics(font.id, &lineheight, &baseline, &ascent, &descent);
155159
pipe_output_write(&lineheight, sizeof lineheight);
@@ -160,7 +164,13 @@ static void setFont(void *family) {
160164
g_free(family);
161165
}
162166

163-
static void commandFont(pipe_buffer *target) { parameters_alloc_to_call(target, &font, sizeof font, setFont); }
167+
static void commandFont(pipe_buffer *target) {
168+
if (is_sync_pipe_buffer(target)) {
169+
parameters_alloc_to_call(target, &font, sizeof font, setFontMetric);
170+
} else {
171+
parameters_alloc_to_call(target, &font, sizeof font, setFontElem);
172+
}
173+
}
164174

165175
// remove font
166176

@@ -170,9 +180,17 @@ static struct {
170180

171181
_Static_assert(sizeof fontid == 2, "wrong fontid align");
172182

173-
static void setFontRem() { font_elem_rem(fontid.id); }
183+
static void setFontElemRem() { font_elem_rem(fontid.id); }
184+
185+
static void setFontMetricRem() { font_metric_rem(fontid.id); }
174186

175-
static void commandFontRem(pipe_buffer *target) { parameters_to_call(target, &fontid, sizeof fontid, setFontRem); }
187+
static void commandFontRem(pipe_buffer *target) {
188+
if (is_sync_pipe_buffer(target)) {
189+
parameters_to_call(target, &fontid, sizeof fontid, setFontMetricRem);
190+
} else {
191+
parameters_to_call(target, &fontid, sizeof fontid, setFontElemRem);
192+
}
193+
}
176194

177195
// split text
178196

@@ -185,7 +203,7 @@ static struct {
185203
_Static_assert(sizeof split == 6, "wrong split align");
186204

187205
static void splitText(void *text) {
188-
int16_t *out = font_split_text(split.fontid, text, split.edge, split.indent);
206+
int16_t *out = font_metric_split_text(split.fontid, text, split.edge, split.indent);
189207
if (out == NULL) {
190208
int16_t value = 0;
191209
pipe_output_write(&value, sizeof value);
@@ -209,7 +227,7 @@ static struct {
209227

210228
static void rectText(void *text) {
211229
int16_t width, height;
212-
font_rect_text(textrect.fontid, text, &width, &height);
230+
font_metric_rect_text(textrect.fontid, text, &width, &height);
213231
pipe_output_write(&width, sizeof width);
214232
pipe_output_write(&height, sizeof width);
215233
pipe_output_flush();

draw.c

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,11 @@ typedef struct {
155155
int height;
156156
PangoFontDescription *desc;
157157
PangoLayout *layout;
158-
PangoLayout *split_layout;
159158
} font_elem;
160159

161-
static GHashTable *font_table = NULL;
162-
163160
static PangoContext *top_pango_context = NULL;
164161

165-
void font_elem_add(int id, int height, char *family, int style, int variant, int weight, int stretch) {
162+
static font_elem *font_elem_new(int height, char *family, int style, int variant, int weight, int stretch) {
166163
if (top_pango_context == NULL) {
167164
top_pango_context = gtk_widget_get_pango_context(top);
168165
}
@@ -177,27 +174,51 @@ void font_elem_add(int id, int height, char *family, int style, int variant, int
177174
pango_font_description_set_stretch(e->desc, stretch);
178175
e->layout = pango_layout_new(top_pango_context);
179176
pango_layout_set_font_description(e->layout, e->desc);
180-
e->split_layout = pango_layout_new(top_pango_context);
181-
pango_layout_set_font_description(e->split_layout, e->desc);
182-
pango_layout_set_wrap(e->split_layout, PANGO_WRAP_WORD_CHAR);
183-
if (font_table == NULL) {
184-
font_table = g_hash_table_new(g_direct_hash, g_direct_equal);
185-
}
186-
g_hash_table_insert(font_table, GINT_TO_POINTER(id), e);
177+
return e;
187178
}
188179

189-
void font_elem_rem(int id) {
190-
font_elem *e = g_hash_table_lookup(font_table, GINT_TO_POINTER(id));
191-
g_assert(e);
192-
g_hash_table_remove(font_table, GINT_TO_POINTER(id));
193-
g_object_unref(e->split_layout);
180+
static void font_elem_free(font_elem *e) {
194181
g_object_unref(e->layout);
195182
pango_font_description_free(e->desc);
196183
g_free(e);
197184
}
198185

186+
static GHashTable *font_elem_table = NULL;
187+
188+
void font_elem_add(int id, int height, char *family, int style, int variant, int weight, int stretch) {
189+
font_elem *e = font_elem_new(height, family, style, variant, weight, stretch);
190+
if (font_elem_table == NULL) {
191+
font_elem_table = g_hash_table_new(g_direct_hash, g_direct_equal);
192+
}
193+
g_hash_table_insert(font_elem_table, GINT_TO_POINTER(id), e);
194+
}
195+
196+
void font_elem_rem(int id) {
197+
font_elem *e = g_hash_table_lookup(font_elem_table, GINT_TO_POINTER(id));
198+
g_assert(e);
199+
g_hash_table_remove(font_elem_table, GINT_TO_POINTER(id));
200+
font_elem_free(e);
201+
}
202+
203+
static GHashTable *font_metric_table = NULL;
204+
205+
void font_metric_add(int id, int height, char *family, int style, int variant, int weight, int stretch) {
206+
font_elem *e = font_elem_new(height, family, style, variant, weight, stretch);
207+
if (font_metric_table == NULL) {
208+
font_metric_table = g_hash_table_new(g_direct_hash, g_direct_equal);
209+
}
210+
g_hash_table_insert(font_metric_table, GINT_TO_POINTER(id), e);
211+
}
212+
213+
void font_metric_rem(int id) {
214+
font_elem *e = g_hash_table_lookup(font_metric_table, GINT_TO_POINTER(id));
215+
g_assert(e);
216+
g_hash_table_remove(font_metric_table, GINT_TO_POINTER(id));
217+
font_elem_free(e);
218+
}
219+
199220
void get_font_metrics(int fontid, int16_t *lineheight, int16_t *baseline, int16_t *ascent, int16_t *descent) {
200-
font_elem *f = g_hash_table_lookup(font_table, GINT_TO_POINTER(fontid));
221+
font_elem *f = g_hash_table_lookup(font_metric_table, GINT_TO_POINTER(fontid));
201222
g_assert(f);
202223
*baseline = (int16_t)(pango_layout_get_baseline(f->layout) / PANGO_SCALE);
203224
PangoFontMetrics *metrics = pango_context_get_metrics(pango_layout_get_context(f->layout), f->desc, NULL);
@@ -209,13 +230,14 @@ void get_font_metrics(int fontid, int16_t *lineheight, int16_t *baseline, int16_
209230

210231
// text split
211232

212-
int16_t *font_split_text(int fontid, char *text, int edge, int indent) {
213-
font_elem *f = g_hash_table_lookup(font_table, GINT_TO_POINTER(fontid));
233+
int16_t *font_metric_split_text(int fontid, char *text, int edge, int indent) {
234+
font_elem *f = g_hash_table_lookup(font_metric_table, GINT_TO_POINTER(fontid));
214235
g_assert(f);
215-
pango_layout_set_width(f->split_layout, PANGO_SCALE * edge);
216-
pango_layout_set_indent(f->split_layout, PANGO_SCALE * indent);
217-
pango_layout_set_text(f->split_layout, text, -1);
218-
GSList *top = pango_layout_get_lines_readonly(f->split_layout);
236+
pango_layout_set_wrap(f->layout, PANGO_WRAP_WORD_CHAR);
237+
pango_layout_set_width(f->layout, PANGO_SCALE * edge);
238+
pango_layout_set_indent(f->layout, PANGO_SCALE * indent);
239+
pango_layout_set_text(f->layout, text, -1);
240+
GSList *top = pango_layout_get_lines_readonly(f->layout);
219241
int16_t length = 0;
220242
for (GSList *e = top; e != NULL; e = e->next) {
221243
length++;
@@ -227,13 +249,14 @@ int16_t *font_split_text(int fontid, char *text, int edge, int indent) {
227249
PangoLayoutLine *line = e->data;
228250
*pos++ = (int16_t)(line->length);
229251
}
252+
pango_layout_set_wrap(f->layout, PANGO_WRAP_NONE);
230253
return out;
231254
}
232255

233256
// text rect
234257

235-
void font_rect_text(int fontid, char *text, int16_t *width, int16_t *height) {
236-
font_elem *f = g_hash_table_lookup(font_table, GINT_TO_POINTER(fontid));
258+
void font_metric_rect_text(int fontid, char *text, int16_t *width, int16_t *height) {
259+
font_elem *f = g_hash_table_lookup(font_metric_table, GINT_TO_POINTER(fontid));
237260
g_assert(f);
238261
pango_layout_set_text(f->layout, text, -1);
239262
int w, h;
@@ -262,7 +285,7 @@ static inline void elem_text_draw(cairo_t *cr, text_elem *e) {
262285
static inline void elem_text_destroy(text_elem *e) { g_free(e->text); }
263286

264287
void elem_text_add(int id, double x, double y, char *text, int fontid, double r, double g, double b, double a) {
265-
font_elem *f = g_hash_table_lookup(font_table, GINT_TO_POINTER(fontid));
288+
font_elem *f = g_hash_table_lookup(font_elem_table, GINT_TO_POINTER(fontid));
266289
g_assert(f);
267290
text_elem *e = g_malloc(sizeof(text_elem));
268291
e->type = DRAW_ELEM_TEXT;

four/call.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ static void commandVersion() {
1313
// exit
1414

1515
static void commandExit(pipe_buffer *target) {
16-
static int exitCount = 0;
17-
if (exitCount++ == 0) {
16+
if (!is_sync_pipe_buffer(target)) {
1817
top_signal_disconnect();
1918
}
2019
io_stop(target);
@@ -148,8 +147,13 @@ static struct {
148147

149148
_Static_assert(sizeof font == 12, "wrong font align");
150149

151-
static void setFont(void *family) {
150+
static void setFontElem(void *family) {
152151
font_elem_add(font.id, font.height, family, font.style, font.variant, font.weight, font.stretch);
152+
g_free(family);
153+
}
154+
155+
static void setFontMetric(void *family) {
156+
font_metric_add(font.id, font.height, family, font.style, font.variant, font.weight, font.stretch);
153157
int16_t lineheight, baseline, ascent, descent;
154158
get_font_metrics(font.id, &lineheight, &baseline, &ascent, &descent);
155159
pipe_output_write(&lineheight, sizeof lineheight);
@@ -160,7 +164,13 @@ static void setFont(void *family) {
160164
g_free(family);
161165
}
162166

163-
static void commandFont(pipe_buffer *target) { parameters_alloc_to_call(target, &font, sizeof font, setFont); }
167+
static void commandFont(pipe_buffer *target) {
168+
if (is_sync_pipe_buffer(target)) {
169+
parameters_alloc_to_call(target, &font, sizeof font, setFontMetric);
170+
} else {
171+
parameters_alloc_to_call(target, &font, sizeof font, setFontElem);
172+
}
173+
}
164174

165175
// remove font
166176

@@ -170,9 +180,17 @@ static struct {
170180

171181
_Static_assert(sizeof fontid == 2, "wrong fontid align");
172182

173-
static void setFontRem() { font_elem_rem(fontid.id); }
183+
static void setFontElemRem() { font_elem_rem(fontid.id); }
184+
185+
static void setFontMetricRem() { font_metric_rem(fontid.id); }
174186

175-
static void commandFontRem(pipe_buffer *target) { parameters_to_call(target, &fontid, sizeof fontid, setFontRem); }
187+
static void commandFontRem(pipe_buffer *target) {
188+
if (is_sync_pipe_buffer(target)) {
189+
parameters_to_call(target, &fontid, sizeof fontid, setFontMetricRem);
190+
} else {
191+
parameters_to_call(target, &fontid, sizeof fontid, setFontElemRem);
192+
}
193+
}
176194

177195
// split text
178196

@@ -185,7 +203,7 @@ static struct {
185203
_Static_assert(sizeof split == 6, "wrong split align");
186204

187205
static void splitText(void *text) {
188-
int16_t *out = font_split_text(split.fontid, text, split.edge, split.indent);
206+
int16_t *out = font_metric_split_text(split.fontid, text, split.edge, split.indent);
189207
if (out == NULL) {
190208
int16_t value = 0;
191209
pipe_output_write(&value, sizeof value);
@@ -209,7 +227,7 @@ static struct {
209227

210228
static void rectText(void *text) {
211229
int16_t width, height;
212-
font_rect_text(textrect.fontid, text, &width, &height);
230+
font_metric_rect_text(textrect.fontid, text, &width, &height);
213231
pipe_output_write(&width, sizeof width);
214232
pipe_output_write(&height, sizeof width);
215233
pipe_output_flush();

0 commit comments

Comments
 (0)