Commit 34c422f4 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

find default font using fontconfig

parent 3c76bfaa
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ find_package(Threads)
find_package(Cairo)
find_package(Freetype)
find_package(HarfBuzz)
find_package(Fontconfig)
find_package(PNG)
include_directories(${CAIRO_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_INCLUDE_DIRS} ${PNG_INCLUDE_DIRS})

@@ -63,7 +64,7 @@ add_library(plotfxlib STATIC
    common/utils/UTF8.cc
    common/utils/wallclock.cc)

set(PLOTFX_LDFLAGS plotfxlib ${CAIRO_LIBRARIES} ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES} ${HARFBUZZ_ICU_LIBRARIES} ${PNG_LIBRARIES})
set(PLOTFX_LDFLAGS plotfxlib ${CAIRO_LIBRARIES} ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES} ${HARFBUZZ_ICU_LIBRARIES} ${PNG_LIBRARIES} ${FONTCONFIG_LIBRARIES})

add_executable(plotfx common/platform/plotfx_cli.cc)
target_link_libraries(plotfx ${PLOTFX_LDFLAGS})
+0 −10
Original line number Diff line number Diff line
@@ -25,13 +25,3 @@ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


INCLUDED FONTS
--------------

Please note that PlotFX includes third party default fonts in extra/fonts. These
are copyright of their respective authors and are licensed under different
conditions. Please see the LICENSE files in the extra/fonts directory for more
information.
+4 −6
Original line number Diff line number Diff line
@@ -56,14 +56,12 @@ ReturnCode document_setup_defaults(Document* doc) {
        path_parts.end());
  }


  static const auto default_font_sans = "Roboto-Medium";
  if (!findFontSimple(default_font_sans, doc->font_searchpath, &doc->font_sans)) {
  static const auto default_font_sans = "Arial,Helvetica:style=Regular,Roman";
  if (!findFontSystem(default_font_sans, &doc->font_sans)) {
    return ReturnCode::errorf(
        "EARG",
        "unable to find default sans-sans font ($0); searched in: $1",
        default_font_sans,
        StringUtil::join(doc->font_searchpath, ", "));
        "unable to find default sans-sans font ($0)",
        default_font_sans);
  }

  return OK;
+36 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <iostream>
#include <fontconfig/fontconfig.h>
#include "font_lookup.h"
#include "utils/fileutil.h"

@@ -50,5 +51,40 @@ bool findFontSimple(
  return false;
}

bool findFontSystem(
    const std::string& font_pattern,
    FontInfo* font_file) {
  std::string file;

  {
    auto fc_config = FcInitLoadConfigAndFonts();
    auto fc_pattern = FcNameParse((FcChar8*) font_pattern.c_str());
    FcDefaultSubstitute(fc_pattern);
    FcConfigSubstitute(fc_config, fc_pattern, FcMatchPattern);
    FcResult fc_res;
    auto fc_font = FcFontMatch(fc_config, fc_pattern, &fc_res);
    if (fc_font && fc_res == FcResultMatch) {
      char* fc_file;
      if (FcPatternGetString(fc_font, FC_FILE, 0, (FcChar8**) &fc_file) == FcResultMatch) {
        file = std::string(fc_file);
      }
      FcPatternDestroy(fc_font);
    }
    FcPatternDestroy(fc_pattern);
    FcConfigDestroy(fc_config);
  }

  if (file.empty()) {
    return false;
  }

  *font_file = FontInfo {
    .font_file = file
  };

  return true;
}


} // namespace plotfx
+4 −0
Original line number Diff line number Diff line
@@ -38,5 +38,9 @@ bool findFontSimple(
    const std::vector<std::string>& search_paths,
    FontInfo* font_file);

bool findFontSystem(
    const std::string& font_pattenr,
    FontInfo* font_file);

} // namespace plotfx
Loading