1 /+ 2 The MIT License (MIT) 3 4 Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)> 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 +/ 24 25 module desgui.base.ftglyphrender; 26 27 public import desgui.core.textrender; 28 import desmath.linear.vector; 29 import desil; 30 31 import derelict.freetype.ft; 32 import derelict.freetype.types; 33 34 import desutil.logger; 35 mixin( PrivateLoggerMixin ); 36 37 class FTGlyphRenderException: Exception 38 { 39 @safe pure nothrow this( string msg, string file=__FILE__, size_t line=__LINE__ ) 40 { super( msg, file, line ); } 41 } 42 43 class FTGlyphRender : DiGlyphRender 44 { 45 private: 46 static lib_inited = false; 47 static FT_Library ft; 48 49 static FTGlyphRender[string] openFTGR; 50 51 FT_Face face; 52 53 static this() 54 { 55 if( !lib_inited ) 56 { 57 DerelictFT.load(); 58 if( FT_Init_FreeType( &ft ) ) 59 throw new FTGlyphRenderException( "Couldn't init freetype library" ); 60 61 lib_inited = true; 62 } 63 } 64 65 this( string fontname ) 66 { 67 import std.file; 68 if( !fontname.exists ) 69 throw new FTGlyphRenderException( "Couldn't open font '" ~ fontname ~ "': file not exist" ); 70 71 bool loaderror = false; 72 foreach( i; 0 .. 100 ) 73 { 74 if( FT_New_Face( ft, fontname.dup.ptr, 0, &face ) ) loaderror = true; 75 else { loaderror = false; break; } 76 log_error( "font load attempt: %d", i ); 77 } 78 79 if( loaderror ) 80 throw new FTGlyphRenderException( "Couldn't open font '" ~ fontname ~ "': loading error" ); 81 82 if( FT_Select_Charmap( face, FT_Encoding.FT_ENCODING_UNICODE ) ) 83 throw new FTGlyphRenderException( "Couldn't select unicode encoding" ); 84 } 85 86 auto color = col4( 1,1,1,1 ); 87 public: 88 89 static DiGlyphRender get( string fontname ) 90 { 91 if( fontname !in openFTGR ) 92 openFTGR[fontname] = new FTGlyphRender( fontname ); 93 return openFTGR[fontname]; 94 } 95 96 void setParams( in DiGlyphParam p ) 97 { 98 FT_Set_Pixel_Sizes( face, 0, p.height ); 99 color = p.color; 100 } 101 102 @property ImageType imtype() const { return ImageType( ImCompType.UBYTE, 4 ); } 103 104 DiGlyphInfo render( wchar ch ) 105 { 106 if( FT_Load_Char( face, cast(size_t)ch, FT_LOAD_RENDER ) ) 107 throw new FTGlyphRenderException( "Couldn't load char" ); 108 109 auto g = face.glyph; 110 ivec2 sz = ivec2( g.bitmap.width, g.bitmap.rows ); 111 112 alias vec!(4,ubyte,"rgba") bcol4; 113 114 auto ret = DiGlyphInfo( ivec2( g.bitmap_left, -g.bitmap_top ), 115 ivec2( cast(int)( g.advance.x >> 6 ), 116 cast(int)( g.advance.y >> 6 ) ), 117 Image( imsize_t(sz), imtype ) ); 118 119 foreach( y; 0 .. sz.y ) 120 foreach( x; 0 .. sz.x ) 121 ret.img.access!bcol4(x,y) = bcol4( col4( color.rgb, color.a * g.bitmap.buffer[y*sz.x+x] / 255.0 ) * 255 ); 122 123 return ret; 124 } 125 126 ~this() 127 { 128 if( FT_Done_Face !is null ) 129 FT_Done_Face( face ); 130 } 131 132 static ~this() 133 { 134 if( lib_inited && FT_Done_FreeType !is null ) 135 FT_Done_FreeType( ft ); 136 } 137 }