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 30 extern void svg_bird_font_item_delete (svg_bird_font_item* item); 31 extern svg_bird_font_item* svg_bird_font_item_create (string font_file, int font_size); 32 33 extern bool svg_bird_has_font_config (); 34 extern void svg_bird_set_font_config (FcConfig* f); 35 36 [CCode (cname = "FcInitLoadConfigAndFonts")] 37 public extern FcConfig* FcInitLoadConfigAndFonts (); 38 39 [CCode (cname = "FcConfigAppFontAddDir")] 40 public extern string* FcConfigAppFontAddDir (FcConfig* config, string path); 41 42 [CCode (cname = "FcConfigSetSysRoot")] 43 public extern void FcConfigSetSysRoot (FcConfig* config, string path); 44 45 [CCode (cname = "FcConfigParseAndLoad")] 46 public extern bool FcConfigParseAndLoad (FcConfig* config, string path, bool complain); 47 48 [CCode (cname = "FcConfigSetCurrent")] 49 public extern void FcConfigSetCurrent (FcConfig* config); 50 51 [CCode (cname = "FcConfigCreate")] 52 public extern FcConfig* FcConfigCreate (); 53 54 [CCode (cname = "FcConfigFilename")] 55 public extern string FcConfigFilename (string path); 56 57 58 namespace SvgBird { 59 60 public class Text : Object { 61 string font_family = ""; 62 int font_size = 12; 63 string content; 64 65 public double x = 0; 66 public double y = 0; 67 68 svg_bird_font_item* font = null; 69 70 public Text () { 71 if (!svg_bird_has_font_config ()) { 72 init_font_config (); 73 } 74 75 set_font ("Roboto"); 76 } 77 78 ~Text () { 79 svg_bird_font_item_delete (font); 80 } 81 82 public void set_font_size (int s) { 83 font_size = s; 84 set_font (font_family); 85 } 86 87 public void set_font (string font_family) { 88 if (font != null) { 89 svg_bird_font_item_delete (font); 90 } 91 92 font = svg_bird_font_item_create (font_family, font_size); 93 94 if (font == null) { 95 font = svg_bird_font_item_create ("sans-serif", font_size); 96 } 97 } 98 99 public void init_font_config () { 100 FcConfig* config; 101 102 #if MAC 103 config = FcConfigCreate(); 104 105 string bundle = (!) BirdFont.get_settings_directory ().get_path (); 106 FcConfigSetSysRoot(config, bundle); 107 108 string path = FcConfigFilename((!) SearchPaths.search_file(null, "fontconfig.settings").get_path ()); 109 bool loaded = FcConfigParseAndLoad(config, path, true); 110 111 if (!loaded) { 112 warning ("Fontconfig initialization failed."); 113 } 114 115 FcConfigSetCurrent (config); 116 #else 117 config = FcInitLoadConfigAndFonts (); 118 #endif 119 svg_bird_set_font_config (config); 120 } 121 122 public void set_text (string t) { 123 content = t.replace ("\n", " "); 124 content = content.replace ("\t", " "); 125 126 while (content.index_of (" ") > -1) { 127 content = content.replace (" ", " "); 128 } 129 130 content = content.strip (); 131 } 132 133 public Text.create_copy (Text c) { 134 Object.copy_attributes (c, this); 135 font_family = c.font_family; 136 x = c.x; 137 y = c.y; 138 font_size = c.font_size; 139 } 140 141 public override void draw_outline (Context cr) { 142 cr.save (); 143 cr.translate (x, y); 144 145 if (font != null) { 146 svg_bird_draw_text (cr, font, content); 147 } else { 148 warning ("No font."); 149 } 150 151 cr.restore (); 152 } 153 154 public override bool is_empty () { 155 return false; 156 } 157 158 public override Object copy () { 159 return new Text.create_copy (this); 160 } 161 162 public override string to_string () { 163 return "Text"; 164 } 165 } 166 167 } 168