Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 5023 → Rev 5024

/contrib/sdk/samples/freetype/txview/Makefile
0,0 → 1,29
CC = kos32-gcc
LD = kos32-ld
 
SDK_DIR:= $(abspath ../../..)
 
LDFLAGS = -static -S -nostdlib -T $(SDK_DIR)/sources/newlib/app.lds -Map txview.map --image-base 0
 
CFLAGS = -c -fno-ident -O2 -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32
 
INCLUDES= -I $(SDK_DIR)/sources/newlib/libc/include -I $(SDK_DIR)/sources/freetype/include
LIBPATH:= -L $(SDK_DIR)/lib -L /home/autobuild/tools/win32/mingw32/lib
 
SOURCES = main.c \
fontlib.c \
tview.c
 
OBJECTS = $(patsubst %.c, %.o, $(SOURCES))
 
 
default: txview
 
txview: $(OBJECTS) Makefile
$(LD) $(LDFLAGS) $(LIBPATH) -o txview $(OBJECTS) -lfreetype.dll -lpixlib.dll -lgcc -lc.dll -lapp
objcopy txview -O binary
 
 
%.o : %.c Makefile $(SOURCES)
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $<
/contrib/sdk/samples/freetype/txview/fontlib.c
0,0 → 1,275
 
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include <pixlib2.h>
 
typedef struct
{
int l;
int t;
int r;
int b;
}rect_t;
 
typedef struct
{
FT_Face face;
int height;
int base;
 
FT_Glyph glyph[256];
 
}font_t;
 
static FT_Face def_face;
 
typedef unsigned int color_t;
 
unsigned int ansi2utf32(unsigned char ch);
 
font_t *create_font(FT_Face face, int size);
 
void my_draw_bitmap(bitmap_t *win, FT_Bitmap *bitmap, int dstx, int dsty, int col)
{
uint8_t *dst;
uint8_t *src, *tmpsrc;
 
uint32_t *tmpdst;
int i, j;
 
dst = win->data + dsty * win->pitch + dstx*4;
src = bitmap->buffer;
 
// printf("buffer %x width %d rows %d\n",
// bitmap->buffer, bitmap->width, bitmap->rows);
 
 
for( i = 0; i < bitmap->rows; i++ )
{
tmpdst = (uint32_t*)dst;
tmpsrc = src;
 
dst+= win->pitch;
src+= bitmap->pitch;
 
for( j = 0; j < bitmap->width; j++)
{
int a = *tmpsrc++;
int sr, sg, sb;
int dr, dg, db;
 
if( a != 0) a++;
 
db = *tmpdst & 0xFF;
dg = (*tmpdst >> 8) & 0xFF;
dr = (*tmpdst >> 16) & 0xFF;
 
sb = col & 0xFF;
sg = (col >> 8) & 0xFF;
sr = (col >> 16) &0xFF;
 
db = (a*sb + db*(256-a))/256;
dg = (a*sg + dg*(256-a))/256;
dr = (a*sr + dr*(256-a))/256;
 
*tmpdst++ = 0xFF000000|(dr<<16)|(dg<<8)|db;
};
}
};
 
 
int draw_text_ext(bitmap_t *winbitmap, font_t *font, char *text, int len, rect_t *rc, int color)
{
FT_UInt glyph_index;
FT_Bool use_kerning = 0;
FT_BitmapGlyph glyph;
FT_UInt previous;
 
int x, y, w;
int col, ncol;
unsigned char ch;
int err = 0;
 
use_kerning = FT_HAS_KERNING( font->face );
previous = 0;
col = 0;
 
x = rc->l << 6;
y = rc->b;
 
w = (rc->r - rc->l) << 6;
 
while( len-- )
{
ch = *text++;
 
if(ch == '\n' || ch == '\r')
continue;
 
if(ch == '\t')
{
ncol = (col+4) & ~3;
if( col < ncol)
{
glyph_index = FT_Get_Char_Index( font->face, ansi2utf32(' ') );
 
while( col < ncol)
{
if ( use_kerning && previous && glyph_index )
{
FT_Vector delta;
FT_Get_Kerning( font->face, previous, glyph_index, FT_KERNING_DEFAULT, &delta );
x += delta.x ;
}
 
if( x + (font->glyph[ch]->advance.x >> 10) > w)
break;
 
x += font->glyph[ch]->advance.x >> 10;
previous = glyph_index;
col ++;
};
};
continue;
};
 
glyph_index = FT_Get_Char_Index( font->face, ansi2utf32(ch) );
 
if ( use_kerning && previous && glyph_index )
{
FT_Vector delta;
FT_Get_Kerning( font->face, previous, glyph_index, FT_KERNING_DEFAULT, &delta );
x += delta.x ;
}
 
if( x + (font->glyph[ch]->advance.x >> 10) > w)
break;
 
glyph = (FT_BitmapGlyph)font->glyph[ch];
 
my_draw_bitmap(winbitmap, &glyph->bitmap, (x >> 6) + glyph->left,
y - glyph->top, color);
 
x += font->glyph[ch]->advance.x >> 10;
previous = glyph_index;
};
 
return err;
};
 
 
int init_fontlib()
{
static FT_Library library;
FT_Face face = NULL;
int err;
 
err = FT_Init_FreeType( &library );
if ( err )
{
printf("an error occurred during FreeType initialization\n");
goto done;
}
 
err = FT_New_Face( library, "/kolibrios/Fonts/IstokWeb.ttf", 0, &face );
// err = FT_New_Face( library, "/kolibrios/Fonts/lucon.ttf", 0, &face );
if ( err == FT_Err_Unknown_File_Format )
{
printf("font format is unsupported\n");
goto done;
 
}
else if ( err )
{
printf("font file could not be read or broken\n");
goto done;
 
}
 
def_face = face;
 
done:
 
return err;
};
 
 
unsigned int ansi2utf32(unsigned char ch)
{
if(ch < 0x80)
return ch;
 
if(ch < 0xB0)
return 0x410-0x80 + ch;
 
if(ch < 0xE0)
return 0;
 
if(ch < 0xF0)
return 0x440-0xE0 + ch;
 
if(ch == 0xF0)
return 0x401;
else if(ch==0xF1)
return 0x451;
else return 0;
}
 
 
font_t *create_font(FT_Face xface, int size)
{
font_t *font;
int i, err;
 
font = malloc(sizeof(*font));
if(font == NULL)
return font;
 
memset(font, 0, sizeof(*font));
 
font->face = (xface == NULL) ? def_face : xface;
font->height = size;
 
err = FT_Set_Pixel_Sizes( font->face, 0, size );
 
for(i = 0; i < 256; i++)
{
FT_UInt glyph_index;
FT_BitmapGlyph glyph_bitmap;
 
glyph_index = FT_Get_Char_Index( font->face, ansi2utf32(i) );
 
err = FT_Load_Glyph( font->face, glyph_index, FT_LOAD_DEFAULT );
if ( err )
{
font->glyph[i] = font->glyph[0] ;
continue;
};
 
err = FT_Get_Glyph( font->face->glyph, &font->glyph[i] );
if (err)
{
font->glyph[i] = font->glyph[0] ;
continue;
};
 
if ( font->glyph[i]->format != FT_GLYPH_FORMAT_BITMAP )
{
err = FT_Glyph_To_Bitmap( &font->glyph[i], FT_RENDER_MODE_NORMAL, 0, 1 );
if ( err )
continue;
 
glyph_bitmap = (FT_BitmapGlyph)font->glyph[i];
 
if(glyph_bitmap->top > font->base)
font->base = glyph_bitmap->top;
}
}
 
return font;
}
/contrib/sdk/samples/freetype/txview/main.c
0,0 → 1,142
#include <stdio.h>
#include <string.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include <kos32sys.h>
#include <pixlib2.h>
 
