Skip to content

Commit

Permalink
Move stext 'color' to be 'argb'.
Browse files Browse the repository at this point in the history
Move vectors into line with chars. We now return a consistent
representation.

'color' has been deliberately changed to 'argb' as a) it's
clearer, and b) it forces people to update their code and not
suddenly see failures with alpha being unexpectedly in the top
8 bits.
  • Loading branch information
robinwatts committed Nov 14, 2024
1 parent a2ab6fd commit 3937b65
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
4 changes: 2 additions & 2 deletions include/mupdf/fitz/structured-text.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ struct fz_stext_block
struct { fz_stext_line *first_line, *last_line; } t;
struct { fz_matrix transform; fz_image *image; } i;
struct { fz_stext_struct *down; int index; } s;
struct { uint8_t stroked; uint8_t rgba[4]; } v;
struct { uint8_t stroked; uint32_t argb; } v;
struct { fz_stext_grid_positions *xs; fz_stext_grid_positions *ys; } b;
} u;
fz_stext_block *prev, *next;
Expand All @@ -338,7 +338,7 @@ struct fz_stext_char
int c; /* unicode character value */
uint16_t bidi; /* even for LTR, odd for RTL - probably only needs 8 bits? */
uint16_t flags;
int color; /* sRGB hex color */
uint32_t argb; /* sRGB hex color (alpha in top 8 bits, then r, then g, then b in low bits) */
fz_point origin;
fz_quad quad;
float size;
Expand Down
32 changes: 9 additions & 23 deletions source/fitz/stext-device.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ add_char_to_line(fz_context *ctx, fz_stext_page *page, fz_stext_line *line, fz_m
}

ch->c = c;
ch->color = color;
ch->argb = color;
ch->bidi = bidi;
ch->origin = *p;
ch->size = size;
Expand Down Expand Up @@ -1060,14 +1060,15 @@ fz_stext_extract(fz_context *ctx, fz_stext_device *dev, fz_text_span *span, fz_m
do_extract(ctx, dev, span, ctm, 0, span->len);
}

static int hexrgb_from_color(fz_context *ctx, fz_colorspace *colorspace, const float *color)
static int hexrgba_from_color(fz_context *ctx, fz_colorspace *colorspace, const float *color, float alpha)
{
float rgb[3];
fz_convert_color(ctx, colorspace, color, fz_device_rgb(ctx), rgb, NULL, fz_default_color_params);
return
(fz_clampi(rgb[0] * 255, 0, 255) << 16) |
(fz_clampi(rgb[1] * 255, 0, 255) << 8) |
(fz_clampi(rgb[2] * 255, 0, 255));
(fz_clampi(alpha * 255 + 0.5f, 0, 255) << 24) |
(fz_clampi(rgb[0] * 255 + 0.5f, 0, 255) << 16) |
(fz_clampi(rgb[1] * 255 + 0.5f, 0, 255) << 8) |
(fz_clampi(rgb[2] * 255 + 0.5f, 0, 255));
}

