What is the correct index position of the Convert command's options of GraphicsMagick - graphicsmagick

I am confused about the covert command of GraphicsMagick.
According to the documentation (http://www.graphicsmagick.org/convert.html), the synopsis of convert command is:
convert [ options ... ] input_file output_file
According to the above synopsis, I can execute the convert command in the following way:
Example 1:
gm convert -strip -scale x400 inputFile.jpg outputFile.jpg
The above command is executed successfully. Now, I ran the the same command with different index position of the options.
Example 2:
gm convert inputFile.jpg -strip -scale x400 outputFile.jpg
The above example executed successfully. The above example's index position of the options are different, does not match against the synopsis.
My question is, which example is correct?

I fear the answer is partly philosophical, partly folklore, partly opinion and partly flexibility of GraphicsMagick and partly rigidity of manpages. Let me explain...
To understand the answer, you need to differentiate between settings and operators. #KurtPfeifle did a great job here which, for the sake of completeness, I will summarise as follows:
Some parameters are settings - they set and retain their value till the end of the command or till changed. For example, -size sets the size of all canvases created after it is set on the commandline. Until the point it is first set, the default canvas size is 1x1, after it is set it remains till the end of the command.
Some parameters are operators - they generally perform some image processing, such as thresholding or converting to greyscale. These ones operate on all images currently in the image list.
Maybe an example will help. First, we use the -size setting:
gm convert -background none xc:red -size 5x5 xc:lime xc:blue -size 10x10 xc:magenta +append setting.png
Initially, the default size was 1x1, so the red canvas comes out at 1x1. Then I changed the setting and it remained at 5x5 for the lime and blue canvases till I changed it to 10x10 for the magenta one. Those were settings and they set things up for the images yet to come.
Now, I do a similar thing with the -colorspace operator:
gm convert xc:red xc:lime xc:blue -colorspace gray +append operator.png
And you see that the -colorspace operator has changed all the images that were already in the image list, i.e. they process images that have previously been added to the list.
If you like, you could simplify things and say "settings apply to all that is to come, and operators apply to all that has gone". Although I am sure there are counter-examples.
The order of the commandline options was rationalised a few years back and GraphicsMagick permits some flexibility to retain some backward compatibility. That is probably the best explanation of your actual question.
So, in general, you should declare settings as soon as posssible on the commandline and apply operators at the point that makes most sense depending on what images are currently in your list.
Another reason is that it would be very cumbersome, to the point of unintelligibility, if all the GraphicsMagick combinations and permutations were put in a conventional manpage. Can you imagine:
gm convert [[[operators|settings]|[settings|operators]] image] [[settings|operators]|[operators]] ...

Related

FontForge: Count of Defined Glyphs

I'm fairly new to FontForge, and I just want to merge two fonts for my specific usage.
I know how to merge two fonts from this question and I'm aware of TTF, ... 65535 lookup limits, so I'm trying to Detach & Remove Glyphs... for some glyph ranges, and for this:
I need to know how many glyph is defined, so I can decide which range to detach and remove. It seems fairly simple info, but I can not find where it is shown.
is there any menu or macro to show the current number of defined glyphs in FontForge?
I was looking for the same info. This will give you the number of defined glyphs fairly quickly:
fontforge -lang=ff \
-c 'Open($1); SelectWorthOutputting(); Print($selection)' "$FONTFILE" \
2>/dev/null |tr -d '][' |tr , '\n' |grep -c 1
The SelectWorthOutputting() function fills an array with ones for each defined glyph, so we can print this out and filter it to get the count.
I think found an answer, however it may not be the best approach. as FontForge saves projects in plain text, we can search for StartChar: keywords in the saved project file (project_name.sfd), which each section started by a StartChar: defines one glyphs in the font project. so if we count them we may know how many glyphs has been defined in the font file, for example, in bash the command:
grep -E "^StartChar\:" project_name.sfd | wc -l
would count them for us, and then we may know how many glyphs has been define so far.
This question pops up as first Google hit, so I'll just place this for posterity, but the proper code answer is from #Jerry Penner.
It's also possible from the program GUI to obtain the info in a quick dirty way:
Reduce the number of displayed glyphs via Encoding > Compact menu
Select the last glyph in your font
At the top right below the menu will be displayed the glyph "number" starting from 0 (so in below picture example the total count is 270).
Note: the number displayed is for the current compact view, not the actual glyph number in the font.

How to replace a string pattern with different strings quickly?

For example, I have many HTML tabs to style, they use different classes, and will have different backgrounds. Background images files have names corresponding to class names.
The way I found to do it is yank:
.tab.home {
background: ...home.jpg...
}
then paste, then :s/home/about.
This is to be repeated for a few times. I found that & can be used to repeat last substitute, but only for the same target string. What is the quickest way to repeat a substitute with different target string?
Alternatively, probably there are more efficient ways to do such a thing?
I had a quick play with some vim macro magic and came up with the following idea... I apologise for the length. I thought it best to explain the steps..
First, place the text block you want to repeat into a register (I picked register z), so with the cursor at the beginning of the .tab line I pressed "z3Y (select reg z and yank 3 lines).
Then I entered the series of VIM commands I wanted into the buffer as )"zp:.,%s/home/. (Just press i and type the commands)
This translate to;
) go the end of the current '{}' block,
"zp paste a copy of the text in register z,
.,%s/home/ which has two tricks.
The .,% ensures the substitution applies to everything from the start of the .tab to the end of the closing }, and,
The command is incomplete (ie, does not have a at the end), so vim will prompt me to complete the command.
Note that while %s/// will perform a substitution across every line of the file, it is important to realise that % is an alias for range 1,$. Using 1,% as a range, causes the % to be used as the 'jump to matching parenthesis' operator, resulting in a range from the current line to the end of the % match. (which in this example, is the closing brace in the block)
Then, after placing the cursor on the ) at the beginning of the line, I typed "qy$ which means yank all characters to the end of the line into register q.
This is important, because simply yanking the line with Y will include a carriage return in the register, and will cause the macro to fail.
I then executed the content of register q with #q and I was prompted to complete the s/home/ on the command line.
After typing the replacement text and pressing enter, the pasted block (from register z) appeared in the buffer with the substitutions already applied.
At this point you can repeat the last #qby simple typing ##. You don't even need to move the cursor down to the end of the block because the ) at the start of the macro does that for you.
This effectively reduces the process of yanking the original text, inserting it, and executing two manual replace commands into a simple ##.
You can safely delete the macro string from your edit buffer when done.
This is incredibly vim-ish, and might waste a bit of time getting it right, but it could save you even more when you do.
Vim macro's might be the trick you are looking for.
From the manual, I found :s//new-replacement. Seemed to be too much typing.
Looking for a better answer.