typedef struct
{
int l;
int t;
int r;
int b;
}rect_t;
 
typedef struct
{
FT_Face face;
int height;
int base;
 
FT_Glyph glyph[256];
 
}font_t;
 
typedef struct
{
bitmap_t bitmap;
font_t *font;
 
char *text;
char **line;
int lines;
int txlines;
int startline;
int endline;
int w;
int h;
}tview_t;
 
int init_tview(tview_t *txv, int width, int height, char *text, int size);
int txv_scroll_up(tview_t *txv);
int txv_scroll_down(tview_t *txv);
 
void* init_fontlib();
int draw_text_ext(bitmap_t *winbitmap, FT_Face face, char *text, int len, rect_t *rc, int color);
 
void draw_window(void)
{
BeginDraw();
DrawWindow(0,0,0,0,NULL,0,0x74);
EndDraw();
}
 
tview_t txv;
 
int main(int argc, char *argv[])
{
ufile_t uf;
oskey_t key;
 
__asm__ __volatile__(
"int $0x40"
::"a"(40), "b"(0xc0000027));
 
if(argc < 2)
uf = load_file("/RD/1/EXAMPLE.ASM");
else uf = load_file(argv[1]);
 
if(uf.data == NULL ||
uf.size == 0)
return 0;
 
init_pixlib(0);
init_fontlib();
 
init_tview(&txv, 480, 600, uf.data, uf.size);
 
BeginDraw();
DrawWindow(10, 40, txv.w+TYPE_3_BORDER_WIDTH*2,
txv.h+TYPE_3_BORDER_WIDTH+get_skin_height(), "Text example", 0x000000, 0x74);
blit_bitmap(&txv.bitmap, TYPE_3_BORDER_WIDTH, get_skin_height(), txv.w, txv.h, 0, 0);
 
EndDraw();
 
for (;;)
{
uint32_t wheels;
int buttons;
pos_t pos;
 
switch (get_os_event())
{
case 1:
draw_window();
blit_bitmap(&txv.bitmap, TYPE_3_BORDER_WIDTH, get_skin_height(), txv.w, txv.h, 0, 0);
break;
case 2:
key = get_key();
switch(key.code)
{
case 27:
return;
 
case 177:
if( txv_scroll_up(&txv) )
blit_bitmap(&txv.bitmap, TYPE_3_BORDER_WIDTH, get_skin_height(), txv.w, txv.h, 0, 0);
break;
 
case 178:
if( txv_scroll_down(&txv) )
blit_bitmap(&txv.bitmap, TYPE_3_BORDER_WIDTH, get_skin_height(), txv.w, txv.h, 0, 0);
break;
}
break;
 
case 3:
// button pressed; we have only one button, close
return;
 
case 6:
// pos = get_mouse_pos();
// buttons = get_mouse_buttons();
wheels = get_mouse_wheels();
 
if( wheels & 0xFFFF)
{
int r;
 
if((short)wheels > 0)
r = txv_scroll_up(&txv);
else
r = txv_scroll_down(&txv);
 
if( r )
blit_bitmap(&txv.bitmap, TYPE_3_BORDER_WIDTH, get_skin_height(), txv.w, txv.h, 0, 0);
}
}
}
return 0;
}