From: Jan Nieuwenhuizen Date: Mon, 13 Dec 2004 17:19:29 +0000 (+0000) Subject: * lily/include/font-metric.hh (index_to_charcode): New function. X-Git-Tag: release/2.5.14~427 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=03a1272a1af4413013a74301870a13369ec340f7;p=lilypond.git * lily/include/font-metric.hh (index_to_charcode): New function. * lily/font-metric.cc (ly:font-glyph-name-to-charcode): Use it in new function. (ly:font-glyp-to-index): Remove. * scm/lily-library.scm (char->unicode-index): Remove. --- diff --git a/ChangeLog b/ChangeLog index 32af2a6bca..3e8e5c7b70 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2004-12-13 Jan Nieuwenhuizen + + * lily/include/font-metric.hh (index_to_charcode): New function. + + * lily/font-metric.cc (ly:font-glyph-name-to-charcode): Use it in + new function. + (ly:font-glyp-to-index): Remove. + + * scm/lily-library.scm (char->unicode-index): Remove. + 2004-12-12 Han-Wen Nienhuys * lily/open-type-font.cc (make_index_to_charcode_map): new method. diff --git a/lily/font-metric.cc b/lily/font-metric.cc index be4a3a23b6..be0d75343e 100644 --- a/lily/font-metric.cc +++ b/lily/font-metric.cc @@ -161,7 +161,7 @@ LY_DEFINE (ly_get_glyph, "ly:get-glyph", return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy (); } -LY_DEFINE (ly_font_get_glyph_index, "ly:font-get-glyph-index", +LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index", 2, 0, 0, (SCM font, SCM name), "Return the index for @{name} in @var{font}.") @@ -173,6 +173,30 @@ LY_DEFINE (ly_font_get_glyph_index, "ly:font-get-glyph-index", return scm_from_int (fm->name_to_index (ly_scm2string (name))); } +LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode", + 2, 0, 0, + (SCM font, SCM index), + "Return the character code for @var{index} @var{font}.") +{ + Font_metric *fm = unsmob_metrics (font); + SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric"); + SCM_ASSERT_TYPE (scm_is_integer (index), index, SCM_ARG2, __FUNCTION__, "index"); + + return scm_from_unsigned_integer (fm->index_to_charcode (ly_scm2int (index))); +} + +LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode", + 2, 0, 0, + (SCM font, SCM name), + "Return the character code for glyph @{name} in @var{font}.") +{ + Font_metric *fm = unsmob_metrics (font); + SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric"); + SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string"); + + return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name)))); +} + LY_DEFINE (ly_text_dimension,"ly:text-dimension", 2, 0, 0, (SCM font, SCM text), @@ -256,11 +280,16 @@ Font_metric::index_to_ascii (int i) const return i; } +unsigned +Font_metric::index_to_charcode (int i) const +{ + return (unsigned) index_to_ascii (i); +} + Stencil Font_metric::get_ascii_char_stencil (int code) const { - SCM at = scm_list_3 (ly_symbol2scm ("char"), - this->self_scm (), + SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (), scm_int2num (code)); Box b = get_ascii_char (code); return Stencil (b, at); diff --git a/lily/include/font-metric.hh b/lily/include/font-metric.hh index f9fe247f3d..ad56d8f931 100644 --- a/lily/include/font-metric.hh +++ b/lily/include/font-metric.hh @@ -26,6 +26,7 @@ public: virtual Box get_indexed_char (int index) const; virtual Box get_ascii_char (int ascii) const; virtual int name_to_index (String) const; + virtual unsigned index_to_charcode (int) const; virtual int index_to_ascii (int) const; virtual Real design_size () const; virtual Stencil find_by_name (String) const; diff --git a/lily/include/guile-compatibility.hh b/lily/include/guile-compatibility.hh index 161ba4e6e4..6d3046570a 100644 --- a/lily/include/guile-compatibility.hh +++ b/lily/include/guile-compatibility.hh @@ -38,6 +38,7 @@ inline SCM scm_cadar (SCM x) { return SCM_CADAR (x); } #define SCM_VECTOR_REF(v, i) (SCM_VELTS ((v))[(i)]) #define scm_from_bool(x) (x ? SCM_BOOL_T : SCM_BOOL_F) #define scm_from_int(x) SCM_MAKINUM (x) +#define scm_from_unsigned_integer(x) scm_uint2num (x) #define scm_is_integer(x) SCM_INUMP (x) #define scm_is_string(x) SCM_STRINGP (x) #define scm_hash_table_p scm_vector_p diff --git a/lily/include/open-type-font.hh b/lily/include/open-type-font.hh index e1260eafc7..5d041d307b 100644 --- a/lily/include/open-type-font.hh +++ b/lily/include/open-type-font.hh @@ -30,6 +30,7 @@ public: virtual Offset attachment_point (String) const; virtual Box get_indexed_char (int) const; virtual int name_to_index (String) const; + virtual unsigned index_to_charcode (int) const; virtual void derived_mark () const; #if 0 virtual int count () const; diff --git a/lily/open-type-font.cc b/lily/open-type-font.cc index 5a9085ef4e..2b474e1114 100644 --- a/lily/open-type-font.cc +++ b/lily/open-type-font.cc @@ -11,25 +11,25 @@ #include #include - + #include "warn.hh" #include "open-type-font.hh" #include "dimensions.hh" -const Real point_constant = 1 PT; +const Real point_constant = 1 PT; FT_Byte * load_table (char const *tag_str, FT_Face face, FT_ULong *length) { FT_ULong tag = FT_MAKE_TAG(tag_str[0], tag_str[1], tag_str[2], tag_str[3]); - + int error_code = FT_Load_Sfnt_Table (face, tag, 0, NULL, length); if (!error_code) { - FT_Byte*buffer = (FT_Byte*) malloc (*length); + FT_Byte *buffer = (FT_Byte*) malloc (*length); if (buffer == NULL) - error ("Not enough memory"); + error (_f ("Cannot allocate %d bytes", *length)); error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length ); if (error_code) @@ -39,15 +39,15 @@ load_table (char const *tag_str, FT_Face face, FT_ULong *length) return buffer; } - - return 0 ; + + return 0; } SCM load_scheme_table (char const *tag_str, FT_Face face) { FT_ULong length = 0; - FT_Byte* buffer = load_table (tag_str, face, &length); + FT_Byte *buffer = load_table (tag_str, face, &length); SCM tab = SCM_EOL; if (buffer) @@ -55,63 +55,67 @@ load_scheme_table (char const *tag_str, FT_Face face) String contents ((Byte const*)buffer, length); contents = "(quote (" + contents + "))"; - SCM alist = scm_c_eval_string (contents.to_str0()); + SCM alist = scm_c_eval_string (contents.to_str0 ()); tab = alist_to_hashq (alist); free (buffer); } return tab; } - + Index_to_charcode_map make_index_to_charcode_map (FT_Face face) { Index_to_charcode_map m; - FT_ULong charcode; - FT_UInt gindex; - - charcode = FT_Get_First_Char( face, &gindex ); - while ( gindex != 0 ) + FT_ULong charcode; + FT_UInt gindex; + + charcode = FT_Get_First_Char (face, &gindex); + while (gindex != 0) { + printf ("index -> code: %d %d \n", gindex, charcode); m[gindex] = charcode; - charcode = FT_Get_Next_Char( face, charcode, &gindex ); + charcode = FT_Get_Next_Char (face, charcode, &gindex); } return m; -} +} + +Open_type_font::Open_type_font (FT_Face face) +{ + face_ = face; + lily_character_table_ = SCM_EOL; + lily_global_table_ = SCM_EOL; + + lily_character_table_ = load_scheme_table ("LILC", face_); + lily_global_table_ = load_scheme_table ("LILY", face_); + index_to_charcode_map_ = make_index_to_charcode_map (face_); +} + +Open_type_font::~Open_type_font() +{ + FT_Done_Face (face_); +} SCM Open_type_font::make_otf (String str) { FT_Face face; - int error_code = FT_New_Face(freetype2_library, str.to_str0(), - 0, &face); - + int error_code = FT_New_Face(freetype2_library, str.to_str0 (), 0, &face); + if (error_code == FT_Err_Unknown_File_Format) { - error("Unsupported font format"); + error (_f ("Unsupported font format: %s", str.to_str0 ())); } else if (error_code) { - error ("Unknown error reading font file."); + error (_f ("Unknown error: %d reading font file: %s", error_code, + str.to_str0 ())); } + Open_type_font *otf = new Open_type_font (face); - Open_type_font * otf = new Open_type_font (face); - - return otf->self_scm (); } -Open_type_font::Open_type_font(FT_Face face) -{ - face_ = face; - lily_character_table_ = SCM_EOL; - lily_global_table_ = SCM_EOL; - - lily_character_table_ = load_scheme_table ("LILC", face_); - lily_global_table_ = load_scheme_table ("LILY", face_); - index_to_charcode_map_ = make_index_to_charcode_map (face_); -} - void Open_type_font::derived_mark () const { @@ -126,23 +130,16 @@ Open_type_font::attachment_point (String glyph_name) const SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F); Offset o; - if (entry == SCM_BOOL_F) + if (entry == SCM_BOOL_F) return o; SCM char_alist = entry; SCM att_scm =scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist)); - - return ly_scm2offset (att_scm); -} - -Open_type_font::~Open_type_font() -{ - FT_Done_Face (face_); + return ly_scm2offset (att_scm); } - Box Open_type_font::get_indexed_char (int signed_idx) const { @@ -157,27 +154,29 @@ Open_type_font::get_indexed_char (int signed_idx) const Box b (Interval (-hb, m.width - hb), Interval (-vb, m.height - vb)); - - - b.scale (design_size() * Real (point_constant) / face_->units_per_EM); + b.scale (design_size () * Real (point_constant) / face_->units_per_EM); return b; } int Open_type_font::name_to_index (String nm) const { - char * nm_str = (char * )nm.to_str0 (); - int idx = FT_Get_Name_Index (face_, nm_str); - - if (idx == 0) - return -1; - else + char *nm_str = (char*) nm.to_str0 (); + if (int idx = FT_Get_Name_Index (face_, nm_str)) return idx; + return -1; } +unsigned +Open_type_font::index_to_charcode (int i) const +{ + return ((Open_type_font*) this)->index_to_charcode_map_[index_to_ascii (i)]; +} Real Open_type_font::design_size () const { - return point_constant * scm_to_double (scm_hashq_ref (lily_global_table_, ly_symbol2scm ("staffsize"), SCM_BOOL_F)); + return point_constant + * scm_to_double (scm_hashq_ref (lily_global_table_, + ly_symbol2scm ("staffsize"), SCM_BOOL_F)); } diff --git a/scm/lily-library.scm b/scm/lily-library.scm index 29e3f476fc..287346d322 100644 --- a/scm/lily-library.scm +++ b/scm/lily-library.scm @@ -343,23 +343,5 @@ possibly turned off." (ly:font-file-name font))))) (define-public (char->unicode-index font char) - ;; (format (current-error-port) "UNICODE:~S:~S:~S\n" - ;; font (ly:font-encoding font) (char->integer char)) - ;; (force-output (current-error-port)) - (+ (case (ly:font-encoding font) - ((fetaMusic) (- #xe000 #x20)) - ((fetaBraces) (- #xe000 #x40)) - ((fetaBraces) (- #xe000 #x40)) - ;;(else 0)) - ;; FIXME: bigcheese says FontSpecific - (else (if (string=? (font-family font) "bigcheese20") - ;;#xf000 0))) - ;; FIXME: hmm, why does name_to_index not return actual - ;; unicode mapping? - - ;; ugh, we must know which font from bigcheese; - ;; feta-proper starts at 0xefc - ;; but we cannot display feta-nummer or feta-din characters - ;; this way - #xe0fc 0))) - (char->integer char))) + ;;(ly:font-index-to-charcode (char->integer char)) + (char->integer char)) diff --git a/scm/output-gnome.scm b/scm/output-gnome.scm index 217640a0da..acced8dd83 100644 --- a/scm/output-gnome.scm +++ b/scm/output-gnome.scm @@ -323,9 +323,9 @@ lilypond -fgnome input/simple-song.ly (define (named-glyph font name) (debugf "glyph:~S\n" name) - (debugf "index:~S\n" (ly:font-get-glyph-index font name)) + (debugf "index:~S\n" (ly:font-glyph-name-to-charcode font name)) (debugf "font:~S\n" (font-family font)) - (text font (integer->char (ly:font-get-glyph-index font name)))) + (text font (integer->char (ly:font-glyph-name-to-charcode font name)))) (define (polygon coords blotdiameter) (let* @@ -360,11 +360,12 @@ lilypond -fgnome input/simple-song.ly (define (text font s) (define (pango-font-name font) + (stderr "FONT-NAME:~S\n" (ly:font-name font)) + (let ((family (font-family font))) ;; Hmm, family is bigcheese20? (if (string=? family "bigcheese20") - (begin (debugf "BIGCHEESE\n") - "LilyPond, 20") + (format #f "~S, ~S" (ly:font-name font) (ly:font-design-size font)) family))) (define (pango-font-size font) @@ -404,4 +405,4 @@ lilypond -fgnome input/simple-song.ly #:size-set #t #:text (if (char? s) (char->utf8-string font s) - (string->utf8-string font s))))) \ No newline at end of file + (string->utf8-string font s)))))