Pango unable to find fonts with librsvg - cairo

I'm using librsvg to render an SVG file to a Cairo context. Afterwards I "manually" render some text on top using Pango.
When rendering the SVG I get Pango warnings that it could not load the fonts:
"couldn't load font "'Calibri' Not-Rotated 16", falling back to "Sans Not-Rotated 16", expect ugly output."
The manual text rendering via Pango afterwards works perfectly fine. I tried multiple different font families and they all render just fine without any warnings.
I even skimmed through the librsvg source to see how it is using Pango but I couldn't see any major differences to what I am doing.
I have no idea left how to continue debugging this issue, any advice on how Pango is locating the fonts under Windows and what may influence its ability to find them is greatly appreciated.
If that matters:
I downloaded all needed binaries from here: http://sourceforge.net/projects/ezwinports/files/

I must concentrate to not think about the amount of time wasted on this issue or my brain would melt.
Anyways, in case someone comes across this post I will share what I had to figure out till it worked:
To get proper error messages from pango register a glib log handler, see below.
Pango assumes a specific folder-structure relative to the pango dll to look for some files. If the pango dll is located in /foo/bin or /foo it will try to locate "pango.modules" under /foo/etc/pango. That file contains information about where to find the pango module dlls. The easiest way I found was to place the pango module dlls under /foo/lib/pango/1.8.0/modules and then execute "pango-querymodules.exe > pango.modules" to generate the contents of the modules file and then manually remove the path, except for the actual dll filename. See below for the contents on my machine.
The actual problem which took me days to find out was this: I used SVGs which were exported from Adobe Illustrator and these contain text elements like this
<text style="font-family:'Arial'; font-size:20;">Foo Bar</text>
which looked fine to me, but the single quotation marks around the font family seem to be included and cause Pango to be unable to find the font 'Arial', because the fonts name is of course actually Arial and not 'Arial', if you know what I mean.
Pango message handler
void PangoMessageHandler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{
//TODO: handle log message
}
g_log_set_handler("Pango", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, PangoMessageHandler, NULL);
pango.modules
"pango-arabic-lang.dll" ArabicScriptEngineLang PangoEngineLang PangoRenderNone arabic:*
"pango-basic-win32.dll" BasicScriptEngineWin32 PangoEngineShape PangoRenderWin32 common:
"pango-indic-lang.dll" devaIndicScriptEngineLang PangoEngineLang PangoRenderNone devanagari:*
"pango-indic-lang.dll" bengIndicScriptEngineLang PangoEngineLang PangoRenderNone bengali:*
"pango-indic-lang.dll" guruIndicScriptEngineLang PangoEngineLang PangoRenderNone gurmukhi:*
"pango-indic-lang.dll" gujrIndicScriptEngineLang PangoEngineLang PangoRenderNone gujarati:*
"pango-indic-lang.dll" oryaIndicScriptEngineLang PangoEngineLang PangoRenderNone oriya:*
"pango-indic-lang.dll" tamlIndicScriptEngineLang PangoEngineLang PangoRenderNone tamil:*
"pango-indic-lang.dll" teluIndicScriptEngineLang PangoEngineLang PangoRenderNone telugu:*
"pango-indic-lang.dll" kndaIndicScriptEngineLang PangoEngineLang PangoRenderNone kannada:*
"pango-indic-lang.dll" mlymIndicScriptEngineLang PangoEngineLang PangoRenderNone malayalam:*
"pango-indic-lang.dll" sinhIndicScriptEngineLang PangoEngineLang PangoRenderNone sinhala:*

Related

Stop huge error output from testing-library

