Skip to content

Commit

Permalink
prepare for aRGB glyph bitmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
dk committed Nov 24, 2024
1 parent f2f9a4e commit b8c6f51
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 16 deletions.
5 changes: 3 additions & 2 deletions class/Image/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ plot_next_glyph(
Byte *arena;
Point offset, size;
Rect glyph;
int default_advance = 0;
int default_advance = 0, bpp;

if ( t->fonts )
if ( !Drawable_switch_font(self, savefont, t->fonts[i]))
Expand All @@ -331,7 +331,8 @@ plot_next_glyph(
if ( !( arena = apc_font_get_glyph_bitmap(
self, t->glyphs[i], flags,
&offset, &size,
t->advances ? NULL : &default_advance
t->advances ? NULL : &default_advance,
&bpp
)))
return false;

Expand Down
3 changes: 2 additions & 1 deletion include/apricot.h
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,7 @@ START_TABLE(ggo,UV)
#define ggoUseHints 0x02
#define ggoUnicode 0x04
#define ggoMonochrome 0x08
#define ggoARGB 0x10

#define ggoMove 0
GGO(Move)
Expand Down Expand Up @@ -4330,7 +4331,7 @@ extern PTextShapeFunc
apc_font_get_text_shaper( Handle self, int *type);

extern Byte*
apc_font_get_glyph_bitmap( Handle self, uint16_t index, unsigned int flags, PPoint offset, PPoint size, int *advance);
apc_font_get_glyph_bitmap( Handle self, uint16_t index, unsigned int flags, PPoint offset, PPoint size, int *advance, int *type);

extern PFont
apc_fonts( Handle self, const char *facename, const char *encoding, int *retCount);
Expand Down
1 change: 1 addition & 0 deletions include/win32/win32guts.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ extern void dwrite_font_done(void);
extern void dwrite_free_face(void *face);
extern Bool dwrite_is_font_colored( Handle self, PDCFont dcfont);
extern Bool dwrite_color_text_out(Handle self, PDCFont dc, PGlyphsOutRec t, int x, int y);
extern uint32_t* dwrite_draw_bitmap( Handle self, uint16_t index, Point size, Point aperture );
extern void dpi_change(void);
extern char * err_msg( DWORD errId, char * buffer);
extern char * err_msg_gplus( GpStatus errId, char * buffer);
Expand Down
2 changes: 1 addition & 1 deletion unix/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ apc_gp_get_mapper_ranges(PFont font, int * count, unsigned int * flags)
}

Byte*
apc_font_get_glyph_bitmap( Handle self, uint16_t index, unsigned int flags, PPoint offset, PPoint size, int *advance)
apc_font_get_glyph_bitmap( Handle self, uint16_t index, unsigned int flags, PPoint offset, PPoint size, int *advance, int *type)
{
#ifdef USE_FONTQUERY
if ( is_opt(optInFontQuery) ) {
Expand Down
99 changes: 96 additions & 3 deletions win32/dwfont.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* DirectWrite-based fonts */

#define INITGUID
#include "guts.h"
#include "win32\win32guts.h"

#include <d2d1.h>
Expand Down Expand Up @@ -298,12 +299,12 @@ enumerate_colorrun(IDWriteColorGlyphRunEnumerator1 *enumerator, unsigned int see
}

static Bool
is_aa_colr( Handle self, PList l)
is_aa_colr( Handle self, PList l, int alpha)
{
int i;
Bool has_no_advances = false, has_colr = false;

if ( sys alpha == 255 ) return false;
if ( alpha == 255 ) return false;

for ( i = 0; i < l->count; i++) {
GlyphColorRec *t = (GlyphColorRec*) l->items[i];
Expand Down Expand Up @@ -418,7 +419,7 @@ dwrite_color_text_out(Handle self, PDCFont dc, PGlyphsOutRec t, int x, int y)
if ( !l )
return false;

if ( is_aa_colr( self, l ))
if ( is_aa_colr( self, l, sys alpha ))
draw_aa_colr( self, dc->hfont, l, x, y );
else
draw_colorglyphs( self, dc->hfont, l );
Expand All @@ -429,6 +430,98 @@ dwrite_color_text_out(Handle self, PDCFont dc, PGlyphsOutRec t, int x, int y)
return true;
}

uint32_t*
dwrite_draw_bitmap( Handle self, uint16_t index, Point size, Point aperture )
{
int i;
PList l;
HDC dc0, dc;
GlyphsOutRec t;
uint32_t *arena = NULL, *ret = NULL;
uint16_t dummy_advance = 42;
int16_t dummy_offset[2] = {0,0};
HBITMAP bitmap = NULL, save_bitmap = NULL;
HFONT save_font;
IDWriteColorGlyphRunEnumerator1 *enumerator;

if ( !dw_ok ) return false;

memset( &t, 0, sizeof(t));
t.len = 1;
t.glyphs = &index;
t.advances = &dummy_advance;
t.positions = dummy_offset;
if ( !( enumerator = get_enumerator(self, sys dc_font, &t, aperture.x, size.y + aperture.y)))
return NULL;

l = enumerate_colorrun( enumerator, t.len );
enumerator->lpVtbl->Release(enumerator);
if ( !l )
return NULL;

if ( !is_aa_colr( self, l, 0 )) {
list_delete_all( l, true );
plist_destroy( l );
return NULL;
}

dc0 = GetDC(NULL);
dc = CreateCompatibleDC(dc0);

if ( !( bitmap = image_create_argb_dib_section( dc, size.x, size.y, &arena)))
goto EXIT;
memset( arena, 0xff, size.x * size.y * 4 );

save_bitmap = SelectObject(dc, bitmap);
save_font = SelectObject(dc, sys dc_font->hfont);
SetTextAlign( dc, TA_BASELINE);
SetBkMode ( dc, TRANSPARENT);
if ( !prima_matrix_is_identity( var current_state.matrix )) {
Matrix *m = &var current_state.matrix;
XFORM xf = {
(*m)[0], -((*m)[1]),
-((*m)[2]), (*m)[3],
0, 0
};
SetWorldTransform( dc, &xf );
}

for ( i = 0; i < l->count; i++) {
GlyphColorRec *t = (GlyphColorRec*) l->items[i];
switch (t-> format) {
case 0:
SetTextColor( dc, 0);
ExtTextOutW(dc, t->baseline.x, t->baseline.y, ETO_GLYPH_INDEX, NULL, (LPCWSTR) t->g.glyphs, 1, NULL);
break;
case DWRITE_GLYPH_IMAGE_FORMATS_COLR:
SetTextColor( dc, t->has_color ? t->colorref : 0);
ExtTextOutW(dc, t->baseline.x, t->baseline.y, ETO_GLYPH_INDEX, NULL, (LPCWSTR) t->g.glyphs, 1, NULL);
break;
}
break;
}

list_delete_all( l, true );
plist_destroy( l );

if ( ( ret = malloc( size.x * size.y * 4 )) != NULL ) {
register uint32_t *src = arena, *dst = ret, n = size.x * size.y;
while (n--) {
*(dst++) = (*src == 0xffffff) ? 0 : (*src | 0xff000000);
src++;
}
memcpy( ret, arena, size.x * size.y * 4);
}

SelectObject(dc, save_bitmap);
SelectObject(dc, save_font);
DeleteObject(bitmap);
EXIT:
DeleteDC(dc);
ReleaseDC(NULL, dc0);
return ret;
}

void
dwrite_free_face(void *face)
{
Expand Down
27 changes: 18 additions & 9 deletions win32/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ run combiners on these fonts
*/
static void
fix_combiners_pdx( Handle self, PGlyphsOutRec t, INT *pdx)
fix_combiners_pdx( PGlyphsOutRec t, INT *pdx)
{
int i;
for ( i = 0; i < t->len; ) {
Expand Down Expand Up @@ -629,7 +629,7 @@ text_gp_glyphs_out( Handle self, PGlyphsOutRec t, int x, int y, Bool fixed_pitch
}

if ( fixed_pitch )
fix_combiners_pdx(self, t, pdx);
fix_combiners_pdx(t, pdx);
ok = ExtTextOutW(sys ps, x, y, ETO_GLYPH_INDEX | ETO_PDY, NULL, (LPCWSTR) t->glyphs, t->len, pdx);
if ( pdx != dx ) free(pdx);
#undef SZ
Expand Down Expand Up @@ -2265,7 +2265,7 @@ apc_font_end_query( Handle self )
}

Byte*
apc_font_get_glyph_bitmap( Handle self, uint16_t index, unsigned int flags, PPoint offset, PPoint size, int *advance)
apc_font_get_glyph_bitmap( Handle self, uint16_t index, unsigned int flags, PPoint offset, PPoint size, int *advance, int *type)
{
int gdi_size;
Byte * gdi_buf;
Expand All @@ -2291,6 +2291,21 @@ apc_font_get_glyph_bitmap( Handle self, uint16_t index, unsigned int flags, PPoi
;
if (( gdi_size = GetGlyphOutlineW(sys ps, index, format, &gm, 0, NULL, &matrix)) < 0 )
return NULL;
size-> x = gm.gmBlackBoxX;
size-> y = gm.gmBlackBoxY;
offset-> x = gm.gmptGlyphOrigin.x;
offset-> y =-gm.gmptGlyphOrigin.y;
if ( gdi_size == 0 )
size-> x = size-> y = 0;

if (
!(flags & ggoMonochrome) &&
(flags & ggoARGB) &&
apc_gp_is_font_colored
) {
//dwrite_draw_bitmap( self, index, *size, *offset);
}

if (( gdi_buf = malloc(gdi_size)) == NULL ) {
warn("Not enough memory");
return NULL;
Expand All @@ -2302,12 +2317,6 @@ apc_font_get_glyph_bitmap( Handle self, uint16_t index, unsigned int flags, PPoi
return NULL;
}

size-> x = gm.gmBlackBoxX;
size-> y = gm.gmBlackBoxY;
offset-> x = gm.gmptGlyphOrigin.x;
offset-> y =-gm.gmptGlyphOrigin.y;
if ( gdi_size == 0 )
size-> x = size-> y = 0;
if ( advance ) {
ABC x;
if (GetCharABCWidthsI( sys ps, index, 1, NULL, &x)) {
Expand Down

0 comments on commit b8c6f51

Please sign in to comment.