Golang vips: How to render text with custom truetype font? - go

When we want to render text an vips image, you do something like this with vips_text:
import "C"
var textImage *C.VipsImage
cText := C.CString("Some text")
cFont := C.CString("Arial 12px")
C.cgo_vips_text(&textImage, cText, cFont)
But here, Arial 12px is a fontconfig string name and assumes that the system has this font installed.
How do I let the program use a custom truetype font file, e.g., Roboto.ttf?
Trying
cFont := C.CString("Roboto.ttf")
probably won't work.
We could put the Roboto.ttf file in the system directory, for example, in Ubuntu you put the file in /usr/share/fonts/truetype, but the code makes the assumption that this font exists, which is not a solution to write machine-independent program. So I wonder if there is a better way to do this so that we can ship the executables with the font file together.

vips uses standard fontconfig font names, so you need to set up fontconfig to add your custom font to its search path.
From the manual it looks like you can set the environment variable XDG_CONFIG_HOME to point to your new font, then from go refer to that font as Roboto 48pt (for example).
It's easy to test from the command-line. Try:
$ fc-match "Roboto"
Roboto-Regular.ttf: "Roboto" "Regular"
$ vips text x2.png "hello" --font "Roboto 48px"
$ vips text x.png "hello" --font "Arial 48px"
$ eog x.png x2.png
And make sure you see two different images.

Related

How to use custom fonts in Ghostscript/PostScript?

