static void measure_word(fz_context *ctx, fz_html_flow *node, float em) { const char *s; int c, g; float w; em = fz_from_css_number(node->style->font_size, em, em); node->x = 0; node->y = 0; node->h = fz_from_css_number_scale(node->style->line_height, em, em, em); w = 0; s = node->text; while (*s) { s += fz_chartorune(&c, s); g = fz_encode_character(ctx, node->style->font, c); if (g) { w += fz_advance_glyph(ctx, node->style->font, g) * em; } else { g = fz_encode_character(ctx, node->style->fallback, c); w += fz_advance_glyph(ctx, node->style->fallback, g) * em; } } node->w = w; node->em = em; }
static void write_comb_string(fz_context *ctx, fz_buffer *buf, const char *a, const char *b, fz_font *font, float cell_w) { float gw, pad, carry = 0; fz_append_byte(ctx, buf, '['); while (a < b) { int c, g; a += fz_chartorune(&c, a); c = fz_windows_1252_from_unicode(c); if (c < 0) c = REPLACEMENT; g = fz_encode_character(ctx, font, c); gw = fz_advance_glyph(ctx, font, g, 0) * 1000; pad = (cell_w - gw) / 2; fz_append_printf(ctx, buf, "%g", -(carry + pad)); carry = pad; fz_append_byte(ctx, buf, '('); if (c == '(' || c == ')' || c == '\\') fz_append_byte(ctx, buf, '\\'); fz_append_byte(ctx, buf, c); fz_append_byte(ctx, buf, ')'); } fz_append_string(ctx, buf, "] TJ\n"); }
static float break_simple_string(fz_context *ctx, fz_font *font, float size, const char *a, const char **endp, float maxw) { const char *space = NULL; float space_x, x = 0; int c, g; while (*a) { a += fz_chartorune(&c, a); if (c >= 256) c = REPLACEMENT; if (c == '\n' || c == '\r') break; if (c == ' ') { space = a; space_x = x; } g = fz_encode_character(ctx, font, c); x += fz_advance_glyph(ctx, font, g, 0) * size; if (space && x > maxw) return *endp = space, space_x; } return *endp = a, x; }
static void draw_flow_box(fz_context *ctx, fz_html *box, float page_top, float page_bot, fz_device *dev, const fz_matrix *ctm) { fz_html_flow *node; fz_text *text; fz_matrix trm; const char *s; float color[3]; float x, y; int c, g; for (node = box->flow_head; node; node = node->next) { if (node->type == FLOW_IMAGE) { if (node->y > page_bot || node->y + node->h < page_top) continue; } else { if (node->y > page_bot || node->y < page_top) continue; } if (node->type == FLOW_WORD) { fz_scale(&trm, node->em, -node->em); text = fz_new_text(ctx, node->style->font, &trm, 0); x = node->x; y = node->y; s = node->text; while (*s) { s += fz_chartorune(&c, s); g = fz_encode_character(ctx, node->style->font, c); fz_add_text(ctx, text, g, c, x, y); x += fz_advance_glyph(ctx, node->style->font, g) * node->em; } color[0] = node->style->color.r / 255.0f; color[1] = node->style->color.g / 255.0f; color[2] = node->style->color.b / 255.0f; fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color, 1); fz_drop_text(ctx, text); } else if (node->type == FLOW_IMAGE) { fz_matrix local_ctm = *ctm; fz_pre_translate(&local_ctm, node->x, node->y); fz_pre_scale(&local_ctm, node->w, node->h); fz_fill_image(ctx, dev, node->image, &local_ctm, 1); } } }
static float measure_simple_string(fz_context *ctx, fz_font *font, const char *text) { float w = 0; while (*text) { int c, g; text += fz_chartorune(&c, text); c = fz_windows_1252_from_unicode(c); if (c < 0) c = REPLACEMENT; g = fz_encode_character(ctx, font, c); w += fz_advance_glyph(ctx, font, g, 0); } return w; }
static void draw_flow_box(fz_context *ctx, fz_html *box, float page_top, float page_bot, fz_device *dev, const fz_matrix *ctm) { fz_html_flow *node; fz_text *text; fz_matrix trm; const char *s; float color[3]; int c, g; for (node = box->flow_head; node; node = node->next) { if (node->type == FLOW_IMAGE) { if (node->y >= page_bot || node->y + node->h <= page_top) continue; } else { if (node->y > page_bot || node->y < page_top) continue; } if (node->type == FLOW_WORD) { fz_scale(&trm, node->em, -node->em); color[0] = node->style->color.r / 255.0f; color[1] = node->style->color.g / 255.0f; color[2] = node->style->color.b / 255.0f; /* TODO: reuse text object if color is unchanged */ text = fz_new_text(ctx); trm.e = node->x; trm.f = node->y; s = node->text; while (*s) { s += fz_chartorune(&c, s); g = fz_encode_character(ctx, node->style->font, c); if (g) { if (node->style->visibility == V_VISIBLE) fz_add_text(ctx, text, node->style->font, 0, &trm, g, c); trm.e += fz_advance_glyph(ctx, node->style->font, g) * node->em; } else { g = fz_encode_character(ctx, node->style->fallback, c); if (g) { if (node->style->visibility == V_VISIBLE) fz_add_text(ctx, text, node->style->fallback, 0, &trm, g, c); trm.e += fz_advance_glyph(ctx, node->style->fallback, g) * node->em; } else { g = fz_encode_character(ctx, node->style->font, 0x25CF); /* bullet */ if (node->style->visibility == V_VISIBLE) fz_add_text(ctx, text, node->style->font, 0, &trm, g, c); trm.e += fz_advance_glyph(ctx, node->style->font, g) * node->em; } } } if (text) { fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color, 1); fz_drop_text(ctx, text); } } else if (node->type == FLOW_IMAGE) { if (node->style->visibility == V_VISIBLE) { fz_matrix local_ctm = *ctm; fz_pre_translate(&local_ctm, node->x, node->y); fz_pre_scale(&local_ctm, node->w, node->h); fz_fill_image(ctx, dev, node->image, &local_ctm, 1); } } } }
static void draw_list_mark(fz_context *ctx, fz_html *box, float page_top, float page_bot, fz_device *dev, const fz_matrix *ctm, int n) { fz_text *text; fz_matrix trm; fz_html_flow *line; float y, w; float color[3]; const char *s; char buf[40]; int c, g; fz_scale(&trm, box->em, -box->em); text = fz_new_text(ctx); line = find_list_mark_anchor(ctx, box); if (line) { y = line->y; } else { float h = fz_from_css_number_scale(box->style.line_height, box->em, box->em, box->em); float a = box->em * 0.8; float d = box->em * 0.2; if (a + d > h) h = a + d; y = box->y + a + (h - a - d) / 2; } if (y > page_bot || y < page_top) return; format_list_number(ctx, box->style.list_style_type, n, buf, sizeof buf); s = buf; w = 0; while (*s) { s += fz_chartorune(&c, s); g = fz_encode_character(ctx, box->style.font, c); w += fz_advance_glyph(ctx, box->style.font, g) * box->em; } s = buf; trm.e = box->x - w; trm.f = y; while (*s) { s += fz_chartorune(&c, s); g = fz_encode_character(ctx, box->style.font, c); fz_add_text(ctx, text, box->style.font, 0, &trm, g, c); trm.e += fz_advance_glyph(ctx, box->style.font, g) * box->em; } color[0] = box->style.color.r / 255.0f; color[1] = box->style.color.g / 255.0f; color[2] = box->style.color.b / 255.0f; fz_fill_text(ctx, dev, text, ctm, fz_device_rgb(ctx), color, 1); fz_drop_text(ctx, text); }