Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Overview

Text in documents can be accessed by finding the glyphs nodes on a page. Each glyphs node will contain a run of text.

Mako Text Analysis

Some documents can be poorly constructed when it comes to text. You may find a document where each character is individually placed. In this case, when Mako imports a document, it uses intelligent heuristics to try and join these individual characters up into runs of text to make onwards processing easier. 

Finding Text

The following code finds all glyphs nodes font anywhere within the content node.

CEDLVectIDOMNode glyphsNodes = content.findChildrenOfType(eDOMNodeType.eDOMGlyphsNode);

Updating Text

Updating text requires a little more knowledge about how documents are constructed. Specifically, A document may contain a font which is subsetted. This means that only the parts of the font used in the document is included.

For example, if a document contains glyphs with the characters 'a', 'b' and 'c', the embedded font may only contain visual information for the characters 'a', 'b' and 'c'. Therefore, if you update the glyphs text from 'abc' to 'abcd', you may find that the character 'd' is not rendered correctly when the document is output and viewed.

To avoid this issue, the full font should be loaded. This can be done by retrieving a specific font from disk, or by finding the font using Mako APIs.

Once the full font has been loaded or found, the text can be updated by using:

glyphs.setFont(fullFont);
glyphs.setUnicodeString(updatedText);
glyphs.setIndices(string.Empty);

Font Indices

If the replacement font is different, it's always a good idea to reset the font's indices in the glyphs node.

Loading a Font from Disk

The code below finds the path to the Arial font in the Windows font folder.

var arialFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "Arial.ttf");
using var stream = IInputStream.createFromFile(factory, arialFontPath);

using var font = IDOMFontOpenType.create(factory, stream);

Finding a Font using Mako  

The code below finds the name of the font used in an existing glyphs node. This font is then attempted to be found using the mako API IJawsMako::findFont(...). If it can't find it, the code returns Arial as a default.

using var originalFont = glyphs.getFont();

if (originalFont.getFontType() != IDOMFont.eFontType.eFontTypeOpenType)
    throw new InvalidOperationException("Font is not compatible with Mako.");

var originalTrueTypeFont = IDOMFontOpenType.fromRCObject(originalFont.toRCObject());
var fontName = originalTrueTypeFont.getFullName(factory, (int) glyphs.getFontIndex());

try
{
    return jawsMako.findFont(fontName, out _);
}
catch (Exception e)
{
    Console.WriteLine($"Failed to find font on local system: {e.Message}");
                
    // Fallback to Arial for testing.
    return jawsMako.findFont("Arial", out _);
}

Converting Text to Paths

In some cases, it may be desired to convert all text into paths. Thankfully Mako makes this very simple.

The code below calls getEquivalentPath(...) to get a new IDOMPathNode. This IDOMPathNode contains the vector content that represents the orignal text. It's been setup correctly so the next step is simple to replace the existing IDOMGlyphs node.

using var path = glyphs.getEquivalentPath();
glyphs.getParentNode().replaceChild(glyphs, path);



  • No labels