5 #include FT_TRUETYPE_TABLES_H
6 #include <fontconfig/fontconfig.h>
7 #include <fontconfig/fcfreetype.h>
12 #define PROGNAME "flt-gui"
14 #elif defined (FLT_OTF)
18 #define PROGNAME "flt-otf"
20 #elif defined (FLT_HB)
24 #define PROGNAME "flt-hb"
26 #else /* (defined (FLT_PANGO)) */
29 #define PANGO_ENABLE_ENGINE
30 #define PANGO_ENABLE_BACKEND
31 #include <pango/pango.h>
32 #include <pango/pango-ot.h>
33 #include <pango/pangoft2.h>
34 #include <pango/pangofc-font.h>
37 static FT_Library ft_library;
40 parse_font_name (char *fontname, char **family, int *size, char **err)
46 pat = FcNameParse ((FcChar8 *) fontname);
49 *err = "invalid name format";
52 if (FcPatternGetString (pat, FC_FAMILY, 0, &fam) != FcResultMatch)
54 FcPatternDestroy (pat);
55 *err = "no family name";
58 if (FcPatternGetDouble (pat, FC_PIXEL_SIZE, 0, &pixelsize) != FcResultMatch)
60 *family = strdup ((char *) fam);
62 FcPatternDestroy (pat);
67 new_face (char *fontname, char **err)
77 if (parse_font_name (fontname, (char **) &fam, &size, err) < 0)
80 pat = FcPatternBuild (0,
81 FC_FAMILY, FcTypeString, fam,
82 FC_SCALABLE, FcTypeBool, FcTrue,
85 os = FcObjectSetBuild (FC_FILE, NULL);
86 fs = FcFontList (NULL, pat, os);
89 *err = "no matching font";
92 FcPatternGetString (fs->fonts[0], FC_FILE, 0, &file);
94 if (FT_New_Face (ft_library, (char *) file, 0, &face))
96 FcFontSetDestroy (fs);
97 FcObjectSetDestroy (os);
98 FcPatternDestroy (pat);
99 *err = "font open fail";
102 FcFontSetDestroy (fs);
103 FcObjectSetDestroy (os);
104 FcPatternDestroy (pat);
105 if (FT_Set_Pixel_Sizes (face, pixelsize, pixelsize))
107 *err = "set pixelsize fail";
125 open_font (char *fontname, char **err)
127 FT_Face ft_face = new_face (fontname, err);
137 fontset = mfontset ("generic");
138 mface_put_prop (face, Mfontset, fontset);
139 mplist_add (plist, Mface, face);
140 mplist_add (plist, Mdevice, Mnil);
141 frame = mframe (plist);
142 m17n_object_unref (plist);
143 m17n_object_unref (face);
144 m17n_object_unref (fontset);
146 font = calloc (1, sizeof (MFLTFont));
147 font->font = mfont_encapsulate (frame, Mfreetype, ft_face);
148 font->face = ft_face;
153 close_font (MFLTFont *font)
155 FT_Done_Face (font->face);
157 m17n_object_unref (frame);
161 flt (MText *mt, int from, int to, MFLTFont *font, char *flt_name)
163 MDrawControl control;
165 memset (&control, 0, sizeof (MDrawControl));
166 control.two_dimensional = 1;
167 control.enable_bidi = 1;
168 control.anti_alias = 1;
169 /*control.disable_caching = 1;*/
170 mtext_put_prop (mt, from, to, Mfont, font->font);
171 mdraw_text_extents (frame, mt, from, to, &control, NULL, NULL, NULL);
175 #elif defined (FLT_OTF) || defined (FLT_HB)
183 get_glyph_id (MFLTFont *font, MFLTGlyph *g)
185 FT_Face face = ((FontInfo *) font)->face;
187 g->code = FT_Get_Char_Index (face, g->code);
189 return (g->code ? 0 : -1);
193 get_metric (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
195 FT_Face face = ((FontInfo *) font)->face;
197 for (; from < to; from++)
199 MFLTGlyph *g = gstring->glyphs + from;
200 FT_Glyph_Metrics *metrics;
202 if (FT_Load_Glyph (face, g->code, FT_LOAD_DEFAULT))
204 metrics = &face->glyph->metrics;
205 g->lbearing = metrics->horiBearingX;
206 g->rbearing = metrics->horiBearingX + metrics->width;
207 g->xadv = metrics->horiAdvance;
208 g->yadv = metrics->vertAdvance;
209 g->ascent = metrics->horiBearingY;
210 g->descent = metrics->height - metrics->horiBearingY;
216 flt (MText *mt, int from, int to, MFLTFont *font, char *flt_name)
218 MFLTGlyphString gstring;
222 memset (&gstring, 0, sizeof (MFLTGlyphString));
223 gstring.glyph_size = sizeof (MFLTGlyph);
224 gstring.allocated = len * 2;
225 gstring.glyphs = alloca (sizeof (MFLTGlyph) * gstring.allocated);
226 for (i = 0; i < len; i++)
227 gstring.glyphs[i].c = mtext_ref_char (mt, from + i);
229 return mflt_run (&gstring, 0, len, font, msymbol (flt_name));
240 #define DEVICE_DELTA(table, size) \
241 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
242 ? ((table).DeltaValue[(size) - (table).StartSize] << 6) \
246 adjust_anchor (OTF_Anchor *anchor, FT_Face face,
247 unsigned code, unsigned x_ppem, unsigned y_ppem, int *x, int *y)
249 if (anchor->AnchorFormat == 2)
252 int ap = anchor->f.f1.AnchorPoint;
254 FT_Load_Glyph (face, (FT_UInt) code, FT_LOAD_MONOCHROME);
255 outline = &face->glyph->outline;
256 if (ap < outline->n_points)
258 *x = outline->points[ap].x;
259 *y = outline->points[ap].y;
262 else if (anchor->AnchorFormat == 3)
264 if (anchor->f.f2.XDeviceTable.offset)
265 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
266 if (anchor->f.f2.YDeviceTable.offset)
267 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
272 tag_name (char *str, unsigned tag)
275 *str++ = (tag >> 16) & 0xFF;
276 *str++ = (tag >> 8) & 0xFF;
282 encode_features (char *str, int count, unsigned *features)
287 for (i = 0; i < count; i++)
289 unsigned tag = features[i];
299 str = tag_name (str, tag);
312 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
313 MFLTGlyphString *in, int from, int to,
314 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
318 OTF *otf = ((FontInfoOTF *) font)->otf;
319 OTF_GlyphString otf_gstring;
321 char script[4], langsys[4];
322 char *gsub_features = NULL, *gpos_features = NULL;
329 otf_gstring.glyphs = NULL;
330 if (OTF_get_table (otf, "head") < 0)
333 ((FontInfoOTF *) font)->otf = NULL;
337 tag_name (script, spec->script);
338 tag_name (langsys, spec->langsys);
340 if (spec->gsub_count > 0)
342 gsub_features = alloca (6 * spec->gsub_count);
345 if (OTF_check_table (otf, "GSUB") < 0)
346 gsub_features = NULL;
348 encode_features (gsub_features, spec->gsub_count, spec->gsub);
351 if (spec->gpos_count)
353 gpos_features = alloca (6 * spec->gpos_count);
356 if (OTF_check_table (otf, "GPOS") < 0)
357 gpos_features = NULL;
359 encode_features (gpos_features, spec->gpos_count, spec->gpos);
363 otf_gstring.size = otf_gstring.used = len;
364 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
365 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
366 for (i = 0; i < len; i++)
368 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
369 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
372 OTF_drive_gdef (otf, &otf_gstring);
377 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
380 if (out->allocated < out->used + otf_gstring.used)
382 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; i++, otfg++)
384 MFLTGlyph *g = out->glyphs + out->used;
387 *g = in->glyphs[from + otfg->f.index.from];
389 for (j = from + otfg->f.index.from; j <= from + otfg->f.index.to; j++)
390 if (in->glyphs[j].code == otfg->glyph_id)
392 g->c = in->glyphs[j].c;
395 g->code = otfg->glyph_id;
401 if (out->allocated < out->used + len)
403 for (i = 0; i < len; i++)
404 out->glyphs[out->used++] = in->glyphs[from + i];
407 font->get_metric (font, out, gidx, out->used);
412 MFLTGlyph *base = NULL, *mark = NULL, *g;
413 int x_ppem, y_ppem, x_scale, y_scale;
415 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
419 face = ((FontInfo *) font)->face;
420 x_ppem = face->size->metrics.x_ppem;
421 y_ppem = face->size->metrics.y_ppem;
422 x_scale = face->size->metrics.x_scale;
423 y_scale = face->size->metrics.y_scale;
425 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
426 i < otf_gstring.used; i++, otfg++, g++)
430 if (! otfg->glyph_id)
432 switch (otfg->positioning_type)
438 int format = otfg->f.f1.format;
440 if (format & OTF_XPlacement)
442 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
443 if (format & OTF_XPlaDevice)
445 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
446 if (format & OTF_YPlacement)
448 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
449 if (format & OTF_YPlaDevice)
451 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
452 if (format & OTF_XAdvance)
454 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
455 if (format & OTF_XAdvDevice)
457 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
458 if (format & OTF_YAdvance)
460 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
461 if (format & OTF_YAdvDevice)
463 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
464 adjustment[i].set = 1;
468 /* Not yet supported. */
474 goto label_adjust_anchor;
475 default: /* i.e. case 6 */
482 int base_x, base_y, mark_x, mark_y;
484 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
485 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
486 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
487 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
489 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
490 adjust_anchor (otfg->f.f4.base_anchor, face, prev->code,
491 x_ppem, y_ppem, &base_x, &base_y);
492 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
493 adjust_anchor (otfg->f.f4.mark_anchor, face, g->code,
494 x_ppem, y_ppem, &mark_x, &mark_y);
495 adjustment[i].xoff = (base_x - mark_x);
496 adjustment[i].yoff = - (base_y - mark_y);
497 adjustment[i].back = (g - prev);
498 adjustment[i].set = 1;
501 if (otfg->GlyphClass == OTF_GlyphClass0)
503 else if (otfg->GlyphClass == OTF_GlyphClassMark)
509 free (otf_gstring.glyphs);
513 font->get_metric (font, in, from, to);
514 for (i = 0; i < len; i++)
516 MFLTGlyph *g = in->glyphs + (from + i);
518 out->glyphs[out->used++] = *g;
520 if (otf_gstring.glyphs)
521 free (otf_gstring.glyphs);
526 open_font (char *fontname, char **err)
528 FT_Face face = new_face (fontname, err);
529 FontInfoOTF *font_info;
533 font_info = malloc (sizeof (FontInfoOTF));
534 font_info->face = face;
535 font_info->otf = OTF_open_ft_face (face);
536 return ((MFLTFont *) font_info);
540 close_font (MFLTFont *font)
542 FontInfoOTF *font_info = (FontInfoOTF *) font;
544 OTF_close (font_info->otf);
545 FT_Done_Face (font_info->face);
565 HB_StreamRec gdef_stream;
569 MPlist *otf_spec_cache;
573 convertStringToGlyphIndices (HB_Font font, const HB_UChar16 *string,
574 hb_uint32 length, HB_Glyph *glyphs,
575 hb_uint32 *numGlyphs, HB_Bool rightToLeft)
581 getGlyphAdvances (HB_Font font, const HB_Glyph *glyphs, hb_uint32 numGlyphs,
582 HB_Fixed *advances, int flags /*HB_ShaperFlag*/)
586 for (i = 0; i < numGlyphs; i++)
591 canRender (HB_Font font, const HB_UChar16 *string, hb_uint32 length)
597 getPointInOutline (HB_Font font, HB_Glyph glyph, int flags /*HB_ShaperFlag*/,
598 hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos,
601 FT_Face face = font->faceData;
604 if ((error = (HB_Error) FT_Load_Glyph (face, glyph, flags)))
606 if (face->glyph->format != ft_glyph_format_outline)
607 return (HB_Error) HB_Err_Invalid_GPOS_SubTable;
608 *nPoints = face->glyph->outline.n_points;
611 if (point > *nPoints)
612 return (HB_Error) HB_Err_Invalid_GPOS_SubTable;
614 *xpos = face->glyph->outline.points[point].x;
615 *ypos = face->glyph->outline.points[point].y;
621 getGlyphMetrics (HB_Font font, HB_Glyph glyph, HB_GlyphMetrics *metrics)
626 getFontMetric (HB_Font font, HB_FontMetric metric)
631 #define MAKE_TAG(name) ((((FT_ULong) (name)[0]) << 24) \
632 | (((FT_ULong) (name)[1]) << 16) \
633 | (((FT_ULong) (name)[2]) << 8) \
637 new_stream (FT_Face face, char *tagname, HB_Stream stream)
639 FT_ULong tag = MAKE_TAG (tagname);
643 if (! FT_IS_SFNT (face))
644 return HB_Err_Invalid_Argument;
646 if (FT_Load_Sfnt_Table (face, tag, 0, NULL, &len))
647 return HB_Err_Table_Missing;
650 return HB_Err_Out_Of_Memory;
651 if (FT_Load_Sfnt_Table (face, tag, 0, buf, &len))
654 return HB_Err_Table_Missing;
656 stream->base = (HB_Byte *) buf;
659 stream->cursor = NULL;
665 load_gdef (FontInfoHB *font_info)
669 if (! font_info->gdef_stream.base)
671 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GDEF",
672 &font_info->gdef_stream);
673 if (err != HB_Err_Ok)
676 return HB_Load_GDEF_Table (&font_info->gdef_stream, &font_info->gdef);
680 load_gsub (FontInfoHB *font_info)
684 HB_LookupList *lookup_list;
687 if (! font_info->gdef)
689 if ((err = load_gdef (font_info)) != HB_Err_Ok)
692 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GSUB", &stream);
693 if (err != HB_Err_Ok)
695 err = HB_Load_GSUB_Table (&stream, &font_info->gsub,
696 font_info->gdef, &font_info->gdef_stream);
698 lookup_list = &font_info->gsub->LookupList;
699 for (i = 0; i < lookup_list->LookupCount; i++)
700 lookup_list->Properties[i] = HB_GLYPH_PROPERTIES_UNKNOWN;
705 load_gpos (FontInfoHB *font_info)
709 HB_LookupList *lookup_list;
712 if (! font_info->gdef)
714 if ((err = load_gdef (font_info)) != HB_Err_Ok)
717 err = new_stream ((FT_Face) font_info->hb_font.faceData, "GPOS", &stream);
718 if (err != HB_Err_Ok)
720 err = HB_Load_GPOS_Table (&stream, &font_info->gpos,
721 font_info->gdef, &font_info->gdef_stream);
723 lookup_list = &font_info->gpos->LookupList;
724 for (i = 0; i < lookup_list->LookupCount; i++)
725 lookup_list->Properties[i] = HB_GLYPH_PROPERTIES_UNKNOWN;
729 const HB_FontClass hb_fontClass = {
730 convertStringToGlyphIndices, getGlyphAdvances, canRender,
731 getPointInOutline, getGlyphMetrics, getFontMetric
735 setup_features (int gsubp, FontInfoHB *font_info, MFLTOtfSpec *spec,
738 int count, preordered;
739 unsigned int *features;
740 FT_UShort script, langsys;
741 FT_UShort req_feature, index;
742 FT_UInt *feature_list;
748 if (! font_info->gsub)
750 if (load_gsub (font_info) != HB_Err_Ok)
753 if (HB_GSUB_Select_Script (font_info->gsub, spec->script, &script)
758 if (HB_GSUB_Select_Language (font_info->gsub, spec->langsys,
759 script, &langsys, &req_feature)
764 langsys = req_feature = 0xFFFF;
765 count = spec->gsub_count;
766 features = spec->gsub;
770 if (! font_info->gpos)
772 if (load_gpos (font_info) != HB_Err_Ok)
775 if (HB_GPOS_Select_Script (font_info->gpos, spec->script, &script)
780 if (HB_GPOS_Select_Language (font_info->gpos, spec->langsys,
781 script, &langsys, &req_feature)
786 langsys = req_feature = 0xFFFF;
787 count = spec->gpos_count;
788 features = spec->gpos;
791 for (preordered = 0; preordered < count; preordered++)
792 if (features[preordered] == 0)
794 if ((gsubp ? HB_GSUB_Query_Features (font_info->gsub, script, langsys,
796 : HB_GPOS_Query_Features (font_info->gpos, script, langsys,
803 for (i = 0; feature_list[i]; i++);
806 info->indices = malloc (sizeof (FT_UShort) * ((req_feature != 0xFFFF) + i));
808 if (req_feature != 0xFFFF)
809 info->indices[i++] = req_feature;
810 for (j = 0; j < preordered; j++)
811 if ((gsubp ? HB_GSUB_Select_Feature (font_info->gsub, features[j],
812 script, langsys, &index)
813 : HB_GPOS_Select_Feature (font_info->gpos, features[j],
814 script, langsys, &index))
816 info->indices[i++] = index;
818 for (j = 0; feature_list[j]; j++)
820 for (k = preordered + 1; k < count; k++)
821 if (feature_list[j] == features[k])
825 ? HB_GSUB_Select_Feature (font_info->gsub, feature_list[j],
826 script, langsys, &index)
827 : HB_GPOS_Select_Feature (font_info->gpos, feature_list[j],
828 script, langsys, &index))
830 info->indices[i++] = index;
836 setup_otf_spec (MFLTFont *font, MFLTOtfSpec *spec)
838 FontInfoHB *font_info = (FontInfoHB *) font;
841 if (spec->gsub_count + spec->gpos_count == 0)
843 ginfo = mplist_get (font_info->otf_spec_cache, spec->sym);
845 return (ginfo->gsub.count + ginfo->gpos.count > 0 ? ginfo : NULL);
846 ginfo = calloc (1, sizeof (GsubGposInfo));
847 mplist_push (font_info->otf_spec_cache, spec->sym, ginfo);
848 setup_features (1, font_info, spec, &(ginfo->gsub));
849 setup_features (0, font_info, spec, &(ginfo->gpos));
854 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
855 MFLTGlyphString *in, int from, int to,
856 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
858 FontInfoHB *font_info = (FontInfoHB *) font;
863 HB_UShort *apply_order_save;
864 HB_UShort apply_count_save;
865 int gpos_applied = 0;
872 if (hb_buffer_new (&buf) != FT_Err_Ok)
874 for (i = from; i < to; i++)
876 if (hb_buffer_add_glyph (buf, in->glyphs[i].code, 0, i) != FT_Err_Ok)
879 ginfo = setup_otf_spec (font, spec);
882 if (ginfo->gsub.count > 0)
884 if (! font_info->gdef
885 && load_gdef (font_info) != HB_Err_Ok)
887 apply_order_save = font_info->gsub->FeatureList.ApplyOrder;
888 apply_count_save = font_info->gsub->FeatureList.ApplyCount;
889 font_info->gsub->FeatureList.ApplyOrder = ginfo->gsub.indices;
890 font_info->gsub->FeatureList.ApplyCount = ginfo->gsub.count;
891 err = HB_GSUB_Apply_String (font_info->gsub, buf);
892 font_info->gsub->FeatureList.ApplyOrder = apply_order_save;
893 font_info->gsub->FeatureList.ApplyCount = apply_count_save;
894 if (err != HB_Err_Ok && err != HB_Err_Not_Covered)
896 if (out->used + buf->in_length > out->allocated)
900 if (ginfo->gpos.count > 0
901 && (font_info->gpos || load_gpos (font_info) == HB_Err_Ok))
903 apply_order_save = font_info->gpos->FeatureList.ApplyOrder;
904 apply_count_save = font_info->gpos->FeatureList.ApplyCount;
905 font_info->gpos->FeatureList.ApplyOrder = ginfo->gpos.indices;
906 font_info->gpos->FeatureList.ApplyCount = ginfo->gpos.count;
907 err = HB_GPOS_Apply_String (&font_info->hb_font, font_info->gpos,
908 FT_LOAD_DEFAULT, buf, FALSE, FALSE);
909 font_info->gpos->FeatureList.ApplyOrder = apply_order_save;
910 font_info->gpos->FeatureList.ApplyCount = apply_count_save;
911 if (err == HB_Err_Ok)
915 for (i = 0; i < buf->in_length; i++)
917 HB_GlyphItem hg = buf->in_string + i;
918 HB_Position pos = buf->positions + i;
921 out->glyphs[out->used] = in->glyphs[hg->cluster];
922 g = out->glyphs + out->used++;
923 if (g->code != hg->gindex)
924 g->c = 0, g->code = hg->gindex;
925 adjustment[i].set = gpos_applied;
928 adjustment[i].xadv = pos->x_advance;
929 adjustment[i].yadv = pos->y_advance;
930 adjustment[i].xoff = pos->x_pos;
931 adjustment[i].yoff = - pos->y_pos;
932 adjustment[i].back = pos->back;
933 adjustment[i].advance_is_absolute = pos->new_advance;
940 hb_buffer_free (buf);
941 for (i = 0; i < len; i++)
942 out->glyphs[out->used++] = in->glyphs[from + i];
947 open_font (char *fontname, char **err)
949 FT_Face face = new_face (fontname, err);
950 FontInfoHB *font_info;
954 font_info = calloc (1, sizeof (FontInfoHB));
955 font_info->face = face;
956 font_info->hb_font.klass = &hb_fontClass;
957 font_info->hb_font.faceData = face;
958 font_info->hb_font.userData = NULL;
959 font_info->hb_font.x_ppem = face->size->metrics.x_ppem;
960 font_info->hb_font.x_scale = face->size->metrics.x_scale;
961 font_info->hb_font.y_scale = face->size->metrics.y_scale;
962 font_info->hb_font.y_ppem = face->size->metrics.y_ppem;
963 font_info->otf_spec_cache = mplist ();
964 return ((MFLTFont *) font_info);
968 close_font (MFLTFont *font)
970 FontInfoHB *font_info = (FontInfoHB *) font;
975 HB_Done_GDEF_Table (font_info->gdef);
976 free (font_info->gdef_stream.base);
979 HB_Done_GSUB_Table (font_info->gsub);
981 HB_Done_GPOS_Table (font_info->gpos);
982 FT_Done_Face ((FT_Face) (font_info->hb_font.faceData));
983 for (p = font_info->otf_spec_cache; mplist_key (p) != Mnil;
986 GsubGposInfo *ginfo = mplist_value (p);
988 if (ginfo->gsub.count > 0)
989 free (ginfo->gsub.indices);
990 if (ginfo->gpos.count > 0)
991 free (ginfo->gpos.indices);
993 m17n_object_unref (font_info->otf_spec_cache);
999 #else /* (defined (FLT_PANGO) */
1004 PangoOTRuleset *ruleset;
1014 PangoFontMap *fontmap;
1015 PangoContext *context;
1016 PangoFcFont *pango_font;
1017 MPlist *otf_spec_cache;
1021 get_glyph_id (MFLTFont *font, MFLTGlyph *g)
1023 FontInfoPango *font_info = (FontInfoPango *) font;
1025 g->code = pango_fc_font_get_glyph (font_info->pango_font, g->c);
1026 return (g->code ? 0 : -1);
1030 get_metric (MFLTFont *font, MFLTGlyphString *gstring, int from, int to)
1032 FontInfoPango *font_info = (FontInfoPango *) font;
1035 for (i = from; i < to; i++)
1037 MFLTGlyph *g = gstring->glyphs + from;
1038 PangoRectangle inc, logical;
1040 pango_font_get_glyph_extents (PANGO_FONT (font_info->pango_font),
1041 gstring->glyphs[i].code, &inc, &logical);
1042 g->lbearing = inc.x;
1043 g->rbearing = inc.x + inc.width;
1044 g->xadv = logical.width;
1046 g->ascent = - inc.y;
1047 g->descent = inc.height + inc.y;
1051 #ifndef PANGO_OT_DEFAULT_LANGUAGE
1052 #define PANGO_OT_DEFAULT_LANGUAGE ((guint) 0xFFFF)
1054 #ifndef PANGO_OT_ALL_GLYPHS
1055 #define PANGO_OT_ALL_GLYPHS ((guint) 0xFFFF)
1059 setup_features (PangoOTTableType type, FontInfoPango *font_info,
1060 MFLTOtfSpec *spec, FeatureInfo *info)
1062 int count, preordered;
1063 unsigned int *features;
1064 guint script, langsys;
1065 guint req_feature, index;
1066 guint *feature_list;
1070 PangoOTInfo *ot_info;
1072 face = pango_fc_font_lock_face (font_info->pango_font);
1073 ot_info = pango_ot_info_get (face);
1074 pango_fc_font_unlock_face (font_info->pango_font);
1076 if (! pango_ot_info_find_script (ot_info, type, spec->script, &script))
1080 if (! pango_ot_info_find_language (ot_info, type, script,
1082 &langsys, &req_feature))
1087 langsys = PANGO_OT_DEFAULT_LANGUAGE;
1088 req_feature = 0xFFFF;
1090 if (type == PANGO_OT_TABLE_GSUB)
1092 count = spec->gsub_count;
1093 features = spec->gsub;
1097 count = spec->gpos_count;
1098 features = spec->gpos;
1100 feature_list = NULL;
1101 for (preordered = 0; preordered < count; preordered++)
1102 if (features[preordered] == 0)
1104 feature_list = pango_ot_info_list_features (ot_info, type, 0,
1111 for (i = 0; feature_list[i]; i++);
1114 info->indices = malloc (sizeof (guint) * ((req_feature != 0xFFFF) + i));
1116 if (req_feature != 0xFFFF)
1117 info->indices[i++] = req_feature;
1118 for (j = 0; j < preordered; j++)
1119 if (pango_ot_info_find_feature (ot_info, type, features[j],
1120 script, langsys, &index))
1121 info->indices[i++] = index;
1123 for (j = 0; feature_list[j]; j++)
1125 for (k = preordered + 1; k < count; k++)
1126 if (feature_list[j] == features[k])
1129 && pango_ot_info_find_feature (ot_info, type, feature_list[j],
1130 script, langsys, &index))
1131 info->indices[i++] = index;
1134 info->ruleset = pango_ot_ruleset_new (ot_info);
1135 for (i = 0; i < info->count; i++)
1136 pango_ot_ruleset_add_feature (info->ruleset, type, info->indices[i],
1137 PANGO_OT_ALL_GLYPHS);
1141 setup_otf_spec (FontInfoPango *font_info, MFLTOtfSpec *spec)
1143 GsubGposInfo *ginfo;
1145 if (spec->gsub_count + spec->gpos_count == 0)
1147 ginfo = mplist_get (font_info->otf_spec_cache, spec->sym);
1149 return (ginfo->gsub.count + ginfo->gpos.count > 0 ? ginfo : NULL);
1150 ginfo = calloc (1, sizeof (GsubGposInfo));
1151 mplist_push (font_info->otf_spec_cache, spec->sym, ginfo);
1152 setup_features (PANGO_OT_TABLE_GSUB, font_info, spec, &(ginfo->gsub));
1153 setup_features (PANGO_OT_TABLE_GPOS, font_info, spec, &(ginfo->gpos));
1158 drive_otf (MFLTFont *font, MFLTOtfSpec *spec,
1159 MFLTGlyphString *in, int from, int to,
1160 MFLTGlyphString *out, MFLTGlyphAdjustment *adjustment)
1162 FontInfoPango *font_info = (FontInfoPango *) font;
1163 PangoOTBuffer *buffer;
1164 GsubGposInfo *ginfo;
1165 PangoGlyphString *glyphs;
1168 buffer = pango_ot_buffer_new (font_info->pango_font);
1169 for (i = from; i < to; i++)
1170 pango_ot_buffer_add_glyph (buffer, in->glyphs[i].code, 0, i);
1171 ginfo = setup_otf_spec (font_info, spec);
1174 if (ginfo->gsub.count > 0)
1175 pango_ot_ruleset_substitute (ginfo->gsub.ruleset, buffer);
1176 if (ginfo->gpos.count > 0)
1177 pango_ot_ruleset_position (ginfo->gpos.ruleset, buffer);
1178 glyphs = pango_glyph_string_new ();
1179 pango_ot_buffer_output (buffer, glyphs);
1180 for (i = 0; i < glyphs->num_glyphs; i++)
1182 PangoGlyphInfo *glyph_info = glyphs->glyphs + i;
1185 out->glyphs[out->used] = in->glyphs[glyphs->log_clusters[i]];
1186 g = out->glyphs + out->used++;
1187 if (g->code != glyph_info->glyph)
1188 g->c = 0, g->code = glyph_info->glyph;
1189 g->xoff = glyph_info->geometry.x_offset;
1190 g->yoff = glyph_info->geometry.y_offset;
1191 g->xadv = glyph_info->geometry.width;
1193 adjustment[i].set = 0;
1195 pango_ot_buffer_destroy (buffer);
1196 pango_glyph_string_free (glyphs);
1200 pango_ot_buffer_destroy (buffer);
1201 for (i = from; i < to; i++)
1202 out->glyphs[out->used++] = in->glyphs[i];
1208 open_font (char *fontname, char **err)
1210 FontInfoPango *font_info = NULL;
1211 PangoFontMap *fontmap;
1212 PangoContext *context;
1213 PangoFontDescription *desc;
1214 PangoFont *pango_font;
1219 if (parse_font_name (fontname, &family, &size, err) < 0)
1221 desc = pango_font_description_new ();
1222 pango_font_description_set_family (desc, family);
1224 pango_size = size * PANGO_SCALE;
1225 pango_font_description_set_absolute_size (desc, pango_size);
1227 fontmap = pango_ft2_font_map_new ();
1228 context = pango_context_new ();
1229 pango_context_set_font_map (context, fontmap);
1230 pango_font = pango_context_load_font (context, desc);
1231 pango_font_description_free (desc);
1234 font_info = calloc (1, sizeof (FontInfoPango));
1235 font_info->fontmap = fontmap;
1236 font_info->context = context;
1237 font_info->pango_font = PANGO_FC_FONT (pango_font);
1238 font_info->otf_spec_cache = mplist ();
1242 g_object_unref (context);
1243 g_object_unref (fontmap);
1245 return (MFLTFont *) font_info;
1249 close_font (MFLTFont *font)
1251 FontInfoPango *font_info = (FontInfoPango *) font;
1253 g_object_unref (font_info->context);
1254 g_object_unref (font_info->fontmap);
1258 flt (MText *mt, int from, int to, MFLTFont *font, char *flt_name)
1260 MFLTGlyphString gstring;
1261 int len = to - from;
1264 memset (&gstring, 0, sizeof (MFLTGlyphString));
1265 gstring.glyph_size = sizeof (MFLTGlyph);
1266 gstring.allocated = len * 2;
1267 gstring.glyphs = alloca (sizeof (MFLTGlyph) * gstring.allocated);
1268 for (i = 0; i < len; i++)
1269 gstring.glyphs[i].c = mtext_ref_char (mt, from + i);
1271 return mflt_run (&gstring, 0, len, font, msymbol (flt_name));
1277 main (int argc, char **argv)
1279 char *font_name, *flt_name;
1287 fprintf (stderr, "Usage: flt FONT-NAME FLT-NAME < TEXT-FILE\n");
1291 font_name = argv[1];
1294 FT_Init_FreeType (&ft_library);
1296 font = open_font (font_name, &err);
1299 fprintf (stderr, "Font error: %s: %s\n", argv[1], err);
1302 font->get_glyph_id = get_glyph_id;
1303 font->get_metric = get_metric;
1304 font->drive_otf = drive_otf;
1306 mt = mconv_decode_stream (msymbol ("utf-8"), stdin);
1307 len = mtext_len (mt);
1308 for (i = 0, j = mtext_character (mt, i, len, '\n'); i < len;
1309 i = j + 1, j = mtext_character (mt, i, len, '\n'))
1313 if (flt (mt, i, j, font, flt_name) < 0)
1315 fprintf (stderr, "Error in FLT processing.\n");
1319 m17n_object_unref (mt);