RightAlign Text in Graphics

Hi

I’m trying to Right-Align text in a PDF on a Web project without success. The same code in a Desktop project seems to work fine.

Am I doing something wrong or is there a known issue?

Here are the links to both projects:

https://obatics.ie/downloads/samples/RightAlign_DeskTop.xojo_binary_project
https://obatics.ie/downloads/samples/RightAlign_Web.xojo_binary_project

Thanks for any time and help ye can provide

Chris

I’ve been dealing with the same issue. Apparently there is a bug in how this is handled in Web.

Thanks Hector. At least I know now - it saves losing more hair :smiley:

I’ll need to work on this again in the next couple of days so I’ll keep you posted on anything I find

1 Like

What is it doing wrong? I don’t have the latest version, so I can not run the code.
I am right aligning in my webapp without any issues.

Right align in the web app is done in CSS/HTML.

To right align in graphics requires measuring the text, and then set the drawstring left as
g.width-g.Stringwidth(text)

Problem is if your host does not have the font that you want to use, stringwidth won’t work. In Xojo Cloud, one can add fonts to the host. In a VPS, it is probably more difficult. Unless Lifeboat takes care of that, I don’t know.

It’s not calculating the length of the text correctly, so aligning to the right is difficult/impossible

Lifeboat doesn’t take care ot if yet. I have added the fonts using copyfonts and attempted to add the fonts manually to a VPS server but not luck getting this to work correctly yet. I’ll give it a try in a couple of days and report back

g.TextWidth(“ABC”) return 0, it is a bug or have at least a not supported error for web.
Xojo 2021r3.1
and TextShape is not supported too because drawobject is not supported^^
chaos framework …

That is exactly the issue I reported. More vicious even, when you debug on your desktop, since it has all the fonts, the issue won’t show.

But once the app runs on a host, if fonts are not present, indeed textwidth and stringwidth fail.

I never had to add the fonts manually, so I did not know if lifeboat would help.

I suppose the usual way to install Linux fonts would work, but it may depend on the distro. It is also quite possible one has to install the desktop as well.

Before we go blaming the server and font, try running the web project on your desktop. This is what I get running the web project in debug mode on macOS, with, confirmed, Helvetica installed.

There’s a problem of some kind with Web / PDFGraphics that needs to be fixed.

I’m a little out of my depth with all of this, I’m afraid. I looks like it’s a problem running on Windows DeskTop as well:

I wonder is it a PDFDocuments bug? I moved from from DynaPDF to create my reports where it wasn’t an issue…

You did not read what I posted. The issue comes indeed from hosts not having the necessary fonts. On a desktop, such as Mac or Windows, all the fonts are there.

In Xojo web, it is possible to add fonts by simple copy. years ago, Phillip Zedalis added fonts for me on 1701.

I am not familiar enough with Linux in host version to know where the fonts can be installed. But it must be possible.

You did not read what I posted. My screenshot comes from running the web app on Desktop, where indeed I have the fonts installed.

You’re jumping to blame the server when it doesn’t even work on Mac Desktop with the fonts installed. Chris O’s post shows that it fails on Windows Desktop too.

2 Likes

Maybe this has to do with: #68914

Read the comments from Javier:

Yeah, the problem is in the underlying Graphic class for Console (Web) reporting different width values than those reported for Desktop… so the PDF draws the text at it expected size, but the width doesn’t gets the proper value.

1 Like

I’ve just spent the last couple of hours researching installing these fonts on Linux. The answers suggest the font metrics files which are provided by Adobe. Installing the fonts themselves makes you responsible for following the license for each of them.

As I understand it, installing the core-14 shouldn’t be a problem we have to face as the PDF generating software (Xojo) should be aware of the font metrics of the core-14.

1 Like

When I installed on my CentOS server, it turned out to be worse. All StringWidths returned Zero. So I decided just to try to figure the width out myself. It’s a bit clunky, but if anyone really needs to right or centre justify at the moment, it might help.

On the Mac, I created a Json table of HelveticaNeue 12 Point Char Widths for Bold and Plain text using the following code

dim chars() as string
dim widths() as Double
dim bWidths() as Double

dim p as new Picture(300,100)
dim g as Graphics=p.Graphics
g.FontName="HelveticaNeue"
g.FontSize=12
g.FontUnit=FontUnits.Point

dim s as string=TextArea1.Text

dim jWidths as new JSONItem
for index as integer=0 to s.Length-1
  jWidths.Value(s.Middle(index,1))=g.TextWidth(s.Middle(index,1))
next

g.Bold=True

for index as integer=0 to s.Length-1
  jWidths.Value("b"+s.Middle(index,1))=g.TextWidth(s.Middle(index,1))
next


TextArea1.Text=jWidths.ToString

(I prefix the Char with ‘b’ for the BOLD chars)

The contents of TextArea1 allowed me to create a constant HELVETICANEUE12

Then I used the following function to return the estimated width of a string:

Public Function CharWidth(S as String, PointSize as Double = 12, Bold as Boolean = False) As Double
  if s="" then Return 0
  
  if Widths=nil then
    try
      Widths=new JSONItem(HELVETICANEUE12)
    catch err
      dim xx as integer
    end try
  end if
  
  dim ws as Double
  for index as integer=0 to S.Length-1
    dim found as Boolean

    //I understand these are the average widths of bold and non-bold characters
    //When the character width isn't found
    dim w as Double=583.44/514
    if bold then
      w=613.25/684
    end if
    
    if Widths<>nil then
      dim key as string=S.Middle(index,1)
      if Bold then 
        key="b"+key
      end if
      w=Widths.Lookup(key,w)
    end if
    w=w*PointSize/12
    ws=ws+w
  next
  
  Return ws
  
  
End Function

It seems to work well on all platforms. I’m sure it’s needless to say, If you need to use a different font you need to add it and make the json keys more complex.

1 Like

The last time I had to do this was circa 1995 on DOS.

Congratulations, Chris.

Creating a character width table is a nice workaround to the lack of fonts on the server.