Jump to content
How to Install Plugins ×

Label Maker v1.0 published 7/9/2024


Recommended Posts

Label Maker v1.0

 

I remember having one of these when I was a kid:

image.png

This plugin is basically the same thing...

 

image.png

 

 

This plugin is made from this request: https://forums.getpaint.net/topic/127977-suggestion-insert-text-with-color-in-background/

 

 

Download

 

LabelMaker.zip

 

Run the included bat file to install.  Once installed, it will show up under Effects > Text Formations > Label Maker.

 

 

Source Code

 

Spoiler
// Name: Label Maker
// Submenu: Text Formations
// Author: BoltBait
// Title: BoltBait's Label Maker v1.0
// Version: 1.0
// Desc: Render text on a colored background
// Keywords: text|render|labels|maker
// URL: https://BoltBait.com/pdn
#region UICode
TextboxControl Amount1 = ""; // [255] Text
FontFamily Amount2 = "Arial"; // Font
IntSliderControl Amount6 = 72; // [10,200] Size
ColorWheelControl Amount3 = ColorBgra.FromBgr(0, 0, 0); // [PrimaryColor!] Text Color
ColorWheelControl Amount4 = ColorBgra.FromBgr(255, 255, 255); // [SecondaryColor!] Background Color
PanSliderControl Amount5 = new Vector2Double(0.000, 0.000); // Location
#endregion
protected override unsafe void OnDraw(IDeviceContext deviceContext)
{
    string myText = " " + Amount1.Trim() + " ";
  
    // Maintain background
    deviceContext.DrawImage(Environment.SourceImage);

    // Get the size of our canvas
    RectFloat sourceBounds = new RectFloat(Point2Float.Zero, Environment.Document.Size);

    // Create a brush for text and a brush for the background
  
    // Whenever using the color out of a color wheel control, you must double cast it otherwise you'll get
    // the wrong shade of color.
    ISolidColorBrush solidFillBrush = deviceContext.CreateSolidColorBrush((LinearColorA)(SrgbColorA)Amount3);
    ISolidColorBrush backgroundBrush = deviceContext.CreateSolidColorBrush((LinearColorA)(SrgbColorA)Amount4);

    // Set the antialiasing modes
    deviceContext.AntialiasMode = AntialiasMode.PerPrimitive;
    deviceContext.TextAntialiasMode = TextAntialiasMode.Grayscale;

    // Set the text rendering mode
    deviceContext.UseTextRenderingMode(TextRenderingMode.Outline);

    // Prepare for rendering text
    IDirectWriteFactory textFactory = this.Services.GetService<IDirectWriteFactory>();
    IGdiFontMap fm = textFactory.GetGdiFontMap();

    // Select your font to look at the font properties
    FontProperties fp = fm.TryGetFontProperties(Amount2);

    // Use your font properties to create an actual font
    ITextFormat textFormat = textFactory.CreateTextFormat(
        fp.FontFamilyName, // font family name
        null,
        FontWeight.Bold, // font weight
        FontStyle.Normal, // font style
        fp.Stretch, // how to stretch the font
        Amount6); // size in points

    // Combine your text with your text format to create a layout
    ITextLayout textLayout = textFactory.CreateTextLayout(
        myText, 
        textFormat, 
        TextLayoutConstants.DefaultMaxWidth,
        TextLayoutConstants.DefaultMaxHeight);

    // Measure the text we're going to render
    TextMeasurement tm = textLayout.Measure();

    // Make a rectangle of the background strip we're going to color in
    RectFloat r = new RectFloat(sourceBounds.Center.X - (tm.Width/2), sourceBounds.Center.Y - (tm.Height/2), tm.Width, tm.Height);
  
    // Calculate the difference in location based on the [+] control
    float xoffset = r.X * (float)Amount5.X;
    float yoffset = r.Y * (float)Amount5.Y;

    // Here we always offset all drawing by 0.5f (half a pixel) to get cleaner renders
    // as this draws on pixel boundaries instead of between them.
    using (deviceContext.UseTranslateTransform(xoffset + 0.5f, yoffset + 0.5f))
    {
        // Draw the background rectangle
        deviceContext.FillRectangle(r, backgroundBrush);
        // Draw the text
        deviceContext.DrawText(
            myText, // the actual text to render
            textFormat, // format of text defined above
            r, // text location
            solidFillBrush, // our brush, from above
            DrawTextOptions.None); // Options
    }
}

 

 

Enjoy! B) :beer: 

 

  • Like 3
  • Upvote 4
Link to comment
Share on other sites

Woooohooo! A new plugin!

 

Thanks for this @BoltBait, and many thanks also for the annotated source code :)

Link to comment
Share on other sites

6 minutes ago, ReMake said:

@BoltBait, have you compiled the effect in CodeLab?

 

Yes, I used Paint.NET v5.0.13 (latest stable release) and CodeLab v6.12.

 

I assume you've tried building it with PdN 5.1... @Rick Brewster has made some changes to PdN that break CodeLab in this area and this script won't build.  Rick is actively working on designing/coding some changes in this area and when he's done making it easy for plugin authors, I will support those changes in a new version of CodeLab.

 

Until then, just use the latest stable build.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...