View Single Post
Old 01-14-2020, 10:12 AM   #9
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Quote:
Originally Posted by mespotine View Post
Any idea on how to do that properly? Would be really helpful for us right now...
I don't have Reaper on my work machine so I can't test this, but something like:
Code:
local scale = 1.3
local CHAR = "M"

local scaledFontCache = {
  --[[
  Will have the following structure:

  Arial = {     font face
    [12] = 18   original point size: scaled point size
  }
  ]]--
}

local fontSizeCache = {
  --[[
  Will have the following structure:

  Arial = {     font face
    [12] = 32   point size: pixel size
  }
  ]]--
}

local function getScaledFontSize(font, size)
  -- See if we've scaled this font + size already
  local cached = scaledFontCache[font] and scaledFontCache[font][size]
  if cached then return cached end

  -- Initialize tables for this font if necessary
  if not scaledFontCache[font] then 
    scaledFontCache[font] = {} 
    fontSizeCache[font] = {}
  end

  gfx.setfont(font, size)
  local _, originalHeight = gfx.measurestr(CHAR)
  local targetHeight = originalHeight * scale

  -- Increase the size and measure until we hit the target height
  -- This could be done more efficiently with something like a binary search
  local currentSize = size
  local currentHeight = 0
  repeat
    currentSize = currentSize + 1

    -- See if we've measured this font + size already
    if fontSizeCache[font][currentSize] then
      currentHeight = fontSizeCache[font][currentSize]
    else
      gfx.setfont(font, currentSize)
      _, currentHeight = gfx.measurestr(CHAR)

      -- Add this measurement to the size cache
      fontSizeCache[font][currentSize] = currentHeight
    end
  until (currentHeight >= targetHeight)

  -- Add this scaled font + size to the cache
  scaledFontCache[font][size] = currentSize

  return currentSize
end
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote