{"id":673,"date":"2011-01-12T20:20:22","date_gmt":"2011-01-13T01:20:22","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=673"},"modified":"2021-03-10T19:16:39","modified_gmt":"2021-03-11T00:16:39","slug":"powerbuilder-add-remove-a-font-at-runtime","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/powerbuilder-add-remove-a-font-at-runtime\/","title":{"rendered":"PowerBuilder &#8211; Add \/Remove a Font at Runtime"},"content":{"rendered":"<p>Here is a way to add (and remove) a font to the user&#8217;s machine at runtime. You can also use this to check if a user has a particular font installed since the code also shows how to list all the installed fonts (need MS Word for this). Way back in my PB5 days I coded a process to print checks. Rather than fool with preprinted forms this application printed all of the data to produce a check which could be processed by banks just like any other. The printer would have to use special magnetic ink and we also used a special MICR font for the bank routing and account numbers. Although the application never really made it into the &#8216;real&#8217; world, I always thought the potential for fraud was significant. If the font were dynamically added and removed from the workstation, this potential would have been somewhat less.<\/p>\n<p>The basic process makes use of two windows API calls: AddFontResource and RemoveFontResource. The declarations are as follows (both ANSI and Unicode):<\/p>\n<pre name=\"code\" class=\"VB\">Function int AddFontResourceA(string lpFileName) library &quot;gdi32.dll&quot;\nFunction int AddFontResourceW(string lpFileName) library &quot;gdi32.dll&quot;\nFunction int RemoveFontResourceA(string lpFileName) library &quot;gdi32.dll&quot;\nFunction int RemoveFontResourceW(string lpFileName) library &quot;gdi32.dll&quot;<\/pre>\n<p>To demonstrate the techniques, I&#8217;ve created a sample application.<\/p>\n<p><a href=\"http:\/\/anvil-of-time.com\/wordpress\/wp-content\/uploads\/2011\/01\/LoadFonts1.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-676 size-full\" title=\"Add &amp; Remove Font Example\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts1.png\" alt=\"\" width=\"426\" height=\"316\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts1.png 426w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts1-300x222.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts1-150x111.png 150w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/a><\/p>\n<p>In the &#8216;Load Fonts&#8217; button clicked event there is the following:<\/p>\n<pre name=\"code\" class=\"VB\">integer li_fonts, li_i\nlong\tll_row\nOLEObject lole_word, lole_fonts\nstring ls_file_name\ndw_1.reset() \/\/ clear existing entries\n\nlole_word = CREATE oleobject\nlole_fonts = CREATE oleobject\nTRY\n\tlole_word.connecttonewobject(&#039;word.application&#039;)\nCATCH (runtimeerror a)\n\tMessagebox(&#039;Error&#039;,&#039;Error connecting with MS Word. Process terminating.&#039;)\n\tRETURN -1\nEND TRY\nlole_word.visible = FALSE \/\/ dont want to see word\nlole_fonts = lole_word.fontnames\nli_fonts = lole_fonts.count\n\/\/ build font list in datawindow\nFOR li_i = 1 to li_fonts\n\tll_row = dw_1.insertrow(0)\n\tdw_1.setitem(ll_row,&#039;fontname&#039;,lole_fonts.item(li_i))\nNEXT\ndw_1.sort()\nlole_word.quit() \/\/ close word\nDESTROY lole_fonts\nDESTROY lole_word<\/pre>\n<p>This uses OLE with MS Word to retrieve the list of font names installed on the system.<\/p>\n<p><a href=\"http:\/\/anvil-of-time.com\/wordpress\/wp-content\/uploads\/2011\/01\/LoadFonts2.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-677 size-full\" title=\"Font List Populated\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts2.png\" alt=\"\" width=\"426\" height=\"316\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts2.png 426w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts2-300x222.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts2-150x111.png 150w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/a><\/p>\n<p>There is a font file included with the example which can be used as the temporary font in the demonstration. Run the application and click on the &#8216;Select Font File&#8217; button to choose this file.<\/p>\n<p>Now click the &#8216;Add Font&#8217; button. The font&#8217;s name and a sample of it should appear in the appropriate fields.<\/p>\n<p><a href=\"http:\/\/anvil-of-time.com\/wordpress\/wp-content\/uploads\/2011\/01\/LoadFonts3.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-678 size-full\" title=\"Select Font File\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts3.png\" alt=\"\" width=\"426\" height=\"316\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts3.png 426w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts3-300x222.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts3-150x111.png 150w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/a><\/p>\n<p>If you click the &#8216;Load Fonts&#8217; button again you will see the new font name in the list.<\/p>\n<p><a href=\"http:\/\/anvil-of-time.com\/wordpress\/wp-content\/uploads\/2011\/01\/LoadFonts4.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-679 size-full\" title=\"New Font Added\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts4.png\" alt=\"\" width=\"426\" height=\"316\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts4.png 426w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts4-300x222.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts4-150x111.png 150w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/a><\/p>\n<p>Finally, click the &#8216;Remove Font&#8217; button to remove it. The &#8216;New Font Example&#8217; text will now revert.<\/p>\n<p>If you click the &#8216;Load Fonts&#8217; button again you will see the new font name has been removed from the list.<\/p>\n<p><a href=\"http:\/\/anvil-of-time.com\/wordpress\/wp-content\/uploads\/2011\/01\/LoadFonts5.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-680 size-full\" title=\"New Font Removed\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts5.png\" alt=\"\" width=\"426\" height=\"316\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts5.png 426w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts5-300x222.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/LoadFonts5-150x111.png 150w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/a><\/p>\n<p>The zip archive containing the PB files, exports, and the sample font file can be downloaded <a href=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/01\/addfont.zip\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a way to add (and remove) a font to the user&#8217;s machine at runtime. You can also use this to check if a user has a particular font installed since the code also shows how to list all the installed fonts (need MS Word for this). Way back in my PB5 days I&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,10],"tags":[13,16,42],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/673"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=673"}],"version-history":[{"count":10,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/673\/revisions"}],"predecessor-version":[{"id":1919,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/673\/revisions\/1919"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}