From cbd1d6a63a5d696b7d6a5aba9ee7305ea228416a Mon Sep 17 00:00:00 2001 From: Martin Hosken Date: Fri, 21 May 2010 15:16:43 +0100 Subject: [PATCH 1/5] Rename Grxxx to HbGrxxx --- src/hb-graphite.cc | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/hb-graphite.cc b/src/hb-graphite.cc index 1b887f4ea..708757c7f 100644 --- a/src/hb-graphite.cc +++ b/src/hb-graphite.cc @@ -46,10 +46,10 @@ typedef struct _featureSetting { int value; } featureSetting; -class GrBufferTextSrc : public gr::ITextSource +class HbGrBufferTextSrc : public gr::ITextSource { public: - GrBufferTextSrc(hb_buffer_t *buff, hb_feature_t *feats, unsigned int num_features) + HbGrBufferTextSrc(hb_buffer_t *buff, hb_feature_t *feats, unsigned int num_features) { hb_feature_t *aFeat = feats; featureSetting *aNewFeat; @@ -64,7 +64,7 @@ public: aNewFeat->value = aFeat->value; } }; - ~GrBufferTextSrc() { hb_buffer_destroy(buffer); delete[] features; }; + ~HbGrBufferTextSrc() { hb_buffer_destroy(buffer); delete[] features; }; virtual gr::UtfType utfEncodingForm() { return gr::kutf32; }; virtual size_t getLength() { return buffer->len; }; virtual size_t fetch(gr::toffset ichMin, size_t cch, gr::utf32 * prgchBuffer) @@ -120,12 +120,12 @@ private: unsigned int nFeatures; }; -class GrHbFont : public gr::Font +class HbGrFont : public gr::Font { public: - GrHbFont(hb_font_t *font, hb_face_t *face) : gr::Font() + HbGrFont(hb_font_t *font, hb_face_t *face) : gr::Font() { m_font = hb_font_reference(font); m_face = hb_face_reference(face); initfont(); }; - ~GrHbFont() + ~HbGrFont() { std::map::iterator p = m_blobs.begin(); while (p != m_blobs.end()) @@ -133,7 +133,7 @@ public: hb_font_destroy(m_font); hb_face_destroy(m_face); }; - GrHbFont (const GrHbFont &font) : gr::Font(font) + HbGrFont (const HbGrFont &font) : gr::Font(font) { *this = font; m_blobs = std::map(font.m_blobs); @@ -142,7 +142,7 @@ public: hb_font_reference(m_font); hb_face_reference(m_face); }; - virtual GrHbFont *copyThis() { return new GrHbFont(*this); }; + virtual HbGrFont *copyThis() { return new HbGrFont(*this); }; virtual bool bold() { return m_bold; }; virtual bool italic() { return m_italic; }; virtual float ascent() { float asc; getFontMetrics(&asc, NULL, NULL); return asc; }; @@ -210,7 +210,7 @@ private: std::map m_blobs; }; -void GrHbFont::initfont() +void HbGrFont::initfont() { const void *pOS2 = getTable(gr::kttiOs2, NULL); const void *pHead = getTable(gr::kttiHead, NULL); @@ -228,10 +228,10 @@ hb_graphite_shape (hb_font_t *font, unsigned int num_features) { /* create text source */ - GrBufferTextSrc textSrc(buffer, features, num_features); + HbGrBufferTextSrc textSrc(buffer, features, num_features); /* create grfont */ - GrHbFont grfont(font, face); + HbGrFont grfont(font, face); /* create segment */ int *firsts; @@ -264,7 +264,7 @@ hb_graphite_shape (hb_font_t *font, hb_buffer_ensure(buffer, numGlyphs); pSegment.getUniscribeClusters(firsts, numChars, NULL, flags, numGlyphs, NULL); glyph_range = pSegment.glyphs(); - for (pGlyph = glyph_infos, iGlyph = glyph_range.first; iGlyph < glyph_range.second; + for (pGlyph = glyph_infos, iGlyph = glyph_range.first; iGlyph != glyph_range.second; iGlyph++, pGlyph++) { *pGlyph = iGlyph->glyphID(); } @@ -285,7 +285,7 @@ hb_graphite_shape (hb_font_t *font, float curradvx = 0., curradvy = 0.; for (pPosition = hb_buffer_get_glyph_positions(buffer), iGlyph = glyph_range.first; - iGlyph < glyph_range.second; pPosition++, iGlyph++) + iGlyph != glyph_range.second; pPosition++, iGlyph++) { pPosition->x_offset = iGlyph->origin() - curradvx; pPosition->y_offset = iGlyph->yOffset() - curradvy; From 1432ab15c163eb0b5be3de66a4cb3df15ad73500 Mon Sep 17 00:00:00 2001 From: Martin Hosken Date: Sat, 22 May 2010 00:56:40 +0100 Subject: [PATCH 2/5] Add graphical output to hbtestfont --- contrib/python/lib/harfbuzz.pyx | 100 +----------------------------- contrib/python/scripts/hbtestfont | 62 +++++++++++++++++- src/Makefile.am | 2 +- src/hb-ot-shape.cc | 2 +- src/hb-ot.h | 2 +- src/hb-shape.cc | 2 +- 6 files changed, 67 insertions(+), 103 deletions(-) diff --git a/contrib/python/lib/harfbuzz.pyx b/contrib/python/lib/harfbuzz.pyx index 2a97c6cb1..139d3eac5 100644 --- a/contrib/python/lib/harfbuzz.pyx +++ b/contrib/python/lib/harfbuzz.pyx @@ -33,103 +33,7 @@ cdef extern from "hb-common.h" : cdef extern from "hb-unicode.h" : # there must be a better way of syncing this list with the true source ctypedef enum hb_script_t : - HB_SCRIPT_INVALID_CODE = -1, - HB_SCRIPT_COMMON = 0, - HB_SCRIPT_INHERITED, - HB_SCRIPT_ARABIC, - HB_SCRIPT_ARMENIAN, - HB_SCRIPT_BENGALI, - HB_SCRIPT_BOPOMOFO, - HB_SCRIPT_CHEROKEE, - HB_SCRIPT_DESERET, - HB_SCRIPT_DEVANAGARI, - HB_SCRIPT_ETHIOPIC, - HB_SCRIPT_GEORGIAN, - HB_SCRIPT_GOTHIC, - HB_SCRIPT_GREEK, - HB_SCRIPT_GUJARATI, - HB_SCRIPT_GURMUKHI, - HB_SCRIPT_HAN, - HB_SCRIPT_HANGUL, - HB_SCRIPT_HEBREW, - HB_SCRIPT_HIRAGANA, - HB_SCRIPT_KANNADA, - HB_SCRIPT_KATAKANA, - HB_SCRIPT_KHMER, - HB_SCRIPT_LAO, - HB_SCRIPT_LATIN, - HB_SCRIPT_MALAYALAM, - HB_SCRIPT_MONGOLIAN, - HB_SCRIPT_MYANMAR, - HB_SCRIPT_OGHAM, - HB_SCRIPT_OLD_ITALIC, - HB_SCRIPT_ORIYA, - HB_SCRIPT_RUNIC, - HB_SCRIPT_SINHALA, - HB_SCRIPT_SYRIAC, - HB_SCRIPT_TAMIL, - HB_SCRIPT_TELUGU, - HB_SCRIPT_THAANA, - HB_SCRIPT_THAI, - HB_SCRIPT_TIBETAN, - HB_SCRIPT_CANADIAN_ABORIGINAL, - HB_SCRIPT_YI, - HB_SCRIPT_TAGALOG, - HB_SCRIPT_HANUNDO, - HB_SCRIPT_BUHID, - HB_SCRIPT_TAGBANWA, -# Unicode 4.0 - HB_SCRIPT_BRAILLE, - HB_SCRIPT_CYPRIOT, - HB_SCRIPT_LIMBU, - HB_SCRIPT_OSMANYA, - HB_SCRIPT_SHAVIAN, - HB_SCRIPT_LINEAR_B, - HB_SCRIPT_TAI_LE, - HB_SCRIPT_UGARITIC, -# Unicode 4.1 - HB_SCRIPT_NEW_TAI_LUE, - HB_SCRIPT_BUGINESE, - HB_SCRIPT_GLAGOLITIC, - HB_SCRIPT_TIFINAGH, - HB_SCRIPT_SYLOTI_NAGRI, - HB_SCRIPT_OLD_PERSIAN, - HB_SCRIPT_KHAROSHTHI, -# Unicode 5.0 - HB_SCRIPT_UNKNOWN, - HB_SCRIPT_BALINESE, - HB_SCRIPT_CUNEIFORM, - HB_SCRIPT_PHOENICIAN, - HB_SCRIPT_PHAGS_PA, - HB_SCRIPT_NKO, -# Unicode 5.1 - HB_SCRIPT_KAYAH_LI, - HB_SCRIPT_LEPCHA, - HB_SCRIPT_REJANG, - HB_SCRIPT_SUNDANESE, - HB_SCRIPT_SAURASHTRA, - HB_SCRIPT_CHAM, - HB_SCRIPT_OL_CHIKI, - HB_SCRIPT_VAI, - HB_SCRIPT_CARIAN, - HB_SCRIPT_LYCIAN, - HB_SCRIPT_LYDIAN -# Unicode-5.2 - HB_SCRIPT_AVESTAN - HB_SCRIPT_BAMUM - HB_SCRIPT_EGYPTIAN_HIEROGLYPHS - HB_SCRIPT_IMPERIAL_ARAMAIC - HB_SCRIPT_INSCRIPTIONAL_PAHLAVI - HB_SCRIPT_INSCRIPTIONAL_PARTHIAN - HB_SCRIPT_JAVANESE - HB_SCRIPT_KAITHI - HB_SCRIPT_LISU - HB_SCRIPT_MEITEI_MAYEK - HB_SCRIPT_OLD_SOUTH_ARABIAN - HB_SCRIPT_OLD_TURKIC - HB_SCRIPT_SAMARITAN - HB_SCRIPT_TAI_THAM - HB_SCRIPT_TAI_VIET + HB_SCRIPT_COMMON = 0 cdef extern from "hb-language.h" : ctypedef void *hb_language_t @@ -279,7 +183,7 @@ cdef class ft : self.engine = engine cdef FT_Face face FT_New_Face(engine, fname, 0, &face) - FT_Set_Char_Size(face, size << 6, size << 6, 144, 144) + FT_Set_Char_Size(face, size << 6, size << 6, 72, 72) self.face = face self.hbface = hb_ft_face_create(face, hb_face_destroy) self.hbfont = hb_ft_font_create(face, hb_font_destroy) diff --git a/contrib/python/scripts/hbtestfont b/contrib/python/scripts/hbtestfont index 4d1134d1f..30110e603 100755 --- a/contrib/python/scripts/hbtestfont +++ b/contrib/python/scripts/hbtestfont @@ -1,6 +1,48 @@ #!/usr/bin/python import harfbuzz, optparse +try: + import gtk + import gobject + import cairo + from gtk import gdk +except : + raise SystemExit +import pygtk +if gtk.pygtk_version < (2, 8) : + print "PyGtk 2.8 or later required" + raise SystemExit + +class GlyphsWindow (gtk.Widget) : + def __init__(self, fontname, size, glyphs) : + gtk.Widget.__init__(self) + self.fontname = fontname + self.size = size + self.glyphs = glyphs + + def do_realize(self) : + self.set_flags(gtk.REALIZED) + self.window = gdk.Window( + self.get_parent_window(), + width = self.allocation.width, + height = self.allocation.height, + window_type = gdk.WINDOW_CHILD, + wclass = gdk.INPUT_OUTPUT, + event_mask = self.get_events() | gdk.EXPOSURE_MASK) + self.window.set_user_data(self) + self.style.attach(self.window) + self.style.set_background(self.window, gtk.STATE_NORMAL) + self.window.move_resize(*self.allocation) + + def do_unrealize(self) : + self.window.destroy() + + def do_expose_event(self, event) : + cr = self.window.cairo_create() + cr.set_matrix(cairo.Matrix(1, 0, 0, 1, 0, 2 * self.size)) + cr.set_font_face(cairo.ToyFontFace(self.fontname)) + cr.set_font_size(self.size) + cr.show_glyphs(self.glyphs) # [(gid, originx, originy)] buffer = None @@ -17,10 +59,11 @@ p.add_option('-s', '--size', default=12, type="int", help="point size") p.add_option('-l', '--lang', help="language code") p.add_option('-c', '--script', help="script code") p.add_option('-f', '--feature', action='append', help="define a feature key=val") +p.add_option('-n', '--fontname', help='Font to use to render glyphs') p.add_option('-d', '--debug', action='store_true', help="Output trace info") (opts, args) = p.parse_args() -ft = harfbuzz.ft(args[0], opts.size, trace = tracefn if opts.debug else None) +ft = harfbuzz.ft(args[0], opts.size) text = "".join(unichr(int(c, 16)) for c in args[1].split(" ")) bytes = text.encode('utf_8') buffer = harfbuzz.buffer(bytes, len(text)) @@ -33,3 +76,20 @@ if opts.feature : ft.shape(buffer, features = features) res = buffer.get_info(64) # scale for 26.6 print res +if opts.fontname : + glyphs = [] + org = [0, 0] + for g in res : + glyphs.append((g.gid, org[0] + g.offset[0], org[1] + g.offset[1])) + org[0] += g.advance[0] + org[1] += g.advance[1] + gobject.type_register(GlyphsWindow) + win = gtk.Window() + win.resize(org[0] + 10, 3 * opts.size + 40) + win.connect('delete-event', gtk.main_quit) + frame = gtk.Frame("glyphs") + win.add(frame) + w = GlyphsWindow(opts.fontname, opts.size, glyphs) + frame.add(w) + win.show_all() + gtk.main() diff --git a/src/Makefile.am b/src/Makefile.am index 810083a98..5f50f122f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -52,7 +52,7 @@ HBSOURCES += \ HBHEADERS += \ hb-ot.h \ hb-ot-layout.h \ - hb-ot-shape.h \ + hb-ot-shape-private.hh \ hb-ot-tag.h \ $(NULL) diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index 3d8966c33..8776a35cc 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -24,7 +24,7 @@ * Red Hat Author(s): Behdad Esfahbod */ -#include "hb-ot-shape.h" +#include "hb-ot-shape-private.hh" #include "hb-buffer-private.hh" diff --git a/src/hb-ot.h b/src/hb-ot.h index 91992b8c2..f14a7eec4 100644 --- a/src/hb-ot.h +++ b/src/hb-ot.h @@ -30,7 +30,7 @@ #include "hb.h" #include "hb-ot-layout.h" -#include "hb-ot-shape.h" +#include "hb-ot-shape-private.hh" #include "hb-ot-tag.h" #endif /* HB_OT_H */ diff --git a/src/hb-shape.cc b/src/hb-shape.cc index af93fc9e3..4f39cf9c3 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -30,7 +30,7 @@ #include "hb-buffer-private.hh" -#include "hb-ot-shape.h" +#include "hb-ot-shape-private.hh" #ifdef HAVE_GRAPHITE #include "hb-graphite.h" From 70ae332fe66510500d303b6fcc79537833b42f05 Mon Sep 17 00:00:00 2001 From: Martin Hosken Date: Sat, 22 May 2010 19:58:00 +0100 Subject: [PATCH 3/5] Add fontconfig to hbtestfont --- contrib/python/lib/fontconfig.pyx | 47 +++++++++++++++++++++++ contrib/python/scripts/hbtestfont | 64 +++++++++++++++++++------------ contrib/python/setup.py | 3 +- 3 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 contrib/python/lib/fontconfig.pyx diff --git a/contrib/python/lib/fontconfig.pyx b/contrib/python/lib/fontconfig.pyx new file mode 100644 index 000000000..16e02891c --- /dev/null +++ b/contrib/python/lib/fontconfig.pyx @@ -0,0 +1,47 @@ +cdef extern from "fontconfig/fontconfig.h" : + ctypedef struct FcPattern : + pass + ctypedef struct FcConfig : + pass + cdef enum FcResult '_FcResult' : + FcResultMatch = 0, FcResultNoMatch, FcResultTypeMismatch, FcResultNoId, + FcResultOutOfMemory + + ctypedef char FcChar8 + FcPattern *FcNameParse(FcChar8 *name) + FcPattern *FcFontMatch(FcConfig *config, FcPattern *match, FcResult *res) + FcResult FcPatternGetInteger(FcPattern *pattern, char *typeid, int index, int *res) + FcResult FcPatternGetString(FcPattern *pattern, char *typeid, int index, FcChar8 **res) + void FcPatternPrint(FcPattern *pattern) + void FcPatternDestroy(FcPattern *pattern) + + FcConfig *FcConfigGetCurrent() + +cdef class fcPattern : + cdef FcPattern *_pattern + + def __init__(self, char *name) : + cdef FcPattern *temp + cdef FcResult res + + temp = FcNameParse(name) + self._pattern = FcFontMatch(FcConfigGetCurrent(), temp, &res) + if res != FcResultMatch : + print "Failed to match" + str(res) + self._pattern = 0 + + def __destroy__(self) : + FcPatternDestroy(self._pattern) + + def getInteger(self, char *typeid, int index) : + cdef int res + if self._pattern == 0 or FcPatternGetInteger(self._pattern, typeid, index, &res) != FcResultMatch : return None + return res + + def getString(self, char *typeid, int index) : + cdef FcChar8 *res + if self._pattern == 0 or FcPatternGetString(self._pattern, typeid, index, &res) != FcResultMatch : return None + return res + + def debugPrint(self) : + FcPatternPrint(self._pattern) diff --git a/contrib/python/scripts/hbtestfont b/contrib/python/scripts/hbtestfont index 30110e603..070e54939 100755 --- a/contrib/python/scripts/hbtestfont +++ b/contrib/python/scripts/hbtestfont @@ -1,6 +1,8 @@ #!/usr/bin/python import harfbuzz, optparse +from fontconfig import fcPattern + try: import gtk import gobject @@ -9,16 +11,19 @@ try: except : raise SystemExit import pygtk + if gtk.pygtk_version < (2, 8) : print "PyGtk 2.8 or later required" raise SystemExit class GlyphsWindow (gtk.Widget) : - def __init__(self, fontname, size, glyphs) : + def __init__(self, fontname, bold, italic, size, glyphs) : gtk.Widget.__init__(self) self.fontname = fontname self.size = size self.glyphs = glyphs + self.bold = bold + self.italic = italic def do_realize(self) : self.set_flags(gtk.REALIZED) @@ -40,7 +45,7 @@ class GlyphsWindow (gtk.Widget) : def do_expose_event(self, event) : cr = self.window.cairo_create() cr.set_matrix(cairo.Matrix(1, 0, 0, 1, 0, 2 * self.size)) - cr.set_font_face(cairo.ToyFontFace(self.fontname)) + cr.set_font_face(cairo.ToyFontFace(self.fontname, cairo.FONT_SLANT_ITALIC if self.italic else cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD if self.bold else cairo.FONT_WEIGHT_NORMAL)) cr.set_font_size(self.size) cr.show_glyphs(self.glyphs) # [(gid, originx, originy)] @@ -49,22 +54,31 @@ buffer = None def tracefn(ft, aType, index) : print aType + "(" + str(index) + "): " + str(buffer.get_info()) -usage = '''usage: %prog [options] fontfile "codepoints" +usage = '''usage: %prog [options] codepoints Generates output of glyphs and positions. Each entry is of the form: glyphid>cluster@(offsetx,offsety)+(advancex,advancey) codepoints is a space separated list of hex values of Unicode codepoints''' p = optparse.OptionParser(usage=usage) -p.add_option('-s', '--size', default=12, type="int", help="point size") +p.add_option('-s', '--size', default=32, type="int", help="point size") p.add_option('-l', '--lang', help="language code") p.add_option('-c', '--script', help="script code") -p.add_option('-f', '--feature', action='append', help="define a feature key=val") -p.add_option('-n', '--fontname', help='Font to use to render glyphs') +p.add_option('-F', '--feature', action='append', help="define a feature key=val") +p.add_option('-f', '--font', help='Font to use to render glyphs. My be a font file', default="verdana") +p.add_option('-b', '--bold', help='Choose bold fonts', action='store_true') +p.add_option('-i', '--italic', help='Choose italic fonts', action='store_true') p.add_option('-d', '--debug', action='store_true', help="Output trace info") (opts, args) = p.parse_args() -ft = harfbuzz.ft(args[0], opts.size) -text = "".join(unichr(int(c, 16)) for c in args[1].split(" ")) +fpat = opts.font + ":weight=" +fpat += "bold" if opts.bold else "medium" +fpat += "italic" if opts.italic else "roman" +pat = fcPattern(fpat) +fname = pat.getString("file", 0) +family = pat.getString("family", 0) +print "Processing: " + fname +ft = harfbuzz.ft(fname, opts.size) +text = "".join(unichr(int(c, 16)) for c in args) bytes = text.encode('utf_8') buffer = harfbuzz.buffer(bytes, len(text)) if (opts.lang or opts.script) : buffer.set_scriptlang(opts.script if opts.script else "", opts.lang if opts.lang else "") @@ -76,20 +90,20 @@ if opts.feature : ft.shape(buffer, features = features) res = buffer.get_info(64) # scale for 26.6 print res -if opts.fontname : - glyphs = [] - org = [0, 0] - for g in res : - glyphs.append((g.gid, org[0] + g.offset[0], org[1] + g.offset[1])) - org[0] += g.advance[0] - org[1] += g.advance[1] - gobject.type_register(GlyphsWindow) - win = gtk.Window() - win.resize(org[0] + 10, 3 * opts.size + 40) - win.connect('delete-event', gtk.main_quit) - frame = gtk.Frame("glyphs") - win.add(frame) - w = GlyphsWindow(opts.fontname, opts.size, glyphs) - frame.add(w) - win.show_all() - gtk.main() + +glyphs = [] +org = [0, 0] +for g in res : + glyphs.append((g.gid, org[0] + g.offset[0], org[1] + g.offset[1])) + org[0] += g.advance[0] + org[1] += g.advance[1] +gobject.type_register(GlyphsWindow) +win = gtk.Window() +win.resize(org[0] + 10, 3 * opts.size + 40) +win.connect('delete-event', gtk.main_quit) +frame = gtk.Frame("glyphs") +win.add(frame) +w = GlyphsWindow(family, opts.bold, opts.italic, opts.size, glyphs) +frame.add(w) +win.show_all() +gtk.main() diff --git a/contrib/python/setup.py b/contrib/python/setup.py index ef2412b48..f59272894 100755 --- a/contrib/python/setup.py +++ b/contrib/python/setup.py @@ -13,7 +13,8 @@ setup(name='harfbuzz', maintainer_email='martin_hosken@sil.org', packages=['harfbuzz'], ext_modules = [ - Extension("harfbuzz", ["lib/harfbuzz.pyx"], libraries=["harfbuzz"], library_dirs=["../../src/.libs"], include_dirs=["/usr/include/freetype2", "../../src"]) + Extension("harfbuzz", ["lib/harfbuzz.pyx"], libraries=["harfbuzz"], library_dirs=["../../src/.libs"], include_dirs=["/usr/include/freetype2", "../../src"]), + Extension("fontconfig", ["lib/fontconfig.pyx"], libraries=["fontconfig"]) ], cmdclass = {'build_ext' : build_ext}, scripts = glob('scripts/*'), From e5bed0a37fe1b0576d08435179e455cb28eadcdb Mon Sep 17 00:00:00 2001 From: Martin Hosken Date: Sat, 22 May 2010 20:19:00 +0100 Subject: [PATCH 4/5] Tidy up hbtestfont and add README --- contrib/python/README | 10 +++ contrib/python/scripts/hbtestfont | 134 +++++++++++++++--------------- 2 files changed, 77 insertions(+), 67 deletions(-) create mode 100644 contrib/python/README diff --git a/contrib/python/README b/contrib/python/README new file mode 100644 index 000000000..72a35279c --- /dev/null +++ b/contrib/python/README @@ -0,0 +1,10 @@ +This contains a wrapping of harfbuzz into python. The module is dependent on pyrex. To build, type: + +python setup.py build + +In addition there is a test application, hbtestfont. It has GTK based gui output and for this, python modules for gtk, gobject and cairo are needed. The application may be run without gui output using the --nogui option. + +Applications may be executed in the build context, without needing to install any modules or libraries, using the runpy script from the contrib/python directory. Thus one might type: + +./runpy script/hbtestfont -f "Charis SIL" 0048 0069 0303 + diff --git a/contrib/python/scripts/hbtestfont b/contrib/python/scripts/hbtestfont index 070e54939..e3a63f58e 100755 --- a/contrib/python/scripts/hbtestfont +++ b/contrib/python/scripts/hbtestfont @@ -3,57 +3,6 @@ import harfbuzz, optparse from fontconfig import fcPattern -try: - import gtk - import gobject - import cairo - from gtk import gdk -except : - raise SystemExit -import pygtk - -if gtk.pygtk_version < (2, 8) : - print "PyGtk 2.8 or later required" - raise SystemExit - -class GlyphsWindow (gtk.Widget) : - def __init__(self, fontname, bold, italic, size, glyphs) : - gtk.Widget.__init__(self) - self.fontname = fontname - self.size = size - self.glyphs = glyphs - self.bold = bold - self.italic = italic - - def do_realize(self) : - self.set_flags(gtk.REALIZED) - self.window = gdk.Window( - self.get_parent_window(), - width = self.allocation.width, - height = self.allocation.height, - window_type = gdk.WINDOW_CHILD, - wclass = gdk.INPUT_OUTPUT, - event_mask = self.get_events() | gdk.EXPOSURE_MASK) - self.window.set_user_data(self) - self.style.attach(self.window) - self.style.set_background(self.window, gtk.STATE_NORMAL) - self.window.move_resize(*self.allocation) - - def do_unrealize(self) : - self.window.destroy() - - def do_expose_event(self, event) : - cr = self.window.cairo_create() - cr.set_matrix(cairo.Matrix(1, 0, 0, 1, 0, 2 * self.size)) - cr.set_font_face(cairo.ToyFontFace(self.fontname, cairo.FONT_SLANT_ITALIC if self.italic else cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD if self.bold else cairo.FONT_WEIGHT_NORMAL)) - cr.set_font_size(self.size) - cr.show_glyphs(self.glyphs) # [(gid, originx, originy)] - -buffer = None - -def tracefn(ft, aType, index) : - print aType + "(" + str(index) + "): " + str(buffer.get_info()) - usage = '''usage: %prog [options] codepoints Generates output of glyphs and positions. Each entry is of the form: glyphid>cluster@(offsetx,offsety)+(advancex,advancey) @@ -68,15 +17,18 @@ p.add_option('-f', '--font', help='Font to use to render glyphs. My be a font fi p.add_option('-b', '--bold', help='Choose bold fonts', action='store_true') p.add_option('-i', '--italic', help='Choose italic fonts', action='store_true') p.add_option('-d', '--debug', action='store_true', help="Output trace info") +p.add_option('--nogui', action='store_true', help="Don't display a gui") (opts, args) = p.parse_args() fpat = opts.font + ":weight=" fpat += "bold" if opts.bold else "medium" +fpat += ":slant=" fpat += "italic" if opts.italic else "roman" pat = fcPattern(fpat) fname = pat.getString("file", 0) family = pat.getString("family", 0) print "Processing: " + fname + ft = harfbuzz.ft(fname, opts.size) text = "".join(unichr(int(c, 16)) for c in args) bytes = text.encode('utf_8') @@ -91,19 +43,67 @@ ft.shape(buffer, features = features) res = buffer.get_info(64) # scale for 26.6 print res -glyphs = [] -org = [0, 0] -for g in res : - glyphs.append((g.gid, org[0] + g.offset[0], org[1] + g.offset[1])) - org[0] += g.advance[0] - org[1] += g.advance[1] -gobject.type_register(GlyphsWindow) -win = gtk.Window() -win.resize(org[0] + 10, 3 * opts.size + 40) -win.connect('delete-event', gtk.main_quit) -frame = gtk.Frame("glyphs") -win.add(frame) -w = GlyphsWindow(family, opts.bold, opts.italic, opts.size, glyphs) -frame.add(w) -win.show_all() -gtk.main() +if not opts.nogui : + try: + import gtk + import gobject + import cairo + from gtk import gdk + except : + raise SystemExit + import pygtk + + if gtk.pygtk_version < (2, 8) : + print "PyGtk 2.8 or later required" + raise SystemExit + + class GlyphsWindow (gtk.Widget) : + def __init__(self, fontname, bold, italic, size, glyphs) : + gtk.Widget.__init__(self) + self.fontname = fontname + self.size = size + self.glyphs = glyphs + self.bold = bold + self.italic = italic + + def do_realize(self) : + self.set_flags(gtk.REALIZED) + self.window = gdk.Window( + self.get_parent_window(), + width = self.allocation.width, + height = self.allocation.height, + window_type = gdk.WINDOW_CHILD, + wclass = gdk.INPUT_OUTPUT, + event_mask = self.get_events() | gdk.EXPOSURE_MASK) + self.window.set_user_data(self) + self.style.attach(self.window) + self.style.set_background(self.window, gtk.STATE_NORMAL) + self.window.move_resize(*self.allocation) + + def do_unrealize(self) : + self.window.destroy() + + def do_expose_event(self, event) : + cr = self.window.cairo_create() + cr.set_matrix(cairo.Matrix(1, 0, 0, 1, 0, 1.5 * self.size)) + cr.set_font_face(cairo.ToyFontFace(self.fontname, cairo.FONT_SLANT_ITALIC if self.italic else cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD if self.bold else cairo.FONT_WEIGHT_NORMAL)) + cr.set_font_size(self.size) + cr.show_glyphs(self.glyphs) # [(gid, originx, originy)] + + glyphs = [] + org = [0, 0] + for g in res : + glyphs.append((g.gid, org[0] + g.offset[0], org[1] + g.offset[1])) + org[0] += g.advance[0] + org[1] += g.advance[1] + + gobject.type_register(GlyphsWindow) + win = gtk.Window() + win.resize(org[0] + 10, 2 * opts.size + 40) + win.connect('delete-event', gtk.main_quit) + frame = gtk.Frame("glyphs") + win.add(frame) + w = GlyphsWindow(family, opts.bold, opts.italic, opts.size, glyphs) + frame.add(w) + win.show_all() + gtk.main() From 3109a86add936ae4cc77541fc026c4fe2db4e328 Mon Sep 17 00:00:00 2001 From: Martin Hosken Date: Mon, 24 May 2010 13:25:37 +0100 Subject: [PATCH 5/5] hb-graphite now no longer has -ve advances within clusters. Fix infinite loop in tag_to_script(). python fixed to use tag_to_script and allow hbtestfont to be passed font files, where fontconfig knows about them. --- contrib/python/lib/harfbuzz.pyx | 4 ++-- contrib/python/scripts/hbtestfont | 6 +++++- src/hb-graphite.cc | 4 ++-- src/hb-ot-tag.c | 1 + 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/contrib/python/lib/harfbuzz.pyx b/contrib/python/lib/harfbuzz.pyx index 139d3eac5..4d71ead83 100644 --- a/contrib/python/lib/harfbuzz.pyx +++ b/contrib/python/lib/harfbuzz.pyx @@ -41,7 +41,7 @@ cdef extern from "hb-language.h" : char * hb_language_to_string(hb_language_t language) cdef extern from "hb-ot-tag.h" : - hb_script_t hb_ot_tag_to_script (char *sname) + hb_script_t hb_ot_tag_to_script (hb_tag_t tag) cdef extern from "hb-buffer.h" : ctypedef struct hb_buffer_t : @@ -141,7 +141,7 @@ cdef class buffer : cdef hb_script_t scriptnum language = hb_language_from_string(lang) - scriptnum = hb_ot_string_to_script(script) + scriptnum = hb_ot_tag_to_script(hb_tag_from_string(script)) hb_buffer_set_script(self.buffer, scriptnum) hb_buffer_set_language(self.buffer, language) diff --git a/contrib/python/scripts/hbtestfont b/contrib/python/scripts/hbtestfont index e3a63f58e..7790fd3ca 100755 --- a/contrib/python/scripts/hbtestfont +++ b/contrib/python/scripts/hbtestfont @@ -20,7 +20,11 @@ p.add_option('-d', '--debug', action='store_true', help="Output trace info") p.add_option('--nogui', action='store_true', help="Don't display a gui") (opts, args) = p.parse_args() -fpat = opts.font + ":weight=" +if opts.font.lower().endswith(".ttf") : + fpat = ":file=" +else : + fpat = "" +fpat += opts.font + ":weight=" fpat += "bold" if opts.bold else "medium" fpat += ":slant=" fpat += "italic" if opts.italic else "roman" diff --git a/src/hb-graphite.cc b/src/hb-graphite.cc index 708757c7f..54a01fc6c 100644 --- a/src/hb-graphite.cc +++ b/src/hb-graphite.cc @@ -291,8 +291,8 @@ hb_graphite_shape (hb_font_t *font, pPosition->y_offset = iGlyph->yOffset() - curradvy; pPosition->x_advance = pPosition->x_offset + iGlyph->advanceWidth(); pPosition->y_advance = pPosition->y_offset + iGlyph->advanceHeight(); -// if (pPosition->x_advance < 0) -// pPosition->x_advance = 0; + if (pPosition->x_advance < 0 && iGlyph->logicalIndex() != iGlyph->attachedClusterBase()->logicalIndex()) + pPosition->x_advance = 0; curradvx += pPosition->x_advance; curradvy += pPosition->y_advance; // fprintf(stderr, "%d@(%f, %f)+(%f, %f)\n", iGlyph->glyphID(), iGlyph->origin(), iGlyph->yOffset(), iGlyph->advanceWidth(), iGlyph->advanceHeight()); diff --git a/src/hb-ot-tag.c b/src/hb-ot-tag.c index d1d42f562..12d22183c 100644 --- a/src/hb-ot-tag.c +++ b/src/hb-ot-tag.c @@ -161,6 +161,7 @@ hb_ot_tag_to_script (hb_tag_t tag) while (*p) if (tag == *p) return i; + p++; } return HB_SCRIPT_UNKNOWN;