Author Topic: Setting a specific font programmatically  (Read 887 times)

0 Members and 1 Guest are viewing this topic.

Eva

  • Jr. Member
  • **
  • Posts: 19
Setting a specific font programmatically
« on: February 14, 2018, 06:19:32 AM »
Hello,

Could someone help me and explain how the fontID value (in ShapeSheet cell Character – Font) is determined? Is it based on the template, or can different computers have different fontIDs even when using the same template? For example, is the Arial fontID (number value) always the same in documents, which use the same template? If I put programmatically a style's fontID value to "4" (on my computer 4 = Arial) in a template, is the style font “Arial” in this template on all computers? It seems that even on the same computer the value of Arial fontID varies in different documents (using different templates)?

All help is appreciated!
Br,
Eva

Surrogate

  • Hero Member
  • *****
  • Posts: 826
    • ShapeSheet™ Knowledge Base
Re: Setting a specific font programmatically
« Reply #1 on: February 14, 2018, 07:28:12 AM »
Hi, Eva !

You can also use FONTTOID function !
Font properties in ShapeSheet functions list
« Last Edit: February 14, 2018, 07:37:02 AM by Surrogate »

Eva

  • Jr. Member
  • **
  • Posts: 19
Re: Setting a specific font programmatically
« Reply #2 on: February 14, 2018, 07:45:38 AM »
Thank you for quick response :)!
I use now Visio 2016 version. The problem is that I have already made the code, and now I am wondering that is it a problem, although it seems to work right at least on two different computers. But I was wondering how the font number is determined, because in some other drawing (based on other template) the Arial fontID is different. I just don't understand how this goes.

I was also wondering, if the FONTTOID()  is a good way to set the font, is it somehow slower for Visio?

Br,
Eva

Surrogate

  • Hero Member
  • *****
  • Posts: 826
    • ShapeSheet™ Knowledge Base
Re: Setting a specific font programmatically
« Reply #3 on: February 14, 2018, 07:47:35 AM »
You mean VBA code?

Eva

  • Jr. Member
  • **
  • Posts: 19
Re: Setting a specific font programmatically
« Reply #4 on: February 14, 2018, 07:48:35 AM »
I mean C# code.

Surrogate

  • Hero Member
  • *****
  • Posts: 826
    • ShapeSheet™ Knowledge Base
Re: Setting a specific font programmatically
« Reply #5 on: February 14, 2018, 07:58:33 AM »
I haven't Visual Studio there and great experience in C# :(

this code in VBA works
Code: [Select]
shp.CellsSRC(visSectionCharacter, 0, visCharacterFont).FormulaU = "Fonttoid(""Arial"")"

Surrogate

  • Hero Member
  • *****
  • Posts: 826
    • ShapeSheet™ Knowledge Base
Re: Setting a specific font programmatically
« Reply #6 on: February 14, 2018, 08:13:59 AM »
May be something like
Code: [Select]
Cell CellFont = shp.get_CellsSRC((short)VisSectionIndices.visSectionCharacter, 0, (short)VisCellIndices.visCharacterFont);
CellFont.FormulaU = "FONT(""Arial"")";
please try this code

Eva

  • Jr. Member
  • **
  • Posts: 19
Re: Setting a specific font programmatically
« Reply #7 on: February 14, 2018, 08:29:03 AM »
Yes, I know now that I should have written it differently.
But the question is, that is it a problem that I have written the value "4" instead of  "Fonttoid(\"Arial\")" to the cell, if it always uses the same template?
Does the fontID remain always the same (Arial = 4) in the same template?

Surrogate

  • Hero Member
  • *****
  • Posts: 826
    • ShapeSheet™ Knowledge Base
Re: Setting a specific font programmatically
« Reply #8 on: February 14, 2018, 08:34:26 AM »
Not allways !
On my side function Fonttoid("Arial") have value  21 !
As I understand this function need for create system-independed solutions. It automatically get local fontID !

Eva

  • Jr. Member
  • **
  • Posts: 19
Re: Setting a specific font programmatically
« Reply #9 on: February 14, 2018, 08:44:53 AM »
But if you used the same template as I, would you then have the same fontID? When I use this specific template, the Fonttoid("Arial") value is 4, but if I open a new blank Visio drawing, then Fonttoid("Arial") has value 21.

Surrogate

  • Hero Member
  • *****
  • Posts: 826
    • ShapeSheet™ Knowledge Base
Re: Setting a specific font programmatically
« Reply #10 on: February 14, 2018, 08:53:47 AM »
I think so !
Quote from: MSDN
Note
In most cases, the font identifier is system-specific. Although the font remains established once used in a file, the FONT function provides consistent access to a particular font across systems and versions of Visio. It is recommended that you use the FONT function to assign fonts instead of referring to font identifiers directly.
proof

Paul Herber

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2627
    • Paul Herber's website
Re: Setting a specific font programmatically
« Reply #11 on: February 14, 2018, 09:51:10 AM »
I must admit I've never understood the whys and wherefores of using a font ID rather than the font name, no other software I know of does such a thing. Some time ago I created some shapes for a company and they specified a certain font (a corporate thing), the font came out 1 off from what they wanted, to them it came out as Algerian or something!
Electronic and Electrical engineering, business and software stencils and applications for Visio -

http://www.paulherber.co.uk/

Eva

  • Jr. Member
  • **
  • Posts: 19
Re: Setting a specific font programmatically
« Reply #12 on: February 14, 2018, 10:41:14 AM »
Thank you very much for all this information!

Eva

  • Jr. Member
  • **
  • Posts: 19
Re: Setting a specific font programmatically
« Reply #13 on: February 15, 2018, 08:25:11 AM »
I found the cause of the problem: when I save the drawing.vsd to drawing.vsdx, the fontID changes when the drawing is opened next time. The font name remains the same ("Arial"), but its ID in the Font cell changes (from 4 to 21). Otherwise it seems to remain the same in the same drawing / template, but it changes when the format changes. Thank you for helping me find this out!