Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opentype #1723

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added pdf/assets/fontkit.js
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not needed.

Empty file.
56 changes: 56 additions & 0 deletions pdf/lib/src/pdf/font/glyph/bbox.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Represents a glyph bounding box
*/
class BBox {
BBox([
this.minX = double.infinity,
this.minY = double.infinity,
this.maxX = double.infinity * -1,
this.maxY = double.infinity * -1,
]);

num minX = double.infinity;
num minY = double.infinity;
num maxX = double.infinity * -1;
num maxY = double.infinity * -1;

/**
* The width of the bounding box
*/
get width {
return this.maxX - this.minX;
}

/**
* The height of the bounding box
*/
get height {
return this.maxY - this.minY;
}

addPoint(num x, num y) {
if (x.abs() != double.infinity) {
if (x < this.minX) {
this.minX = x;
}

if (x > this.maxX) {
this.maxX = x;
}
}

if (y.abs() != double.infinity) {
if (y < this.minY) {
this.minY = y;
}

if (y > this.maxY) {
this.maxY = y;
}
}
}

copy() {
return BBox(this.minX, this.minY, this.maxX, this.maxY);
}
}
77 changes: 77 additions & 0 deletions pdf/lib/src/pdf/font/glyph_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'glyph/bbox.dart';
import 'ot_processor.dart';
import 'ttf_parser.dart';

bool checkMark(int code) {
return false;
}

class GlyphInfo {
GlyphInfo(this.font, int id, [List<int>? codePoints, dynamic features]) {
this.id = id;
this.codePoints = codePoints ?? [];
this.features = {};
if (features is List<String>) {
for (int i = 0; i < features.length; i++) {
var feature = features[i];
this.features[feature] = true;
}
} else if (features is Map<String, bool>) {
this.features = {...features};
}

this.ligatureID = null;
this.ligatureComponent = null;
this.cursiveAttachment = null;
this.markAttachment = null;
this.shaperInfo = null;
}
final TtfParser font;
int _id = 0;
late List<int> codePoints;
late Map<String, bool> features;
bool isMultiplied = false;
bool substituted = false;
bool isLigated = false;
bool isMark = false;
bool isLigature = false;
bool isBase = false;
int markAttachmentType = 0;
BBox bbox = BBox();

dynamic ligatureID;
dynamic ligatureComponent;
dynamic markAttachment;
dynamic cursiveAttachment;
dynamic shaperInfo;

int get id {
return this._id;
}

set id(int val) {
this._id = val;
this.substituted = true;
var GDEF = this.font.GDEF;
if (GDEF != null && GDEF.glyphClassDef) {
// TODO: clean this up
var classID = OTProcessor.getClassID(id, GDEF.glyphClassDef);
this.isBase = classID == 1;
this.isLigature = classID == 2;
this.isMark = classID == 3;
this.markAttachmentType = GDEF.markAttachClassDef
? OTProcessor.getClassID(id, GDEF.markAttachClassDef)
: 0;
} else {
this.isMark =
this.codePoints.length > 0 && this.codePoints.every(checkMark);
this.isBase = !this.isMark;
this.isLigature = this.codePoints.length > 1;
this.markAttachmentType = 0;
}
}

copy() {
return GlyphInfo(this.font, this.id, this.codePoints, this.features);
}
}
82 changes: 82 additions & 0 deletions pdf/lib/src/pdf/font/glyph_iterator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'glyph_info.dart';
import 'gsub_parser.dart';

class GlyphIterator {
GlyphIterator(this.glyphs, [this.options]) {
this.reset(this.options);
}
final List<GlyphInfo> glyphs;
LookupFlag? options;
int index = 0;
Map<String, bool>? flags;
int markAttachmentType = 0;

reset(LookupFlag? options, [int index = 0]) {
this.options = options;
this.flags = options?.flags;
this.markAttachmentType = options?.markAttachmentType ?? 0;
this.index = index;
}

GlyphInfo get cur {
return this.glyphs[this.index];
}

shouldIgnore(GlyphInfo glyph) {
return this.flags != null &&
((this.flags!['ignoreMarks']! && glyph.isMark) ||
(this.flags!['ignoreBaseGlyphs']! && glyph.isBase) ||
(this.flags!['ignoreLigatures']! && glyph.isLigature) ||
(this.markAttachmentType > 0 &&
glyph.isMark &&
glyph.markAttachmentType != this.markAttachmentType));
}

GlyphInfo? move(int dir) {
this.index += dir;
while (0 <= this.index &&
this.index < this.glyphs.length &&
this.shouldIgnore(this.glyphs[this.index])) {
this.index += dir;
}

if (0 > this.index || this.index >= this.glyphs.length) {
return null;
}

return this.glyphs[this.index];
}

GlyphInfo? next() {
return this.move(1);
}

GlyphInfo? prev() {
return this.move(-1);
}

GlyphInfo peek([int count = 1]) {
int idx = this.index;
GlyphInfo res = this.increment(count);
this.index = idx;
return res;
}

int peekIndex([int count = 1]) {
int idx = this.index;
this.increment(count);
int res = this.index;
this.index = idx;
return res;
}

GlyphInfo increment([int count = 1]) {
int dir = count < 0 ? -1 : 1;
count = count.abs();
while (count-- > 0) {
this.move(dir);
}

return this.glyphs[this.index];
}
}
1 change: 1 addition & 0 deletions pdf/lib/src/pdf/font/gpos_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class GposTableParser {}
128 changes: 128 additions & 0 deletions pdf/lib/src/pdf/font/gpos_processor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'ot_processor.dart';

