### Install FreeTypeSharp NuGet Package Source: https://context7.com/ryancheung/freetypesharp/llms.txt Installs the FreeTypeSharp library using the .NET CLI. For UWP projects, a separate NuGet package is available. ```bash dotnet add package FreeTypeSharp dotnet add package FreeTypeSharp.UWP ``` -------------------------------- ### Load Font Face from File (FT_New_Face) Source: https://context7.com/ryancheung/freetypesharp/llms.txt Loads a font face from a specified file path using the `FT_New_Face` function. This example shows how to handle potential errors during loading and access basic font properties like family name, style name, number of glyphs, and units per EM. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; string fontPath = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; var error = FT_New_Face( library.Native, (byte*)Marshal.StringToHGlobalAnsi(fontPath), 0, // face_index: 0 for first face in file &face ); if (error != FT_Error.FT_Err_Ok) { Console.WriteLine($"Failed to load font: {error}"); return; } // Access face properties string familyName = Marshal.PtrToStringAnsi((IntPtr)face->family_name); string styleName = Marshal.PtrToStringAnsi((IntPtr)face->style_name); Console.WriteLine($"Font: {familyName} {styleName}"); Console.WriteLine($"Number of glyphs: {face->num_glyphs}"); Console.WriteLine($"Units per EM: {face->units_per_EM}"); // Clean up FT_Done_Face(face); } ``` -------------------------------- ### Load Font Face from Memory (FT_New_Memory_Face) Source: https://context7.com/ryancheung/freetypesharp/llms.txt Loads a font face directly from a byte array in memory using `FT_New_Memory_Face`. This is useful for embedded fonts or fonts loaded from network streams. The example includes pinning the byte array to prevent garbage collection issues. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.IO; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); // Load font data into memory byte[] fontData = File.ReadAllBytes("arial.ttf"); // Pin the array to prevent GC from moving it fixed (byte* fontDataPtr = fontData) { FT_FaceRec_* face; var error = FT_New_Memory_Face( library.Native, fontDataPtr, (IntPtr)fontData.Length, (IntPtr)0, // face_index &face ); if (error == FT_Error.FT_Err_Ok) { Console.WriteLine($"Loaded font from memory: {Marshal.PtrToStringAnsi((IntPtr)face->family_name)}"); FT_Done_Face(face); } } } ``` -------------------------------- ### Initialize and Render Glyph with FreeTypeSharp Source: https://github.com/ryancheung/freetypesharp/blob/main/README.md Demonstrates the basic workflow of initializing the FreeType library, loading a font face, setting character size, and rendering a glyph using the managed C# API. This requires importing the FreeTypeSharp namespaces and using unsafe pointers to interact with the native FreeType structures. ```csharp using static FreeTypeSharp.FT; using static FreeTypeSharp.FT_LOAD; using static FreeTypeSharp.FT_Render_Mode_; FT_LibraryRec_* lib; FT_FaceRec_* face; var error = FT_Init_FreeType(&lib); error = FT_New_Face(lib, (byte*)Marshal.StringToHGlobalAnsi("some_font_name.ttf"), 0, &face); error = FT_Set_Char_Size(face, 0, 16 * 64, 300, 300); var glyphIndex = FT_Get_Char_Index(face, 'F'); error = FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT); error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL); ``` -------------------------------- ### Initialize and Use FreeTypeFaceFacade Source: https://context7.com/ryancheung/freetypesharp/llms.txt Demonstrates how to initialize a font face using FreeTypeFaceFacade, set character sizes, and retrieve font metrics and glyph information. This wrapper handles the conversion of internal FreeType metrics into pixel values for easier consumption. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); var facade = new FreeTypeFaceFacade(library, face); facade.SelectCharSize(16, 96, 96); Console.WriteLine($"Family: {facade.MarshalFamilyName()}"); Console.WriteLine($"Ascender: {facade.Ascender} pixels"); uint glyphIndex = facade.GetCharIndex('W'); FT_Load_Glyph(face, glyphIndex, FT_LOAD.FT_LOAD_DEFAULT); FT_Render_Glyph(face->glyph, FT_Render_Mode_.FT_RENDER_MODE_NORMAL); Console.WriteLine($"Glyph width: {facade.GlyphMetricWidth} pixels"); FT_Done_Face(face); } ``` -------------------------------- ### Initialize FreeType Library (FT_Init_FreeType) Source: https://context7.com/ryancheung/freetypesharp/llms.txt Initializes a new FreeType library instance using the low-level C API. This is a prerequisite for all other FreeType operations. It also demonstrates how to retrieve the FreeType version and properly clean up the library. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; unsafe { FT_LibraryRec_* lib; var error = FT_Init_FreeType(&lib); if (error != FT_Error.FT_Err_Ok) { Console.WriteLine($"Failed to initialize FreeType: {error}"); return; } // Get version info int major, minor, patch; FT_Library_Version(lib, &major, &minor, &patch); Console.WriteLine($"FreeType version: {major}.{minor}.{patch}"); // Output: FreeType version: 2.13.2 // Clean up when done FT_Done_FreeType(lib); } ``` -------------------------------- ### Managed FreeType Library Wrapper (FreeTypeLibrary) Source: https://context7.com/ryancheung/freetypesharp/llms.txt Demonstrates the use of the `FreeTypeLibrary` managed wrapper class, which simplifies FreeType library initialization and cleanup using the IDisposable pattern. This provides a safer alternative to manual pointer management. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; unsafe { // Using statement ensures proper cleanup using (var library = new FreeTypeLibrary()) { int major, minor, patch; FT_Library_Version(library.Native, &major, &minor, &patch); Console.WriteLine($"FreeType version: {major}.{minor}.{patch}"); // Check if library is disposed Console.WriteLine($"Library disposed: {library.Disposed}"); // Output: Library disposed: False } // Library automatically cleaned up here } ``` -------------------------------- ### FT_Init_FreeType - Initialize Library Source: https://context7.com/ryancheung/freetypesharp/llms.txt Initializes the FreeType library instance required for all subsequent font operations. ```APIDOC ## FT_Init_FreeType ### Description Initializes a new FreeType library instance. This must be called before any other FreeType operations. ### Method C# Unsafe Method ### Endpoint FT.FT_Init_FreeType(FT_LibraryRec_** alibrary) ### Parameters #### Path Parameters - **alibrary** (FT_LibraryRec_**) - Required - A pointer to the library handle to be initialized. ### Response #### Success Response (FT_Err_Ok) - **error** (FT_Error) - Returns 0 on success, otherwise an error code. ``` -------------------------------- ### Render Glyph to Bitmap using FreeTypeSharp Source: https://context7.com/ryancheung/freetypesharp/llms.txt Shows how to convert a loaded glyph outline into a bitmap using FT_Render_Glyph. It explains how to access raw bitmap data and mentions various render modes like normal, mono, and LCD. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); FT_Set_Char_Size(face, 0, (IntPtr)(48 * 64), 96, 96); uint glyphIndex = FT_Get_Char_Index(face, (UIntPtr)'R'); FT_Load_Glyph(face, glyphIndex, FT_LOAD.FT_LOAD_DEFAULT); var error = FT_Render_Glyph(face->glyph, FT_Render_Mode_.FT_RENDER_MODE_NORMAL); if (error == FT_Error.FT_Err_Ok) { var bitmap = face->glyph->bitmap; for (int y = 0; y < bitmap.rows; y++) { for (int x = 0; x < bitmap.width; x++) { byte pixel = bitmap.buffer[y * bitmap.pitch + x]; } } } FT_Done_Face(face); } ``` -------------------------------- ### FT_New_Face - Load Font Face from File Source: https://context7.com/ryancheung/freetypesharp/llms.txt Loads a font face from a specified file path on the disk. ```APIDOC ## FT_New_Face ### Description Loads a font face from a file path. A face represents a single font in a font file. ### Method C# Unsafe Method ### Endpoint FT.FT_New_Face(FT_Library library, byte* filepathname, int face_index, FT_Face* aface) ### Parameters #### Path Parameters - **library** (FT_Library) - Required - The FreeType library handle. - **filepathname** (byte*) - Required - The path to the font file. - **face_index** (int) - Required - The index of the face in the file (0 for the first face). - **aface** (FT_Face*) - Required - Pointer to the face handle to be created. ### Response #### Success Response (FT_Err_Ok) - **error** (FT_Error) - Returns 0 on success, otherwise an error code. ``` -------------------------------- ### Load Fonts from Memory with FreeTypeFaceFacade Source: https://context7.com/ryancheung/freetypesharp/llms.txt Shows how to load a font file into a memory buffer and initialize a FreeTypeFaceFacade instance from that buffer. This is useful for fonts embedded as application resources. ```csharp using FreeTypeSharp; using System; using System.IO; unsafe { using var library = new FreeTypeLibrary(); byte[] fontData = File.ReadAllBytes("myfont.ttf"); IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(fontData.Length); System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length); var facade = new FreeTypeFaceFacade(library, fontPtr, fontData.Length, 0); facade.SelectCharSize(24, 96, 96); Console.WriteLine($"Loaded font: {facade.MarshalFamilyName()}"); } ``` -------------------------------- ### Manipulate Glyphs with FT_Bitmap Operations Source: https://context7.com/ryancheung/freetypesharp/llms.txt Demonstrates how to copy a rendered glyph bitmap and apply an emboldening effect. This requires an initialized FreeType library and a loaded font face. ```csharp unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); FT_Set_Char_Size(face, 0, (IntPtr)(24 * 64), 96, 96); uint glyphIndex = FT_Get_Char_Index(face, (UIntPtr)'B'); FT_Load_Glyph(face, glyphIndex, FT_LOAD.FT_LOAD_DEFAULT); FT_Render_Glyph(face->glyph, FT_Render_Mode_.FT_RENDER_MODE_NORMAL); FT_Bitmap_ targetBitmap; FT_Bitmap_Init(&targetBitmap); var error = FT_Bitmap_Copy(library.Native, &face->glyph->bitmap, &targetBitmap); if (error == FT_Error.FT_Err_Ok) { FT_Bitmap_Embolden(library.Native, &targetBitmap, (IntPtr)(1 << 6), (IntPtr)(1 << 6)); Console.WriteLine($"Original: {face->glyph->bitmap.width}x{face->glyph->bitmap.rows}"); Console.WriteLine($"Emboldened: {targetBitmap.width}x{targetBitmap.rows}"); FT_Bitmap_Done(library.Native, &targetBitmap); } FT_Done_Face(face); } ``` -------------------------------- ### Create Stroked Outlines (FT_Stroker) - C# Source: https://context7.com/ryancheung/freetypesharp/llms.txt Demonstrates how to use FT_Stroker to create stroked versions of glyphs, useful for text effects like borders. It configures stroke parameters, applies the stroke to a glyph, and converts the result to a bitmap. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); FT_Set_Char_Size(face, 0, (IntPtr)(48 * 64), 96, 96); // Create stroker FT_StrokerRec_* stroker; FT_Stroker_New(library.Native, &stroker); // Configure stroke: 2 pixel radius, round caps and joins FT_Stroker_Set( stroker, (IntPtr)(2 << 6), // radius in 26.6 format FT_Stroker_LineCap_.FT_STROKER_LINECAP_ROUND, FT_Stroker_LineJoin_.FT_STROKER_LINEJOIN_ROUND, (IntPtr)0 // miter_limit (not used for round joins) ); // Load glyph uint glyphIndex = FT_Get_Char_Index(face, (UIntPtr)'O'); FT_Load_Glyph(face, glyphIndex, FT_LOAD.FT_LOAD_DEFAULT); // Get glyph copy for stroking FT_GlyphRec_* glyph; FT_Get_Glyph(face->glyph, &glyph); // Apply stroke FT_Glyph_Stroke(&glyph, stroker, 1); // 1 = destroy original // Convert to bitmap FT_Glyph_To_Bitmap(&glyph, FT_Render_Mode_.FT_RENDER_MODE_NORMAL, null, 1); // Cleanup FT_Done_Glyph(glyph); FT_Stroker_Done(stroker); FT_Done_Face(face); } ``` -------------------------------- ### Set Character Size using FT_Set_Char_Size Source: https://context7.com/ryancheung/freetypesharp/llms.txt Configures the font face size using 26.6 fractional points and DPI settings. This method is ideal for high-precision layout calculations. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); var error = FT_Set_Char_Size( face, (IntPtr)(16 * 64), (IntPtr)(16 * 64), 96, 96 ); if (error == FT_Error.FT_Err_Ok) { var metrics = face->size->metrics; Console.WriteLine($"Ascender: {(int)metrics.ascender >> 6} pixels"); } FT_Done_Face(face); } ``` -------------------------------- ### Glyph Outline Operations - C# Source: https://context7.com/ryancheung/freetypesharp/llms.txt Shows how to access and manipulate glyph outlines using FreeTypeSharp. This includes retrieving outline data, calculating bounding boxes, transforming, translating, and emboldening outlines. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); FT_Set_Char_Size(face, 0, (IntPtr)(48 * 64), 96, 96); uint glyphIndex = FT_Get_Char_Index(face, (UIntPtr)'S'); FT_Load_Glyph(face, glyphIndex, FT_LOAD.FT_LOAD_NO_BITMAP); // Access outline data var outline = face->glyph->outline; Console.WriteLine($"Outline points: {outline.n_points}"); Console.WriteLine($"Outline contours: {outline.n_contours}"); // Get bounding box FT_BBox_ bbox; FT_Outline_Get_BBox(&outline, &bbox); Console.WriteLine($"Bounding box: ({(int)bbox.xMin >> 6}, {(int)bbox.yMin >> 6}) to ({(int)bbox.xMax >> 6}, {(int)bbox.yMax >> 6})"); // Get control box (tighter bounds) FT_BBox_ cbox; FT_Outline_Get_CBox(&outline, &cbox); // Transform outline FT_Matrix_ matrix; matrix.xx = (IntPtr)(1 << 16); // 1.0 in 16.16 format matrix.xy = (IntPtr)0; matrix.yx = (IntPtr)0; matrix.yy = (IntPtr)(1 << 16); FT_Outline_Transform(&outline, &matrix); // Translate outline FT_Outline_Translate(&outline, (IntPtr)(10 << 6), (IntPtr)(5 << 6)); // Embolden (make bolder) FT_Outline_Embolden(&outline, (IntPtr)(1 << 6)); // 1 pixel strength FT_Done_Face(face); } ``` -------------------------------- ### Render Text to Pixel Buffer Source: https://context7.com/ryancheung/freetypesharp/llms.txt Provides a complete workflow for rendering a string of text into a raw byte array pixel buffer. It includes handling kerning, glyph positioning, and manual blitting to a grayscale image buffer. ```csharp unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); FT_Set_Char_Size(face, 0, (IntPtr)(16 * 64), 96, 96); string text = "Hello, World!"; int imageWidth = 400; int imageHeight = 50; byte[] imageBuffer = new byte[imageWidth * imageHeight]; int penX = 10; int penY = 35; bool hasKerning = ((int)face->face_flags & (int)FT_FACE_FLAG.FT_FACE_FLAG_KERNING) != 0; uint previousGlyph = 0; foreach (char c in text) { uint glyphIndex = FT_Get_Char_Index(face, (UIntPtr)c); if (hasKerning && previousGlyph != 0 && glyphIndex != 0) { FT_Vector_ kerning; FT_Get_Kerning(face, previousGlyph, glyphIndex, FT_Kerning_Mode_.FT_KERNING_DEFAULT, &kerning); penX += (int)kerning.x >> 6; } FT_Load_Glyph(face, glyphIndex, FT_LOAD.FT_LOAD_RENDER); var bitmap = face->glyph->bitmap; int bitmapLeft = face->glyph->bitmap_left; int bitmapTop = face->glyph->bitmap_top; for (int row = 0; row < bitmap.rows; row++) { for (int col = 0; col < bitmap.width; col++) { int x = penX + bitmapLeft + col; int y = penY - bitmapTop + row; if (x >= 0 && x < imageWidth && y >= 0 && y < imageHeight) { byte pixel = bitmap.buffer[row * bitmap.pitch + col]; int index = y * imageWidth + x; imageBuffer[index] = (byte)Math.Min(255, imageBuffer[index] + pixel); } } } penX += (int)face->glyph->advance.x >> 6; previousGlyph = glyphIndex; } Console.WriteLine($"Rendered '{text}' to {imageWidth}x{imageHeight} buffer"); FT_Done_Face(face); } ``` -------------------------------- ### Set Pixel Size using FT_Set_Pixel_Sizes Source: https://context7.com/ryancheung/freetypesharp/llms.txt Sets the font size directly in pixels. This is the preferred approach for screen rendering where DPI-independent sizing is required. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); var error = FT_Set_Pixel_Sizes(face, 0, 24); if (error == FT_Error.FT_Err_Ok) { Console.WriteLine("Font size set to 24 pixels"); } FT_Done_Face(face); } ``` -------------------------------- ### Select Character Map (FT_Select_Charmap) - C# Source: https://context7.com/ryancheung/freetypesharp/llms.txt Demonstrates how to select a character map (encoding) for a FreeType face. It lists available charmaps and then selects the Unicode encoding. This is crucial for correctly mapping character codes to glyph indices. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); Console.WriteLine($"Number of charmaps: {face->num_charmaps}"); // List available charmaps for (int i = 0; i < face->num_charmaps; i++) { var charmap = face->charmaps[i]; Console.WriteLine($" Charmap {i}: encoding={charmap->encoding}"); } // Select Unicode encoding (most common) var error = FT_Select_Charmap(face, FT_Encoding_.FT_ENCODING_UNICODE); if (error == FT_Error.FT_Err_Ok) { Console.WriteLine("Unicode charmap selected"); } // Other common encodings: // FT_ENCODING_MS_SYMBOL - Microsoft Symbol encoding // FT_ENCODING_APPLE_ROMAN - Apple Roman encoding // FT_ENCODING_ADOBE_STANDARD - Adobe Standard encoding FT_Done_Face(face); } ``` -------------------------------- ### Fixed-Point Conversion Utilities (FreeTypeCalc) - C# Source: https://context7.com/ryancheung/freetypesharp/llms.txt Provides utility methods for converting between FreeType's 26.6 fixed-point format and standard 32-bit integers. This is essential for correctly interpreting font metrics and setting font sizes. ```csharp using FreeTypeSharp; // Convert integer to 26.6 fixed-point (for FreeType input) int pointSize = 16; int fixedPointSize = FreeTypeCalc.Int32ToF26Dot6(pointSize); // 16 * 64 = 1024 Console.WriteLine($"{pointSize} points = {fixedPointSize} in 26.6 format"); // Convert 26.6 fixed-point to integer (for output) int fixedValue = 1536; // From FreeType metrics int pixelValue = FreeTypeCalc.F26Dot6ToInt32(fixedValue); // 1536 / 64 = 24 Console.WriteLine($"{fixedValue} in 26.6 format = {pixelValue} pixels"); // Manual conversion (equivalent operations) int toFixed = 12 << 6; // 12 * 64 = 768 int fromFixed = 768 >> 6; // 768 / 64 = 12 ``` -------------------------------- ### Load Glyph Data using FT_Load_Glyph Source: https://context7.com/ryancheung/freetypesharp/llms.txt Loads a specific glyph into the face's slot using an index. Supports various flags like FT_LOAD_DEFAULT or FT_LOAD_NO_HINTING to control rendering behavior. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); FT_Set_Char_Size(face, 0, (IntPtr)(16 * 64), 96, 96); uint glyphIndex = FT_Get_Char_Index(face, (UIntPtr)'G'); var error = FT_Load_Glyph(face, glyphIndex, FT_LOAD.FT_LOAD_DEFAULT); if (error == FT_Error.FT_Err_Ok) { var metrics = face->glyph->metrics; Console.WriteLine($"Glyph width: {(int)metrics.width >> 6} pixels"); } FT_Done_Face(face); } ``` -------------------------------- ### Load Glyph by Character Code using FreeTypeSharp Source: https://context7.com/ryancheung/freetypesharp/llms.txt Demonstrates the use of FT_Load_Char to load a glyph directly from a character code. This function simplifies the process by combining index retrieval and glyph loading into a single operation. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); FT_Set_Char_Size(face, 0, (IntPtr)(24 * 64), 96, 96); var error = FT_Load_Char(face, (UIntPtr)'H', FT_LOAD.FT_LOAD_RENDER); if (error == FT_Error.FT_Err_Ok) { var bitmap = face->glyph->bitmap; Console.WriteLine($"Character 'H' bitmap: {bitmap.width}x{bitmap.rows} pixels"); } FT_Done_Face(face); } ``` -------------------------------- ### Accessing Android Resource IDs Source: https://github.com/ryancheung/freetypesharp/blob/main/FreeTypeSharp.Android.Test/Resources/AboutResources.txt Demonstrates the structure of the auto-generated Resource class. This class maps resource files to integer tokens that can be used throughout the application. ```csharp public class Resource { public class Drawable { public const int icon = 0x123; } public class Layout { public const int main = 0x456; } public class Strings { public const int first_string = 0xabc; public const int second_string = 0xbcd; } } ``` -------------------------------- ### FreeTypeCalc Utilities Source: https://context7.com/ryancheung/freetypesharp/llms.txt Utility class for converting between FreeType's 26.6 fixed-point format and standard integers. ```APIDOC ## FreeTypeCalc ### Description Utility class for converting between FreeType's 26.6 fixed-point format and standard integers. ### Method Static Methods ### Parameters #### Request Body - **Int32ToF26Dot6** (int) - Converts integer to 26.6 fixed-point. - **F26Dot6ToInt32** (int) - Converts 26.6 fixed-point to integer. ### Request Example int fixedValue = FreeTypeCalc.Int32ToF26Dot6(16); ### Response #### Success Response (200) - **result** (int) - The converted numeric value. ``` -------------------------------- ### FT_New_Memory_Face - Load Font from Memory Source: https://context7.com/ryancheung/freetypesharp/llms.txt Loads a font face from a memory buffer, useful for embedded resources or network streams. ```APIDOC ## FT_New_Memory_Face ### Description Loads a font face from a memory buffer. ### Method C# Unsafe Method ### Endpoint FT.FT_New_Memory_Face(FT_Library library, byte* file_base, int file_size, int face_index, FT_Face* aface) ### Parameters #### Path Parameters - **library** (FT_Library) - Required - The FreeType library handle. - **file_base** (byte*) - Required - Pointer to the font data in memory. - **file_size** (int) - Required - Size of the font data. - **face_index** (int) - Required - The index of the face in the data. - **aface** (FT_Face*) - Required - Pointer to the face handle to be created. ### Response #### Success Response (FT_Err_Ok) - **error** (FT_Error) - Returns 0 on success, otherwise an error code. ``` -------------------------------- ### FT_Outline Operations Source: https://context7.com/ryancheung/freetypesharp/llms.txt Access and manipulate glyph outlines for vector operations and custom rendering. ```APIDOC ## FT_Outline Operations ### Description Access and manipulate glyph outlines for vector operations and custom rendering. ### Method C# Method Calls ### Parameters #### Path Parameters - **outline** (FT_Outline*) - Required - The outline structure to manipulate. - **matrix** (FT_Matrix*) - Optional - Transformation matrix for scaling/rotation. ### Request Example FT_Outline_Transform(&outline, &matrix); ### Response #### Success Response (200) - **status** (void) - Modifies the outline structure in-place. ``` -------------------------------- ### Handle FreeType Errors in C# Source: https://context7.com/ryancheung/freetypesharp/llms.txt Demonstrates how to handle FreeType errors using the FT_Error enum and the FreeTypeException wrapper in C#. It shows checking specific error codes and throwing exceptions for unrecoverable issues. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System; using System.Runtime.InteropServices; unsafe { try { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; // Attempt to load non-existent font var error = FT_New_Face( library.Native, (byte*)Marshal.StringToHGlobalAnsi("nonexistent.ttf"), 0, &face ); // Check specific error codes switch (error) { case FT_Error.FT_Err_Ok: Console.WriteLine("Font loaded successfully"); break; case FT_Error.FT_Err_Cannot_Open_Resource: Console.WriteLine("Cannot open font file"); break; case FT_Error.FT_Err_Unknown_File_Format: Console.WriteLine("Unknown or unsupported font format"); break; case FT_Error.FT_Err_Invalid_File_Format: Console.WriteLine("Font file is corrupted"); break; default: Console.WriteLine($"Error: {error}"); break; } // Using wrapper that throws FreeTypeException if (error != FT_Error.FT_Err_Ok) { // FreeTypeException provides descriptive error messages throw new FreeTypeException(error); } } catch (FreeTypeException ex) { Console.WriteLine($"FreeType error: {ex.Message}"); } } ``` -------------------------------- ### Select Bitmap Strikes for Fixed-Size Fonts Source: https://context7.com/ryancheung/freetypesharp/llms.txt Illustrates how to detect and select specific bitmap strikes for fonts that contain embedded bitmap data, such as emoji or bitmap-only fonts. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("emoji.ttf"), 0, &face); var facade = new FreeTypeFaceFacade(library, face); if (facade.HasFixedSizes && facade.HasBitmapStrikes) { int targetSize = 32; int bestIndex = facade.FindNearestMatchingPixelSize(targetSize); facade.SelectFixedSize(bestIndex); Console.WriteLine($"Selected size index {bestIndex} for target {targetSize}px"); } FT_Done_Face(face); } ``` -------------------------------- ### Retrieve Glyph Index using FT_Get_Char_Index Source: https://context7.com/ryancheung/freetypesharp/llms.txt Maps a character code to its corresponding glyph index within the font. Returns 0 if the character is not supported by the font file. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); uint glyphA = FT_Get_Char_Index(face, (UIntPtr)'A'); uint glyphMissing = FT_Get_Char_Index(face, (UIntPtr)0x1F600); Console.WriteLine($"Glyph index for 'A': {glyphA}"); FT_Done_Face(face); } ``` -------------------------------- ### FT_Stroker Source: https://context7.com/ryancheung/freetypesharp/llms.txt Create stroked (outlined) versions of glyphs for text effects like borders and outlines. ```APIDOC ## FT_Stroker ### Description Create stroked (outlined) versions of glyphs for text effects like borders and outlines. ### Method C# Method Calls ### Parameters #### Path Parameters - **radius** (IntPtr) - Required - Stroke radius in 26.6 format. - **line_cap** (FT_Stroker_LineCap_) - Required - Style of line caps. - **line_join** (FT_Stroker_LineJoin_) - Required - Style of line joins. ### Request Example FT_Stroker_Set(stroker, (IntPtr)(2 << 6), FT_Stroker_LineCap_.FT_STROKER_LINECAP_ROUND, FT_Stroker_LineJoin_.FT_STROKER_LINEJOIN_ROUND, (IntPtr)0); ### Response #### Success Response (200) - **status** (void) - Configures the stroker for subsequent glyph processing. ``` -------------------------------- ### FT_Select_Charmap Source: https://context7.com/ryancheung/freetypesharp/llms.txt Selects a character map (encoding) for a font face to map character codes to glyph indices. ```APIDOC ## FT_Select_Charmap ### Description Selects a character map (encoding) for the font face to map character codes to glyph indices. ### Method C# Method Call ### Endpoint FT_Select_Charmap(FT_FaceRec_* face, FT_Encoding_ encoding) ### Parameters #### Path Parameters - **face** (FT_FaceRec_*) - Required - The font face handle. - **encoding** (FT_Encoding_) - Required - The encoding type to select (e.g., FT_ENCODING_UNICODE). ### Request Example FT_Select_Charmap(face, FT_Encoding_.FT_ENCODING_UNICODE); ### Response #### Success Response (200) - **error** (FT_Error) - Returns FT_Err_Ok if successful. ``` -------------------------------- ### Calculate Kerning Between Glyphs using FreeTypeSharp Source: https://context7.com/ryancheung/freetypesharp/llms.txt Retrieves the kerning vector between two glyphs to adjust spacing. It checks for kerning support in the font face and converts the fixed-point result into pixel units. ```csharp using FreeTypeSharp; using static FreeTypeSharp.FT; using System.Runtime.InteropServices; unsafe { using var library = new FreeTypeLibrary(); FT_FaceRec_* face; FT_New_Face(library.Native, (byte*)Marshal.StringToHGlobalAnsi("arial.ttf"), 0, &face); FT_Set_Char_Size(face, 0, (IntPtr)(16 * 64), 96, 96); bool hasKerning = ((int)face->face_flags & (int)FT_FACE_FLAG.FT_FACE_FLAG_KERNING) != 0; if (hasKerning) { uint leftGlyph = FT_Get_Char_Index(face, (UIntPtr)'A'); uint rightGlyph = FT_Get_Char_Index(face, (UIntPtr)'V'); FT_Vector_ kerning; var error = FT_Get_Kerning(face, leftGlyph, rightGlyph, FT_Kerning_Mode_.FT_KERNING_DEFAULT, &kerning); if (error == FT_Error.FT_Err_Ok) { int kernX = (int)kerning.x >> 6; int kernY = (int)kerning.y >> 6; Console.WriteLine($"Kerning A-V: x={kernX}, y={kernY} pixels"); } } FT_Done_Face(face); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.