Basic FreeType renderer implementation.
[gdipp.git] / gdipp_server / freetype.cpp
blobd7e0b54e36792f8e85efb1afea562994c355612d
1 #include "stdafx.h"
2 #include "freetype.h"
3 #include "gdipp_config/constant_server.h"
4 #include "gdipp_server/global.h"
5 #include "gdipp_server/helper.h"
7 namespace gdipp
10 FT_Library ft_lib;
11 FTC_Manager ft_cache_man;
12 FTC_ImageCache ft_glyph_cache;
13 FT_Glyph empty_outline_glyph;
15 void initialize_freetype()
17 FT_Error ft_error;
19 ft_error = FT_Init_FreeType(&ft_lib);
20 assert(ft_error == 0);
22 FT_LcdFilter lcd_filter = static_cast<FT_LcdFilter>(config_instance.get_number(L"/gdipp/server/freetype_lcd_filter/text()", static_cast<int>(server_config::LCD_FILTER)));
23 ft_error = FT_Library_SetLcdFilter(ft_lib, lcd_filter);
24 assert(ft_error == 0);
26 unsigned int b = server_cache_size >> 2;
27 ft_error = FTC_Manager_New(ft_lib,
28 1 >> b,
29 1 >> (b + 1),
30 1 >> (b + 18),
31 face_requester,
32 NULL,
33 &ft_cache_man);
34 assert(ft_error == 0);
36 ft_error = FTC_ImageCache_New(ft_cache_man, &ft_glyph_cache);
37 assert(ft_error == 0);
39 empty_outline_glyph = make_empty_outline_glyph();
42 void destroy_freetype()
44 FT_Error ft_error;
46 FTC_Manager_Done(ft_cache_man);
48 ft_error = FT_Done_FreeType(ft_lib);
49 assert(ft_error == 0);
52 FT_Error face_requester(FTC_FaceID face_id, FT_Library library, FT_Pointer request_data, FT_Face *aface)
54 FT_Open_Args args = {};
55 args.flags = FT_OPEN_STREAM;
56 args.stream = font_mgr_instance.lookup_stream(face_id);
58 return FT_Open_Face(library, &args, font_mgr_instance.lookup_face_index(face_id), aface);
61 int get_freetype_kern(const FTC_Scaler scaler, WORD left_glyph, WORD right_glyph)
63 FT_Error ft_error;
65 FT_Size size;
66 ft_error = FTC_Manager_LookupSize(ft_cache_man, scaler, &size);
67 assert(ft_error == 0);
69 FT_Vector delta;
70 ft_error = FT_Get_Kerning(size->face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &delta);
71 assert(ft_error == 0);
73 return int_from_26dot6(delta.x);
76 FT_Glyph make_empty_outline_glyph()
78 FT_Glyph empty_glyph;
80 FT_Error ft_error;
82 FT_GlyphSlotRec glyph_slot = {};
83 glyph_slot.library = ft_lib;
84 glyph_slot.format = FT_GLYPH_FORMAT_OUTLINE;
86 ft_error = FT_Get_Glyph(&glyph_slot, &empty_glyph);
87 if (ft_error != 0)
88 return NULL;
90 return empty_glyph;