class GPOSProcessor extends OTProcessor {
GPOSProcessor(super.font, super.table);

applyPositionValue(sequenceIndex, value) {
var position = this.positions[this.glyphIterator.peekIndex(sequenceIndex)];
if (value.xAdvance != null) {
position.xAdvance += value.xAdvance;
}

if (value.yAdvance != null) {
position.yAdvance += value.yAdvance;
}

if (value.xPlacement != null) {
position.xOffset += value.xPlacement;
}

if (value.yPlacement != null) {
position.yOffset += value.yPlacement;
}

// Adjustments for font variations
var variationProcessor = this.font.variationProcessor;
var variationStore =
this.font.GDEF != null ? this.font.GDEF.itemVariationStore : null;
if (variationProcessor && variationStore) {
if (value.xPlaDevice) {
position.xOffset += variationProcessor.getDelta(
variationStore, value.xPlaDevice.a, value.xPlaDevice.b);
}

if (value.yPlaDevice) {
position.yOffset += variationProcessor.getDelta(
variationStore, value.yPlaDevice.a, value.yPlaDevice.b);
}

if (value.xAdvDevice) {
position.xAdvance += variationProcessor.getDelta(
variationStore, value.xAdvDevice.a, value.xAdvDevice.b);
}

if (value.yAdvDevice) {
position.yAdvance += variationProcessor.getDelta(
variationStore, value.yAdvDevice.a, value.yAdvDevice.b);
}
}

// TODO: device tables
}

applyLookup(lookupType, table) {
return false;
}

applyAnchor(markRecord, baseAnchor, baseGlyphIndex) {}

getAnchor(anchor) {
// TODO: contour point, device tables
var x = anchor.xCoordinate;
var y = anchor.yCoordinate;

// Adjustments for font variations
var variationProcessor = this.font.variationProcessor;
var variationStore =
this.font.GDEF != null ? this.font.GDEF.itemVariationStore : null;
if (variationProcessor && variationStore) {
if (anchor.xDeviceTable) {
x += variationProcessor.getDelta(
variationStore, anchor.xDeviceTable.a, anchor.xDeviceTable.b);
}

if (anchor.yDeviceTable) {
y += variationProcessor.getDelta(
variationStore, anchor.yDeviceTable.a, anchor.yDeviceTable.b);
}
}

return {x, y};
}

applyFeatures(userFeatures, glyphs, advances) {
super.applyFeatures(userFeatures, glyphs, advances);

for (var i = 0; i < this.glyphs.length; i++) {
this.fixCursiveAttachment(i);
}

this.fixMarkAttachment();
}

fixCursiveAttachment(i) {
var glyph = this.glyphs[i];
if (glyph.cursiveAttachment != null) {
var j = glyph.cursiveAttachment;

glyph.cursiveAttachment = null;
this.fixCursiveAttachment(j);

this.positions[i].yOffset += this.positions[j].yOffset;
}
}

fixMarkAttachment() {
for (int i = 0; i < this.glyphs.length; i++) {
var glyph = this.glyphs[i];
if (glyph.markAttachment != null) {
var j = glyph.markAttachment;

this.positions[i].xOffset += this.positions[j].xOffset;
this.positions[i].yOffset += this.positions[j].yOffset;

if (this.direction == 'ltr') {
for (int k = j; k < i; k++) {
this.positions[i].xOffset -= this.positions[k].xAdvance;
this.positions[i].yOffset -= this.positions[k].yAdvance;
}
} else {
for (int k = j + 1; k < i + 1; k++) {
this.positions[i].xOffset += this.positions[k].xAdvance;
this.positions[i].yOffset += this.positions[k].yAdvance;
}
}
}
}
}
}
Loading
Loading