Package org.apache.fontbox.ttf

Examples of org.apache.fontbox.ttf.CmapSubtable$SubHeader


        Assert.assertNotNull(cmapTable);

        CmapSubtable[] cmaps = cmapTable.getCmaps();
        Assert.assertNotNull(cmaps);

        CmapSubtable cmap = null;

        for (CmapSubtable e : cmaps)
        {
            if (e.getPlatformId() == NameRecord.PLATFORM_WINDOWS
                    && e.getPlatformEncodingId() == NameRecord.ENCODING_WINDOWS_UNICODE_BMP)
            {
                cmap = e;
                break;
            }
        }

        Assert.assertNotNull(cmap);

        PostScriptTable post = arial.getPostScript();
        Assert.assertNotNull(post);

        String[] glyphNames = arial.getPostScript().getGlyphNames();
        Assert.assertNotNull(glyphNames);

        // test a WGL4 (Macintosh standard) name
        int gid = cmap.getGlyphId(0x2122); // TRADE MARK SIGN
        Assert.assertEquals("trademark", glyphNames[gid]);

        // test an additional name
        gid = cmap.getGlyphId(0x20AC); // EURO SIGN
        Assert.assertEquals("Euro", glyphNames[gid]);
    }
View Full Code Here


            // The conforming reader shall select glyphs by translating characters from the
            // encoding specified by the predefined CMap to one of the encodings in the TrueType
            // font's 'cmap' table. The means by which this is accomplished are implementation-
            // dependent.

            CmapSubtable cmap = getUnicodeCmap(ttf.getCmap());
            String unicode;

            if (cid2gid != null || hasIdentityCid2Gid)
            {
                int cid = codeToCID(code);
                // strange but true, Acrobat allows non-embedded GIDs, test with PDFBOX-2060
                if (hasIdentityCid2Gid)
                {
                    return cid;
                }
                else
                {
                    return cid2gid[cid];
                }
            }
            else if (!parent.isSymbolic())
            {
                // this nonsymbolic behaviour isn't well documented, test with PDFBOX-1422

                // if the font descriptor's Nonsymbolic flag is set, the conforming reader shall
                // create a table that maps from character codes to glyph names
                String name = null;

                // If the Encoding entry is one of the names MacRomanEncoding, WinAnsiEncoding,
                // or a dictionary, then the table is initialized as normal
                // todo: Encoding is not allowed though, right? So this never happens?
                /*if (getFontEncoding() != null)
                {
                    name = getFontEncoding().getName(cid);
                }*/

                // Any undefined entries in the table shall be filled using StandardEncoding
                if (name == null)
                {
                    name = StandardEncoding.INSTANCE.getName(code);
                }

                // map to a Unicode value using the Adobe Glyph List
                unicode = GlyphList.getAdobeGlyphList().toUnicode(name);
            }
            else
            {
                int cid = codeToCID(code);
                unicode = parent.toUnicode(cid); // code = CID for TTF
            }

            if (unicode == null)
            {
                return 0;
            }
            else if (unicode.length() > 1)
            {
                LOG.warn("trying to map a multi-byte character using 'cmap', result will be poor");
            }
            return cmap.getGlyphId(unicode.codePointAt(0));
        }
        else
        {
            // If the TrueType font program is embedded, the Type 2 CIDFont dictionary shall contain
            // a CIDToGIDMap entry that maps CIDs to the glyph indices for the appropriate glyph
View Full Code Here

     * Returns the best Unicode from the font (the most general). The PDF spec says that "The means
     * by which this is accomplished are implementation-dependent."
     */
    private CmapSubtable getUnicodeCmap(CmapTable cmapTable)
    {
        CmapSubtable cmap = cmapTable.getSubtable(CmapTable.PLATFORM_UNICODE,
                                                  CmapTable.ENCODING_UNICODE_2_0_FULL);
        if (cmap == null)
        {
            cmap = cmapTable.getSubtable(CmapTable.PLATFORM_UNICODE,
                                         CmapTable.ENCODING_UNICODE_2_0_BMP);
View Full Code Here

        // hmm there does not seem to be a clear definition for StemV,
        // this is close enough and I am told it doesn't usually get used.
        fd.setStemV(fd.getFontBoundingBox().getWidth() * .13f);

        CmapTable cmapTable = ttf.getCmap();
        CmapSubtable uniMap = cmapTable.getSubtable(CmapTable.PLATFORM_UNICODE,
                CmapTable.ENCODING_UNICODE_2_0_FULL);
        if (uniMap == null)
        {
            uniMap = cmapTable.getSubtable(CmapTable.PLATFORM_UNICODE,
                    CmapTable.ENCODING_UNICODE_2_0_BMP);
        }
        if (uniMap == null)
        {
            uniMap = cmapTable.getSubtable(CmapTable.PLATFORM_WINDOWS,
                    CmapTable.ENCODING_WIN_UNICODE);
        }
        if (uniMap == null)
        {
            // Microsoft's "Recommendations for OpenType Fonts" says that "Symbol" encoding
            // actually means "Unicode, non-standard character set"
            uniMap = cmapTable.getSubtable(CmapTable.PLATFORM_WINDOWS,
                    CmapTable.ENCODING_WIN_SYMBOL);
        }
        if (uniMap == null)
        {
            // there should always be a usable cmap, if this happens we haven't tried hard enough
            // to find one. Furthermore, if we loaded the font from disk then we should've checked
            // first to see that it had a suitable cmap before calling createFontDescriptor
            throw new IllegalArgumentException("ttf: no suitable cmap for font '" +
                    ttf.getNaming().getFontFamily() + "', found: " +
                    Arrays.toString(cmapTable.getCmaps()));
        }

        if (this.getFontEncoding() == null)
        {
            // todo: calling this.getFontEncoding() doesn't work if the font is loaded
            //       from the local system, because it relies on the FontDescriptor!
            //       We make do for now by returning an incomplete descriptor pending further
            //       refactoring of PDFont#determineEncoding().
            return fd;
        }

        Map<Integer, String> codeToName = this.getFontEncoding().getCodeToNameMap();

        int firstChar = Collections.min(codeToName.keySet());
        int lastChar = Collections.max(codeToName.keySet());

        HorizontalMetricsTable hMet = ttf.getHorizontalMetrics();
        int[] widthValues = hMet.getAdvanceWidth();
        // some monospaced fonts provide only one value for the width
        // instead of an array containing the same value for every glyphid
        boolean isMonospaced = fd.isFixedPitch() || widthValues.length == 1;
        int nWidths = lastChar - firstChar + 1;
        List<Integer> widths = new ArrayList<Integer>(nWidths);
        // use the first width as default
        // proportional fonts -> width of the .notdef character
        // monospaced-fonts -> the first width
        int defaultWidth = Math.round(widthValues[0] * scaling);
        for (int i = 0; i < nWidths; i++)
        {
            widths.add(defaultWidth);
        }

        // A character code is mapped to a glyph name via the provided font encoding
        // Afterwards, the glyph name is translated to a glyph ID.
        // For details, see PDFReference16.pdf, Section 5.5.5, p.401
        //
        for (Map.Entry<Integer, String> e : codeToName.entrySet())
        {
            String name = e.getValue();
            // pdf code to unicode by glyph list.
            if (!name.equals(".notdef"))
            {
                String c = GlyphList.getAdobeGlyphList().toUnicode(name); // todo: we're supposed to use the 'provided font encoding'
                int charCode = c.codePointAt(0);
                int gid = uniMap.getGlyphId(charCode);
                if (gid != 0)
                {
                    if (isMonospaced)
                    {
                        widths.set(e.getKey() - firstChar, defaultWidth);
View Full Code Here

TOP

Related Classes of org.apache.fontbox.ttf.CmapSubtable$SubHeader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.