1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * (C) Copyright IBM Corp. 1998-2004 - All Rights Reserved
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "LETypes.h"
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "LEFontInstance.h"
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "OpenTypeTables.h"
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "AnchorTables.h"
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "MarkArrays.h"
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "GlyphPositioningTables.h"
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "AttachmentPosnSubtables.h"
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "MarkToLigaturePosnSubtables.h"
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "GlyphIterator.h"
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "LESwaps.h"
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruLEGlyphID MarkToLigaturePositioningSubtable::findLigatureGlyph(GlyphIterator *glyphIterator) const
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (glyphIterator->prev()) {
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return glyphIterator->getCurrGlyphID();
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return 0xFFFF;
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querule_int32 MarkToLigaturePositioningSubtable::process(GlyphIterator *glyphIterator, const LEFontInstance *fontInstance) const
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    LEGlyphID markGlyph = glyphIterator->getCurrGlyphID();
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 markCoverage = getGlyphCoverage((LEGlyphID) markGlyph);
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (markCoverage < 0) {
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // markGlyph isn't a covered mark glyph
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    LEPoint markAnchor;
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const MarkArray *markArray = (const MarkArray *) ((char *) this + SWAPW(markArrayOffset));
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 markClass = markArray->getMarkClass(markGlyph, markCoverage, fontInstance, markAnchor);
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_uint16 mcCount = SWAPW(classCount);
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (markClass < 0 || markClass >= mcCount) {
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // markGlyph isn't in the mark array or its
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // mark class is too big. The table is mal-formed!
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    // FIXME: we probably don't want to find a ligature before a previous base glyph...
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    GlyphIterator ligatureIterator(*glyphIterator, (le_uint16) (lfIgnoreMarks /*| lfIgnoreBaseGlyphs*/));
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    LEGlyphID ligatureGlyph = findLigatureGlyph(&ligatureIterator);
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 ligatureCoverage = getBaseCoverage((LEGlyphID) ligatureGlyph);
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const LigatureArray *ligatureArray = (const LigatureArray *) ((char *) this + SWAPW(baseArrayOffset));
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_uint16 ligatureCount = SWAPW(ligatureArray->ligatureCount);
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (ligatureCoverage < 0 || ligatureCoverage >= ligatureCount) {
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // The ligature glyph isn't covered, or the coverage
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // index is too big. The latter means that the
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // table is mal-formed...
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 markPosition = glyphIterator->getCurrStreamPosition();
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    Offset ligatureAttachOffset = SWAPW(ligatureArray->ligatureAttachTableOffsetArray[ligatureCoverage]);
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const LigatureAttachTable *ligatureAttachTable = (const LigatureAttachTable *) ((char *) ligatureArray + ligatureAttachOffset);
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 componentCount = SWAPW(ligatureAttachTable->componentCount);
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    le_int32 component = ligatureIterator.getMarkComponent(markPosition);
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (component >= componentCount) {
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        // should really just bail at this point...
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        component = componentCount - 1;
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const ComponentRecord *componentRecord = &ligatureAttachTable->componentRecordArray[component * mcCount];
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    Offset anchorTableOffset = SWAPW(componentRecord->ligatureAnchorTableOffsetArray[markClass]);
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const AnchorTable *anchorTable = (const AnchorTable *) ((char *) ligatureAttachTable + anchorTableOffset);
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    LEPoint ligatureAnchor, markAdvance, pixels;
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    anchorTable->getAnchor(ligatureGlyph, fontInstance, ligatureAnchor);
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fontInstance->getGlyphAdvance(markGlyph, pixels);
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    fontInstance->pixelsToUnits(pixels, markAdvance);
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    float anchorDiffX = ligatureAnchor.fX - markAnchor.fX;
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    float anchorDiffY = ligatureAnchor.fY - markAnchor.fY;
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    glyphIterator->setCurrGlyphBaseOffset(ligatureIterator.getCurrStreamPosition());
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if (glyphIterator->isRightToLeft()) {
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        glyphIterator->setCurrGlyphPositionAdjustment(anchorDiffX, anchorDiffY, -markAdvance.fX, -markAdvance.fY);
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        LEPoint ligatureAdvance;
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fontInstance->getGlyphAdvance(ligatureGlyph, pixels);
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fontInstance->pixelsToUnits(pixels, ligatureAdvance);
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        glyphIterator->setCurrGlyphPositionAdjustment(anchorDiffX - ligatureAdvance.fX, anchorDiffY - ligatureAdvance.fY, -markAdvance.fX, -markAdvance.fY);
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return 1;
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END
104