What are some ways to utilize Windows GDI functions in .NET that are not available in GDI+?

I need assistance in accessing a GDI method that seems to be missing in GDI+ within my .NET application.

Specifically, I am looking for information on this particular method which retrieves kerning pairs for a specified font. My goal is to incorporate kerning (letter-spacing) into HTML5 canvas, even though it is not currently supported. I believe extracting kerning pairs on my server and providing a kerning table to the client would be the best approach.

Could someone guide me on how to utilize Windows library functions like this from within .NET?

Answer №1

If you're looking to utilize this technique, check out the Windows API for implementing it:

  • 1 : Include the System.Runtime.InteropServices namespace in your project
  • 2 : Incorporate the API class into your project and utilize the GetKerningPairs method. Remember that this function relies on a struct called KERNINGPAIR, so it must be properly defined in our class to avoid compilation errors!

    class API
    {
        [DllImport("gdi32.dll")]
        static extern uint GetKerningPairs(IntPtr hdc, uint nNumPairs,
        [Out] KERNINGPAIR[] lpkrnpair);
    
        [StructLayout(LayoutKind.Sequential)]
        struct KERNINGPAIR
        {
            public ushort wFirst; // could potentially be defined as char
            public ushort wSecond; //could potentially be defined as char
            public int iKernAmount;
    
            public KERNINGPAIR(ushort wFirst, ushort wSecond, int iKernAmount)
            {
                this.wFirst = wFirst;
                this.wSecond = wSecond;
                this.iKernAmount = iKernAmount;
            }
    
            public override string ToString()
            {
                return (String.Format("{{First={0}, Second={1}, Amount={2}}}", wFirst, wSecond, iKernAmount));
            }
        }
    }
    

You can now access this method through the API class.

Answer №2

It seems like you might be facing a bit of trouble.

As mentioned on this forum thread

There doesn't seem to be specific classes for kerning pairs. Glyphs will provide default values for glyph indices and advance widths.

You could potentially give pinvoke a try, but as stated in the post,

Keep in mind: it looks like the data returned is limited to the default unicode block.

There might be a method to access more information by changing the code page (just my speculation).

The only way I've found to obtain ALL kerning data is by directly parsing the files; definitely not an easy task.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Timeout occurred while acquiring a turn-based concurrency lock for the actor '{actorName}' after {time}

My service is responsible for creating Actors of a certain type with specific names: var storer = this.serviceClient.Create<IStorer>(new ActorId(agencyToProcess.Name)); After creating the Actor, I then call a method within it: await storer.StoreSt ...

The functionality of opening a new tab when clicking on a link image is not functioning correctly on Mozilla, whereas it

Check out my code below: <a target="_blank" href="#" onclick="window.open('https://www.google.com','_blank');"> <img src="#{request.contextPath}/resources/img/landing-page/terdaftar-kominfo.png" /> </a> ...

Tips for creating a website loading screen that allows users to fully immerse themselves in the experience

I am looking to improve the visibility of the loading screen that I have created. Ideally, it should last around 3-5 seconds using JQuery, as it is already in use on the website. Below is the code snippet: Additionally, I am not very familiar with JQuery, ...

Validating an item within an enumeration

In my enum, I store various UI element values organized as follows: const uiElementAttributes = { 1: { id: 1, color: 'red', icon: 'icon-something.png' }, 2: { id: 2, color: 'yellow', ...

Truncating decimal points in JavaScript

In this scenario, if the number after the decimal point is 0, then the zero should be removed. Otherwise, the number should be displayed as it is. Here are some examples of what I am aiming for in vue: 100.023 => 100.023 1230.0 => 1230 ...

The onsubmit event in Javascript activates the parent's onclick event

On my HTML page, I have a table with a form in each row. Clicking on the row should open a detail page, but clicking within the form should not trigger this action. Here is an example of what my row looks like: <tr onclick="window.parent.location.href ...

Execute the SubmitChanges() function right before calling a stored procedure

When running a stored procedure after the SubmitChanges() function, the stored procedure is executed before the SubmitChanges() process is completed. The purpose of this function is to transfer data from another server (involving multiple inserts). Initia ...

Alert from module (from ./node_modules/laravel-mix/node_modules/postcss-loader/dist/cjs.js):

I'm facing an issue while trying to set up the laravel breeze package for default authentication in my Laravel 9 project. After running npm run dev, I encountered a Laravel Mix error. WARNING in ./resources/css/app.css (./node_modules/css-loader/dis ...

Leveraging Jest for mocking "import * as" operations

Is there a way to effectively mock this specific type of import using jest? import * as some from 'some-package'; ...

tips for accessing a PHP variable within an AJAX success function and setting it as a JavaScript variable

How can I access a PHP back end variable in a front-end AJAX success callback function? Here is my PHP code: if($_POST["action"] == 'check_ot_count') { $totaltime = 0; $ot_hours = $_POST["test"]; $month = $_P ...

Troubleshooting issues with NodeJS and Express authentication middleware functionality

I am currently working on implementing the isUserAuthenticated function to run on every server request. This function is required in the app.js file and utilized using app.use(authenticate.isUserAuthenticated). In my project, there is an /authenticate rou ...

Looking for a particular root node in a tree structure?

I am currently working on converting docx files to xml and using DOM to locate the parent node of a specific element. For instance <chapter> <title> <label>Chapter 1 </label> </title> ...

Implementing Bootstrap 4 accordions within a Knockout foreach loop, adjusting the icon upon expansion/collapse dilemma

I have a collection of collapsible sections within a main collapsible section like: <div id="parentAccordion" class="card-accordion"> <div class="card"> <div class="card-header bg-black text-white pointer-cursor"> ...

An unspecified error arose in GDI+ while attempting to store the image

I am attempting to convert a web form into an image file using the following code: using System.Drawing.Printing; using System.Drawing; using System.Drawing.Imaging; protected void btntopdf_Click(object sender, EventArgs e) { ...

Content is not a valid property within the task

Is there a solution for the error message shown below? I tried following a tutorial but I keep receiving a red line indicating that there is no definition of Content. https://www.youtube.com/watch?v=IVvJX4CoLUY private void UploadFile_Cliked(object sender ...

Angular instructions

I am fairly new to working with Angular. Currently, I am using Angular 1.6 and I have been tasked with making some modifications to an existing application. After doing some research, I discovered that the previous developer had used in order to create a ...

Experiencing inaccuracies in Magento's item validation process when checking the quantity of items being added to the cart

Upon entering a text string in the quantity field of the "Add to Cart" input box, Magento does not display an error message but instead considers it as a quantity of "1". Is there a way to modify this behavior and have the validation system mark strings ...

Stop objects from shifting while easily applying a border

I have a code that adds a red border around elements when you mouseover them and removes it when you mouseout. However, the elements jump around when the border is added because it changes their dimensions. Is there a way to stop this jumping behavior? ...

Masking credit card numbers in an Asp.net text box upon entry, validating the input, and securely processing the credit card information

I am currently maintaining an Asp.Net web application that deals with processing credit card payments. However, in order to comply with new regulations, I am required to mask the credit card number as it is being entered. For example, if the first number e ...

Unexpected Issue with JavaScript Ajax (Using jQuery.post): The Promise State Turns to "Rejected"

Recently, I've been encountering some issues while trying to debug my jQuery.post() call. The responses I'm getting are quite puzzling and I'm at a loss on how to proceed next. If anyone has any suggestions or insights, I would greatly appre ...