Commit ffbc46b
Set font size
Changed files (4)
modifiedlibbirdfont/DrawingTools.valaaddedlibbirdfont/Renderer/Text.valadeletedlibbirdfont/Renderer/TextArea.valamodifiedlibbirdfont/Renderer/TextTab.vala
Diff
@@ -785,7 +785,7 @@ public class DrawingTools : ToolCollection { if (BirdFont.has_argument ("--test")) { Tool text_rendering = new Tool ("text_rendering"); text_rendering.select_action.connect((self) => {- TextArea.test ();+ Text.test (); }); test_tools.add_tool (text_rendering); new file mode 100644@@ -0,0 +1,213 @@+/*+ Copyright (C) 2014 Johan Mattsson++ This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version.++ This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details.+*/++using Cairo;++namespace BirdFont {++/** Test implementation of a birdfont rendering engine. */+public class Text {+ + FontCache font_cache;+ Font font;+ string text;+ GlyphSequence glyph_sequence;+ double line_gap = 20;+ public delegate void Iterator (Glyph glyph, double kerning);+ + public Text () {+ font = new Font ();+ text = "";+ glyph_sequence = new GlyphSequence ();+ font_cache = FontCache.get_default_cache ();+ }++ public void set_font_cache (FontCache font_cache) {+ this.font_cache = font_cache;+ }+ + public void set_text (string text) { + int index;+ unichar c;+ string name;+ Glyph? g;+ + this.text = text;+ glyph_sequence = new GlyphSequence ();+ + index = 0;+ while (text.get_next_char (ref index, out c)) {+ name = font.get_name_for_character (c);+ g = font.get_glyph_by_name (name);+ glyph_sequence.glyph.add (g);+ } + }++ public void iterate (Iterator iter) {+ Glyph glyph;+ double w, kern;+ int wi;+ Glyph? prev;+ GlyphSequence word_with_ligatures;+ GlyphRange? gr_left, gr_right;+ double row_height;+ GlyphSequence word;+ + row_height = get_row_height ();+ + glyph = new Glyph ("", '\0');++ w = 0;+ prev = null;+ kern = 0;+ + word = glyph_sequence;+ wi = 0;+ word_with_ligatures = word.process_ligatures ();+ gr_left = null;+ gr_right = null;+ foreach (Glyph? g in word_with_ligatures.glyph) {+ if (prev == null || wi == 0) {+ kern = 0;+ } else {+ return_if_fail (wi < word_with_ligatures.ranges.size);+ return_if_fail (wi - 1 >= 0);+ + gr_left = word_with_ligatures.ranges.get (wi - 1);+ gr_right = word_with_ligatures.ranges.get (wi);++ kern = font.get_kerning_classes ().get_kerning_for_pair (((!)prev).get_name (), ((!)g).get_name (), gr_left, gr_right);+ }+ + // process glyph+ glyph = (g == null) ? font.get_not_def_character ().get_current () : (!) g;+ iter (glyph, kern);+ + prev = g;+ wi++;+ }+ }++ public double get_max_extent_x (double font_size_in_pixels) {+ double x = 0;+ double ratio = font_size_in_pixels / get_row_height ();+ + iterate ((glyph, kerning) => {+ x += (glyph.get_width () + kerning) * ratio;+ });+ + return x;+ }+ + public bool load_font (string file) {+ Font? f = font_cache.get_font (file);+ + if (f != null) {+ font = (!) f;+ }+ + return f != null;+ }+ + public void draw (Context cr, int px, int py, int width, int height, double font_size_in_pixels) {+ double x, y;+ double row_height, ratio;++ row_height = get_row_height (); + ratio = font_size_in_pixels / row_height;+ + cr.save ();++ y = get_row_height () + font.base_line + py;+ x = px;+ + iterate ((glyph, kerning) => {+ double lsb = glyph.left_limit;+ + glyph.add_help_lines ();+ + x += kerning * ratio;+ cr.save ();+ cr.new_path ();+ foreach (Path path in glyph.path_list) {+ draw_path (cr, path, lsb, x, y, ratio);+ }+ cr.fill ();+ cr.restore ();+ + x += glyph.get_width () * ratio;+ });+ + cr.set_source_rgba (0, 0, 0, 1);+ cr.fill ();++ cr.restore (); + } ++ void draw_path (Context cr, Path path, double lsb, double x, double y, double scale) {+ EditPoint e, prev;+ double xa, ya, xb, yb, xc, yc, xd, yd;+ + if (path.points.size > 0) {++ prev = path.points.get (0);+ xa = (prev.x - lsb) * scale + x;+ ya = y - prev.y * scale;+ cr.move_to (xa, ya);+ + for (int i = 1; i < path.points.size; i++) {+ e = path.points.get (i).copy ();+ PenTool.convert_point_segment_type (prev, e, PointType.CUBIC);+ + xb = (prev.get_right_handle ().x - lsb) * scale + x;+ yb = y - prev.get_right_handle ().y * scale;++ xc = (e.get_left_handle ().x - lsb) * scale + x;+ yc = y - e.get_left_handle ().y * scale;+ + xd = (e.x - lsb) * scale + x;+ yd = y - e.y * scale;+ + cr.curve_to (xb, yb, xc, yc, xd, yd);+ cr.line_to (xd, yd);+ + prev = e;+ }+ + // close path+ e = path.points.get (0);+ + xb = (prev.get_right_handle ().x - lsb) * scale + x;+ yb = y - prev.get_right_handle ().y * scale;++ xc = (e.get_left_handle ().x - lsb) * scale + x;+ yc = y - e.get_left_handle ().y * scale;+ + xd = (e.x - lsb) * scale + x;+ yd = y - e.y * scale;+ + cr.curve_to (xb, yb, xc, yc, xd, yd);+ }+ }++ double get_row_height () {+ return font.top_limit - font.bottom_limit;+ }+ + internal static void test () {+ MainWindow.get_tab_bar ().add_tab (new TextTab ());+ }+}++}deleted file mode 100644@@ -1,211 +0,0 @@-/*- Copyright (C) 2014 Johan Mattsson-- This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 3 of the - License, or (at your option) any later version.-- This library is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details.-*/--using Cairo;--namespace BirdFont {--/** Test implementation of a birdfont rendering engine. */-public class TextArea {- - FontCache font_cache;- Font font;- string text;- GlyphSequence glyph_sequence;- double line_gap = 20;- public delegate void Iterator (Glyph glyph, double kerning);- - public TextArea () {- font = new Font ();- text = "";- glyph_sequence = new GlyphSequence ();- font_cache = FontCache.get_default_cache ();- }-- public void set_font_cache (FontCache font_cache) {- this.font_cache = font_cache;- }- - public void set_text (string text) { - int index;- unichar c;- string name;- Glyph? g;- - this.text = text;- glyph_sequence = new GlyphSequence ();- - index = 0;- while (text.get_next_char (ref index, out c)) {- name = font.get_name_for_character (c);- g = font.get_glyph_by_name (name);- glyph_sequence.glyph.add (g);- } - }-- public void iterate (Iterator iter) {- Glyph glyph;- double w, kern;- int wi;- Glyph? prev;- GlyphSequence word_with_ligatures;- GlyphRange? gr_left, gr_right;- double row_height;- GlyphSequence word;- - row_height = get_row_height ();- - glyph = new Glyph ("", '\0');-- w = 0;- prev = null;- kern = 0;- - word = glyph_sequence;- wi = 0;- word_with_ligatures = word.process_ligatures ();- gr_left = null;- gr_right = null;- foreach (Glyph? g in word_with_ligatures.glyph) {- if (prev == null || wi == 0) {- kern = 0;- } else {- return_if_fail (wi < word_with_ligatures.ranges.size);- return_if_fail (wi - 1 >= 0);- - gr_left = word_with_ligatures.ranges.get (wi - 1);- gr_right = word_with_ligatures.ranges.get (wi);-- kern = font.get_kerning_classes ().get_kerning_for_pair (((!)prev).get_name (), ((!)g).get_name (), gr_left, gr_right);- }- - // process glyph- glyph = (g == null) ? font.get_not_def_character ().get_current () : (!) g;- iter (glyph, kern);- - prev = g;- wi++;- }- }-- public double get_max_extent_x (double font_size_in_pixels) {- double x = 0;- - iterate ((glyph, kerning) => {- x += glyph.get_width () + kerning;- });- - return x;- }- - public bool load_font (string file) {- Font? f = font_cache.get_font (file);- - if (f != null) {- font = (!) f;- }- - return f != null;- }- - public void draw (Context cr, int px, int py, int width, int height, double font_size_in_pixels) {- double x, y;- double row_height, ratio;-- row_height = get_row_height (); - ratio = font_size_in_pixels / row_height;- - cr.save ();- //cr.scale (ratio, ratio);-- y = get_row_height () + font.base_line + py;- x = px;- - iterate ((glyph, kerning) => {- double lsb = glyph.left_limit;- - glyph.add_help_lines ();- - x += kerning;- cr.save ();- cr.new_path ();- foreach (Path path in glyph.path_list) {- draw_path (cr, path, lsb, x, y);- }- cr.fill ();- cr.restore ();- - x += glyph.get_width ();- });- - cr.set_source_rgba (0, 0, 0, 1);- cr.fill ();-- cr.restore (); - } -- void draw_path (Context cr, Path path, double lsb, double x, double y) {- EditPoint e, prev;- double xb, yb, xc, yc, xd, yd;- - if (path.points.size > 0) {-- prev = path.points.get (0);- cr.move_to (prev.x - lsb + x, y - prev.y);- - for (int i = 1; i < path.points.size; i++) {- e = path.points.get (i).copy ();- PenTool.convert_point_segment_type (prev, e, PointType.CUBIC);- - xb = prev.get_right_handle ().x - lsb + x;- yb = y - prev.get_right_handle ().y;-- xc = e.get_left_handle ().x - lsb + x;- yc = y - e.get_left_handle ().y;- - xd = e.x - lsb + x;- yd = y - e.y;- - cr.curve_to (xb, yb, xc, yc, xd, yd);- cr.line_to (xd, yd);- - prev = e;- }- - // close path- e = path.points.get (0);- - xb = prev.get_right_handle ().x - lsb + x;- yb = y - prev.get_right_handle ().y;-- xc = e.get_left_handle ().x - lsb + x;- yc = y - e.get_left_handle ().y;- - xd = e.x - lsb + x;- yd = y - e.y;- - cr.curve_to (xb, yb, xc, yc, xd, yd);- }- }-- double get_row_height () {- return font.top_limit - font.bottom_limit;- }- - internal static void test () {- MainWindow.get_tab_bar ().add_tab (new TextTab ());- }-}--}@@ -19,20 +19,20 @@ namespace BirdFont { /** Testing class for an experimental implementation of a birdfont rendering engine. */ public class TextTab : FontDisplay { - TextArea text_area;- TextArea text_area2;- TextArea text_area3;+ Text text_area;+ Text text_area2;+ Text text_area3; public TextTab () { - text_area = new TextArea ();+ text_area = new Text (); text_area.load_font ("testfont.bf"); text_area.set_text ("Test"); - text_area2 = new TextArea ();+ text_area2 = new Text (); text_area2.load_font ("testfont.bf"); text_area2.set_text ("Birdfont ÅÄÖ"); - text_area3 = new TextArea ();+ text_area3 = new Text (); text_area3.load_font ("testfont.bf"); text_area3.set_text ("TEST 3"); }