Multiline prompt formatting incorrectly due to date command in zsh

I have the following in my .zshrc:
setopt PROMPT_SUBST
precmd(){
echo""
LEFT="$fg[cyan]$USERNAME#$HOST $fg[green]$PWD"
RIGHT="$fg[yellow]$(date +%I:%M\ %P)"
RIGHTWIDTH=$(($COLUMNS-${#LEFT}))
echo $LEFT${(l:$RIGHTWIDTH:)RIGHT}
}
PROMPT="$ "
This gives me the following screenshot
The time part on the right is always not going all the way to the edge of the terminal, even when resized. I think this is due to the $(date +%I:%M\ %P) Anyone know how to fix this?
EDIT: Zoomed in screenshot
While your idea is commendable, the problem you suffer from is that your LEFT and RIGHT contains ANSI escape sequences (for colors), which should be zero-width characters, but are nevertheless counted toward the length of a string if you naively use $#name, or ${(l:expr:)name}.
Also, as a matter of style, you're better off using Zsh's builtin prompt expansion, which wraps a lot of common things people may want to see in their prompts in short percent escape sequences. In particular, there are builtin sequences for colors, so you don't need to rely on nonstandard $fg[blah].
Below is an approximate of your prompt written in my preferred coding style... Not exactly, I made everything super verbose so as to be understandable (hopefully). The lengths of left and right preprompts are calculated after stripping the escape sequences for colors and performing prompt expansion, which gives the correct display length (I can't possibly whip that up in minutes; I ripped the expression off pure).
precmd(){
local preprompt_left="%F{cyan}%n#%m %F{green}%~"
local preprompt_right="%F{yellow}%D{%I:%M %p}%f"
local preprompt_left_length=${#${(S%%)preprompt_left//(\%([KF1]|)\{*\}|\%[Bbkf])}}
local preprompt_right_length=${#${(S%%)preprompt_right//(\%([KF1]|)\{*\}|\%[Bbkf])}}
local num_filler_spaces=$((COLUMNS - preprompt_left_length - preprompt_right_length))
print -Pr $'\n'"$preprompt_left${(l:$num_filler_spaces:)}$preprompt_right"
}
PROMPT="$ "
Edit: In some terminal emulators, printing exactly $COLUMN characters might wrap the line. In that case, replace the appropriate line with
local num_filler_spaces=$((COLUMNS - preprompt_left_length - preprompt_right_length - 1))
End of edit.
This is very customizable, because you can put almost anything in preprompt_left and preprompt_right and still get the correct lengths — just remember to use prompt escape sequence for zero width sequences, e.g., %F{}%f for colors, %B%b for bold, etc. Again, read the docs on prompt expansion: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html.
Note: You might notice that %D{%I:%M %p} expands to things like 11:35 PM. That's because I would like to use %P to get pm, but not every implementation of strftime supports %P. Worst case scenario: if you really want lowercase but %P is not supported, use your original command subsitution $(date +'%I:%M %P').
Also, I'm using %~ instead of %/, so you'll get ~/Desktop instead of /c/Users/johndoe/Desktop. Some like it, some don't. However, as I said, this is easily customizable.

fread - skip lines starting with certain character - "#"

I am using the fread function in R for reading files to data.tables objects.
However, when reading the file I'd like to skip lines that start with #, is that possible?
I could not find any mention to that in the documentation.
fread can read from a piped command that filters out such lines, like this:
fread("grep -v '^#' filename")
Not currently, but it's on the list to do.
Are the # lines at the top forming a header which is more than 30 lines long?
If so, that's come up before and the solution is :
fread("filename", autostart=60)
where 60 is chosen to be inside the block of data to be read.
From ?fread :
Once the separator is found on line autostart, the number of columns
is determined. Then the file is searched backwards from autostart
until a row is found that doesn't have that number of columns. Thus,
the first data row is found and any human readable banners are
automatically skipped. This feature can be particularly useful for
loading a set of files which may not all have consistently sized
banners. Setting skip>0 overrides this feature by setting
autostart=skip+1 and turning off the search upwards step.
The default autostart=30 might just need bumping up a bit in your case.
Or maybe skip=n or skip="string" helps :
If -1 (default) use the procedure described below starting on line autostart to find the first data row. skip>=0 means ignore autostart and take line skip+1 as the first data row (or column names according to header="auto"|TRUE|FALSE as usual). skip="string" searches for "string" in the file (e.g. a substring of the column names row) and starts on that line (inspired by read.xls in package gdata).

How to create a transparent image with GraphicsMagick CLI?

I'm coming up empty handed while trying to find out how to modify the opacity of an image using the GraphicsMagick CLI. I'm simply trying to input a standard image and convert it to the corresponding image with a reduced opacity. I'm running GraphicsMagick 1.3.14. Using ImageMagick, I can issue the command:
convert input.png -alpha set -channel a -evaluate set 50% +channel output.png
However, the -alpha option (among others) is unrecognized as a valid GM convert option (convert option reference here). Any help would be greatly appreciated.
This question was cross-posted on SuperUser, as I later thought SuperUser might be the more appropriate outlet. To quote Bob Friesenhahn:
The equivalent in GraphicsMagick is
gm convert input.png -operator Opacity Multiply 0.5 output.png
or
gm convert input.png -operator Opacity Assign 50% output.png
depending on what you really want to do (modulate or assign).
You should add -matte prior to -operator if you don't know if the image already has an active opacity channel.
Bob

Resources