static void
Expand All @@ -1078,7 +1079,7 @@ fz_stext_fill_text(fz_context *ctx, fz_device *dev, const fz_text *text, fz_matr
fz_text_span *span;
if (text == tdev->lasttext)
return;
tdev->color = hexrgb_from_color(ctx, colorspace, color);
tdev->color = hexrgba_from_color(ctx, colorspace, color, alpha);
tdev->new_obj = 1;
for (span = text->head; span; span = span->next)
fz_stext_extract(ctx, tdev, span, ctm);
Expand All @@ -1094,7 +1095,7 @@ fz_stext_stroke_text(fz_context *ctx, fz_device *dev, const fz_text *text, const
fz_text_span *span;
if (text == tdev->lasttext)
return;
tdev->color = hexrgb_from_color(ctx, colorspace, color);
tdev->color = hexrgba_from_color(ctx, colorspace, color, alpha);
tdev->new_obj = 1;
for (span = text->head; span; span = span->next)
fz_stext_extract(ctx, tdev, span, ctm);
Expand Down Expand Up @@ -1795,30 +1796,15 @@ check_for_strikeout(fz_context *ctx, fz_stext_device *tdev, fz_stext_page *page,
}
}

static uint8_t
to255(float x)
{
if (x <= 0)
return 0;
if (x >= 1)
return 255;
return (uint8_t)(x*255 + 0.5);
}

static void
add_vector(fz_context *ctx, fz_stext_page *page, fz_rect bbox, int stroked, fz_colorspace *cs, const float *color, float alpha, fz_color_params cp)
{
float rgb[3];
fz_stext_block *b = add_block_to_page(ctx, page);

b->type = FZ_STEXT_BLOCK_VECTOR;
b->bbox = bbox;
b->u.v.stroked = stroked;
fz_convert_color(ctx, cs, color, fz_device_rgb(ctx), rgb, NULL, cp);
b->u.v.rgba[0] = to255(rgb[0]);
b->u.v.rgba[1] = to255(rgb[1]);
b->u.v.rgba[2] = to255(rgb[2]);
b->u.v.rgba[3] = to255(alpha);
b->u.v.argb = hexrgba_from_color(ctx, cs, color, alpha);
}

static void
Expand Down
15 changes: 8 additions & 7 deletions source/fitz/stext-output.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ fz_print_stext_block_as_html(fz_context *ctx, fz_output *out, fz_stext_block *bl
fz_font *font = NULL;
float size = 0;
int sup = 0;
int color = 0;
uint32_t color = 0;

for (line = block->u.t.first_line; line; line = line->next)
{
Expand All @@ -318,13 +318,13 @@ fz_print_stext_block_as_html(fz_context *ctx, fz_output *out, fz_stext_block *bl
for (ch = line->first_char; ch; ch = ch->next)
{
int ch_sup = detect_super_script(line, ch);
if (ch->font != font || ch->size != size || ch_sup != sup || ch->color != color)
if (ch->font != font || ch->size != size || ch_sup != sup || ch->argb != color)
{
if (font)
fz_print_style_end_html(ctx, out, font, size, sup, color);
font = ch->font;
size = ch->size;
color = ch->color;
color = ch->argb;
sup = ch_sup;
fz_print_style_begin_html(ctx, out, font, size, sup, color);
}
Expand Down Expand Up @@ -631,14 +631,15 @@ as_xml(fz_context *ctx, fz_stext_block *block, fz_output *out)
name = font_full_name(ctx, font);
fz_write_printf(ctx, out, "<font name=\"%s\" size=\"%g\">\n", name, size);
}
fz_write_printf(ctx, out, "<char quad=\"%g %g %g %g %g %g %g %g\" x=\"%g\" y=\"%g\" bidi=\"%d\" color=\"#%06x\" flags=\"%d\" c=\"",
fz_write_printf(ctx, out, "<char quad=\"%g %g %g %g %g %g %g %g\" x=\"%g\" y=\"%g\" bidi=\"%d\" color=\"#%06x\" alpha=\"#%02x\" flags=\"%d\" c=\"",
ch->quad.ul.x, ch->quad.ul.y,
ch->quad.ur.x, ch->quad.ur.y,
ch->quad.ll.x, ch->quad.ll.y,
ch->quad.lr.x, ch->quad.lr.y,
ch->origin.x, ch->origin.y,
ch->bidi,
ch->color,
ch->argb & 0xFFFFFF,
ch->argb>>24,
ch->flags);
switch (ch->c)
{
Expand Down Expand Up @@ -682,9 +683,9 @@ as_xml(fz_context *ctx, fz_stext_block *block, fz_output *out)
break;

case FZ_STEXT_BLOCK_VECTOR:
fz_write_printf(ctx, out, "<vector bbox=\"%g %g %g %g\" stroke=\"%d\" rgba=\"%02x%02x%02x%02x\"/>\n",
fz_write_printf(ctx, out, "<vector bbox=\"%g %g %g %g\" stroke=\"%d\" argba=\"%08x\"/>\n",
block->bbox.x0, block->bbox.y0, block->bbox.x1, block->bbox.y1,
!!block->u.v.stroked, block->u.v.rgba[0], block->u.v.rgba[1], block->u.v.rgba[2], block->u.v.rgba[3]);
!!block->u.v.stroked, block->u.v.argb);
break;

case FZ_STEXT_BLOCK_GRID:
Expand Down
2 changes: 1 addition & 1 deletion source/tools/murun.c
Original file line number Diff line number Diff line change
Expand Up @@ -5571,7 +5571,7 @@ stext_walk(js_State *J, fz_stext_block *block)
ffi_pushfont(J, ch->font);
js_pushnumber(J, ch->size);
ffi_pushquad(J, ch->quad);
js_pushnumber(J, ch->color);
js_pushnumber(J, ch->argb);
js_call(J, 6);
js_pop(J, 1);
}
Expand Down

0 comments on commit 3937b65

Please sign in to comment.