I love testing-library, have used it a lot in a React project, and I'm trying to use it in an Angular project now - but I've always struggled with the enormous error output, including the HTML text of the render. Not only is this not usually helpful (I couldn't find an element, here's the HTML where it isn't); but it gets truncated, often before the interesting line if you're running in debug mode.
I simply added it as a library alongside the standard Angular Karma+Jasmine setup.
I'm sure you could say the components I'm testing are too large if the HTML output causes my console window to spool for ages, but I have a lot of integration tests in Protractor, and they are SO SLOW :(.
I would say the best solution would be to use the configure method and pass a custom function for getElementError which does what you want.
You can read about configuration here: https://testing-library.com/docs/dom-testing-library/api-configuration
An example of this might look like:
configure({
getElementError: (message: string, container) => {
const error = new Error(message);
error.name = 'TestingLibraryElementError';
error.stack = null;
return error;
},
});
You can then put this in any single test file or use Jest's setupFiles or setupFilesAfterEnv config options to have it run globally.
I am assuming you running jest with rtl in your project.
I personally wouldn't turn it off as it's there to help us, but everyone has a way so if you have your reasons, then fair enough.
1. If you want to disable errors for a specific test, you can mock the console.error.
it('disable error example', () => {
const errorObject = console.error; //store the state of the object
console.error = jest.fn(); // mock the object
// code
//assertion (expect)
console.error = errorObject; // assign it back so you can use it in the next test
});
2. If you want to silence it for all the test, you could use the jest --silent CLI option. Check the docs
The above might even disable the DOM printing that is done by rtl, I am not sure as I haven't tried this, but if you look at the docs I linked, it says
"Prevent tests from printing messages through the console."
Now you almost certainly have everything disabled except the DOM recommendations if the above doesn't work. On that case you might look into react-testing-library's source code and find out what is used for those print statements. Is it a console.log? is it a console.warn? When you got that, just mock it out like option 1 above.
UPDATE
After some digging, I found out that all testing-library DOM printing is built on prettyDOM();
While prettyDOM() can't be disabled you can limit the number of lines to 0, and that would just give you the error message and three dots ... below the message.
Here is an example printout, I messed around with:
TestingLibraryElementError: Unable to find an element with the text: Hello ther. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
...
All you need to do is to pass in an environment variable before executing your test suite, so for example with an npm script it would look like:
DEBUG_PRINT_LIMIT=0 npm run test
Here is the doc
UPDATE 2:
As per the OP's FR on github this can also be achieved without injecting in a global variable to limit the PrettyDOM line output (in case if it's used elsewhere). The getElementError config option need to be changed:
dom-testing-library/src/config.js
// called when getBy* queries fail. (message, container) => Error
getElementError(message, container) {
const error = new Error(
[message, prettyDOM(container)].filter(Boolean).join('\n\n'),
)
error.name = 'TestingLibraryElementError'
return error
},
The callstack can also be removed
You can change how the message is built by setting the DOM testing library message building function with config. In my Angular project I added this to test.js:
configure({
getElementError: (message: string, container) => {
const error = new Error(message);
error.name = 'TestingLibraryElementError';
error.stack = null;
return error;
},
});
This was answered here: https://github.com/testing-library/dom-testing-library/issues/773 by https://github.com/wyze.

Testing VSCode extensions - how to verify that decorations are set

I have an extension which adds text decorations at the end of some of the lines. I'd like to write a test which verifies that the text decorations are added and also to assert in the test that their value is correct.
suite('Extension Tests', () => {
test('Should work', async() => {
const fixturePath = path.join(__dirname, '..', '..', 'test', 'fixtures');
const uri = vscode.Uri.file(path.join(fixturePath, 'a.js'));
const document = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(document);
window.activeTextEditor.getDecorations(); //???
});
});
It seems that the getDecorations API is missing from editor. What can I do to resolve this?
As of VSCode 1.37.1 (2019-08-30), the answer is no, there is no way to write an automated test for decorations using the decorations extensions API.
There are automated tests of decorations in the vscode sources, particularly modelDecorations.test.ts, but they use private APIs.
Microsoft has an example extension that uses decorators, decorator-sample, but it has no tests.
There are a few open issues related to this, although none directly addresses the inability to enumerate existing decorations:
50346: Lacking decorations-related APIs (mainly about incremental update for better performance); closed as duplicate of:
585: Provide an API for advanced/semantic source highlighting
50415: Missing API for knowing when an extension's decorations get trashed
54938: Make decoration provider API public
Therefore I recommend filing a new issue.
2022-04-27: As noted in a comment, issue 136164: Provide Access to a TextEditors Decorations for Testing Purposes was filed on 2021-10-30. It was then closed as unnecessary because you can test that your extension calls the extension API as intended. I find this response inadequate because there's no way to test that the effect is as intended (for example, TextEditor.setDecorations removes existing decorations that have the same type, and that's a subtle interaction that would only be revealed by examining the effects), but the issue is locked so yet another issue would have to be filed to raise an objection.

adding syntax highlighting to Jupyter notebook cell magic

