The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Text.vala in libsvgbird

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libsvgbird/Text.vala.
Text extent
1 /* 2 Copyright (C) 2016 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 using Math; 17 18 [SimpleType] 19 [CCode (has_type_id = false)] 20 public extern struct FcConfig { 21 } 22 23 [SimpleType] 24 [CCode (has_type_id = false)] 25 extern struct svg_bird_font_item { 26 } 27 28 extern void svg_bird_draw_text (Context cr, svg_bird_font_item* font, string text); 29 extern void svg_bird_get_extent (svg_bird_font_item* font, string text, out double w, out double h); 30 31 extern void svg_bird_font_item_delete (svg_bird_font_item* item); 32 extern svg_bird_font_item* svg_bird_font_item_create (string font_file, int font_size); 33 34 extern bool svg_bird_has_font_config (); 35 extern void svg_bird_set_font_config (FcConfig* f); 36 37 [CCode (cname = "FcInitLoadConfigAndFonts")] 38 public extern FcConfig* FcInitLoadConfigAndFonts (); 39 40 [CCode (cname = "FcConfigAppFontAddDir")] 41 public extern string* FcConfigAppFontAddDir (FcConfig* config, string path); 42 43 [CCode (cname = "FcConfigSetSysRoot")] 44 public extern void FcConfigSetSysRoot (FcConfig* config, string path); 45 46 [CCode (cname = "FcConfigParseAndLoad")] 47 public extern bool FcConfigParseAndLoad (FcConfig* config, string path, bool complain); 48 49 [CCode (cname = "FcConfigSetCurrent")] 50 public extern void FcConfigSetCurrent (FcConfig* config); 51 52 [CCode (cname = "FcConfigCreate")] 53 public extern FcConfig* FcConfigCreate (); 54 55 [CCode (cname = "FcConfigFilename")] 56 public extern string FcConfigFilename (string path); 57 58 59 namespace SvgBird { 60 61 public class Text : Object { 62 string font_family = ""; 63 int font_size = 12; 64 string content; 65 66 public double x = 0; 67 public double y = 0; 68 69 svg_bird_font_item* font = null; 70 71 public Text () { 72 if (!svg_bird_has_font_config ()) { 73 init_font_config (); 74 } 75 76 set_font ("Roboto"); 77 } 78 79 ~Text () { 80 svg_bird_font_item_delete (font); 81 } 82 83 public void get_text_extents (out double w, out double h) { 84 svg_bird_get_extent (font, content, out w, out h); 85 } 86 87 public override bool update_boundaries (Context context) { 88 double w; 89 double h; 90 get_text_extents (out w, out h); 91 left = x; 92 top = y; 93 bottom = y + h; 94 right = x + w; 95 return true; 96 } 97 98 public void set_font_size (int s) { 99 font_size = s; 100 set_font (font_family); 101 } 102 103 public void set_font (string font_family) { 104 if (font != null) { 105 svg_bird_font_item_delete (font); 106 } 107 108 font = svg_bird_font_item_create (font_family, font_size); 109 } 110 111 public void init_font_config () { 112 FcConfig* config; 113 114 #if MAC 115 config = FcConfigCreate(); 116 117 string bundle = (!) BirdFont.get_settings_directory ().get_path (); 118 FcConfigSetSysRoot(config, bundle); 119 120 string path = FcConfigFilename((!) SearchPaths.search_file(null, "fontconfig.settings").get_path ()); 121 bool loaded = FcConfigParseAndLoad(config, path, true); 122 123 if (!loaded) { 124 warning ("Fontconfig initialization failed."); 125 } 126 127 FcConfigSetCurrent (config); 128 #else 129 config = FcInitLoadConfigAndFonts (); 130 #endif 131 svg_bird_set_font_config (config); 132 } 133 134 public void set_text (string t) { 135 content = t.replace ("\n", " "); 136 content = content.replace ("\t", " "); 137 138 while (content.index_of (" ") > -1) { 139 content = content.replace (" ", " "); 140 } 141 142 content = content.strip (); 143 } 144 145 public Text.create_copy (Text c) { 146 Object.copy_attributes (c, this); 147 font_family = c.font_family; 148 x = c.x; 149 y = c.y; 150 font_size = c.font_size; 151 } 152 153 public override void draw_outline (Context cr) { 154 cr.save (); 155 cr.translate (x, y); 156 157 double w, h; 158 svg_bird_get_extent (font, content, out w, out h); 159 160 if (font != null) { 161 svg_bird_draw_text (cr, font, content); 162 } 163 164 cr.restore (); 165 } 166 167 public override bool is_empty () { 168 return false; 169 } 170 171 public override Object copy () { 172 return new Text.create_copy (this); 173 } 174 175 public override string to_string () { 176 return "Text"; 177 } 178 } 179 180 } 181