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 float ui_draw_glyph(fz_font *font, int gid, float x, float y) { struct glyph *glyph; float s0, t0, s1, t1, xc, yc; glyph = lookup_glyph(font, gid, &x, &y); if (!glyph) return 0; s0 = (float) glyph->s / g_cache_w; t0 = (float) glyph->t / g_cache_h; s1 = (float) (glyph->s + glyph->w) / g_cache_w; t1 = (float) (glyph->t + glyph->h) / g_cache_h; xc = floorf(x) + glyph->lsb; yc = floorf(y) - glyph->top + glyph->h; glTexCoord2f(s0, t0); glVertex2f(xc, yc - glyph->h); glTexCoord2f(s1, t0); glVertex2f(xc + glyph->w, yc - glyph->h); glTexCoord2f(s1, t1); glVertex2f(xc + glyph->w, yc); glTexCoord2f(s0, t1); glVertex2f(xc, yc); return fz_advance_glyph(ctx, font, gid, 0) * g_font_size; }
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; }
void fz_show_string(fz_context *ctx, fz_text *text, fz_font *user_font, fz_matrix *trm, const char *s, int wmode) { fz_font *font; int gid, ucs; float adv; while (*s) { s += fz_chartorune(&ucs, s); gid = fz_encode_character_with_fallback(ctx, user_font, ucs, 0, &font); fz_show_glyph(ctx, text, font, trm, gid, ucs, wmode); adv = fz_advance_glyph(ctx, font, gid, wmode); if (wmode == 0) fz_pre_translate(trm, adv, 0); else fz_pre_translate(trm, 0, -adv); } }
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_font *font; 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->content.text; if (node->char_r2l) { float w = 0; const char *t = s; while (*t) { t += fz_chartorune(&c, t); if (node->mirror) c = ucdn_mirror(c); g = fz_encode_character_with_fallback(ctx, node->style->font, c, 0, &font); w += fz_advance_glyph(ctx, font, g) * node->em; } trm.e += w; while (*s) { s += fz_chartorune(&c, s); if (node->mirror) c = ucdn_mirror(c); g = fz_encode_character_with_fallback(ctx, node->style->font, c, 0, &font); trm.e -= fz_advance_glyph(ctx, font, g) * node->em; if (node->style->visibility == V_VISIBLE) fz_add_text(ctx, text, font, 0, &trm, g, c); } trm.e += w; } else { while (*s) { s += fz_chartorune(&c, s); g = fz_encode_character_with_fallback(ctx, node->style->font, c, 0, &font); if (node->style->visibility == V_VISIBLE) fz_add_text(ctx, text, font, 0, &trm, g, c); trm.e += fz_advance_glyph(ctx, 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->content.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_font *font; 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_with_fallback(ctx, box->style.font, c, UCDN_SCRIPT_LATIN, &font); w += fz_advance_glyph(ctx, 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_with_fallback(ctx, box->style.font, c, UCDN_SCRIPT_LATIN, &font); fz_add_text(ctx, text, font, 0, &trm, g, c); trm.e += fz_advance_glyph(ctx, 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); }
static void fz_text_extract(fz_context *ctx, fz_text_device *dev, fz_text *text, const fz_matrix *ctm, fz_text_style *style) { fz_font *font = text->font; FT_Face face = font->ft_face; fz_matrix tm = text->trm; fz_matrix trm; float adv; float ascender = 1; float descender = 0; int multi; int i, j, err; if (text->len == 0) return; if (dev->spans == NULL) dev->spans = new_span_soup(ctx); if (style->wmode == 0) { if (font->ft_face) { fz_lock(ctx, FZ_LOCK_FREETYPE); err = FT_Set_Char_Size(font->ft_face, 64, 64, 72, 72); if (err) fz_warn(ctx, "freetype set character size: %s", ft_error_string(err)); ascender = (float)face->ascender / face->units_per_EM; descender = (float)face->descender / face->units_per_EM; fz_unlock(ctx, FZ_LOCK_FREETYPE); } else if (font->t3procs && !fz_is_empty_rect(&font->bbox)) { ascender = font->bbox.y1; descender = font->bbox.y0; } } else { ascender = font->bbox.x1; descender = font->bbox.x0; } style->ascender = ascender; style->descender = descender; tm.e = 0; tm.f = 0; fz_concat(&trm, &tm, ctm); for (i = 0; i < text->len; i++) { /* Calculate new pen location and delta */ tm.e = text->items[i].x; tm.f = text->items[i].y; fz_concat(&trm, &tm, ctm); /* Calculate bounding box and new pen position based on font metrics */ adv = fz_advance_glyph(ctx, font, text->items[i].gid); /* Check for one glyph to many char mapping */ for (j = i + 1; j < text->len; j++) if (text->items[j].gid >= 0) break; multi = j - i; if (multi == 1) { fz_add_text_char(ctx, dev, style, text->items[i].ucs, &trm, adv, text->wmode); } else { for (j = 0; j < multi; j++) { fz_add_text_char(ctx, dev, style, text->items[i + j].ucs, &trm, adv/multi, text->wmode); } i += j - 1; } dev->lastchar = text->items[i].ucs; } }
float ui_measure_character(fz_context *ctx, int ucs) { fz_font *font; int gid = fz_encode_character_with_fallback(ctx, g_font, ucs, 0, &font); return fz_advance_glyph(ctx, font, gid, 0) * g_font_size; }