Changing font in example

I’m using the SimpleControls example as a boiler plate.
First thing was that I wanted to change the font.
In config.h I changed line 54
from #define ROBOTO_FN "Roboto-Regular.ttf"
to __ #define WORKSANS_FN "WorkSans-Regular.ttf"

In SimpleControls.cpp I changed line 82
from pGraphics->LoadFont("Roboto-Regular", ROBOTO_FN);
to __ pGraphics->LoadFont("Roboto-Regular", WORKSANS_FN);

I also put the font file WorkSans-Regular.ttf into the ressources/fonts folder and removed the Roboto-Regular.ttf file.
Result: It works!

However, there is still one mention of Roboto-Regular to change. When editing line 82 in SimpleControls.cpp like so:
pGraphics->LoadFont("WorkSans-Regular", WORKSANS_FN);

It is compiling OK, but on runtime it will abort like this:

 ../iPlug2/IGraphics/Drawing/IGraphicsNanoVG.cpp:599: void iplug::igraphics::IGraphicsNanoVG::PrepareAndMeasureText(const iplug::igraphics::IText&, const char*, iplug::igraphics::IRECT&, double&, double&) const: Assertion `nvgFindFont(mVG, text.mFont) != -1 && "No font found - did you forget to load it?"' failed.
Aborted (core dumped)

According to the reference the first parameter in LoadFont is the “fontID”.

I expect that the problem here is to do with the fact that the vector controls such as IVKnobControl have two text styles as part of their IVStyle, one for the label and one for the value. The default font is Roboto, so if you don’t explicitly set the style of the control to one which overrides the label and value font, and then you don’t load Roboto it will crash.

Does that make sense?

I don’t think that’s the issue. All fonts on the controls are now WorkSans. I even deleted Roboto from the ressources/fonts folder and it doesn’t complain on compile or render. Just changing that one mention of Roboto in the code somehow makes problems.
I mean, in the end I got what I wanted, it is just that I don’t understand the issue.

I believe there are some controls that will still be requiring Roberto since their style is not changed. Iplugcontrols is a complex example with lots of controls might be better to start with a simpler example

it’s the SimpleControls

I don’t know what that is I’m afraid

I should have mentioned I’m on Linux. Could it be related to that?

iPlug2 doesn’t yet support Linux so probably! There is no example called SimpleControls

1 Like

Ah, oops. My colleague distilled SimpleControls from Iplugcontrols.

I also faced the fact that an error occurs if in this line pGraphics->LoadFont write a different value than Roboto-Regular. However, if you do not change the Roboto-Regular parameter, then any font can be loaded.
I also successfully loaded the OTF format font, but for this I made changes in the IGraphicsWin.cpp file in function LoadPlatformFont in this way:

PlatformFontPtr IGraphicsWin::LoadPlatformFont(const char* fontID, const char* fileNameOrResID)
{
  StaticStorage<InstalledFont>::Accessor fontStorage(sPlatformFontCache);

  std::unique_ptr<InstalledFont> pFont;
  void* pFontMem = nullptr;
  int resSize = 0;
  WDL_String fullPath;
 
  EResourceLocation fontLocation = LocateResource(fileNameOrResID, "ttf", fullPath, GetBundleID(), GetWinModuleHandle(), nullptr);

  if (fontLocation == kNotFound)
  {
    fontLocation = LocateResource(fileNameOrResID, "otf", fullPath, GetBundleID(), GetWinModuleHandle(), nullptr);
  }

  if (fontLocation == kNotFound)
    return nullptr;

  switch (fontLocation)
  {
    case kAbsolutePath:
    {
      HANDLE file = CreateFile(fullPath.Get(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
      if (file)
      {
        HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
        if (mapping)
        {
          pFontMem = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
          pFont = std::make_unique<InstalledFont>(pFontMem, resSize);
          UnmapViewOfFile(pFontMem);
          CloseHandle(mapping);
        }
        CloseHandle(file);
      }
    }
    break;
    case kWinBinary:
    {
      pFontMem = const_cast<void *>(LoadWinResource(fullPath.Get(), "ttf", resSize, GetWinModuleHandle()));
      if (pFontMem == nullptr)
        pFontMem = const_cast<void *>(LoadWinResource(fullPath.Get(), "otf", resSize, GetWinModuleHandle()));
      pFont = std::make_unique<InstalledFont>(pFontMem, resSize);
    }
    break;
  } 

  if (pFontMem && pFont && pFont->IsValid())
  {
    IFontInfo fontInfo(pFontMem, resSize, 0);
    WDL_String family = fontInfo.GetFamily();
    int weight = fontInfo.IsBold() ? FW_BOLD : FW_REGULAR;
    bool italic = fontInfo.IsItalic();
    bool underline = fontInfo.IsUnderline();

    HFONT font = GetHFont(family.Get(), weight, italic, underline);

    if (font)
    {
      fontStorage.Add(pFont.release(), fileNameOrResID);
      return PlatformFontPtr(new Font(font, "", false));
    }
  }

  return nullptr;

And in the resource file main.rc you need to write the following:
FONT_FILENAME OTF FONT_FILENAME

1 Like