I'm trying to figure out how to activate CodeMirror syntax highlighting for a CodeMirror-supported language (cypher) within a cell for a custom Jupyter cell magic (%%mymagic). The magic isn't associated with a special kernel - it just runs Python commands that process the string entered into the cell that I want to highlight. From what I can tell, this ostensibly can be done using something like
from notebook.services.config.manager import ConfigManager
cm = ConfigManager()
cm.update('notebook', {'CodeCell': {'highlight_modes': {'magic_cypher': {'reg': '^%%mymagic'}}}})
within the class that implements the magic.
I can't seem to get this to work, however; no change in highlighting occurs when I enter stuff in a cell that starts with %%mymagic. Is the above approach accurate? Does 'magic_cypher' need to have a specific format? Does the magic need to somehow specify the MIME type CodeMirror associates with the desired highlighting language? I'm using notebook 5.0.0, jupyter_core 4.3.0, and python 2.7.13.
The following code works for SQL when placed in ~/.jupyter/custom/custom.js with notebook 5.x:
require(['notebook/js/codecell'], function(codecell) {
codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
Jupyter.notebook.get_cells().map(function(cell){
if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
});
});
Credit goes to Thomas K for this info!
The case where I've been successful doing this was in adding SQL highlighting for the %%sql magic. I did this by adding the following to ~/.jupyter/custom/custom.js. The first line adds the mode to the Codemirror configuration, the rest apply the style to any existing cells in the workbook that need it (later cells will get styled appropriately as they are created). I haven't been successful in having it happen when the magic is installed, although I expect that it is possible.
IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
IPython.notebook.events.one('kernel_ready.Kernel', function(){
IPython.notebook.get_cells().map(function(cell){
if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
});

Gtk.stock is deprecated, what's the alternative?

I've been learning to develop to Gtk and most of the examples online suggests the use of Gtk.stock icons. However, its use produces warnings that it has been deprecated and I can't find the alternative to these icons.
Code examples are:
open_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.OPEN)
open_button.clicked.connect (openfile)
new_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.NEW)
new_button.clicked.connect (createNew)
save_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.SAVE)
save_button.clicked.connect (saveFile)
That generates error as:
/tmp/text_editor-exercise_7_1.vala.c:258:2: warning: 'GtkStock' is deprecated [-Wdeprecated-declarations]
_tmp1_ = (GtkToolButton*) gtk_tool_button_new_from_stock (GTK_STOCK_OPEN);
Which is the alternative and how it would look in the code above?
GTK+3 has moved over to the freedesktop.org Icon Naming Specification and internationalised labels. Taking Gtk.Stock.OPEN as an example. The GNOME Developer documentation for GTK_STOCK_OPEN gives two replacements:
GTK_STOCK_OPEN has been deprecated since version 3.10 and should not be used in newly-written code. Use named icon "document-open" or the label "_Open".
The Named Icon Method
The named icon method would be something like:
var open_icon = new Gtk.Image.from_icon_name( "document-open",
IconSize.SMALL_TOOLBAR
)
var open_button = new Gtk.ToolButton( open_icon, null )
The Label Method
The label method makes use of gettext to translate the label in to the current runtime language of the program. This is indicated by the underscore before the label. The line in your program would be:
var open_button = new Gtk.ToolButton( null, dgettext( "gtk30", "_Open") )
gettext uses domains, which are files containing the translations. The Gtk+3 domain is gtk30. You will also need to add a line at the beginning of your program to change the default locale for the C language, which is US English ASCII, to the locale of the run time environment:
init
Intl.setlocale()
To compile the Genie program you will need to set the default domain for gettext. This is usually set to nothing:
valac -X -DGETTEXT_PACKAGE --pkg gtk+-3.0 my_program.gs
When you run your program you will get the "_Open" translated to your locale. You can also change the locale. If you have the French locale installed then running the program with:
LC_ALL=fr ./my_program
will have the "_Open" label appear in French.
You may see in examples _( "_OPEN" ). The _() is a function like dgettext but uses a default domain. You may want to keep the default domain to the translation file for your own program. Using _( "_translate me" ) is a bit less typing that dgettext( "mydomain", "_translate me" ). To set the default domain in Genie add a line before init:
const GETTEXT_PACKAGE:string = "mydomain"
init
Intl.setlocale()

Get font of recognized character with Tesseract-OCR

Is it possible to get the font of the recognized characters with Tesseract-OCR, i.e. are they Arial or Times New Roman, either from the command-line or using the API.
I'm scanning documents that might have different parts with different fonts, and it would be useful to have this information.
Tesseract has an API WordFontAttributes function defined in ResultIterator class that you can use.
Based on nguyenq's answer i wrote a simple python script that prints the font name for each detected char. This script uses the python lib tesserocr.
from tesserocr import PyTessBaseAPI, RIL, iterate_level
def get_font(image_path):
with PyTessBaseAPI() as api:
api.SetImageFile(image_path)
api.Recognize()
ri = api.GetIterator()
level = RIL.SYMBOL
for r in iterate_level(ri, level):
symbol = r.GetUTF8Text(level)
word_attributes = r.WordFontAttributes()
if symbol:
print(u'symbol {}, font: {}'.format(symbol, word_attributes['font_name']))
get_font('logo.jpg')