I convert a PostScript file to PDF by Ghostscript. I have a problem embedding/installing Type 1 fonts.
For installing Type 1 Font, I can add the PFA file path to Ghostscript Fontmap, which should be in /usr/share/ghostscript/version/FONTMAP, but I have no such file in /usr/share/ghostscript/9.50` or similar folders on Ubuntu 20.04.
How can I include the font file directly within the script:
Instead of
/Times-Bold findfont 10 scalefont setfont
something like
(/home/font.pfa) 10 scalefont setfont
Does PostScript/Ghostscript use AFM file data or read the glyph widths or just from the glyph structure provided in PFA file?
The fonts and Fontmap file can be placed in several directories. Here is a typical search path:
/usr/share/ghostscript/9.52/Resource/Init/Fontmap
/usr/share/ghostscript/9.52/lib/Fontmap
/usr/share/ghostscript/9.52/Resource/Font/Fontmap
/usr/share/ghostscript/fonts/Fontmap
/usr/share/fonts/Type1/Fontmap
/usr/share/fonts/Fontmap
I sometimes use fonts that are not installed in the search path just in the current working directory. I use the gs -P and either of these work:
(font.pfa) 12 selectfont
/font.pfa 12 selectfont
The search path can also be modified by adding the directories to the GS_FONTPATH or GS_LIB environment variables.
The AFM file is not mandatory and the metrics can be obtained from the font alone. Some programs use the AFM file instead of the actual fonts and so they are needed for those programs.
You do, you just have the path/name slightly wrong:
/usr/share/ghostscript/9.50/Resource/Init/Fontmap.GS
You can also use your own custom fontmap using a command line parameter: "-sFONTMAP=/path/to/custom/fontmap" (best to copy the system one, and add your customisations to the copy)
You can't, not like that, anyway - that's not how Postscript works. Postscript always references fonts by name (not by file/path), so whilst there ways to read the font file(s), you still need to know the font name(s) in order to scale and set the font(s).
Ghostscript does not use AFM files, it gets the metrics from the fonts and glyph outlines.
Hope that helps some....

Ghostscript changed Orientation from landscape to portrait

I have a PDF file that I need to change the orientation from landscape to portrait, using this command GS. The Orientation switch it is not changing the orientation. Is there a extra setting I need to add?
::Path to your Ghostscript EXE
set GSC="C:\Program Files (x86)\gs\gs9.16\bin\gswin32.exe"
%GSC% -sDEVICE=pdfwrite -sOutputFile="D:\TRANS\PDF\2_ZNOR_00000000000000000070.pdf" -dNOPAUSE -dEPSCrop -c "<</Orientation 3>> setpagedevice" -f "D:\TRANS\PDF\1_ZNOR_00000000000000000070.pdf"
It worked for me
List<string> switches = new List<string>
{
"-empty",
"-dQUIET",
"-dSAFER",
"-dBATCH",
"-dNOPAUSE",
"-dNOPROMPT",
"-dDEVICEHEIGHTPOINTS=612",
"-dDEVICEWIDTHPOINTS=792",
"-dFIXEDMEDIA",
"-dFitPage",
"-sDEVICE=pdfwrite",
"-dAutoRotatePages=/None",
"-sOutputFile=" + resultFilePath,
"-c",
"<</Orientation 1>> setpagedevice",
"-f"
};
switches.Add(this.FilePath);
Most of the switches you are using there will have no effect at all, and you haven't done anything to rotate the PDF file either.
The first thing to note is that Ghostscript is really the wrong tool for this job, it doesn't modify PDF files, it generates completely new ones. The process is described here
The -dEPSCrop switch only has an effect if the input is an EPS file, otherwise it does nothing.
The operands to setpagedevice are incorrect, you are passing an empty hex string <> when setpagedevice expects a dictionary <<>>. Even then an empty dictionary will do nothing.
I expect you intended to put <</Orientation 1>> setpagedevice but actually that's only a media matching request. In order to get Ghostscript to create a rotated PDF file you would have to alter the media size, set it to fixed, and set -dFitPage. You would also have to set the AutoRotatePages switch to None in order to prevent the automatic reorientation of the output so that the text is horizontal.
Since you haven't supplied the PDF file to look at, nor stated its media size, I can only guess at the sizes. However, something like
gswin32c.exe -sDEVICE=pdfwrite -sOutputFile=new.pdf -dDEVICEHEIGHTPOINTS=612 -dDEVICEWIDTHPOINTS=792 -dFitPage -dAutoRotatePages=/None input.pdf

How do I convert a Markdown document with Japanese to Beamer?

For example, I have this Markdown document:
## Markdown test
Japanese 日本語
I run:
pandoc mwe.rmd -t beamer -o mwe.pdf --latex-engine=xelatex -V mainfont=MS\ Mincho
The words 日本語 simply disappeared in the resulted file. If I don't use the Beamer template then it works correctly.
I don't have to use pandoc. Anything that gets me from Markdown to PDF (slides) on a Mac (with MacTex) would work for me.
If there is no easy solution, I'd be okay with anything that results in non-Beamer PDF slides.
I'm assuming that you do have installed a font named MS Mincho on your system, and it shows up when your start the Font Book.app? (Looks like, otherwise your "normal" PDF output wouldn't work, but you said it does...)
There are various options to check and verify, which exact font name you should use.
1. Font Book.app (GUI application)
Start Font Book.app.
Type mincho into the top right search box.
All installed fonts with 'Mincho' in their names show up.
Click on one of the font faces (NOT the main entry!) in the list.
A font sample will be displayed.
Click on the button with the little i-logo.
The font's metadata will display.
From the font's metadata you can infer the PostScript name and the Full name of the font. Both should work with XeLaTeX. (I usually put quotes around font names with spaces: -V mainfont="YuMincho Medium"
Here is a screenshot with the relevant parts of the Font Book.app UI highlighted in red. Sorry, I do not have MS Mincho installed, I can only show it with another font:
2. fc-list (command line utility)
fc-list is a command line utility that is available via the MacPorts fontconfig package.
If you have it installed, use it.
To get a list of font names available for XeTeX, you can simply run:
fc-list -f "%{family}\n"
fc-list :outline -f "%{family}\n"
The second command suppresses the listing of bitmap only fonts. Such fonts are unusable for TeX. -- For some more verbosity, and a nice formatting of the info, you could also run:
fc-list :outline -f " family: %{family}\nfullname: %{fullname}\n file: %{file}\n\n"
To get a list of names containing 'Mincho', run:
fc-list -f "%{family}\n" | grep -i mincho
Change your setup
Now that this smaller problem ("Which font names should I use?") is out of the way, lets deal with your main one:
The Pandoc Beamer template (and standard Beamer itself) does not use the \setmainfont command. Therefor putting -V mainfont=... onto the Pandoc command line does not do anything.
You can check this by querying the default internal template used by Pandoc to produce beamer output:
$ pandoc -D beamer | less
Search for a $mainfont$ variable in there and you'll find none!
You have to modify your setup a bit to get success:
First, create a simple text file named mincho.tex with the following two lines of content (I'm using my Mincho font name here, so I can really test if my advice will work):
\usepackage{xeCJK}
\setCJKmainfont{YuMincho Medium}
The xeCJK package is required by XeLaTeX for supporting Japanese (and Chinese+Korean) fonts.
Second, add -H mincho.tex to the command line so the above code snippet is included into the LaTeX code generated by Pandoc.
This is the complete command to convert your Markdown to Beamer-PDF:
pandoc \
mwe.rmd \
-t beamer \
-o mwe.pdf \
--latex-engine=xelatex \
-H mincho.tex
Result (screenshot):
The fonts used by the Beamer-PDF are these:
$ pdffonts mwe.pdf
name type encoding emb sub uni objID
----------------------------------- ------------ ----------- --- ----- ---- -----
TZVOMD+LMSans8-Regular-Identity-H CID Type 0C Identity-H yes yes yes 7 0
WMSBXQ+LMSans12-Regular-Identity-H CID Type 0C Identity-H yes yes yes 30 0
FXCTKJ+LMSans10-Regular-Identity-H CID Type 0C Identity-H yes yes yes 32 0
NXJKDD+YuMin-Medium-Identity-H CID Type 0C Identity-H yes yes no 34 0

Changing mac folder icon with Ruby?

Is it possible to change the icon of a Mac folder using a set of Ruby commands? I believe that OSX requires a .icon file to be present within the modified folder, perhaps there is a specific way of converting a jpg or png to the .icon criteria?
-- Edit (Working solution. Requires ImageMagick and OSXUtils)
* note, for my application I intended to set folder icons. It is entirely possible this could work for files as well.
def set_icon image, folder
# Convert to absolute paths and setup
image = File.expand_path image
folder = File.expand_path folder
dim = 512
thumb = folder + '/'+ 'thumb.png' # PNG supports transparency
icon = folder + '/'+ 'icon.icns'
# Convert original to thumbnail
system "convert '#{ image }' -quiet -thumbnail '#{dim}x#{dim}>' \
-background none -gravity center -extent #{dim}x#{dim} '#{ thumb }'"
# Set icon format. Causes 'libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area'
system "sips -s format icns '#{ thumb }' --out '#{ icon }'"
# Set the icon
system "seticon -d '#{ icon }' '#{ folder }'"
# Cleanup
FileUtils.rm thumb
FileUtils.rm icon
end
It's been years since I played with them, but .icon files' format were documented by Apple's documentation and Wikepedia.
If I remember right, the name has a trailing "\r" to make it harder to type, but that's easily handled from code.
You should be able to use the normal File.rename method to move an .icon file into a folder, and the Finder should do the right thing.
Looking at your code, I'd do some things differently:
require 'fileutils'
def set_icon image, folder
# Convert to absolute paths and setup
image = File.expand_path image
folder = File.expand_path folder
temp = File.join(folder, 'temp2' + File.extname(image))
# Copy image
FileUtils.cp(image, temp)
# Take an image and make the image its own icon
system "sips -Z 512 -i #{ temp }"
# Extract the icon to its own resource file
system "DeRez -only icns #{ temp } > tmpicns.rsrc"
# Append a resource to the folder you want to icon-ize
system "Rez tmpicns.rsrc -o $'#{ folder }/Icon\r'"
# Use the resource to set the icon.
system "SetFile -a C #{ folder }"
end
Rather than rely on sprintf or % ("format") to build the strings, instead use simple interpolation. sprintf strings are great when you need to force column widths and coerce values into a different representation, but they're overkill when you're inserting a single value that isn't formatted.
sips has this option, which looks promising, but it's not documented well in the man page:
-i
--addIcon
Add a Finder icon to image file.
Also, Stack Overflow's sibling site "Ask Different" has "Why setting image as its own icon with sips result a blurred icon? Are there any alternatives?", "How do I set an icon for a directory via the CLI? and "Changing a file or folder icon using the Terminal" which look useful.

How to convert LWFN to TTF?

Looking for an easy (preferably free) way to convert a few Mac PostScript Type 1 (LWFN) fonts to TTF.
Any ideas?
Try fondu.
Use fondu to convert it into a .bdf file.
(installing fondu may return 'installation failed' but it runs anyway.)
a) copy your font to the desktop b) open a terminal and type cd
Desktop/ c) then type fondu YOURFONTNAME, it will create a .bdf
file
And then use a standard font converter e.g. http://www.freefontconverter.com/ to convert the .bdf to a .ttf (TrueType) or .otf (OpenType) file.

Resources